Skip to content
Open

14 #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bminterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

purgeList = []
allMessages = []
currentAddress = None

def _getKeyLocation(): #make this not suck later
return '~/.config/PyBitmessage/keys.dat'
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
34 changes: 24 additions & 10 deletions incoming.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import select
import logging


class ChatterboxConnection(object):
END = "\r\n"
def __init__(self, conn):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -120,24 +132,25 @@ 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):
data = data.split()
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):
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion outgoing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down