-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailInterface.py
331 lines (266 loc) · 11.5 KB
/
EmailInterface.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/python
import smtplib
import os
import re
from lib.Ledger import Ledger
import lib.functions
from poplib import POP3_SSL
# Import the email modules we'll need
from email.mime.text import MIMEText
import email
class Message(object):
recipient = ""
message = ""
def __init__(self, recipient, message):
self.recipient = recipient
self.message = message
def __eq__(self, other):
if isinstance(other, Message):
return self.recipient == other.recipient and \
self.message == other.message
return NotImplemented
def __ne__(self, other):
ret = self.__eq__(other)
if ret is NotImplemented:
return ret
return not ret
def __str__(self):
return "To: %s; Message: %s" % (self.recipient, self.message)
class Command(object):
command = ""
args = []
issuer = None
issuee = None
def __init__(self, command, issuer, args, issuee=None):
self.command = command
self.issuer = issuer
self.args = args
self.issuee = issuee
def __str__(self):
return "command: %s, args: %s, issuer: %s" \
% (command, args, issuer)
class EmailLedgerInterface(Ledger):
from_regex = ".*<(.*)>.*"
debug_mode = False
def __init__(self, mail_server, port, username, password):
super(EmailLedgerInterface, self).__init__()
self.mail_server = mail_server
self.port = port
self.username = username
self.password = password
self.command_queue = []
self.message_list = []
def receiveCommands(self):
pop_client = POP3_SSL(self.mail_server, self.port)
pop_client.user(self.username)
pop_client.pass_(self.password)
total_messages = len(pop_client.list()[1])
if total_messages > 0:
for ii in xrange(1, total_messages + 1):
headers = email.message_from_string(lib.functions.listToStr(
pop_client.retr(ii)[1]))
sender = ""
print "Received email: %s" % headers['subject']
# check if it's a ledger email
if 'ledger' not in headers['subject'].lower():
continue
print "it's a ledger email"
# parse out who the email is from
m = re.match(self.from_regex, headers['from'])
if m:
sender = m.group(1)
else:
continue
print "sender is %s" % sender
ledger_command_received = False
for part in headers.walk():
if part.get_content_type() == 'text/plain':
print part.get_payload()
lines = part.get_payload().split('\n')
for line in lines:
if self.parseLedgerCommand(sender, line):
ledger_command_received = True
print "Received command: %s from %s" \
% (line, sender)
if not ledger_command_received:
self.queueMessage(sender, "You must begin each command "
"with 'ledger'")
pop_client.quit()
def parseLedgerCommand(self, sender, command):
split = command.split()
# find the command line
if len(split) > 0:
if split[0].lower() == "ledger":
self.command_queue.append(Command(command,
sender, split[1:]))
return True
return False
def performCommands(self):
for command in self.command_queue:
args = command.args
# help
if len(args) >= 1 and args[0].lower() == "help":
self.queueMessage(command.issuer,
"Commands:\n"
"add me <uname> - add yourself as a user\n"
"add email <email> - add email address to\n"
" your account\n"
"remove email <email> - remove <email> from\n"
" your account\n"
"get my emails - get a list of email\n"
" addresses attached to\n"
" your account\n"
"<uname> owes me <amount> - <uname> owes you money\n"
"i paid <uname> <amount> - you paid <uname> some\n"
" - money\n"
"i owe <uname> <amount> - you owe <uname> money\n"
"get users - get a list of users\n"
"get my dues - get who you owe and\n"
" who owes you money\n"
"help - get this help again\n"
"\n"
"Remember: Always put 'ledger' in the subject line "
"and before each command. Also, usernames must be a "
"single token. (no spaces)\n"
"\n"
"eg. ledger add me my_name\n"
"\n"
"And of course..."
" https://github.com/SudoVim/EmailLedger\n")
continue
# add me <uname>
if len(args) >= 3 and ("%s %s" % (args[0], args[1])).lower() \
== "add me":
st, msg = self.addUser(args[2], command.issuer)
self.queueMessage(command.issuer, msg)
continue
# add email <email>
if len(args) >= 3 and ("%s %s" % (args[0], args[1])).lower() \
== "add email":
st, uname = self.getUnameFromEmail(command.issuer)
if not st:
self.notAUser(command.issuer)
continue
st, msg = self.addEmail(uname, args[2])
self.queueMessage(command.issuer, msg)
continue
# remove email <email>
if len(args) >= 3 and ("%s %s" % (args[0], args[1])).lower() \
== "remove email":
st, uname = self.getUnameFromEmail(command.issuer)
if not st:
self.notAUser(command.issuer)
continue
st, msg = self.removeEmail(uname, args[2])
self.queueMessage(command.issuer, msg)
continue
# get my emails
if len(args) >= 3and ("%s %s %s" % (args[0], args[1],
args[2])).lower() == "get my emails":
st, uname = self.getUnameFromEmail(command.issuer)
if not st:
self.notAUser(command.issuer)
continue
st, msg = self.listEmails(uname)
self.queueMessage(command.issuer, msg)
continue
owesMe = False
ower = ""
owee = ""
other_party = ""
st = False
# <uname> owes me <amount>
if len(args) >= 4 and ("%s %s" % (args[1], args[2])).lower() \
== "owes me":
ower = args[0]
st, owee = self.getUnameFromEmail(command.issuer)
other_party = ower
owesMe = True
# i paid <uname> <amount>
if len(args) >= 4 and ("%s %s" % (args[0], args[1])).lower() == \
"i paid":
ower = args[2]
st, owee = self.getUnameFromEmail(command.issuer)
other_party = ower
owesMe = True
# i owe <uname> <amount>
if len(args) >= 4 and ("%s %s" % (args[0], args[1])).lower() == \
"i owe":
owee = args[2]
st, ower = self.getUnameFromEmail(command.issuer)
other_party = owee
owesMe = True
# 'owes me' and 'i paid' work the same way
if owesMe:
if not st:
self.notAUser(command.issuer)
continue
amount = float(args[3].strip("$"))
st, msg = self.addDue(ower, owee, amount)
self.queueMessage(command.issuer, msg)
if st:
self.queueMessage(self.getEmailFromUname(other_party)[1],
msg)
continue
# get users
if len(args) >= 2 and ("%s %s" % (args[0], args[1])).lower() \
== "get users":
self.queueMessage(command.issuer, self.listUsers())
continue
# get my dues
if len(args) >= 3 and ("%s %s %s" % (args[0], args[1], \
args[2])).lower() == "get my dues":
st, user = self.getUnameFromEmail(command.issuer)
if not st:
self.notAUser(command.issuer)
continue
self.queueMessage(command.issuer, self.listDues(user))
continue
self.queueMessage(command.issuer, "%s: I didn't understand you. "
"Send 'ledger help' for "
"assistance" % command.command)
self.command_queue = []
def notAUser(self, to_address):
self.queueMessage(to_address, "You are not a user.\n"
"Send 'ledger help' for assistance.")
def queueMessage(self, to_address, message):
if isinstance(to_address, list):
for addr in to_address:
self.message_list.append(Message(addr, message))
else:
self.message_list.append(Message(to_address, message))
def sendMessages(self):
if self.debug_mode:
return
# sort messages by recipient
sorted_messages = {}
for msg in self.message_list:
try:
sorted_messages[msg.recipient]
except KeyError:
sorted_messages[msg.recipient] = []
sorted_messages[msg.recipient].append(msg)
for key in sorted_messages.keys():
msg_to_send = "--------------------------\n"
for msg in sorted_messages[key]:
msg_to_send += msg.message + "\n"
msg_to_send += "--------------------------\n"
# From http://docs.python.org/library/email-examples.html
# Create a text/plain message
msg = MIMEText(msg_to_send)
me = "%[email protected]" % self.username
you = key
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'ledger'
msg['From'] = me
msg['To'] = you
server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login('%[email protected]' % self.username,self.password)
server.sendmail('%[email protected]' % self.username, you,
msg.as_string())
server.close()
self.message_list = []