Exemplo n.º 1
0
    def test_import_one_message_w_unicode_data(self):
        from mailbox import MaildirMessage
        import os
        log = DummyLogger()
        fqn = os.path.join(os.path.dirname(__file__), 'borked_encoding.email')
        with open(fqn) as f:
            msg1 = MaildirMessage(f)
        msg1.replace_header('To', '*****@*****.**')

        queues = {}

        po = self._make_one(StringIO(
            "[post office]\n"
            "zodb_uri = filestorage:test.db\n"
            "maildir = test/Maildir\n"
            "max_message_size = 5mb\n"
            "[queue:A]\n"
            "filters =\n"
            "\tto_hostname:exampleA.com\n"
            ),
            queues=queues,
            messages=[msg1]
            )
        po.MaildirMessage = MaildirMessage
        po.reconcile_queues()
        po.import_messages(log)

        self.assertEqual(len(self.messages), 0)
        A = queues['A']
        self.assertEqual(len(A), 1)
        queued = A.pop_next()
        self.assertEqual(queued.get('Message-ID'), msg1.get('Message-ID'))
        self.assertEqual(len(log.infos), 2)
Exemplo n.º 2
0
 def __init__(self, body=None):
     if isinstance(body, file):
         MaildirMessage.__init__(self, body)
     else:
         MaildirMessage.__init__(self)
         self.set_payload(body)
         self["From"] = "Woody Woodpecker <*****@*****.**>"
         self["Subject"] = "Double date tonight"
         self["Message-Id"] = "12389jdfkj98"
Exemplo n.º 3
0
 def __init__(self, body=None):
     if isinstance(body, file):
         MaildirMessage.__init__(self, body)
     else:
         MaildirMessage.__init__(self)
         self.set_payload(body)
         self['From'] = 'Woody Woodpecker <*****@*****.**>'
         self['Subject'] = 'Double date tonight'
         self['Message-Id'] = '12389jdfkj98'
Exemplo n.º 4
0
 def from_maildir(cls, uid: int, maildir_msg: MaildirMessage,
                  maildir: Maildir, key: str,
                  email_id: Optional[ObjectId],
                  thread_id: Optional[ObjectId],
                  maildir_flags: MaildirFlags) -> Message:
     flag_set = maildir_flags.from_maildir(maildir_msg.get_flags())
     recent = maildir_msg.get_subdir() == 'new'
     msg_dt = datetime.fromtimestamp(maildir_msg.get_date())
     return cls(uid, msg_dt, flag_set,
                email_id=email_id, thread_id=thread_id,
                recent=recent, maildir=maildir, key=key)
Exemplo n.º 5
0
 def from_maildir(cls, uid: int, maildir_msg: MaildirMessage,
                  maildir_flags: 'MaildirFlags',
                  metadata_only: bool) -> 'Message':
     flag_set = maildir_flags.from_maildir(maildir_msg.get_flags())
     recent = maildir_msg.get_subdir() == 'new'
     msg_dt = datetime.fromtimestamp(maildir_msg.get_date())
     if metadata_only:
         return cls(uid, flag_set, msg_dt, recent=recent,
                    maildir_flags=maildir_flags)
     else:
         msg_data = bytes(maildir_msg)
         return cls.parse(uid, msg_data, flag_set, msg_dt, recent=recent,
                          maildir_flags=maildir_flags)
Exemplo n.º 6
0
    async def encryptMessage(self, message):
        from io import BytesIO
        try:
            body = bytes(message)

            encrypted = await self.rsaExec.encryptData(BytesIO(body),
                                                       self.pubKey)
            payload = base64.b64encode(encrypted).decode()
        except Exception as e:
            raise e
        else:
            eMessage = MaildirMessage()
            eMessage['From'] = '*****@*****.**'
            eMessage.set_payload(payload)
            return eMessage
Exemplo n.º 7
0
def store_message(maildir, state, uid, message, flags):
    msg = MaildirMessage(message)
    if '\\Seen' in flags:
        msg.add_flag('S')

    flags = msg.get_flags()

    uid = int(uid)
    s = state.get(uid)
    if s:
        if s.flags != flags:
            maildir.set_flags(s.msgkey, flags)
    else:
        key = maildir.add(msg, flags)
        state.put(uid, key, flags)
