forked from Bitmessage/PyBitmessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_smtpDeliver.py
117 lines (109 loc) · 4.38 KB
/
class_smtpDeliver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
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 config
from network.threads import StoppableThread
SMTPDOMAIN = "bmaddr.lan"
class smtpDeliver(StoppableThread):
"""SMTP client thread for delivery"""
name = "smtpDeliver"
_instance = None
def stopThread(self):
# pylint: disable=no-member
try:
queues.UISignallerQueue.put(("stopThread", "data"))
except: # noqa:E722
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
# 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 = config.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
toLabel = map(
lambda y: config.safeGet(y, "label"),
filter(
lambda x: x == toAddress, config.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: # noqa:E722
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)