-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotsync
More file actions
executable file
·149 lines (125 loc) · 4.99 KB
/
notsync
File metadata and controls
executable file
·149 lines (125 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
import ConfigParser
import imaplib
import re
import subprocess
import time
import logging
import os.path
import mailbox
from cnotmuch import notmuch
mid_regexp = re.compile('message-id:\s+\<(.*)\>', re.IGNORECASE)
ret_regexp = re.compile('([0-9]+) +.*')
# The `find_message' routine will throw an exception if the database
# is modified in another thread, so we have to retry in that case.
def mid_exists(nm, mid):
global debug
exists = False
attempted = False
while not attempted:
try:
exists = nm.find_message(mid)
attempted = True
except NotmuchError:
logging.debug('Exception during find_message')
pass
return exists
def poll_server(host, user, password, directory):
nm = notmuch.Database()
srv = imaplib.IMAP4_SSL(host)
srv.login(user, password)
typ, msgnums = srv.select()
bottom = '1'
for top in msgnums:
logging.debug('Checking the range %s - %s.' % (bottom, top))
typ, data = srv.fetch('%s:%s' % (bottom, top), '(BODY.PEEK[HEADER.FIELDS (MESSAGE-ID)])')
for mid_or_str in data:
if type(mid_or_str) == tuple:
(ret, mid) = mid_or_str
mid_match = mid_regexp.match(mid)
if mid_match:
mid = mid_match.group(1)
if not mid_exists(nm, mid):
logging.info('New: %s' % mid)
ret_match = ret_regexp.match(ret)
if ret_match:
num = ret_match.group(1)
typ, data = srv.fetch(num, '(RFC822)')
store(data[0][1], directory)
else:
logging.debug('Return code regexp did not match (%s).' % ret)
else:
logging.debug('Message ID regexp did not match (%s).' % mid)
bottom = top
srv.close()
srv.logout()
def delete_old(host, user, password, delay):
# `delay' is in days.
then = time.time() - (delay * 24 * 60 * 60)
thenstr = time.strftime('%d-%b-%Y', time.gmtime(then))
logging.debug('Deleting messages BEFORE %s' % thenstr)
srv = imaplib.IMAP4_SSL(host)
srv.login(user, password)
typ, msgnums = srv.select()
typ, data = srv.search(None, '(BEFORE %s)' % thenstr)
if data[0] != '':
# Break the request up into 100 message chunks to avoid
# upsetting some IMAP servers with very long requests (Sun
# Java Messaging Server at least).
def chunks(l, n):
for i in xrange(0, len(l), n):
yield l[i:i+n]
for i in chunks(data[0].split(), 100):
logging.info('Deleting %s' % ' '.join(i))
srv.store(','.join(i), '+FLAGS', '\\Deleted')
srv.close()
srv.logout()
def store(message, directory):
# Allow for strftime formatting in the directory name.
directory = time.strftime(directory)
logging.debug('Maildir for store is %s' % directory)
md = mailbox.Maildir(directory, factory = None, create = True)
# Remove DOS line endings.
# Is this completely safe?
msg = mailbox.MaildirMessage(message.replace('\x0d\x0a', '\x0a'))
md.add(msg)
md.close()
def main():
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser('~/.notsync.cfg'))
log_level = logging.WARN
if config.has_option('general', 'verbose') and \
config.getboolean('general', 'verbose'):
log_level = logging.INFO
if config.has_option('general', 'debug') and \
config.getboolean('general', 'debug'):
log_level = logging.DEBUG
keep = 30
if config.has_option('general', 'keep'):
keep = config.getint('general', 'keep')
maildir = os.path.expanduser('~/Maildir')
if config.has_option('general', 'maildir'):
maildir = os.path.expanduser(config.get('general', 'maildir'))
logging.basicConfig(level=log_level, datefmt="%Y%m%d %H:%M:%S",
format="%(asctime)s - %(levelname)s - %(message)s")
for server in config.get('general', 'servers').split():
logging.info('Polling %s...' % server)
sub = ''
if config.has_option(server, 'subdir'):
sub = config.get(server, 'subdir')
d = os.path.join(maildir, sub)
poll_server(host = config.get(server, 'host'),
user = config.get(server, 'user'),
password = config.get(server, 'password'),
directory = d)
if config.has_option('general', 'delete_mail') and \
config.getboolean('general', 'delete_mail'):
logging.info('Cleaning %s...' % server)
delete_old(host = config.get(server, 'host'),
user = config.get(server, 'user'),
password = config.get(server, 'password'),
delay = keep)
else:
logging.debug('Not deleting old mail.')
if __name__ == '__main__':
main()