forked from Arceliar/bmwrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outgoing.py
113 lines (102 loc) · 3.88 KB
/
outgoing.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
import asyncore
from smtpd import SMTPServer
import email.parser, email.header
import bminterface
import logging
class outgoingServer(SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
parser = email.parser.FeedParser()
parser.feed(data)
msg = parser.close()
toAddress = msg['To']
fromAddress = msg['From']
subject = u' '.join(unicode(t[0], t[1] or 'UTF-8') for t in email.header.decode_header(msg['Subject'])).encode('UTF-8')
body = self._bmformat(msg)
#Make sure we don't send an actually blank subject or body--this can cause problems.
if not subject:
subject = ' '
if not body:
body = ' '
if bminterface.send(toAddress, fromAddress, subject, body):
logging.info("Message queued for sending...")
else:
logging.info("There was an error trying to send the message...")
return 0
def _bmformat(self, msg):
disclaimer = "\n<!-- Email sent from bmwrapper -->\n<!-- https://github.com/Arceliar/bmwrapper -->\n"
imageNotice = "<!-- Note: An image is attached below -->\n"
if not msg.is_multipart():
#This is a single part message, so there's nothing to do.
#Will still parse, just to get rid of awkward quote '>' everywhere.
myText, oldText = self._parseQuoteText(msg.get_payload())
return myText + oldText
else:
#This is a multipart message.
#Unfortunately, now we have to actually do work.
myText, oldText, image = self._recurseParse(msg)
return imageNotice + myText + oldText + disclaimer + image
def _recurseParse(self, msg):
text = ''
image = ''
for item in msg.get_payload():
if 'text/plain' in item['Content-Type']:
text += item.get_payload()
elif 'image' in item['Content-Type']:
[filetype, name] = item['Content-Type'].rstrip().split('\n')
name = name.replace('name', 'alt')
imageraw = item.get_payload().rstrip().split('\n')
imagedata = ''
for line in imageraw:
if not line[0] == '-':
imagedata += line
image += '<img '+name+' src="data:'+filetype+'base64,'+imagedata+'" />'
elif 'multipart' in item['Content-Type']:
firstTest_new, text_new, image_new = self._recurseParse(item)
text += firstText_new + text_new
image += image_new
else:
#Note that there's a chance we may die horribly if nothing returns.
pass
firstText, text = self._parseQuoteText(text)
return firstText, text, image
def _parseQuoteText(self, text):
rawText = text.split('\n')
tempText = []
text = ''
firstText = ''
n = 0
while len(rawText):
for line in range(len(rawText)):
if rawText[line]:
if (rawText[line][0] == '>'): # and rawText[line].strip('>')[0] == ' '):
rawText[line] = rawText[line][1:]
if rawText[line]:
if rawText[line][0] == ' ':
rawText[line] = rawText[line][1:]
tempText.append(rawText[line])
else:
if n == 0:
firstText += rawText[line]+'\n'
else:
text += rawText[line]+'\n'
else:
if n == 0:
firstText += '\n'
else:
text += '\n'
if len(tempText):
text += '\n\n------------------------------------------------------\n'
rawText = tempText
tempText = []
n += 1
text = text.rstrip('\n')
firstText = firstText.rstrip('\n')
return firstText, text
def run():
foo = outgoingServer(('localhost', 12345), None)
try:
asyncore.loop()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
run()