mod.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import re
  2. from .mail import getEmailsFromUser, getModerationData, sendEmail
  3. from .utils import getFileLines
  4. EMAIL_SIMPLE_REGEX = '[\w\.-]+@[\w\.-]+'
  5. class HackyMod:
  6. def __init__(self,
  7. listName='', users=[], sympaCommandEmail='',
  8. blacklistFile=[], listContactEmail=None,
  9. moderatorEmail=None, moderatorPassword=None, imapSSLServer=None,
  10. imapSSLPort=993, smtpServer=None, smtpPort=0):
  11. self.listName = listName
  12. self.users = users
  13. self.sympaCommandEmail = sympaCommandEmail
  14. self.blacklistFile = blacklistFile
  15. self.listContactEmail = listContactEmail
  16. self.moderatorEmail = moderatorEmail
  17. self.moderatorPassword = moderatorPassword
  18. self.imapSSLServer = imapSSLServer
  19. self.imapSSLPort = imapSSLPort
  20. self.smtpServer = smtpServer
  21. self.smtpPort = smtpPort
  22. def __parseEmailFromSubject(self, subject):
  23. results = re.findall(EMAIL_SIMPLE_REGEX, subject)
  24. if len(results) > 0:
  25. return results[0]
  26. return None
  27. def __isUserSubscribed(self, email):
  28. users = self.users
  29. # print('Compring %s with subscribed users' % email)
  30. for usr in users:
  31. if usr == email:
  32. return True
  33. return False
  34. def __isUserInBlackList(self, email):
  35. # Added a blacklist file (testing)
  36. blacklist = getFileLines(self.blacklistFile, removeEOL=True)
  37. for usr in blacklist:
  38. if usr == email:
  39. return True
  40. return False
  41. def __isGoodUser(self, email):
  42. isSubscribed = self.__isUserSubscribed(email)
  43. isInBlackList = self.__isUserInBlackList(email)
  44. print(' [+] %s is subscribed: %s' % (email, isSubscribed))
  45. print(' [+] %s is in black list: %s' % (email, isInBlackList))
  46. return isSubscribed and not isInBlackList
  47. def moderate(self):
  48. emails = getEmailsFromUser(self.listContactEmail, self.moderatorEmail,
  49. self.moderatorPassword, self.imapSSLServer, self.imapSSLPort, self.listName)
  50. for email in emails:
  51. senderEmail = self.__parseEmailFromSubject(email['subject'])
  52. if not senderEmail:
  53. print('[MOD] - Invalid email subject')
  54. continue
  55. print('[MOD] - Moderating message from %s' % senderEmail)
  56. moderationCode = getModerationData(email['content'], self.listName, senderEmail)
  57. if not moderationCode:
  58. print(' [+] Email with invalid format')
  59. continue
  60. print(' [+] Email has moderation code %s' % moderationCode)
  61. if self.__isGoodUser(senderEmail):
  62. print(' [+] %s is a good user :D, distributing.' % senderEmail)
  63. subject = 'DISTRIBUTE % s %s' % (self.listName, moderationCode)
  64. # sendEmail(self.sympaDistributeEmail, subject)
  65. attempts = 1
  66. while True:
  67. sent = sendEmail(self.sympaCommandEmail, subject,
  68. self.moderatorEmail, self.moderatorPassword,
  69. self.smtpServer, self.smtpPort)
  70. if sent:
  71. print('[MOD] - Email distributed successfully')
  72. break
  73. attempts += 1
  74. if attempts == 10:
  75. break
  76. else:
  77. print(' [-] %s is not a good user the message has to be moderated manually' % senderEmail)