3 # Simple autoreply script
5 # read qmail module config
6 $p = -l $0 ? readlink($0) : $0;
7 $p =~ /^(.*)\/[^\/]+$/;
8 open(CONF, "$1/config") || die "Failed to open $1/config";
16 # read headers and body
19 if (/^(\S+):\s+(.*)/) {
27 if ($header{'x-webmin-autoreply'}) {
28 print STDERR "Cancelling autoreply to an autoreply\n";
31 if ($header{'x-mailing-list'} ||
33 $header{'precedence'} =~ /junk|bulk|list/i ||
34 $header{'to'} =~ /Multiple recipients of/i) {
35 # Do nothing if post is from a mailing list
38 if ($header{'from'} =~ /postmaster|mailer-daemon/i) {
39 # Do nothing if post is a bounce
43 # work out the correct to address
44 @to = ( &split_addresses($header{'to'}),
45 &split_addresses($header{'cc'}),
46 &split_addresses($header{'bcc'}) );
49 if ($t->[0] =~ /^([^\@\s]+)/ && $1 eq $ARGV[1]) {
54 # build list of default reply headers
55 $rheader{'From'} = $to;
56 $rheader{'To'} = $header{'reply-to'} ? $header{'reply-to'}
58 $rheader{'Subject'} = "Autoreply to $header{'subject'}";
59 $rheader{'X-Webmin-Autoreply'} = 1;
60 $rheader{'X-Originally-To'} = $header{'to'};
62 # read the autoreply file
63 if (open(AUTO, $ARGV[0])) {
65 s/\$SUBJECT/$header{'subject'}/g;
66 s/\$FROM/$header{'from'}/g;
68 s/\$DATE/$header{'date'}/g;
70 if (/^(\S+):\s*(.*)/ && !$doneheaders) {
81 $rbody = "Failed to open autoreply file $ARGV[0] : $!";
84 # Open the replies tracking DBM, if one was set
85 if ($rheader{'Reply-Tracking'}) {
86 $track_replies = dbmopen(%replies, $rheader{'Reply-Tracking'}, 0700);
89 # See if we have replied to this address before
90 $period = $rheader{'Reply-Period'} || 60*60;
91 ($from) = &split_addresses($header{'from'});
93 $lasttime = $replies{$from->[0]};
95 if ($now < $lasttime+$period) {
96 # Autoreplied already in this period .. just halt
99 $replies{$from->[0]} = $now;
102 delete($rheader{'Reply-Tracking'});
103 delete($rheader{'Reply-Period'});
105 # run qmail and feed it the reply
106 open(MAIL, "|$config{'qmail_dir'}/bin/qmail-inject");
107 foreach $h (keys %rheader) {
108 print MAIL "$h: $rheader{$h}\n";
114 # split_addresses(string)
115 # Splits a comma-separated list of addresses into [ email, real-name ] pairs
118 local (@rv, $str = $_[0]);
120 if ($str =~ /^[\s,]*(([^<>\(\)\s]+)\s+\(([^\(\)]+)\))(.*)$/) {
121 push(@rv, [ $2, $3, $1 ]);
124 elsif ($str =~ /^[\s,]*("([^"]+)"\s+<([^\s<>]+)>)(.*)$/ ||
125 $str =~ /^[\s,]*(([^<>]+)\s+<([^\s<>]+)>)(.*)$/ ||
126 $str =~ /^[\s,]*(([^<>\[\]]+)\s+\[mailto:([^\s\[\]]+)\])(.*)$/||
127 $str =~ /^[\s,]*(()<([^\s<>]+)>)(.*)/ ||
128 $str =~ /^[\s,]*(()([^\s<>,]+))(.*)/) {
129 push(@rv, [ $3, $2, $1 ]);