def render(self, parent): """ :type parent: xml.etree.ElementTree.Element :param parent: :return: """ child = etree.SubElement(parent, "Header") child1 = etree.SubElement(child, "Name") child1.text = self.name child2 = etree.SubElement(child, "Value") try: child2.text = CommonMethods.cdata_wrap(self.value) except ValueError as ve: pass except TypeError as te: pass
def process_headers(self): if isinstance(self.payload, str): self.body_content = self.payload self.body_only = True return for header, value in self.payload.items(): if header == "Content-Type": expression = CommonMethods.get_content_type(value) if len(expression) > 1: self.content_type = expression[0] # Is this a charset identification if expression[1] == 'charset': self.charset = expression[2] else: self.content_type_param.append(Parameter(expression[1], expression[2])) continue else: self.content_type = expression[0] continue if header == "Content-Transfer-Encoding": self.transfer_encoding = value continue if header == "Content-Disposition": try: self.disposition = value.split(";")[0] fn = value.split(";")[1].split("=")[1] if len(fn.split("''")) > 1: self.disposition_file_name = unquote(fn.split("''")[1]) else: self.disposition_file_name = unquote(fn) continue except IndexError as e: self.other_mime_header.append(Header(header, value)) if header == "Content-ID": self.content_id = CommonMethods.cdata_wrap(value) continue if header == "Content-Description": self.content_name = value continue self.other_mime_header.append(Header(header, value))
def _process_plaintext_body(self): t = "" if isinstance(self.payload, Message): t = re.sub("\[\[", "\\[\\[", self.payload.get_payload()) t = re.sub("]]", "\]\]", t) elif isinstance(self.payload, str): t = re.sub("\[\[", "\\[\\[", self.payload) t = re.sub("]]", "\]\]", t) s = sys.getsizeof(t) if s > (1024 ** 2): # This is probably not a plaintext payload. Punt to external body. self._full_ext_body() return try: sbint = IntBodyContent(CommonMethods.cdata_wrap(t), self.transfer_encoding, self.charset) if sbint.content == '' or sbint is None: self.payload = None return self.body_content.append(sbint) except ValueError as ve: self.logger.error("{}".format(ve)) self.payload = None
def __init__(self, rel_path, local_id, message, fn=None): """Constructor for Message""" self.logger = logging.getLogger("MessageType") self.message = message # type: Message self.fn = fn # First parts of the schema message-type self.relative_path = rel_path # type: str self.local_id = local_id self.message_id = CommonMethods.cdata_wrap( self.message.get("Message-ID")) # type: str if self.message_id == '' or self.message_id is None: self.message_id = 'No Message-ID supplied' self.mime_version = CommonMethods.cdata_wrap( self.message.get("MIME-Version")) # type: str self.incomplete = [] # type: list[IncompleteParse] # xm:message-headers xml_d = CommonMethods.tup_to_xml_date( CommonMethods.parsedate_tz(self.message.get("Date"))) self.orig_date = xml_d # type: str self.m_from = CommonMethods.cdata_wrap( self.message.get("From")) # type: str self.sender = CommonMethods.cdata_wrap( self.message.get("Sender")) # type: str try: self.m_to = CommonMethods.cdata_wrap( self.message.get("To")) # type: str except TypeError as te: self.logger.error("{}".format(te)) self.incomplete.append( IncompleteParse('TypeError parsing To Header', te)) self.cc = CommonMethods.cdata_wrap(self.message.get("Cc")) # type: str self.bcc = CommonMethods.cdata_wrap( self.message.get("Bcc")) # type: str self.in_reply_to = CommonMethods.cdata_wrap( self.message.get("In-Reply-To")) self.references = CommonMethods.cdata_wrap( self.message.get("References")) # type: str self.comments = CommonMethods.cdata_wrap( self.message.get("Comments")) # type: str self.keywords = CommonMethods.cdata_wrap( self.message.get("Keywords")) # type: str try: self.subject = CommonMethods.cdata_wrap( self.message.get("Subject")) # type: str except TypeError as te: self.logger.error("{}".format(te)) self.incomplete.append( IncompleteParse('TypeError parsing Subject line', te)) try: self.status_flag = status.get( self.message.get("Status")) # type: str except Exception as e: self.incomplete.append( IncompleteParse('TypeError parsing Status', e)) self.headers = [] # type: list[Header] self.single_body = [] # type: list[SingleBody] self.multiple_body = [] # type: list[MultiBody] try: self.eol = CommonMethods.get_eol( self.message.as_string(policy=self.message.policy.clone( utf8=True))) # type: str except KeyError as e: self.logger.error("Inspect Message: KeyError {}".format( self.message.get("Message-ID"))) self.incomplete.append(IncompleteParse('KeyError parsing EOL', e)) except UnicodeEncodeError as ue: print(sys.gettrace()) self.logger.error("Inspect Message: UnicodeEncodeError {}".format( self.message.get("Message-ID"))) self.incomplete.append( IncompleteParse('UnicodeEncodeError parsing EOL', ue)) except LookupError as le: self.logger.error("Inspect Message: LookupError {}".format( self.message.get("Message-ID"))) self.incomplete.append( IncompleteParse('LookupError parsing EOL', le)) except Exception as er: self.incomplete.append( IncompleteParse('LookupError parsing EOL', er)) finally: self.eol = 'LF' self.hash = CommonMethods.get_hash( self.message.as_bytes()) # type: Hash self._process_headers() self._process_payload()