-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaunch_worker_mail.py
308 lines (263 loc) · 12.2 KB
/
launch_worker_mail.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# This file is part of Open-Capture For MEM Courrier.
# Open-Capture 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.
# Open-Capture For MEM Courrier 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 Open-Capture For MEM Courrier. If not, see <https://www.gnu.org/licenses/>.
# @dev : Nathan Cheval <nathan.cheval@outlook.fr>
import os
import sys
import argparse
import tempfile
import datetime
from src.main import launch
from src.classes.SMTP import SMTP
import src.classes.Log as logClass
import src.classes.Mail as mailClass
import src.classes.Config as configClass
import src.classes.WebServices as webserviceClass
def str2bool(value):
"""
Function to convert string to boolean
:return: Boolean
"""
return value.lower() in "true"
def convert_to_dict(message):
new_msg = {
'uid': message.uid,
'obj': message.obj,
'subject': message.subject,
'from': message.from_,
'to': message.to,
'cc': message.cc,
'bcc': message.bcc,
'reply_to': message.reply_to,
'date': message.date,
'headers': message.headers,
'text': message.text,
'html': message.html,
'attachments': [],
'from_values': message.from_values,
'to_values': message.to_values,
'cc_values': message.cc_values,
'bcc_values': message.bcc_values,
'reply_to_values': message.reply_to_values
}
for att in message.attachments:
new_msg['attachments'].append({
'filename': att.filename,
'payload': att.payload,
'content_id': att.content_id,
'content_type': att.content_type,
'size': att.size,
'content_disposition': att.content_disposition,
'part': att.part
})
return new_msg
def check_folders(folder_crawl, folder_dest=False):
"""
Check if IMAP folder exist
:param folder_crawl: IMAP source folder
:param folder_dest: IMAP destination folder (if action is made to move or delete)
:return: Boolean
"""
if not Mail.check_if_folder_exist(folder_crawl):
print('The folder to crawl "' + folder_to_crawl + '" doesnt exist')
return False
else:
if folder_dest is not False:
if not Mail.check_if_folder_exist(folder_dest, True):
print('The destination folder "' + str(folder_dest) + '" doesnt exist')
return False
return True
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--config", required=True, help="path to config.ini")
ap.add_argument("-cm", "--config_mail", required=True, help="path to mail.ini")
ap.add_argument('-p', "--process", required=True, default='MAIL_1')
ap.add_argument("-s", '--script', required=False, help="Script name")
args = vars(ap.parse_args())
if not os.path.exists(args['config']) or not os.path.exists(args['config_mail']):
sys.exit('config file couldn\'t be found')
process = args['process']
print('Start process : ' + process)
config = configClass.Config()
config.load_file(args['config'])
config_mail = configClass.Config()
config_mail.load_file(args['config_mail'])
if config_mail.cfg.get(process) is None:
sys.exit('Process ' + process + ' is not set into ' + args['config_mail'] + ' file')
global_log = logClass.Log(config.cfg['GLOBAL']['logfile'])
now = datetime.datetime.now()
path = config_mail.cfg['GLOBAL']['batchpath'] + '/' + process + '/' + str('%02d' % now.year) + str('%02d' % now.month) + str('%02d' % now.day) + '/'
path_without_time = config_mail.cfg['GLOBAL']['batchpath']
web_service = webserviceClass.WebServices(
config.cfg['OCForMEM']['host'],
config.cfg['OCForMEM']['user'],
config.cfg['OCForMEM']['password'],
global_log,
config.cfg['GLOBAL']['timeout'],
config.cfg['OCForMEM']['certpath']
)
SMTP = SMTP(
config_mail.cfg['GLOBAL']['smtp_notif_on_error'],
config_mail.cfg['GLOBAL']['smtp_host'],
config_mail.cfg['GLOBAL']['smtp_port'],
config_mail.cfg['GLOBAL']['smtp_login'],
config_mail.cfg['GLOBAL']['smtp_pwd'],
config_mail.cfg['GLOBAL']['smtp_ssl'],
config_mail.cfg['GLOBAL']['smtp_starttls'],
config_mail.cfg['GLOBAL']['smtp_dest_admin_mail'],
config_mail.cfg['GLOBAL']['smtp_delay'],
config_mail.cfg['GLOBAL']['smtp_auth'],
config_mail.cfg['GLOBAL']['smtp_from_mail']
)
Mail = mailClass.Mail(
config_mail.cfg[process]['auth_method'],
config_mail.cfg['OAUTH'],
config_mail.cfg['EXCHANGE'],
config_mail.cfg['GRAPHQL'],
config_mail.cfg[process]['host'],
config_mail.cfg[process]['port'],
config_mail.cfg[process]['login'],
config_mail.cfg[process]['password'],
web_service,
SMTP,
)
cfg = config_mail.cfg[process]
secured_connection = cfg['securedconnection']
action = cfg['actionafterprocess']
folder_to_crawl = cfg['foldertocrawl']
folder_destination = cfg['folderdestination']
import_only_attachments = str2bool(cfg['importonlyattachments'])
priority_mail_subject = str2bool(config_mail.cfg[process]['prioritytomailsubject'])
priority_mail_date = str2bool(config_mail.cfg[process]['prioritytomaildate'])
priority_mail_from = str2bool(config_mail.cfg[process]['prioritytomailfrom'])
is_form = str2bool(config_mail.cfg[process]['isform'])
force_utf8 = str2bool(config_mail.cfg[process]['forceutf8'])
add_mail_headers_in_body = str2bool(config_mail.cfg[process]['addmailheadersinbody'])
Mail.test_connection(secured_connection)
extensionsAllowed = []
for extension in config_mail.cfg[process]['extensionsallowed'].split(','):
extensionsAllowed.append(extension.strip().lower())
if action == 'move':
check = check_folders(folder_to_crawl, folder_destination)
else:
check = check_folders(folder_to_crawl)
if check:
Mail.select_folder(folder_to_crawl)
emails = Mail.retrieve_message(folder_to_crawl)
if len(emails) > 0:
now = datetime.datetime.now()
if not os.path.exists(path):
os.makedirs(path)
year, month, day = [str('%02d' % now.year), str('%02d' % now.month), str('%02d' % now.day)]
hour, minute, second, microsecond = [str('%02d' % now.hour), str('%02d' % now.minute), str('%02d' % now.second), str('%02d' % now.microsecond)]
date_batch = year + month + day + '_' + hour + minute + second + microsecond
batch_path = tempfile.mkdtemp(dir=path, prefix='BATCH_' + date_batch + '_')
print('Batch path : ' + batch_path)
print('Batch error name : ' + batch_path.split('/MailCollect')[0] + '/MailCollect/_ERROR/' + batch_path.split('/MailCollect/')[1])
Log = logClass.Log(batch_path + '/' + date_batch + '.log')
Log.info('Start following batch : ' + os.path.basename(os.path.normpath(batch_path)))
Log.info('Import only attachments is : ' + str(import_only_attachments))
Log.info('Action after processing e-mail is : ' + action)
Log.info('Number of e-mail to process : ' + str(len(emails)))
i = 1
already_processed_uid = []
if os.path.exists(path_without_time + f'/unique_id_already_processed_{process}'):
with open(path_without_time + f'/unique_id_already_processed_{process}', 'r', encoding='UTF-8') as uid_file:
already_processed_uid = list(filter(None, uid_file.read().split(';')))
uid_file.close()
for msg in emails:
if Mail.auth_method == 'exchange':
msg_id = str(msg.conversation_id.id)
elif Mail.auth_method == 'graphql':
msg_id = str(msg['id'])
else:
msg = convert_to_dict(msg)
msg_id = str(msg['uid'])
if msg_id in already_processed_uid:
Log.info('E-mail with unique id ' + msg_id + ' already processed, skipping...')
continue
if config_mail.cfg[process].get('store_unique_id_already_processed') is None or str2bool(config_mail.cfg[process]['store_unique_id_already_processed']):
with open(path_without_time + f'/unique_id_already_processed_{process}', 'a',
encoding='UTF-8') as uid_file:
uid_file.write(msg_id + ';')
uid_file.close()
# Backup all the e-mail into batch path
Mail.backup_email(msg, batch_path, force_utf8, add_mail_headers_in_body, Log)
ret, file = Mail.construct_dict_before_send_to_mem(msg, config_mail.cfg[process], batch_path, Log)
_from = ret['mail']['emailFrom']
if Mail.auth_method == 'exchange':
document_date = msg.datetime_created
elif Mail.auth_method == 'graphql':
document_date = datetime.datetime.strptime(msg['receivedDateTime'], '%Y-%m-%dT%H:%M:%SZ')
else:
document_date = msg['date']
if not import_only_attachments:
launch({
'cpt': str(i),
'file': file,
'from': _from,
'isMail': True,
'isForm': is_form,
'msg_uid': str(msg_id),
'msg': {'date': document_date.strftime('%d/%m/%Y %H:%M:%S'), 'subject': msg['subject'], 'uid': msg_id},
'process': process,
'data': ret['mail'],
'config': args['config'],
'script': args['script'],
'config_mail': args['config_mail'],
'batch_path': batch_path,
'nb_of_mail': str(len(emails)),
'attachments': ret['attachments'],
'extensionsAllowed': extensionsAllowed,
'log': batch_path + '/' + date_batch + '.log',
'priority_mail_subject': priority_mail_subject,
'priority_mail_date': priority_mail_date,
'priority_mail_from': priority_mail_from,
'error_path': path_without_time + '/_ERROR/' + process + '/' + year + month + day
})
else:
Log.info('Start to process only attachments')
if len(ret['attachments']) > 0:
Log.info('Found ' + str(len(ret['attachments'])) + ' attachments')
cpt = 1
for attachment in ret['attachments']:
if attachment['format'].lower() == 'pdf':
launch({
'cpt': str(cpt),
'script': args['script'],
'isMail': 'attachments',
'data': ret['mail'],
'from': _from,
'process': process,
'config': args['config'],
'config_mail': args['config_mail'],
'file': attachment['file'],
'format': attachment['format'],
'priority_mail_date': priority_mail_date,
'priority_mail_from': priority_mail_from,
'priority_mail_subject': priority_mail_subject,
'log': batch_path + '/' + date_batch + '.log'
})
else:
Log.error('Attachment n°' + str(cpt) + ' is not a pdf file')
cpt += 1
else:
Log.info('No attachments found')
if action not in ['move', 'delete', 'none']:
action = 'none'
if action == 'move':
Log.info('Move mail into archive folder : ' + folder_destination)
Mail.move_to_destination_folder(msg, folder_destination, Log)
i = i + 1
else:
sys.exit('Folder do not contain any e-mail. Exit...')
else:
sys.exit(0)