]> 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:

0c3839ff025d7d39bc26d072ce5d4e126a7c542d
[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 INBOX_DIR = MAILDIR
19
20 tmd = mailbox.Maildir(TICKLER_DIR)
21 imd = mailbox.Maildir(INBOX_DIR)
22 msgids = dict()
23
24 def move_to_inbox(key):
25     msg = tmd.get_message(key)
26     print >>sys.stdout, 'I: move message %s to inbox' % key
27     msg.add_header('X-Tickle-Delivered', time.strftime('%c'))
28     imd.add(msg)
29     tmd.discard(key)
30
31 for key, msg in tmd.iteritems():
32     msgid = msg.get('Message-Id')
33     if msgid is None:
34         print >>sys.stderr, 'W: message without ID: ' + key
35         move_to_inbox(key)
36
37     tickle = msg.get('X-Tickle')
38     if tickle is None:
39         print >>sys.stderr, 'W: message without tickle information: ' + key
40         move_to_inbox(key)
41
42     if msgids.get(msgid, None) is None:
43         msgids[msgid] = list()
44     msgids[msgid].append((int(tickle.split(' ', 1)[0]), key, msg))
45
46 for msgid, msgs in msgids.iteritems():
47     msgs.sort()
48     prev_tickle = None
49     for tickle, key, msg in msgs:
50         if tickle == prev_tickle:
51             print >>sys.stderr, 'I: discarding duplicate %s of message %s' % (key, msgid)
52             tmd.discard(key)
53             continue
54         prev_tickle = tickle
55
56         t = time.time()
57         if t >= tickle:
58             move_to_inbox(key)
59         else:
60             print >>sys.stdout, 'I: message %s still has %d seconds' % (key,
61                     int(tickle - t))