forked from V07D/bitmessage-email-gateway
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bitmessage-gateway.py
executable file
·1139 lines (987 loc) · 40 KB
/
bitmessage-gateway.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
#!/usr/bin/python
## imports
import os
import re
import time
import datetime
import argparse
import logging
import signal
import sys
import threading
import xmlrpclib
import json
import smtplib
import base64
import email
import fcntl
import html2text
import lib.gpg
import lib.bminbox
import lib.bmsignal
import lib.bmmega
#import lib.sendingtemplates
import lib.payment
import lib.milter
import Milter
from lib.config import BMConfig
from lib.mysql import BMMySQL
from lib.bmlogging import BMLogging
from lib.bmapi import BMAPI
from lib.sendbm import SendBMTemplate, SendBM
from lib.bmmessage import BMMessage
import lib.maintenance
import lib.charset
import lib.user
import random
from subprocess import call
from BeautifulSoup import BeautifulSoup
from email.parser import Parser
from email.header import decode_header
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email import Charset
from email.generator import Generator
try:
import pyinotify
have_inotify = True
except ImportError:
have_inotify = False
import pprint
## setup logging
BMLogging()
## check if username is banned
def is_banned_username(username):
if username in BMConfig().get("bmgateway", "banned_usernames"):
return True
else:
return False
## read email from file
def read_email(k):
try:
f = open(BMConfig().get("bmgateway", "bmgateway", "mail_folder") + k, 'r')
message = f.read()
return message
except IOError:
logging.error('Could not read email: ' + BMConfig().get("bmgateway", "bmgateway", "mail_folder") + k)
return
## delete email from file
def delete_email(k):
try:
os.remove(BMConfig().get("bmgateway", "bmgateway", "mail_folder") + k)
except OSError:
logging.error('Could not delete email: ' + BMConfig().get("bmgateway", "bmgateway", "mail_folder") + k)
## save email into another directory
def save_email(k):
savedir = BMConfig().get("bmgateway", "bmgateway", "mail_folder") + "../cur/"
try:
os.rename(BMConfig().get("bmgateway", "bmgateway", "mail_folder") + k,
savedir + k)
except OSError:
logging.error('Could not save email: ' + BMConfig().get("bmgateway", "bmgateway", "mail_folder") + k + " to " + savedir)
## generate a bitmessage address for an incoming email adress
def generate_sender_address(email):
## generate random address
time_start = time.time()
address = BMAPI().conn().createRandomAddress(base64.b64encode(email))
time_stop = time.time()
time_total = int(time_stop - time_start)
logging.info('Generated sender address for ' + email + ' in ' + str(time_total) + ' seconds')
return address
## check for new bitmessages
def get_inbox():
while True:
try:
messages = json.loads(BMAPI().conn().getAllInboxMessages())['inboxMessages']
except:
logging.warn('Could not read inbox messages via API %s. Retrying...', sys.exc_info()[0])
BMAPI().disconnect()
time.sleep(random.random()+0.5)
continue
break
return messages
def get_outbox():
while True:
try:
messages = json.loads(BMAPI().conn().getAllSentMessages())['sentMessages']
except:
logging.warn('Could not read outbox messages via API %s. Retrying...', sys.exc_info()[0])
BMAPI().disconnect()
time.sleep(random.random()+0.5)
continue
break
return messages
## send outbound email
def send_email(recipient, sender, subject, body, bm_id, userdata = None):
## open connection
server = smtplib.SMTP('localhost')
server.set_debuglevel(0)
## build message
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
## Signature
if BMConfig().get("bmgateway", "bmgateway", "signature") is not None:
body += "-- \n" + \
BMConfig().get("bmgateway", "bmgateway", "signature") + \
"\n"
enc_body = None
sender_key = None
recipient_key = None
sign = BMConfig().get("bmgateway", "pgp", "sign")
encrypt = BMConfig().get("bmgateway", "pgp", "encrypt")
if userdata:
if userdata.expired():
sign = False
encrypt = False
else:
# only override if not expired and pgp allowed globally
if sign:
sign = (userdata.pgp == 1)
if encrypt:
encrypt = (userdata.pgp == 1)
#TODO find out if already encrypted/signed
## generate a signing key if we dont have one
if sign:
if not lib.gpg.check_key(sender, whatreturn="key", operation="sign"):
lib.gpg.create_primary_key(sender)
sender_key = lib.gpg.check_key(sender, whatreturn="key", operation="sign")
if not sender_key:
logging.error('Could not find or upload user\'s keyid: %s', sender)
## search for recipient PGP key
if encrypt:
# if lib.gpg.find_key(recipient):
recipient_key = lib.gpg.check_key(recipient, whatreturn="key", operation="encrypt")
if not recipient_key:
logging.info('Could not find recipient\'s keyid, not encrypting: %s', recipient)
# make sure sender has an encryption key
if not lib.gpg.check_key(sender, whatreturn="key", operation="encrypt"):
if not lib.gpg.check_key(sender, whatreturn="key", operation="sign"):
lib.gpg.create_primary_key(sender)
lib.gpg.create_subkey(sender)
if sender_key and recipient_key:
enc_body = lib.gpg.encrypt_text(body, recipient_key, sender_key)
logging.info('Encrypted and signed outbound mail from %s to %s', sender, recipient)
elif recipient_key:
enc_body = lib.gpg.encrypt_text(body, recipient_key)
logging.info('Encrypted outbound mail from %s to %s', sender, recipient)
elif sender_key and not recipient == BMConfig().get("bmgateway", "bmgateway", "bug_report_address_email"):
logging.info('Signed outbound mail from %s to %s', sender, recipient)
enc_body = lib.gpg.sign_text(body, sender_key)
## only encrypt if the operation was successful
if enc_body:
body = enc_body
text = body
## encode as needed
body = MIMEText(body, 'plain')
## attach body with correct encoding
msg.attach(body)
text = msg.as_string()
## send message
try:
status = server.sendmail(sender, recipient, text, [], ["NOTIFY=SUCCESS,FAILURE,DELAY", "ORCPT=rfc822;" + recipient])
logging.info('Sent email from %s to %s', sender, recipient)
BMMessage.deleteStatic(bm_id, folder = "inbox")
## send failed
except smtplib.SMTPException as e:
logging.error('Could not send email from %s to %s: %s', sender, recipient, e)
server.quit()
for rcpt in e.recipients:
return e.recipients[rcpt]
server.quit()
return
## list known addresses
def list_addresses():
## print all addresses
print "\n####################################\nInternal Address List\n####################################"
for tmp_email in BMAPI().address_list:
print tmp_email + "\t\t\t" + BMAPI().address_list[tmp_email]
print ''
print "\n####################################\nUser List\n####################################"
lib.user.GWUser()
print ""
def str_timestamp():
return time.strftime("Generated at: %b %d %Y %H:%M:%S\r\n", time.gmtime())
## delete address
def delete_address(address):
## try to delete and don't worry about if it actually goes through
BMAPI().conn().deleteAddressBookEntry(address)
BMAPI().conn().deleteAddress(address)
lib.user.GWUser(bm = address).delete()
if BMConfig().get("bmgateway", "bmgateway", "debug"):
logging.debug('Deleted bitmessage address, ' + address)
def field_in_list(message, address_list,
message_field, list_field):
result = False
try:
if message[message_field] == address_list[BMConfig().get("bmgateway", "bmgateway", list_field)]:
result = True
except:
result = False
return result
## check for new bitmessages to process
def check_bminbox(intcond):
global interrupted
## get all messages
#all_messages = json.loads(api['conn'].getAllInboxMessages())['inboxMessages']
logging.info("Entering BM inbox checker loop")
intcond.acquire()
while not interrupted:
all_messages = get_inbox()
## if no messages
if not all_messages:
try:
intcond.wait(BMConfig().get("bmgateway", "bmgateway", "process_interval"))
except KeyboardInterrupt:
break
continue
## loop through messages to find unread
for a_message in all_messages:
## if already read, delete and break
if a_message['read'] == 1:
BMMessage.deleteStatic(a_message['msgid'])
continue
## check if already processed, maybe from another instance
if lib.bminbox.check_message_processed(a_message['msgid']):
logging.info('Message %s has already been processed deleting...', a_message['msgid'])
# BMMessage.deleteStatic(a_message['msgid'])
# continue
## if the message is unread, load it by ID to trigger the read flag
message = json.loads(BMAPI().conn().getInboxMessageByID(a_message['msgid'], False))['inboxMessage'][0]
## if a blank message was returned
if not message:
logging.error('API returned blank message when requesting a message by msgID')
delete_bitmessage_inbox(bm_id)
BMMessage.deleteStatic(a_message['msgid'])
continue
## find message ID
bm_id = message['msgid']
## check if receive address is a DEregistration request
if field_in_list(message, BMAPI().address_list,
'toAddress', 'deregistration_address_label'):
## check if address is registered
userdata = lib.user.GWUser(bm = message['fromAddress'])
## if the sender is actually registered and wants to deregister
if userdata.check():
## process deregistration
logging.info('Processed deregistration request for user ' + userdata.email)
delete_address(message['fromAddress'])
## send deregistration confirmation email
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "deregistration_address_label")),
recipient = message['fromAddress'],
template = "deregistration-confirmed",
addmaps = {
'email': userdata.email
})
## bogus deregistration request
else:
logging.warn('Purged malicious deregistration bitmessage from ' + message['fromAddress'])
elif field_in_list(message, BMAPI().address_list, 'toAddress', 'bug_report_address_label'):
userdata = lib.user.GwUser(bm = message['fromAddress'])
# if not, create a fake one
# relay to ticket
## check if receive address is a registration request
elif field_in_list(message, BMAPI().address_list,
'toAddress', 'registration_address_label'):
userdata = lib.user.GWUser(bm = message['fromAddress'])
if userdata.check(): # status, config, etc
command = base64.b64decode(message['subject']).lower()
if command == "config":
logging.info('Config request from %s', message['fromAddress'])
body = base64.b64decode(message['message'])
data = {}
for line in body.splitlines():
line = re.sub("#.*", "", line)
option = re.search("(\S+)\s*:\s*(\S+)", line)
if option is None:
continue
if option.group(1).lower() == "pgp":
data['pgp'] = lib.user.GWUserData.pgp(option.group(2))
elif option.group(1).lower() == "attachments":
data['attachments'] = lib.user.GWUserData.zero_one(option.group(2))
#elif option.group(1).lower() == "flags":
#data['flags'] = lib.user.GWUserData.numeric(option.group(2))
elif option.group(1).lower() == "archive":
data['archive'] = lib.user.GWUserData.zero_one(option.group(2))
elif option.group(1).lower() == "masterpubkey_btc":
data['masterpubkey_btc'] = lib.user.GWUserData.public_seed(option.group(2))
# reset offset unless set explicitly
if data['masterpubkey_btc'] is not None and not 'offset_btc' in data:
data['offset_btc'] = "0"
elif option.group(1).lower() == "offset_btc":
data['offset_btc'] = lib.user.GWUserData.numeric(option.group(2))
elif option.group(1).lower() == "feeamount":
data['feeamount'] = lib.user.GWUserData.numeric(option.group(2), 8)
elif option.group(1).lower() == "feecurrency":
data['feecurrency'] = lib.user.GWUserData.currency(option.group(2))
else:
pass
if userdata.update(data):
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "configchange",
addmaps = {
})
else:
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "confignochange",
addmaps = {
})
pass
elif command == "status" or command == "" or not command:
logging.info('Status request from %s', message['fromAddress'])
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "status",
addmaps = {
'email': userdata.email,
'domain': userdata.domain,
'active': "Yes" if userdata.active else "No",
'cansend': "Yes" if userdata.cansend else "No",
'cancharge': "Yes" if userdata.cancharge else "No",
'caninvoice': "Yes" if userdata.caninvoice else "No",
'pgp': "server" if userdata.pgp else "local",
'attachments': "Yes" if userdata.attachments else "No",
'expires': userdata.exp.strftime("%B %-d %Y"),
'masterpubkey_btc': userdata.masterpubkey_btc if userdata.masterpubkey_btc else "N/A",
'offset_btc': str(userdata.offset_btc) if userdata.masterpubkey_btc else "N/A",
'feeamount': str(userdata.feeamount) if userdata.masterpubkey_btc else "N/A",
'feecurrency': str(userdata.feecurrency) if userdata.masterpubkey_btc else "N/A",
'archive': "Yes" if userdata.archive else "No",
'flags': hex(userdata.flags),
'aliases': ', '.join(userdata.aliases) if userdata.aliases else "None"
})
else:
logging.info('Invalid command from %s', message['fromAddress'])
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "command-invalid",
addmaps = {
'command': command,
'email': userdata.email
})
else: # attempt to register new user
## find requested username
proposed_registration_user = base64.b64decode(message['subject']).lower()
#full_registration_user = registration_user + '@' + BMConfig().get("bmgateway", "bmgateway", "domain_name")
valid_one = re.match('^[\w]{4,20}$', proposed_registration_user) is not None
valid_two = re.match('^[\w]{4,20}@' + BMConfig().get("bmgateway", "bmgateway", "domain_name") + '$', proposed_registration_user) is not None
# strip domain if they sent it during registration
if valid_one:
full_registration_user = proposed_registration_user.lower() + '@' + BMConfig().get("bmgateway", "bmgateway", "domain_name")
registration_user = proposed_registration_user.lower()
elif valid_two:
full_registration_user = proposed_registration_user.lower()
registration_user = proposed_registration_user.split('@')[0].lower()
else:
logging.info('Invalid email address in registration request for %s', proposed_registration_user)
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "registration-invalid",
addmaps = {
'email': proposed_registration_user
})
BMMessage.deleteStatic(bm_id)
continue
## if username is valid check if it's available
## check if address is already registered to a username or is banned
if is_banned_username(registration_user):
logging.info('Banned email address in registration request for %s', registration_user)
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "registration-duplicate",
addmaps = {
'email': full_registration_user
})
BMMessage.deleteStatic(bm_id)
continue
elif lib.user.GWUser(email = full_registration_user).check():
logging.info('Duplicate email address in registration request for %s', registration_user)
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "registration-duplicate",
addmaps = {
'email': full_registration_user
})
BMMessage.deleteStatic(bm_id)
continue
logging.info('Received registration request for email address %s ', full_registration_user)
lib.user.GWUser(empty = True).add(bm = message['fromAddress'], email = full_registration_user)
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "registration-confirmed",
addmaps = {
'email': full_registration_user
})
## if sent to the generic recipient or sender address
elif field_in_list(message, BMAPI().address_list,
'toAddress', 'relay_address_label'):
## if user is not registered, purge
userdata = lib.user.GWUser(bm = message['fromAddress'])
if not userdata.check():
if BMConfig().get("bmgateway", "bmgateway", "allow_unregistered_senders"):
bm_sender = message['fromAddress'] + '@' + BMConfig().get("bmgateway", "bmgateway", "domain_name")
else:
logging.warn('Purged bitmessage from non-registered user ' + message['fromAddress'])
BMMessage.deleteStatic(bm_id)
continue
## if user is registered, find their username @ domain
else:
bm_sender = userdata.email
## find outbound email address
bm_receiver = re.findall(r'[\w\.\+-]+@[\w\.-]+\.[\w]+', base64.b64decode(message['subject']))
if len(bm_receiver) > 0:
bm_receiver = bm_receiver[0]
## if there is no receiver mapping or the generic address didnt get a valid outbound email, deny it
if not bm_receiver:
# FIXME explain to sender what is whrong
logging.warn('Received and purged bitmessage with unknown recipient (likely generic address and bad subject)')
if BMConfig().get("bmgateway", "bmgateway", "respond_to_missing"):
SendBMTemplate(
sender = message['toAddress'],
recipient = message['fromAddress'],
template = "relay-missing-recipient",
addmaps = {
'email': userdata.email,
})
BMMessage.deleteStatic(bm_id)
continue
# expired or cannot send
if (userdata.expired() or userdata.cansend == 0) and not \
(bm_receiver == BMConfig().get("bmgateway", "bmgateway", "bug_report_address_email")): # can still contact bugreport
btcaddress, amount = lib.payment.payment_exists_domain (BMConfig().get("bmgateway", "bmgateway", "domain_name"), userdata.bm)
# create new one
if btcaddress == False:
btcaddress, amount = lib.payment.create_invoice_domain (BMConfig().get("bmgateway", "bmgateway", "domain_name"), userdata.bm)
SendBMTemplate(
sender = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "registration_address_label")),
recipient = message['fromAddress'],
template = "accountexpired",
addmaps = {
'btcuri': lib.payment.create_payment_uri(btcaddress, 'BTC', amount,
BMConfig().get("bmgateway", "bmgateway", "companyname"), 'User ' + userdata.bm + " / " + userdata.email + ' subscription'),
'service': 'Subscription for ' + userdata.email + ' from ' + datetime.date.today().strftime("%B %-d %Y") +
' until ' + userdata.exp.strftime("%B %-d %Y"),
'email': userdata.email
})
logging.warn("User " + message['fromAddress'] + " notified of payment requirement")
BMMessage.deleteStatic(bm_id)
continue
bm_subject = base64.b64decode(message['subject'])
## handle removal of embedded MAILCHUCK-FROM:: tag for replies
bm_subject = bm_subject.replace('MAILCHUCK-FROM::' + bm_receiver + ' | ', '');
## remove email address from subject
if field_in_list(message, BMAPI().address_list, 'toAddress', 'relay_address_label'):
bm_subject = bm_subject.replace(bm_receiver, '')
## get message contents
bm_body = base64.b64decode(message['message'])
## pad with a newline, otherwise it may look ugly
if bm_body[-1:] != '\n':
bm_body += '\n'
## send message and delete bitmessage, bitches
if (float(userdata.lastrelay) + BMConfig().get("bmgateway", "bmgateway", "throttle") > time.time()):
SendBMTemplate(
sender = message['toAddress'],
recipient = message['fromAddress'],
template = "relay-throttle",
addmaps = {
'email': userdata.email,
'throttledelta': str(int((float(userdata.lastrelay) +
BMConfig().get("bmgateway", "bmgateway", "throttle") - time.time() + 60)/60))
})
logging.warn('Throttled %s', message['fromAddress'])
BMMessage.deleteStatic(bm_id)
continue
else:
retval = send_email(bm_receiver, bm_sender, bm_subject, bm_body, bm_id, userdata = userdata)
if retval is None:
logging.info('Relayed from %s to %s', message['fromAddress'], bm_receiver)
else:
if retval[0] >= 400 and retval[0] < 500:
# do not delete, repeatable
continue
else:
SendBMTemplate(
sender = message['toAddress'],
recipient = message['fromAddress'],
template = "smtperror",
addmaps = {
'emailrcpt': bm_receiver,
'errcode': retval[0],
'errmessage': retval[1]
}
)
## remove message
BMMessage.deleteStatic(bm_id)
lib.bminbox.set_message_processed(bm_id)
intcond.wait(BMConfig().get("bmgateway", "bmgateway", "process_interval"))
intcond.release()
logging.info("Leaving BM inbox checker loop")
def check_bmoutbox(intcond):
global interrupted
## get all messages
#all_messages = json.loads(api['conn'].getAllInboxMessages())['inboxMessages']
logging.info("Entering BM outbox checker loop")
intcond.acquire()
while not interrupted:
all_messages = get_outbox()
logging.info("Trashing old outbox messages")
## if no messages
if not all_messages:
try:
intcond.wait(BMConfig().get("bmgateway", "bmgateway", "outbox_process_interval"))
except KeyboardInterrupt:
break
continue
## loop through messages to find unread
for a_message in all_messages:
if a_message['status'] == 'ackreceived':
userdata = lib.user.GWUser(bm = a_message['toAddress'])
if userdata:
userdata.setlastackreceived(a_message['lastActionTime'])
BMMessage.deleteStatic(a_message['msgid'], folder = "outbox")
logging.info("Vacuuming DB")
result = BMAPI().conn().deleteAndVacuum()
intcond.wait(BMConfig().get("bmgateway", "bmgateway", "outbox_process_interval"))
intcond.release()
logging.info("Leaving BM outbox checker loop")
def check_boxes():
check_bminbox()
check_bmoutbox()
def handle_email(k):
global address_list
userdata = None
## read email from file
msg_raw = read_email(k)
if not msg_raw:
logging.error('Could not open email file: ' + k)
return
## extract header
msg_headers = Parser().parsestr(msg_raw)
## check if email was valid
if not msg_headers:
logging.error('Malformed email detected and purged')
delete_email(k)
return
## find email source and dest addresses
msg_sender = msg_headers["From"]
## failed delivery email
if msg_sender == '<>' or not msg_sender:
msg_sender = BMConfig().get("bmgateway", "bmgateway", "relay_address_label")
else:
try:
msg_sender = re.findall(r'[\w\.+-]+@[\w\.-]+.[\w]+', msg_sender)[0]
except:
pass
msg_sender = msg_sender.lower()
msg_recipient = ""
## find email details
if msg_headers["To"]:
rcpts = re.findall(r'[\w\.+-]+@[\w\.-]+.[\w]+', msg_headers["To"])
if len(rcpts) > 0:
msg_recipient = rcpts[0]
## strip extension (user+foo@domain)
msg_recipient = re.sub(r'\+.*@', '@', msg_recipient)
msg_recipient = msg_recipient.lower()
userdata = lib.user.GWUser(email = msg_recipient, unalias = True)
## check if we have a recipient address for the receiving email
if not userdata or not userdata.check():
## look for X-Original-To instead
rcpts = re.findall(r'[\w\.+-]+@[\w\.-]+.[\w]+', msg_headers["X-Original-To"])
if len(rcpts) > 0:
msg_recipient = rcpts[0]
msg_recipient = re.sub(r'\+.*@', '@', msg_recipient)
msg_recipient = msg_recipient.lower()
userdata = lib.user.GWUser(email = msg_recipient, unalias = True)
## no valid recipient
#if not msg_recipient in addressbook:
# logging.warn('Purged email destined for unknown user ' + msg_recipient + ' from ' + msg_sender)
# logging.debug(msg_headers)
# delete_email(k)
# return
## check if we have valid sender and recipient details
if not msg_sender or not msg_recipient:
logging.warn('Malformed email detected and purged')
delete_email(k)
return
## set bitmessage destination address
bm_to_address = userdata.bm
## set from address
## check to see if we need to generate a sending address for the source email address
# if not msg_sender in address_list:
# bm_from_address = generate_sender_address(msg_sender)
# address_list[msg_sender] = bm_from_address
# else:
bm_from_address = BMAPI().get_address(BMConfig().get("bmgateway", "bmgateway", "relay_address_label"))
## find message subject
msg_subject = decode_header(msg_headers['subject'])[0]
if(msg_subject[1]):
msg_subject = unicode(msg_subject[0], msg_subject[1])
else:
msg_subject = lib.charset.safeDecode(msg_subject[0])
## find message body contents in plaintext
msg_tmp = email.message_from_string(msg_raw)
# handle DSN
if msg_tmp.get_content_type() == "multipart/report" and msg_tmp.get_param("report-type", "") == "delivery-status" and msg_tmp.get("Auto-Submitted", "") == "auto-replied":
for part in msg_tmp.walk():
if part and part.get_content_type() == 'message/delivery-status':
for subpart in part.get_payload(decode = 0):
if subpart.get("Action", "") in ("relayed", "delivered", "expanded"):
logging.info ("Successful DSN from " + bm_to_address)
lib.user.GWUser(bm = bm_to_address).setlastrelay(lastrelay = time.time())
delete_email(k)
return
msg_body = u''
body_raw = ''
decrypt_ok = False
sigverify_ok = False
mega_fileids = []
# DKIM
ar = msg_tmp.get_param("dkim", "missing", "Authentication-Results")
if ar == "missing":
try:
domain = msg_sender.split("@")[-1]
if lib.user.GWDomain(domain).check() and domain == msg_tmp.get_param("d", "missing", "DKIM-Signature"):
ar = "pass" # we trust MTA to reject fakes
except:
pass
## inline PGP
for part in msg_tmp.walk():
if part and part.get_content_type() == 'text/plain' and not (part.has_key("Content-Disposition") and part.__getitem__("Content-Disposition")[:11] == "attachment;"):
part_str = part.get_payload(decode=1)
if userdata.pgp == 1:
if userdata.flags & 1 == 1:
pgpparts = part_str.split("-----")
# hack for absent pgp
if not pgpparts or len(pgpparts) < 4:
msg_body += lib.charset.safeDecode(part_str, part.get_content_charset(None))
continue
state = 0
pgp_body = ""
for pgppart in pgpparts:
if pgppart == "BEGIN PGP MESSAGE":
pgp_body = "-----" + pgppart + "-----"
state = 1
elif pgppart == "END PGP MESSAGE":
pgp_body += "-----" + pgppart + "-----"
# import from sql if necessary
lib.gpg.check_key(msg_recipient)
decrypted, sigverify_ok = lib.gpg.decrypt_content(pgp_body, msg_sender, msg_recipient)
if isinstance(decrypted, basestring):
part_str = decrypted
decrypt_ok = True
#else:
#part_str = part.get_payload(decode = 0)
sigresult = "fail"
if sigverify_ok:
sigresult = "ok"
logging.info("Decrypted email from " + msg_sender + " to " + msg_recipient + ", signature: " + sigresult)
state = 0
elif pgppart == "BEGIN PGP SIGNED MESSAGE":
pgp_body += "-----" + pgppart + "-----"
state = 2
elif pgppart == "BEGIN PGP SIGNATURE":
pgp_body += "-----" + pgppart + "-----"
state = 3
elif pgppart == "END PGP SIGNATURE":
pgp_body += "-----" + pgppart + "-----"
# import from sql if necessary
lib.gpg.check_key(msg_recipient)
plain, sigverify_ok = lib.gpg.verify(pgp_body, msg_sender, msg_recipient)
if isinstance(plain, basestring):
part_str = plain
#else:
#part_str = part.get_payload(decode = 0)
sigresult = "fail"
if sigverify_ok:
sigresult = "ok"
logging.info("Verifying PGP signature from " + msg_sender + " to " + msg_recipient + ": " + sigresult)
state = 0
elif state == 0:
msg_body += lib.charset.safeDecode(pgppart, part.get_content_charset(None))
elif state > 0:
pgp_body += lib.charset.safeDecode(pgppart, part.get_content_charset(None))
else:
if "BEGIN PGP MESSAGE" in part_str:
# import from sql if necessary
lib.gpg.check_key(msg_recipient)
decrypted, sigverify_ok = lib.gpg.decrypt_content(part_str, msg_sender, msg_recipient)
if isinstance(decrypted, basestring):
part_str = decrypted
decrypt_ok = True
else:
part_str = part.get_payload(decode = 0)
logging.info("Decrypted email from " + msg_sender + " to " + msg_recipient)
elif "BEGIN PGP SIGNED MESSAGE" in part_str:
# import from sql if necessary
lib.gpg.check_key(msg_recipient)
plain, sigverify_ok = lib.gpg.verify(part_str, msg_sender, msg_recipient)
if isinstance(plain, basestring):
part_str = plain
else:
part_str = part.get_payload(decode = 0)
# PGP END
body_raw += part.as_string(False)
#print part.get_content_charset()
#print msg_tmp.get_charset()
part_str = lib.charset.safeDecode(part_str, part.get_content_charset(None))
msg_body += part_str
## if there's no plaintext content, convert the html
if not msg_body or userdata.html == 2:
for part in msg_tmp.walk():
if part and part.get_content_type() == 'text/html' and not (part.has_key("Content-Disposition") and part.__getitem__("Content-Disposition")[:11] == "attachment;"):
part_str = part.get_payload(decode=1)
h = html2text.HTML2Text()
h.inline_links = False
if userdata.html == 1:
msg_body += lib.charset.safeDecode(part_str, part.get_content_charset(None))
elif userdata.html == 2:
msg_body = lib.charset.safeDecode(part_str, part.get_content_charset(None))
else:
msg_body += h.handle(lib.charset.safeDecode(part_str, part.get_content_charset(None)))
#msg_body = msg_body + html2text.html2text(unicode(part.get_payload(), part.get_content_charset()))
## if there's no plaintext or html, check if it's encrypted
# PGP/MIME
has_encrypted_parts = False
if not msg_body:
for part in msg_tmp.walk():
if part.get_content_type() == 'application/pgp-encrypted':
has_encrypted_parts = True
# import from sql if necessary
if userdata.pgp == 1:
lib.gpg.check_key(msg_recipient)
## look for encrypted attachment containing text
if part.get_content_type() == 'application/octet-stream' and has_encrypted_parts:
part_str = part.get_payload(decode=1)
if userdata.pgp == 0:
msg_body += part_str
continue
## if we see the pgp header, decrypt
if 'BEGIN PGP MESSAGE' in part_str:
decrypted_data, sigverify_ok = lib.gpg.decrypt_content(part_str, msg_sender, msg_recipient, True)
## decrypt failed
if not decrypted_data:
logging.error("Decryption of email destined for " + msg_recipient + " failed")
msg_body += part.get_payload(decode=0)
continue
logging.info("Decrypted email from " + msg_sender + " to " + msg_recipient)
msg_body += decrypted_data
decrypt_ok = True
elif "BEGIN PGP SIGNED MESSAGE" in part_str:
plain, sigverify_ok = lib.gpg.verify(part_str, msg_sender, msg_recipient)
if isinstance(plain, basestring):
msg_body += plain
else:
msg_body += part.get_payload(decode = 0)
## unknown attachment
else:
logging.debug("Received application/octet-stream type in inbound email, but did not see encryption header")
if not sigverify_ok:
for part in msg_tmp.walk():
if part.get_content_type() == 'application/pgp-signature':
if userdata.pgp == 0:
msg_body = '-----BEGIN PGP SIGNED MESSAGE-----\n' + msg_body
msg_body += '\n-----BEGIN PGP SIGNATURE-----\n'
msg_body += part.get_payload(decode=0)
msg_body += '\n-----END PGP SIGNATURE-----\n'
continue
# import from sql if necessary
lib.gpg.check_key(msg_recipient)
plain, sigverify_ok = lib.gpg.verify(body_raw, msg_sender, msg_recipient, part.get_payload(decode=1))
if userdata.attachments == 1 and not userdata.expired():
for part in msg_tmp.walk():
if part.has_key("Content-Disposition") and part.__getitem__("Content-Disposition")[:11] == "attachment;":
# fix encoding
try:
filename = email.header.decode_header(part.get_filename())
encoding = filename[0][1]
filename = filename[0][0]
except:
filename = part.get_filename()
encoding = False
fileid, link = lib.bmmega.mega_upload(userdata.bm, filename, part.get_payload(decode = 1))
mega_fileids.append(fileid)
if encoding:
filename = unicode(filename, encoding)
logging.info("Attachment \"%s\" (%s)", filename, part.get_content_type())
msg_body = "Attachment \"" + filename + "\" (" + part.get_content_type() + "): " + link + "\n" + msg_body
if userdata.pgp == 1:
if not decrypt_ok:
msg_body = "WARNING: PGP encryption missing or invalid. The message content could be exposed to third parties.\n" + msg_body
if not sigverify_ok:
msg_body = "WARNING: PGP signature missing or invalid. The authenticity of the message could not be verified.\n" + msg_body
else:
# msg_body = "WARNING: Server-side PGP is off, passing message as it is.\n" + msg_body
pass
if not ar[0:4] == "pass":
msg_body = "WARNING: DKIM signature missing or invalid. The email may not have been sent through legitimate servers.\n" + msg_body
logging.info('Incoming email from %s to %s', msg_sender, msg_recipient)
sent = SendBM(bm_from_address, bm_to_address,
'MAILCHUCK-FROM::' + msg_sender + ' | ' + msg_subject.encode('utf-8'),
msg_body.encode('utf-8'))
if sent.status:
for fileid in mega_fileids:
# cur.execute ("UPDATE mega SET ackdata = %s WHERE fileid = %s AND ackdata IS NULL", (ackdata.decode("hex"), fileid))
pass
## remove email file
if userdata.archive == 1:
#print msg_body
save_email(k)
else:
delete_email(k)
class InotifyEventHandler(pyinotify.ProcessEvent):
def process_default(self, event):
if os.path.isfile(event.pathname):
# to avoid crashes from taking down the whole processing, in the future for parallel processing
fhandle = open (event.pathname, 'r')
fcntl.flock(fhandle, fcntl.LOCK_EX)
email_thread = threading.Thread(target=handle_email, name='EmailIn', args=(event.name,))
email_thread.start()
email_thread.join()
fcntl.flock(fhandle, fcntl.LOCK_UN)
fhandle.close()
def inotify_incoming_emails():
mdir = BMConfig().get("bmgateway", "bmgateway", "mail_folder")
wm = pyinotify.WatchManager()
notifier = pyinotify.ThreadedNotifier(wm, InotifyEventHandler())
notifier.setName ("Inotify")
wm.add_watch (mdir, pyinotify.IN_CREATE|pyinotify.IN_CLOSE_WRITE|pyinotify.IN_MOVED_TO)
#wm.add_watch (mdir, pyinotify.ALL_EVENTS, rec=True)
return notifier
## check for new mail to process
def check_emails(intcond=None):
## find new messages in folders
mdir = os.listdir(BMConfig().get("bmgateway", "bmgateway", "mail_folder"))
## no new mail
if not mdir:
return
## iterate through new messages, each in thread so that crashes do not prevent continuing
for k in mdir: