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.
 
 
 

110 lines
3.3 KiB

  1. #TODO add the license
  2. #I had to rewrite this class because the python MIME email.mime (version 2.5)
  3. #are buggy, they use \n instead \r\n for new line which is not compliant
  4. #to standard!
  5. # http://bugs.python.org/issue5525
  6. #TODO do not load all the message in memory stream it from the disk
  7. import re
  8. import random
  9. import sys
  10. #new line
  11. NL = '\r\n'
  12. _width = len(repr(sys.maxint - 1))
  13. _fmt = '%%0%dd' % _width
  14. class MIMEMessage:
  15. def __init__(self):
  16. self._files = []
  17. self._xmlMessage = ""
  18. self._startCID = ""
  19. self._boundary = ""
  20. def makeBoundary(self):
  21. #create the boundary
  22. msgparts = []
  23. msgparts.append(self._xmlMessage)
  24. for i in self._files:
  25. msgparts.append(i.read())
  26. #this sucks, all in memory
  27. alltext = NL.join(msgparts)
  28. self._boundary = _make_boundary(alltext)
  29. #maybe I can save some memory
  30. del alltext
  31. del msgparts
  32. self._startCID = "<" + (_fmt % random.randrange(sys.maxint)) + (_fmt % random.randrange(sys.maxint)) + ">"
  33. def toString(self):
  34. '''it return a string with the MIME message'''
  35. if len(self._boundary) == 0:
  36. #the makeBoundary hasn't been called yet
  37. self.makeBoundary()
  38. #ok we have everything let's start to spit the message out
  39. #first the XML
  40. returnstr = NL + "--" + self._boundary + NL
  41. returnstr += "Content-Type: text/xml; charset=\"us-ascii\"" + NL
  42. returnstr += "Content-Transfer-Encoding: 7bit" + NL
  43. returnstr += "Content-Id: " + self._startCID + NL + NL
  44. returnstr += self._xmlMessage + NL
  45. #then the files
  46. for file in self._files:
  47. returnstr += "--" + self._boundary + NL
  48. returnstr += "Content-Type: application/octet-stream" + NL
  49. returnstr += "Content-Transfer-Encoding: binary" + NL
  50. returnstr += "Content-Id: <" + str(id(file)) + ">" + NL + NL
  51. file.seek(0)
  52. returnstr += file.read() + NL
  53. #closing boundary
  54. returnstr += "--" + self._boundary + "--" + NL
  55. return returnstr
  56. def attachFile(self, file):
  57. '''
  58. it adds a file to this attachment
  59. '''
  60. self._files.append(file)
  61. def addXMLMessage(self, xmlMessage):
  62. '''
  63. it adds the XML message. we can have only one XML SOAP message
  64. '''
  65. self._xmlMessage = xmlMessage
  66. def getBoundary(self):
  67. '''
  68. this function returns the string used in the mime message as a
  69. boundary. First the write method as to be called
  70. '''
  71. return self._boundary
  72. def getStartCID(self):
  73. '''
  74. This function returns the CID of the XML message
  75. '''
  76. return self._startCID
  77. def _make_boundary(text=None):
  78. #some code taken from python stdlib
  79. # Craft a random boundary. If text is given, ensure that the chosen
  80. # boundary doesn't appear in the text.
  81. token = random.randrange(sys.maxint)
  82. boundary = ('=' * 10) + (_fmt % token) + '=='
  83. if text is None:
  84. return boundary
  85. b = boundary
  86. counter = 0
  87. while True:
  88. cre = re.compile('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
  89. if not cre.search(text):
  90. break
  91. b = boundary + '.' + str(counter)
  92. counter += 1
  93. return b