Merge pull request #535 from Atheros1/master

API commands: createChan, joinChan, leaveChan, deleteAddress
This commit is contained in:
Jonathan Warren 2013-10-25 16:59:24 -07:00
commit 9dd674a117
4 changed files with 142 additions and 18 deletions

View File

@ -12,9 +12,10 @@ warrantless wiretapping programs.
Development
----------
If you plan to put a non-trivial amount of work into coding new features, it
is recommended that you first solicit feedback on the DevTalk pseudo-mailing
list:
Bitmessage is a collaborative project. You are welcome to submit pull requests
although if you plan to put a non-trivial amount of work into coding new
features, it is recommended that you first solicit feedback on the DevTalk
pseudo-mailing list:
BM-2D9QKN4teYRvoq2fyzpiftPh9WP9qggtzh

View File

@ -139,7 +139,7 @@ def decodeAddress(address):
integer = decodeBase58(address)
if integer == 0:
status = 'invalidcharacters'
return status,0,0,0
return status,0,0,""
#after converting to hex, the string will be prepended with a 0x and appended with a L
hexdata = hex(integer)[2:-1]
@ -161,7 +161,7 @@ def decodeAddress(address):
if checksum != sha.digest()[0:4]:
status = 'checksumfailed'
return status,0,0,0
return status,0,0,""
#else:
# print 'checksum PASSED'
@ -172,11 +172,11 @@ def decodeAddress(address):
if addressVersionNumber > 4:
print 'cannot decode address version numbers this high'
status = 'versiontoohigh'
return status,0,0,0
return status,0,0,""
elif addressVersionNumber == 0:
print 'cannot decode address version numbers of zero.'
status = 'versiontoohigh'
return status,0,0,0
return status,0,0,""
streamNumber, bytesUsedByStreamNumber = decodeVarint(data[bytesUsedByVersionNumber:])
#print streamNumber
@ -191,16 +191,16 @@ def decodeAddress(address):
elif len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) == 18:
return status,addressVersionNumber,streamNumber,'\x00\x00'+data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]
elif len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) < 18:
return 'ripetooshort',0,0,0
return 'ripetooshort',0,0,""
elif len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) > 20:
return 'ripetoolong',0,0,0
return 'ripetoolong',0,0,""
else:
return 'otherproblem',0,0,0
return 'otherproblem',0,0,""
elif addressVersionNumber == 4:
if len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) > 20:
return 'ripetoolong',0,0,0
return 'ripetoolong',0,0,""
elif len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) < 4:
return 'ripetooshort',0,0,0
return 'ripetooshort',0,0,""
else:
x00string = '\x00' * (20 - len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]))
return status,addressVersionNumber,streamNumber,x00string+data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]

View File

