r4490 - developers/werner/bin

werner at docs.openmoko.org werner at docs.openmoko.org
Thu Jun 19 04:26:22 CEST 2008


Author: werner
Date: 2008-06-19 04:26:20 +0200 (Thu, 19 Jun 2008)
New Revision: 4490

Added:
   developers/werner/bin/mdflt
Log:
mdflt: a simple Maildir (re)classifier that keeps me from completely drowning
  in mails ...



Added: developers/werner/bin/mdflt
===================================================================
--- developers/werner/bin/mdflt	                        (rev 0)
+++ developers/werner/bin/mdflt	2008-06-19 02:26:20 UTC (rev 4490)
@@ -0,0 +1,108 @@
+#!/usr/bin/perl
+#
+# mdflt - Maildir filter
+#
+# Copyright (C) 2008 by Openmoko, Inc.
+# Written 2008 by Werner Almesberger
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+
+#
+# This is a very simply classifier for Maildir directories. Unlike procmail,
+# it works on the user's Maildir after reception, so it doesn't need to be
+# inserted into the delivery path.
+#
+# Example use:
+#
+# isync -a
+# cd $HOME/Maildir
+# mdmov -s -r sender foo-list-bounces foo new/*,
+#
+
+
+sub move
+{
+    local ($from, $to) = @_;
+
+    if ($dry_run || $verbose) {
+	print STDERR "$from -> $to\n";
+    }
+    if (!$dry_run) {
+	rename($from, $to) || die "$from -> $to: $!";
+    }
+}
+
+
+sub usage
+{
+    print STDERR
+      "usage: $0 [-d] [-s] [-v] [-r field regexp folder] ... file ...\n";
+    print STDERR"  -d                      dry-run mode\n";
+    print STDERR"  -s                      print statistics at the end\n";
+    print STDERR"  -v                      verbose mode\n";
+    print STDERR"  -r field regexp folder  add a filing rule\n";
+    exit(1);
+}
+
+
+while ($ARGV[0] =~ /^-/) {
+    if ($ARGV[0] eq "-d") {
+	shift @ARGV;
+	$dry_run = 1;
+    } elsif ($ARGV[0] eq "-s") {
+	shift @ARGV;
+	$stat = 1;
+    } elsif ($ARGV[0] eq "-v") {
+	shift @ARGV;
+	$verbose = 1;
+    } elsif ($ARGV[0] eq "-r") {
+	shift @ARGV;
+	&usage unless defined $ARGV[2];
+	$field[$rules] = uc shift @ARGV;
+	$re[$rules] = shift @ARGV;
+	$folder[$rules] = shift @ARGV;
+	die "$folder[$rules]: $!" unless -d $folder[$rules];
+	$rules++;
+    } else {
+	&usage;
+    }
+}
+
+for $name (@ARGV) {
+    if (!open(FILE, $name)) {
+	warn "$name: $!";
+	next;
+    }
+    undef %hdr;
+    undef $last;
+    while (<FILE>) {
+	chop;
+	last if /^$/;
+	if (/^(\S+):\s+/) {
+	    $last = uc $1;
+	    $hdr{$last} = $';
+	} elsif (/^\s+/) {
+	    die "unexpected continuation in $name:\n$_\n" unless defined $last;
+	    $hdr{$last} .= " ".$';
+	} else {
+	    die "bad header in $name:\n$_\n";
+	}
+    }
+    for ($i = 0; $i != $rules; $i++) {
+	next unless defined $hdr{$field[$i]};
+	next unless $hdr{$field[$i]} =~ m/$re[$i]/;
+	($base = $name) =~ s|^.*/||;
+	&move($name, $folder[$i]."/".$base);
+	$cnt[$i]++;
+    }
+    close FILE;
+}
+if ($stat) {
+    for ($i = 0; $i != $rules; $i++) {
+	print STDERR "[$i] $cnt[$i] -> $folder[$i]\n" if $cnt[$i];
+    }
+}


Property changes on: developers/werner/bin/mdflt
___________________________________________________________________
Name: svn:executable
   + *





More information about the commitlog mailing list