-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmpostauth-handler
executable file
·82 lines (71 loc) · 2.28 KB
/
mmpostauth-handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/perl
use Getopt::Long;
use LWP::UserAgent;
use strict;
use open IO => ':bytes';
use constant SUCCESS => 0;
use constant TEMPORARY_FAILURE => 75;
our $URL;
our $HOST = 'localhost';
our $FROM;
our $TO;
our $DEBUG;
&GetOptions( 'url=s' => \$URL,
'host=s' => \$HOST,
'from=s' => \$FROM,
'to=s' => \$TO,
'debug!' => \$DEBUG );
exit( &post_message( &read_message() ) );
sub post_message {
my( $msg ) = @_;
my $ua = LWP::UserAgent->new( keep_alive => 1, timeout => 60 );
if( my $res = $ua->post( $URL,
Content_Type => 'form-data',
Content => [ message => [ undef,
'message',
Content_type => 'message/rfc822',
Content => $msg ] ] ) ){
print $res->as_string() if $DEBUG;
if( $res->is_success ){
# When `action' CGI script returns 200 status code,
# it means everything goes well. Thus, we can remove this
# message from MTA's queue.
return SUCCESS;
} elsif( $res->code =~ m/\A4\d{2}\Z/ ){
# When `action' CGI script returns 4XX status code,
# it means non-recoverable failure.
if( $HOST and $FROM and $TO ){
my $smtp = Net::SMTP->new( $HOST );
$smtp->mail( $FROM ) && $smtp->to( $TO ) && $smtp->data( $msg );
}
# For non-recoverable failure, we have to ignore this
# message inevitably.
return SUCCESS;
}
}
# When `action' CGI script returns other error status code,
# it means recoverable failure. We must keep this message in
# MTA's queue.
TEMPORARY_FAILURE;
}
sub read_message {
my @buf;
while( <> ){
push( @buf, $_ );
}
join( '', @buf );
}
=head1 AUTHOR
TSUCHIYA Masatoshi <tsuchm@gmail.com>
=head1 COPYRIGHT
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
=cut