#!/usr/bin/python # fomenca v0.3 - auto set encodings in email message # (C) Mikhail Ramendik mr@ramendik.ru 2004 # License: GPL v2 or later # Requires: enca 1.7, pyenca 0.4; see http://trific.ath.cx/software/enca/ # Usage: filter the message through this # Changelog: # 0.1 initial version # 0.2 fixed a bug that broke base64 messages # 0.3 thanks to Yeti, enca's author, for a new version which supports MIME # charsets! Accordingly, instead of a conversion table, I now use these # charsets directly # Set the language here. I'll probably add a commandline way of setting later Lng="ru" import sys import email import email.Message import Enca InStrings=sys.stdin.read() WrittenOut=0 Anz=Enca.Analyser(Lng) try: Msg=email.message_from_string(InStrings,strict=0) for Part in Msg.walk(): if Part.is_multipart(): continue ContentType=Part.get_content_type() if ContentType.count("text")>0: #so it's a text - let's handle the charset try: Txt=Part.get_payload(decode=1) Cst=Anz.analyse(Txt).charset(Enca.NAME_STYLE_MIME) Part.set_param("charset",Cst) except: pass sys.stdout.write(Msg.__str__()) WrittenOut=1 finally: if not WrittenOut:sys.stdout.write(InStrings)