Exemplo n.º 1
0
    def onMime(self, who, data):
        ''' Handles incoming MIME messages, such as those sent by
        NotesBuddy containing embedded image data '''

        from email.Parser import Parser
        from StringIO import StringIO

        print '<mime>%s' % who[0]
        msg = Parser().parsestr(data)

        html = StringIO()  # combined text segments
        images = {}  # map of Content-ID:binary image

        for part in msg.walk():
            mt = part.get_content_maintype()
            if mt == 'text':
                html.write(part.get_payload(decode=True))
            elif mt == 'image':
                cid = part.get('Content-ID')
                images[cid] = part.get_payload(decode=True)

        print ' <text/html>:', html.getvalue()
        html.close()

        print ' <images>:', [k[1:-1] for k in images.keys()]
Exemplo n.º 2
0
    def __init__(self, ui, path, rev=None):
        super(gnuarch_source, self).__init__(ui, path, rev=rev)

        if not os.path.exists(os.path.join(path, '{arch}')):
            raise NoRepo(
                _("%s does not look like a GNU Arch repository") % path)

        # Could use checktool, but we want to check for baz or tla.
        self.execmd = None
        if util.findexe('baz'):
            self.execmd = 'baz'
        else:
            if util.findexe('tla'):
                self.execmd = 'tla'
            else:
                raise util.Abort(_('cannot find a GNU Arch tool'))

        commandline.__init__(self, ui, self.execmd)

        self.path = os.path.realpath(path)
        self.tmppath = None

        self.treeversion = None
        self.lastrev = None
        self.changes = {}
        self.parents = {}
        self.tags = {}
        self.catlogparser = Parser()
        self.encoding = encoding.encoding
        self.archives = []
Exemplo n.º 3
0
    def __parse_revision_logs(self, fqrevlist, update=True):
        changesets = []
        logparser = Parser()
        c = ExternalCommand(cwd=self.repository.basedir,
                            command=self.repository.command("cat-archive-log"))
        for fqrev in fqrevlist:
            out, err = c.execute(fqrev, stdout=PIPE, stderr=PIPE)
            if c.exit_status:
                raise GetUpstreamChangesetsFailure(
                    "%s returned status %d saying\n%s" %
                    (str(c), c.exit_status, err.read()))
            err = None
            try:
                msg = logparser.parse(out)
            except Exception, err:
                pass
            if not err and msg.is_multipart():
                err = "unable to parse log description"
            if not err and update and msg.has_key('Continuation-of'):
                err = "in-version continuations not supported"
            if err:
                raise GetUpstreamChangesetsFailure(str(err))

            date = self.__parse_date(msg['Date'], msg['Standard-date'])
            author = msg['Creator']
            revision = fqrev
            logmsg = [msg['Summary']]
            s  = msg.get('Keywords', "").strip()
            if s:
                logmsg.append('Keywords: ' + s)
            s = msg.get_payload().strip()
            if s:
                logmsg.append(s)
            logmsg = '\n'.join(logmsg)
            changesets.append(Changeset(revision, date, author, logmsg))
Exemplo n.º 4
0
    def __parse_revision_logs(self, fqrevlist, update=True):
        changesets = []
        logparser = Parser()
        c = ExternalCommand(cwd=self.repository.basedir,
                            command=self.repository.command("cat-archive-log"))
        for fqrev in fqrevlist:
            out, err = c.execute(fqrev, stdout=PIPE, stderr=PIPE)
            if c.exit_status:
                raise GetUpstreamChangesetsFailure(
                    "%s returned status %d saying\n%s" %
                    (str(c), c.exit_status, err.read()))
            err = None
            try:
                msg = logparser.parse(out)
            except Exception, err:
                pass
            if not err and msg.is_multipart():
                err = "unable to parse log description"
            if not err and update and msg.has_key('Continuation-of'):
                err = "in-version continuations not supported"
            if err:
                raise GetUpstreamChangesetsFailure(str(err))

            date = self.__parse_date(msg['Date'], msg['Standard-date'])
            author = msg['Creator']
            revision = fqrev
            logmsg = [msg['Summary']]
            s = msg.get('Keywords', "").strip()
            if s:
                logmsg.append('Keywords: ' + s)
            s = msg.get_payload().strip()
            if s:
                logmsg.append(s)
            logmsg = '\n'.join(logmsg)
            changesets.append(Changeset(revision, date, author, logmsg))
Exemplo n.º 5
0
    def parse(self, content):
        p = Parser()
        msgobj = p.parsestr(content)
        subject = self.parse_header_field(msgobj['Subject'])
        attachments = []
        body = []
        html = []
        self.parse_body(msgobj.walk(), attachments, body, html)
        body = u'\n'.join(body)
        html = u'\n'.join(html)

        tos = self.get_address_list(msgobj, 'To')
        tos.extend(self.get_address_list(msgobj, 'X-Original-To'))
        ccs = self.get_address_list(msgobj, 'Cc')
        resent_tos = self.get_address_list(msgobj, 'resent-to')
        resent_ccs = self.get_address_list(msgobj, 'resent-cc')

        from_field = parseaddr(msgobj.get('From'))
        from_field = (self.parse_header_field(from_field[0]), from_field[1])
        date = self.parse_date(msgobj.get("Date"))
        return {
            'msgobj': msgobj,
            'date': date,
            'subject': subject,
            'body': body,
            'html': html,
            'from': from_field,
            'to': tos,
            'cc': ccs,
            'resent_to': resent_tos,
            'resent_cc': resent_ccs,
            'attachments': attachments
        }
Exemplo n.º 6
0
    def test_create_multipart_string_from_files(self):
        files = []
        values = []
        for i in range(3):
            _, filename = tempfile.mkstemp()
            files.append(('plain', filename))
            with open(filename, 'wb') as f:
                value = "value-%d" % i
                f.write(value)
                values.append(value)

        result = Util.create_multipart_string_from_files(files)

        for mime, f in files:
            os.remove(f)

        with closing(StringIO(result)) as buf:
            parser = Parser()
            msg = parser.parse(buf)

        for part in msg.walk():
            if msg.is_multipart():
                i = 0
                for msg in part.get_payload():
                    self.assertEqual(values[i], msg.get_payload())
                    i += 1
Exemplo n.º 7
0
def poll_newsgroup(mlist, conn, first, last, glock):
    listname = mlist.internal_name()
    # NEWNEWS is not portable and has synchronization issues.
    for num in range(first, last):
        glock.refresh()
        try:
            headers = conn.head(repr(num))[3]
            found_to = False
            beenthere = False
            for header in headers:
                i = header.find(':')
                value = header[:i].lower()
                if i > 0 and value == 'to':
                    found_to = True
                # FIXME 2010-02-16 barry use List-Post header.
                if value <> 'x-beenthere':
                    continue
                if header[i:] == ': %s' % mlist.posting_address:
                    beenthere = True
                    break
            if not beenthere:
                body = conn.body(repr(num))[3]
                # Usenet originated messages will not have a Unix envelope
                # (i.e. "From " header).  This breaks Pipermail archiving, so
                # we will synthesize one.  Be sure to use the format searched
                # for by mailbox.UnixMailbox._isrealfromline().  BAW: We use
                # the -bounces address here in case any downstream clients use
                # the envelope sender for bounces; I'm not sure about this,
                # but it's the closest to the old semantics.
                lines = ['From %s  %s' % (mlist.GetBouncesEmail(),
                                          time.ctime(time.time()))]
                lines.extend(headers)
                lines.append('')
                lines.extend(body)
                lines.append('')
                p = Parser(Message.Message)
                try:
                    msg = p.parsestr(NL.join(lines))
                except email.Errors.MessageError as e:
                    log.error('email package exception for %s:%d\n%s',
                              mlist.linked_newsgroup, num, e)
                    raise _ContinueLoop
                if found_to:
                    del msg['X-Originally-To']
                    msg['X-Originally-To'] = msg['To']
                    del msg['To']
                msg['To'] = mlist.posting_address
                # Post the message to the locked list
                inq = Switchboard(config.INQUEUE_DIR)
                inq.enqueue(msg,
                            listid=mlist.list_id,
                            fromusenet=True)
                log.info('posted to list %s: %7d', listname, num)
        except nntplib.NNTPError as e:
            log.exception('NNTP error for list %s: %7d', listname, num)
        except _ContinueLoop:
            continue
        # Even if we don't post the message because it was seen on the
        # list already, update the watermark
        mlist.usenet_watermark = num
