Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import string
- import sys
- import ConfigParser
- import socket # need this for error handling with try:except
- import smtplib
- import email
- import imaplib
- import quopri
- import re
- def logthis(prefix,msg):
- print "[%s] %s" %(prefix,msg)
- class Config:
- def __init__(self):
- self.config = ConfigParser.ConfigParser()
- self.config.read("file.conf")
- self.sections = self.config.sections
- def get_atributes(self,list_name):
- try:
- self.list_name = list_name
- self.username = list_name
- self.domain = self.config.get(list_name,'domain')
- self.source = self.config.get(list_name,'source')
- self.max_size = self.config.get(list_name,'max_size')
- self.subject = self.config.get(list_name,'subject')
- self.password = self.config.get(list_name,'password')
- self.imapserver = self.config.get(list_name,'imap_host')
- self.smtpserver = self.config.get(list_name,'smtp_host')
- return 1
- except ConfigParser.NoOptionError,e:
- logthis(self.list_name,e)
- return 0
- class ShotMail:
- def __init__(self,list_name):
- self.cfg = Config()
- if self.cfg.get_atributes(list_name) == 1:
- if self.read_source() == 1:
- self._connect()
- def read_source(self):
- try:
- with open(self.cfg.source) as f:
- self.USER_DATABASE = f.readlines()
- #print [s.replace('\n','') for s in self.USER_DATABASE]
- self.USER_DATABASE = map(string.strip,self.USER_DATABASE)
- return 1
- except IOError,e:
- logthis(self.cfg.list_name,e)
- return 0
- def _connect(self):
- try:
- # Log-in in imap using SSL
- self.conn = imaplib.IMAP4_SSL(self.cfg.imapserver)
- self.conn.login(self.cfg.username,self.cfg.password)
- # create mail boxes
- if self.conn.select('OVERSIZE')[0] == 'NO': self.conn.create('OVERSIZE')
- if self.conn.select('INVALID')[0] == 'NO': self.conn.create('INVALID')
- if self.conn.select('CMD')[0] == 'NO': self.conn.create('CMD')
- # Select inbox and look for new messages
- if self.conn.select('INBOX')[0] != 'NO':
- return_code, data_range = self.conn.search(None,'ALL')
- self._retrieve(data_range)
- else:
- logthis(self.cfg.list_name,"inbox not found")
- except imaplib.IMAP4.error,e:
- logthis(self.cfg.list_name,e)
- except socket.gaierror,e:
- logthis(self.cfg.list_name,e)
- def _retrieve(self,data_range):
- print "Processing %d messages" % (len(data_range))
- for msg_id in data_range[0].split():
- return_code, mail_data = self.conn.fetch(msg_id,'(RFC822)')
- # store mail size
- self.size = mail_data[0][0]
- # store and decode the mail content
- msg_content = quopri.decodestring(mail_data[0][1])
- # create a mail object
- self.msg = email.message_from_string(msg_content)
- # Apply rules over every message
- self.check_server_msg()
- self.conn.logout()
- def check_server_msg(self):
- mail_subject, mail_from, recv, return_path = self._get_header()
- # Authorization based on True/False
- # Here should apply rules
- forward_flag = []
- forward_flag.append(self.is_allowed_user(return_path))
- forward_flag.append(self.check_size())
- if forward_flag[0] == True:
- if forward_flag[1] == True:
- print "Sending message from",mail_from
- self._send(mail_subject,mail_from)
- else:
- print "Return a message, complaining about max_size"
- else:
- print "Do not send message from",mail_from
- def check_size(self):
- return True
- def is_allowed_user(self,return_path):
- for user in self.USER_DATABASE:
- if return_path == '':
- return False
- if user.replace('\n','') == return_path:
- return True
- return False
- def _get_header(self):
- recv = ''
- for i in self.msg.items():
- if i[0] == 'Return-Path':
- return_path = i[1]
- return_path = return_path[1:-1]
- if i[0] == 'Subject':
- mail_subject = i[1]
- if i[0] == 'From':
- mail_from = i[1]
- if i[0] == 'Received':
- recv = recv + i[1] + "\n"
- return [mail_subject, mail_from, recv, return_path]
- def _send(self,mail_subject,mail_from):
- server = smtplib.SMTP(self.cfg.smtpserver,25)
- server.ehlo()
- server.starttls()
- server.ehlo()
- server.login(self.cfg.username,self.cfg.password)
- # Get message and change headers
- msg = self.msg
- msg.replace_header('Subject',self.cfg.subject+" "+mail_subject)
- for user in self.USER_DATABASE:
- user = user.replace('\n','')
- #msg.replace_header('To',user)
- if user != '':
- print "sending to",user
- server.sendmail(self.cfg.username,user,msg.as_string())
- server.quit()
- if __name__ == '__main__':
- config = Config()
- sections = config.sections()
- for section in sections:
- ShotMail(section)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement