-
Notifications
You must be signed in to change notification settings - Fork 1
/
jmap-backup.py
executable file
·290 lines (249 loc) · 9.92 KB
/
jmap-backup.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python3
"""
Back up a Fastmail JMAP mailbox in .eml format
https://nathangrigg.com/2021/08/fastmail-backup/
https://www.fastmail.com/for-developers/integrating-with-fastmail/
https://www.fastmail.com/for-developers/
https://jmap.io/crash-course.html
"""
import argparse
import collections
import datetime as dt
import os
import string
import sys
import subprocess
import importlib
import json
ADDITIONAL_MODULES = [ 'requests' ]
# prereqs
for module in ADDITIONAL_MODULES:
try:
m = importlib.import_module(module)
globals()[module] = m
except ImportError:
sys.exit(f"{module} module could not be loaded, check README for installation requirements")
def str_to_bool(s):
return s and s.lower() in [ 'true', '1', 'yes', 'on' ]
Session = collections.namedtuple('Session', 'headers account_id api_url download_template')
Email = collections.namedtuple('Email', 'id blob_id date subject')
DEBUG = str_to_bool(os.getenv('JMAP_DEBUG'))
NOT_BEFORE = os.getenv('NOT_BEFORE', '2000-01-01')
DEFAULT_CONFIG = '~/.jmapbackup/fastmail.json'
CONNECT_TIMEOUT = 3
READ_TIMEOUT = 20
def dbg(*args, newline=True):
if not DEBUG:
return
s = ' '.join(map(str, args))
if newline:
print(s, file=sys.stderr)
else:
print(s, file=sys.stderr, end='')
def get_session(token):
headers = {'Authorization': 'Bearer ' + token}
r = requests.get('https://api.fastmail.com/.well-known/jmap',
headers=headers,
timeout=(CONNECT_TIMEOUT, READ_TIMEOUT))
dbg("Status code (get_session):", r.status_code)
dbg("Response text (get_session):", r.text)
[account_id] = list(r.json()['accounts'])
api_url = r.json()['apiUrl']
download_template = r.json()['downloadUrl']
return Session(headers, account_id, api_url, download_template)
def query(session, start, end):
json_request = {
'using': ['urn:ietf:params:jmap:core', 'urn:ietf:params:jmap:mail'],
'methodCalls': [
[
'Email/query',
{
'accountId': session.account_id,
'sort': [{'property': 'receivedAt', 'isAscending': False}],
'filter': {
'after': start.strftime('%Y-%m-%dT%H:%M:%SZ'),
'before': end.strftime('%Y-%m-%dT%H:%M:%SZ'),
},
'limit': 50,
},
'0',
],
[
'Email/get',
{
'accountId': session.account_id,
'#ids': {
'name': 'Email/query',
'path': '/ids/*',
'resultOf': '0',
},
'properties': ['blobId', 'receivedAt', 'subject'],
},
'1',
],
],
}
dbg("JSON request:", json_request)
while True:
response = requests.post(
session.api_url, json=json_request, headers=session.headers
)
dbg("Status code (query):", response.status_code)
dbg("Response text (query):", response.text)
if response.status_code == 403:
sys.exit("Permission denied: Disallowed capabilities: urn:ietf:params:jmap:mail")
full_response = response.json()
if any(x[0].lower() == 'error' for x in full_response['methodResponses']):
sys.exit(f'Error received from server: {full_response!r}')
response = [x[1] for x in full_response['methodResponses']]
if not response[0]['ids']:
return
for item in response[1]['list']:
date = dt.datetime.fromisoformat(item['receivedAt'].rstrip('Z'))
yield Email(item['id'], item['blobId'], date, item['subject'])
query_request = json_request['methodCalls'][0][1]
query_request['anchor'] = response[0]['ids'][-1]
query_request['anchorOffset'] = 1
def email_filename(email):
subject = (
email.subject.translate(str.maketrans('', '', string.punctuation))[:50]
if email.subject else '')
date = email.date.strftime('%Y%m%d_%H%M%S')
directory = email.date.strftime('%Y-%m')
filename = f'{date}_{email.id}_{subject.strip()}.eml'
return directory, filename
def run_if(cmd):
if cmd:
if os.path.exists(cmd[0]):
dbg(f'executing: `{" ".join(cmd)}`')
subprocess.run(cmd)
else:
print(f'invalid command: {cmd}', file=sys.stderr)
def check_dest_dir(dest_dir, retry=True):
dir_exists = os.path.exists(dest_dir)
if retry or dir_exists:
return dir_exists
else:
sys.exit(f"Error: destination path '{dest_dir}' does not exist (you may need to mount it?)")
def download_email(session, email, base_dir):
try:
directory, filename = email_filename(email)
full_directory = os.path.join(base_dir, directory)
if not os.path.exists(full_directory):
os.makedirs(full_directory)
full_path = os.path.join(full_directory, filename)
r = requests.get(
session.download_template.format(
accountId=session.account_id,
blobId=email.blob_id,
name='email',
type='application/octet-stream',
),
headers=session.headers,
timeout=(CONNECT_TIMEOUT, READ_TIMEOUT)
)
r.raise_for_status()
with open(full_path, 'wb') as fh:
fh.write(r.content)
dbg(f'Downloaded {email.id} {email.date.strftime("%Y-%m-%d %H:%M:%S")}')
except requests.RequestException as e:
dbg(f"Failed to download {email.id}: {e}")
return False
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Back up a Fastmail JMAP mailbox in .eml format', add_help=False)
parser.add_argument('-h','--help', action='store_true', help=argparse.SUPPRESS)
parser.add_argument('-v','--verify', action='store_true', help='Fully verify backed up emails and redownload if missing')
parser.add_argument('-o','--open', action='store_true', help='Open the configured dest_dir in Finder')
parser.add_argument('-c','--config', help=f'Path to config file (default: {DEFAULT_CONFIG})', nargs=1)
args = parser.parse_args()
if args.help:
parser.print_help()
sys.exit(0)
if args.config:
cfg_file = os.path.expanduser(args.config[0])
else:
cfg_file = os.path.expanduser(DEFAULT_CONFIG)
if not os.path.exists(cfg_file):
sys.exit(f"Error: configuration file '{cfg_file}' does not exist")
try:
with open(cfg_file, 'r') as fh:
config = json.load(fh)
except Exception as e:
sys.exit(f'error: {e}')
# parse pre- and post-commands
PRE_COMMAND = [os.path.expanduser(c) for c in config.get('pre_cmd', [])]
POST_COMMAND = [os.path.expanduser(c) for c in config.get('post_cmd', [])]
run_if(PRE_COMMAND)
dest_dir = config['dest_dir']
check_dest_dir(dest_dir, False)
if args.open:
subprocess.run(['open', dest_dir])
#subprocess.run(POST_COMMAND)
sys.exit(0)
#calculate date window
session = get_session(config['token'])
delay_hours = config.get('delay_hours', 24)
end_window = dt.datetime.now(dt.timezone.utc).replace(microsecond=0) - dt.timedelta(
hours=delay_hours
)
# On first run, use 'not_before' if set in config (YYYY-MM-DD); otherwise use NOT_BEFORE var
not_before_str = str(config.get('not_before', NOT_BEFORE))
dbg(f'Will not archive email prior to {not_before_str}')
not_before = dt.datetime.strptime(not_before_str, '%Y-%m-%d').replace(tzinfo=dt.timezone.utc)
if args.verify:
dbg('Verification enabled (this will take longer)')
start_window = not_before
last_verify_count = config.get('last_verify_count', None)
else:
start_window = config.get('last_end_time')
if start_window and isinstance(start_window, str):
start_window = dt.datetime.fromisoformat(start_window)
else:
start_window = not_before
num_results = 0
num_verified = 0
failed_downloads = []
for email in query(session, start_window, end_window):
directory, filename = email_filename(email)
full_directory = os.path.join(dest_dir, directory)
full_path = os.path.join(full_directory, filename)
if os.path.exists(full_path):
dbg(f'{full_path} ok')
else:
if download_email(session, email, dest_dir):
num_results += 1
else:
failed_downloads.append(email)
continue
if args.verify:
num_verified += 1
if num_verified % 100 == 0:
if last_verify_count and last_verify_count > 0:
pct = '{:.1f}'.format((num_verified / last_verify_count) * 100)
dbg(f'\rVerified {pct}% ({num_verified} of {last_verify_count})', newline=False)
else:
dbg(f'\rVerified {num_verified}', newline=False)
dbg('\n')
dbg('Done!')
# retry failed downloads
if failed_downloads:
dbg(f'Retrying {len(failed_downloads)} failed downloads')
for email in failed_downloads:
if download_email(session, email, dest_dir):
num_results += 1
if args.verify:
num_verified += 1
else:
dbg(f'Failed to download {email.id} after retry')
if args.verify:
print(f'Verified: {num_verified}')
print(f'Archived: {num_results}')
if isinstance(end_window, dt.datetime):
end_window = end_window.strftime('%Y-%m-%dT%H:%M:%SZ')
config['last_end_time'] = end_window
if num_verified > 0:
config['last_verify_count'] = num_verified
with open(cfg_file, 'w') as fh:
json.dump(config, fh, indent=4)
run_if(POST_COMMAND)