Exemplo n.º 8
0
def newfile(event):
    fd = open(event.pathname, 'r')
    mail = MaildirMessage(message=fd)
    subject_header = dec_header(mail['Subject'])
    from_header = dec_header(mail['From']).replace("<", "&lt;").replace(
        ">", "&gt;")
    to_header = dec_header(mail['To'])

    message = "%s\n<i>%s</i>" % (from_header, subject_header)

    print("event: new message from '%s', sending notification" % from_header)

    if (mail['Delivered-To'] == None) or (mail['Delivered-To'] == ""):
        subject = to_header
    else:
        subject = dec_header(mail['Delivered-To'])

    subject = subject.replace("<", "").replace(">", "")
    subject = (subject[:100] + '..') if len(subject) > 100 else subject

    n = Notify.Notification.new(subject, message, "mail-unread-new")

    fd.close()
    n.set_timeout(notification_timeout)
    n.show()
Exemplo n.º 9
0
def process(msg: mailbox.MaildirMessage) -> Item:
    sender, address = parseaddr(msg["From"])
    date = datetime.datetime.utcfromtimestamp(
        msg.get_date()).isoformat()  # ISO 8601 format
    subject = msg["Subject"]
    content = ""
    for part in msg.walk():
        if part.get_content_type() == "text/plain" or part.get_content_type(
        ) == "text/html":
            content += part.get_payload(decode=True).decode('utf-8')
    print(sender)
    print(address)
    print(date)
    print(subject)
    print(content)
    return Item(sender, address, date, subject, content)
Exemplo n.º 10
0
def add_file(go, path):
    if not go:
        print(f"Would add {path}")
        return

    # Open the mailbox
    md = Maildir(DEST.joinpath(DEST_FOLDER), create=False)

    # Determine the UID for the next message
    uid_re = re.compile(",U=([0-9]+),")
    uid = 1 + max(
        chain([0],
              map(lambda key: int(uid_re.search(key).groups()[0]), md.keys())))

    print(f"Next UID: {uid}")

    # Determine the time when the message was originally delivered
    msg_bytes = path.read_bytes()
    msg = MaildirMessage(msg_bytes)
    msg_time = time.mktime(parsedate(msg["Date"]))

    # Create a Maildir filename in the format used by OfflineIMAP
    key = (f"{int(msg_time)}_0.{os.getpid()}.{socket.gethostname()},U={uid},"
           f"FMD5={md5(DEST_FOLDER.encode()).hexdigest()}:2,S")

    # Complete path
    dest = DEST.joinpath(DEST_FOLDER, "cur", key)
    assert not dest.exists() and dest.parent.exists()

    # Write the file
    print(f"Write {key}")
    dest.write_bytes(msg_bytes)

    # Update the utime
    os.utime(dest, (msg_time, msg_time))
Exemplo n.º 11
0
    def update_metadata(self, key: str, msg: MaildirMessage) -> None:
        """Uses :func:`os.rename` to atomically update the message filename
        based on :meth:`~mailbox.MaildirMessage.get_info`.

        """
        subpath = self._lookup(key)
        subdir, name = os.path.split(subpath)
        new_subdir = msg.get_subdir()
        new_name = key + self.colon + msg.get_info()
        if subdir != new_subdir:
            raise ValueError('Message subdir may not be updated')
        elif name != new_name:
            new_subpath = os.path.join(msg.get_subdir(), new_name)
            old_path = os.path.join(self._path, subpath)
            new_path = os.path.join(self._path, new_subpath)
            os.rename(old_path, new_path)
            self._toc[key] = new_subpath
Exemplo n.º 12
0
    def update_metadata(self, key: str, msg: MaildirMessage) -> None:
        """Uses :func:`os.rename` to atomically update the message filename
        based on :meth:`~mailbox.MaildirMessage.get_info`.

        """
        subpath = self._lookup(key)
        subdir, name = os.path.split(subpath)
        new_subdir = msg.get_subdir()
        new_name = key + self.colon + msg.get_info()
        if subdir != new_subdir:
            raise ValueError('Message subdir may not be updated')
        elif name != new_name:
            new_subpath = os.path.join(msg.get_subdir(), new_name)
            old_path = os.path.join(self._path, subpath)
            new_path = os.path.join(self._path, new_subpath)
            os.rename(old_path, new_path)
            self._toc[key] = new_subpath
Exemplo n.º 13
0
 def to_maildir(cls, prepared_msg: PreparedMessage, recent: bool,
                maildir_flags: MaildirFlags) -> MaildirMessage:
     flag_str = maildir_flags.to_maildir(prepared_msg.flag_set)
     when = prepared_msg.when or datetime.now()
     literal: bytes = prepared_msg.ref
     maildir_msg = MaildirMessage(literal)
     maildir_msg.set_flags(flag_str)
     maildir_msg.set_subdir('new' if recent else 'cur')
     maildir_msg.set_date(when.timestamp())
     return maildir_msg