Exemplo n.º 8
0
    def parse(self, content):
        p = Parser()
        msgobj = p.parsestr(content)
        subject = self.parse_header_field(msgobj['Subject'])
        attachments = []
        body = []
        html = []
        self.parse_body(msgobj.walk(), attachments, body, html)
        body = u'\n'.join(body)
        html = u'\n'.join(html)

        tos = self.get_address_list(msgobj, 'To')
        tos.extend(self.get_address_list(msgobj, 'X-Original-To'))
        ccs = self.get_address_list(msgobj, 'Cc')
        resent_tos = self.get_address_list(msgobj, 'resent-to')
        resent_ccs = self.get_address_list(msgobj, 'resent-cc')

        from_field = parseaddr(msgobj.get('From'))
        from_field = (self.parse_header_field(from_field[0]), from_field[1])
        date = self.parse_date(msgobj.get("Date"))
        return {
            'msgobj': msgobj,
            'date': date,
            'subject': subject,
            'body': body,
            'html': html,
            'from': from_field,
            'to': tos,
            'cc': ccs,
            'resent_to': resent_tos,
            'resent_cc': resent_ccs,
            'attachments': attachments
        }
Exemplo n.º 9
0
def get_header_content(name='', str_encoded=''):
    message = Parser().parse(open(sys.argv[1]))
    for line in NEWLINE.split(message.get(name)):
        decoded_headers = decode_header(line)
        for parts in decoded_headers:
            str_encoded = str_encoded + parts[0]
    return re.sub('(\r\n|\s|\t){2,}', ' ', str_encoded)
Exemplo n.º 10
0
def parse(content):
    p = EmailParser()
    msgobj = p.parsestr(content)
    if msgobj['Subject'] is not None:
        decodefrag = decode_header(msgobj['Subject'])
        subj_fragments = []
        for s, enc in decodefrag:
            if enc:
                s = unicode(s , enc).encode('utf8', 'replace')
            subj_fragments.append(s)
        subject = ''.join(subj_fragments)
    else:
        subject = None

    attachments = []
    body = None
    html = None
    images = []
    images_content_type = [
        "image/jpg",
        "image/jpeg",
        "image/png",
        "image/tiff"
        "application/pdf",
    ]

    for part in msgobj.walk():
        print part.get_content_type()
        attachment = parse_attachment(part)
        if attachment:
            attachments.append(attachment)
        elif part.get_content_type() == "text/plain":
            if body is None:
                body = ""
            body += unicode(
                part.get_payload(decode=True),
                part.get_content_charset(),
                'replace'
            ).encode('utf8', 'replace')
        elif part.get_content_type() == "text/html":
            if html is None:
                html = ""
            html += unicode(
                part.get_payload(decode=True),
                part.get_content_charset(),
                'replace'
            ).encode('utf8', 'replace')
        elif part.get_content_type() in images_content_type:
            images.append(StringIO(part.get_payload(decode=True)))

    return {
        'subject': subject,
        'body': body,
        'html': html,
        'from': parseaddr(msgobj.get('From'))[1],
        'to': parseaddr(msgobj.get('To'))[1],
        'attachments': attachments,
        'images': images,
    }
Exemplo n.º 11
0
def parse(content):
    p = EmailParser()
    if type(content) == str:
        msgobj = p.parsestr(content)
    else:
        msgobj = p.parse(content)
    attachments = []
    return _parse2(msgobj, attachments)
def parse(content):
    """
    Eメールのコンテンツを受け取りparse,encodeして返す
    """
    p = EmailParser()
    msgobj = p.parse(content)
    attachments = []
    return parse2(msgobj, attachments)
def parse(content):
    """
    Eメールのコンテンツを受け取りparse,encodeして返す
    """
    p = EmailParser()
    msgobj = p.parse(content)
    attachments = []
    return parse2(msgobj, attachments)
Exemplo n.º 14
0
def parse(content):
    p = EmailParser()
    if type(content) == str:
        msgobj = p.parsestr(content)
    else:
        msgobj = p.parse(content)
    attachments = []
    return _parse2(msgobj, attachments)
Exemplo n.º 15
0
def showmessage(i, msgList):
    if 1 <= i <= len(msgList):
        print('-'*80)
        msg = Parser( ).parsestr(msgList[i-1])
        print(msg.get_payload( ))            # print(s payload: string, or [Messages]
       #print(msgList[i-1]              # old: print(s entire mail--hdrs+text
        print('-'*80)                    # to get text only, call file.read( )
    else:                               # after rfc822.Message reads hdr lines
        print('Bad message number')
Exemplo n.º 16
0
def parse(content):
    """
	parse email
    """
    p = EmailParser()
	#check content is a file or text
	#if content is path...

    #msgobj = p.parse(content)
	
    msgobj = p.parsestr(content)
    if msgobj['Subject'] is not None:
        decodefrag = decode_header(msgobj['Subject'])
        subj_fragments = []
        for s , enc in decodefrag:
            if enc:
                s = unicode(s , enc).encode('utf8','replace')
            subj_fragments.append(s)
        subject = ''.join(subj_fragments)
    else:
        subject = None

    attachments = []
    body = None
    html = None
    for part in msgobj.walk():
        attachment = parse_attachment(part)
        if attachment:
            attachments.append(attachment)
        elif part.get_content_type() == "text/plain":
            if body is None:
                body = ""
                if part.get_content_charset:
                    body += part.get_payload(decode=True)
                else:
                    body += unicode(
                    part.get_payload(decode=True),
                    part.get_content_charset(),
                'replace'
            ).encode('utf8','replace')
        elif part.get_content_type() == "text/html":
            if html is None:
                html = ""
            html += unicode(
                part.get_payload(decode=True),
                part.get_content_charset(),
                'replace'
            ).encode('utf8','replace')
    return {
        'subject' : subject,
        'body' : body,
        'html' : html,
        'from' : parseaddr(msgobj.get('From'))[1], 
        'to' : parseaddr(msgobj.get('To'))[1], 
		'date' : parse_date(msgobj.get('Date')),
        'attachments': attachments,
    }
Exemplo n.º 17
0
def showmessage(i, msgList):
    if 1 <= i <= len(msgList):
        print('-'*80)
        msg = Parser( ).parsestr(msgList[i-1])
        print(msg.get_payload( ))            # print(s payload: string, or [Messages]
       #print(msgList[i-1]              # old: print(s entire mail--hdrs+text
        print('-'*80)                    # to get text only, call file.read( )
    else:                               # after rfc822.Message reads hdr lines
        print('Bad message number')
