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.

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