&audit_sms: Stripping improved
[PerlMail.git] / perlmail-accept
1 #! /usr/bin/perl
2 #
3 # $Id$
4
5 use vars qw($VERSION);
6 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
7 use strict;
8 use warnings;
9
10
11 INIT {
12         require Sys::Syslog;
13         Sys::Syslog::openlog("lacemail","pid","mail");
14         my @syslogging_stack;
15         sub syslogging_on_save
16         {
17                 push @syslogging_stack,$SIG{"__WARN__"},$SIG{"__DIE__" };
18                 $SIG{"__WARN__"}=sub { Sys::Syslog::syslog("warning","WARN: %s",$_[0]); };      # disabled: print STDERR $_[0];
19                 $SIG{"__DIE__" }=sub { Sys::Syslog::syslog("crit"   ,"DIE: %s" ,$_[0]); };
20         }
21         syslogging_on_save();
22         sub syslogging_restore
23         {
24                 $SIG{"__DIE__" }=pop @syslogging_stack;
25                 $SIG{"__WARN__"}=pop @syslogging_stack;
26         }
27         }
28
29
30 use Mail::Audit qw(MAPS);
31 require IO::Handle;
32 use Carp qw(cluck confess);
33 use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED WSTOPSIG);
34 require POSIX;  # for ceil
35 use User::Utmp;
36 require File::Basename;
37 use Getopt::Long;
38 require Mail::Address;
39 require MIME::Words;
40 require Cz::Cstocs;
41 require HTML::Entities;
42 require MIME::Head;
43 require Lingua::EN::Squeeze;
44 require Mail::Mailer;
45 require HTTP::Cookies;
46 require HTTP::Request;
47 require LWP::UserAgent;
48 use URI::Escape 'uri_escape';
49
50
51 my $HOME="/home/lace";
52 my $Mail="$HOME/Mail";
53 my @ValidUsers=qw(root lace short kratochvil _local);
54 my $IdleMax=60;
55 my $MaxBodySMS=0x1000;  # max bytes to pass to Lingua::EN::Squeeze
56 my $SMSmailError='short+err@ucw.cz';
57 my $SMSwebRcpt='00420602431329';
58 my $SMSmailRcpt=$SMSwebRcpt.'@sms.eurotel.cz';
59 my $SMScontact='<short@ucw.cz>';
60
61 our($Message,$Audit,@AuditStored,$store_ignore,$store_ignorenewmail,$store_profile,$DoBell);
62 our(%audit_profile,@sms_squeezes,@alternates_host);     # imported
63 my %alternates_host;    # from @alternates_host
64
65 # from RedHat "procmail-3.22-5"
66 # /i should be only $procmailFROM_DAEMON but how it can hurt to /i all?
67 our $procmailTO_        =qr'^((Original-)?(Resent-)?(To|Cc|Bcc)|(X-Envelope|Apparently(-Resent)?)-To):(.*[^-a-zA-Z0-9_.])?'mio;
68 our $procmailTO         =qr'^((Original-)?(Resent-)?(To|Cc|Bcc)|(X-Envelope|Apparently(-Resent)?)-To):(.*[^a-zA-Z])?'mio;
69 our $procmailFROM_DAEMON=qr'^(Mailing-List:|Precedence:.*(junk|bulk|list)|To: Multiple recipients of |(((Resent-)?(From|Sender)|X-Envelope-From):|>?From )([^>]*[^(.%@a-z0-9])?(Post(ma?(st(e?r)?|n)|office)|(send)?Mail(er)?|daemon|m(mdf|ajordomo)|n?uucp|LIST(SERV|proc)|NETSERV|o(wner|ps)|r(e(quest|sponse)|oot)|b(ounce|bs\.smtp)|echo|mirror|s(erv(ices?|er)|mtp(error)?|ystem)|A(dmin(istrator)?|MMGR|utoanswer))(([^).!:a-z0-9][-_a-z0-9]*)?[%@>        ][^<)]*(\(.*\).*)?)?$([^>]|$))'mio;
70 our $procmailFROM_MAILER=qr'^(((Resent-)?(From|Sender)|X-Envelope-From):|>?From )[^>]*\b(Post(ma(st(er)?|n)|office)|(send)?Mail(er)?|daemon|mmdf|n?uucp|ops|r(esponse|oot)|(bbs\.)?smtp(error)?|s(erv(ices?|er)|ystem)|A(dmin(istrator)?|MMGR))(([^).!:a-z0-9][-_a-z0-9]*)?[%@>  ][^<)]*(\(.*\).*)?)?$([^>]|$)'mio;
71 # perl-5.8.0 does not cope w/original FROM_MAILER on the third '?' character
72 # Thus we did '([^>]*[^(.%@a-z0-9])?' -> '[^>]*\b', I hope it is somehow similiar
73 # original FROM_MAILER  =qr'^(((Resent-)?(From|Sender)|X-Envelope-From):|>?From )([^>]*[^(.%@a-z0-9])?(Post(ma(st(er)?|n)|office)|(send)?Mail(er)?|daemon|mmdf|n?uucp|ops|r(esponse|oot)|(bbs\.)?smtp(error)?|s(erv(ices?|er)|ystem)|A(dmin(istrator)?|MMGR))(([^).!:a-z0-9][-_a-z0-9]*)?[%@>    ][^<)]*(\(.*\).*)?)?$([^>]|$)'mio;
74
75 my $opt_mode;
76 my $opt_smstest;        # 1 or $smscount
77 my $opt_idle;
78
79
80 sub process;
81
82 sub stdin
83 {
84         syslogging_restore();   # This is more a debugging session
85         local $/="\n";
86         my $message="";
87         local $_;
88         while (<>) {
89                 die "Invalid 'From ' line: $_" if $message eq "" && !/^From /;
90                 if (/^From / && $message) {
91                         process $message;
92                         $message="";
93                         }
94                 $message.=$_;
95                 }
96         process $message if $message;
97         exit 0;
98 }
99
100 # FIXME: separate 'lacemail'-transfer together with lacemail-submit away
101 sub inetd
102 {
103         die "Excessive arguments" if @ARGV;
104
105         IO::Handle::autoflush STDOUT 1;
106
107         while (1) {
108                 local $/="\n";
109                 my $length=<STDIN>;
110                 confess "Unexpected EOF" if !defined $length;
111                 confess "Missing EOL" if $length!~s/\n$//s;
112                 exit 0 if $length eq "BYE";
113                 confess "Unrecognized length: $length" if $length!~/^\d+$/;
114                 my $message;
115                 local $_;
116                 $length==($_=read STDIN,$message,$length) or confess "Got $_ out of required $length bytes";
117                 $length==length $message or confess "False read return ".length($message)." instead of $length";
118                 {
119                         local *STDOUT;
120                         local *STDERR;
121                         local $DoBell=0;
122                         process $message;
123                         if ($DoBell) {
124                                 bell() or warn "Unable to BELL";
125                                 }
126                         }
127                 print STDOUT "1";
128                 }
129         die "NOTREACHED";
130 }
131
132 sub bell
133 {
134         local *BELL;
135         open BELL,">/dev/tty11" or return 0;
136         print BELL "\x07";
137         close BELL or return 0;
138         return 1;
139 }
140
141 sub useridle
142 {
143         my %valid_users=map(($_=>1),@ValidUsers);
144         my($idlebest,$linebest);
145         for my $utmp (User::Utmp::getut(),{ "ut_line"=>"psaux" }) {
146                 local $_;
147                 next if defined($_=$utmp->{"ut_type"}) && $_!=User::Utmp::USER_PROCESS;
148                 next if defined($_=$utmp->{"ut_user"}) && !$valid_users{$_};
149                 my $line="/dev/".$utmp->{"ut_line"};
150                 my $atime=(stat $line)[8];
151                 my $what="user \"".($utmp->{"ut_user"} || "<local>")."\", line \"$line\"";
152                 warn "Unable to stat $what" and next if !$atime;
153                 my $idle=time()-$atime;
154                 warn "atime in future for $what" and next if $idle<0;
155                 next if $idle>$IdleMax;
156                 next if defined $idlebest && $idlebest<=$idle;
157                 $idlebest=$idle;
158                 $linebest=$line;
159                 }
160         return !wantarray() ? $idlebest : ($idlebest,$linebest);
161 }
162
163 # return only the very (recursive) first part
164 sub body_first
165 {
166         return $Audit if !$Audit->is_mime();
167         my $first=$Audit;
168         local $_;
169         $first=$_ while $_=$first->parts(0);
170         return $first;
171 }
172
173 sub mimehead
174 {
175 my($part)=@_;
176
177         return $Audit->is_mime() ? $part->head()
178                         : MIME::Head->new([ split "\n",$Audit->head()->as_string() ])
179                         ;
180 }
181
182 sub mimebody
183 {
184 my($part)=@_;
185
186         # be vary cautious here as most of $part methods will encode it!
187         return join "",@{$Audit->body()} if !$Audit->is_mime();
188         my $bodyhandle=$part->bodyhandle();
189         # If MIME is corrupted we don't get bodyhandle() for this part
190         # It may occur when "boundary" is specified by header but no such boundary is found in the body
191         return $bodyhandle->as_string() if $bodyhandle;
192         warn "MIME corrupted, adapting";
193         return $part->body_as_string();
194 }
195
196 sub mime_type
197 {
198 my($part)=@_;
199
200         return $Audit->is_mime() ? $part->effective_type() : mimehead($part)->mime_type();
201 }
202
203 sub body_simple
204 {
205         my $first=body_first();
206         my $r=mimebody($first);
207         my $mime_type=mime_type($first);
208            if ($mime_type eq "text/html") {
209                 # HTML::FormatText just does a useless text layouts
210                 # PerlIO::via::StripHTML probably needs PerlIO input (?)
211                 $r=~s/<[^>]*>//gs;
212                 $r=HTML::Entities::decode($r);
213                 # FIXME: detect charset from <meta> tag: "Content-type: text/html; charset=<???>"
214                 }
215         elsif ($mime_type eq "application/pgp-encrypted"
216                && (my $filename=mimehead($first)->mime_attr("Content-Disposition.filename"))
217                ) {
218                 # first part contains just "Version: 1" as of GnuPG v1.0.4 (GNU/Linux)
219                 $r="pgp($filename)";
220                 }
221         if ((my $charset=mimehead($first)->mime_attr("Content-Type.charset"))) {
222                 my $cstocs=Cz::Cstocs->new($charset,"ascii");
223                 $r=&$cstocs($r) if $cstocs;     # charset may be unknown
224                 }
225         return $r;
226 }
227
228 sub parts_linear
229 {
230 my($part)=@_;
231
232         return $Audit if !$part && !$Audit->is_mime();
233         $part||=$Audit;
234         # don't use '!$part->parts()' as even 0-parts-multiparts are still multiparts
235         return $part if $part->bodyhandle();
236         return map { (parts_linear($_)); } $part->parts();
237 }
238
239 sub smsbuild
240 {
241 my($smsi,$smscount)=@_;
242
243         return "$smsi/$smscount:" if $smscount>1;
244         return "";
245 }
246
247 # FIXME: rewrite &send_cz_eurotel properly by own code
248 # patch for http://kiwi.ms.mff.cuni.cz/%7Etom/programming/src/sendsms.tar.gz/sendsms.pl
249 my $agent=LWP::UserAgent->new();
250 $agent->agent("LaceMail $VERSION; contact=$SMScontact; ");
251 my($request1,$response1);       # for &send_cz_eurotel
252 my($name,$value,$type,$disabled,$q2);
253
254 # &send_cz_eurotel returns: error
255 # BEGIN http://kiwi.ms.mff.cuni.cz/%7Etom/programming/src/sendsms.tar.gz/sendsms.pl
256 sub parse_inputs
257 {
258         my ($resp) = @_;
259         my @inputs;
260         my $ct;
261         my @c;
262         $ct=$resp->content();
263
264         @c=split '>', $ct;
265         grep {
266            if (/(<input|<select|<textarea)([^<>]*)(>|$)/i) { 
267              my $txt=$2, $name="", $value="", $type="x", $disabled=0;
268              my $ipoc;
269              if ($txt =~ /type="([^"]*)"/i) { $type=$1; }
270              elsif ($txt =~ /type=([^" ]*)[ >]/i) { $type=$1; }
271              if ($txt =~ /name="([^"]*)"/i) { $name=$1; }
272              elsif ($txt =~ /name=([^" ]*)[ >]/i) { $name=$1; }
273              if ($txt =~ /value="([^"]*)"/i) { $value=$1; }
274              elsif ($txt =~ /value=([^" ]*)[ >]/i) { $value=$1; }
275              if ($txt =~ /disabled/i) { $disabled=1; }
276              if ($name ne "" && $type ne "" && $type ne "button" && ! $disabled)
277              {
278                $ipoc=$#inputs;
279                $inputs[$ipoc+1][0]=$name;
280                $inputs[$ipoc+1][1]=$value;
281              }
282            }
283         0; } @c;
284         return @inputs;
285 }
286
287 sub make_query
288 {
289         my (@inputs) = @_;
290
291         my $i;
292         my $query = "";
293
294         for ($i=0; $i<=$#inputs; $i++)
295         {
296           my($q1, $q2);
297           if ($i>0) { $query="$query&"; }
298           $q1=uri_escape($inputs[$i][0]);
299           $q2=uri_escape($inputs[$i][1]);
300           $query="$query$q1=$q2";
301         }
302
303         #change @ and space back
304         $query=~ s/%20/+/g;
305         $query=~ s/%40/@/g;
306         return $query;
307 }
308
309 sub send_cz_eurotel
310 {
311         my ($id,$text,$mail,$directd) = @_;
312         my $src_url = "http://www2.eurotel.cz/sms/index.html";
313         my @inputs;
314         my $query = "";
315         my $cookie = HTTP::Cookies->new;
316         my $pref;
317
318         #check if correct number
319         if (substr($id,0,5)!="00420") { return -1; }
320         $pref=substr($id,5,3);
321         if (!($pref eq "601" || $pref eq "602" || $pref eq "606" || $pref eq "607"  || ($pref ge "720" && $pref le "729"))) { return -1; }
322         
323         #get form page, extract cookies
324         $request1=new HTTP::Request('GET', "$src_url?n_pagestyle=new");
325         $response1=$agent->request($request1);
326         if ($response1->code != 200) { return -3; }
327         $cookie->extract_cookies($response1);
328
329         #parse the form
330         @inputs=parse_inputs($response1);
331         
332         #fill the form
333         $inputs[2][1]=substr($id,5,3);
334         $inputs[3][1]=substr($id,8,6);
335         $inputs[4][1]=$mail;
336
337         #direct display
338         $inputs[6][1]="sms";
339         $inputs[6][1]="show" if ($directd>0);
340
341         $inputs[7][1]=$text;
342
343 #       for ($i=0; $i<=$#inputs; $i++) { print "[$i] $inputs[$i][0] $inputs[$i][1]\n"; } 
344
345         #make query
346         $query=make_query(@inputs);
347
348         #POST the form
349         my $header = new HTTP::Headers( 
350                 'Content-Length' => length($query),
351                 'Content-Type' => 'application/x-www-form-urlencoded',
352                 'Accept' => '*/*',
353                 'Referer' => $src_url
354         );
355         my $request2 = new HTTP::Request('POST',$src_url, $header, $query);
356         $cookie->add_cookie_header($request2);
357         my $response2 = $agent->request($request2);
358
359         if ($response2->code != 200) { return -3; }
360
361         #check for success
362         if ($response2->content() !~ /byla.*odesl.*na na SMS centrum/)
363         {
364           return -2;
365         }
366         return 0;
367 }
368 # END http://kiwi.ms.mff.cuni.cz/%7Etom/programming/src/sendsms.tar.gz/sendsms.pl
369
370 sub smslens
371 {
372 my($ignorenewmail,$smscount,%args)=@_;
373
374         return map({
375                         my $l=160;
376                         if (!$ignorenewmail) {  # send by mail
377                                 $l-=length("Z emailu $SMSmailError: ");
378                                 $l-=length(smsbuild($_,$smscount));
379                                 }
380                         else {  # send by web
381                                 $l-=length("Z WWW x/5: ");
382                                 $l-=length(smsbuild($_,POSIX::ceil($smscount/5)));
383                                 }
384                         $l;
385                         } (0..$smscount-1));
386 }
387
388 sub smssend_web
389 {
390 my($squeezed,$smscount,@lens)=@_;
391
392         $smscount=POSIX::ceil($smscount/5);
393         for my $smsi (0..$smscount-1) {
394                 my $len=$lens[$smsi];
395                 $squeezed=~/^.{0,$len}/s;
396                 my $frag=$&;
397                 $squeezed=$';
398                 return 0 if send_cz_eurotel($SMSwebRcpt,$frag,"",0);
399                 }
400         return 1;
401 }
402
403 sub smssend_mail
404 {
405 my($squeezed,$smscount,@lens)=@_;
406
407         my $recalclen=0;
408         for ($smscount=0;$recalclen<length $squeezed;$smscount++) {
409                 $recalclen+=$lens[$smscount];
410                 }
411         for my $smsi (0..$smscount-1) {
412                 my $len=$lens[$smsi];
413                 $squeezed=~/^.{0,$len}/s;
414                 my $frag=$&;
415                 $squeezed=$';
416                 my $mail=Mail::Mailer->new("sendmail","-f","$SMSmailError");
417                 $mail->open({
418                                 "To"=>$SMSmailRcpt,
419                                 "From"=>$SMSmailError,  # no longer displayed anyway
420                                 "X-LaceMail-Version"=>$VERSION,
421                                 "X-LaceMail-Contact"=>$SMScontact,
422                                 });
423                 print $mail smsbuild($smsi,$smscount).$frag."\n";
424                 # FIXME: check errors
425                 $mail->close();
426                 }
427         return 1;
428 }
429
430 sub smssend
431 {
432 my($ignorenewmail,$smscount,%args)=@_;
433
434         my $text=audit_sms(
435                         "subject"=>unmime($Audit->subject()),
436                         "from"=>[ Mail::Address->parse(unmime($Audit->from())) ],
437                         "body"=>substr(body_simple(),0,$MaxBodySMS*(1+0.25*$smscount)),
438                         %args);
439         my $texthead="";
440         ($texthead,$text)=@$text if ref $text;
441         do { print "$texthead\n$text\n"; return; } if $opt_smstest;
442         my @lens=smslens($ignorenewmail,$smscount,%args);
443         my $maxlen=0;
444         $maxlen+=$_ for (@lens);
445         my $squeezed;
446         for my $squeeze (@sms_squeezes) {
447                 local $_;
448                  Lingua::EN::Squeeze::SqueezeControl($_)    if defined ($_=$squeeze->{"SqueezeControl"});
449                 $Lingua::EN::Squeeze::SQZ_OPTIMIZE_LEVEL or 1;  # prevent: Name "$_" used only once: possible typo
450                 $Lingua::EN::Squeeze::SQZ_OPTIMIZE_LEVEL=$_ if defined ($_=$squeeze->{"SQZ_OPTIMIZE_LEVEL"});
451                 $squeezed=Lingua::EN::Squeeze::SqueezeText($text);
452                 chomp $squeezed;
453                 last if $maxlen>=length($texthead.$squeezed);
454                 }
455         $squeezed=substr $texthead.$squeezed,0,$maxlen; # strip if we passed thru last for() above
456         my $recalclen=0;
457         for ($smscount=0;$recalclen<length $squeezed;$smscount++) {
458                 $recalclen+=$lens[$smscount];
459                 }
460         my $func=($ignorenewmail ? \&smssend_web : \&smssend_mail);
461         &$func($squeezed,$smscount,@lens);
462 }
463
464 sub smssend_tryall
465 {
466 my($ignorenewmail,@args)=@_;
467
468         return if !$opt_smstest && !$opt_idle && defined useridle();
469         local $_;
470         return $_ if                     $_=smssend(1,@args);   # web
471         return $_ if !$ignorenewmail && ($_=smssend(0,@args));  # mail
472         warn "Unable to SMSsend the mail";
473         return 0;
474 }
475
476 sub cut
477 {
478         local $_=$_[0];
479         return "<???>" if !defined($_) || /^\s*$/s;
480         s/^\s*//s;
481         s/\s*$//s;
482         return $_ if length($_)<128;
483         return substr($_,0,128)."...";
484 }
485
486 our $profile_eval_depth=0;
487 # ($name || @$name)
488 sub profile_eval
489 {
490 my($name)=@_;
491
492         die "Nesting profile: $name" if 0x10<=(local $profile_eval_depth=$profile_eval_depth+1);
493         return @$name if ref $name;
494         die "Profile not found: $name" if !exists $audit_profile{$name};
495         my @this=@{$audit_profile{$name}};
496         return (profile_eval($'),@this[1..$#this]) if $this[0] && $this[0]=~/^=/;
497         return @this;
498 }
499
500 sub address_show
501 {
502 my($text)=@_;
503
504         return join(",",map({ $_->name() or $_->address(); } Mail::Address->parse($text)));
505 }
506
507 sub unmime
508 {
509 my($text)=@_;
510
511         return join "",map({
512                         my $cstocs;
513                         for (${$_}[1],"iso-8859-2") {
514                                 last if $_ && ($cstocs=Cz::Cstocs->new($_,"ascii"));
515                                 }
516                         &$cstocs(${$_}[0]);
517                         } MIME::Words::decode_mimewords($text));
518 }
519
520 # $folder: "$folder; comment"
521 # $profile as profile_eval($name)
522 sub store
523 {
524 my($folder,$profile,%args)=@_;
525
526         $profile=$store_profile if !$profile;
527         my %do=map({ (!/=/ ? ($_=>1) : ($`=>$')); } profile_eval($profile));
528         Sys::Syslog::syslog("info","%s%s: %s: %s",
529                                         (!$store_ignore ? "" : "IGNORED[$store_ignore]: "),
530                                         map({ cut($_); } $folder,address_show(unmime($Audit->from())),unmime($Audit->subject())),
531                                         )
532                         if $do{"syslog"};
533         $folder=~s/;.*$//s;
534         $folder="$Mail/".$' if $folder=~/^=/;
535         push @AuditStored,$folder if $do{"did"};
536         return if $store_ignore;
537         $DoBell++ if $do{"bell"};
538         write_message($folder);
539         smssend_tryall $store_ignorenewmail,$do{"sms"},%args if $do{"sms"};
540 }
541
542 our $did_last=0;
543
544 # no &$funcref=>did smth in this block
545 # &$funcref,@funcargs
546 sub did
547 {
548 my($funcref,@funcargs)=@_;
549
550         return @AuditStored!=$did_last if !$funcref;
551         local $did_last=@AuditStored;
552         &$funcref(@funcargs);
553         return @AuditStored!=$did_last;
554 }
555
556 # Never use Mail::Audit->store() as it will reformat MIME bodies and possibly corrupt OpenPGP!
557 sub write_message
558 {
559 my($folder)=@_;
560
561         local *F;
562         open F,">>$folder" or do { warn "Append \"$folder\": $!"; return 0; };
563         {
564                 local $_;
565                 ($_=Mail::Audit::audit_get_lock(\*F,$folder)) and do { warn "Lock \"$folder\": $!"; last; };
566                 seek F,0,IO::Handle::SEEK_END or do { warn "Seek-end \"$folder\": $!"; last; };
567                 # FIXME: Check for '^From ' to not to rely on our network peer
568                 print F $Message or do { warn "Write to \"$folder\": $!"; last; };
569                 do { print F "\n"; warn "Missing trailing newline, fixed"; } if $Message!~/\n$/s;
570                 close F or do { warn "Close \"$folder\""; last; };
571                 return 1;       # OK
572                 }
573         warn "MAIL DROPPED for folder: $folder";
574         close F;
575         return 0;       # failed
576 }
577
578 sub process
579 {
580 my($message)=@_;
581
582         local $_=$_;
583         my $save_=$_;
584         local $Message=$message;
585         local $Audit=Mail::Audit->new(
586                         "emergency"=>"$Mail/emergency",
587                         "data"=>[map("$_\n",split("\n",$message))],
588                         "log"=>"$HOME/.lacemail.log",
589                         "loglevel"=>99,
590                         );
591         local @AuditStored=();
592         do { smssend 0,$opt_smstest; return; } if $opt_smstest;
593         write_message("$Mail/input");
594         audit();
595         warn 'Corrupted $_, repaired' if defined($save_)!=defined($_) || (defined($_) && $save_ ne $_);
596 }
597
598 # utility functions:
599
600 # return: true (error-message or "1") if is spam
601 sub razor2
602 {
603         # razor-check has exit code 1 if NOT spam, code 0 if IS spam
604         local *CHILD;
605         local $SIG{"PIPE"}=sub { warn "razor2 gave me SIGPIPE: broken pipe"; };
606         # prevent Razor2's: Can't call method "log" on unblessed reference at Razor2/Client/Agent.pm line 212.
607         local $ENV{"HOME"}=$HOME;
608         open CHILD,'|'
609                                         .'('.'(razor-check 2>&1;echo >&3 $?)'
610                                                         .'|sed "s/^/razor-check: /"'
611                                                         .'|logger -t "lacemail['.$$.']" -p mail.crit'
612                                                         .') 3>&1'
613                                         .'|exit `cat`'
614                         or return 0;
615         print CHILD $Message;
616         my $return;
617         {
618                 local $/=undef();
619                 $return=<CHILD> || 1;
620                 }
621         close CHILD;
622         return undef() if !WIFEXITED($?);
623         return undef() if  WIFSIGNALED($?);
624         return undef() if  WIFSTOPPED($?);
625         return undef() if WEXITSTATUS($?);
626         return $return; # is-spam
627 }
628
629 # NOTE: returns undef() if !wantarray and the first header is unrecognized
630 # Returns also hosts
631 sub Received_for
632 {
633         my @r=();
634         for my $hdr ($Audit->head->get("Received")) {
635                 my($for)=($hdr=~/\bfor\s+\<?(\S+)\>?\b/);
636                 return $for if !wantarray();
637                 push @r,$for if $for;
638                 my($from,$fromaddr)=($hdr=~/\bfrom\s+(\S+)\b.*?\[((?:\d{1,3}\.){3}\d{1,3})\]/);
639                 push @r,"$from:$fromaddr" if $from;
640                 }
641         return @r;
642 }
643
644 # Extended Mail::Audit::MAPS
645 # $domain,$full,[$timeout]
646 sub dnsbl
647 {
648 my($domain,$full,$timeout)=@_;
649
650         $timeout||=30;  # sec
651         $Mail::Audit::MAPS::host=$domain;
652         my @hosts=map({ s/^.*://; "[$_]"; }     # strip DNS part
653                         grep({ /^([^:@]*):/ && !$alternates_host{$1}; } (Received_for()))       # leave only foreign hosts
654                         );
655         splice @hosts,1 if !$full && @hosts;    # "&& @hosts" to prevent: WARN: splice() offset past end of array
656         {
657                 package My::Audit::Faked;
658                 sub received { return @{$_[0]->{"received"}}; }
659                 }
660         my $self_faked={
661                         "received"=>[@hosts],
662                         };
663         bless $self_faked,"My::Audit::Faked";
664         return Mail::Audit::rblcheck($self_faked,$timeout);
665 }
666
667 our %muttrc_pending=();
668 sub muttrc
669 {
670 my($muttrc)=@_;
671
672         $muttrc||="$HOME/.muttrc";
673         $muttrc=~s/^\~/$HOME/;
674         do { warn "Looping muttrc, ignoring: $muttrc"; return (); } if $muttrc_pending{$muttrc};
675         local $muttrc_pending{$muttrc}=1;
676         local *MUTTRC;
677         open MUTTRC,$muttrc or do { warn "open \"$muttrc\": $!"; return (); };
678         local $/="\n";
679         local $_;
680         my @r=();
681         # far emulation mutt/init.c/mutt_parse_rc_line()
682         while (<MUTTRC>) {
683                 s/^[\s;]*//s;
684                 s/[#;].*$//s;
685                 s/\s*$//s;
686                 next if !/^(\S+)\s*/s;
687                 if ($1 eq "source") {
688                         $_=$';
689                         do { warn "Wrong 'source' parameters at $muttrc:$.: $_"; next; } if !/^\S+$/;
690                         push @r,muttrc($_);
691                         next;
692                         }
693                 push @r,$_;
694                 }
695         close MUTTRC or warn "close \"$muttrc\": $!";
696         return wantarray() ? @r : join("",map("$_\n",@r));
697 }
698
699 my %mutteval_charmap=(          # WARNING: Don't use "" or "0" here, see below for "|| warn"!
700                 '\\'=>"\\",
701                 'r'=>"\r",
702                 'n'=>"\n",
703                 't'=>"\t",
704                 'f'=>"\f",
705                 'e'=>"\e",
706                 );
707 # mutt/init.c/mutt_extract_token()
708 sub mutteval
709 {
710         local $_=$_[0];
711         return $_ if !s/^"//;
712         do { warn "Missing trailing quote in: $_"; return $_; } if !s/"$//;
713         s/\\(.)/$mutteval_charmap{$1} || warn "Undefined '\\$1' sequence in: $_";/ges;
714         return $_;
715 }
716
717 sub muttrc_get
718 {
719 my(@headers)=@_;
720
721         my @r=map({ (ref $_ ? $_ : qr/^\s*set\s+\Q$_\E\s*=\s*(.*?)\s*$/si); } @headers);
722         my %r=map(($_=>undef()),@r);
723         for (muttrc()) {
724                 for my $ritem (@r) {
725                         /$ritem/si or next;
726                         $r{$ritem}=mutteval $1;
727                         }
728                 }
729         for my $var (grep { !defined($r{$_}) } @r) {
730                 warn "Variable '$var' not found in muttrc";
731                 return undef();
732                 }
733         return wantarray() ? %r : $r{$r[0]};
734 }
735
736 sub muttrc_aliases
737 {
738         my %r=();
739         for (muttrc()) {
740                 next if !(my $key=(/^alias\s+(\S+)\s+/)[0]);
741                 for my $addrobj (Mail::Address->parse($')) {
742                         my $addr=$addrobj->address();
743                         my $ref=\$r{"\L$addr"};
744                         $$ref=$key if !$$ref;   # use always the first occurence to prefer nicks
745                         }
746                 }
747         return %r;
748 }
749
750 # FIXME: Unify
751 # BEGIN lacemail-sendmail
752 # return: Mail::Address instance or undef()
753 sub parseone
754 {
755 my($line)=@_;
756
757         return undef() if !defined $line;
758         my @r=Mail::Address->parse($line);
759         warn "Got ".scalar(@r)." addresses while wanting just one; when parsing: $line" if 1!=@r;
760         return $r[0];
761 }
762 # END lacemail-sendmail
763
764 # FIXME: host may get multiple recipients and thus not showing "for <...>"
765 # FIXME: muttrc_get("from") is too strict
766 sub store_muttrc_alternates
767 {
768 my($prefix,$profile)=@_;
769
770         my $alternates=muttrc_get("alternates") or return;
771         my $alternatesre=qr/$alternates/si;
772         my $From=muttrc_get("from") or return;
773         my $Fromre=qr/^\Q$From\E$/si;
774         my $Fromobj=parseone $From or return;
775         warn "'From' \"$From\" not matched by 'alternates': $alternatesre"
776                         if $From!~/$alternates/si;
777         for my $for (reverse Received_for()) {
778                 $for=~s/:.*$//; # strip IP address here
779                 if ($Fromobj->user() ne "prog-mutt") {
780                         next if lc($for) eq lc($From);
781                         }
782                 else {
783                         my $forobj=parseone $for;
784                         if ($forobj && $forobj->host()) {
785                                 # it is 'for' our primary address
786                                 next if lc($forobj->host()) eq lc($Fromobj->host());    # or 'return'? shouldn't matter
787                                 }
788                         }
789                 next if !$alternates_host{lc $for} && $for!~/$alternatesre/si;
790                 store "$prefix\L$for",($profile || []);
791                 return;
792                 }
793 }
794
795 # $header: ref CODE
796 # $header: !ref => $Audit->get($header)
797 # $maybeaddress: qr/regex/i
798 # $maybeaddress: "string"
799 # $maybeaddress: "<Regexp:regex>"       # hack :-(
800 # $maybeaddress: "<user@host>"
801 # $maybeaddress: "<user@>"
802 # $maybeaddress: "<@host>"
803 sub _headercore
804 {
805 my($re,$justone,$header,$maybeaddress)=@_;
806
807         if (ref $header) {
808                 $header=join(",",&$header());
809                 }
810         else {
811                 $header=$Audit->get($header);
812                 }
813         return 0 if !$header;
814         return $header=~/$maybeaddress/i if "Regexp" eq ref $maybeaddress;
815         return $header=~/$re/i if !defined(my $want=($maybeaddress=~/^\<(.*)\>$/)[0]);
816         my @parsed=Mail::Address->parse($header);
817         warn "'mailto:' forbidden in pattern: $want" if $want=~/^\Qmailto:\E/;
818         return 0 if $justone && 1!=@parsed;
819         return grep {
820                            if ($want=~/^Regexp:/)
821                                 { $_->address()=~/$'/i; }
822                         elsif ($want=~/\@$/)
823                                 { $_->user()   =~/^(?:\Qmailto:\E)?\Q$`\E/i; }
824                         elsif ($want=~/^\@/)
825                                 { $_->host()   =~/^\Q$'\E/i; }
826                         else
827                                 { $_->address()=~/^(?:\Qmailto:\E)?\Q$want\E/i; }
828                         } @parsed;
829 }
830
831 sub headerhas
832 {
833 my($header,$substr)=@_;
834
835         return _headercore(qr/\Q$substr\E/i,0,$header,$substr);
836 }
837
838 sub headeris
839 {
840 my($header,$string)=@_;
841
842         return _headercore(qr/\Q$string\E/i,1,$header,$string);
843 }
844
845 # $header,%$map
846 sub header_remap
847 {
848 my($header,$map)=@_;
849
850         my $text=$Audit->get($header);
851         my $orig=$text;
852         while (my($from,$to)=each(%$map)) {
853                 $text=~s/\b\Q$from\E\b/$to/gsi;
854                 }
855         return if $text eq $orig;
856         $Audit->put_header("X-LaceMail-header_remap-$header",$orig);
857         $Audit->replace_header($header,$text);
858 }
859
860
861 # MAIN
862
863 my $basedir=File::Basename::dirname($0);
864 $Getopt::Long::ignorecase=0;
865 die "GetOptions error" if !Getopt::Long::GetOptions(
866                   "inetd"    ,sub { $opt_mode=\&inetd; },
867                   "stdin"    ,sub { $opt_mode=\&stdin; },
868                   "smstest:s",sub { $opt_mode=\&stdin; $opt_smstest=($_[1] || 1); },
869                   "idle!"    ,\$opt_idle,
870                   "idletest" ,sub { syslogging_restore(); print((defined($_=useridle()) ? $_ : "<undef>")."\n"); exit 0; },
871                   "muttrc"   ,sub { syslogging_restore(); print scalar muttrc(); exit 0; },
872                 "d|basedir=s",\&basedir,
873                 );
874 # "Excessive arguments" checked in &inetd
875 die "Missing mode" if !$opt_mode;
876
877 my $filenameMyAudit="$basedir/My-Audit.pm";
878 open AUDIT,$filenameMyAudit or die "open \"$filenameMyAudit\": $!";
879 {
880         local $/=undef();
881         eval <AUDIT> or die "eval \"$filenameMyAudit\": $@";
882         audit_init();
883         %alternates_host=map((lc($_)=>1),@alternates_host);
884         }
885 close AUDIT or warn "close \"$filenameMyAudit\": $!";
886
887 &$opt_mode();
888 die "NOTREACHED";