Advertisement
Guest User

Untitled

a guest
May 31st, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import string
  4. import sys
  5. import ConfigParser
  6. import socket # need this for error handling with try:except
  7. import smtplib
  8. import email
  9. import imaplib
  10. import quopri
  11. import re
  12.  
  13. def logthis(prefix,msg):
  14. print "[%s] %s" %(prefix,msg)
  15.  
  16. class Config:
  17. def __init__(self):
  18. self.config = ConfigParser.ConfigParser()
  19. self.config.read("file.conf")
  20. self.sections = self.config.sections
  21.  
  22. def get_atributes(self,list_name):
  23. try:
  24. self.list_name = list_name
  25. self.username = list_name
  26. self.domain = self.config.get(list_name,'domain')
  27. self.source = self.config.get(list_name,'source')
  28. self.max_size = self.config.get(list_name,'max_size')
  29. self.subject = self.config.get(list_name,'subject')
  30. self.password = self.config.get(list_name,'password')
  31. self.imapserver = self.config.get(list_name,'imap_host')
  32. self.smtpserver = self.config.get(list_name,'smtp_host')
  33. return 1
  34. except ConfigParser.NoOptionError,e:
  35. logthis(self.list_name,e)
  36. return 0
  37.  
  38. class ShotMail:
  39.  
  40. def __init__(self,list_name):
  41. self.cfg = Config()
  42. if self.cfg.get_atributes(list_name) == 1:
  43. if self.read_source() == 1:
  44. self._connect()
  45.  
  46.  
  47. def read_source(self):
  48. try:
  49. with open(self.cfg.source) as f:
  50. self.USER_DATABASE = f.readlines()
  51. #print [s.replace('\n','') for s in self.USER_DATABASE]
  52. self.USER_DATABASE = map(string.strip,self.USER_DATABASE)
  53. return 1
  54. except IOError,e:
  55. logthis(self.cfg.list_name,e)
  56. return 0
  57.  
  58. def _connect(self):
  59. try:
  60. # Log-in in imap using SSL
  61. self.conn = imaplib.IMAP4_SSL(self.cfg.imapserver)
  62. self.conn.login(self.cfg.username,self.cfg.password)
  63. # create mail boxes
  64. if self.conn.select('OVERSIZE')[0] == 'NO': self.conn.create('OVERSIZE')
  65. if self.conn.select('INVALID')[0] == 'NO': self.conn.create('INVALID')
  66. if self.conn.select('CMD')[0] == 'NO': self.conn.create('CMD')
  67. # Select inbox and look for new messages
  68. if self.conn.select('INBOX')[0] != 'NO':
  69. return_code, data_range = self.conn.search(None,'ALL')
  70. self._retrieve(data_range)
  71. else:
  72. logthis(self.cfg.list_name,"inbox not found")
  73. except imaplib.IMAP4.error,e:
  74. logthis(self.cfg.list_name,e)
  75. except socket.gaierror,e:
  76. logthis(self.cfg.list_name,e)
  77.  
  78. def _retrieve(self,data_range):
  79. print "Processing %d messages" % (len(data_range))
  80. for msg_id in data_range[0].split():
  81. return_code, mail_data = self.conn.fetch(msg_id,'(RFC822)')
  82. # store mail size
  83. self.size = mail_data[0][0]
  84. # store and decode the mail content
  85. msg_content = quopri.decodestring(mail_data[0][1])
  86. # create a mail object
  87. self.msg = email.message_from_string(msg_content)
  88. # Apply rules over every message
  89. self.check_server_msg()
  90. self.conn.logout()
  91.  
  92. def check_server_msg(self):
  93. mail_subject, mail_from, recv, return_path = self._get_header()
  94.  
  95. # Authorization based on True/False
  96. # Here should apply rules
  97. forward_flag = []
  98. forward_flag.append(self.is_allowed_user(return_path))
  99. forward_flag.append(self.check_size())
  100.  
  101. if forward_flag[0] == True:
  102. if forward_flag[1] == True:
  103. print "Sending message from",mail_from
  104. self._send(mail_subject,mail_from)
  105. else:
  106. print "Return a message, complaining about max_size"
  107. else:
  108. print "Do not send message from",mail_from
  109.  
  110. def check_size(self):
  111. return True
  112.  
  113. def is_allowed_user(self,return_path):
  114. for user in self.USER_DATABASE:
  115. if return_path == '':
  116. return False
  117. if user.replace('\n','') == return_path:
  118. return True
  119. return False
  120.  
  121. def _get_header(self):
  122. recv = ''
  123. for i in self.msg.items():
  124. if i[0] == 'Return-Path':
  125. return_path = i[1]
  126. return_path = return_path[1:-1]
  127. if i[0] == 'Subject':
  128. mail_subject = i[1]
  129. if i[0] == 'From':
  130. mail_from = i[1]
  131. if i[0] == 'Received':
  132. recv = recv + i[1] + "\n"
  133. return [mail_subject, mail_from, recv, return_path]
  134.  
  135.  
  136. def _send(self,mail_subject,mail_from):
  137. server = smtplib.SMTP(self.cfg.smtpserver,25)
  138. server.ehlo()
  139. server.starttls()
  140. server.ehlo()
  141. server.login(self.cfg.username,self.cfg.password)
  142.  
  143. # Get message and change headers
  144. msg = self.msg
  145. msg.replace_header('Subject',self.cfg.subject+" "+mail_subject)
  146.  
  147. for user in self.USER_DATABASE:
  148. user = user.replace('\n','')
  149. #msg.replace_header('To',user)
  150. if user != '':
  151. print "sending to",user
  152. server.sendmail(self.cfg.username,user,msg.as_string())
  153.  
  154. server.quit()
  155.  
  156. if __name__ == '__main__':
  157. config = Config()
  158. sections = config.sections()
  159.  
  160. for section in sections:
  161. ShotMail(section)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement