def importnews(self, cb, token): msgproc = os.popen2(['/usr/lib/news/bin/sm', token ], 'rt') msgproc[0].close() msg = email.message_from_file(msgproc[1]) msgproc[1].close() author = decode_header(msg['From']) if author is None: cb(True, None) return author = email.Utils.parseaddr(author) ngs = [ng.strip() for ng in msg['Newsgroups'].split(',')] ngs = [ng for ng in ngs if ng in VALID_NGS or ng.startswith("mud.") ] if len(ngs)==0: cb(True, None) return subj = decode_header(msg['Subject'] or '') mid = msg['Message-ID'] ref = msg['References'] or '' ref = ref.replace("\n","").replace("\t"," ") ref = [msgid.strip() for msgid in ref.split(' ')] ref = [msgid for msgid in ref if len(msgid)>1] date = decode_header(msg['Date']) if not date is None: date = email.Utils.parsedate_tz(date) if date is None: date = time.time() else: date = email.Utils.mktime_tz(date) if msg.is_multipart(): text = None for part in msg.get_payload(): if part.get_content_type() == 'text/plain': text = part.get_payload(None, True) cs = part.get_content_charset() break if text is None: cb(True, None) return else: text = msg.get_payload(None, True) cs = msg.get_content_charset() subj = konvert_umlaute(subj) addr = author[1] author = konvert_umlaute(author[0] or author[1]) text = konvert_umlaute(unicode(text,cs or "iso8859-15")) self.mudnews.importnews(cb, ngs, author, subj, int(date), mid, ref, text)
def cancelnews(self, cb, token): msgproc = os.popen2(['/usr/lib/news/bin/sm', token ], 'rt') msgproc[0].close() msg = email.message_from_file(msgproc[1]) msgproc[1].close() author = decode_header(msg['From']) cmd = decode_header(msg['Control']) if author is None or cmd is None: cb(True, None) return author = email.Utils.parseaddr(author) cmd = cmd.split(" ",1) if len(cmd)<2: cb(True, None) return if cmd[0].lower() != "cancel": cb(True, None) return ngs = [ng.strip() for ng in msg['Newsgroups'].split(',')] ngs = [ng for ng in ngs if ng in VALID_NGS or ng.startswith("mud.") ] if len(ngs)==0: cb(True, None) return author = konvert_umlaute(author[0] or author[1]) self.mudnews.cancelnews(cb, ngs, author, cmd[1])
def process_mail(): if not 'LOCAL_PART' in os.environ: raise RuntimeError('LOCAL_PART environment variable missing.') if not 'SENDER' in os.environ: raise RuntimeError('SENDER environment variable missing.') recipient = os.environ['LOCAL_PART'] sender = os.environ['SENDER'] if filter_spam(recipient.lower()): # Check for Spam... spamass = subprocess.Popen('/usr/bin/spamassassin -e -p /etc/spamassassin.conf', stdout = subprocess.PIPE, shell = True, close_fds = True) msg = email.message_from_file(spamass.stdout) if msg.get('X-Spam-Flag','').lower() == 'yes': raise RuntimeError('Your mail was detected as SPAM.') else: msg = email.message_from_file(sys.stdin) author = decode_header(msg['From']) if author is None: raise RuntimeError('No FROM address given.') cc = email.utils.getaddresses(msg.get_all('To', []) + msg.get_all('Cc', [])) date = decode_header(msg['Date']) if not date is None: date = email.Utils.parsedate_tz(date) if date is None: date = time.time() else: date = email.Utils.mktime_tz(date) header = msg.items() if msg.is_multipart(): text = None for part in msg.get_payload(): if part.get_content_type() == 'text/plain': text = part.get_payload(None, True) cs = part.get_content_charset() # Replace header entries with the ones from the payload plheader = part.items() plheaderentries = set([entry[0] for entry in plheader]) header = [ entry for entry in header if not entry[0] in plheaderentries ] + plheader break if text is None: raise RuntimeError('No plain text part found. HTML only mails are not accepted.') else: text = msg.get_payload(None, True) cs = msg.get_content_charset() subj = konvert_umlaute(decode_header(msg['Subject'] or '')) text = konvert_umlaute(unicode(text,cs or "iso8859-15")) author_addr = email.Utils.parseaddr(author) addtext = konvert_umlaute(sender) + ":\n" + konvert_umlaute(author) + "\n" if msg['Reply-To']: addtext += konvert_umlaute(decode_header(msg['Reply-To'])) + "\n" mudmail = client.synconnect(SOCKETNAME, "mail") mudmail.receivemail(recipient, strip_localhost(author_addr[1]), [ strip_localhost(addr[1]) for addr in cc ], subj, date, addtext + "\n" + text, header) mudmail._close()