This repository has been archived on 2024-12-10. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-10/src/class_smtpDeliver.py

118 lines
4.4 KiB
Python
Raw Normal View History

"""
2019-12-23 10:49:03 +01:00
SMTP client thread for delivering emails
"""
# pylint: disable=unused-variable
import smtplib
import urlparse
from email.header import Header
from email.mime.text import MIMEText
import queues
import state
from bmconfigparser import BMConfigParser
from network.threads import StoppableThread
SMTPDOMAIN = "bmaddr.lan"
class smtpDeliver(StoppableThread):
"""SMTP client thread for delivery"""
name = "smtpDeliver"
_instance = None
def stopThread(self):
2020-01-15 11:47:26 +01:00
# pylint: disable=no-member
try:
2020-01-15 11:47:26 +01:00
queues.UISignallerQueue.put(("stopThread", "data"))
except:
pass
super(smtpDeliver, self).stopThread()
@classmethod
def get(cls):
"""(probably) Singleton functionality"""
if not cls._instance:
cls._instance = smtpDeliver()
return cls._instance
def run(self):
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
2020-01-15 11:47:26 +01:00
# pylint: disable=deprecated-lambda
while state.shutdown == 0:
command, data = queues.UISignalQueue.get()
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
dest = BMConfigParser().safeGet("bitmessagesettings", "smtpdeliver", '')
if dest == '':
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')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = fromAddress + '@' + SMTPDOMAIN
2020-01-15 11:47:26 +01:00
toLabel = map(
lambda y: BMConfigParser().safeGet(y, "label"),
2020-01-15 11:47:26 +01:00
filter(
lambda x: x == toAddress, BMConfigParser().addresses())
)
if toLabel:
msg['To'] = "\"%s\" <%s>" % (Header(toLabel[0], 'utf-8'), toAddress + '@' + SMTPDOMAIN)
else:
msg['To'] = toAddress + '@' + SMTPDOMAIN
client.ehlo()
client.starttls()
client.ehlo()
client.sendmail(msg['From'], [to], msg.as_string())
self.logger.info(
'Delivered via SMTP to %s through %s:%i ...',
to, u.hostname, u.port)
client.quit()
except:
self.logger.error('smtp delivery error', exc_info=True)
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:
self.logger.warning(
'Command sent to smtpDeliver not recognized: %s', command)