Exemplo n.º 18
0
 def parse(self, content):
     p = Parser()
     msgobj = p.parsestr(content)
     if msgobj['Subject'] is not None:
         decodefrag = decode_header(msgobj['Subject'])
         subj_fragments = []
         for s , enc in decodefrag:
             if enc:
                 s = unicode(s , enc).encode('utf8','replace')
             subj_fragments.append(s)
         subject = ''.join(subj_fragments)
     else:
         subject = None
     subject = subject.replace('\n\t', " ")
     attachments = []
     body = None
     html = None
     for part in msgobj.walk():
         attachment = self.parse_attachment(part)
         if attachment:
             attachments.append(attachment)
         elif part.get_content_type() == "text/plain":
             if body is None:
                 body = ""
             charset = part.get_content_charset() or 'ascii'
             body += unicode(
                 part.get_payload(decode=True),
                 charset,
                 'replace').encode('utf8','replace')
         elif part.get_content_type() == "text/html":
             if html is None:
                 html = ""
             charset = part.get_content_charset() or 'ascii'
             html += unicode(
                 part.get_payload(decode=True),
                 charset,
                 'replace').encode('utf8','replace')
     tos = getaddresses(msgobj.get_all('To', []))
     ccs = getaddresses(msgobj.get_all('Cc', []))
     resent_tos = getaddresses(msgobj.get_all('resent-to', []))
     resent_ccs = getaddresses(msgobj.get_all('resent-cc', []))
     date = self.parse_date(msgobj.get("Date"))
     return {
         'msgobj': msgobj,
         'date': date,
         'subject': subject,
         'body': body,
         'html': html,
         'from': parseaddr(msgobj.get('From')),
         'to': tos,
         'cc': ccs,
         'resent_to': resent_tos,
         'resent_cc': resent_ccs,
         'attachments': attachments
     }
Exemplo n.º 19
0
    def check_email(self, ip, username, password, hash_code, threshold):
        self.log.info("checking inbox for matching hash code %s" % hash_code)
        subject_to_match = self.generate_subject(hash_code)

        start = time.time()
        found = False
        parser = Parser()


        already_slept = False

        # continue to loop over the inbox messages while we haven't found
        # what we're looking for and we're still under our time threshold
        self.log.info("starting inbox-checking loop")
        while not found and time.time() - start < threshold:
            self.log.info("checking inbox")

            # the only way to fail the check from a test case is to check the inbox
            # immediately, so we have to pass that option in manually
            if self.options.get("nofirstsleep") and not already_slept: already_slept = True
            else: time.sleep(int(self.config.get("Inbox_plugin", "check_delay")))

            # we re-connect every time because for some reason sometimes the
            # pop list gets cached, so re-listing it doesn't do any good.
            self.log.info("connecting and logging in")
            inbox = self.default_class(ip, self.port)
            inbox.getwelcome()
            inbox.user(username)
            inbox.pass_(password)

            self.log.info("getting inbox status")
            inbox.stat()
            status, messages, octets = inbox.list()


            # loop over the messages
            self.log.info("looping over %d messages", len(messages))
            for message in messages:
                index, size = message.split(" ")
                status, message, size = inbox.retr(index)
                message = "\n".join(message)

                # try to match the email subject to the subject we're expecting
                headers = parser.parsestr(message, True)
                if headers["Subject"] == subject_to_match:
                    self.log.info("found match")
                    inbox.dele(index)
                    found = True
                    break

            self.log.info("logging out of inbox")
            inbox.quit()

        return found
Exemplo n.º 20
0
 def stepCheckBugMessageNotificationReAssign(self, sequence=None, sequence_list=None, **kw):
   """
     Check the bug message when re-assign
   """
   last_message = self.portal.MailHost._last_message
   self.assertNotEquals((), last_message)
   mfrom, mto, messageText = last_message
   from email.Parser import Parser
   p = Parser()
   m = p.parsestr(messageText)
   self.assertTrue('Re-assign!' in m.get_payload()[0].get_payload(decode=True))
Exemplo n.º 21
0
def parse(content):
    """
    Parse the email and return a dictionary of relevant data.
    """
    p = EmailParser()
    msgobj = p.parsestr(content)
    if msgobj['Subject'] is not None:
        decodefrag = decode_header(msgobj['Subject'])
        subj_fragments = []
        for s , enc in decodefrag:
            if enc:
                s = unicode(s , enc).encode('utf8','replace')
            subj_fragments.append(s)
        subject = ''.join(subj_fragments)
    else:
        subject = None

    attachments = []
    body = None
    html = None
    for part in msgobj.walk():
        attachment = parse_attachment(part)
        if attachment:
            attachments.append(attachment)
        elif part.get_content_type() == "text/plain":
            if body is None:
                body = ""
            body += unicode(
                part.get_payload(decode=True),
                part.get_content_charset(),
                'replace'
            ).encode('utf8','replace')
        elif part.get_content_type() == "text/html":
            if html is None:
                html = ""
            html += unicode(
                part.get_payload(decode=True),
                part.get_content_charset(),
                'replace'
            ).encode('utf8','replace')
    return {
        'subject' : subject,
        'body' : body,
        'html' : html,
        'from' : parseaddr(msgobj.get('From'))[1], # Leave off the name and only return the address
        'to' : parseaddr(msgobj.get('To'))[1], # Leave off the name and only return the address
        'attachments': attachments,
    }
Exemplo n.º 22
0
def parse(content):
    """
    Eメールのコンテンツを受け取りparse,encodeして返す
    """
    p = EmailParser()
    msgobj = p.parse(content)
    if msgobj['Subject'] is not None:
        decodefrag = decode_header(msgobj['Subject'])
        subj_fragments = []
        for s , enc in decodefrag:
            if enc:
                s = str(s , enc).encode('utf8','replace')
            subj_fragments.append(s)
        subject = ''.join(subj_fragments)
    else:
        subject = None

    attachments = []
    body = None
    html = None
    for part in msgobj.walk():
        attachment = parse_attachment(part)
        if attachment:
            attachments.append(attachment)
        elif part.get_content_type() == "text/plain":
            if body is None:
                body = ""
            body += str(
                part.get_payload(decode=True),
                part.get_content_charset(),
                'replace'
            ).encode('utf8','replace')
        elif part.get_content_type() == "text/html":
            if html is None:
                html = ""
            html += str(
                part.get_payload(decode=True),
                part.get_content_charset(),
                'replace'
            ).encode('utf8','replace')
    return {
        'subject' : subject,
        'body' : body,
        'html' : html,
        'from' : parseaddr(msgobj.get('From'))[1], # 名前は除いてメールアドレスのみ抽出
        'to' : parseaddr(msgobj.get('To'))[1], # 名前は除いてメールアドレスのみ抽出
        'attachments': attachments,
    }
