disposable-email/disposable-email.py

454 lines
16 KiB
Python
Raw Normal View History

2013-06-05 13:47:16 +00:00
#!/usr/bin/env python
from __future__ import with_statement
from smtpd import SMTPServer
import asyncore
from email import message_from_string
import os
from datetime import datetime
import smtplib
#ripped out of mimetypes.MimeTypes().types_map_inv
exts = {
'application/andrew-inset': '.ez',
'application/annodex': '.anx',
'application/atomcat+xml': '.atomcat',
'application/atomserv+xml': '.atomsrv',
'application/atom+xml': '.atom',
'application/bbolin': '.lin',
'application/cap': '.cap',
'application/cu-seeme': '.cu',
'application/davmount+xml': '.davmount',
'application/dsptype': '.tsp',
'application/ecmascript': '.es',
'application/hta': '.hta',
'application/java-archive': '.jar',
'application/javascript': '.js',
'application/java-serialized-object': '.ser',
'application/java-vm': '.class',
'application/m3g': '.m3g',
'application/mac-binhex40': '.hqx',
'application/mathematica': '.nb',
'application/msaccess': '.mdb',
'application/msword': '.doc',
'application/mxf': '.mxf',
'application/octet-stream': '.bin',
'application/oda': '.oda',
'application/ogg': '.ogx',
'application/pdf': '.pdf',
'application/pgp-keys': '.key',
'application/pgp-signature': '.pgp',
'application/pics-rules': '.prf',
'application/pkcs7-mime': '.p7c',
'application/postscript': '.ps',
'application/rar': '.rar',
'application/rdf+xml': '.rdf',
'application/rss+xml': '.rss',
'application/rtf': '.rtf',
'application/rtf': '.rtf',
'application/smil': '.smil',
'application/vnd.android.package-archive': '.apk',
'application/vnd.cinderella': '.cdy',
'application/vnd.google-earth.kml+xml': '.kml',
'application/vnd.google-earth.kmz': '.kmz',
'application/vnd.mozilla.xul+xml': '.xul',
'application/vnd.ms-excel': '.xls',
'application/vnd.ms-pki.seccat': '.cat',
'application/vnd.ms-pki.stl': '.stl',
'application/vnd.ms-powerpoint': '.ppt',
'application/vnd.oasis.opendocument.chart': '.odc',
'application/vnd.oasis.opendocument.database': '.odb',
'application/vnd.oasis.opendocument.formula': '.odf',
'application/vnd.oasis.opendocument.graphics': '.odg',
'application/vnd.oasis.opendocument.graphics-template': '.otg',
'application/vnd.oasis.opendocument.image': '.odi',
'application/vnd.oasis.opendocument.presentation': '.odp',
'application/vnd.oasis.opendocument.presentation-template': '.otp',
'application/vnd.oasis.opendocument.spreadsheet': '.ods',
'application/vnd.oasis.opendocument.spreadsheet-template': '.ots',
'application/vnd.oasis.opendocument.text-master': '.odm',
'application/vnd.oasis.opendocument.text': '.odt',
'application/vnd.oasis.opendocument.text-template': '.ott',
'application/vnd.oasis.opendocument.text-web': '.oth',
'application/vnd.openxmlformats-officedocument.presentationml.presentation': '.pptx',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow': '.ppsx',
'application/vnd.openxmlformats-officedocument.presentationml.template': '.potx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': '.xlsx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template': '.xltx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': '.docx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template': '.dotx',
'application/vnd.rim.cod': '.cod',
'application/vnd.smaf': '.mmf',
'application/vnd.stardivision.calc': '.sdc',
'application/vnd.stardivision.chart': '.sds',
'application/vnd.stardivision.draw': '.sda',
'application/vnd.stardivision.impress': '.sdd',
'application/vnd.stardivision.writer-global': '.sgl',
'application/vnd.stardivision.writer': '.sdw',
'application/vnd.sun.xml.calc': '.sxc',
'application/vnd.sun.xml.calc.template': '.stc',
'application/vnd.sun.xml.draw': '.sxd',
'application/vnd.sun.xml.draw.template': '.std',
'application/vnd.sun.xml.impress': '.sxi',
'application/vnd.sun.xml.impress.template': '.sti',
'application/vnd.sun.xml.math': '.sxm',
'application/vnd.sun.xml.writer.global': '.sxg',
'application/vnd.sun.xml.writer': '.sxw',
'application/vnd.sun.xml.writer.template': '.stw',
'application/vnd.symbian.install': '.sis',
'application/vnd.visio': '.vsd',
'application/vnd.wap.wbxml': '.wbxml',
'application/vnd.wap.wmlc': '.wmlc',
'application/vnd.wap.wmlscriptc': '.wmlsc',
'application/vnd.wordperfect5.1': '.wp5',
'application/vnd.wordperfect': '.wpd',
'application/x-123': '.wk',
'application/x-7z-compressed': '.7z',
'application/x-abiword': '.abw',
'application/x-apple-diskimage': '.dmg',
'application/x-bcpio': '.bcpio',
'application/x-bittorrent': '.torrent',
'application/x-cab': '.cab',
'application/x-cbr': '.cbr',
'application/x-cbz': '.cbz',
'application/x-cdf': '.cdf',
'application/x-cdlink': '.vcd',
'application/x-chess-pgn': '.pgn',
'application/x-cpio': '.cpio',
'application/x-debian-package': '.deb',
'application/x-dms': '.dms',
'application/x-doom': '.wad',
'application/x-dvi': '.dvi',
'application/x-font': '.gsf',
'application/x-freemind': '.mm',
'application/x-futuresplash': '.spl',
'application/x-gnumeric': '.gnumeric',
'application/x-go-sgf': '.sgf',
'application/x-graphing-calculator': '.gcf',
'application/x-gtar': '.tgz',
'application/x-hdf': '.hdf',
'application/xhtml+xml': '.xhtml',
'application/x-httpd-eruby': '.rhtml',
'application/x-httpd-php3': '.php3',
'application/x-httpd-php3-preprocessed': '.php3p',
'application/x-httpd-php4': '.php4',
'application/x-httpd-php5': '.php5',
'application/x-httpd-php': '.php',
'application/x-httpd-php-source': '.phps',
'application/x-ica': '.ica',
'application/x-info': '.info',
'application/x-internet-signup': '.ins',
'application/x-iphone': '.iii',
'application/x-iso9660-image': '.iso',
'application/x-jam': '.jam',
'application/x-java-jnlp-file': '.jnlp',
'application/x-jmol': '.jmz',
'application/x-kchart': '.chrt',
'application/x-killustrator': '.kil',
'application/x-koan': '.skm',
'application/x-kpresenter': '.kpr',
'application/x-kspread': '.ksp',
'application/x-kword': '.kwd',
'application/x-latex': '.latex',
'application/x-lha': '.lha',
'application/x-lyx': '.lyx',
'application/x-lzh': '.lzh',
'application/x-lzx': '.lzx',
'application/x-maker': '.fm',
'application/x-mif': '.mif',
'application/xml': '.xml',
'application/x-msdos-program': '.exe',
'application/x-msi': '.msi',
'application/x-ms-wmd': '.wmd',
'application/x-ms-wmz': '.wmz',
'application/x-netcdf': '.nc',
'application/x-ns-proxy-autoconfig': '.pac',
'application/x-nwc': '.nwc',
'application/x-object': '.o',
'application/x-oz-application': '.oza',
'application/x-pkcs12': '.p12',
'application/x-pkcs7-certreqresp': '.p7r',
'application/x-pkcs7-crl': '.crl',
'application/x-python-code': '.pyc',
'application/x-qgis': '.qgs',
'application/x-quicktimeplayer': '.qtl',
'application/x-redhat-package-manager': '.rpm',
'application/x-ruby': '.rb',
'application/x-shar': '.shar',
'application/x-shockwave-flash': '.swf',
'application/x-silverlight': '.scr',
'application/xspf+xml': '.xspf',
'application/x-stuffit': '.sit',
'application/x-sv4cpio': '.sv4cpio',
'application/x-sv4crc': '.sv4crc',
'application/x-tar': '.tar',
'application/x-tex-gf': '.gf',
'application/x-texinfo': '.texi',
'application/x-tex-pk': '.pk',
'application/x-trash': '.bak',
'application/x-troff-man': '.man',
'application/x-troff-me': '.me',
'application/x-troff-ms': '.ms',
'application/x-troff': '.tr',
'application/x-ustar': '.ustar',
'application/x-wais-source': '.src',
'application/x-wingz': '.wz',
'application/x-x509-ca-cert': '.crt',
'application/x-xcf': '.xcf',
'application/x-xfig': '.fig',
'application/x-xpinstall': '.xpi',
'application/zip': '.zip',
'audio/amr': '.amr',
'audio/amr-wb': '.awb',
'audio/annodex': '.axa',
'audio/basic': '.snd',
'audio/flac': '.flac',
'audio/midi': '.mid',
'audio/midi': '.mid',
'audio/mpeg': '.mp3',
'audio/ogg': '.ogg',
'audio/prs.sid': '.sid',
'audio/x-aiff': '.aif',
'audio/x-gsm': '.gsm',
'audio/x-mpegurl': '.m3u',
'audio/x-ms-wax': '.wax',
'audio/x-ms-wma': '.wma',
'audio/x-pn-realaudio': '.rm',
'audio/x-realaudio': '.ra',
'audio/x-scpls': '.pls',
'audio/x-sd2': '.sd2',
'audio/x-wav': '.wav',
'chemical/x-alchemy': '.alc',
'chemical/x-cache': '.cac',
'chemical/x-cache-csf': '.csf',
'chemical/x-cactvs-binary': '.cbin',
'chemical/x-cdx': '.cdx',
'chemical/x-cerius': '.cer',
'chemical/x-chem3d': '.c3d',
'chemical/x-chemdraw': '.chm',
'chemical/x-cif': '.cif',
'chemical/x-cmdf': '.cmdf',
'chemical/x-cml': '.cml',
'chemical/x-compass': '.cpa',
'chemical/x-crossfire': '.bsd',
'chemical/x-csml': '.csm',
'chemical/x-ctx': '.ctx',
'chemical/x-cxf': '.cxf',
'chemical/x-embl-dl-nucleotide': '.embl',
'chemical/x-galactic-spc': '.spc',
'chemical/x-gamess-input': '.gam',
'chemical/x-gaussian-checkpoint': '.fch',
'chemical/x-gaussian-cube': '.cub',
'chemical/x-gaussian-input': '.gau',
'chemical/x-gaussian-log': '.gal',
'chemical/x-gcg8-sequence': '.gcg',
'chemical/x-genbank': '.gen',
'chemical/x-hin': '.hin',
'chemical/x-isostar': '.istr',
'chemical/x-jcamp-dx': '.jdx',
'chemical/x-kinemage': '.kin',
'chemical/x-macmolecule': '.mcm',
'chemical/x-macromodel-input': '.mmd',
'chemical/x-mdl-molfile': '.mol',
'chemical/x-mdl-rdfile': '.rd',
'chemical/x-mdl-rxnfile': '.rxn',
'chemical/x-mdl-sdfile': '.sdf',
'chemical/x-mdl-tgf': '.tgf',
'chemical/x-mmcif': '.mcif',
'chemical/x-mol2': '.mol2',
'chemical/x-molconn-Z': '.b',
'chemical/x-mopac-graph': '.gpt',
'chemical/x-mopac-input': '.mop',
'chemical/x-mopac-out': '.moo',
'chemical/x-mopac-vib': '.mvb',
'chemical/x-ncbi-asn1-ascii': '.prt',
'chemical/x-ncbi-asn1-binary': '.aso',
'chemical/x-ncbi-asn1-spec': '.asn',
'chemical/x-pdb': '.pdb',
'chemical/x-rosdal': '.ros',
'chemical/x-swissprot': '.sw',
'chemical/x-vamas-iso14976': '.vms',
'chemical/x-vmd': '.vmd',
'chemical/x-xtel': '.xtel',
'chemical/x-xyz': '.xyz',
'image/gif': '.gif',
'image/ief': '.ief',
'image/jpeg': '.jpg',
'image/jpg': '.jpg',
'image/pcx': '.pcx',
'image/pict': '.pic',
'image/png': '.png',
'image/svg+xml': '.svg',
'image/tiff': '.tiff',
'image/vnd.djvu': '.djv',
'image/vnd.wap.wbmp': '.wbmp',
'image/x-canon-cr2': '.cr2',
'image/x-canon-crw': '.crw',
'image/x-cmu-raster': '.ras',
'image/x-coreldraw': '.cdr',
'image/x-coreldrawpattern': '.pat',
'image/x-coreldrawtemplate': '.cdt',
'image/x-corelphotopaint': '.cpt',
'image/x-epson-erf': '.erf',
'image/x-icon': '.ico',
'image/x-jg': '.art',
'image/x-jng': '.jng',
'image/x-ms-bmp': '.bmp',
'image/x-nikon-nef': '.nef',
'image/x-olympus-orf': '.orf',
'image/x-photoshop': '.psd',
'image/x-portable-anymap': '.pnm',
'image/x-portable-bitmap': '.pbm',
'image/x-portable-graymap': '.pgm',
'image/x-portable-pixmap': '.ppm',
'image/x-rgb': '.rgb',
'image/x-xbitmap': '.xbm',
'image/x-xpixmap': '.xpm',
'image/x-xwindowdump': '.xwd',
'message/rfc822': '.eml',
'model/iges': '.igs',
'model/mesh': '.msh',
'model/x3d+binary': '.x3db',
'model/x3d+vrml': '.x3dv',
'model/x3d+xml': '.x3d',
'text/cache-manifest': '.manifest',
'text/calendar': '.ics',
'text/css': '.css',
'text/csv': '.csv',
'text/h323': '.323',
'text/html': '.htm',
'text/iuls': '.uls',
'text/mathml': '.mml',
'text/plain': '.txt',
'text/richtext': '.rtx',
'text/scriptlet': '.sct',
'text/tab-separated-values': '.tsv',
'text/texmacs': '.tm',
'text/vnd.sun.j2me.app-descriptor': '.jad',
'text/vnd.wap.wmlscript': '.wmls',
'text/vnd.wap.wml': '.wml',
'text/x-bibtex': '.bib',
'text/x-boo': '.boo',
'text/x-chdr': '.h',
'text/x-c++hdr': '.hpp',
'text/x-component': '.htc',
'text/x-csh': '.csh',
'text/x-csrc': '.c',
'text/x-c++src': '.cpp',
'text/x-diff': '.diff',
'text/x-dsrc': '.d',
'text/x-haskell': '.hs',
'text/x-java': '.java',
'text/x-literate-haskell': '.lhs',
'text/x-moc': '.moc',
'text/x-pascal': '.pas',
'text/x-pcs-gcd': '.gcd',
'text/x-perl': '.pl',
'text/x-python': '.py',
'text/x-scala': '.scala',
'text/x-setext': '.etx',
'text/x-sgml': '.sgml',
'text/x-sh': '.sh',
'text/x-tcl': '.tcl',
'text/x-tex': '.tex',
'text/xul': '.xul',
'text/x-vcalendar': '.vcs',
'text/x-vcard': '.vcf',
'video/3gpp': '.3gp',
'video/annodex': '.axv',
'video/dl': '.dl',
'video/dv': '.dv',
'video/fli': '.fli',
'video/gl': '.gl',
'video/mp4': '.mp4',
'video/mpeg': '.mpg',
'video/ogg': '.ogv',
'video/quicktime': '.mov',
'video/vnd.mpegurl': '.mxu',
'video/x-flv': '.flv',
'video/x-la-asf': '.lsf',
'video/x-matroska': '.mkv',
'video/x-mng': '.mng',
'video/x-ms-asf': '.asf',
'video/x-msvideo': '.avi',
'video/x-ms-wmv': '.wmv',
'video/x-ms-wm': '.wm',
'video/x-ms-wmx': '.wmx',
'video/x-ms-wvx': '.wvx',
'video/x-sgi-movie': '.movie',
'x-conference/x-cooltalk': '.ice',
'x-epoc/x-sisx-app': '.sisx',
'x-world/x-vrml': '.vrml'
}
forward_addr = {
'josch':'j.schauer@email.de'
}
class DisposableMailServer(SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
rcpttos = [x[:-17] for x in rcpttos
if x.endswith('@mister-muffin.de')
and len(x)>17]
if not rcpttos:
return None
if "platinum" in rcpttos:
return None
print "new mail for", rcpttos
msg = message_from_string(data)
basepath = '/var/www/mister-muffin.de/mail/'
counter = 1
now = datetime.now()
header, _ = msg.as_string().split('\n\n', 1)
for name in rcpttos:
if name in forward_addr:
server = smtplib.SMTP("mx-ha01.web.de")
server.sendmail(mailfrom, [forward_addr[name]], data)
server.quit()
path = os.path.join(basepath, name, now.isoformat())
if not os.path.exists(path):
os.makedirs(path)
# not using the mailbox module because it creates mailboxes with
# executable bit and messes up the From
with open(os.path.join(path, "message.mbox"), "w") as f:
f.write("From %s %s\r\n"%(mailfrom, now.strftime('%a %b %d %H:%M:%S %Y')))
f.write(data)
with open(os.path.join(path, "header.txt"), "w") as f:
f.write(header)
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
filename = part.get_filename()
if not filename:
ext = exts.get(part.get_content_type())
if not ext:
ext = '.bin'
filename = 'part-%03d%s'%(counter,ext)
else:
filename = 'part-%03d-%s'%(counter,filename)
filename = os.path.join(path, filename)
if part.get_payload(decode=True):
with open(filename, "w") as f:
f.write(part.get_payload(decode=True))
counter += 1
if __name__ == '__main__':
proxy = DisposableMailServer(("mister-muffin.de", 25), ("localhost", 25))
try:
asyncore.loop()
except KeyboardInterrupt:
pass