10 def __init__(self, fragment):
11 self._next_fragment = 0
12 self._raw_content = ""
14 parts = fragment.split()
17 raise Exception("Invalid fragment!")
19 if parts[0] == "PLAIN":
20 self._msg_fmt = Message.Format.PLAIN
21 elif parts[0] == "HTML":
22 self._msg_fmt = Message.Format.HTML
24 raise Exception("Unknown message type %s!" % msg_fmt)
27 self._msg_len = int(parts[1])
29 raise Exception("Invalid message length %s!" % parts[1])
31 if self._msg_len < 1 or self._msg_len > 1000:
32 raise Exception("Invalid message length %d!" % self._msg_len)
34 def feed_fragment(self, fragment):
35 parts = fragment.split()
38 raise Exception("Invalid fragment!")
41 fragment_number = int(parts[0])
43 raise Exception("Invalid fragment number %s!" % parts[0])
45 if fragment_number == self._next_fragment and fragment_number < self._msg_len:
46 self._next_fragment += 1
48 raise Exception("Unexpected fragment!")
50 self._raw_content += parts[1]
52 def is_complete(self):
53 return self._next_fragment == self._msg_len
55 def get_content(self):
57 msgB64Bytes = self._raw_content.encode("UTF-8")
58 msgBytes = base64.b64decode(msgB64Bytes)
59 msgStr = msgBytes.decode("UTF-8")
61 raise Exception("Message decoding error!")
63 if self._msg_fmt == Message.Format.PLAIN:
68 elif self._msg_fmt == Message.Format.HTML:
70 "format": "org.matrix.custom.html",
72 "body": html2text.html2text(msgStr),
73 "formatted_body": msgStr