-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimap-util2
executable file
·242 lines (220 loc) · 8.51 KB
/
imap-util2
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python
#=======================================================================
# Various actions on IMAP folders
# Examples:
# imap-util2 $acct login
# imap-util2 $acct capabilities
# imap-util2 $acct namespace
# imap-util2 $acct folders
# imap-util2 $acct select $folder
# imap-util2 $acct older $folder $days
# imap-util2 $acct delete-older $folder $days
# imap-util2 $acct export $folder [uid...]
#=======================================================================
from __future__ import print_function
import sys
import imapclient
import argparse
try:
import configparser
except ImportError:
import ConfigParser as configparser
import os.path
import re
import datetime as dt
import logging
From_RE = re.compile(b'^From ', flags=re.MULTILINE)
def die(msg,code=1):
print(msg, file=sys.stderr)
sys.exit(code)
def arg(*args, **kw):
def decor(f):
if not hasattr(f, 'argdefs'):
f.argdefs = []
f.argdefs.insert(0, (args, kw))
return f
return decor
class ImapUtil(object):
def __init__(self, rcfile='~/.imap'):
self.rcfile = rcfile
def main(self):
ap = argparse.ArgumentParser()
ap.add_argument('-v','--verbose', action='store_true',
help='Show progress')
ap.add_argument('-d','--debug', action='store_true',
help='Low-level tracing')
ap.add_argument('account',
help='Account name in %s' % self.rcfile)
sub = ap.add_subparsers(dest='action', help='Action to perform')
actfunc = {}
for name, func in sorted(self.__class__.__dict__.items()):
if name.startswith('do_'):
action = name[3:].replace('_', '-')
actfunc[action] = func
sp = sub.add_parser(action)
for aargs, akw in getattr(func, 'argdefs', []):
sp.add_argument(*aargs, **akw)
args = ap.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
elif args.verbose:
logging.basicConfig(level=logging.INFO)
self.acct, action = args.account, args.action
if action is None: # PY3 bug
ap.error('No action given')
cp = configparser.ConfigParser()
cp.read(os.path.expanduser(self.rcfile))
host = cp.get(self.acct,'host')
user = cp.get(self.acct,'user')
password = cp.get(self.acct,'password')
try:
ssl = cp.getboolean(self.acct,'ssl')
except configparser.NoOptionError:
ssl = False
try:
port = cp.getint(self.acct,'port')
except configparser.NoOptionError:
port = None
try:
self.trash = cp.get(self.acct,'trash')
except configparser.NoOptionError:
self.trash = None
self.client = imapclient.IMAPClient(host, port, use_uid=True, ssl=ssl)
if args.debug:
self.client.debug = True
self.client.login(user, password)
# Route to appropriate function
func = actfunc[action]
func(self, args)
def do_capabilities(self, args):
for c in self.client.capabilities():
print(str(c.decode('ascii')))
def do_namespace(self, args):
ns = self.client.namespace()
# E.g. ((('', '.'),), None, None)
# personal, other, shared
print(ns)
@arg('folder', nargs='?')
def do_folders(self, args):
if args.folder is not None:
lst = self.client.list_folders(args.folder)
else:
lst = self.client.list_folders()
for flags, delimiter, name in lst:
print(name)
@arg('folder')
def do_select(self, args):
folder = args.folder
res = self.client.select_folder(folder)
print("%d message(s) in %s" % (res[b'EXISTS'], folder))
@arg('--export', type=str, help='Export messages')
@arg('--unread', action='store_true', help='Treat even if unread')
@arg('folder')
@arg('days', type=int)
def do_older(self, args):
lst = self.get_older(args.folder, args.days, unread=args.unread)
if args.export:
self.export(lst, os.path.expanduser(args.export))
print(lst)
@arg('--limit', type=int, help='Treat only this number of messages')
@arg('--export', type=str, help='Export messages before deletion')
@arg('--unread', action='store_true', help='Delete even if unread')
@arg('folder')
@arg('days', type=int)
def do_purge_older(self, args):
folder = args.folder
lst = self.get_older(folder, args.days, unread=args.unread)
if not lst:
return
if args.limit:
lst = lst[:args.limit]
if args.export:
self.export(lst, os.path.expanduser(args.export))
self.client.delete_messages(lst)
self.client.expunge()
if args.verbose:
print("Purged %s message%s from %s" % \
(len(lst), '' if len(lst)==1 else 's', folder))
@arg('--unread', action='store_true', help='Trash even if unread')
@arg('folder')
@arg('days', type=int)
def do_trash_older(self, args):
if self.trash is None:
raise RuntimeError('No trash folder defined for %s' % self.acct)
folder = args.folder
lst = self.get_older(folder, args.days, unread=args.unread)
if not lst:
return
self.client.copy(lst, self.trash)
self.client.delete_messages(lst)
if args.verbose:
print("Deleted %s message%s from %s" % \
(len(lst), '' if len(lst)==1 else 's', folder))
def get_older(self, folder, days, unread=False):
self.client.select_folder(folder)
date = dt.date.today() - dt.timedelta(days=days)
maybe_seen = () if unread else ('SEEN',)
lst = self.client.search(maybe_seen +
('UNDELETED',
'BEFORE', date))
return lst
@arg('--delete', action='store_true', help='Delete messages after export')
@arg('--limit', type=int, help='Export only this number of messages')
@arg('--output', type=str, default='/dev/stdout', help='Output file')
@arg('folder')
@arg('seq', nargs='*', type=int)
def do_export(self, args):
folder = args.folder
seqs = args.seq
self.client.select_folder(folder)
if len(seqs) == 0:
lst = self.client.search()
if args.limit:
lst = lst[:args.limit]
else:
lst = [int(a) for a in seqs]
#print(lst)
# res = self.client.fetch(lst, ['ENVELOPE', 'RFC822.HEADER','RFC822.TEXT'])
self.export(lst, os.path.expanduser(args.output))
if args.delete:
self.client.delete_messages(lst)
self.client.expunge()
def export(self, lst, output):
if os.path.isdir(output):
self.export_emails(lst, output)
else:
self.export_mboxo(lst, output)
def export_emails(self, lst, outdir):
"""Export listed messages from current folder as separate messages"""
res = self.client.fetch(lst, ['RFC822.HEADER','RFC822.TEXT'])
for uid in lst:
outfile = os.path.join(outdir, '%d.eml' % uid)
with open(outfile, 'wb') as out:
hdr = res[uid][b'RFC822.HEADER']
text = res[uid][b'RFC822.TEXT']
out.write(hdr)
out.write(text)
def export_mboxo(self, lst, outfile):
'''Export listed messages from current folder to a file.
The file is written in mboxo format.
'''
res = self.client.fetch(lst, ['RFC822.HEADER','RFC822.TEXT'])
with open(outfile, 'wb') as out:
for uid in lst:
# Envelope cam contain invalid date e.g. "Tue, Wed, 18 June 2014 00:42:51 -0800"
# So don't even fetch that
#env = res[uid]['ENVELOPE']
#from_ = env.from_[0]
#out.write('From %s@%s\n' % (from_.mailbox, from_.host))
out.write(b'From ???@???\n')
hdr = res[uid][b'RFC822.HEADER']
text = res[uid][b'RFC822.TEXT']
#assert not text.startswith('From ')
#assert '\nFrom ' not in text
out.write(hdr)
#out.write(text.encode('latin-1'))
xtext, nsubs = From_RE.subn(b'>From ', text)
out.write(xtext)
out.write(b'\n')
if __name__=='__main__':
ImapUtil().main()