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.

74 lines
2.1 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 _process_message(remote_addr, mailfrom, mailto, data):
  12. to_parts = mailto.split("@")
  13. code = to_parts[0]
  14. try:
  15. data = data.decode()
  16. except UnicodeError:
  17. data = "[binary data]"
  18. if not RE_UUID.match(code):
  19. return f"Not an UUID: {code}"
  20. try:
  21. check = Check.objects.get(code=code)
  22. except Check.DoesNotExist:
  23. return f"Check not found: {code}"
  24. action = "success"
  25. if check.subject or check.subject_fail:
  26. action = "ign"
  27. subject = email.message_from_string(data).get("subject", "")
  28. if check.subject and check.subject in subject:
  29. action = "success"
  30. elif check.subject_fail and check.subject_fail in subject:
  31. action = "fail"
  32. ua = "Email from %s" % mailfrom
  33. check.ping(remote_addr, "email", "", ua, data, action)
  34. return f"Processed ping for {code}"
  35. class Listener(SMTPServer):
  36. def __init__(self, localaddr, stdout):
  37. self.stdout = stdout
  38. super(Listener, self).__init__(localaddr, None, decode_data=False)
  39. def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
  40. # get a new db connection in case the old one has timed out:
  41. connections.close_all()
  42. result = _process_message(peer[0], mailfrom, rcpttos[0], data)
  43. self.stdout.write(result)
  44. class Command(BaseCommand):
  45. help = "Listen for ping emails"
  46. def add_arguments(self, parser):
  47. parser.add_argument(
  48. "--host", help="ip address to listen on, default 0.0.0.0", default="0.0.0.0"
  49. )
  50. parser.add_argument(
  51. "--port", help="port to listen on, default 25", type=int, default=25
  52. )
  53. def handle(self, host, port, *args, **options):
  54. _ = Listener((host, port), self.stdout)
  55. print("Starting SMTP listener on %s:%d ..." % (host, port))
  56. asyncore.loop()