Repository navigation
Expand file tree
/
Copy pathimapbox.py
More file actions
305 lines (238 loc) · 13.2 KB
/
Copy pathimapbox.py
File metadata and controls
305 lines (238 loc) · 13.2 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from mailboxresource import save_emails, get_folders
from dsn import get_account, input_dsn
import argparse
import configparser
import os
import sys
import signal
import getpass
from utilities import errorHandler, get_version, is_docker, imaputf7decode, DollarInterpolation
from search import do_search
from server import start_server
from hooks import dispatch, dispatch_status, make_mail_item, make_status_item, account_directories, all_directories, parse_hook_entry, split_hook_entries, join_hooks, set_config_dir
def load_configuration(args):
config = configparser.ConfigParser(allow_no_value=True, interpolation=DollarInterpolation(), allow_unnamed_section=True)
if args.specific_config:
locations = args.specific_config
else:
locations = ['./config.cfg', '/etc/imapbox/config.cfg', os.path.expanduser('~/.config/imapbox/config.cfg')]
read_files = config.read(locations)
if read_files:
set_config_dir(os.path.dirname(os.path.abspath(read_files[0])))
options = {
'days': None,
'local_folder': '.',
'wkhtmltopdf': None,
'specific_folders': False,
'test_only': False,
'search_filter': None,
'search_output': None,
'input_dsn': False,
'server': None,
'accounts': [],
'hooks': {},
'_hookbuffer': []
}
# set default folders, if within a docker container
if is_docker():
options['local_folder'] = '/var/imapbox'
options['wkhtmltopdf'] = '/usr/bin/wkhtmltopdf'
if config.has_section('imapbox'):
if config.has_option('imapbox', 'days'):
options['days'] = config.getint('imapbox', 'days')
if config.has_option('imapbox', 'local_folder'):
options['local_folder'] = os.path.expanduser(config.get('imapbox', 'local_folder'))
if config.has_option('imapbox', 'wkhtmltopdf'):
options['wkhtmltopdf'] = os.path.expanduser(config.get('imapbox', 'wkhtmltopdf'))
if config.has_option('imapbox', 'specific_folders'):
options['specific_folders'] = config.getboolean('imapbox', 'specific_folders')
if config.has_option('imapbox', 'test_only'):
if config.get('imapbox', 'test_only') == 'folders':
options['test_only'] = 'folders'
else:
options['test_only'] = config.getboolean('imapbox', 'test_only')
if config.has_option('imapbox', 'server'):
options['server'] = config.get('imapbox', 'server').strip()
try:
for option, value in config.items('imapbox'):
if option not in ('hook', 'hooks'):
continue
for entry in split_hook_entries(value):
event, target, hook_args = parse_hook_entry(entry)
options['hooks'].setdefault(event, []).append((target, tuple(hook_args)))
except (configparser.Error, ValueError) as e:
errorHandler(e, 'Invalid hook in config (expected "event,\\"target\\"[, ...]")')
if args.specific_dsn:
for dsn in args.specific_dsn:
try:
account = get_account(dsn)
if None == account['host'] or None == account['username'] or None == account['password']:
raise ValueError('host / username or password not set')
except Exception as e:
errorHandler(e, 'Invalid DSN (' + dsn + ')')
options['accounts'].append(account)
else:
for section in config.sections():
if (section == configparser.UNNAMED_SECTION):
continue
if ('imapbox' == section):
continue
if args.specific_account and (args.specific_account != section):
continue
# ! updating this, you should also update dsn.py
account = {
'name': section,
'host': None,
'port': 993,
'username': None,
'password': None,
'remote_folder': 'INBOX', # String (might contain a comma separated list of folders)
'exclude_folder': None, # String (might contain a comma separated list of folders)
'ssl': False,
}
if config.has_option(section, 'dsn'):
account = get_account(config.get(section, 'dsn'), account['name'])
if config.has_option(section, 'host'):
account['host'] = config.get(section, 'host')
if config.has_option(section, 'port'):
account['port'] = config.get(section, 'port')
if config.has_option(section, 'username'):
account['username'] = config.get(section, 'username')
if config.has_option(section, 'password'):
account['password'] = config.get(section, 'password')
elif not account['password']:
try:
prompt=('Password for ' + account['username'] + ':' + account['host'] + ': ')
account['password'] = getpass.getpass(prompt=prompt)
except Exception as e:
errorHandler(e, 'No password set for account {}. Could not ask for password (no CLI?)'.format(section), exitCode=None)
if config.has_option(section, 'ssl'):
if config.get(section, 'ssl').lower() == "true":
account['ssl'] = True
if config.has_option(section, 'remote_folder'):
account['remote_folder'] = config.get(section, 'remote_folder')
if config.has_option(section, 'exclude_folder'):
account['exclude_folder'] = config.get(section, 'exclude_folder')
if None == account['host'] or None == account['username'] or None == account['password']:
errorHandler(section, 'Invalid account')
continue
options['accounts'].append(account)
if args.local_folder:
options['local_folder'] = args.local_folder
if args.days:
options['days'] = args.days
if args.wkhtmltopdf:
options['wkhtmltopdf'] = args.wkhtmltopdf
if args.specific_folders:
options['specific_folders'] = True
if args.test_only:
if args.test_only == 'folders':
options['test_only'] = 'folders'
else:
options['test_only'] = True
if args.search_filter:
options['search_filter'] = args.search_filter
if args.search_output:
options['search_output'] = args.search_output
if args.input_dsn:
options['input_dsn'] = args.input_dsn
if args.server:
options['server'] = args.server
if args.hooks:
for entry in args.hooks:
try:
event, target, hook_args = parse_hook_entry(entry)
except ValueError:
errorHandler(entry, 'Invalid --hook (expected "event,\\"command\\"[,\\"arg\\",...]")')
continue
options['hooks'].setdefault(event, []).append((target, tuple(hook_args)))
if args.show_version:
print(get_version())
sys.exit(0)
return options
def main():
argparser = argparse.ArgumentParser(description='Dump a IMAP folder into .eml, .txt and .html files, and optionally convert them into PDFs.\nBananaAcid Edition\n\n' + get_version('version: '), formatter_class=argparse.RawTextHelpFormatter)
argparser.add_argument('-l', '--local-folder', dest='local_folder', metavar='PATH', help='Local folder where to create the email folders')
argparser.add_argument('-d', '--days', dest='days', metavar='NUMBER', help='Number of days back to get in the IMAP account', type=int)
argparser.add_argument('-n', '--dsn', dest='specific_dsn', metavar='DSN', help='Use a specific DSN as account like imap[s]://username:password@host:port/folder,folder', action='append')
argparser.add_argument('-a', '--account', dest='specific_account', metavar='ACCOUNT', help='Select a specific account section from the config to backup')
argparser.add_argument('-f', '--folders', dest='specific_folders', help='Backup into specific account subfolders', action='store_true')
argparser.add_argument('-w', '--wkhtmltopdf', dest='wkhtmltopdf', metavar='PATH', help='The location of the wkhtmltopdf binary. By default it will be located automatically if no path is provided.', default='', nargs='?')
argparser.add_argument('-t', '--test', dest='test_only', nargs='?', const=True, default=False, metavar='"folders"', help='Only a connection and folder retrieval test will be performed, adding the optional "folders" as parameter will also show the found folders')
argparser.add_argument('-c', '--config', dest='specific_config', metavar='PATH', help='Path to a config file to use')
argparser.add_argument('-v', '--version', dest='show_version', help='Show the current version', action='store_true')
argparser.add_argument('-s', '--search', dest='search_filter', metavar='FILTER', help='Search in backed-up emails (Filter: `Keyword,\"fnmatch syntax\"`)')
argparser.add_argument('-so', '--search-output', dest='search_output', metavar='"text"|"json"', help='Search result output type (default: "text")', default="text", choices=['text', 'json'])
argparser.add_argument('-i', '--input-dsn', dest='input_dsn', nargs='?', const=True, default=False, metavar='"gui"', help='Helper to generate a DSN string, adding the optional "gui" parameter will open the DSN generator in a GUI, can be used with --test')
argparser.add_argument('--server', dest='server', metavar='CRONTABSTRING', help='Starts as a server, triggering with the specified cron string')
argparser.add_argument('--hook', dest='hooks', metavar='EVENT,"TARGET"', help='Run the specified TARGET whenever the EVENT happens (EVENT: serverstart, accountstart, newmail, accountdone, newmails, done, error, or all). TARGET is a URL (JSON posted via HTTP, defaults to POST, prefix "get+" to use GET) or an executable (JSON piped to its stdin). e.g. `--hook newmail,"./hook-addToElasticSearch.sh"` or `--hook all,"http://host.docker.internal:8088/"`. Can be repeated.', action='append')
args = argparser.parse_args()
options = load_configuration(args)
if options['search_filter']:
do_search(options)
if options['input_dsn']:
if options['input_dsn'] == 'gui':
try:
from gui import open_gui # placed here, because it might not be needed on load, so loading speeds up
open_gui(options)
except ModuleNotFoundError:
errorHandler(None, 'Python module not available. Please install kivy.')
else:
options = input_dsn(options)
if not options['test_only']:
sys.exit(0)
if not options['accounts']:
argparser.print_help()
sys.exit(1)
if options['server']:
start_server(options, do_accounts)
sys.exit(0)
do_accounts(options)
def do_accounts(options):
rootDir = options['local_folder']
for account in options['accounts']:
print('\n{}/{} (on {})'.format(account['name'], account['remote_folder'], account['host']))
if options['test_only']:
try:
folders = get_folders(account)
if options['test_only'] == 'folders':
folder_entries_decoded = imaputf7decode( ', '.join(folders) )
print(' - Folders:', folder_entries_decoded)
print(' - SUCCESS: Login and folder retrieval')
except:
errorHandler(None, ' - FAILED: Login and folder retrieval', exitCode=None)
continue
if options['specific_folders']:
basedir = os.path.join(rootDir, account['name'])
else:
basedir = rootDir
dispatch_status(options['hooks'], ['accountstart'], make_status_item('accountstart', account, basedir, [basedir]))
try:
if account['remote_folder'] == "__ALL__":
folders = get_folders(account)
else:
folders = str.split(account['remote_folder'], ',')
for folder_entry in folders:
folder_entry_decoded = imaputf7decode(folder_entry);
print("Saving folder: " + folder_entry_decoded)
folder_account = dict(account)
folder_account['remote_folder'] = folder_entry
local_folder = os.path.join(basedir, folder_entry_decoded.replace('"', ''))
save_emails(folder_account, options, local_folder)
except Exception as e:
dispatch(options['hooks'], 'error', make_mail_item('error', account, None, None, success=False, error={'error': str(e)}))
errorHandler(e, ' - FAILED')
dispatch_status(options['hooks'], ['accountdone'], make_status_item('accountdone', account, basedir, account_directories(options['_hookbuffer'], account['name'])))
dispatch_status(options['hooks'], ['done', 'newmails'], make_status_item('done', None, rootDir, all_directories(options['_hookbuffer'])))
options['_hookbuffer'].clear()
join_hooks()
def sigint_handler(signal, frame):
try:
sys.exit(130)
except SystemExit:
os._exit(130)
signal.signal(signal.SIGINT, sigint_handler)
if __name__ == '__main__':
main()