You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3 KiB

  1. import asyncore
  2. import email
  3. import re
  4. from smtpd import SMTPServer
  5. from django.core.management.base import BaseCommand
  6. from django.db import connections
  7. from hc.api.models import Check
  8. RE_UUID = re.compile(
  9. "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$"
  10. )
  11. def _match(subject, keywords):
  12. for s in keywords.split(","):
  13. s = s.strip()
  14. if s and s in subject:
  15. return True
  16. return False
  17. def _process_message(remote_addr, mailfrom, mailto, data):
  18. to_parts = mailto.split("@")
  19. code = to_parts[0]
  20. try:
  21. data = data.decode()
  22. except UnicodeError:
  23. data = "[binary data]"
  24. if not RE_UUID.match(code):
  25. return f"Not an UUID: {code}"
  26. try:
  27. check = Check.objects.get(code=code)
  28. except Check.DoesNotExist:
  29. return f"Check not found: {code}"
  30. action = "success"
  31. if check.subject or check.subject_fail:
  32. action = "ign"
  33. subject = email.message_from_string(data).get("subject", "")
  34. if check.subject and _match(subject, check.subject):
  35. action = "success"
  36. elif check.subject_fail and _match(subject, check.subject_fail):
  37. action = "fail"
  38. ua = "Email from %s" % mailfrom
  39. check.ping(remote_addr, "email", "", ua, data, action)
  40. return f"Processed ping for {code}"
  41. class Listener(SMTPServer):
  42. def __init__(self, localaddr, stdout):
  43. self.stdout = stdout
  44. super(Listener, self).__init__(localaddr, None, decode_data=False)
  45. def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
  46. # get a new db connection in case the old one has timed out:
  47. connections.close_all()
  48. result = _process_message(peer[0], mailfrom, rcpttos[0], data)
  49. self.stdout.write(result)
  50. class Command(BaseCommand):
  51. help = "Listen for ping emails"
  52. def add_arguments(self, parser):
  53. parser.add_argument(
  54. "--host", help="ip address to listen on, default 0.0.0.0", default="0.0.0.0"
  55. )
  56. parser.add_argument(
  57. "--port", help="port to listen on, default 25", type=int, default=25
  58. )
  59. def handle(self, host, port, *args, **options):
  60. _ = Listener((host, port), self.stdout)
  61. print("Starting SMTP listener on %s:%d ..." % (host, port))
  62. asyncore.loop()