Exemplo n.º 23
0
 def __init__(self, slice=None, numslices=1):
     # Don't call the base class constructor, but build enough of the
     # underlying attributes to use the base class's implementation.
     self._stop = 0
     self._dir = os.path.join(mm_cfg.MAILDIR_DIR, 'new')
     self._cur = os.path.join(mm_cfg.MAILDIR_DIR, 'cur')
     self._parser = Parser(Message)
 def actualizar(self):
     """Ontiene los datos del servidor"""
     print "llamada a actualizar"
     # Se obtiene el numero de mensajes pendientes y se hace un
     # bucle para cada mensaje
     self.numero = len(self.m.list()[1])
     for i in range(self.numero):
         #    print "Mensaje numero"+str(i+1)
         #    print "--------------------"
         # Se lee el mensaje
         self.response, self.headerLines, self.bytes = self.m.retr(i + 1)
         # Se mete todo el mensaje en un unico string
         self.mensaje = "\n".join(self.headerLines)
         # Se parsea el mensaje
         self.p = Parser()
         self.email = self.p.parsestr(self.mensaje)
         # Se sacan por pantalla los campos from, to y subject
         print "From: " + self.email["From"]
         print "To: " + self.email["To"]
         print "Subject: " + self.email["Subject"]
         print self.email.get_content_type()
         print self.email.get_payload(decode=True)
         print self.email.is_multipart()
         self.tipo = self.email.get_content_type()
         for codigo in self.codigos:
             if ("text/plain" == self.tipo) and (self.email.get_payload(decode=True).lower() == codigo):
                 # Si es texto plano, se escribe en pantalla
                 print self.email.get_payload(decode=True)
                 self.m.dele(i + 1)  # le sumamos 1 ya que python cuenta desde 0 y los emails desde 1
                 self.cerrar_conexion()  # nos desconectamos solo si borramos algo, y lo borramos solo si es un mensaje de "ok"
                 self.conectar()  # y de esta forma se guardan los archivos en el server
                 return codigo  # retorna el comando que se encontro en el e-mail
     return False  # retorna falso en caso de no encontrar nada
Exemplo n.º 25
0
def get_message_date(content, header='Date'):
    """Parses mail and returns resulting timestamp.

    :param header: the header to extract date from;
    :returns: timestamp or `None` in the case of failure.
    """

    message = MailParser().parsestr(content, True)
    dateheader = message.get(header)
    # parsedate_tz returns a 10-tuple that can be passed to mktime_tz
    # Will be None if missing or not in a valid format.  Note that
    # indexes 6, 7, and 8 of the result tuple are not usable.
    datetuple = email.utils.parsedate_tz(dateheader)
    if datetuple is None:
        return None
    return email.utils.mktime_tz(datetuple)
Exemplo n.º 26
0
class MessageIterator(object):
    """Iterates over a messages in a series of gzipped mailboxes in the 
       benchmark data directory.  Conveniently aggregates all messages
       in all mailboxes into a single iterable.
    """
    email_parser = Parser()

    def __init__(self):
        self.file_list = get_mailbox_filenames()
        self._next_file()

    def _next_file(self):
        if self.file_list:
            fname = self.file_list.pop(0)

            # Read whole thing into memory and manipulate it.
            # Not the most efficient but good enough for testing
            print "load %s" % fname
            self.messages = gzip.open(os.path.join(
                BENCHMARK_DATA_DIR, fname)).read().split('\nFrom ')

        else:
            raise StopIteration

    def next(self):
        if not self.messages:
            self._next_file()
        message = self.messages.pop(0)
        return self.email_parser.parsestr(message[message.index('\n') + 1:])

    def __iter__(self):
        return self
Exemplo n.º 27
0
def get_message_date(content, header='Date'):
    """Parses mail and returns resulting timestamp.

    :param header: the header to extract date from;
    :returns: timestamp or `None` in the case of failure.
    """

    message = MailParser().parsestr(content, True)
    dateheader = message.get(header)
    # parsedate_tz returns a 10-tuple that can be passed to mktime_tz
    # Will be None if missing or not in a valid format.  Note that
    # indexes 6, 7, and 8 of the result tuple are not usable.
    datetuple = email.utils.parsedate_tz(dateheader)
    if datetuple is None:
        return None
    return email.utils.mktime_tz(datetuple)
Exemplo n.º 28
0
def message_from_string(s, *args, **kws):
    """Parse a string into a Message object model.

    Optional _class and strict are passed to the Parser constructor.
    """
    from email.Parser import Parser
    return Parser(*args, **kws).parsestr(s)
Exemplo n.º 29
0
    def __init__(self, ui, path, revs=None):
        super(gnuarch_source, self).__init__(ui, path, revs=revs)

        if not os.path.exists(os.path.join(path, '{arch}')):
            raise NoRepo(_("%s does not look like a GNU Arch repository")
                         % path)

        # Could use checktool, but we want to check for baz or tla.
        self.execmd = None
        if util.findexe('baz'):
            self.execmd = 'baz'
        else:
            if util.findexe('tla'):
                self.execmd = 'tla'
            else:
                raise util.Abort(_('cannot find a GNU Arch tool'))

        commandline.__init__(self, ui, self.execmd)

        self.path = os.path.realpath(path)
        self.tmppath = None

        self.treeversion = None
        self.lastrev = None
        self.changes = {}
        self.parents = {}
        self.tags = {}
        self.catlogparser = Parser()
        self.encoding = encoding.encoding
        self.archives = []
Exemplo n.º 30
0
def message_from_file(fp, *args, **kws):
    """Read a file and parse its contents into a Message object model.

    Optional _class and strict are passed to the Parser constructor.
    """
    from email.Parser import Parser
    return Parser(*args, **kws).parse(fp)
Exemplo n.º 31
0
def decodeMessageAsString(msg):
    """ This helper method takes Message object or string and returns
        string which does not contain base64 encoded parts
        Returns message without any encoding in parts
    """
    if isinstance(msg, str):
        msg = Parser().parsestr(msg)

    new = deepcopy(msg)
    # From is utf8 encoded: '=?utf-8?q?Site_Administrator_=3C=3E?='
    new.replace_header('From', decode_header(new['From'])[0][0])
    new.replace_header('Subject', decode_header(new['Subject'])[0][0])
    charset = Charset('utf-8')
    charset.header_encoding = SHORTEST
    charset.body_encoding = QP
    charset.output_charset = 'utf-8'

    for part in new.walk():
        if part.get_content_maintype() == "multipart":
            continue
        decoded = part.get_payload(decode=1)
        del part['Content-Transfer-Encoding']
        part.set_payload(decoded, charset)

    return new.as_string()
class lector_email:
    """Clase utilizada para leer los emails de comandos"""

    def __init__(self, usuario, clave):
        """Se establece conexion con el servidor pop de gmail"""
        self.usuario = usuario
        self.clave = clave
        self.conectar()
        self.codigos = []
        self.codigos.append("ok")  # Definimos un codigo inicial y basico

    def conectar(self):
        """Se conecta con el servidor pop3"""
        self.m = poplib.POP3_SSL("pop.gmail.com", 995)
        self.m.user(self.usuario)
        self.m.pass_(self.clave)
        print "conectado al server pop3"

    def definir_mensaje(self, nuevo_mensaje):
        """Agrega nuevo_mensaje a la lista sef.codigo"""
        self.codigos.append(nuevo_mensaje.lower())

    def cerrar_conexion(self):
        """Encargado de cerrar la coneccion"""
        self.m.quit()

    def actualizar(self):
        """Ontiene los datos del servidor"""
        print "llamada a actualizar"
        # Se obtiene el numero de mensajes pendientes y se hace un
        # bucle para cada mensaje
        self.numero = len(self.m.list()[1])
        for i in range(self.numero):
            #    print "Mensaje numero"+str(i+1)
            #    print "--------------------"
            # Se lee el mensaje
            self.response, self.headerLines, self.bytes = self.m.retr(i + 1)
            # Se mete todo el mensaje en un unico string
            self.mensaje = "\n".join(self.headerLines)
            # Se parsea el mensaje
            self.p = Parser()
            self.email = self.p.parsestr(self.mensaje)
            # Se sacan por pantalla los campos from, to y subject
            print "From: " + self.email["From"]
            print "To: " + self.email["To"]
            print "Subject: " + self.email["Subject"]
            print self.email.get_content_type()
            print self.email.get_payload(decode=True)
            print self.email.is_multipart()
            self.tipo = self.email.get_content_type()
            for codigo in self.codigos:
                if ("text/plain" == self.tipo) and (self.email.get_payload(decode=True).lower() == codigo):
                    # Si es texto plano, se escribe en pantalla
                    print self.email.get_payload(decode=True)
                    self.m.dele(i + 1)  # le sumamos 1 ya que python cuenta desde 0 y los emails desde 1
                    self.cerrar_conexion()  # nos desconectamos solo si borramos algo, y lo borramos solo si es un mensaje de "ok"
                    self.conectar()  # y de esta forma se guardan los archivos en el server
                    return codigo  # retorna el comando que se encontro en el e-mail
        return False  # retorna falso en caso de no encontrar nada
