]> git.madduck.net Git - etc/mailfilter.git/blob - bin/process-tickler

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

always filter gpg mail into inbox
[etc/mailfilter.git] / bin / process-tickler
1 #!/usr/bin/python2.5
2 # -*- coding: utf-8 -*-
3 #
4 # process-tickler – process message in tickler maildir
5 #
6 # Copyright © martin f. krafft <madduck@debian.org>
7 # Released under the terms of the Artistic Licence 2.0
8 #
9
10 import mailbox
11 import os
12 import sys
13 import time
14
15 HOME = os.getenv('HOME')
16 MAILDIR = os.path.join(HOME, '.maildir')
17 TICKLER_DIR = os.path.join(MAILDIR, '.tickler')
18 DEST_DIR = os.path.join(MAILDIR, '.resubmit')
19
20 tmd = mailbox.Maildir(TICKLER_DIR)
21 dmd = mailbox.Maildir(DEST_DIR)
22 msgids = dict()
23
24 def resubmit(key):
25     msg = tmd.get_message(key)
26     print >>sys.stdout, 'I: resubmit message %s' % key
27     if msg.has_key('X-Tickle-Delivered'):
28         msg.replace_header('X-Tickle-Delivered', time.strftime('%c'))
29     else:
30         msg.add_header('X-Tickle-Delivered', time.strftime('%c'))
31     dmd.add(msg)
32     tmd.discard(key)
33
34 for key, msg in tmd.iteritems():
35     msgid = msg.get('Message-Id')
36     if msgid is None:
37         print >>sys.stderr, 'W: message without ID: ' + key
38         resubmit(key)
39
40     tickle = msg.get('X-Tickle')
41     if tickle is None:
42         print >>sys.stderr, 'W: message without tickle information: ' + msgid
43         resubmit(key)
44
45     if msgids.get(msgid, None) is None:
46         msgids[msgid] = list()
47     msgids[msgid].append((int(tickle.split(' ', 1)[0]), key, msg))
48
49 for msgid, msgs in msgids.iteritems():
50     msgs.sort()
51     prev_tickle = None
52     for tickle, key, msg in msgs:
53         if tickle == prev_tickle:
54             print >>sys.stderr, 'I: discarding duplicate %s of message %s' % (key, msgid)
55             tmd.discard(key)
56             continue
57         prev_tickle = tickle
58
59         t = time.time()
60         if t >= tickle:
61             resubmit(key)
62         else:
63             print >>sys.stdout, 'I: message %s still has %d seconds' % (key,
64                     int(tickle - t))