diff --git a/bminterface.py b/bminterface.py index a9a9092..423af94 100644 --- a/bminterface.py +++ b/bminterface.py @@ -9,6 +9,7 @@ purgeList = [] allMessages = [] +currentAddress = None def _getKeyLocation(): #make this not suck later return '~/.config/PyBitmessage/keys.dat' @@ -64,6 +65,11 @@ def _stripAddress(address): logging.info("converted address " + orig + " to " + retstring) return retstring +def registerAddress(address): + global currentAddress + currentAddress = address + logging.debug("Set current address to %s" % currentAddress) + def send(toAddress, fromAddress, subject, body): toAddress = _stripAddress(toAddress) fromAddress = _stripAddress(fromAddress) @@ -76,9 +82,17 @@ def send(toAddress, fromAddress, subject, body): def _getAll(): global allMessages + global currentAddress if not allMessages: api = _makeApi(_getKeyLocation()) allMessages = json.loads(api.getAllInboxMessages()) + logging.debug("current address is %s" % currentAddress) + if currentAddress is not None: + ret = [] + for msg in allMessages['inboxMessages']: + if msg['toAddress'] == currentAddress: + ret.append(msg) + return dict(inboxMessages=ret) return allMessages def get(msgID): diff --git a/incoming.py b/incoming.py index 209afd4..7a8987a 100644 --- a/incoming.py +++ b/incoming.py @@ -9,6 +9,7 @@ import select import logging + class ChatterboxConnection(object): END = "\r\n" def __init__(self, conn): @@ -36,6 +37,15 @@ def recvall(self, END=END): def handleUser(data): + d = data.split() + logging.debug("data:%s" % d) + username = d[-1] + if username[:3] == 'BM-': + logging.debug("Only showing messages for %s" % username) + bminterface.registerAddress(username) + else: + logging.debug("Showing all messages in the inbox") + bminterface.registerAddress(None) return "+OK user accepted" def handlePass(data): @@ -61,9 +71,11 @@ def handleStat(data): return returnData def handleList(data): - cmd, msgId = data.split() + dataList = data.split() + cmd = dataList[0] msgSizes = _getMsgSizes() - if msgId is not None: + if len(dataList) > 1: + msgId = dataList[1] # means the server wants a single message response i = int(msgId) - 1 if i >= len(msgSizes): @@ -120,7 +132,9 @@ def handleQuit(data): def handleCapa(data): returnData = "+OK List of capabilities follows\r\n" - returnData += "CAPA\r\nTOP\r\nUSER\r\nPASS\r\nUIDL\r\n." + for k in dispatch: + returnData += "%s\r\n" % k + returnData += "." return returnData def handleUIDL(data): @@ -128,16 +142,15 @@ def handleUIDL(data): logging.debug(data) if len(data) == 1: refdata = bminterface.getUIDLforAll() + logging.debug(refdata) + returnData = '+OK\r\n' + for msgID, d in enumerate(refdata): + returnData += "%s %s\r\n" % (msgID+1, d) + returnData += '.' else: refdata = bminterface.getUIDLforSingle(int(data[1])-1) - logging.debug(refdata) - if len(refdata) == 1: + logging.debug(refdata) returnData = '+OK ' + data[0] + str(refdata[0]) - else: - returnData = '+OK listing UIDL numbers...\r\n' - for msgID in range(len(refdata)): - returnData += str(msgID+1) + ' ' + refdata[msgID] + '\r\n' - returnData += '.' return returnData def makeEmail(dateTime, toAddress, fromAddress, subject, body): @@ -247,6 +260,7 @@ def incomingServer_main(host, port, run_event): conn.sendall("+OK server ready") while run_event.is_set(): data = conn.recvall() + logging.debug("Answering %s" % data) command = data.split(None, 1)[0] try: cmd = dispatch[command] diff --git a/outgoing.py b/outgoing.py index c2e787d..608a9c6 100644 --- a/outgoing.py +++ b/outgoing.py @@ -13,7 +13,10 @@ def process_message(self, peer, mailfrom, rcpttos, data): toAddress = msg['To'] fromAddress = msg['From'] subject = u' '.join(unicode(t[0], t[1] or 'UTF-8') for t in email.header.decode_header(msg['Subject'])).encode('UTF-8') - body = self._bmformat(msg) + if msg.is_multipart(): + body = self._bmformat(msg) + else: + body = msg.get_payload() #Make sure we don't send an actually blank subject or body--this can cause problems. if not subject: