From db81f0c11ed9f58cd585ed95f6ffb1b7d2b0635c Mon Sep 17 00:00:00 2001 From: "Grant T. Olson" Date: Thu, 5 Sep 2013 06:31:52 -0400 Subject: [PATCH 1/2] Add add/deleteAddressBook APIs, extract address verification into reuable code, and make some QT stuff re-renderable --- src/bitmessagemain.py | 114 +++++++++++++++-------------------- src/bitmessageqt/__init__.py | 32 +++++++--- 2 files changed, 72 insertions(+), 74 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index a73f81e1..229b8a16 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -148,6 +148,25 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): except TypeError as e: raise APIError(22, "Decode error - " + str(e)) + def _verifyAddress(self, address): + status, addressVersionNumber, streamNumber, ripe = decodeAddress(address) + if status != 'success': + logger.warn('API Error 0007: Could not decode address %s. Status: %s.', address, status) + + if status == 'checksumfailed': + raise APIError(8, 'Checksum failed for address: ' + address) + if status == 'invalidcharacters': + raise APIError(9, 'Invalid characters in address: ' + address) + if status == 'versiontoohigh': + raise APIError(10, 'Address version number too high (or zero) in address: ' + address) + raise APIError(7, 'Could not decode address: ' + address + ' : ' + status) + if addressVersionNumber < 2 or addressVersionNumber > 3: + raise APIError(11, 'The address version number currently must be 2 or 3. Others aren\'t supported. Check the address.') + if streamNumber != 1: + raise APIError(12, 'The stream number must be 1. Others aren\'t supported. Check the address.') + + return (status, addressVersionNumber, streamNumber, ripe) + def _handle_request(self, method, params): if method == 'helloWorld': (a, b) = params @@ -183,6 +202,33 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): data += json.dumps({'label':label.encode('base64'), 'address': address}, indent=4, separators=(',', ': ')) data += ']}' return data + elif method == 'addAddressBook' or method == 'addAddressbook': + if len(params) != 2: + raise APIError(0, "I need label and address") + label, address = 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) + shared.UISignalQueue.put(('rerenderInboxFromLabels','')) + shared.UISignalQueue.put(('rerenderSentToLabels','')) + shared.UISignalQueue.put(('rerenderAddressBook','')) + return "Added address %s to address book" % address + elif method == 'deleteAddressBook' or method == 'deleteAddressbook': + if len(params) != 1: + 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!') @@ -496,40 +542,10 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): raise APIError(6, 'The encoding type must be 2 because that is the only one this program currently supports.') subject = self._decode(subject, "base64") message = self._decode(message, "base64") - status, addressVersionNumber, streamNumber, toRipe = decodeAddress( - toAddress) - if status != 'success': - logger.warn('API Error 0007: Could not decode address %s. Status: %s.', toAddress, status) - - if status == 'checksumfailed': - raise APIError(8, 'Checksum failed for address: ' + toAddress) - if status == 'invalidcharacters': - raise APIError(9, 'Invalid characters in address: ' + toAddress) - if status == 'versiontoohigh': - raise APIError(10, 'Address version number too high (or zero) in address: ' + toAddress) - raise APIError(7, 'Could not decode address: ' + toAddress + ' : ' + status) - if addressVersionNumber < 2 or addressVersionNumber > 3: - raise APIError(11, 'The address version number currently must be 2 or 3. Others aren\'t supported. Check the toAddress.') - if streamNumber != 1: - raise APIError(12, 'The stream number must be 1. Others aren\'t supported. Check the toAddress.') - status, addressVersionNumber, streamNumber, fromRipe = decodeAddress( - fromAddress) - if status != 'success': - logger.warn('API Error 0007: Could not decode address %s. Status: %s.', fromAddress, status) - - if status == 'checksumfailed': - raise APIError(8, 'Checksum failed for address: ' + fromAddress) - if status == 'invalidcharacters': - raise APIError(9, 'Invalid characters in address: ' + fromAddress) - if status == 'versiontoohigh': - raise APIError(10, 'Address version number too high (or zero) in address: ' + fromAddress) - raise APIError(7, 'Could not decode address: ' + fromAddress + ' : ' + status) - if addressVersionNumber < 2 or addressVersionNumber > 3: - raise APIError(11, 'The address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.') - if streamNumber != 1: - raise APIError(12, 'The stream number must be 1. Others aren\'t supported. Check the fromAddress.') toAddress = addBMIfNotPresent(toAddress) fromAddress = addBMIfNotPresent(fromAddress) + status, addressVersionNumber, streamNumber, toRipe = self._verifyAddress(toAddress) + self._verifyAddress(fromAddress) try: fromAddressEnabled = shared.config.getboolean( fromAddress, 'enabled') @@ -570,23 +586,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): subject = self._decode(subject, "base64") message = self._decode(message, "base64") - status, addressVersionNumber, streamNumber, fromRipe = decodeAddress( - fromAddress) - if status != 'success': - logger.warn('API Error 0007: Could not decode address %s. Status: %s.', fromAddress, status) - - if status == 'checksumfailed': - raise APIError(8, 'Checksum failed for address: ' + fromAddress) - if status == 'invalidcharacters': - raise APIError(9, 'Invalid characters in address: ' + fromAddress) - if status == 'versiontoohigh': - raise APIError(10, 'Address version number too high (or zero) in address: ' + fromAddress) - raise APIError(7, 'Could not decode address: ' + fromAddress + ' : ' + status) - if addressVersionNumber < 2 or addressVersionNumber > 3: - raise APIError(11, 'the address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.') - if streamNumber != 1: - raise APIError(12, 'the stream number must be 1. Others aren\'t supported. Check the fromAddress.') fromAddress = addBMIfNotPresent(fromAddress) + self._verifyAddress(fromAddress) try: fromAddressEnabled = shared.config.getboolean( fromAddress, 'enabled') @@ -638,22 +639,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): if len(params) > 2: raise APIError(0, 'I need either 1 or 2 parameters!') address = addBMIfNotPresent(address) - status, addressVersionNumber, streamNumber, toRipe = decodeAddress( - address) - if status != 'success': - logger.warn('API Error 0007: Could not decode address %s. Status: %s.', address, status) - - if status == 'checksumfailed': - raise APIError(8, 'Checksum failed for address: ' + address) - if status == 'invalidcharacters': - raise APIError(9, 'Invalid characters in address: ' + address) - if status == 'versiontoohigh': - raise APIError(10, 'Address version number too high (or zero) in address: ' + address) - raise APIError(7, 'Could not decode address: ' + address + ' : ' + status) - if addressVersionNumber < 2 or addressVersionNumber > 3: - raise APIError(11, 'The address version number currently must be 2 or 3. Others aren\'t supported.') - if streamNumber != 1: - raise APIError(12, 'The stream number must be 1. Others aren\'t supported.') + self._verifyAddress(address) # First we must check to see if the address is already in the # subscriptions list. queryreturn = sqlQuery('''select * from subscriptions where address=?''', address) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 209c7129..6fd3615c 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -351,16 +351,7 @@ class MyForm(QtGui.QMainWindow): self.loadSent() # Initialize the address book - queryreturn = sqlQuery('SELECT * FROM addressbook') - for row in queryreturn: - label, address = row - self.ui.tableWidgetAddressBook.insertRow(0) - newItem = QtGui.QTableWidgetItem(unicode(label, 'utf-8')) - self.ui.tableWidgetAddressBook.setItem(0, 0, newItem) - newItem = QtGui.QTableWidgetItem(address) - newItem.setFlags( - QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) - self.ui.tableWidgetAddressBook.setItem(0, 1, newItem) + self.rerenderAddressBook() # Initialize the Subscriptions self.rerenderSubscriptions() @@ -427,6 +418,10 @@ class MyForm(QtGui.QMainWindow): "setStatusIcon(PyQt_PyObject)"), self.setStatusIcon) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( "rerenderInboxFromLabels()"), self.rerenderInboxFromLabels) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( + "rerenderSentToLabels()"), self.rerenderSentToLabels) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( + "rerenderAddressBook()"), self.rerenderAddressBook) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( "rerenderSubscriptions()"), self.rerenderSubscriptions) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( @@ -1478,6 +1473,19 @@ class MyForm(QtGui.QMainWindow): self.ui.tableWidgetSent.item( i, 0).setText(unicode(toLabel, 'utf-8')) + def rerenderAddressBook(self): + self.ui.tableWidgetAddressBook.setRowCount(0) + queryreturn = sqlQuery('SELECT * FROM addressbook') + for row in queryreturn: + label, address = row + self.ui.tableWidgetAddressBook.insertRow(0) + newItem = QtGui.QTableWidgetItem(unicode(label, 'utf-8')) + self.ui.tableWidgetAddressBook.setItem(0, 0, newItem) + newItem = QtGui.QTableWidgetItem(address) + newItem.setFlags( + QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) + self.ui.tableWidgetAddressBook.setItem(0, 1, newItem) + def rerenderSubscriptions(self): self.ui.tableWidgetSubscriptions.setRowCount(0) queryreturn = sqlQuery('SELECT label, address, enabled FROM subscriptions') @@ -3185,6 +3193,10 @@ class UISignaler(QThread): self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"), data) elif command == 'rerenderInboxFromLabels': self.emit(SIGNAL("rerenderInboxFromLabels()")) + elif command == 'rerenderSentToLabels': + self.emit(SIGNAL("rerenderSentToLabels()")) + elif command == 'rerenderAddressBook': + self.emit(SIGNAL("rerenderAddressBook()")) elif command == 'rerenderSubscriptions': self.emit(SIGNAL("rerenderSubscriptions()")) elif command == 'removeInboxRowByMsgid': From bfd79e0ae18bdd88533a53ee35f06ea5cefe09b0 Mon Sep 17 00:00:00 2001 From: "Grant T. Olson" Date: Thu, 5 Sep 2013 06:42:12 -0400 Subject: [PATCH 2/2] Use same argument order as addSubscription for addAddressBook --- src/bitmessagemain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 229b8a16..dce43f40 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -205,7 +205,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): elif method == 'addAddressBook' or method == 'addAddressbook': if len(params) != 2: raise APIError(0, "I need label and address") - label, address = params + address, label = params label = self._decode(label, "base64") address = addBMIfNotPresent(address) self._verifyAddress(address)