Exemplo n.º 14
0
def new_mail(event):
    with open(event.pathname, 'r') as f:
        mail = MaildirMessage(message=f)
        efrom = 'From: ' + mail['From']
        esubject = 'Subject: ' + mail['Subject']
        n = pynotify.Notification(
            "New mail in " + '/'.join(event.path.split('/')[-3:-1]),
            efrom + "\n" + esubject)
        n.set_timeout(8000)
        n.show()
Exemplo n.º 15
0
def parse_div(soup):
    "Parses a message div (class blockpost) and return a MaildirMessage"
    print soup['id']
    msg = MaildirMessage()

    def encode(data):
        "Create a header encoded as UTF-8"
        return Header(data, 'UTF-8')

    msg['Date'] = find_date(soup).strftime("%a, %d %b %Y %H:%M:%S %z")
    msg['From'] = encode('%s <nobody@localhost>' % soup.find('strong').text)
    msg['Subject'] = encode(soup.find('h3').text)

    body = soup.find('div', 'postmsg')
    body = html2text(str(body).decode('utf-8'))

    msg.set_payload(body, 'UTF-8')

    return msg
Exemplo n.º 16
0
 def maildir_msg(self) -> MaildirMessage:
     flag_str = self.maildir_flags.to_maildir(self.permanent_flags)
     msg_bytes = bytes(self.get_body(binary=True))
     maildir_msg = MaildirMessage(msg_bytes)
     maildir_msg.set_flags(flag_str)
     maildir_msg.set_subdir('new' if self.recent else 'cur')
     if self.internal_date is not None:
         maildir_msg.set_date(self.internal_date.timestamp())
     return maildir_msg
Exemplo n.º 17
0
    def get_message_metadata(self, key: str) -> MaildirMessage:
        """Like :meth:`~mailbox.Maildir.get_message` but the message contents
        are not read from disk.

        """
        msg = MaildirMessage()
        subpath = self._lookup(key)
        subdir, name = os.path.split(subpath)
        msg.set_subdir(subdir)
        if self.colon in name:
            msg.set_info(name.rsplit(self.colon, 1)[-1])
        msg.set_date(os.path.getmtime(os.path.join(self._path, subpath)))
        return msg
Exemplo n.º 18
0
def store_mail(tf, mail, maildir, metadata):
    """Store the given mail inside the maildir with its metadata"""
    # Retrieve the message
    msg = MaildirMessage(tf.extractfile(mail['name']))
    msg.set_flags(get_msgflags(metadata[mail['name']]))
    folder = path.dirname(mail['name'])
    # Find the mailbox in which to store the mail
    md = None
    if re.match('Inbox(\![0-9]+)?$', folder):
        md = Maildir(maildir)
    elif re.match('Inbox/', folder):
        # Nested folders under Inbox, put them under a folder named
        # 'INBOX'.
        folder = folder.replace('/', '.').replace('Inbox', 'INBOX')
        md = Maildir(path.join(maildir, '.' + folder), factory=None)
    elif re.match('Sent(\ Items.*)?', folder):
        md = Maildir(path.join(maildir, '.' + 'Sent'), factory=None)
    else:
        md = Maildir(path.join(maildir, '.' + folder), factory=None)
    # Store the mail
    md.add(msg)
Exemplo n.º 19
0
def newfile(event):
    fd = open(event.pathname, 'r')
    mail = MaildirMessage(message=fd)
    From = "From:" + dec_header(mail['From'])
    Subject = "Subject:" + dec_header(mail['Subject'])
    fd.close()
    try:
        f = open(fnotify, 'a')
        f.write('mutt' + ' ' + From + ' ' + Subject + '\n')
        f.close
    except:
        print "Unexpected error:", sys.exc_info()[0]
Exemplo n.º 20
0
def newfile(event):
    fd = open(event.pathname, 'r')
    mail = MaildirMessage(message=fd)
    From = "[From]: " + dec_header(mail['From'])
    Subject = "[Subject]: " + dec_header(mail['Subject'])
    n = pynotify.Notification(
        "New mail in " + '/'.join(event.path.split('/')[-3:-1]),
        From + "\n" + Subject)
    fd.close()
    n.set_icon_from_pixbuf(icon)
    n.set_timeout(12000)
    n.show()
Exemplo n.º 21
0
def newfile(event):
    want_payload = 0

    def get_text(msg):
        text = ""
        if msg.is_multipart():
            html = None
            for part in msg.get_payload():
                if part.get_content_charset() is None:
                    charset = chardet.detect(str(part))['encoding']
                else:
                    charset = part.get_content_charset()
                if part.get_content_type() == 'text/plain':
                    text = decode_str(
                        ' '.join(part.get_payload().split('\n')) + "\n")
                if part.get_content_type() == 'text/html':
                    html = str(part.get_payload(decode=True), str(charset),
                               "ignore")
            if html is None:
                return text.strip()
            else:
                msg_data = lxml.html.document_fromstring(html.strip())
                return str("\n".join(etree.XPath("//text()")(msg_data)))
        elif part.get_content_type() == 'text/plain':
            text = decode_str(' '.join(part.get_payload().split('\n')) + "\n")
            ret = "\n".join(
                [ll.rstrip() for ll in text.splitlines() if ll.strip()])
            # text = str(msg.get_payload(decode=True),msg.get_content_charset(),'ignore')
            return ret.strip()

    def decode_str(string):
        return str(make_header(decode_header(string)))

    def decode_field(field):
        return decode_str(mail[field])

    fd = open(event.pathname, 'r')
    mail = MaildirMessage(message=fd)
    From = "[From]: " + decode_field('From')
    Subject = "[Subject]: " + decode_field('Subject')
    Date = "[Date]: " + decode_field('Date')
    Payload = ""
    if want_payload:
        Payload = "[Text]: " + get_text(mail)[0:2]
    n = notify2.Notification(
        "New mail in " + '/'.join(event.path.split('/')[-3:-1]),
        From + "\n" + Subject + Payload + "\n" + Date)
    fd.close()

    n.set_icon_from_pixbuf(icon)
    n.set_timeout(12000)
    n.show()
Exemplo n.º 22
0
    def notify(self, event):
        # Handling a new mail

        fd = open(event.pathname, 'r')
        mail = MaildirMessage(message=fd)
        From = "From: " + self.dec_header(mail['From'])
        Subject = "Subject: " + self.dec_header(mail['Subject'])
        n = pynotify.Notification(
            "New mail in " + '/'.join(event.path.split('/')[-3:-1]),
            From + "\n" + Subject)
        fd.close()
        n.set_icon_from_pixbuf(unread_icon_pixbuf)
        n.set_timeout(notification_timeout)
        n.show()
Exemplo n.º 23
0
    def get_message_metadata(self, key: str) -> MaildirMessage:
        """Like :meth:`~mailbox.Maildir.get_message` but the message contents
        are not read from disk.

        """
        msg = MaildirMessage()
        subpath = self._lookup(key)
        subdir, name = os.path.split(subpath)
        msg.set_subdir(subdir)
        if self.colon in name:
            msg.set_info(name.rsplit(self.colon, 1)[-1])
        msg.set_date(os.path.getmtime(os.path.join(self._path, subpath)))
        return msg
Exemplo n.º 24
0
 def populateMailbox(self, username):
     # Here will the mail directory of the user be stored
     pathShort = "/var/vmail/" + self.domain + "/" + username
     pathLong = pathShort + "/Maildir"
     # Create those directories
     os.mkdir(pathShort)
     mailbox = Maildir(pathLong)
     # And now populate that directory
     numOfMails = random.randint(1, 6666)
     percentageRead = random.randint(0, 100)
     for i in xrange(1, numOfMails):
         message = MaildirMessage(message=str(i))
         message.set_subdir("cur")
         readSeed = random.randint(0, 100)
         if percentageRead <= readSeed:
             message.set_flags("S")
         mailbox.add(message)
     self.chmodMailbox(pathShort)
     
     
Exemplo n.º 25
0
 def __getitem__(self, key):
     path, info = self.toc[key]
     msg = MaildirMessage(open(path).read())
     msg.set_flags(parse_info(info))
     msg.msgkey = key
     return msg
Exemplo n.º 26
0
def format_message(nm_msg, mid):
    fn = list(nm_msg.filenames())[0]
    msg = MaildirMessage(open(fn))
    return format_message_walk(msg, mid)
def message_factory(stream: BinaryIO) -> MaildirMessage:
    message = message_from_binary_file(fp=stream, policy=policy.default)
    return MaildirMessage(message)
Exemplo n.º 28
0
 def parse_message(cls, local_message_id: str,
                   message: MaildirMessage) -> Message:
     obj = mailparser.parse_from_string(message.as_string())
     return Message.from_mailparser(local_message_id=local_message_id,
                                    obj=obj)
Exemplo n.º 29
0
 def __getitem__(self, name):
     value = MaildirMessage.__getitem__(self, name)
     return decode_header(value)
Exemplo n.º 30
0
 def __setitem__(self, name, value):
     MaildirMessage.__setitem__(self, name, encode_header(name, value))