Exemplo n.º 33
0
def parse(content):
    """
    Parse the email and return a dictionary of relevant data.
    """
    p = EmailParser()
    msgobj = p.parsestr(content)
    if msgobj['Subject'] is not None:
        decodefrag = decode_header(msgobj['Subject'])
        subj_fragments = []
        for s, enc in decodefrag:
            if enc:
                s = unicode(s, enc).encode('utf8', 'replace')
            subj_fragments.append(s)
        subject = ''.join(subj_fragments)
    else:
        subject = None

    attachments = []
    body = None
    html = None
    for part in msgobj.walk():
        attachment = parse_attachment(part)
        if attachment:
            attachments.append(attachment)
        elif part.get_content_type() == "text/plain":
            if body is None:
                body = ""
            body += unicode(part.get_payload(decode=True),
                            part.get_content_charset(),
                            'replace').encode('utf8', 'replace')
        elif part.get_content_type() == "text/html":
            if html is None:
                html = ""
            html += unicode(part.get_payload(decode=True),
                            part.get_content_charset(),
                            'replace').encode('utf8', 'replace')
    return {
        'subject': subject,
        'body': body,
        'html': html,
        'from': parseaddr(msgobj.get(
            'From'))[1],  # Leave off the name and only return the address
        'to': parseaddr(msgobj.get('To'))
        [1],  # Leave off the name and only return the address
        'attachments': attachments,
    }
Exemplo n.º 34
0
    def __init__(self, env, raw_response):
        self.env = env
        self.boundary = None
        h, self.body = raw_response.split(Response.SEP + Response.SEP, 1)
        s, self.raw_headers = h.split(Response.SEP, 1)

        self.server_protocol, self.status = s.split(None, 1)
        self.headers = Parser().parsestr(self.raw_headers, headersonly=True)
Exemplo n.º 35
0
 def __init__(self, message):
     self._parser = Parser()
     self.requests = []
     self.responses = []
     if isinstance(message, basestring):
         self._parsestr(message)
     else:
         self._parse(message)
Exemplo n.º 36
0
    def __init__(self, user, password, host, port = 110, ssl = False):
        self.user = user
        self.password = password
        self.host = host
        self.port = int(port)
        self.ssl = ssl

        self.mbox = None
        self.parser = EmailParser()
Exemplo n.º 37
0
  def addMessage(self, msg, flags=None, date=None):
    #print "Add Message: %s :: %s" % (msg, flags)
    # passes a file handler here, need to cache fetchBodyFile so I can find the message id.
    #   list of uids
    #     uids[sequence_number] = msg_uid
    #   add new uid
    #     rpush[msg_uid]
    parse = Parser()
    email_obj = parse.parse(msg)
    fp = StringIO()
    g = Generator(fp, mangle_from_=False, maxheaderlen=60)
    g.flatten(email_obj)
    body = fp.getvalue()

    msg_uid = self.conn.incr("%s:mailboxes:%s:uidnext" % (self.user,
      self.folder)) - 1;
    
    self.conn.zadd("%s:mailboxes:%s:uidlist"% (self.user, self.folder), msg_uid, msg_uid)

    self.seqlist.append((msg_uid, ['\Recent']))
    seq_number = len(self.seqlist)

    if flags:
        self.conn.sadd("%s:mailboxes:%s:mail:%s:flags" % (self.user, self.folder,
          msg_uid), *flags)
    self.conn.set("%s:mailboxes:%s:mail:%s:date" % (self.user, self.folder,
      msg_uid), rfc822date())
    self.conn.incr("%s:mailboxes:%s:count" % (self.user, self.folder))
    self.conn.set("%s:mailboxes:%s:mail:%s:size" % (self.user, self.folder,
      msg_uid), len(body))
    self.conn.set("%s:mailboxes:%s:mail:%s:body" % (self.user, self.folder,
      msg_uid), body)
    self.conn.sadd("%s:mailboxes:%s:mail:%s:headers" % (self.user, self.folder,
      msg_uid), *(email_obj.keys()))
    #print email_obj.keys()
    for header in email_obj.keys():
      self.conn.set("%s:mailboxes:%s:mail:%s:header:%s" % (self.user,
        self.folder, msg_uid, header.lower()), email_obj[header])
      #print header, msg_uid, self.folder, self.user
    self.conn.incr("%s:mailboxes:%s:recent" % (self.user, self.folder))
    self.recent_count += 1
    self.conn.publish("%s:mailboxes:%s:channel" % (self.user, self.folder),
            "count %d" % (msg_uid))
    return defer.succeed(seq_number)
Exemplo n.º 38
0
def parse(content):
    """
    Eメールのコンテンツを受け取りparse,encodeして返す
    """
    p = EmailParser()
    msgobj = p.parse(content)
    if msgobj['Subject'] is not None:
        decodefrag = decode_header(msgobj['Subject'])
        subj_fragments = []
        for s, enc in decodefrag:
            if enc:
                s = unicode(s, enc).encode('utf8', 'replace')
            subj_fragments.append(s)
        subject = ''.join(subj_fragments)
    else:
        subject = None

    attachments = []
    body = None
    html = None
    for part in msgobj.walk():
        attachment = parse_attachment(part)
        if attachment:
            attachments.append(attachment)
        elif part.get_content_type() == "text/plain":
            if body is None:
                body = ""
            body += unicode(part.get_payload(decode=True),
                            part.get_content_charset(),
                            'replace').encode('utf8', 'replace')
        elif part.get_content_type() == "text/html":
            if html is None:
                html = ""
            html += unicode(part.get_payload(decode=True),
                            part.get_content_charset(),
                            'replace').encode('utf8', 'replace')
    return {
        'subject': subject,
        'body': body,
        'html': html,
        'from': parseaddr(msgobj.get('From'))[1],  # 名前は除いてメールアドレスのみ抽出
        'to': parseaddr(msgobj.get('To'))[1],  # 名前は除いてメールアドレスのみ抽出
        'attachments': attachments,
    }
