Esempio 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)
Esempio n. 2
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()
Esempio n. 3
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))
Esempio n. 4
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
Esempio n. 5
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()
Esempio n. 6
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()
Esempio n. 7
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]
Esempio n. 8
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()
Esempio n. 9
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
Esempio n. 10
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()
Esempio n. 11
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
Esempio n. 12
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)
     
     
def message_factory(stream: BinaryIO) -> MaildirMessage:
    message = message_from_binary_file(fp=stream, policy=policy.default)
    return MaildirMessage(message)
Esempio n. 14
0
def format_message(nm_msg, mid):
    fn = list(nm_msg.filenames())[0]
    msg = MaildirMessage(open(fn))
    return format_message_walk(msg, mid)