@ -38,6 +38,8 @@ from debug import logger
import helper_bootstrap
import proofofwork
str_chan = '[chan]'
import sys
if sys.platform == 'darwin':
if float("{1}.{2}".format(*sys.version_info)) < 7.5:
@ -150,8 +152,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
def _decode(self, text, decode_type):
try:
return text.decode(decode_type)
except TypeError as e:
raise APIError(22, "Decode error - " + str(e))
except Exception as e:
raise APIError(22, "Decode error - " + str(e) + ". Had trouble while decoding string: " + repr(text))
def _verifyAddress(self, address):
status, addressVersionNumber, streamNumber, ripe = decodeAddress(address)
@ -387,6 +389,113 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
('getDeterministicAddress', addressVersionNumber,
streamNumber, 'unused API address', numberOfAddresses, passphrase, eighteenByteRipe))
return shared.apiAddressGeneratorReturnQueue.get()
elif method == 'createChan':
if len(params) == 0:
raise APIError(0, 'I need parameters.')
elif len(params) == 1:
passphrase, = params
passphrase = self._decode(passphrase, "base64")
if len(passphrase) == 0:
raise APIError(1, 'The specified passphrase is blank.')
# It would be nice to make the label the passphrase but it is
# possible that the passphrase contains non-utf-8 characters.
try:
unicode(passphrase, 'utf-8')
label = str_chan + ' ' + passphrase
except:
label = str_chan + ' ' + repr(passphrase)
addressVersionNumber = 4
streamNumber = 1
shared.apiAddressGeneratorReturnQueue.queue.clear()
logger.debug('Requesting that the addressGenerator create chan %s.', passphrase)
shared.addressGeneratorQueue.put(('createChan', addressVersionNumber, streamNumber, label, passphrase))
queueReturn = shared.apiAddressGeneratorReturnQueue.get()
if len(queueReturn) == 0:
raise APIError(24, 'Chan address is already present.')
address = queueReturn[0]
#add address to addressbook
queryreturn = sqlQuery("SELECT address FROM addressbook WHERE address=?", address)
if queryreturn == []:
sqlExecute("INSERT INTO addressbook VALUES(?,?)", label, address)
shared.UISignalQueue.put(('rerenderInboxFromLabels',''))
shared.UISignalQueue.put(('rerenderSentToLabels',''))
shared.UISignalQueue.put(('rerenderAddressBook',''))
return address
elif method == 'joinChan':
if len(params) < 2:
raise APIError(0, 'I need two parameters.')
elif len(params) == 2:
passphrase, suppliedAddress= params
passphrase = self._decode(passphrase, "base64")
if len(passphrase) == 0:
raise APIError(1, 'The specified passphrase is blank.')
# It would be nice to make the label the passphrase but it is
# possible that the passphrase contains non-utf-8 characters.
try:
unicode(passphrase, 'utf-8')
label = str_chan + ' ' + passphrase
except:
label = str_chan + ' ' + repr(passphrase)
status, addressVersionNumber, streamNumber, toRipe = self._verifyAddress(suppliedAddress)
suppliedAddress = addBMIfNotPresent(suppliedAddress)
shared.apiAddressGeneratorReturnQueue.queue.clear()
shared.addressGeneratorQueue.put(('joinChan', suppliedAddress, label, passphrase))
addressGeneratorReturnValue = shared.apiAddressGeneratorReturnQueue.get()
if addressGeneratorReturnValue == 'chan name does not match address':
raise APIError(18, 'Chan name does not match address.')
if len(addressGeneratorReturnValue) == 0:
raise APIError(24, 'Chan address is already present.')
createdAddress = addressGeneratorReturnValue[0]
#add address to addressbook
queryreturn = sqlQuery("SELECT address FROM addressbook WHERE address=?", createdAddress)
if queryreturn == []:
sqlExecute("INSERT INTO addressbook VALUES(?,?)", label, createdAddress)
shared.UISignalQueue.put(('rerenderInboxFromLabels',''))
shared.UISignalQueue.put(('rerenderSentToLabels',''))
shared.UISignalQueue.put(('rerenderAddressBook',''))
return "success"
elif method == 'leaveChan':
if len(params) == 0:
raise APIError(0, 'I need parameters.')
elif len(params) == 1:
address, = params
status, addressVersionNumber, streamNumber, toRipe = self._verifyAddress(address)
address = addBMIfNotPresent(address)
if not shared.config.has_section(address):
raise APIError(13, 'Could not find this address in your keys.dat file.')
if not shared.safeConfigGetBoolean(address, 'chan'):
raise APIError(25, 'Specified address is not a chan address. Use deleteAddress API call instead.')
shared.config.remove_section(address)
with open(shared.appdata + 'keys.dat', 'wb') as configfile:
shared.config.write(configfile)
sqlExecute('''DELETE FROM addressbook WHERE address=?''', address)
shared.UISignalQueue.put(('rerenderInboxFromLabels',''))
shared.UISignalQueue.put(('rerenderSentToLabels',''))
shared.UISignalQueue.put(('rerenderAddressBook',''))
return 'success'
elif method == 'deleteAddress':
if len(params) == 0:
raise APIError(0, 'I need parameters.')
elif len(params) == 1:
address, = params
status, addressVersionNumber, streamNumber, toRipe = self._verifyAddress(address)
address = addBMIfNotPresent(address)
if not shared.config.has_section(address):
raise APIError(13, 'Could not find this address in your keys.dat file.')
shared.config.remove_section(address)
with open(shared.appdata + 'keys.dat', 'wb') as configfile:
shared.config.write(configfile)
shared.UISignalQueue.put(('rerenderInboxFromLabels',''))
shared.UISignalQueue.put(('rerenderSentToLabels',''))
shared.reloadMyAddressHashes()
return 'success'
elif method == 'getAllInboxMessages':
queryreturn = sqlQuery(
'''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox where folder='inbox' ORDER BY received''')
@ -818,6 +927,15 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
else:
networkStatus = 'connectedAndReceivingIncomingConnections'
return json.dumps({'networkConnections':len(shared.connectedHostsList),'numberOfMessagesProcessed':shared.numberOfMessagesProcessed, 'numberOfBroadcastsProcessed':shared.numberOfBroadcastsProcessed, 'numberOfPubkeysProcessed':shared.numberOfPubkeysProcessed, 'networkStatus':networkStatus, 'softwareName':'PyBitmessage','softwareVersion':shared.softwareVersion}, indent=4, separators=(',', ': '))
elif method == 'decodeAddress':
#decode an address
if len(params) != 1:
raise APIError(0, 'I need 1 parameter!')
address, = params
status, addressVersion, streamNumber, ripe = decodeAddress(address)
return json.dumps({'status':status, 'addressVersion':addressVersion,
'streamNumber':streamNumber, 'ripe':ripe.encode('base64')}, indent=4,
separators=(',', ': '))
else:
raise APIError(20, 'Invalid method: %s' % method)

View File

@ -315,10 +315,15 @@ class singleWorker(threading.Thread):
shared.broadcastToSendDataQueues((
streamNumber, 'advertiseobject', inventoryHash))
shared.UISignalQueue.put(('updateStatusBar', ''))
try:
shared.config.set(
myAddress, 'lastpubkeysendtime', str(int(time.time())))
with open(shared.appdata + 'keys.dat', 'wb') as configfile:
shared.config.write(configfile)
except:
# The user deleted the address out of the keys.dat file before this
# finished.
pass
def sendBroadcast(self):
queryreturn = sqlQuery(