#!/usr/local/bin/python ###################################################################### # # mailjab.py # # David Simmons, April 12, 2008 # # Accept a mail message on stdin and forward a summary of # it to a jabber address. # # Here's how I use it in my procmailrc, to send a copy of whitelisted # mails to this script (and also to my mailbox, of course): # # :0 # * ^From:.*(really_important_person@server.com|another_address@tardis.nu) # { # :0 ic # |/home/simmons/bin/mailjab.py # # :0: # /var/mail/simmons # } # ###################################################################### import sys,os,xmpp,time,email,re DST_JABBER_ID="jabber-user@some-jabber-server.com" SRC_JABBER_ID="mailjab@some-jabber-server.com" SRC_JABBER_USERNAME="mailjab" SRC_JABBER_PASSWORD="super-duper-secret" ###################################################################### # slurp the raw email from stdin ###################################################################### raw_email = sys.stdin.read() ###################################################################### # fork, so we don't hold up mail delivery ###################################################################### try: pid = os.fork() if pid > 0: # exit the parent sys.exit(0) except OSError, e: print >>sys.stderr, "fork failed: %d (%s)" % (e.errno, e.strerror) sys.exit(1) ###################################################################### # parse the mail message into a summary ###################################################################### # read mail msg = email.message_from_string(raw_email) # parse sender match = re.search(r'([\w.-]+)@[\w.-]+', msg["From"]) if match: sender = match.group(1) else: match = re.search(r'<([\w.-]+)>', msg["From"]) if match: sender = match.group(1) else: sender = msg["From"] summary = sender + " \" "; # parse subject if msg["Subject"]: summary += msg["Subject"] # parse body body = "" if msg.is_multipart(): for part in msg.walk(): if part.get_content_type() == "text/plain": body = part.get_payload() else: body = msg.get_payload() sig_pattern = re.compile(r'^--.*$', re.DOTALL|re.MULTILINE) body = sig_pattern.sub('', body) summary += " \" " + re.sub(r'\s+', " ", body) # squeeze summary summary = re.sub(r'\s+', ' ', summary); summary = re.sub(r'^\s', '', summary); summary = re.sub(r'\s$', '', summary); # truncate summary if len(summary) > 256: summary = summary[0:256] + "..." ###################################################################### # send the jabber message ###################################################################### # connect to jabber jid=xmpp.protocol.JID(SRC_JABBER_ID) client=xmpp.Client(jid.getDomain(),debug=[]) con=client.connect(None,None,0,False) if not con: print >>sys.stderr, 'mailjab could not connect!' sys.exit(1) # authenticate auth=client.auth(SRC_JABBER_USERNAME,SRC_JABBER_PASSWORD,"Mail") if not auth: print >>sys.stderr, "mailjab could not authenticate!" sys.exit(1) # send the message id=client.send(xmpp.protocol.Message(DST_JABBER_ID,"NEW MAIL: "+summary)) # disconnect client.disconnect()