2018-10-10 14:00:53 +02:00
|
|
|
"""
|
2019-12-23 10:49:03 +01:00
|
|
|
SMTP client thread for delivering emails
|
2018-10-10 14:00:53 +02:00
|
|
|
"""
|
|
|
|
# pylint: disable=unused-variable
|
|
|
|
|
2016-06-30 12:27:36 +02:00
|
|
|
import smtplib
|
|
|
|
import urlparse
|
2018-10-10 14:00:53 +02:00
|
|
|
from email.header import Header
|
|
|
|
from email.mime.text import MIMEText
|
2016-06-30 12:27:36 +02:00
|
|
|
|
2017-02-08 13:41:56 +01:00
|
|
|
import queues
|
2017-01-14 23:20:15 +01:00
|
|
|
import state
|
2018-10-10 14:00:53 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
2019-08-06 13:04:33 +02:00
|
|
|
from network.threads import StoppableThread
|
2016-06-30 12:27:36 +02:00
|
|
|
|
|
|
|
SMTPDOMAIN = "bmaddr.lan"
|
|
|
|
|
2018-10-10 14:00:53 +02:00
|
|
|
|
2019-08-01 13:37:26 +02:00
|
|
|
class smtpDeliver(StoppableThread):
|
2018-10-10 14:00:53 +02:00
|
|
|
"""SMTP client thread for delivery"""
|
2019-08-01 13:37:26 +02:00
|
|
|
name = "smtpDeliver"
|
2016-06-30 12:27:36 +02:00
|
|
|
_instance = None
|
|
|
|
|
|
|
|
def stopThread(self):
|
2020-01-15 11:47:26 +01:00
|
|
|
# pylint: disable=no-member
|
2016-06-30 12:27:36 +02:00
|
|
|
try:
|
2020-01-15 11:47:26 +01:00
|
|
|
queues.UISignallerQueue.put(("stopThread", "data"))
|
2021-09-16 15:31:33 +02:00
|
|
|
except: # noqa:E722
|
2016-06-30 12:27:36 +02:00
|
|
|
pass
|
|
|
|
super(smtpDeliver, self).stopThread()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls):
|
2018-10-10 14:00:53 +02:00
|
|
|
"""(probably) Singleton functionality"""
|
2016-06-30 12:27:36 +02:00
|
|
|
if not cls._instance:
|
2017-02-20 20:53:50 +01:00
|
|
|
cls._instance = smtpDeliver()
|
2016-06-30 12:27:36 +02:00
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
def run(self):
|
2018-10-10 14:00:53 +02:00
|
|
|
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
|
2020-01-15 11:47:26 +01:00
|
|
|
# pylint: disable=deprecated-lambda
|
2017-01-14 23:20:15 +01:00
|
|
|
while state.shutdown == 0:
|
2017-02-08 13:41:56 +01:00
|
|
|
command, data = queues.UISignalQueue.get()
|
2016-06-30 12:27:36 +02:00
|
|
|
if command == 'writeNewAddressToTable':
|
|
|
|
label, address, streamNumber = data
|
|
|
|
elif command == 'updateStatusBar':
|
|
|
|
pass
|
|
|
|
elif command == 'updateSentItemStatusByToAddress':
|
|
|
|
toAddress, message = data
|
|
|
|
elif command == 'updateSentItemStatusByAckdata':
|
|
|
|
ackData, message = data
|
|
|
|
elif command == 'displayNewInboxMessage':
|
|
|
|
inventoryHash, toAddress, fromAddress, subject, body = data
|
2017-01-11 14:27:19 +01:00
|
|
|
dest = BMConfigParser().safeGet("bitmessagesettings", "smtpdeliver", '')
|
2016-06-30 15:19:58 +02:00
|
|
|
if dest == '':
|
2016-06-30 12:27:36 +02:00
|
|
|
continue
|
|
|
|
try:
|
|
|
|
u = urlparse.urlparse(dest)
|
|
|
|
to = urlparse.parse_qs(u.query)['to']
|
|
|
|
client = smtplib.SMTP(u.hostname, u.port)
|
|
|
|
msg = MIMEText(body, 'plain', 'utf-8')
|
2016-07-09 09:58:01 +02:00
|
|
|
msg['Subject'] = Header(subject, 'utf-8')
|
2016-06-30 12:27:36 +02:00
|
|
|
msg['From'] = fromAddress + '@' + SMTPDOMAIN
|
2020-01-15 11:47:26 +01:00
|
|
|
toLabel = map(
|
2018-10-10 14:00:53 +02:00
|
|
|
lambda y: BMConfigParser().safeGet(y, "label"),
|
2020-01-15 11:47:26 +01:00
|
|
|
filter(
|
2018-10-10 14:00:53 +02:00
|
|
|
lambda x: x == toAddress, BMConfigParser().addresses())
|
|
|
|
)
|
|
|
|
if toLabel:
|
2016-07-09 09:58:01 +02:00
|
|
|
msg['To'] = "\"%s\" <%s>" % (Header(toLabel[0], 'utf-8'), toAddress + '@' + SMTPDOMAIN)
|
|
|
|
else:
|
|
|
|
msg['To'] = toAddress + '@' + SMTPDOMAIN
|
2016-06-30 12:27:36 +02:00
|
|
|
client.ehlo()
|
|
|
|
client.starttls()
|
|
|
|
client.ehlo()
|
|
|
|
client.sendmail(msg['From'], [to], msg.as_string())
|
2019-08-06 13:04:33 +02:00
|
|
|
self.logger.info(
|
|
|
|
'Delivered via SMTP to %s through %s:%i ...',
|
|
|
|
to, u.hostname, u.port)
|
2016-06-30 12:27:36 +02:00
|
|
|
client.quit()
|
2021-09-16 15:31:33 +02:00
|
|
|
except: # noqa:E722
|
2019-08-06 13:04:33 +02:00
|
|
|
self.logger.error('smtp delivery error', exc_info=True)
|
2016-06-30 12:27:36 +02:00
|
|
|
elif command == 'displayNewSentMessage':
|
|
|
|
toAddress, fromLabel, fromAddress, subject, message, ackdata = data
|
|
|
|
elif command == 'updateNetworkStatusTab':
|
|
|
|
pass
|
|
|
|
elif command == 'updateNumberOfMessagesProcessed':
|
|
|
|
pass
|
|
|
|
elif command == 'updateNumberOfPubkeysProcessed':
|
|
|
|
pass
|
|
|
|
elif command == 'updateNumberOfBroadcastsProcessed':
|
|
|
|
pass
|
|
|
|
elif command == 'setStatusIcon':
|
|
|
|
pass
|
|
|
|
elif command == 'changedInboxUnread':
|
|
|
|
pass
|
|
|
|
elif command == 'rerenderMessagelistFromLabels':
|
|
|
|
pass
|
|
|
|
elif command == 'rerenderMessagelistToLabels':
|
|
|
|
pass
|
|
|
|
elif command == 'rerenderAddressBook':
|
|
|
|
pass
|
|
|
|
elif command == 'rerenderSubscriptions':
|
|
|
|
pass
|
|
|
|
elif command == 'rerenderBlackWhiteList':
|
|
|
|
pass
|
|
|
|
elif command == 'removeInboxRowByMsgid':
|
|
|
|
pass
|
|
|
|
elif command == 'newVersionAvailable':
|
|
|
|
pass
|
|
|
|
elif command == 'alert':
|
|
|
|
title, text, exitAfterUserClicksOk = data
|
|
|
|
elif command == 'stopThread':
|
|
|
|
break
|
|
|
|
else:
|
2019-08-06 13:04:33 +02:00
|
|
|
self.logger.warning(
|
|
|
|
'Command sent to smtpDeliver not recognized: %s', command)
|