5b2fbd91a1a659fb64db8249c2144cdb6d73eea3
[PerlMail.git] / PerlMail / Config.pm
1 #! /usr/bin/perl
2
3 #       $Id$
4 # Copyright (C) 2002-2003 Jan Kratochvil <project-PerlMail@jankratochvil.net>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 package PerlMail::Config;
22 use vars qw($VERSION);
23 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
24 use strict;
25 use warnings;
26
27 require Exporter;
28 use vars qw(@ISA @EXPORT);
29 @ISA=qw(Exporter);
30 @EXPORT=qw(
31                 $HOME
32                 $Mail @ValidUsers $IdleMax $MaxBodySMS @SMSwebRcpt $SMSwebRcpt_username
33                 $Lock_pathname $PeerAddr $Socket_timeout $DB_table $DBI_database $DBI_user $DBI_pwd
34                 $sendmail_orig @addr_addon &FromAddress @h_rcpt @h_from
35                 %audit_profile @sms_squeezes @alternates_host @dnsbl_whitelist
36
37                 $Audit $is_pgp $opt_F $procmailFROM_MAILER $store_ignore $store_ignorenewmail
38                 $store_profile
39                 );
40
41 require Mail::Alias;
42
43
44 BEGIN {
45         for (qw(headerhas store headeris did dnsbl store_muttrc_alternates Received_for parts_linear mime_type
46                         body_first mimehead razor2 header_remap)) {
47                 eval 'sub '.$_.' { return ::'.$_.'(@_); }';
48                 }
49         }
50
51
52 # perlmail-accept & perlmail-sendmail
53
54 our $HOME="/home/lace";
55
56
57 # perlmail-accept
58
59 our $Mail="$HOME/Mail";
60 our @ValidUsers=qw(root lace short kratochvil _local);
61 our $IdleMax=10;
62 our $MaxBodySMS=0x1000; # max bytes to pass to Lingua::EN::Squeeze
63 our @SMSwebRcpt=qw(420 602 431329);
64 our $SMSwebRcpt_username="lace2";
65
66
67 # perlmail-submit
68
69 our $Lock_pathname="/tmp/PerlMail.lock";
70 our $PeerAddr="exuhome.dyn.jankratochvil.net.:852";
71 #our $PeerAddr="127.0.0.1:2852";
72 our $Socket_timeout=7600;       # 15sec is NOT enough!
73 our $DB_table="PerlMail_folder";
74 our $DBI_database="short";
75 our $DBI_user="short";
76 our $DBI_pwd=$ENV{"HOME"}."/priv/mysql.".$DBI_user.".pwd";
77
78
79 # perlmail-sendmail
80
81 my @lists=qw(
82                 tacplus-l@disaster.com
83                 gsm@sh.cvut.cz
84                 linux-fsdevel@vger.kernel.org
85                 n9k@pandora.cz
86                 dev9k@pandora.cz
87                 gsm@pandora.cz
88                 ros-general@reactos.com
89                 ros-kernel@reactos.com
90                 ros-cvs@reactos.com
91                 4cinfo@atrey.karlin.mff.cuni.cz
92                 libtool@gnu.org
93                 libtool-patches@gnu.org
94                 wine-license@winehq.com
95                 wine-devel@winehq.com
96                 wine-patches@winehq.com
97                 gtk-devel-list@gnome.org
98                 gnome-vfs-list@gnome.org
99                 captive-announce-list@jankratochvil.net
100                 captive-list@jankratochvil.net
101                 captive-devel-list@jankratochvil.net
102                 );
103
104 our $sendmail_orig=(-x ($_="/usr/sbin/sendmail-orig") ? $_ : "/usr/sbin/sendmail");
105 # Mail-Alias-1.12 defaults to "/etc/mail/aliases" which does not exist on RedHat sendmail-8.12.5-7
106 # Mail-Alias-1.12 will clutter $_ !
107 our @addr_addon=(Mail::Alias->new("/etc/aliases")->exists("sentout") ? ("sentout") : ());
108
109 our $opt_F;     # imported
110 our $is_pgp;    # imported
111 my %lists=map(($_=>1),@lists);
112 sub FromAddress
113 {
114 my($rcpt,$iserror)=@_;
115
116         my $phrase=(defined $opt_F ? $opt_F : "Jan Kratochvil");
117         {
118                 last if !$is_pgp;
119                 last if $iserror;
120                 local *F;
121                 local $_;
122                 my $filename="$HOME/.gnupg/options";
123                 open F,$filename or do { warn "Open \"$filename\": $!"; last; };
124                 local $/="\n";
125                 my @keys=map((/^\s*default-key\s+(\S+)\s*$/),<F>);
126                 @keys==1 or do { warn "Found ".scalar(@keys)." 'default-key's in your \"$filename\", ignoring"; last; };
127                 close F or warn "Close \"$filename\": $!";
128                 my $default_key=$keys[0];
129                 $default_key=~/^[[:xdigit:]]{8}$/ or do { warn "Invalid 'default-key', ignoring: $default_key"; last; };
130                 return Mail::Address->new(
131                                 $phrase,
132                                 'pgp-'.uc($default_key).'@jankratochvil.net',
133                                 );
134                 }
135         return Mail::Address->new($phrase,'lace@jankratochvil.net') if !$lists{$rcpt->address()};
136         return Mail::Address->new(
137                         $phrase,
138                         'rcpt'
139                                         .'-'
140                                         .(defined($rcpt->user()) ? $rcpt->user() : "NOUSER")
141                                         .".AT."
142                                         .(defined($rcpt->host()) ? $rcpt->host() : "LOCAL")
143                                         .'@jankratochvil.net',
144                         );
145 }
146
147 # RedHat sendmail-8.9.3-20/src/conf.c/HdrInfo[]/\Q/* destination fields */\E
148 # FIXME: Recognize "Resent-$_" headers for -t but when we are in 'resent' mode?
149 our @h_rcpt=(   # case in-sensitive!
150                 "To",
151                 "Cc",
152                 "Bcc",
153                 "Apparently-To",
154                 );
155 # ordering matters; first header found is substituted
156 # last header is subsituted if no one is found
157 our @h_from=(
158                 "Resent-From",
159                 "From",
160                 );
161
162
163 # My-Audit
164
165 our %audit_profile=(
166                 "btw"   =>[],
167                 "silent"=>["=btw"   ,"did"],
168                 "log"   =>["=silent","syslog"],
169                 "bell"  =>["=log"   ,"bell"],
170                 "sms"   =>["=bell"  ,"sms=1"],
171                 "crit"  =>["=sms"   ,"sms=3"],
172                 );
173 our @sms_squeezes=(
174                 { "SqueezeControl"=>"noconv"                         },
175                 { "SqueezeControl"=>"conv"  ,"SQZ_OPTIMIZE_LEVEL"=>0 },
176                 { "SqueezeControl"=>"conv"  ,"SQZ_OPTIMIZE_LEVEL"=>1 },
177                 { "SqueezeControl"=>"med"   ,"SQZ_OPTIMIZE_LEVEL"=>0 },
178                 { "SqueezeControl"=>"med"   ,"SQZ_OPTIMIZE_LEVEL"=>1 },
179                 { "SqueezeControl"=>"max"   ,"SQZ_OPTIMIZE_LEVEL"=>0 },
180                 { "SqueezeControl"=>"max"   ,"SQZ_OPTIMIZE_LEVEL"=>1 },
181                 );
182 our @alternates_host=(
183                 "jabberwock.ucw.cz",    # short@ucw.cz
184                 "atrey.karlin.mff.cuni.cz",     # short@atrey.karlin.mff.cuni.cz
185                 "k332.feld.cvut.cz",    # short@k332.feld.cvut.cz
186                 );
187 our @dnsbl_whitelist=(
188                 "195.250.128.83",       # smtp3.vol.cz; vol.cz.multistage.blackholes.five-ten-sg.com.
189                 "64.49.222.22",         # mail.pm.org: rackspace.com.spam-support.blackholes.five-ten-sg.com.
190                 "208.147.243.5",        # gambit.liquidcomm.net: cw.net.spam-support.blackholes.five-ten-sg.com.
191                 "213.235.135.70",       # smtp.tiscali.cz: tiscali.cz.multistage.blackholes.five-ten-sg.com.
192                 "205.139.198.11",       # eniac.disaster.com: cw.net.spam-support.blackholes.five-ten-sg.com.
193                 "127.0.0.2",                    # 2.0.0.127.relays.ordb.org.
194                 "65.113.40.131",        # bozo.vmware.com: qwest.net.spam-support.blackholes.five-ten-sg.com.
195                 "66.218.85.33",         # mta2.wss.scd.yahoo.com: yahoo.com.spam.blackholes.five-ten-sg.com.
196                 "212.80.76.42",         # mx2.seznam.cz: seznam.cz.free.blackholes.five-ten-sg.com.
197                 );
198
199 our $Audit;     # imported
200 our $procmailFROM_MAILER;       # imported
201 our $store_ignorenewmail;       # imported
202 our $store_profile;     # imported
203 our $store_ignore;      # imported
204 sub audit
205 {
206         $store_profile=undef();
207         # TODO: <short-m@> storage?
208
209         # never spawn new mail if FROM_MAILER
210         # $isFROM_MAILER postponed after maillists as they may look as FROM_MAILER
211         #use re 'debug';
212         my $isFROM_MAILER=$Audit->header()=~/$procmailFROM_MAILER/mio;
213         $store_ignorenewmail=(0
214                         || $isFROM_MAILER
215                         || headerhas "From",'<Regexp:^owner->'
216                         );
217
218         # spam honeypots
219         return if did sub {
220                 local $_;
221                 # Do not local $store_file as it is our-imported
222                 $store_profile="silent";
223                 store "=spam"         if grep /^\Qshort\@k332.feld.cvut.cz\E/i,Received_for();
224                 # TODO: foreign violation of RFC 822 section 4.4.4, Subject:.*Automatick.+odpov.+v.+nep.+tomnosti
225                 store "=spam"         if headeris "From",'<ghandchi@hotmail.com>';
226                 store "=spam"         if headeris "From",'<newsletter@levnapc.cz>';
227                 store "=spam"         if headeris "From",'<Tomas@dtpstudio.cz>';
228                 store "=spam"         if headeris "From",'<BNcom@email.bn.com>';
229                 store "=spam"         if headeris "From",'<e4luck@lists.opt4email.com>';
230                 store "=spam"         if headeris "From",'<mailcontests@lists.servitall.com>';
231                 store "=spam"         if headeris "From",'<canda@lica.cz>';
232                 {
233                         # weak detection: files with text/html w/o text/plain are usually a spam
234                         my @types_linear=map({ mime_type($_); } parts_linear());
235                         store "=spam"       if grep({ $_ eq "text/html"; } @types_linear) && !grep({ $_ eq "text/plain"; } @types_linear);
236                         }
237                 store "=spam"         if ($_=mimehead(body_first())->mime_attr("Content-Type.charset")) && /^big5/i;
238                 };
239         $store_profile=undef();
240
241         # spam detection
242         return if did sub {
243                 # Do not local $store_file as it is our-imported
244                 $store_profile="silent";
245                 local $_;
246                 store "=spam".($_ eq 1 ? "" : ";$_")      if $_=razor2();
247                 };
248         $store_profile=undef();
249         return if did sub {
250                 # Do not local $store_file as it is our-imported
251                 $store_profile="silent";
252                 local $_;
253                 store "=spam"                      .";$_","log" if $_=dnsbl '.relays.ordb.org.' ,1;     # all hosts
254                 store "=spam"                      .";$_","log" if $_=dnsbl '.blackholes.mail-abuse.org.' ,1;   # all hosts
255                 # we don't check all hosts as they can be "dialup" category, FIXME: check for it
256                 store "=spam"                      .";$_","log" if $_=dnsbl '.blackholes.five-ten-sg.com.',0; # just first
257                 # I don't send viruses but viruses propagate mails of mine
258                 store "=spam"                                   if headeris "X-Mailer",'ravmd/8.3.2';
259                 };
260         $store_profile=undef();
261
262         # special delivery
263         store "=err","bell" and return if headerhas \&Received_for,'<short+err@>';
264
265         # ppl-wished foreign remapping, Reply-To is left untouched!
266         # FIXME: modifications are now being dropped by &write_message!
267         header_remap("From",{
268                         'kerere@post.cz'               =>'kamzik@k332.feld.cvut.cz',
269                         'profes@mbox.vol.cz'           =>'kratochvilova@egp.cz',
270                         'jkrouzek@mbox.vol.cz'         =>'krouzek@mbox.fsv.cuni.cz',
271                         'jakub.gorner@lidovky.cz'      =>'tonda@disnet.cz',
272                         'jan.kolar@videoprogress.cz'   =>'jenda.kolar@volny.cz',
273                         'daniel.rulicek@cponline.cz'   =>'daniel.rulicek@cpress.cz',
274                         'pavel@suse.cz'                =>'pavel@ucw.cz',
275                         });
276
277         # My obsolete e-mail addresses
278         store_muttrc_alternates "=redirect-","btw";
279
280         # nasty public lists with $store_ignore
281         {
282                 # Do not local $store_file as it is our-imported
283                 $store_profile="log";
284                 local $store_ignore;
285                 $store_ignore="smsmail"    if 1==$Audit->body() && length(join "",$Audit->body())<180;  # SMS mail
286                 $store_ignore="sms OS"     if $Audit->subject()=~/^Email pro: /;        # "^Email pro: gsm@sh\.cvut\.cz$";
287                 $store_ignore="list-moron" if grep { headeris "From",$_; } qw(
288                                 <kempny@>
289                                 <help.me@wo.cz>
290                                 <mr.death@mail.cz>
291                                 <danx3@centrum.cz>
292                                 <@mujoskar.cz>
293                                 <satko@quanto.nr.sanet.sk>
294                                 <vithous@attorney.cz> <viroman@attorney.cz>
295                                 <konf@klain.cz>
296                                 <stein@tiscali.cz>
297                                 <barevnej@volny.cz>
298                                 );
299
300                 store "=gsm"               if headeris  "Sender"   ,'<owner-gsm@sh.cvut.cz>';
301                 store "=gsmpand"           if headeris  "List-Post",'<gsm@pandora.cz>';
302                 store "=9kc","log"         if headeris  "List-Post",'<n9k@pandora.cz>';
303                 store "=9kcd","log"        if headeris  "List-Post",'<dev9k@pandora.cz>';
304                 }
305         $store_profile=undef();
306
307         # lists
308         store "=mozillabug","log"    if headeris "From"     ,'<bugzilla-daemon@mozilla.org>';
309         store "=9ku","log"           if headeris "List-Id"  ,'<9000.listman.net>';
310         store "=9kd","log"           if headeris "Sender"   ,'<owner-9000-developers@geekstuff.co.uk>';
311         store "=spong","log"         if headeris "List-Id"  ,'<spong-users.lists.sourceforge.net>';
312         store "=gtkd","silent"       if headeris "List-Id"  ,'<gtk-devel-list.gnome.org>';
313         store "=gnomevfs","log"      if headeris "List-Id"  ,'<gnome-vfs-list.gnome.org>';
314         store "=mffstatnice","bell"  if headeris "List-Post",'<statnice@atrey.karlin.mff.cuni.cz>';
315         store "=hw","log"            if headeris "List-Post",'<hw-news@list.gin.cz>';
316         store "=gnokii","log"        if headeris "List-Id"  ,'<gnokii-users.mail.freesoftware.fsf.org>';
317         store "=winelic","log"       if headeris "List-Id"  ,'<wine-license.winehq.com>';
318         store "=wined","silent"      if headeris "List-Id"  ,'<wine-devel.winehq.org>';
319         store "=winepat","silent"    if headeris "List-Id"  ,'<wine-patches.winehq.com>';
320         store "=winecvs","silent"    if headeris "List-Id"  ,'<wine-cvs.winehq.com>';
321         store "=wineann","log"       if headeris "List-Id"  ,'<wine-announce.winehq.com>';
322         store "=ros","log"           if headeris "List-Id"  ,'<ros-general.reactos.geldorp.nl>';
323         store "=roskernel","log"     if headeris "List-Id"  ,'<ros-kernel.reactos.com>';
324         store "=roscvs","silent"     if headeris "List-Id"  ,'<ros-cvs.reactos.com>';
325         store "=rosbug","log"        if headeris "Reply-To" ,'<scarab@reactos.wox.org>';
326         store "=fsd","silent"        if headeris "X-Mailing-List",'<linux-fsdevel@vger.kernel.org>';
327         store "=kerneld","silent"    if headeris "X-Mailing-List",'<linux-kernel@vger.kernel.org>';
328         store "=surprise","sms"      if headeris "List-Post",'<surprise@atrey.karlin.mff.cuni.cz>';
329         store "=surprisesuse","sms"  if headeris "Sender"   ,'<owner-surprise@suse.cz>';
330         store "=tacacs","log"        if headeris "Sender"   ,'<tacplus-l@disaster.com>';
331         store "=tacacs","log"        if headeris "Sender"   ,'<owner-tacplus-l@disaster.com>';
332         store "=tacacs","log"        if headeris "List-Id"  ,'<devel.lists.tacplus.org>';
333         store "=pm","sms"            if headeris "Sender"   ,'<owner-prague-pm@pm.org>';
334         store "=radary","log"        if headeris "Reply-To" ,'<pha@radary.cz>';
335         store "=dnet","log"          if headeris "Sender"   ,'<@lists.distributed.net>';
336         store "=linux-input","log"   if headeris "List-Post",'<linux-input@atrey.karlin.mff.cuni.cz>';
337         store "=strom","bell"        if headeris "List-Post",'<vodni-strom@atrey.karlin.mff.cuni.cz>';
338         store "=netinfo","log"       if headeris "Sender"   ,'<owner-netinfo-l@vol.cz>';
339         store "=saintmj","log"       if headeris "From"     ,'<netsaint@kam-enterprise.ms.mff.cuni.cz>';
340         store "=saintmj","log"       if headeris "From"     ,'<netsaint@kam.mff.cuni.cz>';
341         store "=4cerr","bell"        if headeris "From"     ,'<owner-4cinfo@atrey.karlin.mff.cuni.cz>';
342         store "=4c","sms"            if headeris "List-Post",'<4cinfo@atrey.karlin.mff.cuni.cz>';
343         store "=slashdot","bell"     if headeris "From"     ,'<slashdot@slashdot.org>';
344         store "=freshmeat","bell"    if headeris "From"     ,'<noreply@freshmeat.net>';
345         store "=sourceforge","bell"  if headeris "From"     ,'<noreply@sourceforge.net>';
346         store "=gsmperlcvs","silent" if headeris("From"     ,'<johan@intra.tektonica.com>')
347                                         && $Audit->subject()=~/^'.*' has been updated!$/;
348         store "=libtoold","log"      if headeris "List-Id"  ,'<libtool.gnu.org>';
349         store "=libtoolpat","log"    if headeris "List-Id"  ,'<libtool-patches.gnu.org>';
350         store "=cap","bell"          if headeris "List-Id"  ,'<captive-list.jankratochvil.net>';
351         store "=capd","bell"         if headeris "List-Id"  ,'<captive-devel-list.jankratochvil.net>';
352         store "=capann","bell"       if headeris "List-Id"  ,'<captive-announce-list.jankratochvil.net>';
353         # own webs
354         store "=energie","bell"      if headeris "From"     ,qr/^EnergieWeb/;
355         store "=ats","log"           if headeris("From"     ,'<root@ms.atspraha.cz>')
356                                         || (headeris("From",'<online@ringier.cz>') && headerhas("To",'<blesk@atspraha.cz>'));
357         store "=atscasablanca","log" if headeris "From"     ,'<casablanca@ms.atspraha.cz>';
358         store "=www-sms","log"       if headeris "List-Id"  ,'<www-sms-developers.lists.sourceforge.net>';
359
360         # Petr Koutecky does not mark his Stuff
361         store "=koutecky","log"      if headeris "From"     ,'<velkyhroch@seznam.cz>';
362
363         store "=errm","bell"         if $isFROM_MAILER && !did();
364
365         store "==","sms"             if !did;
366 }
367
368 sub audit_sms_address
369 {
370 my($obj)=@_;
371
372         my $address=$obj->address();
373         if (my $alternates=muttrc_get("alternates")) {
374                 return "I" if $address=~/$alternates/si;
375                 }
376         my %aliases=muttrc_aliases();
377         if (my $alias=$aliases{lc $address}) {
378                 local $_=$alias;
379                 s/\b(Bus)siness$/$1/i;
380                 s/\.ident$//i;
381                 return $_;
382                 }
383         local $_=$address;
384         s/\.cz$//i;
385         return $_;
386 }
387
388 sub audit_sms
389 {
390 my(%args)=@_;
391
392         my $from=(@{$args{"from"}} ? join(",",map({ audit_sms_address($_); } @{$args{"from"}})) : "?");
393         local $_;
394
395         $_=$args{"subject"};
396         # headers
397         s/(?:Re|Aw|Odp|Fw|Fwd|OT)(?:\[\d+\])?://ig;
398         # former subject
399         s/\bbylo:.*$//i;
400         s/\[\s*WAS:.*\]\s*$//i;
401         # trim
402         s/^\s*//s;
403         s/\s*$//s;
404         my $subject=$_;
405
406         $_=$args{"body"};
407         # max. 9 lines of .sig
408         s/\n-- (?:\n[^\n]*){0,9}$//gs;
409         # "Original Message"/"Puvodni zprava" etc. up to empty line
410         # "- - - Original message: - - -" is by "Lotus Notes Release 5.0.5  September 22, 2000"
411         s/(^|\n)[\s^\n]*(?:-----[\w\s]*-----|- - - Original message: - - -)[\s^\n]*(?:\n[^\n]+)*\n{2,}(?:\s*[^>\s].*$)?/\n/gs;
412         # Remove "..." lines (is it used by anyone except me?)
413         s/^\Q...\E$/*/gm;
414         # quoting "> "
415         s/^(?:\s*[[:upper:]]{0,3}>)+.*$/*/gm;
416         s/(?:^|\n)(?:\*\n+)+/\n*\n/gs;
417         # attributions
418         s/^.*\b(?:wrote|writes|napsal jste):\s*$//gm;
419         my $body=$_;
420
421         return [$from,"($subject)$body"];
422 }
423
424 1;