/
mt_scrtry_rn.py
executable file
·694 lines (617 loc) · 31.7 KB
/
mt_scrtry_rn.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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#! /usr/bin/env python
# MailTask Alpha: The Email Manager
# Copyright (C) 2015 Patrick Simmons
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import client
import cPickle
import datetime
import email
import email.message
import email.parser
import email.utils
import icalendar
from ontask_messages import *
import os
import mt_chronos
import mt_utils
import pytz
from select import select
import socket
import shutil
import sys
import time
try:
import mt_gcal_sync
do_gcal = True
print "Google Calendar extension enabled"
except ImportError:
do_gcal = False
print "Google Calendar extension disabled"
##l_timedep_tasks: uidpath->msg
l_timedep_tasks = {}
class Msg_Dict:
def __init__(self):
##mid_dict: Format: { "Message-ID" : set(["reverse_ref_mid1", ...]), ... }
# Note: "reverse_ref_mid1" is the message ID of a message or task with Message-ID in its 'References' header.
# For tasks, since they have no message IDs, "reverse_ref_mid1" is instead a UIDPath.
self.rev_mid_dict = {}
##uid_lookup_dict: Format: { "Message-ID" : "uidpath" }
self.uid_lookup_dict = {}
##Process the 'References' header of msg and add information to rev_mid_dict and uidpath
def process_msg(self,msg,uidpath):
print "In process_msg for "+uidpath
sys.stdout.flush()
if 'Message-ID' in msg:
self.uid_lookup_dict[msg['Message-ID']]=uidpath
if 'References' in msg:
#Unlike for normal headers, we must sanitize References differently:
#They can use either a comma, a space, or a newline as a separator. Bastards.
related_mids = msg['References'].replace("\r","").replace("\n",",").replace("\t",",").replace(" ",",")
print "process_msg: related_mids: "+repr(related_mids)
sys.stdout.flush()
for rmid_ in related_mids.split(","):
rmid = rmid_.strip()
if rmid=="":
continue
print "process_msg: updating rev_mid_dict key "+rmid
sys.stdout.flush()
if rmid not in self.rev_mid_dict:
self.rev_mid_dict[rmid]=set()
print "Before: rev_mid_dict["+rmid+"]: "+repr(self.rev_mid_dict[rmid])
sys.stdout.flush()
if 'Message-ID' in msg:
self.rev_mid_dict[rmid].add(msg['Message-ID'])
else:
self.rev_mid_dict[rmid].add(uidpath)
print "After: rev_mid_dict["+rmid+"]: "+repr(self.rev_mid_dict[rmid])
sys.stdout.flush()
##Use the stale cache data to remove a deleted or modified message's references from the dictionary
def delete_msg(self,msg,uidpath):
if 'Message-ID' in msg and msg['Message-ID'] in self.uid_lookup_dict:
del self.uid_lookup_dict[msg['Message-ID']]
if 'References' in msg:
#Unlike for normal headers, we must sanitize References differently:
#They can use either a comma, a space, or a newline as a separator. Bastards.
related_mids = msg['References'].replace("\r","").replace("\n",",").replace("\t",",").replace(" ",",")
for rmid_ in related_mids.split(","):
rmid = rmid_.strip()
if rmid=="":
continue
if rmid in self.rev_mid_dict:
if 'Message-ID' in msg:
self.rev_mid_dict[rmid].discard(msg['Message-ID'])
else:
self.rev_mid_dict[rmid].discard(uidpath)
if not len(self.rev_mid_dict[rmid]):
del self.rev_mid_dict[rmid]
##handle_msg: analyze a new message and, if it's important,
# act in whatever way is appropriate
def handle_msg(uidpath,rfc822,mirror_flag):
global l_timedep_tasks
global nsync
print "In handle_msg for "+uidpath
sys.stdout.flush()
#Ignore reflections except for MID dictionary processing
if mirror_flag:
print "Found in mirror."
#Examine References header and update msg_dict
msg_dict.process_msg(email.parser.Parser().parsestr(rfc822),uidpath)
print "mirror: "+repr(nsync.mirror)
sys.stdout.flush()
return
msg = email.parser.Parser().parsestr(rfc822)
#Ignore specified accounts, unless sender in unignored_senders
if 'From' in msg and client.get_email_addr_from_header(msg['From']) not in unignored_senders and client.get_nick_from_header(msg['From']) not in unignored_senders:
for acct in ignored_accounts:
if uidpath.find(acct+"/")==0:
print "In ignored account: done."
sys.stdout.flush()
return
else:
print "Sender in unignored_senders or not in ignored account."
#We need to handle an updated Task, following protocol
if uidpath.find("Tasks")==0:
task_revised = False
if msg['X-MailTask-Completion-Status']=="Incomplete" and uidpath!="Tasks/BLACKHOLE":
if 'X-MailTask-Virgin' not in msg and (type(msg.get_payload())==str or len(msg.get_payload())==1) or 'X-MailTask-Forced-Complete' in msg:
del msg['X-MailTask-Completion-Status']
msg['X-MailTask-Completion-Status']="Completed"
task_revised = True
elif 'X-MailTask-Virgin' in msg and len(msg.get_payload())>1:
del msg['X-MailTask-Virgin']
task_revised = True
elif msg['X-MailTask-Completion-Status']=="Completed":
del msg['X-MailTask-Forced-Complete']
del msg['X-MailTask-Completion-Status']
msg['X-MailTask-Completion-Status']="Incomplete"
if 'X-MailTask-Virgin' not in msg and (type(msg.get_payload())==str or len(msg.get_payload())==1):
msg['X-MailTask-Virgin']=""
task_revised = True
elif msg['X-MailTask-Completion-Status']=="Time-Dependent":
l_timedep_tasks[uidpath]=msg
elif uidpath!="Tasks/BLACKHOLE":
print "*BUG!*: We found a Task with no or invalid X-MailTask-Completion-Status."
sys.stdout.flush()
#Examine References header and update msg_dict
msg_dict.process_msg(msg,uidpath)
#Handle Google Calendar Sync
try:
if do_gcal:
dinfo = None
gcal_id = msg['X-MailTask-GCalID']
subject_str = "MailTask: "+msg.get('Subject',"No Subject")
body_str = mt_utils.get_body(msg).get_payload()
if 'X-MailTask-Date-Info' in msg:
dinfo = mt_utils.gtstfxmdis(msg['X-MailTask-Date-Info'])
if len(dinfo)==1:
dinfo = (dinfo[0],dinfo[0])
if not gcal_id and 'X-MailTask-Date-Info' in msg:
gcal_id = mt_gcal_sync.insert_gcal_event((subject_str,body_str,dinfo[0],dinfo[1]))
msg['X-MailTask-GCalID']=gcal_id
task_revised = True
elif gcal_id and 'X-MailTask-Date-Info' not in msg:
mt_gcal_sync.delete_gcal_event(gcal_id)
del msg['X-MailTask-GCalID']
task_revised = True
elif gcal_id and 'X-MailTask-Date-Info' in msg:
mt_gcal_sync.update_gcal_event(gcal_id,(subject_str,body_str,dinfo[0],dinfo[1]))
except Exception as e:
print "WARNING: Google Calendar Error: "+e.message
#If necessary, update task
if task_revised:
nsync.node_update(uidpath,msg.as_string())
print "handle_msg: update "+uidpath+" and add to mirror"
print "mirror: "+repr(nsync.mirror)
sys.stdout.flush()
else: #not a Task: scan email headers and, if appropriate, create a new related task or update an existing one
if 'From' not in msg or 'Message-ID' not in msg or client.get_email_addr_from_header(msg['From']) in ignored_senders:
print "handle_msg: "+uidpath+" is from an ignored sender."
sys.stdout.flush()
return
#If we're doing the calendar thing, check for the codeword in the subject
if do_gcal and 'Subject' in msg and msg['Subject'].find(mt_gcal_sync.codeword)!=-1:
chronos_tuples = []
def scan_component(component):
result = mt_chronos.extract_calendar_event(component.get_payload(decode=True))
if result!=None:
chronos_tuples.append(result)
try:
mt_utils.walk_attachments(msg,scan_component)
for entry in chronos_tuples:
gcal_id = mt_gcal_sync.insert_gcal_event(entry)
newtask=email.message.Message()
nt_body = email.message.Message()
nt_body['Content-Type'] = "text/plain"
payload=entry[1]
nt_body.set_payload(payload)
mt_utils.attach_payload(newtask,nt_body)
newtask['Content-Type']="multipart/x.MailTask"
newtask['Date']=email.utils.formatdate(localtime=True)
newtask['Subject'] = entry[0]
newtask['X-MailTask-Type'] = "Checklist"
newtask['X-MailTask-Completion-Status'] = "Time-Dependent"
newtask['X-MailTask-Date-Info'] = email.utils.formatdate(timeval=entry[2],localtime=True)+" / "+email.utils.formatdate(timeval=entry[3],localtime=True)
newtask['X-MailTask-GCalID'] = gcal_id
mt_utils.set_related_ids(newtask,[msg['Message-ID']])
nsync.node_update("Tasks/NEWMESSAGE",newtask.as_string())
return
except Exception as e:
print "ERROR: Attempted to schedule calendar event and failed; probable bug."
print "The exception: "+repr(e)
sys.stdout.flush()
else:
ical_tuples = []
def scan_component(component):
try:
result = icalendar.Calendar.from_ical(component.get_payload(decode=True))
except:
return
x = result.property_items()
in_event = False
entry = ["","",0,0]
for i in range(len(x)):
try:
if len(x[i])>=2 and x[i][1]=="VEVENT":
in_event = x[i][0]=="BEGIN"
if in_event:
if x[i][0]=="SUMMARY":
entry[0] += str(x[i][1])
elif x[i][0]=="DESCRIPTION":
entry[1] += str(x[i][1])
elif x[i][0]=="DTSTART":
entry[2] = int((x[i][1].dt - datetime.datetime(1970,1,1,tzinfo=pytz.utc)).total_seconds())
elif x[i][0]=="DTEND":
entry[3] = int((x[i][1].dt - datetime.datetime(1970,1,1,tzinfo=pytz.utc)).total_seconds())
elif x[i][0]=="STATUS" and x[i][1]=="CANCELLED":
entry[0] = "CANCELED: " + entry[0]
elif entry[2] and entry[3]:
ical_tuples.append(entry)
entry = ["","",0,0]
except:
pass
try:
mt_utils.walk_attachments(msg,scan_component)
for entry in ical_tuples:
if do_gcal:
gcal_id = mt_gcal_sync.insert_gcal_event(entry)
newtask=email.message.Message()
nt_body = email.message.Message()
nt_body['Content-Type'] = "text/plain"
payload=entry[1]
nt_body.set_payload(payload)
mt_utils.attach_payload(newtask,nt_body)
newtask['Content-Type']="multipart/x.MailTask"
newtask['Date']=email.utils.formatdate(localtime=True)
newtask['Subject'] = entry[0]
newtask['X-MailTask-Type'] = "Checklist"
newtask['X-MailTask-Completion-Status'] = "Time-Dependent"
newtask['X-MailTask-Date-Info'] = email.utils.formatdate(timeval=entry[2],localtime=True)+" / "+email.utils.formatdate(timeval=entry[3],localtime=True)
if do_gcal:
newtask['X-MailTask-GCalID'] = gcal_id
mt_utils.set_related_ids(newtask,[msg['Message-ID']])
nsync.node_update("Tasks/NEWMESSAGE",newtask.as_string())
if len(ical_tuples):
return
except Exception as e:
print "ERROR: Attempted to schedule calendar event and failed; probable bug."
print "The exception: "+repr(e)
sys.stdout.flush()
#[TODO] Support
if msg['Subject'].find("[TODO]")!=-1:
print "handle_msg: TODO message found"
sys.stdout.flush()
task_fname = "Tasks/NEWMESSAGE"
task_subject = client.get_nick_from_header(msg['From']) + " TODO"
for task in nsync.cache['Tasks']:
if task[1]['Subject']==task_subject:
task_fname = "Tasks/"+task[1]['UID']
break
if task_fname=="Tasks/NEWMESSAGE":
print "handle_msg: Creating new TODO task for "+task_subject
todo_task=email.message.Message()
todo_task['Content-Type']="multipart/x.MailTask"
todo_task['Subject'] = task_subject
todo_task['X-MailTask-Type'] = "Checklist"
todo_task['X-MailTask-Completion-Status'] = "Incomplete"
task_body = email.message.Message()
task_body['Content-Type'] = "text/plain"
task_body.set_payload("TODO List\n\n")
mt_utils.attach_payload(todo_task,task_body)
todo_task['X-MailTask-Virgin'] = "Yes"
related_ids = []
else:
todo_task = email.parser.Parser().parse(open(os.path.join(client.cachedir,task_fname)))
task_body = todo_task.get_payload()[0]
related_ids = mt_utils.get_related_ids(todo_task)
del todo_task['Date']
todo_task['Date']=email.utils.formatdate(localtime=True)
payload_addition = msg.get_payload()
if type(payload_addition)!=str and len(payload_addition):
payload_addition = payload_addition[0].get_payload()
if type(payload_addition)!=str:
payload_addition = "CHECK MESSAGES"
task_body.set_payload(task_body.get_payload()+'\n'+payload_addition)
related_ids.append(msg['Message-ID'])
mt_utils.set_related_ids(todo_task,related_ids)
nsync.node_update(task_fname,todo_task.as_string())
return
#Okay, now, first check if there is a Task out there that refers to a Message-ID that is also referred to by this message
task_mid = None
if 'References' in msg:
print "handle_msg: searching "+uidpath+" related_mids for referring Task"
sys.stdout.flush()
related_mids = msg['References'].replace("\r","").replace("\n",",").replace("\t",",").replace(" ",",")
print "handle_msg: "+repr(related_mids)
sys.stdout.flush()
for rmid_ in related_mids.split(","):
rmid = rmid_.strip()
if rmid=="":
continue
print "handle_msg: examining "+rmid
sys.stdout.flush()
if rmid in msg_dict.rev_mid_dict:
print "handle_msg: msg_dict.rev_mid_dict["+rmid+"]: "+repr(msg_dict.rev_mid_dict[rmid])
sys.stdout.flush()
for referring_mid in msg_dict.rev_mid_dict[rmid]:
if referring_mid.find("Tasks/")==0:
task_mid = referring_mid
break
if task_mid!=None:
break
#We found one: update its References and Date headers and completion status.
if task_mid!=None:
print "handle_msg: Updating reference header of task "+task_mid
sys.stdout.flush()
task_msg = email.parser.Parser().parse(open(os.path.join(client.cachedir,task_mid)))
#Update related IDs
relmids = mt_utils.get_related_ids(task_msg)
relmids.append(msg['Message-ID'])
mt_utils.set_related_ids(task_msg,relmids)
#Update task_msg Date field
del task_msg['Date']
task_msg['Date']=email.utils.formatdate(localtime=True)
#Update task completion status
del task_msg['X-MailTask-Forced-Complete']
del task_msg['X-MailTask-Completion-Status']
task_msg['X-MailTask-Completion-Status']="Incomplete"
nsync.node_update(task_mid,task_msg.as_string())
else: #No existing task, so we must make one
print "handle_msg: Creating new task referencing MID "+msg['Message-ID']
sys.stdout.flush()
newtask=email.message.Message()
nt_body = email.message.Message()
nt_body['Content-Type'] = "text/plain"
payload=""
if client.get_email_addr_from_header(msg['From']) in email_info:
payload=email_info[client.get_email_addr_from_header(msg['From'])]
elif client.get_nick_from_header(msg['From']) in email_info:
payload=email_info[client.get_nick_from_header(msg['From'])]
nt_body.set_payload(payload)
mt_utils.attach_payload(newtask,nt_body)
newtask['Content-Type']="multipart/x.MailTask"
newtask['Date']=email.utils.formatdate(localtime=True)
newtask['Subject'] = client.get_nick_from_header(msg['From'])+": "+msg['Subject'] if 'Subject' in msg and 'From' in msg and client.get_nick_from_header(msg['From']) else "New Task"
newtask['X-MailTask-Type'] = "Checklist"
newtask['X-MailTask-Completion-Status'] = "Incomplete"
newtask['X-MailTask-Virgin'] = "Yes"
#GitHub's email support is broken and we have to work around it
broken_github_relid = ""
msg_relids = mt_utils.get_related_ids(msg)
for relid in msg_relids:
if relid[len(relid)-11:]=="github.com>":
broken_github_relid = relid
break
if broken_github_relid!="":
mt_utils.set_related_ids(newtask,[msg['Message-ID'],broken_github_relid])
newtask.replace_header("Subject",broken_github_relid[1:broken_github_relid.find('@')])
else:
mt_utils.set_related_ids(newtask,[msg['Message-ID']])
nsync.node_update("Tasks/NEWMESSAGE",newtask.as_string())
##Update cache and send info to server
# Returns True if any messages processed, False otherwise
# nsync.server_update_queue is a deque of pending client socket communiques
def server_synchronize():
global nsync
#select system call: I finally get to use this!
retvals = select((nsync.smessage_conn_socket,nsync.cmessage_conn_socket),(nsync.cmessage_conn_socket,),(),0)
#This is what we return: did we process messages?
messages_processed = False
#First: read from server notification socket if data is available
#These are the possible server-initiated messages:
#NODE-UPDATE-NOTIFY, CONNECT-STATUS-NOTIFY, FOLDER-INVALIDATE-NOTIFY,
#KEEPALIVE-NOTIFY, and FECC-OFF.
#We will ignore CONNECT-STATUS-NOTIFY.
if nsync.smessage_conn_socket in retvals[0]:
messages_processed = True
smessage = OnTask_Message.message_from_socket(nsync.smessage_conn)
if smessage.cmd_id=="NODE-UPDATE-NOTIFY":
#Parse message body
body = smessage.body
uidpath = body[:body.index('\n')]
modtime = int(body[(body.index('\n')+1):find_nth_substring('\n',2,body)])
rfc822 = body[find_nth_substring('\n',2,body)+1:]
print "NODE-UPDATE-NOTIFY: "+uidpath
sys.stdout.flush()
mirror_flag = nsync.mirror_remove(uidpath)
cacheadd_necessary = True
#Special case the address book
if uidpath=="ADDRESSBOOK":
os.remove(os.path.join(client.cachedir,uidpath))
open(os.path.join(client.cachedir,uidpath),'w').write(rfc822)
client.initialize_addrbook()
cacheadd_necessary = False
#Add body to cache if necessary
tokens = uidpath.rpartition('/')
if tokens[0] in nsync.cache:
for record in nsync.cache[tokens[0]]:
if record[1]["UID"]==tokens[2] and record[0] >= modtime:
cacheadd_necessary = False
break
if cacheadd_necessary:
gcal_id = None
try: #if file already exists, we need to remove it from our memory and disk caches
os.stat(os.path.join(client.cachedir,uidpath))
print "In NODE-UPDATE-NOTIFY: deleting stale cache data"
sys.stdout.flush()
msg = email.parser.Parser().parse(open(os.path.join(client.cachedir,uidpath)))
gcal_id = msg['X-MailTask-GCalID']
msg_dict.delete_msg(msg,uidpath)
nsync.remove_from_cache(uidpath)
except OSError:
pass
if rfc822!="": #only add to cache if file would not be empty
print "In NODE-UPDATE-NOTIFY: adding to cache"
sys.stdout.flush()
nsync.add_to_cache(uidpath,rfc822)
handle_msg(uidpath,rfc822,mirror_flag) #Scan message, see if it's important
elif gcal_id and do_gcal:
mt_gcal_sync.delete_gcal_event(gcal_id)
#Update last mod time
client.last_mod_time = modtime
elif smessage.cmd_id=="FOLDER-INVALIDATE-NOTIFY":
#Delete all files in cached folder
shutil.rmtree(os.path.join(client.cachedir,smessage.body)) #oh yeah this is secure yup
os.mkdir(os.path.join(client.cachedir,smessage.body))
#Clear entry in nsync.cache
nsync.cache[smessage.body] = []
elif smessage.cmd_id=="KEEPALIVE-NOTIFY":
print "Received server's keepalive-notify"
sys.stdout.flush()
nsync.keepalive_notify()
elif smessage.cmd_id=="FECC-OFF":
#Disconnect/reconnect after notifying user of bug
print "Server terminated connection and indicated protocol error:\n\n"+smessage.body
sys.stdout.flush()
raise Exception("FECC-OFF received from server")
#Send ACK message to server
nsync.smessage_conn.write(OnTask_Message("ACK","").get_message_string())
nsync.smessage_conn.flush()
if len(nsync.server_update_queue):
print "nsync.server_update_queue: "+repr(nsync.server_update_queue)
sys.stdout.flush()
#Now, handle client-initiated messages through server_update_queue.
if not len(nsync.server_update_queue):
return messages_processed #No client-initiated messages if queue empty
#Callables with f_1 in name only need cmessage_conn to be writeable.
#f_2 (and others, theoretically) need cmessage_conn ready for reading.
if repr(nsync.server_update_queue[0]).find("f_1")!=-1 and nsync.cmessage_conn_socket in retvals[1] or nsync.cmessage_conn_socket in retvals[0]:
queued_cmessage_handler = nsync.server_update_queue.popleft()
handler_retval = queued_cmessage_handler()
nsync.cmessage_conn.flush()
if handler_retval!=None:
nsync.server_update_queue.appendleft(handler_retval)
if handler_retval!=queued_cmessage_handler:
messages_processed = True
return messages_processed
def initialize_email_info_and_ignored_senders_accounts():
global email_info
global ignored_senders
global ignored_accounts
global unignored_senders
email_info={}
ignored_senders=set()
ignored_accounts=set()
unignored_senders=set()
f_email_info = open("email_info.txt")
try:
while True:
ot_m = OnTask_Message.message_from_socket(f_email_info)
email_info[ot_m.cmd_id]=ot_m.body
except:
pass
for line in map(str.rstrip,open("ignored_senders.txt").readlines()):
ignored_senders.add(line)
for line in map(str.rstrip,open("ignored_accounts.txt").readlines()):
ignored_accounts.add(line)
for line in map(str.rstrip,open("unignored_senders.txt").readlines()):
unignored_senders.add(line)
def do_timedep_check():
for entry in l_timedep_tasks.items():
tasktype = mt_utils.get_task_type(entry[1])
current_time = time.time()
if tasktype=="Deadline" and current_time > email.utils.mktime_tz(email.utils.parsedate_tz(entry[1]['X-MailTask-Date-Info'])) or tasktype=="Meeting" and current_time > email.utils.mktime_tz(email.utils.parsedate_tz(entry[1]['X-MailTask-Date-Info'].split("/")[1].strip())):
del entry[1]['X-MailTask-Completion-Status']
entry[1]['X-MailTask-Completion-Status']="Completed"
nsync.node_update(entry[0],entry[1].as_string())
del l_timedep_tasks[entry[0]]
def main():
global msg_dict
global nsync
global l_timedep_tasks
#Name of program and intro text
print "MailTask Alpha: The Email Manager"
print "(c) 2015 by Patrick Simmons"
print
print "This program is free software: you can redistribute it and/or modify"
print "it under the terms of the GNU General Public License as published by"
print "the Free Software Foundation, either version 3 of the License, or"
print "(at your option) any later version."
print "This program is distributed in the hope that it will be useful,"
print "but WITHOUT ANY WARRANTY; without even the implied warranty of"
print "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
print "GNU General Public License for more details."
print
print "Libraries used:"
print "html2text (c) 2012 Aaron Swartz (GPLv3)"
print "requests_structures.py (c) The Requests Project (original Apachev2; any modifications GPLv3)"
print
print "This program is dedicated to the memory of Aaron Swartz."
#Socket Default Timeout
socket.setdefaulttimeout(600)
client.c_state = client.ClientState() #I no longer think this is needed, but just in case.
client.cachedir = os.path.abspath(".")
client.initialize_account_info()
settings = open(os.path.join(client.cachedir,"settings"))
client.password = settings.readline().rstrip()
if do_gcal:
mt_gcal_sync.codeword = settings.readline().rstrip()
try:
client.last_mod_time = int(settings.readline().rstrip())
except:
print "WARNING: no mod time in settings file; using 1/1/1970."
client.last_mod_time = 0
msg_dict = Msg_Dict()
nsync = client.ClientNetSync()
#Local function to add all files in a folder to cache
def folder_cache_add(folder,process_instead=False):
if not process_instead:
nsync.cache[folder]=[]
for fname in os.listdir(os.path.join(client.cachedir,folder)):
if not process_instead:
nsync.add_to_cache(folder+"/"+fname,open(os.path.join(client.cachedir,folder+"/"+fname)).read(),False)
else:
msg_dict.process_msg(email.parser.Parser().parse(open(os.path.join(client.cachedir,folder+"/"+fname))),folder+"/"+fname)
#If we have a pickle, use it; otherwise, read in all emails and parse them (slow)
if os.path.isfile(client.cachedir+"/client.pickle"):
nsync.cache = cPickle.load(open(client.cachedir+"/client.pickle","rb"))
if os.path.isfile(client.cachedir+"/l_timedep_tasks.pickle"):
l_timedep_tasks = cPickle.load(open(client.cachedir+"/l_timedep_tasks.pickle","rb"))
else:
#Add all account folders to cache
for x in range(len(client.account_info)):
folder_cache_add(repr(x)+"/INBOX")
folder_cache_add(repr(x)+"/Sent")
folder_cache_add("Tasks")
if os.path.isfile(client.cachedir+"/msg_dict.pickle"):
msg_dict = cPickle.load(open(client.cachedir+"/msg_dict.pickle","rb"))
else:
for x in range(len(client.account_info)):
folder_cache_add(repr(x)+"/INBOX",True)
folder_cache_add(repr(x)+"/Sent",True)
folder_cache_add("Tasks",True)
nsync.initialize_cache()
if do_gcal:
mt_gcal_sync.initialize()
#email_info and ignored_senders
initialize_email_info_and_ignored_senders_accounts()
def dump_data():
cPickle.dump(nsync.cache,open(client.cachedir+"/client.pickle","wb"),cPickle.HIGHEST_PROTOCOL)
cPickle.dump(msg_dict,open(client.cachedir+"/msg_dict.pickle","wb"),cPickle.HIGHEST_PROTOCOL)
cPickle.dump(l_timedep_tasks,open(client.cachedir+"/l_timedep_tasks.pickle","wb"),cPickle.HIGHEST_PROTOCOL)
settings = open(os.path.join(client.cachedir,"settings"),'w')
settings.write(client.password+"\n")
if do_gcal:
settings.write(mt_gcal_sync.codeword+"\n")
settings.write(repr(client.last_mod_time)+"\n")
#Main loop
last_timedep_check=0
startup_status=2
try:
while True:
while server_synchronize():
if len(nsync.server_update_queue) > 50 and (startup_status==0 or startup_status==2) or len(nsync.server_update_queue) < 40 and startup_status==1:
startup_status+=1
if os.path.isfile(client.cachedir+"/FORCE_EXIT") or startup_status==3:
dump_data()
if startup_status==3:
print "FATAL: We appear to have lost our cmessage connection."
sys.exit(0)
time.sleep(0.1)
time.sleep(5)
if time.time()-last_timedep_check > 600:
print "Performing timed pickle backup."
sys.stdout.flush()
do_timedep_check()
last_timedep_check=time.time()
if os.path.isfile(client.cachedir+"/client.pickle"):
shutil.copyfile(client.cachedir+"/client.pickle",client.cachedir+"/client.pickle.bak")
if os.path.isfile(client.cachedir+"/msg_dict.pickle"):
shutil.copyfile(client.cachedir+"/msg_dict.pickle",client.cachedir+"/msg_dict.pickle.bak")
if os.path.isfile(client.cachedir+"/l_timedep_tasks.pickle"):
shutil.copyfile(client.cachedir+"/l_timedep_tasks.pickle",client.cachedir+"/l_timedep_tasks.pickle.bak")
dump_data()
except Exception as e:
dump_data()
print "Failed with exception: "+repr(e)
sys.stdout.flush()
if __name__=="__main__":
main()