Exemplo n.º 39
0
    def _checkModeratorMessage(self, messageText, sender, moderator, group, postingText):
        p = Parser()
        msg = p.parsestr(messageText)
        headers = dict(msg.items())
        del headers['Message-ID']
        self.assertEqual(
            headers,
            {'From': sender,
             'To': moderator,
             'Subject': 'Moderate new %s message: activities etc' % (group,),
             'Content-Type': 'message/rfc822'})

        posting = p.parsestr(postingText)
        attachment = msg.get_payload()[0]

        for header in ['from', 'to', 'subject', 'message-id', 'newsgroups']:
            self.assertEqual(posting[header], attachment[header])

        self.assertEqual(posting.get_payload(), attachment.get_payload())
Exemplo n.º 40
0
def message_from_file(fp, _class=None, strict=False):
    """Read a file and parse its contents into a Message object model.

    Optional _class and strict are passed to the Parser constructor.
    """
    from email.Parser import Parser
    if _class is None:
        from email.Message import Message
        _class = Message
    return Parser(_class, strict=strict).parse(fp)
Exemplo n.º 41
0
def message_from_string(s, _class=None, strict=False):
    """Parse a string into a Message object model.

    Optional _class and strict are passed to the Parser constructor.
    """
    from email.Parser import Parser
    if _class is None:
        from email.Message import Message
        _class = Message
    return Parser(_class, strict=strict).parsestr(s)
Exemplo n.º 42
0
 def test_bogus_boundary(self):
     fp = openfile('msg_15.txt')
     try:
         data = fp.read()
     finally:
         fp.close()
     p = Parser()
     # Note, under a future non-strict parsing mode, this would parse the
     # message into the intended message tree.
     self.assertRaises(Errors.BoundaryError, p.parsestr, data)
Exemplo n.º 43
0
    def _checkModeratorMessage(self, messageText, sender, moderator, group, postingText):
        p = Parser()
        msg = p.parsestr(messageText)
        headers = dict(msg.items())
        del headers['Message-ID']
        self.assertEquals(
            headers,
            {'From': sender,
             'To': moderator,
             'Subject': 'Moderate new %s message: activities etc' % (group,),
             'Content-Type': 'message/rfc822'})

        posting = p.parsestr(postingText)
        attachment = msg.get_payload()[0]

        for header in ['from', 'to', 'subject', 'message-id', 'newsgroups']:
            self.assertEquals(posting[header], attachment[header])

        self.assertEquals(posting.get_payload(), attachment.get_payload())
Exemplo n.º 44
0
    def test_create_multipart_string(self):
        files = []
        values = []
        for i in range(3):
            value = "value-%d" % i
            values.append(value)
            files.append(('plain', value))

        result = Util.create_multipart_string(files)

        with closing(StringIO(result)) as buf:
            parser = Parser()
            msg = parser.parse(buf)

        for part in msg.walk():
            if msg.is_multipart():
                i = 0
                for msg in part.get_payload():
                    self.assertEqual(values[i], msg.get_payload())
                    i += 1
Exemplo n.º 45
0
def parse(content):
    """Parse the content string of an email message and return a dict of the parts"""

    p = EmailParser()
    msgobj = p.parsestr(content)

    msgheaders = {}
    for header_key in header_parts:
        header_val = get_header_component (msgobj, header_key)
        if header_val and header_val != 'None':
            msgheaders[header_key] = header_val

    attachments = []
    body = None
    html = None
    for part in msgobj.walk():
        attachment = parse_attachment(part)
        if attachment:
            attachments.append(attachment)
        elif part.get_content_type() == "text/plain":
            if body is None:
                body = ""
            body += unicode(
                part.get_payload(decode=True),
                unicode_encoding if part.get_content_charset() is None else part.get_content_charset(),
                'replace'
            ).encode(unicode_encoding,'replace')
        elif part.get_content_type() == "text/html":
            if html is None:
                html = ""
            html += unicode(
                part.get_payload(decode=True),
                unicode_encoding if part.get_content_charset() is None else part.get_content_charset(),
                'replace'
            ).encode(unicode_encoding,'replace')
    return {
        'headers' : msgheaders,
        'body' : body,
        'html' : html,
        'attachments': attachments,
    }
Exemplo n.º 46
0
def parse(content):
    """Parse the content string of an email message and return a dict of the parts"""

    p = EmailParser()
    msgobj = p.parsestr(content)

    msgheaders = {}
    for header_key in header_parts:
        header_val = get_header_component(msgobj, header_key)
        if header_val and header_val != 'None':
            msgheaders[header_key] = header_val

    attachments = []
    body = None
    html = None
    for part in msgobj.walk():
        attachment = parse_attachment(part)
        if attachment:
            attachments.append(attachment)
        elif part.get_content_type() == "text/plain":
            if body is None:
                body = ""
            body += unicode(
                part.get_payload(decode=True),
                unicode_encoding if part.get_content_charset() is None else
                part.get_content_charset(),
                'replace').encode(unicode_encoding, 'replace')
        elif part.get_content_type() == "text/html":
            if html is None:
                html = ""
            html += unicode(
                part.get_payload(decode=True),
                unicode_encoding if part.get_content_charset() is None else
                part.get_content_charset(),
                'replace').encode(unicode_encoding, 'replace')
    return {
        'headers': msgheaders,
        'body': body,
        'html': html,
        'attachments': attachments,
    }
Exemplo n.º 47
0
    def _checkModeratorMessage(self, messageText, sender, moderator, group, postingText):
        p = Parser()
        msg = p.parsestr(messageText)
        headers = dict(msg.items())
        del headers["Message-ID"]
        self.assertEqual(
            headers,
            {
                "From": sender,
                "To": moderator,
                "Subject": "Moderate new %s message: activities etc" % (group,),
                "Content-Type": "message/rfc822",
            },
        )

        posting = p.parsestr(postingText)
        attachment = msg.get_payload()[0]

        for header in ["from", "to", "subject", "message-id", "newsgroups"]:
            self.assertEqual(posting[header], attachment[header])

        self.assertEqual(posting.get_payload(), attachment.get_payload())
Exemplo n.º 48
0
def decryptSmime(mlist, msg, msgdata):
    """Returns (encrypted (bool), signed (bool)), msg is replaced with
       decrypted msg"""

    # FIXME this implementation is _very_ crude.
    # merge some stuff with decryptGpg

    encrypted = False
    signed = False
    plaintext = None
    ciphertext = None

    if msg.get_content_type()=="application/x-pkcs7-mime":
        sm = SMIMEUtils.SMIMEHelper(mlist)
        ciphertext = msg.as_string()
        (plaintext, signed) = sm.decryptMessage(ciphertext)
    else:
        # don't touch the message if it's no S/MIME
        return (encrypted, signed)

    parser = Parser()
    tmpmsg = parser.parsestr(plaintext)

    msg.del_param("x-action")

    for i in ('Content-Type','Content-Disposition','Content-Transfer-Encoding'):
        if tmpmsg.has_key(i):
            if msg.has_key(i):
                msg.replace_header(i,tmpmsg.get(i))
            else:
                msg.add_header(i,tmpmsg.get(i))

    tmppayload = tmpmsg.get_payload()
    msg.set_payload(tmppayload)

    if encrypted:
        msg.add_header('X-Mailman-SLS-decrypted', 'Yes')

    return (encrypted, signed)
Exemplo n.º 49
0
 def actualizar(self):
     """Ontiene los datos del servidor"""
     print "llamada a actualizar"
     # Se obtiene el numero de mensajes pendientes y se hace un
     # bucle para cada mensaje
     self.numero = len(self.m.list()[1])
     for i in range(self.numero):
         #    print "Mensaje numero"+str(i+1)
         #    print "--------------------"
         # Se lee el mensaje
         self.response, self.headerLines, self.bytes = self.m.retr(i + 1)
         # Se mete todo el mensaje en un unico string
         self.mensaje = '\n'.join(self.headerLines)
         # Se parsea el mensaje
         self.p = Parser()
         self.email = self.p.parsestr(self.mensaje)
         # Se sacan por pantalla los campos from, to y subject
         print "From: " + self.email["From"]
         print "To: " + self.email["To"]
         print "Subject: " + self.email["Subject"]
         print self.email.get_content_type()
         print self.email.get_payload(decode=True)
         print self.email.is_multipart()
         self.tipo = self.email.get_content_type()
         for codigo in self.codigos:
             if ("text/plain" == self.tipo) and (self.email.get_payload(
                     decode=True).lower() == codigo):
                 # Si es texto plano, se escribe en pantalla
                 print self.email.get_payload(decode=True)
                 self.m.dele(
                     i + 1
                 )  #le sumamos 1 ya que python cuenta desde 0 y los emails desde 1
                 self.cerrar_conexion(
                 )  #nos desconectamos solo si borramos algo, y lo borramos solo si es un mensaje de "ok"
                 self.conectar(
                 )  #y de esta forma se guardan los archivos en el server
                 return codigo  #retorna el comando que se encontro en el e-mail
     return False  #retorna falso en caso de no encontrar nada
Exemplo n.º 50
0
def showindex(msgList):
    count = 0                                # show some mail headers
    for msgtext in msgList:
        msghdrs = Parser( ).parsestr(msgtext, headersonly=True)
        count   = count + 1
        print('%d:\t%d bytes' % (count, len(msgtext)))
        for hdr in ('From', 'Date', 'Subject'):
            try:
                print('\t%s=>%s' % (hdr, msghdrs[hdr]))
            except KeyError:
                print('\t%s=>(unknown)' % hdr)
            #print('\n\t%s=>%s' % (hdr, msghdrs.get(hdr, '(unknown)')
        if count % 5 == 0:
            raw_input('[Press Enter key]')  # pause after each 5
Exemplo n.º 51
0
    def savemessage(self, uid, content, flags, rtime):
        """Writes a new message, with the specified uid.

        See folder/Base for detail. Note that savemessage() does not
        check against dryrun settings, so you need to ensure that
        savemessage is never called in a dryrun mode."""
        # This function only ever saves to tmp/,
        # but it calls savemessageflags() to actually save to cur/ or new/.
        self.ui.savemessage('maildir', uid, flags, self)
        if uid < 0:
            # We cannot assign a new uid.
            return uid

        if uid in self.messagelist:
            # We already have it, just update flags.
            self.savemessageflags(uid, flags)
            return uid

        # Otherwise, save the message in tmp/ and then call savemessageflags()
        # to give it a permanent home.
        tmpdir = os.path.join(self.getfullname(), 'tmp')
        messagename = self.new_message_filename(uid, flags)
        tmpname = self.save_to_tmp_file(messagename, content)

        if self.utime_from_header:
            try:
                date = emailutil.get_message_date(content, 'Date')
                if date is not None:
                    os.utime(os.path.join(self.getfullname(), tmpname),
                        (date, date))
            # In case date is wrongly so far into the future as to be > max int32
            except Exception as e:
                from email.Parser import Parser
                from offlineimap.ui import getglobalui
                datestr = Parser().parsestr(content, True).get("Date")
                ui = getglobalui()
                ui.warn("UID %d has invalid date %s: %s\n"
                    "Not changing file modification time" % (uid, datestr, e))

        self.messagelist[uid] = self.msglist_item_initializer(uid)
        self.messagelist[uid]['flags'] = flags
        self.messagelist[uid]['filename'] = tmpname
        # savemessageflags moves msg to 'cur' or 'new' as appropriate
        self.savemessageflags(uid, flags)
        self.ui.debug('maildir', 'savemessage: returning uid %d' % uid)
        return uid
Exemplo n.º 52
0
class PopBox:
    def __init__(self, user, password, host, port = 110, ssl = False):
        self.user = user
        self.password = password
        self.host = host
        self.port = int(port)
        self.ssl = ssl

        self.mbox = None
        self.parser = EmailParser()

    def __connect(self):
        if not self.ssl:
            self.mbox = poplib.POP3(self.host, self.port)
        else:
            self.mbox = poplib.POP3_SSL(self.host, self.port)

        self.mbox.user(self.user)
        self.mbox.pass_(self.password)

    def get_mails(self):
        self.__connect()

        messages = []
        print "Starting reading POP messages"
        msgs = self.mbox.list()[1]
        print "POP messages readed: %i" % (len(msgs))
        for msg in msgs:
            msgNum = int(msg.split(" ")[0])
            msgSize = int(msg.split(" ")[1])

            # retrieve only the header
            st = "\n".join(self.mbox.top(msgNum, 0)[1])
            print st
            print "----------------------------------------"
            msg = self.parser.parsestr(st, True) # header only
            sub = utils.mime_decode(msg.get("Subject"))
            msgid = msg.get("Message-Id")
            if not msgid:
                msgid = hash(msg.get("Received") + sub)
            fr = utils.mime_decode(msg.get("From"))
            messages.append( [msgid, sub, fr] )

        self.mbox.quit()
        return messages
Exemplo n.º 53
0
def autorespond(msgList):
    for msgtext in msgList:
        msg = Parser( ).parsestr(msgtext, headersonly=True)
        if msg['Subject'].endswith('getpage') and (msg['From'] != mailuser):
            for msgLine in msgtext.split('\n'):
                if msgLine.startswith('http'):
                    print(msgLine)
                    html = ''
                    try:
                        oa = getpage(msgLine)
                    except Exception:
                        html = 'error=Can not connect to: ' + msgLine
                    if hasattr(oa, 'status'):
                        if oa.status != 200:
                            html = 'error=' + str(oa.status) + ' Can not connect to: ' + msgLine
                    if html == '':
                        html = oa.read()
                    send_email(mailuser, msg['From'], 'RE: ' + msg['Subject'], msgLine, html)
Exemplo n.º 54
0
class HTTPParser(object):
    def __init__(self, message):
        self._parser = Parser()
        self.requests = []
        self.responses = []
        if isinstance(message, basestring):
            self._parsestr(message)
        else:
            self._parse(message)

    def _parse_subrequest(self, subrequest):
        payload = subrequest.get_payload()
        if payload is None:
            raise ParserError("Missing payload in subrequest")

        content_type = subrequest.get("content-transfer-encoding", "").lower()
        if content_type == "quoted-printable":
            payload = quopri.decodestring(payload)
        elif content_type == "base64":
            payload = bdecode(payload)
        return payload

    def _parse(self, fp):
        msg = self._parser.parse(fp)
        for subrequest in msg.walk():
            type = subrequest.get_content_maintype()
            request_id = subrequest.get("multipart-request-id", None)
            if type == "multipart":
                continue  # walk will descend into child messages
            if type == "application":
                payload = self._parse_subrequest(subrequest)
                subtype = subrequest.get_content_subtype()
                if subtype == "http-request":
                    self.requests.append(HTTPRequest(payload, request_id=request_id))
                elif subtype == "http-response":
                    self.responses.append(HTTPResponse(payload))
                else:
                    raise ParserError("Unrecognized message type: '%s'" % subrequest.get_content_type())

    def _parsestr(self, text):
        self._parse(StringIO(text))
