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