Ejemplo n.º 1
0
 def test_expand_uidset(self):
     self.assertEqual(expand_uidset('3'), ['3'])
     self.assertEqual(expand_uidset('3,5'), ['3','5'])
     self.assertEqual(expand_uidset('3:5'), ['3','4','5'])
Ejemplo n.º 2
0
    def _retrieve_contents_for_messages(self, notification, headers_only=False):
        """
            Helper method to deliver message contents/headers
        """
        messageContents = {}
        messageHeaders = {}

        # split the uri parameter into useful parts
        uri = parse_imap_uri(notification['uri'])

        if notification.has_key('uidset'):
            message_uids = expand_uidset(notification['uidset'])
        elif notification.has_key('vnd.cmu.oldUidset'):
            message_uids = expand_uidset(notification['vnd.cmu.oldUidset'])
        elif uri.has_key('UID'):
            message_uids = [ uri['UID'] ]
            notification['uidset'] = ','.join(message_uids)

        # resolve uri into a mailbox path on the local file stystem
        mailbox_path = utils.imap_mailbox_fs_path(uri)

        log.debug("Using mailbox path: %r" % (mailbox_path), level=8)

        # mailbox exists, try reading the message files
        if os.path.exists(mailbox_path):
            for message_uid in message_uids:
                # using message file path like /var/spool/imap/domain/e/example.org/k/lists/kolab^org/devel/lists/kolab.org/[email protected]/1.
                message_file_path = "%s/%s." % (mailbox_path, message_uid)

                log.debug("Open message file: %r" % (message_file_path), level=8)

                attempts = 5
                while attempts > 0:
                    attempts -= 1

                    if os.access(message_file_path, os.R_OK):
                        fp = open(message_file_path, 'r')

                        if headers_only:
                            data = ''
                            for line in fp:
                                data += line
                                if line.strip() == '':
                                    break;
                            data += "\r\n"
                        else:
                            data = fp.read()

                        fp.close()

                        # use email lib to parse message headers
                        try:
                            # find header delimiter
                            pos = data.find("\r\n\r\n")
                            message = message_from_string(data[0:pos])
                            headers = decode_message_headers(message)
                        except:
                            headers = dict()
                            messageHeaders[message_uid] = headers

                        # append raw message data and parsed headers
                        messageContents[message_uid] = data
                        messageHeaders[message_uid] = headers
                        break

                    elif attempts > 0:
                        log.debug("Failed to open message file %r; retry %d more times" % (
                            message_file_path, attempts
                        ), level=5)
                    else:
                        log.warning("Failed to open message file %r for uri=%s; uid=%s" % (
                            message_file_path, notification, message_uid
                        ))

                    time.sleep(1)
                # end while
            # end for
        else:
            log.warning("Mailbox path %r does not exits for uri=%s" % (mailbox_path, notification['uri']))

        return (messageContents, messageHeaders)