/ src / class_smtpDeliver.py
class_smtpDeliver.py
  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  from . import queues
 13  from . 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 = [config.safeGet(y, "label") for y in [x for x in config.addresses() if x == toAddress]]
 63                      if toLabel:
 64                          msg['To'] = "\"%s\" <%s>" % (Header(toLabel[0], 'utf-8'), toAddress + '@' + SMTPDOMAIN)
 65                      else:
 66                          msg['To'] = toAddress + '@' + SMTPDOMAIN
 67                      client.ehlo()
 68                      client.starttls()
 69                      client.ehlo()
 70                      client.sendmail(msg['From'], [to], msg.as_string())
 71                      self.logger.info(
 72                          'Delivered via SMTP to %s through %s:%i ...',
 73                          to, u.hostname, u.port)
 74                      client.quit()
 75                  except:  # noqa:E722
 76                      self.logger.error('smtp delivery error', exc_info=True)
 77              elif command == 'displayNewSentMessage':
 78                  toAddress, fromLabel, fromAddress, subject, message, ackdata = data
 79              elif command == 'updateNetworkStatusTab':
 80                  pass
 81              elif command == 'updateNumberOfMessagesProcessed':
 82                  pass
 83              elif command == 'updateNumberOfPubkeysProcessed':
 84                  pass
 85              elif command == 'updateNumberOfBroadcastsProcessed':
 86                  pass
 87              elif command == 'setStatusIcon':
 88                  pass
 89              elif command == 'changedInboxUnread':
 90                  pass
 91              elif command == 'rerenderMessagelistFromLabels':
 92                  pass
 93              elif command == 'rerenderMessagelistToLabels':
 94                  pass
 95              elif command == 'rerenderAddressBook':
 96                  pass
 97              elif command == 'rerenderSubscriptions':
 98                  pass
 99              elif command == 'rerenderBlackWhiteList':
100                  pass
101              elif command == 'removeInboxRowByMsgid':
102                  pass
103              elif command == 'newVersionAvailable':
104                  pass
105              elif command == 'alert':
106                  title, text, exitAfterUserClicksOk = data
107              elif command == 'stopThread':
108                  break
109              else:
110                  self.logger.warning(
111                      'Command sent to smtpDeliver not recognized: %s', command)