]>
diplodocus.org Git - minc/blob - minc
ebe447f74ff92fda0753bb2cdb3ff8899cc44e9d
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
18 (http://razor.sourceforge.net/), and optionally filters mail into
21 The filtering is quite sophisticated, as it is done using real Perl
22 substitution (s//) commands.
29 require 'sysexits.ph';
35 use Log
::Dispatch
::File
;
36 use POSIX
qw(strftime WEXITSTATUS WIFEXITED);
44 Dump (using Data::Dumper) the FILTERS hash and exit. This is useful
45 for testing the syntax of .mincfilter.
53 Dry run; do not actually incorporate the mail, but log and report to
54 stdout/stderr as normal.
58 Process SPAM only, then exit, leaving all non-spam messages in the
72 our $opt_s; # ;; # stupid cperl-mode
74 if (not getopts
('dhns')) {
81 print("Sorry bub, no help.\n");
87 if ($opt_s) { # ))){ # stupid cperl-mode
97 Where configuration files (.mincfilter) are found. Also,
98 $HOME/Maildir is used for the maildir if MAILDIR is not set.
102 Where mail is delivered.
108 use Env
qw(HOME MAILDIR);
111 die("HOME environment variable must be set.\n");
114 $MAILDIR = "$HOME/Maildir";
121 =item $HOME/.mincfilter
123 This file is Perl code (included via the 'require' directive) which is
124 expected to define the FILTERS hash.
126 =item `mhpath +`/logs/minc.log
128 Where minc logs what it does, unless in -n mode.
130 =item `mhpath +`/logs/dryrun.log
132 Where minc logs what it would do; used in -n mode.
137 require "$HOME/.mincfilter";
146 $logfile = $mh . '/logs/minc.log';
148 $logfile = $mh . '/logs/dryrun.log';
152 ###############################################################################
155 # debuglevels for the logger
156 use constant LOG_DEBUGLEVEL
=> 'info';
157 use constant SCREEN_DEBUGLEVEL
=> 'debug';
159 # characters used to wrap around the id field in the log so I can more
160 # easily parse the msg flow
161 use constant DEBUGCHAR
=> '%';
162 use constant INFOCHAR
=> '=';
163 use constant INCOMINGCHAR
=> '<';
164 use constant SAVECHAR
=> '>';
165 use constant WARNCHAR
=> 'W';
166 use constant ERRORCHAR
=> 'E';
168 my $logger = new Log
::Dispatch
;
169 $logger->add(new Log
::Dispatch
::File
(name
=>'logfile',
174 # log alias, handles getting passed vars that are undef
178 my $timestamp = strftime
('%b %e %H:%M:%S', localtime);
180 foreach my $part (@_) {
181 if (defined($part)) {
185 # no newlines in the log message, thanks
187 $msg = sprintf("%s %s%s %s\n", $timestamp, $act, $act, $msg);
188 $logger->log(level
=>$level, message
=>$msg);
190 if ($act eq SAVECHAR
) {
191 $logger->log(level
=>$level, message
=>"\n");
195 sub logincoming
{ mylog
('info', INCOMINGCHAR
, @_); }
196 sub logsave
{ mylog
('notice', SAVECHAR
, @_); }
197 sub loginfo
{ mylog
('info', INFOCHAR
, @_); }
198 sub logdebug
{ mylog
('debug', DEBUGCHAR
, @_); }
199 sub logwarn
{ mylog
('warning', WARNCHAR
, @_); }
200 sub logerr
{ mylog
('error', ERRORCHAR
, @_); }
205 logincoming
('From: ', $headers{'return-path'});
206 logincoming
('To: ', $headers{'to'});
207 logincoming
('Subject: ', $headers{'subject'});
208 logincoming
('Message-Id: ', $headers{'message-id'});
212 ###############################################################################
216 printf('%s:%s', __FILE__
, __LINE__
);
245 my $mhfolder = shift;
250 $folder = $mh . '/' . $mhfolder;
253 if (not -d
$folder) {
254 foreach $component (split('/', $folder)) {
255 if (defined($component) and length($component) >= 1) {
256 $target = $target . '/' . $component;
257 if (-d
$target or mkdir($target)) {
261 "Failed to create +$mhfolder ($component)");
272 if (not opendir(DIR
, $dir)) {
273 err
(&EX_TEMPFAIL
, "Failed opendir($dir)");
276 # Initialize $! to 0 (success) because stupid stupid Perl provides
277 # no way to distinguish an error from an empty directory; that
278 # means setting $! to 0 and testing it afterwards is the only way
279 # to detect an error. Real Programmers don't handle errors,
283 ($_ ne '.' and $_ ne '..')
284 and $_ = "$MAILDIR/new/$_"
288 err
(&EX_TEMPFAIL
, "Failed readdir($dir)");
296 sub get_highest_msgnum
{
297 my $mhfolder = shift;
303 foreach $msgnum (getfiles
($mhfolder)) {
305 if ($msgnum =~ /^[0-9]+$/) {
306 if ($msgnum > $highest) {
315 # We want to print the name of each list that has new mail only once,
316 # so use this hash to store the lists that have already been printed.
317 # Start the list out with SPAM already in it, since we don't care when
318 # new messages are added to it.
319 my %FOLDERS = ('SPAM'=>1);
323 my $mhfolder = shift;
329 # We must do this even in -n mode because later steps fail without
330 # it. This should be harmless.
333 $msgnum = get_highest_msgnum
($mhfolder);
334 for ($try = 0; ; $try++) {
336 $mhmsg = "$mhfolder/$msgnum";
338 if (not stat($mhmsg)) {
340 # Now we have a non-existent file, let's try to create
341 # it. We must create a zero-byte file first because a
342 # file my appear between our happy stat results and
343 # our later rename(2), which would clobber said file.
344 # So attempt to create a file with this name. If it
345 # succeeds, in just a bit here we'll knowingly clobber
346 # this file with the rename(2) call.
347 if (sysopen(MSG
, $mhmsg, O_WRONLY
| O_EXCL
| O_CREAT
, 0600)) {
354 # This algorithm is different from the maildir one; let's make
355 # 10 tries instead of 3.
357 errx
(&EX_TEMPFAIL
, "Attempted filename exists.");
360 # This algorithm is different; i don't think we need to sleep.
364 logsave
("+$mhfolder");
367 if (not rename($msg, $mhmsg)) {
368 err
(&EX_TEMPFAIL
, "Failed rename($msg, $mhmsg)");
371 # XXX: Lame! Instead, keep a hash of arrays. Keys are folder
372 # names, array elements are message numbers in that list.
373 # Then after all the messages have been sorted, run one mark
374 # command per folder, marking all messages for that folder in
376 if ($mhfolder ne 'SPAM') {
377 $status = system("mark +$mhfolder $msgnum -sequence unseen -add");
378 if (not (WIFEXITED
($status) and WEXITSTATUS
($status) == 0)) {
379 errx
(&EX_TEMPFAIL
, "Failed to mark message unseen");
384 if (not $FOLDERS{$mhfolder}) {
385 print("+$mhfolder\n");
386 $FOLDERS{$mhfolder} = 1;
393 my $current; # current header, used for unfolding lines
394 my $fieldname; # unmolested header name
404 # folded header continuation
406 if (not defined($current)) {
407 print("Malformed message, cannot parse headers.\n");
411 $headers{$current} .= $_;
413 ($fieldname) = split(/:/);
414 $current = lc($fieldname);
415 (undef, $headers{$current}) = split(/^$fieldname:\s*/);
424 ###############################################################################
434 $err =~ s
|/new/|/err/|;
435 $sig =~ s
|/new/|/sig/|;
445 ($message, $!) = split(/:/, $line);
447 logerr
("$err: $message: $!");
450 if (unlink($err) != 1) {
451 err
(&EX_TEMPFAIL
, "Failed unlink($err)");
455 # This is supposed to be a signature created with razor-check
456 # directly after delivery. Currently this isn't supported
457 # because it isn't clear to me how to get that signature back
458 # into razor-check. For now, just unlink any sig files we
459 # find and proceed with full razor-check mode.
462 if (unlink($sig) != 1) {
463 err
(&EX_TEMPFAIL
, "Failed unlink($sig)");
469 $status = system("razor-check < $msg");
470 if (not WIFEXITED
($status)) {
471 err
(&EX_TEMPFAIL
, "Failed to run razor-check < $msg");
472 } elsif (WEXITSTATUS
($status) == 0) {
491 foreach $msg (@msglist) {
492 printf('%sChecking for spam... %6d/%d',
493 "\r", ++$i, scalar(@msglist));
496 store_message
($msg, 'SPAM');
501 print("\nDone: ", scalar(@result), " survivors\n");
507 ###############################################################################
519 %headers = get_headers
($msg);
524 log_headers
(%headers);
526 foreach $header (keys(%FILTERS)) {
527 $contents = $headers{lc($header)};
529 if (defined($contents)) {
530 foreach $regex (keys(%{$FILTERS{$header}})) {
531 $subst = $FILTERS{$header}->{$regex};
533 $try = '$contents =~';
534 $try .= " s|$regex|$subst|is";
550 foreach $msg (@msglist) {
551 $mhfolder = find_mh_folder
($msg);
552 store_message
($msg, $mhfolder);
561 $Data::Dumper
::Indent
= 1;
562 print(Dumper
(\
%FILTERS));
566 @msglist = kill_spam
(getfiles
("$MAILDIR/new"));
569 filter_mail
(@msglist);
579 {'.*<([[:graph:]]+)-([[:graph:]]+)\.lists\.sourceforge\.net>.*'=>'l/$1/$2',
580 '.*<([[:graph:]]+?)(-list)?\.freedesktop\.org>.*'=>'l/freedesktop/$1',
581 '.*<swig\.cs\.uchicago\.edu>.*'=>'l/swig'},
584 {'<mailto:([[:graph:]]+)+@([[:graph:]]+)\.apache\.org>'=>'l/apache/$2/$1'},
589 The first List-Id filter is a surprisingly effective one which creates
590 appropriate folder names (such as l/htdig/updates and l/rox/devel) for
591 all Sourceforge lists to which i subscribe. Certainly there are lists
592 hosted at Sourceforge which do not work well with this, and this is
593 where it is important to remember that the first match is the one
594 uses. Simply put the more specific rules before this one.
596 The next List-Id example is simple. The swig example demonstrates
597 that the folder name does not have to use a portion of the matched
598 header; you can just hardcode the folder name.
600 The List-Post example is a nice one. Most ASF projects have their own
601 apache.org subdomain, with mailing lists hosted there. So, given a
602 list such as dev@httpd.apache.org, this filter will create the folder
603 name l/apache/httpd/dev.
607 Written by Eric Gillespie <epg@pretzelnet.org> with logging code
608 stolen from Adam Lazur <adam@lazur.org>.
610 Design by Eric Gillespie and Doug Porter <dsp@waterspout.com>.