|
@@ -196,7 +196,6 @@ class MailFetcher(object):
|
196
|
196
|
:param token: token to authenticate http connexion
|
197
|
197
|
"""
|
198
|
198
|
self._connection = None
|
199
|
|
- self._mails = []
|
200
|
199
|
self.host = host
|
201
|
200
|
self.port = port
|
202
|
201
|
self.user = user
|
|
@@ -212,15 +211,18 @@ class MailFetcher(object):
|
212
|
211
|
while self._is_active:
|
213
|
212
|
time.sleep(self.delay)
|
214
|
213
|
self._connect()
|
215
|
|
- self._fetch()
|
216
|
|
- self._notify_tracim()
|
|
214
|
+ mails = self._fetch()
|
|
215
|
+ # TODO - G.M - 2017-11-22 retry sending unsended mail
|
|
216
|
+ # These mails are return by _notify_tracim, flag them with "unseen"
|
|
217
|
+ # or store them until new _notify_tracim call
|
|
218
|
+ self._notify_tracim(mails)
|
217
|
219
|
self._disconnect()
|
218
|
220
|
|
219
|
221
|
def stop(self) -> None:
|
220
|
222
|
self._is_active = False
|
221
|
223
|
|
222
|
224
|
def _connect(self) -> None:
|
223
|
|
- # FIXME - G.M - 2017-11-15 Verify connection/disconnection
|
|
225
|
+ # TODO - G.M - 2017-11-15 Verify connection/disconnection
|
224
|
226
|
# Are old connexion properly close this way ?
|
225
|
227
|
if self._connection:
|
226
|
228
|
self._disconnect()
|
|
@@ -239,16 +241,17 @@ class MailFetcher(object):
|
239
|
241
|
self._connection.logout()
|
240
|
242
|
self._connection = None
|
241
|
243
|
|
242
|
|
- def _fetch(self) -> None:
|
|
244
|
+ def _fetch(self) -> list:
|
243
|
245
|
"""
|
244
|
246
|
Get news message from mailbox
|
|
247
|
+ :return: list of new mails
|
245
|
248
|
"""
|
246
|
|
-
|
|
249
|
+ mails = []
|
247
|
250
|
# select mailbox
|
248
|
251
|
rv, data = self._connection.select(self.folder)
|
249
|
252
|
if rv == 'OK':
|
250
|
253
|
# get mails
|
251
|
|
- # FIXME - G.M - 2017-11-15 Which files to select as new file ?
|
|
254
|
+ # TODO - G.M - 2017-11-15 Which files to select as new file ?
|
252
|
255
|
# Unseen file or All file from a directory (old one should be moved/
|
253
|
256
|
# deleted from mailbox during this process) ?
|
254
|
257
|
rv, data = self._connection.search(None, "(UNSEEN)")
|
|
@@ -259,7 +262,7 @@ class MailFetcher(object):
|
259
|
262
|
if rv == 'OK':
|
260
|
263
|
msg = message_from_bytes(data[0][1])
|
261
|
264
|
decodedmsg = DecodedMail(msg)
|
262
|
|
- self._mails.append(decodedmsg)
|
|
265
|
+ mails.append(decodedmsg)
|
263
|
266
|
else:
|
264
|
267
|
log = 'IMAP : Unable to get mail : {}'
|
265
|
268
|
logger.debug(self, log.format(str(rv)))
|
|
@@ -269,11 +272,17 @@ class MailFetcher(object):
|
269
|
272
|
else:
|
270
|
273
|
log = 'IMAP : Unable to open mailbox : {}'
|
271
|
274
|
logger.debug(self, log.format(str(rv)))
|
|
275
|
+ return mails
|
272
|
276
|
|
273
|
|
- def _notify_tracim(self) -> None:
|
274
|
|
- unsended_mail = []
|
275
|
|
- while self._mails:
|
276
|
|
- mail = self._mails.pop()
|
|
277
|
+ def _notify_tracim(self, mails: list) -> list:
|
|
278
|
+ """
|
|
279
|
+ Send http request to tracim endpoint
|
|
280
|
+ :param mails: list of mails to send
|
|
281
|
+ :return: unsended mails
|
|
282
|
+ """
|
|
283
|
+ unsended_mails = []
|
|
284
|
+ while mails:
|
|
285
|
+ mail = mails.pop()
|
277
|
286
|
msg = {'token': self.token,
|
278
|
287
|
'user_mail': mail.get_from_address(),
|
279
|
288
|
'content_id': mail.get_key(),
|
|
@@ -283,7 +292,7 @@ class MailFetcher(object):
|
283
|
292
|
try:
|
284
|
293
|
r = requests.post(self.endpoint, json=msg)
|
285
|
294
|
if r.status_code not in [200, 204]:
|
286
|
|
- log = 'bad status code response when sending mail to tracim: {}' # nopep8
|
|
295
|
+ log = 'bad status code response when sending mail to tracim: {}' # nopep8
|
287
|
296
|
logger.error(self, log.format(str(r.status_code)))
|
288
|
297
|
# TODO - G.M - Verify exception correctly works
|
289
|
298
|
except requests.exceptions.Timeout:
|
|
@@ -295,6 +304,4 @@ class MailFetcher(object):
|
295
|
304
|
log = 'Fail to transmit fetched mail to tracim : {}'
|
296
|
305
|
logger.error(self, log.format(str(e)))
|
297
|
306
|
break
|
298
|
|
- # FIXME - G.M - 2017-11-17 Avoid too short-timed infinite retry ?
|
299
|
|
- # retry later to send those mail
|
300
|
|
- self._mails = unsended_mail
|
|
307
|
+ return unsended_mails
|