some cleanup, some comments

This commit is contained in:
Chuck 2013-07-04 13:33:04 +07:00
parent 5f6be6c2de
commit 561a3f8eae
2 changed files with 25 additions and 22 deletions

View File

@ -12,6 +12,7 @@
#import ctypes #import ctypes
import signal # Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully. import signal # Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully.
# The next 3 are used for the API # The next 3 are used for the API
from ConfigParser import NoOptionError
from SimpleXMLRPCServer import * from SimpleXMLRPCServer import *
import json import json
import singleton import singleton
@ -740,8 +741,8 @@ if __name__ == "__main__":
try: try:
if shared.config.get('bitmessagesettings', 'smtppop3enable') == 'true': if shared.config.get('bitmessagesettings', 'smtppop3enable') == 'true':
smtpServer = bitmessageSMTPServer() smtpServer = bitmessageSMTPServer()
pop3Server = bitmessagePOP3Server(debug=True) pop3Server = bitmessagePOP3Server()
except: except NoOptionError:
pass pass
# And finally launch asyncore # And finally launch asyncore

View File

@ -88,7 +88,7 @@ class bitmessageSMTPChannel(asynchat.async_chat):
data.append(text) data.append(text)
self.__data = '\n'.join(data) self.__data = '\n'.join(data)
status = self.__server.process_message(self.__peer, status = self.__server.process_message(self.__peer,
self.__mailfrom, self.address,
self.__rcpttos, self.__rcpttos,
self.__data) self.__data)
self.__rcpttos = [] self.__rcpttos = []
@ -203,6 +203,10 @@ class bitmessageSMTPChannel(asynchat.async_chat):
if self.__mailfrom: if self.__mailfrom:
self.push('503 Error: nested MAIL command') self.push('503 Error: nested MAIL command')
return return
_, domain = address.split('@', 1)
if domain != self.address:
self.push('530 Access denied: address domain must match Bitmessage identity')
return
self.__mailfrom = address self.__mailfrom = address
print >> smtpd.DEBUGSTREAM, 'sender:', self.__mailfrom print >> smtpd.DEBUGSTREAM, 'sender:', self.__mailfrom
self.push('250 Ok') self.push('250 Ok')
@ -259,7 +263,6 @@ class bitmessageSMTPChannel(asynchat.async_chat):
class bitmessageSMTPServer(smtpd.SMTPServer): class bitmessageSMTPServer(smtpd.SMTPServer):
def __init__(self): def __init__(self):
# TODO - move to separate file/class
smtpport = shared.config.getint('bitmessagesettings', 'smtpport') smtpport = shared.config.getint('bitmessagesettings', 'smtpport')
self.ssl = shared.config.getboolean('bitmessagesettings', 'smtpssl') self.ssl = shared.config.getboolean('bitmessagesettings', 'smtpssl')
@ -279,22 +282,19 @@ class bitmessageSMTPServer(smtpd.SMTPServer):
sock = ssl.wrap_socket(sock, server_side=True, certfile=self.certfile, keyfile=self.keyfile, ssl_version=ssl.PROTOCOL_SSLv23) sock = ssl.wrap_socket(sock, server_side=True, certfile=self.certfile, keyfile=self.keyfile, ssl_version=ssl.PROTOCOL_SSLv23)
bitmessageSMTPChannel(self, sock, peer_address) bitmessageSMTPChannel(self, sock, peer_address)
def process_message(self, peer, mailfrom, rcpttos, data): def process_message(self, peer, address, rcpttos, data):
#print("Peer", peer) #print("Peer", peer)
#print("Mail From", mailfrom) #print("Mail From", address)
#print("Rcpt To", rcpttos) #print("Rcpt To", rcpttos)
#print("Data") #print("Data")
#print(data) #print(data)
#print('--------') #print('--------')
#print(type(mailfrom)) #print(type(address))
message = data message = data
# Determine the fromAddress and make sure it's an owned identity # Determine the fromAddress and make sure it's an owned identity
# TODO - determine the address from a SMTP authorization. fromAddress = address
# TODO - use the mailfrom (a legitimate email address?) when delivering
# real e-mail.
_, fromAddress = mailfrom.split('@', 1)
if not (fromAddress.startswith('BM-') and '.' not in fromAddress): if not (fromAddress.startswith('BM-') and '.' not in fromAddress):
raise Exception("From Address must be a Bitmessage address.") raise Exception("From Address must be a Bitmessage address.")
else: else:
@ -328,11 +328,14 @@ class bitmessageSMTPServer(smtpd.SMTPServer):
for recipient in rcpttos: for recipient in rcpttos:
_, toAddress = recipient.split('@', 1) _, toAddress = recipient.split('@', 1)
if not (toAddress.startswith('BM-') and '.' not in toAddress): if not (toAddress.startswith('BM-') and '.' not in toAddress):
# TODO - deliver message to another SMTP server.. ? # TODO - deliver message to another SMTP server..
# I think this feature would urge adoption: the ability to use the same bitmessage address
# for delivering standard E-mail as well bitmessages.
raise Exception("Cannot yet handle normal E-mail addresses.") raise Exception("Cannot yet handle normal E-mail addresses.")
else: else:
# This is now the 3rd copy of this code. There's one in the API, there's another # This is now the 3rd copy of this message delivery code.
# copy in __init__ for the UI. Yet another exists here. It needs to be refactored # There's one in the API, there's another copy in __init__ for
# the UI. Yet another exists here. It needs to be refactored
# into a utility func! # into a utility func!
status, addressVersionNumber, streamNumber, toRipe = decodeAddress(toAddress) status, addressVersionNumber, streamNumber, toRipe = decodeAddress(toAddress)
if status != 'success': if status != 'success':
@ -346,25 +349,25 @@ class bitmessageSMTPServer(smtpd.SMTPServer):
print 'Error: Address version number too high (or zero) in address: ' + toAddress print 'Error: Address version number too high (or zero) in address: ' + toAddress
shared.printLock.release() shared.printLock.release()
raise Exception("Invalid Bitmessage address: {}".format(toAddress)) raise Exception("Invalid Bitmessage address: {}".format(toAddress))
#toAddress = addBMIfNotPresent(toAddress) # I know there's a BM-, because it's required when using SMTP
toAddressIsOK = False toAddressIsOK = False
try: try:
shared.config.get(toAddress, 'enabled') shared.config.get(toAddress, 'enabled')
except:
toAddressIsOK = True
if not toAddressIsOK:
# The toAddress is one owned by me. We cannot send # The toAddress is one owned by me. We cannot send
# messages to ourselves without significant changes # messages to ourselves without significant changes
# to the codebase. # to the codebase.
shared.printLock.acquire() shared.printLock.acquire()
print "Error: One of the addresses to which you are sending a message, {}, is yours. Unfortunately the Bitmessage client cannot process its own messages. Please try running a second client on a different computer or within a VM.".format(toAddress) print "Error: One of the addresses to which you are sending a message, {}, is yours. Unfortunately the Bitmessage client cannot process its own messages. Please try running a second client on a different computer or within a VM.".format(toAddress)
shared.printLock.release() shared.printLock.release()
except:
toAddressIsOK = True
if not toAddressIsOK:
raise Exception("Cannot send message to {}".format(toAddress)) raise Exception("Cannot send message to {}".format(toAddress))
# The subject is specially formatted to identify it from non-E-mail messages. # The subject is specially formatted to identify it from non-E-mail messages.
subject = "<Bitmessage Mail: 00000000000000000000>" # Reserved, flags. # TODO - The bitfield will be used to convey things like external attachments, etc.
subject = "<Bitmessage Mail: 00000000000000000000>" # Reserved flags.
ackdata = OpenSSL.rand(32) ackdata = OpenSSL.rand(32)
t = ('', toAddress, toRipe, fromAddress, subject, message, ackdata, int(time.time()), 'msgqueued', 1, 1, 'sent', 2) t = ('', toAddress, toRipe, fromAddress, subject, message, ackdata, int(time.time()), 'msgqueued', 1, 1, 'sent', 2)
@ -373,7 +376,7 @@ class bitmessageSMTPServer(smtpd.SMTPServer):
toLabel = '' toLabel = ''
t = (toAddress,) t = (toAddress,)
shared.sqlLock.acquire() shared.sqlLock.acquire()
shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') shared.sqlSubmitQueue.put('''SELECT label FROM addressbook WHERE address=?''')
shared.sqlSubmitQueue.put(t) shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get() queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release() shared.sqlLock.release()
@ -385,6 +388,5 @@ class bitmessageSMTPServer(smtpd.SMTPServer):
# TODO - what should we do with ackdata.encode('hex') ? # TODO - what should we do with ackdata.encode('hex') ?
import sys import sys
smtpd.DEBUGSTREAM = sys.stdout smtpd.DEBUGSTREAM = sys.stdout