/ src / class_smtpDeliver.py.bak
class_smtpDeliver.py.bak
  1  """
  2  SMTP client thread for delivering emails
  3  """
  4  # pylint: disable=unused-variable
  5  
  6  import smtplib
  7  from email.header import Header
  8  
  9  from six.moves import email_mime_text
 10  from six.moves.urllib import parse as urlparse
 11  
 12  import queues
 13  import state
 14  from bmconfigparser import config
 15  from network.threads import StoppableThread
 16  
 17  SMTPDOMAIN = "bmaddr.lan"
 18  
 19  
 20  class smtpDeliver(StoppableThread):
 21      """SMTP client thread for delivery"""
 22      name = "smtpDeliver"
 23      _instance = None
 24  
 25      def stopThread(self):
 26          """Relay shutdown instruction"""
 27          queues.UISignalQueue.put(("stopThread", "data"))
 28          super(smtpDeliver, self).stopThread()
 29  
 30      @classmethod
 31      def get(cls):
 32          """(probably) Singleton functionality"""
 33          if not cls._instance:
 34              cls._instance = smtpDeliver()
 35          return cls._instance
 36  
 37      def run(self):
 38          # pylint: disable=too-many-branches,too-many-statements,too-many-locals
 39          # pylint: disable=deprecated-lambda
 40          while state.shutdown == 0:
 41              command, data = queues.UISignalQueue.get()
 42              if command == 'writeNewAddressToTable':
 43                  label, address, streamNumber = data
 44              elif command == 'updateStatusBar':
 45                  pass
 46              elif command == 'updateSentItemStatusByToAddress':
 47                  toAddress, message = data
 48              elif command == 'updateSentItemStatusByAckdata':
 49                  ackData, message = data
 50              elif command == 'displayNewInboxMessage':
 51                  inventoryHash, toAddress, fromAddress, subject, body = data
 52                  dest = config.safeGet("bitmessagesettings", "smtpdeliver", '')
 53                  if dest == '':
 54                      continue
 55                  try:
 56                      u = urlparse.urlparse(dest)
 57                      to = urlparse.parse_qs(u.query)['to']
 58                      client = smtplib.SMTP(u.hostname, u.port)
 59                      msg = email_mime_text(body, 'plain', 'utf-8')
 60                      msg['Subject'] = Header(subject, 'utf-8')
 61                      msg['From'] = fromAddress + '@' + SMTPDOMAIN
 62                      toLabel = map(
 63                          lambda y: config.safeGet(y, "label"),
 64                          filter(
 65                              lambda x: x == toAddress, config.addresses())
 66                      )
 67                      if toLabel:
 68                          msg['To'] = "\"%s\" <%s>" % (Header(toLabel[0], 'utf-8'), toAddress + '@' + SMTPDOMAIN)
 69                      else:
 70                          msg['To'] = toAddress + '@' + SMTPDOMAIN
 71                      client.ehlo()
 72                      client.starttls()
 73                      client.ehlo()
 74                      client.sendmail(msg['From'], [to], msg.as_string())
 75                      self.logger.info(
 76                          'Delivered via SMTP to %s through %s:%i ...',
 77                          to, u.hostname, u.port)
 78                      client.quit()
 79                  except:  # noqa:E722
 80                      self.logger.error('smtp delivery error', exc_info=True)
 81              elif command == 'displayNewSentMessage':
 82                  toAddress, fromLabel, fromAddress, subject, message, ackdata = data
 83              elif command == 'updateNetworkStatusTab':
 84                  pass
 85              elif command == 'updateNumberOfMessagesProcessed':
 86                  pass
 87              elif command == 'updateNumberOfPubkeysProcessed':
 88                  pass
 89              elif command == 'updateNumberOfBroadcastsProcessed':
 90                  pass
 91              elif command == 'setStatusIcon':
 92                  pass
 93              elif command == 'changedInboxUnread':
 94                  pass
 95              elif command == 'rerenderMessagelistFromLabels':
 96                  pass
 97              elif command == 'rerenderMessagelistToLabels':
 98                  pass
 99              elif command == 'rerenderAddressBook':
100                  pass
101              elif command == 'rerenderSubscriptions':
102                  pass
103              elif command == 'rerenderBlackWhiteList':
104                  pass
105              elif command == 'removeInboxRowByMsgid':
106                  pass
107              elif command == 'newVersionAvailable':
108                  pass
109              elif command == 'alert':
110                  title, text, exitAfterUserClicksOk = data
111              elif command == 'stopThread':
112                  break
113              else:
114                  self.logger.warning(
115                      'Command sent to smtpDeliver not recognized: %s', command)