Пример #1
0
def extract(path):
    if config["attachments-enabled"] != "1":
        return

    print("Extracting attachments from %s..." % path, file = sys.stderr)

    mbox = mailbox.mbox(config["lists-base"] + "/" + path)

    for msg in mbox.keys():
        index = 0
        for part in mbox[msg].walk():
            fn = part.get_filename()
            typ = part.get_content_type()
            if fn is not None \
                    and not mailindex.decode(part.get("Content-Disposition", "inline")).startswith("inline") \
                    and typ not in \
                    ('application/pgp-signature', 'application/pkcs7-signature',
                     'application/x-pkcs7-signature', 'image/x-icon',
                     'message/external-body', 'message/rfc822', 'text/calendar',
                     'text/x-vcard'):

                p = config["attachments-base"] + "/" + path

                try:
                    fn = cleanfilename(fn)

                    if config["attachments-odponly"] != "1" or \
                            fn.lower().endswith(".odp") or \
                            typ.lower().startswith("application/vnd.oasis.opendocument.presentation"):
                        common.mkdirs(p)
                        p += "/%03u-%03u-%s" % (msg, index, fn)

                        if not os.path.exists(p):
                            temppath = common.mktemppath(p)
                        
                            f = open(temppath, "wb")
                            f.write(part.get_payload(decode = True))

                            f.flush()
                            os.fsync(f.fileno())
                            f.close()
                
                            common.rename(temppath, p)
                            common.mkro(p)

                except UnicodeEncodeError:
                    pass

            index += 1
Пример #2
0
def cleanfilename(fn):
    fn = mailindex.decode(fn)
    fn = fn.replace("\n", "_")
    fn = fn.replace("\t", "_")
    fn = fn.replace("/", "_")
    return fn