imap.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from imap_tools import MailBox, AND
  2. import re
  3. import json
  4. import os
  5. import plac
  6. from datetime import datetime, date
  7. class Imap:
  8. commands = ['cleanup', 'move', 'remove', 'add_to_whitelist']
  9. whitelist = {}
  10. blacklist = []
  11. def __init__(self):
  12. with open(os.path.dirname(__file__) + '/whitelist.json', 'r') as frh:
  13. self.whitelist = json.load(frh)
  14. with open(os.path.dirname(__file__) + '/blacklist.json', 'r') as frh:
  15. self.blacklist = json.load(frh)
  16. def cleanup(self):
  17. date_now = datetime.now()
  18. date_criteria = date(date_now.year, date_now.month - 1, 1)
  19. msg_limit = 100
  20. with MailBox('mail.global-cube.com').login('archiv', 'Gc01am64!') as mb:
  21. folder_list = [f.name for f in mb.folder.list() if 'Archive.' in f.name]
  22. for folder in folder_list:
  23. msg_count = mb.folder.status(folder)['MESSAGES']
  24. if msg_count < msg_limit:
  25. continue
  26. mb.folder.set(folder)
  27. messages = mb.fetch(criteria=AND(date_lt=date_criteria), mark_seen=True, headers_only=True)
  28. uids = [msg.uid for msg in messages]
  29. mb.delete(uids[0:(msg_count - msg_limit)])
  30. def move(self):
  31. with MailBox('mail.global-cube.com').login('archiv', 'Gc01am64!') as mb:
  32. messages = mb.fetch(criteria=AND(from_="@global-cube.com"), mark_seen=True, bulk=True, limit=1000, headers_only=True)
  33. for msg in messages:
  34. match = re.findall(r'\+(.*)@', msg.from_)
  35. if not match:
  36. # print(msg.from_, msg.to, msg.subject)
  37. continue
  38. domain = '@' + match[0]
  39. subfolder = 'Archive.' + domain.replace('@', '').replace('.', '_')
  40. if not mb.folder.exists(subfolder):
  41. mb.folder.create(subfolder)
  42. if self.is_valid_message(msg, domain):
  43. mb.move(msg.uid, subfolder)
  44. # else:
  45. # print(domain, ', '.join(msg.to), msg.subject)
  46. # print(msg.text)
  47. # print([att.filename for att in msg.attachments if att.filename.endswith('.pdf')])
  48. def is_valid_message(self, msg, domain):
  49. for to in msg.to:
  50. if domain in to or '@global-cube.de' in to or msg.from_ == to:
  51. continue
  52. if domain not in self.whitelist:
  53. return False
  54. valid_domain = [entry in to for entry in self.whitelist[domain]]
  55. if not any(valid_domain):
  56. return False
  57. return True
  58. def add_to_whitelist(self):
  59. with MailBox('mail.global-cube.com').login('archiv', 'Gc01am64!') as mb:
  60. mb.folder.set('whitelist')
  61. messages = mb.fetch(criteria=AND(from_="@global-cube.com"), mark_seen=True, bulk=True, limit=1000, headers_only=True)
  62. for msg in messages:
  63. match = re.findall(r'\+(.*)@', msg.from_)
  64. if not match:
  65. # print(msg.from_, msg.to, msg.subject)
  66. continue
  67. domain = '@' + match[0]
  68. if not self.is_valid_message(msg, domain):
  69. if domain not in self.whitelist:
  70. self.whitelist[domain] = []
  71. for to in msg.to:
  72. if domain not in to and to not in self.whitelist[domain]:
  73. self.whitelist[domain].append(to)
  74. with open(os.path.dirname(__file__) + '/whitelist.json', 'w') as fwh:
  75. json.dump(self.whitelist, fwh, indent=2)
  76. uids = [msg.uid for msg in messages]
  77. mb.delete(uids)
  78. def remove_absence_messages(self):
  79. with MailBox('mail.global-cube.com').login('versand', 'y6!avXX3tQvr') as mb:
  80. messages = mb.fetch(criteria=AND(to="@global-cube.com"), mark_seen=False, bulk=True, limit=1000, headers_only=True)
  81. selected = []
  82. for msg in messages:
  83. for b in self.blacklist:
  84. if b in msg.subject or b in msg.text:
  85. selected.append(msg.uid)
  86. mb.delete(selected)
  87. def remove(self):
  88. self.remove_absence_messages()
  89. if __name__ == '__main__':
  90. plac.Interpreter.call(Imap)
  91. # Imap().remove_absence_messages()