-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathemail_thread.py
More file actions
479 lines (405 loc) · 15.4 KB
/
email_thread.py
File metadata and controls
479 lines (405 loc) · 15.4 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import time
import email
import imaplib
import poplib_2_4 as poplib
import smtplib
class ServerError(Exception):
pass
class UserPasswordError(Exception):
pass
class EmailServerObject:
def __init__(self, serverDict = None):
self._isInitialized = False
self.setServerInfo( serverDict )
def _pingServer(self):
raise NotImplementedError()
def getServerInfo(self):
return self._serverDict
def setServerInfo(self, newDict = None):
self._serverDict = newDict
try:
self._initializeServer()
except ServerError():
pass
#except:
#return False
return True
def getMail(self):
if self._serverDict.has_key('mailboxes'):
mailboxes = self._serverDict['mailboxes']
else:
mailboxes = ['INBOX']
messages = []
for i in mailboxes:
messages += self._getMail( i )
return messages
def _isServerInfoValid(self):
raise NotImplementedError()
def _initializeServer(self):
raise NotImplementedError()
def releaseServer(self):
raise NotImplementedError()
def _getMail(self, mailBox = None):
raise NotImplementedError()
def _getNewMail(self, sinceTime, mailBox):
raise NotImplementedError()
def keepAlive(self):
raise NotImplementedError()
class IMAPEmail(EmailServerObject):
def _isServerInfoValid(self):
pass
#raise NotImplementedError()
def _initializeServer(self):
if self._serverDict == None:
raise Exception("No server to initialize!")
if not self._isServerInfoValid:
raise Exception("Server info is not valid!")
if self._isInitialized:
self.releaseServer()
if self._serverDict.has_key('SSL') and self._serverDict['SSL']:
IMAPabstract = imaplib.IMAP4_SSL
else:
IMAPabstract = imaplib.IMAP4
server = self._serverDict['server']
if self._serverDict.has_key( 'port' ):
port = self._serverDict['port']
try:
newIMAP = IMAPabstract( server , port )
except:
#print "exception", 1
raise ServerError()
else:
try:
newIMAP = IMAPabstract( server )
except:
#print "exception", 2
raise ServerError()
self._IMAP = newIMAP
try:
self._IMAP.login( self._serverDict['user'], self._serverDict['password'] )
except:
#print "exception",3
raise UserPasswordError()
self._isInitialized = True
def releaseServer(self):
if self._isInitialized:
self._IMAP.close()
self._IMAP.logout()
del self._IMAP
self._isIntialized = False
def _getMail(self, mailBox = None):
if mailBox == None:
mailBox = "INBOX"
if not self._isInitialized:
try:
self._initializeServer()
except:
#print "exception",4
return None
if self._serverDict.has_key('last checked time'):
return self._getNewMail( self._serverDict['last checked time'], mailBox)
else:
return self._getNewMail( imaplib.Time2Internaldate( time.gmtime(0) ), mailBox)
def _getNewMail(self, sinceTime, mailBox):
self._IMAP.select( mailBox )
toSearchFor = sinceTime[:12]+sinceTime[-1:]
#if toSearchFor == imaplib.Time2Internaldate( time.localtime())[:12] +'"':
if 1:
toSearchFor = "UNSEEN"
else:
toSearchFor = "(SINCE "+toSearchFor + ")"
typ, data = self._IMAP.search( None, toSearchFor )
messages = []
for num in data[0].split():
typ, data = self._IMAP.fetch(num, 'RFC822')
message = email.message_from_string( data[0][1] )
messages.append(message)
messages.reverse()
self._serverDict['last checked time'] = imaplib.Time2Internaldate( time.localtime() )
return messages
def keepAlive(self):
if self._isInitialized:
try:
self._IMAP.noop()
except:
#print "exception",5
self._isInitialized = False
class POPemail(EmailServerObject):
def _initializeServer(self):
if self._serverDict == None:
raise Exception("No server to initialize!")
if not self._isServerInfoValid:
raise Exception("Server info is not valid!")
if self._isInitialized:
self.releaseServer()
if self._serverDict.has_key('SSL') and self._serverDict['SSL']:
POPabstract = poplib.POP3_SSL
else:
POPabstract = poplib.POP3
server = self._serverDict['server']
if self._serverDict.has_key( 'port' ):
port = self._serverDict['port']
try:
newPOP = POPabstract( server , port )
except:
#print "exception",6
raise ServerError()
else:
try:
newPOP = POPabstract( server )
except:
#print "exception",7
raise ServerError()
self._POP = newPOP
try:
self._POP.user( self._serverDict['user'] )
self._POP.pass_( self._serverDict['password'])
except:
#print "exception",8
raise UserPasswordError()
self._isInitialized = True
def releaseServer(self):
if self._isInitialized:
self._POP.quit()
self._isIntialized = False
def _getMail(self, noPOPmailboxes):
if not self._isInitialized:
try:
self._initializeServer()
except:
#print "failed server start-POP"
return None
return self._getNewMail()
def _getNewMail(self):
messages = []
for i in range( len(self._POP.list()[1]) ):
#print "another message"
rawText = ""
for j in self._POP.retr(i+1)[1]:
rawText += (j+"\n")
messages.append( email.message_from_string(rawText) )
return messages
def keepAlive(self):
if self._isInitialized:
try:
self._POP.noop()
except:
#print "exception",9
self._isInitialized = False
class SMTPserver:
def __init__(self, serverDict = None):
self._isInitialized = False
self.setServerInfo( serverDict )
def _pingServer(self):
raise NotImplementedError()
def getServerInfo(self):
return self._serverDict
def setServerInfo(self, newDict = None):
self._serverDict = newDict
try:
self._initializeServer()
except ServerError():
pass
#except:
#return False
return True
def _isServerInfoValid(self):
return ( self._serverDict.has_key('From') and self._serverDict.has_key('server') )
def _initializeServer(self):
if self._serverDict == None:
raise Exception("No server to initialize!")
if not self._isServerInfoValid:
raise Exception("Server info is not valid!")
if self._isInitialized:
self._releaseServer()
#print self._serverDict
server = self._serverDict['server']
if self._serverDict.has_key( 'port' ):
port = self._serverDict['port']
try:
newSMTP = smtplib.SMTP( server , port )
except:
#print "exception",10
raise ServerError()
else:
try:
newSMTP = smtplib.SMTP( server )
except:
#print "exception",11
raise
raise ServerError()
self._SMTP = newSMTP
if self._serverDict.has_key('TLS') and self._serverDict['TLS'] == True:
try:
self._SMTP.ehlo()
self._SMTP.starttls()
self._SMTP.ehlo()
except:
#print "exception",12
raise ServerError()
if self._serverDict.has_key('authentication') and self._serverDict['authentication']:
try:
self._SMTP.login( self._serverDict['user'], self._serverDict['password'] )
except:
#print "exception",13
raise #UserPasswordError()
self._isInitialized = True
def _releaseServer(self):
if self._isInitialized:
self._SMTP.quit()
del self._SMTP
self._isInitialized = False
def sendMessage(self, msg):
msg['From'] = self._serverDict['From']
if not self._verifyMessage(msg):
raise Exception("Not a valid message.")
messageText = msg.as_string()
try:
self._initializeServer()
except:
#print "exception",14
#TO DO: use the standard exception
raise
#raise Exception("Could not access SMTP server.")
try:
print self._SMTP.sendmail( msg['From'], msg['To'], messageText )
except:
#TO DO: not sure what to do here
raise
try:
self._releaseServer()
except:
#TO DO: this should eventually be a pass
raise
def _constructText(self, msg):
messageText = ""
for i in msg:
if msg[i] <> 'body':
messageText += (i+": " + msg[i] + "\r\n")
messageText += msg['body']
return messageText
def _verifyMessage(self, msg):
#TO DO: make sure that the address makes sense.
return (msg.has_key('From') and msg.has_key('To'))
import threading
global incoming_email_lock,incoming_email,outgoing_email_lock,outgoing_email
incoming_email_lock = threading.Lock()
incoming_email = []
outgoing_email_lock = threading.Lock()
outgoing_email = []
global server_settings_changed,check_mail_now
server_settings_changed = threading.Event()
check_mail_now = threading.Event()
global incoming_servers_lock,incoming_servers,outgoing_servers_lock,outgoing_servers
incoming_servers_lock = threading.Lock()
incoming_servers = []
outgoing_servers_lock = threading.Lock()
outgoing_servers = []
is_incoming_mail = threading.Event()
is_outgoing_mail = threading.Event()
global system_quit
system_quit = threading.Event()
system_quit.clear()
class IncomingEmailThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global incoming_servers, incoming_servers_lock
incoming_servers_lock.acquire()
self._servers = incoming_servers
incoming_servers_lock.release()
self._settingsChanged = True
self._serverLogonFailed = []
self._serverObjects = []
def run(self):
while 1:
global server_settings_changed, system_quit
if system_quit.isSet():
break
#TO DO: this is a very inefficient way of doing this, but very stable; maybe a more elegant method?
if server_settings_changed.isSet():
global incoming_servers, incoming_servers_lock
incoming_servers_lock.acquire()
self._servers = incoming_servers
server_settings_changed.clear()
incoming_servers_lock.release()
for i in range( len(self._serverObjects) ):
try:
self._serverObjects[i].releaseServer()
except:
#print "exception", 20
pass
del self._serverObjects[i]
for j in self._servers:
try:
if j['protocol'] == 'IMAP':
self._serverObjects.append( IMAPEmail(j) )
if j['protocol'] == 'POP':
self._serverObjects.append( POPemail(j) )
self._serverLogonFailed.append( False )
except:
self._serverLogonFailed.append( True )
newMessages = []
for j in self._serverObjects:
newMessages += j.getMail()
print "received ", len(newMessages)," messages"
global is_incoming_mail
if len(newMessages) > 0:
global incoming_email, incoming_email_lock
incoming_email_lock.acquire()
incoming_email += newMessages
incoming_email_lock.release()
is_incoming_mail.set()
global check_mail_now
check_mail_now.wait(30)
print "receivemail thread woken up"
if check_mail_now.isSet():
check_mail_now.clear()
#for i in self._serverObjects:
#i.keepAlive()
class OutgoingEmailThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._settingsChanged = True
self._serverObjects = []
def run(self):
newMessages = []
while 1:
global server_settings_changed, is_outgoing_mail, system_quit
if system_quit.isSet():
break
global outgoing_servers, outgoing_servers_lock
global outgoing_email, outgoing_email_lock
is_outgoing_mail.wait(180)
print 'sendmail thread woken up'
outgoing_email_lock.acquire()
if len(outgoing_email) > 0:
newMessages = outgoing_email
outgoing_email = []
else:
newMessages = []
outgoing_email_lock.release()
if len(newMessages) > 0:
outgoing_servers_lock.acquire()
self._servers = outgoing_servers
outgoing_servers_lock.release()
newServer = None
for j in self._servers:
try:
newServer = SMTPserver( j )
break
except:
pass
if newServer:
for i in newMessages:
try:
newServer.sendMessage(i)
except Exception, e:
outgoing_email.append( i )
else:
outgoing_email_lock.acquire()
outgoing_email.extend( newMessages )
outgoing_email_lock.release()
is_outgoing_mail.clear()
print outgoing_email
incomingThread = IncomingEmailThread()
outgoingThread = OutgoingEmailThread()