-
Notifications
You must be signed in to change notification settings - Fork 0
/
imapplugin.py
1032 lines (706 loc) · 33.8 KB
/
imapplugin.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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2018 Andreas Schmidt
# 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 acthex
import imaplib
import sys
import email
import email.message
def login(connection_id, host, port, user, password, ssl):
"""
Establish a connection to the specified IMAP server and logs into the specified email account.
:param connection_id: term identifying the IMAP server to connect to
:param host: string containing the IMAP server's hostname or IP address
:param port: integer representing the IMAP server's port
:param user: string containing the email account's user name
:param password: string containing the email account's password
:param ssl: iff 0, the connection is not encrypted
:return:
"""
print("called @login("+ str(connection_id) +", "+ str(host)+", "+ str(port)+", "+ str(user)+", "+ str(password)+", "+ str(ssl)+")")
connection_id = get_term("connection_id", connection_id, True)
host = get_string_from_term("host", host, True)
port = get_int_from_term("port", port, True)
user = get_string_from_term("user", user, True)
password = get_string_from_term("password", password, True)
ssl = get_term("ssl", ssl, True)
if ssl == "0": ssl = False
else: ssl = True
acthex.environment().login(connection_id, host, port, user, password, ssl)
def logged_in(connection_id):
"""
True if there is a connection to an IMAP server identified by connection_id.
:param connection_id: term identifying the IMAP server connection
:return:
"""
print("called &logged_in("+ str(connection_id)+")")
connection_id = get_term("connection_id", connection_id, False)
if acthex.environment().logged_in(connection_id):
print("logged in")
acthex.output(())
else: print("not logged in")
def filter(connection_id, mailbox, filter):
"""
Return the message IDs of the messages that match the given filter
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing the mailbox name
:param filter: string containing a filter
:return:
"""
print("called &filter("+ str(connection_id)+", "+ str(mailbox)+", "+ str(filter)+")")
connection_id = get_term("connection_id", connection_id, False)
mailbox = get_string_from_term("mailbox", mailbox, False)
filter = get_string_from_term("filter", filter, False)
filter = filter.replace("'", '"')
messages = acthex.environment().filter(connection_id, mailbox, filter)
if messages != None:
print("found message_ids " + str(messages))
for message in messages:
acthex.output((message,))
def msg_header(connection_id, mailbox, message_id):
"""
Return pairs (type, value) for each header information of the specified message.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing the mailbox name
:param message_id: integer representing the message ID
:return:
"""
print("called &msg_header(" + str(connection_id)+", "+ str(mailbox)+", "+ str(message_id)+")")
connection_id = get_term("connection_id", connection_id, False)
mailbox = get_string_from_term("mailbox", mailbox, False)
message_id = get_int_from_term("message_id", message_id, False)
headers = acthex.environment().msg_header(connection_id, mailbox, message_id)
if headers != None:
print("found headers", end=" ")
for type, value in headers:
type = type.replace("-", "")
print("("+str(type.lower())+', "'+str(value)+'")', end=" ")
acthex.output((type.lower(), '"'+value+'"'))
print()
def msg_body(connection_id, mailbox, message_id):
"""
Return the body of the specified message.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing the mailbox name
:param message_id: integer representing the message ID
:return:
"""
print("called &msg_body("+ str(connection_id)+", "+ str(mailbox)+", "+ str(message_id)+")")
connection_id = get_term("connection_id", connection_id, False)
mailbox = get_string_from_term("mailbox", mailbox, False)
message_id = get_int_from_term("message_id", message_id, False)
body = acthex.environment().msg_body(connection_id, mailbox, message_id)
if body != None:
body = body.replace("\r\n", "\n")
print("found " + '"'+str(body.replace("\\", "\\\\"))+'"')
acthex.output(('"'+body+'"',))
def mailbox(connection_id, path, name):
"""
Return each mailbox name in the given path, matching the given name.
:param connection_id: term identifying the IMAP server connection
:param path: string containing a path
:param name: string containing a mailbox name
:return:
"""
print("called &mailbox("+ str(connection_id)+", "+ str(path)+", "+ str(name)+")")
connection_id = get_term("connection_id", connection_id, False)
path = get_string_from_term("path", path, False)
name = get_string_from_term("name", name, False)
mailboxes = acthex.environment().mailbox(connection_id, path, name)
if mailboxes is not None:
print("found mailboxes", end=" ")
for mailbox in mailboxes:
print('"'+str(mailbox)+'"' , end=" ")
acthex.output(('"'+mailbox+'"', ))
print()
def set_flag(connection_id, mailbox, message_id, flag):
"""
Set or unset the given flag for the specified message.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing a mailbox name
:param message_id: integer representing the message ID
:param flag: the name of the flag (without leading backslash) with a leading "not " iff the flag should be unset
:return:
"""
print("called @set_flag("+ str(connection_id)+", "+ str(mailbox)+", "+ str(message_id)+", "+ str(flag)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
message_id = get_int_from_term("message_id", message_id, True)
flag = get_string_from_term("flag", flag, True)
flag = flag.split(" ")
if len(flag) == 2 and flag[0] == "not":
acthex.environment().set_flag(connection_id, mailbox, message_id, "\\" + flag[1], False)
elif len(flag) == 1:
acthex.environment().set_flag(connection_id, mailbox, message_id, "\\" + flag[0], True)
else:
print("Error: term flag of action atom set_flag is invalid", file=sys.stderr)
return
def copy_msg(connection_id, mailbox, message_id, new_mailbox):
"""
Copy the specified message to the specified mailbox.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing a mailbox name
:param message_id: integer representing the message ID
:param new_mailbox: string containing a mailbox name
:return:
"""
print("called @copy_msg("+ str(connection_id)+", " +str(mailbox)+", " +str(message_id)+", " +str(new_mailbox)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
message_id = get_int_from_term("message_id", message_id, True)
new_mailbox = get_string_from_term("new_mailbox", new_mailbox, True)
acthex.environment().copy_msg(connection_id, mailbox, message_id, new_mailbox)
def create_msg(connection_id, mailbox, sender, subject, recipients, ccs, bccs, text):
"""
Create a message with the given sender, subject, recipients and text.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing a mailbox name
:param sender:
:param subject:
:param recipients:
:param ccs:
:param bccs:
:param text:
:return:
"""
print("called @create_msg("+ str(connection_id)+", " +str(mailbox)+", "+ str(sender)+", "+ str(subject)+", "+ str(recipients)+", "+ str(ccs)+", "+ str(bccs)+", " +str(text)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
sender = get_string_from_term("sender", sender, True)
subject = get_string_from_term("subject", subject, True)
recipients = get_list_from_function("to", recipients, True)
ccs = get_list_from_function("cc", ccs, True)
bccs = get_list_from_function("bcc", bccs, True)
text = get_string_from_term("text", text, True)
acthex.environment().create_msg(connection_id, mailbox, sender, subject, recipients, ccs, bccs, text)
def move_msg(connection_id, mailbox, message_id, new_mailbox):
"""
Move the specified message to the specified mailbox.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing a mailbox name
:param message_id: integer representing the message ID
:param new_mailbox: string containing a mailbox name
:return:
"""
print("called @move_msg("+ str(connection_id)+", " +str(mailbox)+", "+ str(message_id)+", "+ str(new_mailbox)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
message_id = get_int_from_term("message_id", message_id, True)
new_mailbox = get_string_from_term("new_mailbox", new_mailbox, True)
acthex.environment().move_msg(connection_id, mailbox, message_id, new_mailbox)
def expunge_msgs(connection_id, mailbox):
"""
Exunge all messages marked as deleted in the specified mailbox.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing a mailbox name
:return:
"""
print("called @expunge_mailbox("+ str(connection_id)+", "+ str(mailbox)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
acthex.environment().expunge_msgs(connection_id, mailbox)
def create_mailbox(connection_id, mailbox):
"""
Create a new mailbox.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing the name of the new mailbox
:return:
"""
print("called @create_mailbox("+ str(connection_id)+", "+ str(mailbox)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
acthex.environment().create_mailbox(connection_id, mailbox)
def delete_mailbox(connection_id, mailbox):
"""
Delete the specified mailbox.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing the mailbox name
:return:
"""
print("called @delete_mailbox("+ str(connection_id)+", "+ str(mailbox)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
acthex.environment().delete_mailbox(connection_id, mailbox)
def rename_mailbox(connection_id, mailbox, new_mailbox):
"""
Rename the specified mailbox.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing the mailbox name
:param new_mailbox: string containing the new mailbox name
:return:
"""
print("called @rename_mailbox("+ str(connection_id)+", "+ str(mailbox)+", "+str(new_mailbox)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
new_mailbox = get_string_from_term("new_mailbox", new_mailbox, True)
acthex.environment().rename_mailbox(connection_id, mailbox, new_mailbox)
def reply(connection_id, mailbox, message_id, text):
"""
Create a draft of a reply message to a specified message.
:param connection_id: term identifying the IMAP server connection
:param mailbox: string containing the mailbox name
:param message_id: integer representing the message ID of the message to reply to
:param text: string containing the text of the reply message
:return:
"""
print("called @reply("+ str(connection_id)+", "+ str(mailbox)+", " +str(message_id)+", "+ str(text)+")")
connection_id = get_term("connection_id", connection_id, True)
mailbox = get_string_from_term("mailbox", mailbox, True)
message_id = get_int_from_term("message_id", message_id, True)
text = get_string_from_term("text", text, True)
acthex.environment().reply(connection_id, mailbox, message_id, text)
def register():
acthex.setEnvironment(ImapEnvironment())
acthex.addAction('login', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT))
acthex.addAtom('logged_in', (acthex.CONSTANT,), 0)
acthex.addAtom('filter', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT), 1)
acthex.addAtom('msg_header', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT), 2)
acthex.addAtom('msg_body', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT), 1)
acthex.addAtom('mailbox', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT), 1)
acthex.addAction('set_flag', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT))
acthex.addAction('copy_msg', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT))
acthex.addAction('create_msg', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT))
acthex.addAction('move_msg', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT))
acthex.addAction('expunge_msgs', (acthex.CONSTANT, acthex.CONSTANT))
acthex.addAction('create_mailbox', (acthex.CONSTANT, acthex.CONSTANT))
acthex.addAction('delete_mailbox', (acthex.CONSTANT, acthex.CONSTANT))
acthex.addAction('rename_mailbox', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT))
acthex.addAction('reply', (acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT, acthex.CONSTANT))
class ImapEnvironment(acthex.Environment):
connections = []
def login(self, id, host, port, user, password, ssl):
for connection in self.connections:
if connection.get_id() == id:
connection.logout()
self.connections.remove(connection)
print("logged out from connection '" + id + "'")
break
self.connections.append(Connection(id, host, port, user, password, ssl))
def logged_in(self, id):
for connection in self.connections:
if connection.get_id() == id:
return True
return False
def filter(self, id, mailbox, filter):
for connection in self.connections:
if connection.get_id() == id:
return connection.filter(mailbox, filter)
def msg_header(self, id, mailbox, message_id):
for connection in self.connections:
if connection.get_id() == id:
return connection.msg_header(mailbox, message_id)
def msg_body(self, id, mailbox, message_id):
for connection in self.connections:
if connection.get_id() == id:
return connection.msg_body(mailbox, message_id)
def mailbox(self, id, path, name):
for connection in self.connections:
if connection.get_id() == id:
return connection.mailbox(path, name)
def set_flag(self, id, mailbox, message_id, flag, add):
for connection in self.connections:
if connection.get_id() == id:
connection.set_flag(mailbox, message_id, flag, add)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
def copy_msg(self, id, mailbox, message_id, new_mailbox):
for connection in self.connections:
if connection.get_id() == id:
connection.copy_msg(mailbox, message_id, new_mailbox)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
def create_msg(self, id, mailbox, sender, subject, recipients, ccs, bccs, text):
for connection in self.connections:
if connection.get_id() == id:
connection.create_msg(mailbox, sender, subject, recipients, ccs, bccs, text)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
def move_msg(self, id, mailbox, message_id, new_mailbox):
for connection in self.connections:
if connection.get_id() == id:
connection.move_msg(mailbox, message_id, new_mailbox)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
def expunge_msgs(self, id, mailbox):
for connection in self.connections:
if connection.get_id() == id:
connection.expunge_msgs(mailbox)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
def create_mailbox(self, id, mailbox):
for connection in self.connections:
if connection.get_id() == id:
connection.create_mailbox(mailbox)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
def delete_mailbox(self, id, mailbox):
for connection in self.connections:
if connection.get_id() == id:
connection.delete_mailbox(mailbox)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
def rename_mailbox(self, id, mailbox, new_mailbox):
for connection in self.connections:
if connection.get_id() == id:
connection.rename_mailbox(mailbox, new_mailbox)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
def reply(self, id, mailbox, message_id, text):
for connection in self.connections:
if connection.get_id() == id:
connection.reply(mailbox, message_id, text)
return
print("Error: connection '" + id + "' not found", file=sys.stderr)
class Connection:
__id = None
__connection = None #type IMAP4 or IMAP4_SSL
__user = None
__selected_mailbox = None
def __init__(self, id, host, port, user, password, ssl):
self.__id = id
self.__user = user
try:
if ssl: self.__connection = imaplib.IMAP4_SSL(host, port)
else: self.__connection = imaplib.IMAP4(host, port)
except:
print("Error: connecting to server failed", file=sys.stderr)
sys.exit(1)
try:
t, data = self.__connection.login(user, password)
except:
print("Error: login failed", file = sys.stderr)
sys.exit(1)
if t == "NO":
print("Error: login failed", file = sys.stderr)
sys.exit(1)
print("established connection successfully")
def get_id(self):
return self.__id
def logout(self):
t,data = self.__connection.logout()
if t != "BYE":
print("Error: logout failed", file=sys.stderr)
sys.exit(1)
def select_mailbox(self, mailbox):
if self.__selected_mailbox != mailbox:
try:
t, data = self.__connection.select('"'+mailbox+'"')
if t == "NO":
print("Error: selecting mailbox failed", file=sys.stderr)
return False
self.__selected_mailbox = mailbox
except:
print("Error: selecting mailbox failed", file=sys.stderr)
return False
return True
def filter(self, mailbox, filter):
if not self.select_mailbox(mailbox): return
try:
t, data = self.__connection.search(None, filter)
if t == "NO":
print("Error: searching for messages failed", file=sys.stderr)
return data[0].decode().split()
except:
print("Error: searching for messages failed", file=sys.stderr)
def msg_header(self, mailbox, message_id):
if not self.select_mailbox(mailbox): return
try:
t, data = self.__connection.fetch(str(message_id), '(RFC822)')
if t == "NO":
print("Error: fetching message headers failed", file=sys.stderr)
return
try:
msg = email.message_from_string(data[0][1].decode())
except:
print("Error: fetching message headers failed", file=sys.stderr)
return
return msg.items()
except:
print("Error: fetching message headers failed", file=sys.stderr)
def msg_body(self, mailbox, message_id):
if not self.select_mailbox(mailbox): return
try:
t, data = self.__connection.fetch(str(message_id), '(RFC822)')
if t == "NO":
print("Error: fetching message body failed", file=sys.stderr)
return
email_message = email.message_from_string(data[0][1].decode())
for part in email_message.walk():
if part.get_content_type() == "text/plain":
return part.get_payload()
except:
print("Error: fetching message body failed", file=sys.stderr)
def mailbox(self, path, name):
try:
t, data = self.__connection.list('"'+path+'"', '"'+name+'"')
if t == "NO":
print("Error: listing mailboxes failed", file=sys.stderr)
return
mailboxes = []
if data == [None]: return mailboxes
for m in data:
print("got", m)
try:
m = m.decode()
except:
print("Error: unexpected IMAP server response", file=sys.stderr)
return
if len(m) == 0:
print("Error: unexpected IMAP server response", file=sys.stderr)
return
if m[-1] == '"':
m = m[:-1]
i = m[:-1].rfind('"')
if i == -1 or len(m) <= i+1:
print("Error: unexpected IMAP server response", file=sys.stderr)
return
mailboxes.append(m[i+1:])
else:
m = m.split()
if len(m) == 0:
print("Error: unexpected IMAP server response", file=sys.stderr)
return
mailboxes.append(m[-1])
return mailboxes
except:
print("Error: listing mailboxes failed", file=sys.stderr)
return
def set_flag(self, mailbox, message_id, flag, add):
if not self.select_mailbox(mailbox): sys.exit(1)
try:
t, data = self.__connection.store(str(message_id), "+FLAGS" if add else "-FLAGS", flag)
if t == "NO":
print("Error: storing message flag failed", file=sys.stderr)
sys.exit(1)
except:
print("Error: storing message flag failed", file=sys.stderr)
sys.exit(1)
def copy_msg(self, mailbox, message_id, new_mailbox):
if not self.select_mailbox(mailbox): sys.exit(1)
try:
t, data = self.__connection.copy(str(message_id), new_mailbox)
if t == "NO":
print("Error: copying message failed", file=sys.stderr)
sys.exit(1)
except:
print("Error: copying message failed", file=sys.stderr)
sys.exit(1)
def create_msg(self, mailbox, sender, subject, recipients, ccs, bccs, text):
try:
msg = email.message.Message()
msg.add_header("From", sender)
msg.add_header("Subject", subject)
for recipient in recipients:
msg.add_header("To", recipient)
for cc in ccs:
msg.add_header("Cc", cc)
for bcc in bccs:
msg.add_header("Bcc", bcc)
msg.set_payload(text)
t, data = self.__connection.append(mailbox, "", "", msg.as_string().encode())
if t == "NO":
print("Error: creating message failed", file=sys.stderr)
sys.exit(1)
except:
print("Error: creating message failed", file = sys.stderr)
sys.exit(1)
def move_msg(self, mailbox, message_id, new_mailbox):
if not self.select_mailbox(mailbox):
sys.exit(1)
try:
t, data = self.__connection.copy(str(message_id), new_mailbox)
if t == "NO":
print("Error: moving message failed (message could not be copied)", file=sys.stderr)
sys.exit(1)
try:
t, data = self.__connection.store(str(message_id), "+FLAGS", "\\Deleted")
if t == "NO":
print("Error: moving message failed (message could be copied, but not deleted)", file=sys.stderr)
sys.exit(1)
except:
print("Error: moving message failed (message could be copied, but not deleted)", file=sys.stderr)
sys.exit(1)
except:
print("Error: moving message failed (message could not be copied)", file=sys.stderr)
sys.exit(1)
def expunge_msgs(self, mailbox):
if not self.select_mailbox(mailbox): sys.exit(1)
try:
t, data = self.__connection.expunge()
if t == "NO":
print("Error: expunging all messages failed", file=sys.stderr)
sys.exit(1)
except:
print("Error: expunging all messages failed", file=sys.stderr)
sys.exit(1)
def create_mailbox(self, mailbox):
try:
t, data = self.__connection.create(mailbox)
if t == "NO":
print("Error: creating mailbox failed", file=sys.stderr)
sys.exit(1)
except:
print("Error: creating mailbox failed", file=sys.stderr)
sys.exit(1)
def delete_mailbox(self, mailbox):
try:
t, data = self.__connection.delete(mailbox)
if t == "NO":
print("Error: deleting mailbox failed", file=sys.stderr)
sys.exit(1)
except:
print("Error: deleting mailbox failed", file=sys.stderr)
sys.exit(1)
def rename_mailbox(self, mailbox, new_mailbox):
try:
t, data = self.__connection.rename(mailbox, new_mailbox)
if t == "NO":
print("Error: renaming mailbox failed", file=sys.stderr)
sys.exit(1)
except:
print("Error: renaming mailbox failed", file=sys.stderr)
sys.exit(1)
def reply(self, mailbox, message_id, text):
if not self.select_mailbox(mailbox): sys.exit(1)
try:
t, data = self.__connection.fetch(str(message_id), '(RFC822)')
if t == "NO":
print("Error: replying to message failed (fetching message headers failed)", file=sys.stderr)
sys.exit(1)
headers = email.message_from_string(data[0][1].decode()).items()
try:
sender = None
subject = None
recipients = []
ccs = []
bccs = []
for (type, value) in headers:
if type == "From": sender = value
elif type == "Subject": subject = value
elif type == "To": recipients.append(value)
elif type == "Cc": ccs.append(value)
elif type == "Bcc": bccs.append(value)
msg = email.message.Message()
if sender is not None: msg.add_header("To", sender)
if self.__user is not None: msg.add_header("From", self.__user)
if subject is not None: msg.add_header("Subject", "Re: " + subject)
for recipient in recipients:
if recipient != self.__user:
msg.add_header("Cc", recipient)
for cc in ccs:
msg.add_header("Cc", cc)
for bcc in bccs:
msg.add_header("Bcc", bcc)
msg.set_payload(text)
t, data = self.__connection.append(mailbox, "\Draft", "", msg.as_string().encode())
if t == "NO":
print("Error: replying to message failed (creating message failed)", file=sys.stderr)
sys.exit(1)
except:
print("Error: replying to message failed (creating message failed)", file = sys.stderr)
sys.exit(1)
except:
print("Error: replying to message failed (fetching message headers failed)", file=sys.stderr)
sys.exit(1)
def get_term(term_name, term, isAction):
"""
Check the given term and return its string value.
If the given term is invalid, an error message is printed and if isAction is True, the evaluation of the acthex program is aborted.
:param term_name: string containing an identifier for the given term. (Required for error message)
:param term:
:param isAction:
:return:
"""
if term is None:
print("Error: term " + str(term_name) +" is 'None'", file=sys.stderr)
if isAction: sys.exit(1)
return
if type(term.value()) is not str:
print("Error: term " + str(term_name) + " is not a string", file=sys.stderr)
if isAction: sys.exit(1)
return
if len(term.value()) < 1:
print("Error: term " + str(term_name) + " is too short. (expected a term of length >= 1, but was '" + str(term.value()) + "'", file=sys.stderr)
if isAction: sys.exit(1)
return
return term.value()
def get_string_from_term(term_name, term, isAction):
"""
Check if the given term is a valid string (i.e. if it is in double quotes) and return its string value (without quotes).
If the given term is invalid, an error message is printed and if isAction is True, the evaluation of the acthex program is aborted.
:param term_name: string containing an identifier for the given term. (Required for error message)
:param term:
:param isAction:
:return:
"""
term = get_term(term_name, term, isAction)
if len(term) < 2:
print("Error: term " + str(term_name) + " is too short (expected a term of length >= 2, but was '" + str(term) + "')", file=sys.stderr)
if isAction: sys.exit(1)
return
if term[0] != '"':
print("Error: term " + str(term_name) + " is invalid (expected to begin with \", but was '" + str( term) + "')", file=sys.stderr)
if isAction: sys.exit(1)
return
if term[-1] != '"':
print("Error: term " + str(term_name) + " is invalid (expected to end with \", but was '" + str(term) + "')", file=sys.stderr)
if isAction: sys.exit(1)
return
return term[1:-1]
def get_int_from_term(term_name, term, isAction):
"""
Check if the given term is a valid integer and return its integer value.
If the given term is invalid, an error message is printed and if isAction is True, the evaluation of the acthex program is aborted.
:param term_name: string containing an identifier for the given term. (Required for error message)
:param term:
:param isAction:
:return:
"""
term = get_term(term_name, term, isAction)
try:
return int(term)
except ValueError:
print("Error: term " + str(term_name) + " is invalid (expected an integer, but was '" + str(term )+ "')", file=sys.stderr)
if isAction: sys.exit(1)
return
def get_list_from_function(expected_function_name, function, isAction):
"""
Check if the given term is a valid function of form f(x1, x2, ...), where f is the expected function name, and return its terms x1, x2, ... as a list
If the given term is invalid, an error message is printed and if isAction is True, the evaluation of the acthex program is aborted.
:param expected_function_name:
:param function:
:param isAction:
:return:
"""
function = get_term(expected_function_name, function, True)
if function == expected_function_name: # 0-ary function