main.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/python
  2. import time
  3. from threading import Thread
  4. from logs import initLogs
  5. from configuration import loadConfiguration, settings
  6. from sympa.subscribers import getSubscribersFromFile, getSubscribersFromEmail
  7. from sympa.subscribers import writeSubscribers
  8. from sympa.mod import HackyMod
  9. subscribedUsers = []
  10. def getSubscribersAsync(usersReadyCallback):
  11. global subscribedUsers
  12. first = True
  13. backupSubscribedUsers = getSubscribersFromFile(settings['subscribersFile'])
  14. while True:
  15. print('[ASYNC] - Getting subscribed users...')
  16. subscribedUsers = getSubscribersFromEmail(
  17. listName=settings['listName'],
  18. sympaCommandEmail=settings['sympaCommandEmail'],
  19. listContactEmail=settings['listContactEmail'],
  20. moderatorEmail=settings['moderatorEmail'],
  21. moderatorPassword=settings['moderatorPassword'],
  22. imapServer=settings['imapSSLServer'], imapPort=settings['imapSSLPort'],
  23. smtpServer=settings['smtpServer'], smtpPort=settings['smtpPort'])
  24. print('[ASYNC] - Subscribed users fetched\n\n')
  25. if len(subscribedUsers) == 0:
  26. print(' [-] No subscribers fetched from email, using subscribers file')
  27. subscribedUsers = backupSubscribedUsers
  28. else:
  29. print(' [+] Writing new subscribers to file')
  30. writeSubscribers(settings['subscribersFile'], subscribedUsers)
  31. if first:
  32. first = False
  33. t = Thread(target=usersReadyCallback)
  34. t.start()
  35. print('[ASYNC] - Wating for next subscribed-users retrieval...')
  36. time.sleep(60*60*24)
  37. def startModeration():
  38. global subscribedUsers
  39. mod = HackyMod(
  40. users=subscribedUsers,
  41. blacklistFile=settings['blacklistFile'],
  42. sympaCommandEmail=settings['sympaCommandEmail'],
  43. listName=settings['listName'],
  44. listContactEmail=settings['listContactEmail'],
  45. moderatorEmail=settings['moderatorEmail'],
  46. moderatorPassword=settings['moderatorPassword'],
  47. imapSSLServer=settings['imapSSLServer'],
  48. imapSSLPort=settings['imapSSLPort'],
  49. smtpServer=settings['smtpServer'],
  50. smtpPort=settings['smtpPort'])
  51. while True:
  52. print('\n')
  53. print('[MOD] - Starting moderation')
  54. mod.moderate()
  55. # get emails, parse and moderate them every X minutes
  56. time.sleep(60*10) # cada 2 minutos
  57. def main():
  58. loadConfiguration('./settings.json')
  59. initLogs(settings['logFile'], debug=settings['debug'])
  60. t = Thread(target=getSubscribersAsync, args=(startModeration,))
  61. t.start()
  62. if __name__ == '__main__':
  63. main()