Exemplo n.º 55
0
def accept(game, locale, stream, extend=None):
    global rootdir, orderbase, gamedir, gamename, sender
    if extend is not None:
        orderbase = orderbase + ".pre-" + extend
    savedir = os.path.join(gamedir, orderbase)
    # check if it's one of the pre-sent orders.
    # create the save-directories if they don't exist
    if not os.path.exists(gamedir): os.mkdir(gamedir)
    if not os.path.exists(savedir): os.mkdir(savedir)
    # parse message
    message = Parser().parse(stream)
    email = get_sender(message)
    logger = logging.getLogger(email)
    # write syslog
    if email is None or valid_email(email) == 0:
        logger.warning("invalid email address: " + str(email))
        return -1
    logger.info("received orders from " + email)
    # get an available filename
    maxdate, filename = available_file(savedir, prefix + email)
    if filename is None:
        logger.warning("more than " + str(maxfiles) + " orders from " + email)
        return -1
    # copy the orders to the file

    turndate = None
    maildate = message.get("Date")
    if maildate is None:
        turndate = time.time()
    else:
        turndate = rfc822.mktime_tz(rfc822.parsedate_tz(maildate))

    text_ok = copy_orders(message, filename, email, turndate)
    warning, msg, fail = None, "", False
    if not maildate is None:
        os.utime(filename, (turndate, turndate))
        logger.debug("mail date is '%s' (%d)" % (maildate, turndate))
        if False and turndate < maxdate:
            logger.warning("inconsistent message date " + email)
            warning = " (" + messages["warning-" + locale] + ")"
            msg = msg + formatpar(
                messages["maildate-" + locale] %
                (time.ctime(maxdate), time.ctime(turndate)), 76, 2) + "\n"
    else:
        logger.warning("missing message date " + email)
        warning = " (" + messages["warning-" + locale] + ")"
        msg = msg + formatpar(messages["nodate-" + locale], 76, 2) + "\n"

    print('ACCEPT_MAIL=' + email)
    print('ACCEPT_FILE="' + filename + '"')

    if not text_ok:
        warning = " (" + messages["error-" + locale] + ")"
        msg = msg + formatpar(messages["multipart-" + locale], 76, 2) + "\n"
        logger.warning("rejected - no text/plain in orders from " + email)
        os.unlink(filename)
        savedir = savedir + "/rejected"
        if not os.path.exists(savedir): os.mkdir(savedir)
        maxdate, filename = available_file(savedir, prefix + email)
        store_message(message, filename)
        fail = True

    if sendmail and warning is not None:
        subject = gamename + " " + messages["subject-" + locale] + warning
        print("mail " + subject)
        ps = subprocess.Popen(['mutt', '-s', subject, email],
                              stdin=subprocess.PIPE)
        ps.communicate(msg)

    if not sendmail:
        print text_ok, fail, email
        print filename

    if not fail:
        queue = open(gamedir + "/orders.queue", "a")
        queue.write("email=%s file=%s locale=%s game=%s\n" %
                    (email, filename, locale, game))
        queue.close()

    logger.info("done - accepted orders from " + email)

    return 0
Exemplo n.º 56
0
def parse_email(email_as_string, include_headers, maintain_rfc,
                attach_message_primary):
    """
    This function parses an email and returns an array with different parts of the message.
    :param email_as_string: This represents the email in a bytearray to be processed
    :type email_as_string: basestring
    :param include_headers: This parameter specifies if all headers should be included.
    :type include_headers: bool
    :param maintain_rfc: This parameter specifies if RFC format for email stays intact
    :type maintain_rfc: bool
    :param attach_message_primary: This parameter specifies if first attached email should
      be used as the message for indexing instead of the carrier email
    :type attach_message_primary: bool
    :return: Returns a list with the [date, Message-id, mail_message]
      :rtype: list
    """
    message = email.message_from_string(email_as_string)
    if attach_message_primary:
        message = change_primary_message(message)
    if maintain_rfc:
        index_mail = maintain_rfc_parse(message)
    else:
        mailheaders = Parser().parsestr(message.as_string(), True)
        headers = [
            "%s: %s" % (k, getheader(v)) for k, v in mailheaders.items()
            if k in MAIN_HEADERS
        ]
        if include_headers:
            other_headers = [
                "%s: %s" % (k, getheader(v)) for k, v in mailheaders.items()
                if k not in MAIN_HEADERS
            ]
            headers.extend(other_headers)
        body = []
        if message.is_multipart():
            part_number = 1
            for part in message.walk():
                content_type = part.get_content_type()
                content_disposition = part.get('Content-Disposition')
                if content_type in [
                        'multipart/alternative', 'multipart/mixed'
                ]:
                    # The multipart/alternative part is usually empty.
                    body.append("Multipart envelope header: %s" %
                                str(part.get_payload(decode=True)))
                    continue
                body.append("#START_OF_MULTIPART_%d" % part_number)
                extension = str(
                    os.path.splitext(part.get_filename() or '')[1]).lower()
                if extension in TEXT_FILE_EXTENSIONS or content_type in SUPPORTED_CONTENT_TYPES or \
                   part.get_content_maintype() == 'text' or extension in ZIP_EXTENSIONS:
                    if part.get_filename():
                        body.append("#BEGIN_ATTACHMENT: %s" %
                                    str(part.get_filename()))
                        if extension in ZIP_EXTENSIONS:
                            body.append("\n".join(
                                zip.parse_zip(part, EMAIL_PART)))
                        else:
                            body.append(recode_mail(part))
                        body.append("#END_ATTACHMENT: %s" %
                                    str(part.get_filename()))
                    else:
                        body.append(recode_mail(part))
                else:
                    body.append(
                        "#UNSUPPORTED_ATTACHMENT: file_name = %s - type = %s ; disposition=%s"
                        % (part.get_filename(), content_type,
                           content_disposition))
                body.append("#END_OF_MULTIPART_%d" % part_number)
                part_number += 1
        else:
            body.append(recode_mail(message))
        """mail_for_index = [MESSAGE_PREAMBLE]"""
        mail_for_index = []
        mail_for_index.extend(headers + body)
        index_mail = "\n".join(mail_for_index)
    message_time = float(mktime_tz(parsedate_tz(message['Date'])))
    return [message_time, message['Message-ID'], index_mail]
Exemplo n.º 57
0
# Copyright (C) 2001 Python Software Foundation
Exemplo n.º 58
0
        return default
    line = fo.readline()
    return int(line)

# the main body of the script:
logging.basicConfig(filename=LOG_FILENAME, level=logging.INFO)
logger = logging

infile = sys.stdin
if len(sys.argv)>1:
    logger.info('reading message from %s' % sys.argv[1])
    infile = open(sys.argv[1], "r")
else:
    logger.info('reading message from stdin')

message = Parser().parse(infile)
replyto = message.get('Reply-To') or message.get('From')
sender = None if (replyto is None) else email.utils.parseaddr(replyto)[1]
if sender is None or valid_email(sender)==0:
    logger.error("invalid email address: " + str(sender))
    sys.exit(-1)
else:
    logger = logging.getLogger(sender)
    logger.info("received orders from " + sender)
subject = message.get('Subject')
m = re.match(r'.*ATLA\S*\s*ORDER\S*\s*GAME\s*(\d+)', subject, re.IGNORECASE)
if m is None:
    logger.error("invalid subject line: " + str(subject))
    sys.exit(-2)
game = m.expand(r'game-\1')
if not os.path.exists(game):