示例#1
0
 def is_internal_message(self, request):
     from Products.listen.lib.common import header_validator
     validator = header_validator()
     return validator.validate_headers(request)
示例#2
0
    def createMailFromMessage(self, msg_string, attachments=False):
        context = self.context

        (TextBody, ContentType, HtmlBody, Attachments) = unpackMail(msg_string)
        if attachments:
            for file in Attachments:
                self.addAttachment(file['filename'],
                                   file['filebody'],
                                   file['maintype'] + '/' + file['subtype'])

        # This must be encoded text. Attempt to use specificed encoding to
        # convert to unicode, otherwise default to iso-8859-1, which is a
        # a reasonable superset of 7-bit ascii, and more common than utf-8
        # for email.  The RFCs indicate that no specified encoding means 7-bit
        # ascii, so this should be safe.
        encoding = 'iso-8859-1'
        # ContentType is only set for the TextBody
        if ContentType:
            body = TextBody
            # find charset
            encoding_match = CHARSET_REGEX.search(ContentType)
            if encoding_match:
                encoding = encoding_match.groups()[0]
        else:
            body = convertHTML2Text(HtmlBody)

        try:
            body = body.decode("utf8")
        except UnicodeDecodeError:
            try:
                body = body.decode(encoding)
            except UnicodeDecodeError:
                try:
                    body = body.decode(encoding, 'replace')
                except LookupError:
                    # The email specified an invalid encoding
                    body = body.decode('iso-8859-1', 'replace')

        msg = email.message_from_string(msg_string)
        in_reply_to = msg.get('in-reply-to', context.in_reply_to).strip()
        references = msg.get('references', '').strip()
        # split references on whitespace
        if references:
            references = REF_REGEX.split(references)
        else:
            references = ()
        message_id = msg.get('message-id', context.message_id).strip()
        if not message_id:
            # XXX: This method is acquired inappropriately from the parent
            # MailBoxer for now
            message_id = context.uniqueMessageId()
        # Use a regexp to optionally pull in other headers
        other_headers = []
        # Attempt to acquire the headers regex from our MailingList
        headers_regexp = aq_get(context, 'headers', None)
        if headers_regexp is not None:
            if headers_regexp:
                for (key, value) in msg.items():
                    if (re.match(headers_regexp, key, re.IGNORECASE) and
                        key not in ['subject', 'date', 'from', 'in_reply_to',
                                    'references', 'message_id']):
                        other_headers.append((key, value.strip()))

        context.body = body
        context.in_reply_to = in_reply_to
        context.references = tuple(references)
        context.message_id = message_id

        opencore_headers = [(header[0].lower(), header[1]) for header in other_headers if header[0].lower().startswith("x-opencore")]
        other_headers = [header for header in other_headers if header not in opencore_headers]

        opencore_headers = dict(opencore_headers)
        from Products.listen.lib.common import header_validator
        validator = header_validator()
        if not validator.validate_headers(opencore_headers):
            opencore_headers = []
        else:
            del opencore_headers["x-opencore-validation-key"]
            if opencore_headers.get("x-opencore-send-from", None) is not None:
                context.from_addr = opencore_headers['x-opencore-send-from']
                    
            opencore_headers = opencore_headers.items()
        other_headers.extend(opencore_headers)

        context.other_headers = tuple(other_headers or [])
    def createMailFromMessage(self, msg_string, attachments=False):
        context = self.context

        (TextBody, ContentType, HtmlBody, Attachments) = unpackMail(msg_string)
        if attachments:
            for file in Attachments:
                self.addAttachment(file["filename"], file["filebody"], file["maintype"] + "/" + file["subtype"])

        # This must be encoded text. Attempt to use specificed encoding to
        # convert to unicode, otherwise default to iso-8859-1, which is a
        # a reasonable superset of 7-bit ascii, and more common than utf-8
        # for email.  The RFCs indicate that no specified encoding means 7-bit
        # ascii, so this should be safe.
        encoding = "iso-8859-1"
        # ContentType is only set for the TextBody
        if ContentType:
            body = TextBody
            # find charset
            encoding_match = CHARSET_REGEX.search(ContentType)
            if encoding_match:
                encoding = encoding_match.groups()[0]
        else:
            body = convertHTML2Text(HtmlBody)

        try:
            body = body.decode("utf8")
        except UnicodeDecodeError:
            try:
                body = body.decode(encoding)
            except UnicodeDecodeError:
                try:
                    body = body.decode(encoding, "replace")
                except LookupError:
                    # The email specified an invalid encoding
                    body = body.decode("iso-8859-1", "replace")

        msg = email.message_from_string(msg_string)
        in_reply_to = msg.get("in-reply-to", context.in_reply_to).strip()
        references = msg.get("references", "").strip()
        # split references on whitespace
        if references:
            references = REF_REGEX.split(references)
        else:
            references = ()
        message_id = msg.get("message-id", context.message_id).strip()
        if not message_id:
            # XXX: This method is acquired inappropriately from the parent
            # MailBoxer for now
            message_id = context.uniqueMessageId()
        # Use a regexp to optionally pull in other headers
        other_headers = []
        # Attempt to acquire the headers regex from our MailingList
        headers_regexp = aq_get(context, "headers", None)
        if headers_regexp is not None:
            if headers_regexp:
                for (key, value) in msg.items():
                    if re.match(headers_regexp, key, re.IGNORECASE) and key not in [
                        "subject",
                        "date",
                        "from",
                        "in_reply_to",
                        "references",
                        "message_id",
                    ]:
                        other_headers.append((key, value.strip()))

        context.body = body
        context.in_reply_to = in_reply_to
        context.references = tuple(references)
        context.message_id = message_id

        opencore_headers = [
            (header[0].lower(), header[1]) for header in other_headers if header[0].lower().startswith("x-opencore")
        ]
        other_headers = [header for header in other_headers if header not in opencore_headers]

        opencore_headers = dict(opencore_headers)
        from Products.listen.lib.common import header_validator

        validator = header_validator()
        if not validator.validate_headers(opencore_headers):
            opencore_headers = []
        else:
            del opencore_headers["x-opencore-validation-key"]
            if opencore_headers.get("x-opencore-send-from", None) is not None:
                context.from_addr = opencore_headers["x-opencore-send-from"]

            opencore_headers = opencore_headers.items()
        other_headers.extend(opencore_headers)

        context.other_headers = tuple(other_headers or [])