#! /usr/bin/env python # q.Auto __author__ = "HERMiT" import sys, re, poplib, email, smtplib from email.MIMEText import MIMEText import os DEBUG = 0 POP_USER = "XXXXX" POP_PASS = "XXXXX" POP_SRV = "pop3.XXXXX" SMTP_SRV = "smtp.XXXXX" SMTP_PORT = 25 MY_EMAIL = "XXXXX@XXXXX.XXXXX.XXXXX" MSG_TEXT = " - OK!" LOG_FILE = "/tmp/q.auto.log" if DEBUG == 1: OUT_DIR = "/tmp/out/" else: OUT_DIR = "/tmp/out2/" saveout = sys.stdout fsock = open(LOG_FILE, 'a+') sys.stdout = fsock try: M = poplib.POP3(POP_SRV) try: M.user(POP_USER) M.pass_(POP_PASS) except: print "Can't logon as " + POP_USER print sys.exc_info()[1] sys.exc_clear() numMessages = len(M.list()[1]) for i in range(numMessages): for j in M.retr(i+1): if type(j) == list: numElements = len(j) outString = "" for k in range(numElements): outString += j[k] outString += '\n' message = email.message_from_string(outString) print message patternAG = re.compile("XX") if patternAG.match(message['Subject']): for part in message.walk(): attach_filename = part.get_filename() if attach_filename <> None: f = open(OUT_DIR+attach_filename,'w+') f.write(part.get_payload(decode=True)) #print part.get_payload(decode=True) f.close() text = unicode(attach_filename + MSG_TEXT, "cp1251").encode("koi8-r") msg = MIMEText(text, "", "cp1251") msg['Subject'] = attach_filename + MSG_TEXT msg['From'] = MY_EMAIL msg['To'] = message['From'] s = smtplib.SMTP(SMTP_SRV, SMTP_PORT) s.sendmail(MY_EMAIL, message['From'], msg.as_string()) s.quit() patternRS = re.compile("importTerribleInsurance") if patternRS.match(message['Subject']): os.spawnv(os.P_NOWAIT,LOAN_SH,['&']) patternDBF = re.compile("XXXXX") if patternDBF.match(message['Subject']): for part in message.walk(): attach_filename = part.get_filename() if attach_filename <> None: f = open(OUT_DBF_DIR+attach_filename,'w+') f.write(part.get_payload(decode=True)) #print part.get_payload(decode=True) f.close() text = unicode(attach_filename + MSG_TEXT, "cp1251").encode("koi8-r") msg = MIMEText(text, "", "cp1251") msg['Subject'] = attach_filename + MSG_TEXT msg['From'] = MY_EMAIL msg['To'] = message['From'] s = smtplib.SMTP(SMTP_SRV, SMTP_PORT) s.sendmail(MY_EMAIL, message['From'], msg.as_string()) s.quit() os.system("/usr/bin/perl "+DBF_SH+" "+OUT_DBF_DIR+attach_filename) if DEBUG == 0: M.dele(i+1) except: print "Can't find " + POP_SRV for e in range(len(sys.exc_info())): print sys.exc_info()[e] sys.exc_clear() M.quit() if DEBUG == 0: sys.stdout = saveout fsock.close()