]>
diplodocus.org Git - minc/blob - minc
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.
77 if (!getopts
('dhn')) {
83 print("Sorry bub, no help.\n");
95 Where configuration files (.mincfilter) are found.
99 Where mail is delivered.
109 die("HOME environment variable must be set.\n");
112 $MAILDIR = "$HOME/Maildir";
119 =item $HOME/.mincfilter
121 This file is Perl code (included via the 'require' directive) which is
122 expected to define the FILTERS hash.
124 =item `mhpath +`/logs/minc.log
126 Where minc logs what it does, unless in -n mode.
128 =item `mhpath +`/logs/dryrun.log
130 Where minc logs what it would do; used in -n mode.
135 require "$HOME/.mincfilter";
144 $logfile = $mh . '/logs/minc.log';
146 $logfile = $mh . '/logs/dryrun.log';
150 ###############################################################################
153 my $logger = new Log
::Dispatch
;
154 $logger->add(new Log
::Dispatch
::File
(name
=>'logfile',
159 # log alias, handles getting passed vars that are undef
163 my $timestamp = strftime
('%b %e %H:%M:%S', localtime);
165 foreach my $part (@_) {
166 if (defined($part)) {
170 # no newlines in the log message, thanks
172 $msg = sprintf("%s %s%s %s\n", $timestamp, $act, $act, $msg);
173 $logger->log(level
=>$level, message
=>$msg);
175 if ($act eq SAVECHAR
) {
176 $logger->log(level
=>$level, message
=>"\n");
180 sub logincoming
{ mylog
('info', INCOMINGCHAR
, @_); }
181 sub logsave
{ mylog
('notice', SAVECHAR
, @_); }
182 sub loginfo
{ mylog
('info', INFOCHAR
, @_); }
183 sub logdebug
{ mylog
('debug', DEBUGCHAR
, @_); }
184 sub logwarn
{ mylog
('warning', WARNCHAR
, @_); }
185 sub logerr
{ mylog
('error', ERRORCHAR
, @_); }
190 logincoming
('From: ', $headers{'Return-Path'});
191 logincoming
('To: ', $headers{'To'});
192 logincoming
('Subject: ', $headers{'Subject'});
193 logincoming
('Message-Id: ', $headers{'Message-Id'});
197 ###############################################################################
201 my $mhfolder = shift;
206 $folder = $mh . '/' . $mhfolder;
210 foreach $component (split('/', $folder)) {
211 if (defined($component) and length($component) >= 1) {
212 $target = $target . '/' . $component;
213 if (-d
$target or mkdir($target)) {
216 die("Failed to create +$mhfolder ($component): $!\n");
223 # We want to print the name of each list that has new mail only once,
224 # so use this hash to store the lists that have already been printed.
225 # Start the list out with SPAM already in it, since we don't care when
226 # new messages are added to it.
227 my %FOLDERS = ('SPAM'=>1);
231 my $mhfolder = shift;
237 $mhmsg = `mhpath +$mhfolder new`;
239 $msgnum = basename
($mhmsg);
241 logsave
("+$mhfolder");
244 if (not rename($msg, $mhmsg)) {
245 die("Rename failed: $!\n");
248 if ($mhfolder ne 'SPAM') {
249 system("mark +$mhfolder $msgnum -sequence unseen -add");
253 if (not $FOLDERS{$mhfolder}) {
254 print("+$mhfolder\n");
255 $FOLDERS{$mhfolder} = 1;
262 my $current; # current header, used for unfolding lines
272 # folded header continuation
274 if (!defined($current)) {
275 # XXX: Malformed message; log to stderr
279 $headers{$current} .= $_;
281 ($current) = split(/:/);
283 # XXX: Is it correct simply to eat all whitespace between
284 # the colon and the first text for the header?
285 # Furthermore, is any space at all required? Or is
286 # '^Subject:hey$' a perfectly valid header?
287 (undef, $headers{$current}) = split(/^$current:\s*/);
296 ###############################################################################
306 $err =~ s
|/new/|/err/|;
307 $sig =~ s
|/new/|/sig/|;
317 ($message, $!) = split(/:/, $line);
319 logerr
("$err: $message: $!");
321 if (unlink($err) != 1) {
322 die("Failed unlink($err): $!\n");
325 # This is supposed to be a signature created with razor-check
326 # directly after delivery. Currently this isn't supported
327 # because it isn't clear to me how to get that signature back
328 # into razor-check. For now, just unlink any sig files we
329 # find and proceed with full razor-check mode.
331 if (unlink($sig) != 1) {
332 die("Failed unlink($sig): $!\n");
336 #$status = system("razor-check < $msg");
337 $status = system('false');
338 if (WIFEXITED
($status) and WEXITSTATUS
($status) == 0) {
352 foreach $msg (@msglist) {
354 store_message
($msg, 'SPAM');
364 ###############################################################################
376 %headers = get_headers
($msg);
378 log_headers
(%headers);
380 foreach $header (keys(%FILTERS)) {
381 $contents = $headers{$header};
383 if (defined($contents)) {
384 foreach $regex (keys(%{$FILTERS{$header}})) {
385 $subst = $FILTERS{$header}->{$regex};
387 $try = '$contents =~';
388 $try .= " s|$regex|$subst|is";
404 foreach $msg (@msglist) {
405 $mhfolder = find_mh_folder
($msg);
406 store_message
($msg, $mhfolder);
415 $Data::Dumper
::Indent
= 1;
416 print(Dumper
(\
%FILTERS));
420 @msglist = kill_spam
(glob("$MAILDIR/new/*"));
421 filter_mail
(@msglist);
430 {'.*<([[:graph:]]+)-([[:graph:]]+)\.lists\.sourceforge\.net>.*'=>'l/$1/$2',
431 '.*<([[:graph:]]+?)(-list)?\.freedesktop\.org>.*'=>'l/freedesktop/$1',
432 '.*<swig\.cs\.uchicago\.edu>.*'=>'l/swig'},
435 {'<mailto:([[:graph:]]+)+@([[:graph:]]+)\.apache\.org>'=>'l/apache/$2/$1'},
440 The first List-Id filter is a surprisingly effective one which creates
441 appropriate folder names (such as l/htdig/updates and l/rox/devel) for
442 all Sourceforge lists to which i subscribe. Certainly there are lists
443 hosted at Sourceforge which do not work well with this, and this is
444 where it is important to remember that the first match is the one
445 uses. Simply put the more specific rules before this one.
447 The next List-Id example is simple. The swig example demonstrates
448 that the folder name does not have to use a portion of the matched
449 header; you can just hardcode the folder name.
451 The List-Post example is a nice one. Most ASF projects have their own
452 apache.org subdomain, with mailing lists hosted there. So, given a
453 list such as dev@httpd.apache.org, this filter will create the folder
454 name l/apache/httpd/dev.
458 Written by Eric Gillespie <epg@pretzelnet.org> with logging code
459 stolen from Adam Lazur <adam@lazur.org>.
461 Design by Eric Gillespie and Doug Porter <dsp@waterspout.com>.