Add: re-merge API changes
API handler was rewritten in upstream, previous merge overwrote the changse. This re-enables them.
This commit is contained in:
parent
d3345e051a
commit
bde5bfc42e
619
src/api.py
619
src/api.py
|
@ -149,218 +149,16 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
|
||||||
|
|
||||||
return (status, addressVersionNumber, streamNumber, ripe)
|
return (status, addressVersionNumber, streamNumber, ripe)
|
||||||
|
|
||||||
def _handle_request(self, method, params):
|
|
||||||
if method == 'helloWorld':
|
|
||||||
(a, b) = params
|
|
||||||
return a + '-' + b
|
|
||||||
elif method == 'add':
|
|
||||||
(a, b) = params
|
|
||||||
return a + b
|
|
||||||
elif method == 'statusBar':
|
|
||||||
message, = params
|
|
||||||
shared.UISignalQueue.put(('updateStatusBar', message))
|
|
||||||
elif method == 'listAddresses' or method == 'listAddresses2':
|
|
||||||
data = '{"addresses":['
|
|
||||||
configSections = shared.config.sections()
|
|
||||||
for addressInKeysFile in configSections:
|
|
||||||
if addressInKeysFile != 'bitmessagesettings':
|
|
||||||
status, addressVersionNumber, streamNumber, hash01 = decodeAddress(
|
|
||||||
addressInKeysFile)
|
|
||||||
if len(data) > 20:
|
|
||||||
data += ','
|
|
||||||
if shared.config.has_option(addressInKeysFile, 'chan'):
|
|
||||||
chan = shared.config.getboolean(addressInKeysFile, 'chan')
|
|
||||||
else:
|
|
||||||
chan = False
|
|
||||||
label = shared.config.get(addressInKeysFile, 'label')
|
|
||||||
if method == 'listAddresses2':
|
|
||||||
label = label.encode('base64')
|
|
||||||
data += json.dumps({'label': label, 'address': addressInKeysFile, 'stream':
|
|
||||||
streamNumber, 'enabled': shared.config.getboolean(addressInKeysFile, 'enabled'), 'chan': chan}, indent=4, separators=(',', ': '))
|
|
||||||
data += ']}'
|
|
||||||
return data
|
|
||||||
elif method == 'listAddressBookEntries' or method == 'listAddressbook': # the listAddressbook alias should be removed eventually.
|
|
||||||
queryreturn = sqlQuery('''SELECT label, address from addressbook''')
|
|
||||||
if len(params) == 1:
|
|
||||||
label, = params
|
|
||||||
label = self._decode(label, "base64")
|
|
||||||
queryreturn = sqlQuery('''SELECT label, address from addressbook WHERE label = ?''', label)
|
|
||||||
elif len(params) > 1:
|
|
||||||
raise APIError(0, "Too many paremeters, max 1")
|
|
||||||
data = '{"addresses":['
|
|
||||||
for row in queryreturn:
|
|
||||||
label, address = row
|
|
||||||
label = shared.fixPotentiallyInvalidUTF8Data(label)
|
|
||||||
if len(data) > 20:
|
|
||||||
data += ','
|
|
||||||
data += json.dumps({'label':label.encode('base64'), 'address': address}, indent=4, separators=(',', ': '))
|
|
||||||
data += ']}'
|
|
||||||
return data
|
|
||||||
elif method == 'getAddressBookEntry': # search by label
|
|
||||||
if len(params) != 1:
|
|
||||||
raise APIError(0, "I need a label")
|
|
||||||
label, = params
|
|
||||||
label = self._decode(label, "base64")
|
|
||||||
queryreturn = sqlQuery('''SELECT label, address from addressbook WHERE label = ?''', label)
|
|
||||||
data = '{"address":['
|
|
||||||
for row in queryreturn:
|
|
||||||
label, address = row
|
|
||||||
label = shared.fixPotentiallyInvalidUTF8Data(label)
|
|
||||||
if len(data) > 20:
|
|
||||||
data += ','
|
|
||||||
data += json.dumps({'label':label.encode('base64'), 'address': address}, indent=4, separators=(',', ': '))
|
|
||||||
data += ']}'
|
|
||||||
return data
|
|
||||||
|
|
||||||
elif method == 'addAddressBookEntry' or method == 'addAddressbook': # the addAddressbook alias should be deleted eventually.
|
#Request Handlers
|
||||||
if len(params) != 2:
|
|
||||||
raise APIError(0, "I need label and address")
|
|
||||||
address, label = params
|
|
||||||
label = self._decode(label, "base64")
|
|
||||||
address = addBMIfNotPresent(address)
|
|
||||||
self._verifyAddress(address)
|
|
||||||
queryreturn = sqlQuery("SELECT address FROM addressbook WHERE address=?", address)
|
|
||||||
if queryreturn != []:
|
|
||||||
raise APIError(16, 'You already have this address in your address book.')
|
|
||||||
|
|
||||||
sqlExecute("INSERT INTO addressbook VALUES(?,?)", label, address)
|
def HandleListAddresses(self, method):
|
||||||
shared.UISignalQueue.put(('rerenderInboxFromLabels',''))
|
data = '{"addresses":['
|
||||||
shared.UISignalQueue.put(('rerenderSentToLabels',''))
|
configSections = shared.config.sections()
|
||||||
shared.UISignalQueue.put(('rerenderAddressBook',''))
|
for addressInKeysFile in configSections:
|
||||||
return "Added address %s to address book" % address
|
if addressInKeysFile != 'bitmessagesettings':
|
||||||
elif method == 'deleteAddressBookEntry' or method == 'deleteAddressbook': # The deleteAddressbook alias should be deleted eventually.
|
status, addressVersionNumber, streamNumber, hash01 = decodeAddress(
|
||||||
if len(params) != 1:
|
addressInKeysFile)
|
||||||
raise APIError(0, "I need an address")
|
|
||||||
address, = params
|
|
||||||
address = addBMIfNotPresent(address)
|
|
||||||
self._verifyAddress(address)
|
|
||||||
sqlExecute('DELETE FROM addressbook WHERE address=?', address)
|
|
||||||
shared.UISignalQueue.put(('rerenderInboxFromLabels',''))
|
|
||||||
shared.UISignalQueue.put(('rerenderSentToLabels',''))
|
|
||||||
shared.UISignalQueue.put(('rerenderAddressBook',''))
|
|
||||||
return "Deleted address book entry for %s if it existed" % address
|
|
||||||
elif method == 'createRandomAddress':
|
|
||||||
if len(params) == 0:
|
|
||||||
raise APIError(0, 'I need parameters!')
|
|
||||||
elif len(params) == 1:
|
|
||||||
label, = params
|
|
||||||
eighteenByteRipe = False
|
|
||||||
nonceTrialsPerByte = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultnoncetrialsperbyte')
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 2:
|
|
||||||
label, eighteenByteRipe = params
|
|
||||||
nonceTrialsPerByte = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultnoncetrialsperbyte')
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 3:
|
|
||||||
label, eighteenByteRipe, totalDifficulty = params
|
|
||||||
nonceTrialsPerByte = int(
|
|
||||||
shared.networkDefaultProofOfWorkNonceTrialsPerByte * totalDifficulty)
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 4:
|
|
||||||
label, eighteenByteRipe, totalDifficulty, smallMessageDifficulty = params
|
|
||||||
nonceTrialsPerByte = int(
|
|
||||||
shared.networkDefaultProofOfWorkNonceTrialsPerByte * totalDifficulty)
|
|
||||||
payloadLengthExtraBytes = int(
|
|
||||||
shared.networkDefaultPayloadLengthExtraBytes * smallMessageDifficulty)
|
|
||||||
else:
|
|
||||||
raise APIError(0, 'Too many parameters!')
|
|
||||||
label = self._decode(label, "base64")
|
|
||||||
try:
|
|
||||||
unicode(label, 'utf-8')
|
|
||||||
except:
|
|
||||||
raise APIError(17, 'Label is not valid UTF-8 data.')
|
|
||||||
shared.apiAddressGeneratorReturnQueue.queue.clear()
|
|
||||||
streamNumberForAddress = 1
|
|
||||||
shared.addressGeneratorQueue.put((
|
|
||||||
'createRandomAddress', 4, streamNumberForAddress, label, 1, "", eighteenByteRipe, nonceTrialsPerByte, payloadLengthExtraBytes))
|
|
||||||
return shared.apiAddressGeneratorReturnQueue.get()
|
|
||||||
elif method == 'createDeterministicAddresses':
|
|
||||||
if len(params) == 0:
|
|
||||||
raise APIError(0, 'I need parameters!')
|
|
||||||
elif len(params) == 1:
|
|
||||||
passphrase, = params
|
|
||||||
numberOfAddresses = 1
|
|
||||||
addressVersionNumber = 0
|
|
||||||
streamNumber = 0
|
|
||||||
eighteenByteRipe = False
|
|
||||||
nonceTrialsPerByte = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultnoncetrialsperbyte')
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 2:
|
|
||||||
passphrase, numberOfAddresses = params
|
|
||||||
addressVersionNumber = 0
|
|
||||||
streamNumber = 0
|
|
||||||
eighteenByteRipe = False
|
|
||||||
nonceTrialsPerByte = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultnoncetrialsperbyte')
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 3:
|
|
||||||
passphrase, numberOfAddresses, addressVersionNumber = params
|
|
||||||
streamNumber = 0
|
|
||||||
eighteenByteRipe = False
|
|
||||||
nonceTrialsPerByte = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultnoncetrialsperbyte')
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 4:
|
|
||||||
passphrase, numberOfAddresses, addressVersionNumber, streamNumber = params
|
|
||||||
eighteenByteRipe = False
|
|
||||||
nonceTrialsPerByte = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultnoncetrialsperbyte')
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 5:
|
|
||||||
passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe = params
|
|
||||||
nonceTrialsPerByte = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultnoncetrialsperbyte')
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 6:
|
|
||||||
passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe, totalDifficulty = params
|
|
||||||
nonceTrialsPerByte = int(
|
|
||||||
shared.networkDefaultProofOfWorkNonceTrialsPerByte * totalDifficulty)
|
|
||||||
payloadLengthExtraBytes = shared.config.get(
|
|
||||||
'bitmessagesettings', 'defaultpayloadlengthextrabytes')
|
|
||||||
elif len(params) == 7:
|
|
||||||
passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe, totalDifficulty, smallMessageDifficulty = params
|
|
||||||
nonceTrialsPerByte = int(
|
|
||||||
shared.networkDefaultProofOfWorkNonceTrialsPerByte * totalDifficulty)
|
|
||||||
payloadLengthExtraBytes = int(
|
|
||||||
shared.networkDefaultPayloadLengthExtraBytes * smallMessageDifficulty)
|
|
||||||
else:
|
|
||||||
raise APIError(0, 'Too many parameters!')
|
|
||||||
if len(passphrase) == 0:
|
|
||||||
raise APIError(1, 'The specified passphrase is blank.')
|
|
||||||
if not isinstance(eighteenByteRipe, bool):
|
|
||||||
raise APIError(23, 'Bool expected in eighteenByteRipe, saw %s instead' % type(eighteenByteRipe))
|
|
||||||
passphrase = self._decode(passphrase, "base64")
|
|
||||||
if addressVersionNumber == 0: # 0 means "just use the proper addressVersionNumber"
|
|
||||||
addressVersionNumber = 4
|
|
||||||
if addressVersionNumber != 3 and addressVersionNumber != 4:
|
|
||||||
raise APIError(2,'The address version number currently must be 3, 4, or 0 (which means auto-select). ' + addressVersionNumber + ' isn\'t supported.')
|
|
||||||
if streamNumber == 0: # 0 means "just use the most available stream"
|
|
||||||
streamNumber = 1
|
|
||||||
if streamNumber != 1:
|
|
||||||
raise APIError(3,'The stream number must be 1 (or 0 which means auto-select). Others aren\'t supported.')
|
|
||||||
if numberOfAddresses == 0:
|
|
||||||
raise APIError(4, 'Why would you ask me to generate 0 addresses for you?')
|
|
||||||
if numberOfAddresses > 999:
|
|
||||||
raise APIError(5, 'You have (accidentally?) specified too many addresses to make. Maximum 999. This check only exists to prevent mischief; if you really want to create more addresses than this, contact the Bitmessage developers and we can modify the check or you can do it yourself by searching the source code for this message.')
|
|
||||||
shared.apiAddressGeneratorReturnQueue.queue.clear()
|
|
||||||
logger.debug('Requesting that the addressGenerator create %s addresses.', numberOfAddresses)
|
|
||||||
shared.addressGeneratorQueue.put(
|
|
||||||
('createDeterministicAddresses', addressVersionNumber, streamNumber,
|
|
||||||
'unused API address', numberOfAddresses, passphrase, eighteenByteRipe, nonceTrialsPerByte, payloadLengthExtraBytes))
|
|
||||||
data = '{"addresses":['
|
|
||||||
queueReturn = shared.apiAddressGeneratorReturnQueue.get()
|
|
||||||
for item in queueReturn:
|
|
||||||
if len(data) > 20:
|
if len(data) > 20:
|
||||||
data += ','
|
data += ','
|
||||||
if shared.config.has_option(addressInKeysFile, 'chan'):
|
if shared.config.has_option(addressInKeysFile, 'chan'):
|
||||||
|
@ -376,7 +174,14 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def HandleListAddressBookEntries(self, params):
|
def HandleListAddressBookEntries(self, params):
|
||||||
queryreturn = sqlQuery('''SELECT label, address from addressbook''')
|
if len(params) == 1:
|
||||||
|
label, = params
|
||||||
|
label = self._decode(label, "base64")
|
||||||
|
queryreturn = sqlQuery('''SELECT label, address from addressbook WHERE label = ?''', label)
|
||||||
|
elif len(params) > 1:
|
||||||
|
raise APIError(0, "Too many paremeters, max 1")
|
||||||
|
else:
|
||||||
|
queryreturn = sqlQuery('''SELECT label, address from addressbook''')
|
||||||
data = '{"addresses":['
|
data = '{"addresses":['
|
||||||
for row in queryreturn:
|
for row in queryreturn:
|
||||||
label, address = row
|
label, address = row
|
||||||
|
@ -565,104 +370,127 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
|
||||||
streamNumber, 'unused API address', numberOfAddresses, passphrase, eighteenByteRipe))
|
streamNumber, 'unused API address', numberOfAddresses, passphrase, eighteenByteRipe))
|
||||||
return shared.apiAddressGeneratorReturnQueue.get()
|
return shared.apiAddressGeneratorReturnQueue.get()
|
||||||
|
|
||||||
elif method == 'getAllInboxMessages':
|
def HandleCreateChan(self, params):
|
||||||
queryreturn = sqlQuery(
|
if len(params) == 0:
|
||||||
'''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox where folder='inbox' ORDER BY received''')
|
raise APIError(0, 'I need parameters.')
|
||||||
data = '{"inboxMessages":['
|
elif len(params) == 1:
|
||||||
for row in queryreturn:
|
passphrase, = params
|
||||||
msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row
|
passphrase = self._decode(passphrase, "base64")
|
||||||
subject = shared.fixPotentiallyInvalidUTF8Data(subject)
|
if len(passphrase) == 0:
|
||||||
message = shared.fixPotentiallyInvalidUTF8Data(message)
|
raise APIError(1, 'The specified passphrase is blank.')
|
||||||
if len(data) > 25:
|
# It would be nice to make the label the passphrase but it is
|
||||||
data += ','
|
# possible that the passphrase contains non-utf-8 characters.
|
||||||
data += json.dumps({'msgid': msgid.encode('hex'), 'toAddress': toAddress, 'fromAddress': fromAddress, 'subject': subject.encode(
|
try:
|
||||||
'base64'), 'message': message.encode('base64'), 'encodingType': encodingtype, 'receivedTime': received, 'read': read}, indent=4, separators=(',', ': '))
|
unicode(passphrase, 'utf-8')
|
||||||
data += ']}'
|
label = str_chan + ' ' + passphrase
|
||||||
return data
|
except:
|
||||||
elif method == 'getAllInboxMessageIds' or method == 'getAllInboxMessageIDs':
|
label = str_chan + ' ' + repr(passphrase)
|
||||||
queryreturn = sqlQuery(
|
|
||||||
'''SELECT msgid FROM inbox where folder='inbox' ORDER BY received''')
|
addressVersionNumber = 4
|
||||||
data = '{"inboxMessageIds":['
|
streamNumber = 1
|
||||||
for row in queryreturn:
|
shared.apiAddressGeneratorReturnQueue.queue.clear()
|
||||||
msgid = row[0]
|
logger.debug('Requesting that the addressGenerator create chan %s.', passphrase)
|
||||||
if len(data) > 25:
|
shared.addressGeneratorQueue.put(('createChan', addressVersionNumber, streamNumber, label, passphrase))
|
||||||
data += ','
|
queueReturn = shared.apiAddressGeneratorReturnQueue.get()
|
||||||
data += json.dumps({'msgid': msgid.encode('hex')}, indent=4, separators=(',', ': '))
|
if len(queueReturn) == 0:
|
||||||
data += ']}'
|
raise APIError(24, 'Chan address is already present.')
|
||||||
return data
|
address = queueReturn[0]
|
||||||
elif method == 'getInboxMessageById' or method == 'getInboxMessageByID':
|
return address
|
||||||
if len(params) == 0:
|
|
||||||
raise APIError(0, 'I need parameters!')
|
def HandleJoinChan(self, params):
|
||||||
elif len(params) == 1:
|
if len(params) < 2:
|
||||||
msgid = self._decode(params[0], "hex")
|
raise APIError(0, 'I need two parameters.')
|
||||||
elif len(params) >= 2:
|
elif len(params) == 2:
|
||||||
msgid = self._decode(params[0], "hex")
|
passphrase, suppliedAddress= params
|
||||||
readStatus = params[1]
|
passphrase = self._decode(passphrase, "base64")
|
||||||
if not isinstance(readStatus, bool):
|
if len(passphrase) == 0:
|
||||||
raise APIError(23, 'Bool expected in readStatus, saw %s instead.' % type(readStatus))
|
raise APIError(1, 'The specified passphrase is blank.')
|
||||||
queryreturn = sqlQuery('''SELECT read FROM inbox WHERE msgid=?''', msgid)
|
# It would be nice to make the label the passphrase but it is
|
||||||
# UPDATE is slow, only update if status is different
|
# possible that the passphrase contains non-utf-8 characters.
|
||||||
if queryreturn != [] and (queryreturn[0][0] == 1) != readStatus:
|
try:
|
||||||
sqlExecute('''UPDATE inbox set read = ? WHERE msgid=?''', readStatus, msgid)
|
unicode(passphrase, 'utf-8')
|
||||||
shared.UISignalQueue.put(('changedInboxUnread', None))
|
label = str_chan + ' ' + passphrase
|
||||||
queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox WHERE msgid=?''', msgid)
|
except:
|
||||||
data = '{"inboxMessage":['
|
label = str_chan + ' ' + repr(passphrase)
|
||||||
for row in queryreturn:
|
|
||||||
msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row
|
status, addressVersionNumber, streamNumber, toRipe = self._verifyAddress(suppliedAddress)
|
||||||
subject = shared.fixPotentiallyInvalidUTF8Data(subject)
|
suppliedAddress = addBMIfNotPresent(suppliedAddress)
|
||||||
message = shared.fixPotentiallyInvalidUTF8Data(message)
|
shared.apiAddressGeneratorReturnQueue.queue.clear()
|
||||||
data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'receivedTime':received, 'read': read}, indent=4, separators=(',', ': '))
|
shared.addressGeneratorQueue.put(('joinChan', suppliedAddress, label, passphrase))
|
||||||
data += ']}'
|
addressGeneratorReturnValue = shared.apiAddressGeneratorReturnQueue.get()
|
||||||
return data
|
|
||||||
elif method == 'getAllSentMessages':
|
if addressGeneratorReturnValue == 'chan name does not match address':
|
||||||
import pprint
|
raise APIError(18, 'Chan name does not match address.')
|
||||||
try:
|
if len(addressGeneratorReturnValue) == 0:
|
||||||
queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent where folder='sent' ORDER BY lastactiontime''')
|
raise APIError(24, 'Chan address is already present.')
|
||||||
except:
|
#TODO: this variable is not used to anything
|
||||||
print "Exception in getallSentMessages"
|
createdAddress = addressGeneratorReturnValue[0] # in case we ever want it for anything.
|
||||||
pprint.pprint (queryreturn)
|
return "success"
|
||||||
data = '{"sentMessages":['
|
|
||||||
if type(queryreturn) is list:
|
def HandleLeaveChan(self, params):
|
||||||
for row in queryreturn:
|
if len(params) == 0:
|
||||||
msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row
|
raise APIError(0, 'I need parameters.')
|
||||||
subject = shared.fixPotentiallyInvalidUTF8Data(subject)
|
elif len(params) == 1:
|
||||||
message = shared.fixPotentiallyInvalidUTF8Data(message)
|
address, = params
|
||||||
if len(data) > 25:
|
status, addressVersionNumber, streamNumber, toRipe = self._verifyAddress(address)
|
||||||
data += ','
|
address = addBMIfNotPresent(address)
|
||||||
data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':ackdata.encode('hex')}, indent=4, separators=(',', ': '))
|
if not shared.config.has_section(address):
|
||||||
else:
|
raise APIError(13, 'Could not find this address in your keys.dat file.')
|
||||||
print "queryreturn is not a list"
|
if not shared.safeConfigGetBoolean(address, 'chan'):
|
||||||
pprint.pprint (queryreturn)
|
raise APIError(25, 'Specified address is not a chan address. Use deleteAddress API call instead.')
|
||||||
data += ']}'
|
shared.config.remove_section(address)
|
||||||
return data
|
with open(shared.appdata + 'keys.dat', 'wb') as configfile:
|
||||||
elif method == 'getAllSentMessageIds' or method == 'getAllSentMessageIDs':
|
shared.config.write(configfile)
|
||||||
queryreturn = sqlQuery('''SELECT msgid FROM sent where folder='sent' ORDER BY lastactiontime''')
|
return 'success'
|
||||||
data = '{"sentMessageIds":['
|
|
||||||
for row in queryreturn:
|
def HandleDeleteAddress(self, params):
|
||||||
msgid = row[0]
|
if len(params) == 0:
|
||||||
if len(data) > 25:
|
raise APIError(0, 'I need parameters.')
|
||||||
data += ','
|
elif len(params) == 1:
|
||||||
data += json.dumps({'msgid':msgid.encode('hex')}, indent=4, separators=(',', ': '))
|
address, = params
|
||||||
data += ']}'
|
status, addressVersionNumber, streamNumber, toRipe = self._verifyAddress(address)
|
||||||
return data
|
address = addBMIfNotPresent(address)
|
||||||
elif method == 'getInboxMessagesByReceiver' or method == 'getInboxMessagesByAddress': #after some time getInboxMessagesByAddress should be removed
|
if not shared.config.has_section(address):
|
||||||
if len(params) == 0:
|
raise APIError(13, 'Could not find this address in your keys.dat file.')
|
||||||
raise APIError(0, 'I need parameters!')
|
shared.config.remove_section(address)
|
||||||
toAddress = params[0]
|
with open(shared.appdata + 'keys.dat', 'wb') as configfile:
|
||||||
queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype FROM inbox WHERE folder='inbox' AND toAddress=?''', toAddress)
|
shared.config.write(configfile)
|
||||||
data = '{"inboxMessages":['
|
shared.UISignalQueue.put(('rerenderInboxFromLabels',''))
|
||||||
for row in queryreturn:
|
shared.UISignalQueue.put(('rerenderSentToLabels',''))
|
||||||
msgid, toAddress, fromAddress, subject, received, message, encodingtype = row
|
shared.reloadMyAddressHashes()
|
||||||
subject = shared.fixPotentiallyInvalidUTF8Data(subject)
|
return 'success'
|
||||||
message = shared.fixPotentiallyInvalidUTF8Data(message)
|
|
||||||
if len(data) > 25:
|
def HandleGetAllInboxMessages(self, params):
|
||||||
data += ','
|
queryreturn = sqlQuery(
|
||||||
data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'receivedTime':received}, indent=4, separators=(',', ': '))
|
'''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox where folder='inbox' ORDER BY received''')
|
||||||
data += ']}'
|
data = '{"inboxMessages":['
|
||||||
return data
|
for row in queryreturn:
|
||||||
elif method == 'getSentMessageById' or method == 'getSentMessageByID':
|
msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row
|
||||||
if len(params) == 0:
|
subject = shared.fixPotentiallyInvalidUTF8Data(subject)
|
||||||
raise APIError(0, 'I need parameters!')
|
message = shared.fixPotentiallyInvalidUTF8Data(message)
|
||||||
|
if len(data) > 25:
|
||||||
|
data += ','
|
||||||
|
data += json.dumps({'msgid': msgid.encode('hex'), 'toAddress': toAddress, 'fromAddress': fromAddress, 'subject': subject.encode(
|
||||||
|
'base64'), 'message': message.encode('base64'), 'encodingType': encodingtype, 'receivedTime': received, 'read': read}, indent=4, separators=(',', ': '))
|
||||||
|
data += ']}'
|
||||||
|
return data
|
||||||
|
|
||||||
|
def HandleGetAllInboxMessageIds(self, params):
|
||||||
|
queryreturn = sqlQuery(
|
||||||
|
'''SELECT msgid FROM inbox where folder='inbox' ORDER BY received''')
|
||||||
|
data = '{"inboxMessageIds":['
|
||||||
|
for row in queryreturn:
|
||||||
|
msgid = row[0]
|
||||||
|
if len(data) > 25:
|
||||||
|
data += ','
|
||||||
|
data += json.dumps({'msgid': msgid.encode('hex')}, indent=4, separators=(',', ': '))
|
||||||
|
data += ']}'
|
||||||
|
return data
|
||||||
|
|
||||||
|
def HandleGetInboxMessageById(self, params):
|
||||||
|
if len(params) == 0:
|
||||||
|
raise APIError(0, 'I need parameters!')
|
||||||
|
elif len(params) == 1:
|
||||||
msgid = self._decode(params[0], "hex")
|
msgid = self._decode(params[0], "hex")
|
||||||
elif len(params) >= 2:
|
elif len(params) >= 2:
|
||||||
msgid = self._decode(params[0], "hex")
|
msgid = self._decode(params[0], "hex")
|
||||||
|
@ -1087,73 +915,112 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
|
||||||
'''SELECT hash, payload FROM inventory WHERE tag = '' and objecttype = 2 ; ''')
|
'''SELECT hash, payload FROM inventory WHERE tag = '' and objecttype = 2 ; ''')
|
||||||
with SqlBulkExecute() as sql:
|
with SqlBulkExecute() as sql:
|
||||||
for row in queryreturn:
|
for row in queryreturn:
|
||||||
transmitdata, = row
|
hash01, payload = row
|
||||||
data += json.dumps({'data':transmitdata.encode('hex')}, indent=4, separators=(',', ': '))
|
readPosition = 16 # Nonce length + time length
|
||||||
data += ']}'
|
readPosition += decodeVarint(payload[readPosition:readPosition+10])[1] # Stream Number length
|
||||||
return data
|
t = (payload[readPosition:readPosition+32],hash01)
|
||||||
elif method == 'clientStatus':
|
sql.execute('''UPDATE inventory SET tag=? WHERE hash=?; ''', *t)
|
||||||
if len(shared.connectedHostsList) == 0:
|
|
||||||
networkStatus = 'notConnected'
|
queryreturn = sqlQuery('''SELECT payload FROM inventory WHERE tag = ?''',
|
||||||
elif len(shared.connectedHostsList) > 0 and not shared.clientHasReceivedIncomingConnections:
|
requestedHash)
|
||||||
networkStatus = 'connectedButHaveNotReceivedIncomingConnections'
|
data = '{"receivedMessageDatas":['
|
||||||
else:
|
for row in queryreturn:
|
||||||
networkStatus = 'connectedAndReceivingIncomingConnections'
|
payload, = row
|
||||||
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=(',', ': '))
|
if len(data) > 25:
|
||||||
elif method == 'decodeAddress':
|
data += ','
|
||||||
# Return a meaningful decoding of an address.
|
data += json.dumps({'data':payload.encode('hex')}, indent=4, separators=(',', ': '))
|
||||||
if len(params) != 1:
|
data += ']}'
|
||||||
raise APIError(0, 'I need 1 parameter!')
|
return data
|
||||||
address, = params
|
|
||||||
status, addressVersion, streamNumber, ripe = decodeAddress(address)
|
def HandleClientStatus(self, params):
|
||||||
return json.dumps({'status':status, 'addressVersion':addressVersion,
|
if len(shared.connectedHostsList) == 0:
|
||||||
'streamNumber':streamNumber, 'ripe':ripe.encode('base64')}, indent=4,
|
networkStatus = 'notConnected'
|
||||||
separators=(',', ': '))
|
elif len(shared.connectedHostsList) > 0 and not shared.clientHasReceivedIncomingConnections:
|
||||||
elif method == 'getInboxCount':
|
networkStatus = 'connectedButHaveNotReceivedIncomingConnections'
|
||||||
#queryreturn = sqlQuery('''SELECT read, received < 'now' - 60 AS old, COUNT (*) AS cnt FROM inbox WHERE folder = 'inbox' GROUP BY read, old''')
|
else:
|
||||||
ret = {}
|
networkStatus = 'connectedAndReceivingIncomingConnections'
|
||||||
queryreturn = sqlQuery('''SELECT COUNT (*) AS cnt FROM inbox WHERE folder = 'inbox' AND read = 0 AND received < 'now' - 60''')
|
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=(',', ': '))
|
||||||
for row in queryreturn:
|
|
||||||
count, = row
|
def HandleDecodeAddress(self, params):
|
||||||
ret['oldread'] = count
|
# Return a meaningful decoding of an address.
|
||||||
queryreturn = sqlQuery('''SELECT COUNT (*) AS cnt FROM inbox WHERE folder = 'inbox' AND read = 1 AND received < 'now' - 60''')
|
if len(params) != 1:
|
||||||
for row in queryreturn:
|
raise APIError(0, 'I need 1 parameter!')
|
||||||
count, = row
|
address, = params
|
||||||
ret['oldunread'] = count
|
status, addressVersion, streamNumber, ripe = decodeAddress(address)
|
||||||
queryreturn = sqlQuery('''SELECT COUNT (*) AS cnt FROM inbox WHERE folder = 'inbox' AND read = 0 AND received >= 'now' - 60''')
|
return json.dumps({'status':status, 'addressVersion':addressVersion,
|
||||||
for row in queryreturn:
|
'streamNumber':streamNumber, 'ripe':ripe.encode('base64')}, indent=4,
|
||||||
count, = row
|
separators=(',', ': '))
|
||||||
ret['newread'] = count
|
|
||||||
queryreturn = sqlQuery('''SELECT COUNT (*) AS cnt FROM inbox WHERE folder = 'inbox' AND read = 1 AND received >= 'now' - 60''')
|
def HandleHelloWorld(self, params):
|
||||||
for row in queryreturn:
|
(a, b) = params
|
||||||
count, = row
|
return a + '-' + b
|
||||||
ret['newunread'] = count
|
|
||||||
data = '{"inboxCount":{'
|
def HandleAdd(self, params):
|
||||||
for key in ret:
|
(a, b) = params
|
||||||
val = ret[key]
|
return a + b
|
||||||
if len(data) > 16:
|
|
||||||
data += ','
|
def HandleStatusBar(self, params):
|
||||||
data += json.dumps({key:val}, indent=4, separators=(',', ': '))
|
message, = params
|
||||||
data += '}}'
|
shared.UISignalQueue.put(('updateStatusBar', message))
|
||||||
elif method == 'getSentCount':
|
|
||||||
ret = {}
|
def HandleDeleteAndVacuum(self, params):
|
||||||
queryreturn = sqlQuery('''SELECT COUNT (*) AS cnt FROM sent WHERE folder = 'sent' AND status = 'msgqueued' ''')
|
sqlStoredProcedure('deleteandvacuume')
|
||||||
for row in queryreturn:
|
return 'done'
|
||||||
count, = row
|
|
||||||
ret['queued'] = count
|
handlers = {}
|
||||||
queryreturn = sqlQuery('''SELECT COUNT (*) AS cnt FROM sent WHERE folder = 'sent' AND status = 'msgsent' ''')
|
handlers['helloWorld'] = HandleHelloWorld
|
||||||
for row in queryreturn:
|
handlers['add'] = HandleAdd
|
||||||
count, = row
|
handlers['statusBar'] = HandleStatusBar
|
||||||
ret['awaitingack'] = count
|
handlers['listAddresses'] = HandleListAddresses
|
||||||
data = '{"sentCount":{'
|
handlers['listAddressBookEntries'] = HandleListAddressBookEntries;
|
||||||
for key in ret:
|
handlers['listAddressbook'] = HandleListAddressBookEntries # the listAddressbook alias should be removed eventually.
|
||||||
val = ret[key]
|
handlers['addAddressBookEntry'] = HandleAddAddressBookEntry
|
||||||
if len(data) > 15:
|
handlers['addAddressbook'] = HandleAddAddressBookEntry # the addAddressbook alias should be deleted eventually.
|
||||||
data += ','
|
handlers['deleteAddressBookEntry'] = HandleDeleteAddressBookEntry
|
||||||
data += json.dumps({key:val}, indent=4, separators=(',', ': '))
|
handlers['deleteAddressbook'] = HandleDeleteAddressBookEntry # The deleteAddressbook alias should be deleted eventually.
|
||||||
data += '}}'
|
handlers['createRandomAddress'] = HandleCreateRandomAddress
|
||||||
elif method == 'deleteAndVacuum':
|
handlers['createDeterministicAddresses'] = HandleCreateDeterministicAddresses
|
||||||
sqlStoredProcedure('deleteandvacuume')
|
handlers['getDeterministicAddress'] = HandleGetDeterministicAddress
|
||||||
return 'done'
|
handlers['createChan'] = HandleCreateChan
|
||||||
|
handlers['joinChan'] = HandleJoinChan
|
||||||
|
handlers['leaveChan'] = HandleLeaveChan
|
||||||
|
handlers['deleteAddress'] = HandleDeleteAddress
|
||||||
|
handlers['getAllInboxMessages'] = HandleGetAllInboxMessages
|
||||||
|
handlers['getAllInboxMessageIds'] = HandleGetAllInboxMessageIds
|
||||||
|
handlers['getAllInboxMessageIDs'] = HandleGetAllInboxMessageIds
|
||||||
|
handlers['getInboxMessageById'] = HandleGetInboxMessageById
|
||||||
|
handlers['getInboxMessageByID'] = HandleGetInboxMessageById
|
||||||
|
handlers['getAllSentMessages'] = HandleGetAllSentMessages
|
||||||
|
handlers['getAllSentMessageIds'] = HandleGetAllSentMessageIds
|
||||||
|
handlers['getAllSentMessageIDs'] = HandleGetAllSentMessageIds
|
||||||
|
handlers['getInboxMessagesByReceiver'] = HandleInboxMessagesByReceiver
|
||||||
|
handlers['getInboxMessagesByAddress'] = HandleInboxMessagesByReceiver #after some time getInboxMessagesByAddress should be removed
|
||||||
|
handlers['getSentMessageById'] = HandleGetSentMessageById
|
||||||
|
handlers['getSentMessageByID'] = HandleGetSentMessageById
|
||||||
|
handlers['getSentMessagesByAddress'] = HandleGetSentMessagesByAddress
|
||||||
|
handlers['getSentMessagesBySender'] = HandleGetSentMessagesByAddress
|
||||||
|
handlers['getSentMessageByAckData'] = HandleGetSentMessagesByAckData
|
||||||
|
handlers['trashMessage'] = HandleTrashMessage
|
||||||
|
handlers['trashInboxMessage'] = HandleTrashInboxMessage
|
||||||
|
handlers['trashSentMessage'] = HandleTrashSentMessage
|
||||||
|
handlers['trashSentMessageByAckData'] = HandleTrashSentMessageByAckDAta
|
||||||
|
handlers['sendMessage'] = HandleSendMessage
|
||||||
|
handlers['sendBroadcast'] = HandleSendBroadcast
|
||||||
|
handlers['getStatus'] = HandleGetStatus
|
||||||
|
handlers['addSubscription'] = HandleAddSubscription
|
||||||
|
handlers['deleteSubscription'] = HandleDeleteSubscription
|
||||||
|
handlers['listSubscriptions'] = ListSubscriptions
|
||||||
|
handlers['disseminatePreEncryptedMsg'] = HandleDisseminatePreEncryptedMsg
|
||||||
|
handlers['disseminatePubkey'] = HandleDissimatePubKey
|
||||||
|
handlers['getMessageDataByDestinationHash'] = HandleGetMessageDataByDestinationHash
|
||||||
|
handlers['getMessageDataByDestinationTag'] = HandleGetMessageDataByDestinationHash
|
||||||
|
handlers['clientStatus'] = HandleClientStatus
|
||||||
|
handlers['decodeAddress'] = HandleDecodeAddress
|
||||||
|
handlers['deleteAndVacuum'] = HandleDeleteAndVacuum
|
||||||
|
|
||||||
|
def _handle_request(self, method, params):
|
||||||
|
if (self.handlers.has_key(method)):
|
||||||
|
return self.handlers[method](self ,params)
|
||||||
else:
|
else:
|
||||||
raise APIError(20, 'Invalid method: %s' % method)
|
raise APIError(20, 'Invalid method: %s' % method)
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user