|
@@ -2,66 +2,67 @@ from imap_tools import MailBox, AND
|
|
|
import re
|
|
|
import json
|
|
|
import os
|
|
|
+import plac
|
|
|
from datetime import datetime, date
|
|
|
|
|
|
|
|
|
-whitelist = {}
|
|
|
+class Imap:
|
|
|
+ commands = ['cleanup', 'move']
|
|
|
+ whitelist = {}
|
|
|
|
|
|
+ def __init__(self):
|
|
|
+ self.whitelist = json.load(open(os.path.dirname(__file__) + '/whitelist.json', 'r'))
|
|
|
|
|
|
-def cleanup_folder():
|
|
|
- date_now = datetime.now()
|
|
|
- date_criteria = date(date_now.year, date_now.month - 1, 1)
|
|
|
- msg_limit = 100
|
|
|
+ def cleanup(self):
|
|
|
+ date_now = datetime.now()
|
|
|
+ date_criteria = date(date_now.year, date_now.month - 1, 1)
|
|
|
+ msg_limit = 100
|
|
|
|
|
|
- with MailBox('mail.global-cube.com').login('archiv', 'Gc01am64!') as mb:
|
|
|
- folder_list = [f.name for f in mb.folder.list() if 'Archive.' in f.name]
|
|
|
+ with MailBox('mail.global-cube.com').login('archiv', 'Gc01am64!') as mb:
|
|
|
+ folder_list = [f.name for f in mb.folder.list() if 'Archive.' in f.name]
|
|
|
|
|
|
- for folder in folder_list:
|
|
|
- msg_count = mb.folder.status(folder)['MESSAGES']
|
|
|
- if msg_count < msg_limit:
|
|
|
- continue
|
|
|
- mb.folder.set(folder)
|
|
|
- messages = mb.fetch(criteria=AND(date_lt=date_criteria), mark_seen=True, headers_only=True)
|
|
|
- uids = [msg.uid for msg in messages]
|
|
|
- mb.delete(uids[0:(msg_count - msg_limit)])
|
|
|
-
|
|
|
-
|
|
|
-def move_mails():
|
|
|
- with MailBox('mail.global-cube.com').login('archiv', 'Gc01am64!') as mb:
|
|
|
- messages = mb.fetch(criteria=AND(from_="@global-cube.com"), mark_seen=True, bulk=True, limit=1000, headers_only=True)
|
|
|
+ for folder in folder_list:
|
|
|
+ msg_count = mb.folder.status(folder)['MESSAGES']
|
|
|
+ if msg_count < msg_limit:
|
|
|
+ continue
|
|
|
+ mb.folder.set(folder)
|
|
|
+ messages = mb.fetch(criteria=AND(date_lt=date_criteria), mark_seen=True, headers_only=True)
|
|
|
+ uids = [msg.uid for msg in messages]
|
|
|
+ mb.delete(uids[0:(msg_count - msg_limit)])
|
|
|
|
|
|
- for msg in messages:
|
|
|
- match = re.findall(r'\+(.*)@', msg.from_)
|
|
|
- if not match:
|
|
|
- print(msg.from_, msg.to, msg.subject)
|
|
|
- else:
|
|
|
- domain = '@' + match[0]
|
|
|
+ def move(self):
|
|
|
+ with MailBox('mail.global-cube.com').login('archiv', 'Gc01am64!') as mb:
|
|
|
+ messages = mb.fetch(criteria=AND(from_="@global-cube.com"), mark_seen=True, bulk=True, limit=1000, headers_only=True)
|
|
|
|
|
|
- subfolder = 'Archive.' + domain.replace('@', '').replace('.', '_')
|
|
|
- if not mb.folder.exists(subfolder):
|
|
|
- mb.folder.create(subfolder)
|
|
|
-
|
|
|
- if is_valid_message(msg, domain):
|
|
|
- mb.move(msg.uid, subfolder)
|
|
|
+ for msg in messages:
|
|
|
+ match = re.findall(r'\+(.*)@', msg.from_)
|
|
|
+ if not match:
|
|
|
+ print(msg.from_, msg.to, msg.subject)
|
|
|
else:
|
|
|
- print(domain, ', '.join(msg.to), msg.subject)
|
|
|
- # print(msg.text)
|
|
|
- # print([att.filename for att in msg.attachments if att.filename.endswith('.pdf')])
|
|
|
+ domain = '@' + match[0]
|
|
|
+
|
|
|
+ subfolder = 'Archive.' + domain.replace('@', '').replace('.', '_')
|
|
|
+ if not mb.folder.exists(subfolder):
|
|
|
+ mb.folder.create(subfolder)
|
|
|
|
|
|
+ if self.is_valid_message(msg, domain):
|
|
|
+ mb.move(msg.uid, subfolder)
|
|
|
+ else:
|
|
|
+ print(domain, ', '.join(msg.to), msg.subject)
|
|
|
+ # print(msg.text)
|
|
|
+ # print([att.filename for att in msg.attachments if att.filename.endswith('.pdf')])
|
|
|
|
|
|
-def is_valid_message(msg, domain):
|
|
|
- for to in msg.to:
|
|
|
- if domain in to or '@global-cube.de' in to or msg.from_ == to:
|
|
|
- continue
|
|
|
- if domain not in whitelist:
|
|
|
- return False
|
|
|
- valid_domain = [entry in to for entry in whitelist[domain]]
|
|
|
- if not any(valid_domain):
|
|
|
- return False
|
|
|
- return True
|
|
|
+ def is_valid_message(self, msg, domain):
|
|
|
+ for to in msg.to:
|
|
|
+ if domain in to or '@global-cube.de' in to or msg.from_ == to:
|
|
|
+ continue
|
|
|
+ if domain not in self.whitelist:
|
|
|
+ return False
|
|
|
+ valid_domain = [entry in to for entry in self.whitelist[domain]]
|
|
|
+ if not any(valid_domain):
|
|
|
+ return False
|
|
|
+ return True
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- whitelist = json.load(open(os.path.dirname(__file__) + '/whitelist.json', 'r'))
|
|
|
- move_mails()
|
|
|
- cleanup_folder()
|
|
|
+ plac.Interpreter.call(Imap)
|