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

c853294f09aa174a056272c89de3351dfe8bf6b1
[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     imd.add(msg)
28     tmd.discard(key)
29
30 for key, msg in tmd.iteritems():
31     msgid = msg.get('Message-Id')
32     if msgid is None:
33         print >>sys.stderr, 'W: message without ID: ' + key
34         move_to_inbox(key)
35
36     tickle = msg.get('X-Tickle')
37     if tickle is None:
38         print >>sys.stderr, 'W: message without tickle information: ' + key
39         move_to_inbox(key)
40
41     if msgids.get(msgid, None) is None:
42         msgids[msgid] = list()
43     msgids[msgid].append((int(tickle.split(' ', 1)[0]), key, msg))
44
45 for msgid, msgs in msgids.iteritems():
46     msgs.sort()
47     prev_tickle = None
48     for tickle, key, msg in msgs:
49         if tickle == prev_tickle:
50             print >>sys.stderr, 'I: discarding duplicate %s of message %s' % (key, msgid)
51             tmd.discard(key)
52             continue
53         prev_tickle = tickle
54
55         t = time.time()
56         if t >= tickle:
57             move_to_inbox(key)
58         else:
59             print >>sys.stdout, 'I: message %s still has %d seconds' % (key,
60                     int(tickle - t))