-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimapcp.py
executable file
·258 lines (219 loc) · 9.33 KB
/
imapcp.py
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
# kate: space-indent on; tab-indent off;
""" @package docstring
IMAP Copy
Copy emails and folders from an IMAP account to another.
Creates missing folders and skips existing messages (using message-id).
Source IMAP is always accessed READ-ONLY.
@author Gabriele Tozzi <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import imaplib
import sys
import re
import pprint
import email
import datetime
from optparse import OptionParser
from imaputil import ImapUtil
class main(ImapUtil):
NAME = 'imapcp'
VERSION = '0.5'
def run(self):
# Init pretty printer
pp = pprint.PrettyPrinter(indent = 2)
# Read command line
usage = "%prog <suser>:<spassword>:<shost>:<sport> <duser>:<dpassword>:<dhost>:<dport>"
parser = OptionParser(usage=usage, version=self.NAME + ' ' + self.VERSION)
parser.add_option("-e", "--exclude", dest="exclude", action='append',
help="Exclude folders matching pattern (can be specified multiple times)")
parser.add_option("-f", "--folder", dest="folder",
help="Only copy a single folder (use from:to to specify a different destinatin name)")
parser.add_option("-s", "--simulate", dest="simulate", action='store_true',
help="Do not perform any task")
parser.add_option("-t", "--trim", dest="trim", action='store_true',
help="Trim folder names")
parser.add_option("-k", "--skel", dest="skel", action='store_true',
help="Only copy folder structure")
parser.add_option("--from", dest="fr",
help="Only copy messages older than this date (inclusive)")
parser.add_option("--to", dest="to",
help="Only copy messages newer than this date (inclusive)")
parser.add_option("--ignore", dest="ignore", action="append",
help="Ignore given message id (without '<>', may be specified multiple times")
(options, args) = parser.parse_args()
# Parse ignore list
ignores = []
if options.ignore:
for ignore in options.ignore:
ignores.append('<' + ignore + '>')
print("Ignoring %s" % ignores)
# Parse exclude list
excludes = []
if options.exclude:
for e in options.exclude:
excludes.append(re.compile(e.encode('ascii')))
# Parse from/to dates
fr = None
if options.fr:
fr = datetime.date(*[int(i) for i in options.fr.split('-')])
if fr:
print("Only copying messages newer than %s (included)" % fr)
to = None
if options.to:
to = datetime.date(*[int(i) for i in options.to.split('-')])
if to:
print("Only copying messages older than %s (included)" % to)
# Parse single folder
folder = options.folder.split(':') if options.folder else None
if folder and len(folder) < 2:
folder.append(folder[0])
if folder:
folder[0] = folder[0].encode()
folder[1] = folder[1].encode()
print("Only copying folder %s to folder %s" % (folder[0], folder[1]))
# Parse mandatory arguments
if len(args) < 2:
parser.error("invalid number of arguments")
src = args[0].split(':')
src = {
'user': src[0],
'pass': src[1],
'host': src[2] if len(src) > 2 else 'localhost',
'port': int(src[3]) if len(src) > 3 else 143,
}
dst = args[1].split(':')
dst = {
'user': dst[0],
'pass': dst[1],
'host': dst[2] if len(dst) > 2 else 'localhost',
'port': int(dst[3]) if len(dst) > 3 else 143,
}
# Make connections and authenticate
if src['port'] == 993:
srcconn = imaplib.IMAP4_SSL(src['host'], src['port'])
else:
srcconn = imaplib.IMAP4(src['host'], src['port'])
srcconn.login(src['user'], src['pass'])
srctype, srcdescr = self.getServerType(srcconn)
print("Source server type is", srcdescr)
if dst['port'] == 993:
dstconn = imaplib.IMAP4_SSL(dst['host'], dst['port'])
else:
dstconn = imaplib.IMAP4(dst['host'], dst['port'])
dstconn.login(dst['user'], dst['pass'])
dsttype, dstdescr = self.getServerType(dstconn)
print("Destination server type is", dstdescr)
print("Source folders:")
srcfolders = self.listMailboxes(srcconn)
for f in srcfolders:
print(f)
print("Destination folders:")
dstfolders = self.listMailboxes(dstconn)
for f in dstfolders:
print(f)
# Syncing every source folder
for f in srcfolders:
# Translate folder name
srcfolder = f.name
dstfolder = f.getPathBytes(dsttype, trim=options.trim)
# Check for folder in exclusion/inclusion list
skip = False
if folder:
if folder[0] != srcfolder:
skip = True
elif folder[1]:
dstfolder = folder[1]
else:
for e in excludes:
if e.match(srcfolder):
skip = True
break
if skip:
print("Skipping", srcfolder, "(excluded)")
continue
print("Syncing", srcfolder, 'into', dstfolder)
# Create dst mailbox when missing
dstconn.create(self.quoteFolderName(dstfolder))
# Select source mailbox readonly
res, data = srcconn.select(self.quoteFolderName(srcfolder), True)
if res == 'NO' and srctype == 'exchange' and 'special mailbox' in data[0]:
print("Skipping special Microsoft Exchange Mailbox", srcfolder)
continue
assert res == 'OK', (res, data)
res, data = dstconn.select(self.quoteFolderName(dstfolder), False)
if res == 'OK':
pass
elif res == 'NO':
print('Error selecting folder: {}, trying to create it'.format(str(data)))
# Create and try again
res, data = dstconn.create(self.quoteFolderName(dstfolder))
if res != 'OK':
raise RuntimeError('Error creating mailboxr "{}": {}'.format(dstfolder.decode(), str(data)))
res, data = dstconn.select(self.quoteFolderName(dstfolder), False)
assert res == 'OK', (res, data)
else:
assert False, (res, data)
# Stop here if only copying skeleton
if options.skel:
print("Skipping message copy")
continue
# Fetch all destination messages imap IDS
dstids = self.listMessages(dstconn)
print("Found", len(dstids), "messages in destination folder")
# Fetch destination messages ID
print("Acquiring destination message IDs...", end='', flush=True)
dstmexids = []
for idx, did in enumerate(dstids):
if idx % 100 == 0:
print('.', end='', flush=True)
dstmexids.append(self.getMessageId(dstconn, did))
print(len(dstmexids), "message IDs acquired.")
# Fetch all source messages imap IDS
srcids = self.listMessages(srcconn)
print("Found", len(srcids), "messages in source folder")
# Sync data
for sid in srcids:
# Check for date filter
if fr or to:
h = self.getHeaders(srcconn, sid)
if 'date' not in h:
continue
d = email.utils.parsedate(h['date'])
if not d:
continue
date = datetime.date(d[0], d[1], d[2])
if fr and date < fr:
continue
if to and date > to:
continue
# Get message id
mid = self.getMessageId(srcconn, sid)
if mid in ignores:
print("Ignoring message", mid)
elif not mid in dstmexids:
# Message not found, syncing it
print("Copying message", mid)
if not options.simulate:
mex = self.getMessage(srcconn, sid)
dstconn.append(dstfolder, None, None, mex)
else:
print("Skipping message", mid)
# Logout
srcconn.logout()
dstconn.logout()
if options.simulate:
print("Simulated run, no action taken")
if __name__ == '__main__':
app = main()
app.run()
sys.exit(0)