jpayne@69: # Copyright (C) 2004-2006 Python Software Foundation jpayne@69: # Authors: Baxter, Wouters and Warsaw jpayne@69: # Contact: email-sig@python.org jpayne@69: jpayne@69: """FeedParser - An email feed parser. jpayne@69: jpayne@69: The feed parser implements an interface for incrementally parsing an email jpayne@69: message, line by line. This has advantages for certain applications, such as jpayne@69: those reading email messages off a socket. jpayne@69: jpayne@69: FeedParser.feed() is the primary interface for pushing new data into the jpayne@69: parser. It returns when there's nothing more it can do with the available jpayne@69: data. When you have no more data to push into the parser, call .close(). jpayne@69: This completes the parsing and returns the root message object. jpayne@69: jpayne@69: The other advantage of this parser is that it will never raise a parsing jpayne@69: exception. Instead, when it finds something unexpected, it adds a 'defect' to jpayne@69: the current message. Defects are just instances that live on the message jpayne@69: object's .defects attribute. jpayne@69: """ jpayne@69: jpayne@69: __all__ = ['FeedParser', 'BytesFeedParser'] jpayne@69: jpayne@69: import re jpayne@69: jpayne@69: from email import errors jpayne@69: from email._policybase import compat32 jpayne@69: from collections import deque jpayne@69: from io import StringIO jpayne@69: jpayne@69: NLCRE = re.compile(r'\r\n|\r|\n') jpayne@69: NLCRE_bol = re.compile(r'(\r\n|\r|\n)') jpayne@69: NLCRE_eol = re.compile(r'(\r\n|\r|\n)\Z') jpayne@69: NLCRE_crack = re.compile(r'(\r\n|\r|\n)') jpayne@69: # RFC 2822 $3.6.8 Optional fields. ftext is %d33-57 / %d59-126, Any character jpayne@69: # except controls, SP, and ":". jpayne@69: headerRE = re.compile(r'^(From |[\041-\071\073-\176]*:|[\t ])') jpayne@69: EMPTYSTRING = '' jpayne@69: NL = '\n' jpayne@69: jpayne@69: NeedMoreData = object() jpayne@69: jpayne@69: jpayne@69: jpayne@69: class BufferedSubFile(object): jpayne@69: """A file-ish object that can have new data loaded into it. jpayne@69: jpayne@69: You can also push and pop line-matching predicates onto a stack. When the jpayne@69: current predicate matches the current line, a false EOF response jpayne@69: (i.e. empty string) is returned instead. This lets the parser adhere to a jpayne@69: simple abstraction -- it parses until EOF closes the current message. jpayne@69: """ jpayne@69: def __init__(self): jpayne@69: # Text stream of the last partial line pushed into this object. jpayne@69: # See issue 22233 for why this is a text stream and not a list. jpayne@69: self._partial = StringIO(newline='') jpayne@69: # A deque of full, pushed lines jpayne@69: self._lines = deque() jpayne@69: # The stack of false-EOF checking predicates. jpayne@69: self._eofstack = [] jpayne@69: # A flag indicating whether the file has been closed or not. jpayne@69: self._closed = False jpayne@69: jpayne@69: def push_eof_matcher(self, pred): jpayne@69: self._eofstack.append(pred) jpayne@69: jpayne@69: def pop_eof_matcher(self): jpayne@69: return self._eofstack.pop() jpayne@69: jpayne@69: def close(self): jpayne@69: # Don't forget any trailing partial line. jpayne@69: self._partial.seek(0) jpayne@69: self.pushlines(self._partial.readlines()) jpayne@69: self._partial.seek(0) jpayne@69: self._partial.truncate() jpayne@69: self._closed = True jpayne@69: jpayne@69: def readline(self): jpayne@69: if not self._lines: jpayne@69: if self._closed: jpayne@69: return '' jpayne@69: return NeedMoreData jpayne@69: # Pop the line off the stack and see if it matches the current jpayne@69: # false-EOF predicate. jpayne@69: line = self._lines.popleft() jpayne@69: # RFC 2046, section 5.1.2 requires us to recognize outer level jpayne@69: # boundaries at any level of inner nesting. Do this, but be sure it's jpayne@69: # in the order of most to least nested. jpayne@69: for ateof in reversed(self._eofstack): jpayne@69: if ateof(line): jpayne@69: # We're at the false EOF. But push the last line back first. jpayne@69: self._lines.appendleft(line) jpayne@69: return '' jpayne@69: return line jpayne@69: jpayne@69: def unreadline(self, line): jpayne@69: # Let the consumer push a line back into the buffer. jpayne@69: assert line is not NeedMoreData jpayne@69: self._lines.appendleft(line) jpayne@69: jpayne@69: def push(self, data): jpayne@69: """Push some new data into this object.""" jpayne@69: self._partial.write(data) jpayne@69: if '\n' not in data and '\r' not in data: jpayne@69: # No new complete lines, wait for more. jpayne@69: return jpayne@69: jpayne@69: # Crack into lines, preserving the linesep characters. jpayne@69: self._partial.seek(0) jpayne@69: parts = self._partial.readlines() jpayne@69: self._partial.seek(0) jpayne@69: self._partial.truncate() jpayne@69: jpayne@69: # If the last element of the list does not end in a newline, then treat jpayne@69: # it as a partial line. We only check for '\n' here because a line jpayne@69: # ending with '\r' might be a line that was split in the middle of a jpayne@69: # '\r\n' sequence (see bugs 1555570 and 1721862). jpayne@69: if not parts[-1].endswith('\n'): jpayne@69: self._partial.write(parts.pop()) jpayne@69: self.pushlines(parts) jpayne@69: jpayne@69: def pushlines(self, lines): jpayne@69: self._lines.extend(lines) jpayne@69: jpayne@69: def __iter__(self): jpayne@69: return self jpayne@69: jpayne@69: def __next__(self): jpayne@69: line = self.readline() jpayne@69: if line == '': jpayne@69: raise StopIteration jpayne@69: return line jpayne@69: jpayne@69: jpayne@69: jpayne@69: class FeedParser: jpayne@69: """A feed-style parser of email.""" jpayne@69: jpayne@69: def __init__(self, _factory=None, *, policy=compat32): jpayne@69: """_factory is called with no arguments to create a new message obj jpayne@69: jpayne@69: The policy keyword specifies a policy object that controls a number of jpayne@69: aspects of the parser's operation. The default policy maintains jpayne@69: backward compatibility. jpayne@69: jpayne@69: """ jpayne@69: self.policy = policy jpayne@69: self._old_style_factory = False jpayne@69: if _factory is None: jpayne@69: if policy.message_factory is None: jpayne@69: from email.message import Message jpayne@69: self._factory = Message jpayne@69: else: jpayne@69: self._factory = policy.message_factory jpayne@69: else: jpayne@69: self._factory = _factory jpayne@69: try: jpayne@69: _factory(policy=self.policy) jpayne@69: except TypeError: jpayne@69: # Assume this is an old-style factory jpayne@69: self._old_style_factory = True jpayne@69: self._input = BufferedSubFile() jpayne@69: self._msgstack = [] jpayne@69: self._parse = self._parsegen().__next__ jpayne@69: self._cur = None jpayne@69: self._last = None jpayne@69: self._headersonly = False jpayne@69: jpayne@69: # Non-public interface for supporting Parser's headersonly flag jpayne@69: def _set_headersonly(self): jpayne@69: self._headersonly = True jpayne@69: jpayne@69: def feed(self, data): jpayne@69: """Push more data into the parser.""" jpayne@69: self._input.push(data) jpayne@69: self._call_parse() jpayne@69: jpayne@69: def _call_parse(self): jpayne@69: try: jpayne@69: self._parse() jpayne@69: except StopIteration: jpayne@69: pass jpayne@69: jpayne@69: def close(self): jpayne@69: """Parse all remaining data and return the root message object.""" jpayne@69: self._input.close() jpayne@69: self._call_parse() jpayne@69: root = self._pop_message() jpayne@69: assert not self._msgstack jpayne@69: # Look for final set of defects jpayne@69: if root.get_content_maintype() == 'multipart' \ jpayne@69: and not root.is_multipart(): jpayne@69: defect = errors.MultipartInvariantViolationDefect() jpayne@69: self.policy.handle_defect(root, defect) jpayne@69: return root jpayne@69: jpayne@69: def _new_message(self): jpayne@69: if self._old_style_factory: jpayne@69: msg = self._factory() jpayne@69: else: jpayne@69: msg = self._factory(policy=self.policy) jpayne@69: if self._cur and self._cur.get_content_type() == 'multipart/digest': jpayne@69: msg.set_default_type('message/rfc822') jpayne@69: if self._msgstack: jpayne@69: self._msgstack[-1].attach(msg) jpayne@69: self._msgstack.append(msg) jpayne@69: self._cur = msg jpayne@69: self._last = msg jpayne@69: jpayne@69: def _pop_message(self): jpayne@69: retval = self._msgstack.pop() jpayne@69: if self._msgstack: jpayne@69: self._cur = self._msgstack[-1] jpayne@69: else: jpayne@69: self._cur = None jpayne@69: return retval jpayne@69: jpayne@69: def _parsegen(self): jpayne@69: # Create a new message and start by parsing headers. jpayne@69: self._new_message() jpayne@69: headers = [] jpayne@69: # Collect the headers, searching for a line that doesn't match the RFC jpayne@69: # 2822 header or continuation pattern (including an empty line). jpayne@69: for line in self._input: jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: if not headerRE.match(line): jpayne@69: # If we saw the RFC defined header/body separator jpayne@69: # (i.e. newline), just throw it away. Otherwise the line is jpayne@69: # part of the body so push it back. jpayne@69: if not NLCRE.match(line): jpayne@69: defect = errors.MissingHeaderBodySeparatorDefect() jpayne@69: self.policy.handle_defect(self._cur, defect) jpayne@69: self._input.unreadline(line) jpayne@69: break jpayne@69: headers.append(line) jpayne@69: # Done with the headers, so parse them and figure out what we're jpayne@69: # supposed to see in the body of the message. jpayne@69: self._parse_headers(headers) jpayne@69: # Headers-only parsing is a backwards compatibility hack, which was jpayne@69: # necessary in the older parser, which could raise errors. All jpayne@69: # remaining lines in the input are thrown into the message body. jpayne@69: if self._headersonly: jpayne@69: lines = [] jpayne@69: while True: jpayne@69: line = self._input.readline() jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: if line == '': jpayne@69: break jpayne@69: lines.append(line) jpayne@69: self._cur.set_payload(EMPTYSTRING.join(lines)) jpayne@69: return jpayne@69: if self._cur.get_content_type() == 'message/delivery-status': jpayne@69: # message/delivery-status contains blocks of headers separated by jpayne@69: # a blank line. We'll represent each header block as a separate jpayne@69: # nested message object, but the processing is a bit different jpayne@69: # than standard message/* types because there is no body for the jpayne@69: # nested messages. A blank line separates the subparts. jpayne@69: while True: jpayne@69: self._input.push_eof_matcher(NLCRE.match) jpayne@69: for retval in self._parsegen(): jpayne@69: if retval is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: break jpayne@69: msg = self._pop_message() jpayne@69: # We need to pop the EOF matcher in order to tell if we're at jpayne@69: # the end of the current file, not the end of the last block jpayne@69: # of message headers. jpayne@69: self._input.pop_eof_matcher() jpayne@69: # The input stream must be sitting at the newline or at the jpayne@69: # EOF. We want to see if we're at the end of this subpart, so jpayne@69: # first consume the blank line, then test the next line to see jpayne@69: # if we're at this subpart's EOF. jpayne@69: while True: jpayne@69: line = self._input.readline() jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: break jpayne@69: while True: jpayne@69: line = self._input.readline() jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: break jpayne@69: if line == '': jpayne@69: break jpayne@69: # Not at EOF so this is a line we're going to need. jpayne@69: self._input.unreadline(line) jpayne@69: return jpayne@69: if self._cur.get_content_maintype() == 'message': jpayne@69: # The message claims to be a message/* type, then what follows is jpayne@69: # another RFC 2822 message. jpayne@69: for retval in self._parsegen(): jpayne@69: if retval is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: break jpayne@69: self._pop_message() jpayne@69: return jpayne@69: if self._cur.get_content_maintype() == 'multipart': jpayne@69: boundary = self._cur.get_boundary() jpayne@69: if boundary is None: jpayne@69: # The message /claims/ to be a multipart but it has not jpayne@69: # defined a boundary. That's a problem which we'll handle by jpayne@69: # reading everything until the EOF and marking the message as jpayne@69: # defective. jpayne@69: defect = errors.NoBoundaryInMultipartDefect() jpayne@69: self.policy.handle_defect(self._cur, defect) jpayne@69: lines = [] jpayne@69: for line in self._input: jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: lines.append(line) jpayne@69: self._cur.set_payload(EMPTYSTRING.join(lines)) jpayne@69: return jpayne@69: # Make sure a valid content type was specified per RFC 2045:6.4. jpayne@69: if (str(self._cur.get('content-transfer-encoding', '8bit')).lower() jpayne@69: not in ('7bit', '8bit', 'binary')): jpayne@69: defect = errors.InvalidMultipartContentTransferEncodingDefect() jpayne@69: self.policy.handle_defect(self._cur, defect) jpayne@69: # Create a line match predicate which matches the inter-part jpayne@69: # boundary as well as the end-of-multipart boundary. Don't push jpayne@69: # this onto the input stream until we've scanned past the jpayne@69: # preamble. jpayne@69: separator = '--' + boundary jpayne@69: boundaryre = re.compile( jpayne@69: '(?P' + re.escape(separator) + jpayne@69: r')(?P--)?(?P[ \t]*)(?P\r\n|\r|\n)?$') jpayne@69: capturing_preamble = True jpayne@69: preamble = [] jpayne@69: linesep = False jpayne@69: close_boundary_seen = False jpayne@69: while True: jpayne@69: line = self._input.readline() jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: if line == '': jpayne@69: break jpayne@69: mo = boundaryre.match(line) jpayne@69: if mo: jpayne@69: # If we're looking at the end boundary, we're done with jpayne@69: # this multipart. If there was a newline at the end of jpayne@69: # the closing boundary, then we need to initialize the jpayne@69: # epilogue with the empty string (see below). jpayne@69: if mo.group('end'): jpayne@69: close_boundary_seen = True jpayne@69: linesep = mo.group('linesep') jpayne@69: break jpayne@69: # We saw an inter-part boundary. Were we in the preamble? jpayne@69: if capturing_preamble: jpayne@69: if preamble: jpayne@69: # According to RFC 2046, the last newline belongs jpayne@69: # to the boundary. jpayne@69: lastline = preamble[-1] jpayne@69: eolmo = NLCRE_eol.search(lastline) jpayne@69: if eolmo: jpayne@69: preamble[-1] = lastline[:-len(eolmo.group(0))] jpayne@69: self._cur.preamble = EMPTYSTRING.join(preamble) jpayne@69: capturing_preamble = False jpayne@69: self._input.unreadline(line) jpayne@69: continue jpayne@69: # We saw a boundary separating two parts. Consume any jpayne@69: # multiple boundary lines that may be following. Our jpayne@69: # interpretation of RFC 2046 BNF grammar does not produce jpayne@69: # body parts within such double boundaries. jpayne@69: while True: jpayne@69: line = self._input.readline() jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: mo = boundaryre.match(line) jpayne@69: if not mo: jpayne@69: self._input.unreadline(line) jpayne@69: break jpayne@69: # Recurse to parse this subpart; the input stream points jpayne@69: # at the subpart's first line. jpayne@69: self._input.push_eof_matcher(boundaryre.match) jpayne@69: for retval in self._parsegen(): jpayne@69: if retval is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: break jpayne@69: # Because of RFC 2046, the newline preceding the boundary jpayne@69: # separator actually belongs to the boundary, not the jpayne@69: # previous subpart's payload (or epilogue if the previous jpayne@69: # part is a multipart). jpayne@69: if self._last.get_content_maintype() == 'multipart': jpayne@69: epilogue = self._last.epilogue jpayne@69: if epilogue == '': jpayne@69: self._last.epilogue = None jpayne@69: elif epilogue is not None: jpayne@69: mo = NLCRE_eol.search(epilogue) jpayne@69: if mo: jpayne@69: end = len(mo.group(0)) jpayne@69: self._last.epilogue = epilogue[:-end] jpayne@69: else: jpayne@69: payload = self._last._payload jpayne@69: if isinstance(payload, str): jpayne@69: mo = NLCRE_eol.search(payload) jpayne@69: if mo: jpayne@69: payload = payload[:-len(mo.group(0))] jpayne@69: self._last._payload = payload jpayne@69: self._input.pop_eof_matcher() jpayne@69: self._pop_message() jpayne@69: # Set the multipart up for newline cleansing, which will jpayne@69: # happen if we're in a nested multipart. jpayne@69: self._last = self._cur jpayne@69: else: jpayne@69: # I think we must be in the preamble jpayne@69: assert capturing_preamble jpayne@69: preamble.append(line) jpayne@69: # We've seen either the EOF or the end boundary. If we're still jpayne@69: # capturing the preamble, we never saw the start boundary. Note jpayne@69: # that as a defect and store the captured text as the payload. jpayne@69: if capturing_preamble: jpayne@69: defect = errors.StartBoundaryNotFoundDefect() jpayne@69: self.policy.handle_defect(self._cur, defect) jpayne@69: self._cur.set_payload(EMPTYSTRING.join(preamble)) jpayne@69: epilogue = [] jpayne@69: for line in self._input: jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: self._cur.epilogue = EMPTYSTRING.join(epilogue) jpayne@69: return jpayne@69: # If we're not processing the preamble, then we might have seen jpayne@69: # EOF without seeing that end boundary...that is also a defect. jpayne@69: if not close_boundary_seen: jpayne@69: defect = errors.CloseBoundaryNotFoundDefect() jpayne@69: self.policy.handle_defect(self._cur, defect) jpayne@69: return jpayne@69: # Everything from here to the EOF is epilogue. If the end boundary jpayne@69: # ended in a newline, we'll need to make sure the epilogue isn't jpayne@69: # None jpayne@69: if linesep: jpayne@69: epilogue = [''] jpayne@69: else: jpayne@69: epilogue = [] jpayne@69: for line in self._input: jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: epilogue.append(line) jpayne@69: # Any CRLF at the front of the epilogue is not technically part of jpayne@69: # the epilogue. Also, watch out for an empty string epilogue, jpayne@69: # which means a single newline. jpayne@69: if epilogue: jpayne@69: firstline = epilogue[0] jpayne@69: bolmo = NLCRE_bol.match(firstline) jpayne@69: if bolmo: jpayne@69: epilogue[0] = firstline[len(bolmo.group(0)):] jpayne@69: self._cur.epilogue = EMPTYSTRING.join(epilogue) jpayne@69: return jpayne@69: # Otherwise, it's some non-multipart type, so the entire rest of the jpayne@69: # file contents becomes the payload. jpayne@69: lines = [] jpayne@69: for line in self._input: jpayne@69: if line is NeedMoreData: jpayne@69: yield NeedMoreData jpayne@69: continue jpayne@69: lines.append(line) jpayne@69: self._cur.set_payload(EMPTYSTRING.join(lines)) jpayne@69: jpayne@69: def _parse_headers(self, lines): jpayne@69: # Passed a list of lines that make up the headers for the current msg jpayne@69: lastheader = '' jpayne@69: lastvalue = [] jpayne@69: for lineno, line in enumerate(lines): jpayne@69: # Check for continuation jpayne@69: if line[0] in ' \t': jpayne@69: if not lastheader: jpayne@69: # The first line of the headers was a continuation. This jpayne@69: # is illegal, so let's note the defect, store the illegal jpayne@69: # line, and ignore it for purposes of headers. jpayne@69: defect = errors.FirstHeaderLineIsContinuationDefect(line) jpayne@69: self.policy.handle_defect(self._cur, defect) jpayne@69: continue jpayne@69: lastvalue.append(line) jpayne@69: continue jpayne@69: if lastheader: jpayne@69: self._cur.set_raw(*self.policy.header_source_parse(lastvalue)) jpayne@69: lastheader, lastvalue = '', [] jpayne@69: # Check for envelope header, i.e. unix-from jpayne@69: if line.startswith('From '): jpayne@69: if lineno == 0: jpayne@69: # Strip off the trailing newline jpayne@69: mo = NLCRE_eol.search(line) jpayne@69: if mo: jpayne@69: line = line[:-len(mo.group(0))] jpayne@69: self._cur.set_unixfrom(line) jpayne@69: continue jpayne@69: elif lineno == len(lines) - 1: jpayne@69: # Something looking like a unix-from at the end - it's jpayne@69: # probably the first line of the body, so push back the jpayne@69: # line and stop. jpayne@69: self._input.unreadline(line) jpayne@69: return jpayne@69: else: jpayne@69: # Weirdly placed unix-from line. Note this as a defect jpayne@69: # and ignore it. jpayne@69: defect = errors.MisplacedEnvelopeHeaderDefect(line) jpayne@69: self._cur.defects.append(defect) jpayne@69: continue jpayne@69: # Split the line on the colon separating field name from value. jpayne@69: # There will always be a colon, because if there wasn't the part of jpayne@69: # the parser that calls us would have started parsing the body. jpayne@69: i = line.find(':') jpayne@69: jpayne@69: # If the colon is on the start of the line the header is clearly jpayne@69: # malformed, but we might be able to salvage the rest of the jpayne@69: # message. Track the error but keep going. jpayne@69: if i == 0: jpayne@69: defect = errors.InvalidHeaderDefect("Missing header name.") jpayne@69: self._cur.defects.append(defect) jpayne@69: continue jpayne@69: jpayne@69: assert i>0, "_parse_headers fed line with no : and no leading WS" jpayne@69: lastheader = line[:i] jpayne@69: lastvalue = [line] jpayne@69: # Done with all the lines, so handle the last header. jpayne@69: if lastheader: jpayne@69: self._cur.set_raw(*self.policy.header_source_parse(lastvalue)) jpayne@69: jpayne@69: jpayne@69: class BytesFeedParser(FeedParser): jpayne@69: """Like FeedParser, but feed accepts bytes.""" jpayne@69: jpayne@69: def feed(self, data): jpayne@69: super().feed(data.decode('ascii', 'surrogateescape'))