gettor_requests.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/python2.5
  2. # -*- coding: utf-8 -*-
  3. """
  4. This library implements all of the email parsing features needed for gettor.
  5. """
  6. import sys
  7. import email
  8. import dkim
  9. import re
  10. def getMessage():
  11. """ Read the message into a buffer and return it """
  12. rawMessage = sys.stdin.read()
  13. return rawMessage
  14. def verifySignature(rawMessage):
  15. """ Attempt to verify the DKIM signature of a message and return a positive or negative status """
  16. signature = False
  17. # TODO XXX:
  18. # This should catch DNS exceptions and fail to verify if we have a
  19. # dns timeout
  20. if dkim.verify(rawMessage):
  21. signature = True
  22. return signature
  23. else:
  24. return signature
  25. def parseMessage(message):
  26. """ parse an email message and return a parsed email object """
  27. return email.message_from_string(message)
  28. def parseReply(parsedMessage):
  29. """ Return an email address that we want to email """
  30. # TODO XXX:
  31. # Scrub this data
  32. address = parsedMessage["from"]
  33. return address
  34. def parseRequest(parsedMessage, packages):
  35. """ This parses the request and returns the first specific package name for
  36. sending. If we do not understand the request, we return None as the package
  37. name."""
  38. # XXX TODO:
  39. # Should we pick only the first line of the email body. Drop the rest?
  40. # It may be too unfriendly to our users
  41. for line in email.Iterators.body_line_iterator(parsedMessage):
  42. for package in packages.keys():
  43. print "Line is: " + line
  44. print "Package is " + package
  45. match = re.match(package, line)
  46. if match:
  47. return package
  48. # If we get here, we didn't find a package we're currently serving
  49. return None
  50. if __name__ == "__main__" :
  51. """ Give us an email to understand what we think of it. """
  52. packageList = {
  53. "windows-bundle": "/tmp/windows-bundle.z",
  54. "macosx-bundle": "/tmp/macosx-bundle.z",
  55. "linux-bundle": "/tmp/linux-bundle.z",
  56. "source-bundle": "/tmp/source-bundle.z"
  57. }
  58. print "Fetching raw message."
  59. rawMessage = getMessage()
  60. # This doesn't work without DNS ( no wifi on board current airplane )
  61. print "Verifying signature of message."
  62. signature = verifySignature(rawMessage)
  63. print "Parsing Message."
  64. parsedMessage = parseMessage(rawMessage)
  65. print "Parsing reply."
  66. parsedReply = parseReply(parsedMessage)
  67. print "Parsing package request."
  68. package = parseRequest(parsedMessage, packageList)
  69. if package == None:
  70. package = "help"
  71. else:
  72. package = packageList[package]
  73. print "The signature status of the email is: " + str(signature)
  74. print "The email requested the following reply address: " + parsedReply
  75. print "It looks like the email requested the following package: " + package
  76. print "We would select the following package file: " + package