Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import imaplib
  3. import re
  4. import email
  5.  
  6. ################################################################################
  7. # RULE DEFINITIONS
  8. ################################################################################
  9.  
  10. class ListHeaderRule:
  11. def __init__(self):
  12. self.name = 'List-Header check'
  13.  
  14. def hit_msg(self, msg):
  15. sender = msg['From']
  16. print 'Found unsub header in msg [ {0} ] from: [ {1} ]'.format(msgid, sender)
  17. print 'List-Unsubscribe: {0}'.format(msg['List-Unsubscribe'])
  18.  
  19. def is_hit(self, msg):
  20. return 'List-Unsubscribe' in msg
  21.  
  22. class RegexMatchRule:
  23. def __init__(self, regex):
  24. self.name = 'Regex Match: {0}'.format(regex)
  25. self.regex = regex
  26.  
  27. # Make sure we're not looking at things like attachments or images
  28. self.valid_payload_types= {
  29. 'text/plain': True,
  30. 'text/html': True,
  31. }
  32.  
  33. def hit_msg(self, msg):
  34. print '{0} got a hit in message with subject [ {1} ] from: [{2}]'.format(
  35. self.name, msg['Subject'], msg['From']
  36.  
  37. def is_hit(self, msg):
  38. for part in msg.walk():
  39. if part.get_content_type() in self.valid_payload_types:
  40. re.part.get
  41. payload = part.get_payload()
  42. match = re.match(self.regex, payload, re.I) # Case insensitive match
  43. return True if match else False
  44.  
  45. ################################################################################
  46. # HELPERS
  47. ################################################################################
  48.  
  49. def run_rules(rules, msg):
  50. is_hit = False
  51.  
  52. for rule in rules:
  53. print "============================================================"
  54. print "Executing rule: " + rule.name
  55. print "============================================================"
  56.  
  57. if rule.is_hit(msg):
  58. rule.hit_msg(msg)
  59. is_hit = True
  60. break
  61.  
  62. return is_hit
  63.  
  64. ################################################################################
  65. # CONFIG
  66. ################################################################################
  67.  
  68. EMAIL_USER = 'xxx'
  69. EMAIL_PASS = 'xxx'
  70.  
  71. DUMP_MESSAGES = True
  72. DUMP_LOCATION = '/tmp/unsub_raw_mail'
  73.  
  74. ################################################################################
  75. # MAIN
  76. ################################################################################
  77.  
  78. # Build rules
  79. rules = [
  80. ListHeaderRule(),
  81. RegexMatchRule(r'unsubscribe'),
  82. # RegexMatchRule(r'some other string you want to hit'),
  83. ]
  84.  
  85. mail = imaplib.IMAP4_SSL('imap.gmail.com')
  86. mail.login(EMAIL_USER, EMAIL_PASS)
  87. mail.select('[Gmail]/All Mail')
  88. _, data = mail.search(None, 'All')
  89. ids = data[0]
  90. id_list = ids.split()
  91.  
  92. for msgid in id_list:
  93. _, data = mail.fetch(msgid, '(RFC822)')
  94. raw_msg = data[0][1]
  95. msg = email.message_from_string(raw_msg)
  96.  
  97. # dump all the messages to /tmp
  98. if DUMP_MESSAGES:
  99. if not os.path.exists(DUMP_LOCATION):
  100. os.makedirs(DUMP_DUMP_LOCATION)
  101.  
  102. subject = msg['Subject'].replace(' ', '_')
  103. filename = DUMP_LOCATION + '/' + subject + '.txt' with open(filename, 'w') as f:
  104. f.write(raw_msg)
  105.  
  106. is_hit = run_rules(rules, msg)
  107.  
  108. if is_hit:
  109. hitcount += 1
  110.  
  111. print "============================================================"
  112. print "Status:"
  113. print "============================================================"
  114. print 'Found {0} of {1} messages with unsubscribe headers...'.format(hitcount, len(id_list))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement