]>
diplodocus.org Git - minc/blob - minc
26b7e6c4e25375a8132478cbd25e0ec7caf0bf7c
7 B<minc> - Incorporate mail from a maildir into mh folders.
15 B<minc> is a program for incorporating mail from a maildir to a mh
16 folder hierarchy. It takes mail from a maildir folder (not a maildir
17 folder hierarchy), checks for spam with razor (XXX crossref), and
18 optionally filters mail into separate mh folders.
20 The filtering is quite sophisticated, as it is done using real Perl
21 substitution (s//) commands.
28 require 'sysexits.ph';
34 use Log
::Dispatch
::File
;
35 use POSIX
qw(strftime WEXITSTATUS WIFEXITED);
37 # debuglevels for the logger
38 use constant LOG_DEBUGLEVEL
=> 'info';
39 use constant SCREEN_DEBUGLEVEL
=> 'debug';
41 # characters used to wrap around the id field in the log so I can more
42 # easily parse the msg flow
43 use constant DEBUGCHAR
=> '%';
44 use constant INFOCHAR
=> '=';
45 use constant INCOMINGCHAR
=> '<';
46 use constant SAVECHAR
=> '>';
47 use constant WARNCHAR
=> 'W';
48 use constant ERRORCHAR
=> 'E';
56 Dump (using Data::Dumper) the FILTERS hash and exit. This is useful
57 for testing the syntax of .mincfilter.
65 Dry run; do not actually incorporate the mail, but log and report to
66 stdout/stderr as normal.
70 Process SPAM only, then exit, leaving all non-spam messages in the
84 our $opt_s; # ;; # stupid cperl-mode
86 if (!getopts
('dhns')) {
93 print("Sorry bub, no help.\n");
99 if ($opt_s) { # ))){ # stupid cperl-mode
109 Where configuration files (.mincfilter) are found.
113 Where mail is delivered.
123 die("HOME environment variable must be set.\n");
126 $MAILDIR = "$HOME/Maildir";
133 =item $HOME/.mincfilter
135 This file is Perl code (included via the 'require' directive) which is
136 expected to define the FILTERS hash.
138 =item `mhpath +`/logs/minc.log
140 Where minc logs what it does, unless in -n mode.
142 =item `mhpath +`/logs/dryrun.log
144 Where minc logs what it would do; used in -n mode.
149 require "$HOME/.mincfilter";
158 $logfile = $mh . '/logs/minc.log';
160 $logfile = $mh . '/logs/dryrun.log';
164 ###############################################################################
167 my $logger = new Log
::Dispatch
;
168 $logger->add(new Log
::Dispatch
::File
(name
=>'logfile',
173 # log alias, handles getting passed vars that are undef
177 my $timestamp = strftime
('%b %e %H:%M:%S', localtime);
179 foreach my $part (@_) {
180 if (defined($part)) {
184 # no newlines in the log message, thanks
186 $msg = sprintf("%s %s%s %s\n", $timestamp, $act, $act, $msg);
187 $logger->log(level
=>$level, message
=>$msg);
189 if ($act eq SAVECHAR
) {
190 $logger->log(level
=>$level, message
=>"\n");
194 sub logincoming
{ mylog
('info', INCOMINGCHAR
, @_); }
195 sub logsave
{ mylog
('notice', SAVECHAR
, @_); }
196 sub loginfo
{ mylog
('info', INFOCHAR
, @_); }
197 sub logdebug
{ mylog
('debug', DEBUGCHAR
, @_); }
198 sub logwarn
{ mylog
('warning', WARNCHAR
, @_); }
199 sub logerr
{ mylog
('error', ERRORCHAR
, @_); }
204 logincoming
('From: ', $headers{'return-path'});
205 logincoming
('To: ', $headers{'to'});
206 logincoming
('Subject: ', $headers{'subject'});
207 logincoming
('Message-Id: ', $headers{'message-id'});
211 ###############################################################################
215 printf("%s:%s", __FILE__
, __LINE__
);
244 my $mhfolder = shift;
249 $folder = $mh . '/' . $mhfolder;
253 foreach $component (split('/', $folder)) {
254 if (defined($component) and length($component) >= 1) {
255 $target = $target . '/' . $component;
256 if (-d
$target or mkdir($target)) {
260 "Failed to create +$mhfolder ($component)");
267 # We want to print the name of each list that has new mail only once,
268 # so use this hash to store the lists that have already been printed.
269 # Start the list out with SPAM already in it, since we don't care when
270 # new messages are added to it.
271 my %FOLDERS = ('SPAM'=>1);
275 my $mhfolder = shift;
279 # We must do this even in -n mode because later steps fail without
280 # it. This should be harmless.
283 $mhmsg = `mhpath +$mhfolder new`;
285 $msgnum = basename
($mhmsg);
287 logsave
("+$mhfolder");
290 if (not rename($msg, $mhmsg)) {
291 err
(&EX_TEMPFAIL
, "Failed rename($msg, $mhmsg)");
294 if ($mhfolder ne 'SPAM') {
295 system("mark +$mhfolder $msgnum -sequence unseen -add");
299 if (not $FOLDERS{$mhfolder}) {
300 print("+$mhfolder\n");
301 $FOLDERS{$mhfolder} = 1;
308 my $current; # current header, used for unfolding lines
309 my $fieldname; # unmolested header name
319 # folded header continuation
321 if (!defined($current)) {
322 print("Malformed message, cannot parse headers.\n");
326 $headers{$current} .= $_;
328 ($fieldname) = split(/:/);
329 $current = lc($fieldname);
330 (undef, $headers{$current}) = split(/^$fieldname:\s*/);
339 ###############################################################################
349 $err =~ s
|/new/|/err/|;
350 $sig =~ s
|/new/|/sig/|;
360 ($message, $!) = split(/:/, $line);
362 logerr
("$err: $message: $!");
365 if (unlink($err) != 1) {
366 err
(&EX_TEMPFAIL
, "Failed unlink($err)");
370 # This is supposed to be a signature created with razor-check
371 # directly after delivery. Currently this isn't supported
372 # because it isn't clear to me how to get that signature back
373 # into razor-check. For now, just unlink any sig files we
374 # find and proceed with full razor-check mode.
377 if (unlink($sig) != 1) {
378 err
(&EX_TEMPFAIL
, "Failed unlink($sig)");
384 $status = system("razor-check < $msg");
385 if (WIFEXITED
($status) and WEXITSTATUS
($status) == 0) {
404 foreach $msg (@msglist) {
405 printf('%sChecking for spam... %6d/%d',
406 "\r", ++$i, scalar(@msglist));
409 store_message
($msg, 'SPAM');
414 print("\nDone: ", scalar(@result), " survivors\n");
420 ###############################################################################
432 %headers = get_headers
($msg);
437 log_headers
(%headers);
439 foreach $header (keys(%FILTERS)) {
440 $contents = $headers{lc($header)};
442 if (defined($contents)) {
443 foreach $regex (keys(%{$FILTERS{$header}})) {
444 $subst = $FILTERS{$header}->{$regex};
446 $try = '$contents =~';
447 $try .= " s|$regex|$subst|is";
463 foreach $msg (@msglist) {
464 $mhfolder = find_mh_folder
($msg);
465 store_message
($msg, $mhfolder);
474 $Data::Dumper
::Indent
= 1;
475 print(Dumper
(\
%FILTERS));
479 @msglist = kill_spam
(glob("$MAILDIR/new/*"));
482 filter_mail
(@msglist);
492 {'.*<([[:graph:]]+)-([[:graph:]]+)\.lists\.sourceforge\.net>.*'=>'l/$1/$2',
493 '.*<([[:graph:]]+?)(-list)?\.freedesktop\.org>.*'=>'l/freedesktop/$1',
494 '.*<swig\.cs\.uchicago\.edu>.*'=>'l/swig'},
497 {'<mailto:([[:graph:]]+)+@([[:graph:]]+)\.apache\.org>'=>'l/apache/$2/$1'},
502 The first List-Id filter is a surprisingly effective one which creates
503 appropriate folder names (such as l/htdig/updates and l/rox/devel) for
504 all Sourceforge lists to which i subscribe. Certainly there are lists
505 hosted at Sourceforge which do not work well with this, and this is
506 where it is important to remember that the first match is the one
507 uses. Simply put the more specific rules before this one.
509 The next List-Id example is simple. The swig example demonstrates
510 that the folder name does not have to use a portion of the matched
511 header; you can just hardcode the folder name.
513 The List-Post example is a nice one. Most ASF projects have their own
514 apache.org subdomain, with mailing lists hosted there. So, given a
515 list such as dev@httpd.apache.org, this filter will create the folder
516 name l/apache/httpd/dev.
520 Written by Eric Gillespie <epg@pretzelnet.org> with logging code
521 stolen from Adam Lazur <adam@lazur.org>.
523 Design by Eric Gillespie and Doug Porter <dsp@waterspout.com>.