diff --git a/INSTALL.md b/INSTALL.md index 1cb06c5b..4eb896eb 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -57,8 +57,8 @@ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" Now, install the required dependencies ``` -sudo port install python27 py27-pyqt4 openssl -sudo port install git-core +svn +doc +bash_completion +gitweb +brew install python pyqt +brew install git ``` Download and run PyBitmessage: diff --git a/Makefile b/Makefile index e0ff247a..07e8b7bc 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,8 @@ PREFIX?=/usr/local all: debug: source: - tar -cvzf ../${APP}_${VERSION}.orig.tar.gz ../${APP}-${VERSION} --exclude-vcs + tar -cvf ../${APP}_${VERSION}.orig.tar ../${APP}-${VERSION} --exclude-vcs + gzip -f9n ../${APP}_${VERSION}.orig.tar install: mkdir -p ${DESTDIR}/usr mkdir -p ${DESTDIR}${PREFIX} diff --git a/arch.sh b/arch.sh index 0ccc45b3..795ec69d 100755 --- a/arch.sh +++ b/arch.sh @@ -1,10 +1,11 @@ #!/bin/bash +GIT_APP=PyBitmessage APP=pybitmessage PREV_VERSION=0.3.5 VERSION=0.3.5 RELEASE=1 -ARCH_TYPE=`uname -m` +ARCH_TYPE=any CURRDIR=`pwd` SOURCE=archpackage/${APP}-${VERSION}.tar.gz @@ -25,24 +26,12 @@ make clean rm -f archpackage/*.gz # having the root directory called name-version seems essential -mv ../${APP} ../${APP}-${VERSION} +mv ../${GIT_APP} ../${APP}-${VERSION} tar -cvzf ${SOURCE} ../${APP}-${VERSION} --exclude-vcs # rename the root directory without the version number -mv ../${APP}-${VERSION} ../${APP} +mv ../${APP}-${VERSION} ../${GIT_APP} # calculate the MD5 checksum CHECKSM=$(md5sum ${SOURCE}) sed -i "s/md5sums[^)]*)/md5sums=(${CHECKSM%% *})/g" archpackage/PKGBUILD - -cd archpackage - -# Create the package -tar -c -f ${APP}-${VERSION}.pkg.tar . -sync -xz ${APP}-${VERSION}.pkg.tar -sync - -# Move back to the original directory -cd ${CURRDIR} - diff --git a/archpackage/PKGBUILD b/archpackage/PKGBUILD index efbf33e2..fd13b92a 100644 --- a/archpackage/PKGBUILD +++ b/archpackage/PKGBUILD @@ -3,13 +3,13 @@ pkgname=pybitmessage pkgver=0.3.5 pkgrel=1 pkgdesc="Bitmessage is a P2P communications protocol used to send encrypted messages to another person or to many subscribers. It is decentralized and trustless, meaning that you need-not inherently trust any entities like root certificate authorities. It uses strong authentication which means that the sender of a message cannot be spoofed, and it aims to hide "non-content" data, like the sender and receiver of messages, from passive eavesdroppers like those running warrantless wiretapping programs." -arch=('i686' 'x86_64') +arch=('any') url="https://github.com/Bitmessage/PyBitmessage" license=('MIT') groups=() -depends=('python2' 'qt4' 'python2-pyqt4' 'sqlite' 'openssl' 'gst123') +depends=('python2' 'qt4' 'python2-pyqt4' 'sqlite' 'openssl' 'mpg123') makedepends=() -optdepends=('python2-gevent') +optdepends=('python2-gevent: Python network library that uses greenlet and libevent for easy and scalable concurrency') provides=() conflicts=() replaces=() @@ -19,7 +19,7 @@ install= changelog= source=($pkgname-$pkgver.tar.gz) noextract=() -md5sums=() +md5sums=(ebf89129571571198473559b4b2e552c) build() { cd "$srcdir/$pkgname-$pkgver" ./configure --prefix=/usr diff --git a/debian.sh b/debian.sh index a4d6882e..ea46acaf 100755 --- a/debian.sh +++ b/debian.sh @@ -1,10 +1,10 @@ #!/bin/bash APP=pybitmessage -PREV_VERSION=0.3.4 +PREV_VERSION=0.3.5 VERSION=0.3.5 RELEASE=1 -ARCH_TYPE=`uname -m` +ARCH_TYPE=all DIR=${APP}-${VERSION} if [ $ARCH_TYPE == "x86_64" ]; then diff --git a/debian/changelog b/debian/changelog index 711f6488..8cdaff2c 100644 --- a/debian/changelog +++ b/debian/changelog @@ -14,7 +14,8 @@ pybitmessage (0.3.5-1) raring; urgency=low * Added Russian translation * Added search support in the UI * Added 'make uninstall' - * To improve OSX support, use PKCS5_PBKDF2_HMAC_SHA1 if PKCS5_PBKDF2_HMAC is unavailable + * To improve OSX support, use PKCS5_PBKDF2_HMAC_SHA1 + if PKCS5_PBKDF2_HMAC is unavailable * Added better warnings for OSX users who are using old versions of Python * Repaired debian packaging * Altered Makefile to avoid needing to chase changes diff --git a/debian/control b/debian/control index 5e016a1c..6fb419f4 100644 --- a/debian/control +++ b/debian/control @@ -1,4 +1,5 @@ Source: pybitmessage +Section: mail Priority: extra Maintainer: Bob Mottram (4096 bits) Build-Depends: debhelper (>= 9.0.0) @@ -7,8 +8,7 @@ Homepage: https://github.com/Bitmessage/PyBitmessage Vcs-Git: https://github.com/Bitmessage/PyBitmessage.git Package: pybitmessage -Section: mail -Architecture: any +Architecture: all Depends: ${shlibs:Depends}, ${misc:Depends}, python (>= 2.7.0), openssl, python-qt4, libqt4-dev (>= 4.8.0), python-qt4-dev, sqlite3, libsqlite3-dev, gst123 Suggests: libmessaging-menu-dev Description: Send encrypted messages diff --git a/generate.sh b/generate.sh index 3ab99536..4154fff7 100755 --- a/generate.sh +++ b/generate.sh @@ -26,7 +26,7 @@ packagemonkey -n "PyBitmessage" --version "0.3.5" --dir "." -l "mit" \ --dependspuppy "openssl, python-qt4, sqlite3, sqlite3-dev, " \ "python-openssl, python-sip, gst123" \ --dependsarch "python2, qt4, python2-pyqt4, sqlite, openssl, gst123" \ - --suggestsarch "python2-gevent" --pythonversion 2 \ + --suggestsarch "python2-gevent: Python network library that uses greenlet and libevent for easy and scalable concurrency" --pythonversion 2 \ --dependsebuild "dev-libs/openssl, dev-python/PyQt4[${PYTHON_USEDEP}]" \ --buildebuild "\${PYTHON_DEPS}" --pythonreq "sqlite" \ --repository "https://github.com/Bitmessage/PyBitmessage.git" diff --git a/puppypackage/pybitmessage-0.3.5.pet.specs b/puppypackage/pybitmessage-0.3.5.pet.specs index 939294e4..717cd281 100644 --- a/puppypackage/pybitmessage-0.3.5.pet.specs +++ b/puppypackage/pybitmessage-0.3.5.pet.specs @@ -1 +1 @@ -pybitmessage-0.3.5-1|PyBitmessage|0.3.5|1|Internet;mailnews;|7.2M||pybitmessage-0.3.5-1.pet|+openssl,+python-qt4,+sqlite3,+sqlite3-dev,+python-openssl,+python-sip,+gst123|Send encrypted messages|ubuntu|precise|5| +pybitmessage-0.3.5-1|PyBitmessage|0.3.5|1|Internet;mailnews;|3.8M||pybitmessage-0.3.5-1.pet|+openssl,+python-qt4,+sqlite3,+sqlite3-dev,+python-openssl,+python-sip,+gst123|Send encrypted messages|ubuntu|precise|5| diff --git a/rpmpackage/pybitmessage.spec b/rpmpackage/pybitmessage.spec index b15bd586..3af43532 100644 --- a/rpmpackage/pybitmessage.spec +++ b/rpmpackage/pybitmessage.spec @@ -6,6 +6,7 @@ License: MIT URL: https://github.com/Bitmessage/PyBitmessage Packager: Bob Mottram (4096 bits) Source0: http://yourdomainname.com/src/%{name}_%{version}.orig.tar.gz +BuildArch: noarch Group: Office/Email Requires: python, PyQt4, openssl-compat-bitcoin-libs, gst123 @@ -83,7 +84,8 @@ make install -B DESTDIR=%{buildroot} PREFIX=/usr - Added Russian translation - Added search support in the UI - Added 'make uninstall' -- To improve OSX support, use PKCS5_PBKDF2_HMAC_SHA1 if PKCS5_PBKDF2_HMAC is unavailable +- To improve OSX support, use PKCS5_PBKDF2_HMAC_SHA1 + if PKCS5_PBKDF2_HMAC is unavailable - Added better warnings for OSX users who are using old versions of Python - Repaired debian packaging - Altered Makefile to avoid needing to chase changes diff --git a/src/addresses.py b/src/addresses.py index a6a571f6..1db2105d 100644 --- a/src/addresses.py +++ b/src/addresses.py @@ -97,13 +97,18 @@ def calculateInventoryHash(data): return sha2.digest()[0:32] def encodeAddress(version,stream,ripe): - if version >= 2: + if version >= 2 and version < 4: if len(ripe) != 20: raise Exception("Programming error in encodeAddress: The length of a given ripe hash was not 20.") if ripe[:2] == '\x00\x00': ripe = ripe[2:] elif ripe[:1] == '\x00': ripe = ripe[1:] + elif version == 4: + if len(ripe) != 20: + raise Exception("Programming error in encodeAddress: The length of a given ripe hash was not 20.") + ripe = ripe.lstrip('\x00') + a = encodeVarint(version) + encodeVarint(stream) + ripe sha = hashlib.new('sha512') sha.update(a) @@ -164,7 +169,7 @@ def decodeAddress(address): #print 'addressVersionNumber', addressVersionNumber #print 'bytesUsedByVersionNumber', bytesUsedByVersionNumber - if addressVersionNumber > 3: + if addressVersionNumber > 4: print 'cannot decode address version numbers this high' status = 'versiontoohigh' return status,0,0,0 @@ -191,6 +196,15 @@ def decodeAddress(address): return 'ripetoolong',0,0,0 else: return 'otherproblem',0,0,0 + elif addressVersionNumber == 4: + if len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) > 20: + return 'ripetoolong',0,0,0 + elif len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) < 4: + return 'ripetooshort',0,0,0 + else: + x00string = '\x00' * (20 - len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4])) + return status,addressVersionNumber,streamNumber,x00string+data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4] + def addBMIfNotPresent(address): address = str(address).strip() diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index fd233bd5..721b6a8d 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -9,13 +9,6 @@ # The software version variable is now held in shared.py -# import ctypes -try: - from gevent import monkey - monkey.patch_all() -except ImportError as ex: - print "Not using the gevent module as it was not found. No need to worry." - import signal # Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully. # The next 3 are used for the API from SimpleXMLRPCServer import * @@ -38,10 +31,17 @@ from class_singleWorker import * from class_outgoingSynSender import * from class_singleListener import * from class_addressGenerator import * +from debug import logger # Helper Functions import helper_bootstrap +import sys +if sys.platform == 'darwin': + if float("{1}.{2}".format(*sys.version_info)) < 7.5: + logger.critical("You should use python 2.7.5 or greater. Your version: %s", "{0}.{1}.{2}".format(*sys.version_info)) + sys.exit(0) + def connectToStream(streamNumber): selfInitiatedConnections[streamNumber] = {} if sys.platform[0:3] == 'win': @@ -54,6 +54,13 @@ def connectToStream(streamNumber): a.start() +class APIError(Exception): + def __init__(self, error_number, error_message): + self.error_number = error_number + self.error_message = error_message + def __str__(self): + return "API Error %04i: %s" % (self.error_number, self.error_message) + # This is one of several classes that constitute the API # This class was written by Vaibhav Bhatia. Modified by Jonathan Warren (Atheros). # http://code.activestate.com/recipes/501148-xmlrpc-serverclient-which-does-cookie-handling-and/ @@ -127,20 +134,19 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): else: return False else: - print 'Authentication failed because header lacks Authentication field' + logger.warn('Authentication failed because header lacks Authentication field') time.sleep(2) return False return False - def _dispatch(self, method, params): - self.cookies = [] - - validuser = self.APIAuthenticateClient() - if not validuser: - time.sleep(2) - return "RPC Username or password incorrect or HTTP header lacks authentication at all." - # handle request + def _decode(self, text, decode_type): + try: + return text.decode(decode_type) + except TypeError as e: + raise APIError(22, "Decode error - " + str(e)) + + def _handle_request(self, method, params): if method == 'helloWorld': (a, b) = params return a + '-' + b @@ -166,7 +172,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return data elif method == 'createRandomAddress': if len(params) == 0: - return 'API Error 0000: I need parameters!' + raise APIError(0, 'I need parameters!') elif len(params) == 1: label, = params eighteenByteRipe = False @@ -193,12 +199,12 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): payloadLengthExtraBytes = int( shared.networkDefaultPayloadLengthExtraBytes * smallMessageDifficulty) else: - return 'API Error 0000: Too many parameters!' - label = label.decode('base64') + raise APIError(0, 'Too many parameters!') + label = self._decode(label, "base64") try: unicode(label, 'utf-8') except: - return 'API Error 0017: Label is not valid UTF-8 data.' + raise APIError(17, 'Label is not valid UTF-8 data.') shared.apiAddressGeneratorReturnQueue.queue.clear() streamNumberForAddress = 1 shared.addressGeneratorQueue.put(( @@ -206,7 +212,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return shared.apiAddressGeneratorReturnQueue.get() elif method == 'createDeterministicAddresses': if len(params) == 0: - return 'API Error 0000: I need parameters!' + raise APIError(0, 'I need parameters!') elif len(params) == 1: passphrase, = params numberOfAddresses = 1 @@ -260,24 +266,24 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): payloadLengthExtraBytes = int( shared.networkDefaultPayloadLengthExtraBytes * smallMessageDifficulty) else: - return 'API Error 0000: Too many parameters!' + raise APIError(0, 'Too many parameters!') if len(passphrase) == 0: - return 'API Error 0001: The specified passphrase is blank.' - passphrase = passphrase.decode('base64') + raise APIError(1, 'The specified passphrase is blank.') + passphrase = self._decode(passphrase, "base64") if addressVersionNumber == 0: # 0 means "just use the proper addressVersionNumber" addressVersionNumber = 3 if addressVersionNumber != 3: - return 'API Error 0002: The address version number currently must be 3 (or 0 which means auto-select). ' + addressVersionNumber + ' isn\'t supported.' + raise APIError(2,'The address version number currently must be 3 (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: - return 'API Error 0003: The stream number must be 1 (or 0 which means auto-select). Others aren\'t supported.' + raise APIError(3,'The stream number must be 1 (or 0 which means auto-select). Others aren\'t supported.') if numberOfAddresses == 0: - return 'API Error 0004: Why would you ask me to generate 0 addresses for you?' + raise APIError(4, 'Why would you ask me to generate 0 addresses for you?') if numberOfAddresses > 999: - return 'API Error 0005: 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.' + 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() - print 'Requesting that the addressGenerator create', numberOfAddresses, 'addresses.' + logger.debug('Requesting that the addressGenerator create %s addresses.', numberOfAddresses) shared.addressGeneratorQueue.put( ('createDeterministicAddresses', addressVersionNumber, streamNumber, 'unused API address', numberOfAddresses, passphrase, eighteenByteRipe, nonceTrialsPerByte, payloadLengthExtraBytes)) @@ -291,19 +297,19 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return data elif method == 'getDeterministicAddress': if len(params) != 3: - return 'API Error 0000: I need exactly 3 parameters.' + raise APIError(0, 'I need exactly 3 parameters.') passphrase, addressVersionNumber, streamNumber = params numberOfAddresses = 1 eighteenByteRipe = False if len(passphrase) == 0: - return 'API Error 0001: The specified passphrase is blank.' - passphrase = passphrase.decode('base64') + raise APIError(1, 'The specified passphrase is blank.') + passphrase = self._decode(passphrase, "base64") if addressVersionNumber != 3: - return 'API Error 0002: The address version number currently must be 3. ' + addressVersionNumber + ' isn\'t supported.' + raise APIError(2, 'The address version number currently must be 3. ' + addressVersionNumber + ' isn\'t supported.') if streamNumber != 1: - return 'API Error 0003: The stream number must be 1. Others aren\'t supported.' + raise APIError(3, ' The stream number must be 1. Others aren\'t supported.') shared.apiAddressGeneratorReturnQueue.queue.clear() - print 'Requesting that the addressGenerator create', numberOfAddresses, 'addresses.' + logger.debug('Requesting that the addressGenerator create %s addresses.', numberOfAddresses) shared.addressGeneratorQueue.put( ('getDeterministicAddress', addressVersionNumber, streamNumber, 'unused API address', numberOfAddresses, passphrase, eighteenByteRipe)) @@ -343,8 +349,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return data elif method == 'getInboxMessageById' or method == 'getInboxMessageByID': if len(params) == 0: - return 'API Error 0000: I need parameters!' - msgid = params[0].decode('hex') + raise APIError(0, 'I need parameters!') + msgid = self._decode(params[0], "hex") v = (msgid,) shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox WHERE msgid=?''') @@ -391,7 +397,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return data elif method == 'getInboxMessagesByReceiver' or method == 'getInboxMessagesByAddress': #after some time getInboxMessagesByAddress should be removed if len(params) == 0: - return 'API Error 0000: I need parameters!' + raise APIError(0, 'I need parameters!') toAddress = params[0] v = (toAddress,) shared.sqlLock.acquire() @@ -411,8 +417,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return data elif method == 'getSentMessageById' or method == 'getSentMessageByID': if len(params) == 0: - return 'API Error 0000: I need parameters!' - msgid = params[0].decode('hex') + raise APIError(0, 'I need parameters!') + msgid = self._decode(params[0], "hex") v = (msgid,) shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent WHERE msgid=?''') @@ -429,7 +435,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return data elif method == 'getSentMessagesByAddress' or method == 'getSentMessagesBySender': if len(params) == 0: - return 'API Error 0000: I need parameters!' + raise APIError(0, 'I need parameters!') fromAddress = params[0] v = (fromAddress,) shared.sqlLock.acquire() @@ -449,8 +455,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return data elif method == 'getSentMessageByAckData': if len(params) == 0: - return 'API Error 0000: I need parameters!' - ackData = params[0].decode('hex') + raise APIError(0, 'I need parameters!') + ackData = self._decode(params[0], "hex") v = (ackData,) shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent WHERE ackdata=?''') @@ -467,8 +473,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return data elif method == 'trashMessage': if len(params) == 0: - return 'API Error 0000: I need parameters!' - msgid = params[0].decode('hex') + raise APIError(0, 'I need parameters!') + msgid = self._decode(params[0], "hex") # Trash if in inbox table helper_inbox.trash(msgid) @@ -484,14 +490,14 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'Trashed message (assuming message existed).' elif method == 'trashInboxMessage': if len(params) == 0: - return 'API Error 0000: I need parameters!' - msgid = params[0].decode('hex') + raise APIError(0, 'I need parameters!') + msgid = self._decode(params[0], "hex") helper_inbox.trash(msgid) return 'Trashed inbox message (assuming message existed).' elif method == 'trashSentMessage': if len(params) == 0: - return 'API Error 0000: I need parameters!' - msgid = params[0].decode('hex') + raise APIError(0, 'I need parameters!') + msgid = self._decode(params[0], "hex") t = (msgid,) shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''UPDATE sent SET folder='trash' WHERE msgid=?''') @@ -503,59 +509,57 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'Trashed sent message (assuming message existed).' elif method == 'sendMessage': if len(params) == 0: - return 'API Error 0000: I need parameters!' + raise APIError(0, 'I need parameters!') elif len(params) == 4: toAddress, fromAddress, subject, message = params encodingType = 2 elif len(params) == 5: toAddress, fromAddress, subject, message, encodingType = params if encodingType != 2: - return 'API Error 0006: The encoding type must be 2 because that is the only one this program currently supports.' - subject = subject.decode('base64') - message = message.decode('base64') + 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': - with shared.printLock: - print 'API Error 0007: Could not decode address:', toAddress, ':', status + logger.warn('API Error 0007: Could not decode address %s. Status: %s.', toAddress, status) if status == 'checksumfailed': - return 'API Error 0008: Checksum failed for address: ' + toAddress + raise APIError(8, 'Checksum failed for address: ' + toAddress) if status == 'invalidcharacters': - return 'API Error 0009: Invalid characters in address: ' + toAddress + raise APIError(9, 'Invalid characters in address: ' + toAddress) if status == 'versiontoohigh': - return 'API Error 0010: Address version number too high (or zero) in address: ' + toAddress - return 'API Error 0007: Could not decode address: ' + toAddress + ' : ' + status + 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: - return 'API Error 0011: The address version number currently must be 2 or 3. Others aren\'t supported. Check the toAddress.' + raise APIError(11, 'The address version number currently must be 2 or 3. Others aren\'t supported. Check the toAddress.') if streamNumber != 1: - return 'API Error 0012: The stream number must be 1. Others aren\'t supported. Check the toAddress.' + 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': - with shared.printLock: - print 'API Error 0007: Could not decode address:', fromAddress, ':', status + logger.warn('API Error 0007: Could not decode address %s. Status: %s.', fromAddress, status) if status == 'checksumfailed': - return 'API Error 0008: Checksum failed for address: ' + fromAddress + raise APIError(8, 'Checksum failed for address: ' + fromAddress) if status == 'invalidcharacters': - return 'API Error 0009: Invalid characters in address: ' + fromAddress + raise APIError(9, 'Invalid characters in address: ' + fromAddress) if status == 'versiontoohigh': - return 'API Error 0010: Address version number too high (or zero) in address: ' + fromAddress - return 'API Error 0007: Could not decode address: ' + fromAddress + ' : ' + status + 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: - return 'API Error 0011: The address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.' + raise APIError(11, 'The address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.') if streamNumber != 1: - return 'API Error 0012: The stream number must be 1. Others aren\'t supported. Check the fromAddress.' + raise APIError(12, 'The stream number must be 1. Others aren\'t supported. Check the fromAddress.') toAddress = addBMIfNotPresent(toAddress) fromAddress = addBMIfNotPresent(fromAddress) try: fromAddressEnabled = shared.config.getboolean( fromAddress, 'enabled') except: - return 'API Error 0013: Could not find your fromAddress in the keys.dat file.' + raise APIError(13, 'Could not find your fromAddress in the keys.dat file.') if not fromAddressEnabled: - return 'API Error 0014: Your fromAddress is disabled. Cannot send.' + raise APIError(14, 'Your fromAddress is disabled. Cannot send.') ackdata = OpenSSL.rand(32) @@ -584,40 +588,39 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): elif method == 'sendBroadcast': if len(params) == 0: - return 'API Error 0000: I need parameters!' + raise APIError(0, 'I need parameters!') if len(params) == 3: fromAddress, subject, message = params encodingType = 2 elif len(params) == 4: fromAddress, subject, message, encodingType = params if encodingType != 2: - return 'API Error 0006: The encoding type must be 2 because that is the only one this program currently supports.' - subject = subject.decode('base64') - message = message.decode('base64') + 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, fromRipe = decodeAddress( fromAddress) if status != 'success': - with shared.printLock: - print 'API Error 0007: Could not decode address:', fromAddress, ':', status + logger.warn('API Error 0007: Could not decode address %s. Status: %s.', fromAddress, status) if status == 'checksumfailed': - return 'API Error 0008: Checksum failed for address: ' + fromAddress + raise APIError(8, 'Checksum failed for address: ' + fromAddress) if status == 'invalidcharacters': - return 'API Error 0009: Invalid characters in address: ' + fromAddress + raise APIError(9, 'Invalid characters in address: ' + fromAddress) if status == 'versiontoohigh': - return 'API Error 0010: Address version number too high (or zero) in address: ' + fromAddress - return 'API Error 0007: Could not decode address: ' + fromAddress + ' : ' + status + 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: - return 'API Error 0011: the address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.' + raise APIError(11, 'the address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.') if streamNumber != 1: - return 'API Error 0012: the stream number must be 1. Others aren\'t supported. Check the fromAddress.' + raise APIError(12, 'the stream number must be 1. Others aren\'t supported. Check the fromAddress.') fromAddress = addBMIfNotPresent(fromAddress) try: fromAddressEnabled = shared.config.getboolean( fromAddress, 'enabled') except: - return 'API Error 0013: could not find your fromAddress in the keys.dat file.' + raise APIError(13, 'could not find your fromAddress in the keys.dat file.') ackdata = OpenSSL.rand(32) toAddress = '[Broadcast subscribers]' ripe = '' @@ -635,14 +638,15 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return ackdata.encode('hex') elif method == 'getStatus': if len(params) != 1: - return 'API Error 0000: I need one parameter!' + raise APIError(0, 'I need one parameter!') ackdata, = params if len(ackdata) != 64: - return 'API Error 0015: The length of ackData should be 32 bytes (encoded in hex thus 64 characters).' + raise APIError(15, 'The length of ackData should be 32 bytes (encoded in hex thus 64 characters).') + ackdata = self._decode(ackdata, "hex") shared.sqlLock.acquire() shared.sqlSubmitQueue.put( '''SELECT status FROM sent where ackdata=?''') - shared.sqlSubmitQueue.put((ackdata.decode('hex'),)) + shared.sqlSubmitQueue.put((ackdata,)) queryreturn = shared.sqlReturnQueue.get() shared.sqlLock.release() if queryreturn == []: @@ -652,37 +656,36 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return status elif method == 'addSubscription': if len(params) == 0: - return 'API Error 0000: I need parameters!' + raise APIError(0, 'I need parameters!') if len(params) == 1: address, = params label == '' if len(params) == 2: address, label = params - label = label.decode('base64') + label = self._decode(label, "base64") try: unicode(label, 'utf-8') except: - return 'API Error 0017: Label is not valid UTF-8 data.' + raise APIError(17, 'Label is not valid UTF-8 data.') if len(params) > 2: - return 'API Error 0000: I need either 1 or 2 parameters!' + raise APIError(0, 'I need either 1 or 2 parameters!') address = addBMIfNotPresent(address) status, addressVersionNumber, streamNumber, toRipe = decodeAddress( address) if status != 'success': - with shared.printLock: - print 'API Error 0007: Could not decode address:', address, ':', status + logger.warn('API Error 0007: Could not decode address %s. Status: %s.', address, status) if status == 'checksumfailed': - return 'API Error 0008: Checksum failed for address: ' + address + raise APIError(8, 'Checksum failed for address: ' + address) if status == 'invalidcharacters': - return 'API Error 0009: Invalid characters in address: ' + address + raise APIError(9, 'Invalid characters in address: ' + address) if status == 'versiontoohigh': - return 'API Error 0010: Address version number too high (or zero) in address: ' + address - return 'API Error 0007: Could not decode address: ' + address + ' : ' + status + 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: - return 'API Error 0011: The address version number currently must be 2 or 3. Others aren\'t supported.' + raise APIError(11, 'The address version number currently must be 2 or 3. Others aren\'t supported.') if streamNumber != 1: - return 'API Error 0012: The stream number must be 1. Others aren\'t supported.' + raise APIError(12, 'The stream number must be 1. Others aren\'t supported.') # First we must check to see if the address is already in the # subscriptions list. shared.sqlLock.acquire() @@ -693,7 +696,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): queryreturn = shared.sqlReturnQueue.get() shared.sqlLock.release() if queryreturn != []: - return 'API Error 0016: You are already subscribed to that address.' + raise APIError(16, 'You are already subscribed to that address.') t = (label, address, True) shared.sqlLock.acquire() shared.sqlSubmitQueue.put( @@ -709,7 +712,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): elif method == 'deleteSubscription': if len(params) != 1: - return 'API Error 0000: I need 1 parameter!' + raise APIError(0, 'I need 1 parameter!') address, = params address = addBMIfNotPresent(address) t = (address,) @@ -746,9 +749,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): # object and sends it out to the rest of the Bitmessage network as if it had generated the # message itself. Please do not yet add this to the api doc. if len(params) != 1: - return 'API Error 0000: I need 1 parameter!' + raise APIError(0, 'I need 1 parameter!') encryptedPayload, = params - encryptedPayload = encryptedPayload.decode('hex') + encryptedPayload = self._decode(encryptedPayload, "hex") toStreamNumber = decodeVarint(encryptedPayload[16:26])[0] inventoryHash = calculateInventoryHash(encryptedPayload) objectType = 'msg' @@ -765,9 +768,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): # rest of the Bitmessage network as if it had generated the pubkey object itself. Please # do not yet add this to the api doc. if len(params) != 1: - return 'API Error 0000: I need 1 parameter!' + raise APIError(0, 'I need 1 parameter!') payload, = params - payload = payload.decode('hex') + payload = self._decode(payload, "hex") pubkeyReadPosition = 8 # bypass the nonce if payload[pubkeyReadPosition:pubkeyReadPosition+4] == '\x00\x00\x00\x00': # if this pubkey uses 8 byte time pubkeyReadPosition += 8 @@ -790,11 +793,11 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): # doc. if len(params) != 1: - return 'API Error 0000: I need 1 parameter!' + raise APIError(0, 'I need 1 parameter!') requestedHash, = params if len(requestedHash) != 40: - return 'API Error 0019: The length of hash should be 20 bytes (encoded in hex thus 40 characters).' - requestedHash = requestedHash.decode('hex') + raise APIError(19, 'The length of hash should be 20 bytes (encoded in hex thus 40 characters).') + requestedHash = self._decode(requestedHash, "hex") # This is not a particularly commonly used API function. Before we # use it we'll need to fill out a field in our inventory database @@ -832,11 +835,11 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): # Method will eventually be used by a particular Android app to # retrieve pubkeys. Please do not yet add this to the api docs. if len(params) != 1: - return 'API Error 0000: I need 1 parameter!' + raise APIError(0, 'I need 1 parameter!') requestedHash, = params if len(requestedHash) != 40: - return 'API Error 0019: The length of hash should be 20 bytes (encoded in hex thus 40 characters).' - requestedHash = requestedHash.decode('hex') + raise APIError(19, 'The length of hash should be 20 bytes (encoded in hex thus 40 characters).') + requestedHash = self._decode(requestedHash, "hex") parameters = (requestedHash,) with shared.sqlLock: shared.sqlSubmitQueue.put('''SELECT transmitdata FROM pubkeys WHERE hash = ? ; ''') @@ -849,9 +852,31 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): data += ']}' return data elif method == 'clientStatus': - return '{ "networkConnections" : "%s" }' % str(len(shared.connectedHostsList)) + if len(shared.connectedHostsList) == 0: + networkStatus = 'notConnected' + elif len(shared.connectedHostsList) > 0 and not shared.clientHasReceivedIncomingConnections: + networkStatus = 'connectedButHaveNotReceivedIncomingConnections' + else: + networkStatus = 'connectedAndReceivingIncomingConnections' + return json.dumps({'networkConnections':len(shared.connectedHostsList),'numberOfMessagesProcessed':shared.numberOfMessagesProcessed, 'numberOfBroadcastsProcessed':shared.numberOfBroadcastsProcessed, 'numberOfPubkeysProcessed':shared.numberOfPubkeysProcessed, 'networkStatus':networkStatus}, indent=4, separators=(',', ': ')) else: - return 'API Error 0020: Invalid method: %s' % method + raise APIError(20, 'Invalid method: %s' % method) + + def _dispatch(self, method, params): + self.cookies = [] + + validuser = self.APIAuthenticateClient() + if not validuser: + time.sleep(2) + return "RPC Username or password incorrect or HTTP header lacks authentication at all." + + try: + return self._handle_request(method, params) + except APIError as e: + return str(e) + except Exception as e: + logger.exception(e) + return "API Error 0021: Unexpected API Failure - %s" % str(e) # This thread, of which there is only one, runs the API. @@ -878,7 +903,7 @@ if shared.useVeryEasyProofOfWorkForTesting: shared.networkDefaultPayloadLengthExtraBytes / 7000) class Main: - def start(self, deamon=False): + def start(self, daemon=False): # is the application already running? If yes then exit. thisapp = singleton.singleinstance() @@ -931,7 +956,7 @@ class Main: singleListenerThread.daemon = True # close the main program even if there are threads left singleListenerThread.start() - if deamon == False and shared.safeConfigGetBoolean('bitmessagesettings', 'daemon') == False: + if daemon == False and shared.safeConfigGetBoolean('bitmessagesettings', 'daemon') == False: try: from PyQt4 import QtCore, QtGui except Exception as err: @@ -944,7 +969,7 @@ class Main: else: shared.config.remove_option('bitmessagesettings', 'dontconnect') - if deamon: + if daemon: with shared.printLock: print 'Running as a daemon. The main program should exit this thread.' else: @@ -962,7 +987,6 @@ class Main: def getApiAddress(self): if not shared.safeConfigGetBoolean('bitmessagesettings', 'apienabled'): return None - address = shared.config.get('bitmessagesettings', 'apiinterface') port = shared.config.getint('bitmessagesettings', 'apiport') return {'address':address,'port':port} diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 3e513295..eab7f0e2 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -417,11 +417,11 @@ class MyForm(QtGui.QMainWindow): QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( "updateNetworkStatusTab()"), self.updateNetworkStatusTab) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( - "incrementNumberOfMessagesProcessed()"), self.incrementNumberOfMessagesProcessed) + "updateNumberOfMessagesProcessed()"), self.updateNumberOfMessagesProcessed) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( - "incrementNumberOfPubkeysProcessed()"), self.incrementNumberOfPubkeysProcessed) + "updateNumberOfPubkeysProcessed()"), self.updateNumberOfPubkeysProcessed) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( - "incrementNumberOfBroadcastsProcessed()"), self.incrementNumberOfBroadcastsProcessed) + "updateNumberOfBroadcastsProcessed()"), self.updateNumberOfBroadcastsProcessed) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( "setStatusIcon(PyQt_PyObject)"), self.setStatusIcon) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( @@ -1072,12 +1072,22 @@ class MyForm(QtGui.QMainWindow): if 'linux' in sys.platform: # Note: QSound was a nice idea but it didn't work if '.mp3' in soundFilename: + gst_available=False try: subprocess.call(["gst123", soundFilename], stdin=subprocess.PIPE, stdout=subprocess.PIPE) + gst_available=True except: print "WARNING: gst123 must be installed in order to play mp3 sounds" + if not gst_available: + try: + subprocess.call(["mpg123", soundFilename], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE) + gst_available=True + except: + print "WARNING: mpg123 must be installed in order to play mp3 sounds" else: try: subprocess.call(["aplay", soundFilename], @@ -1260,20 +1270,17 @@ class MyForm(QtGui.QMainWindow): self.actionShow.setChecked(not self.actionShow.isChecked()) self.appIndicatorShowOrHideWindow() - def incrementNumberOfMessagesProcessed(self): - self.numberOfMessagesProcessed += 1 + def updateNumberOfMessagesProcessed(self): self.ui.labelMessageCount.setText(_translate( - "MainWindow", "Processed %1 person-to-person messages.").arg(str(self.numberOfMessagesProcessed))) + "MainWindow", "Processed %1 person-to-person messages.").arg(str(shared.numberOfMessagesProcessed))) - def incrementNumberOfBroadcastsProcessed(self): - self.numberOfBroadcastsProcessed += 1 + def updateNumberOfBroadcastsProcessed(self): self.ui.labelBroadcastCount.setText(_translate( - "MainWindow", "Processed %1 broadcast messages.").arg(str(self.numberOfBroadcastsProcessed))) + "MainWindow", "Processed %1 broadcast messages.").arg(str(shared.numberOfBroadcastsProcessed))) - def incrementNumberOfPubkeysProcessed(self): - self.numberOfPubkeysProcessed += 1 + def updateNumberOfPubkeysProcessed(self): self.ui.labelPubkeyCount.setText(_translate( - "MainWindow", "Processed %1 public keys.").arg(str(self.numberOfPubkeysProcessed))) + "MainWindow", "Processed %1 public keys.").arg(str(shared.numberOfPubkeysProcessed))) def updateNetworkStatusTab(self): # print 'updating network status tab' @@ -3341,80 +3348,57 @@ class myTableWidgetItem(QTableWidgetItem): def __lt__(self, other): return int(self.data(33).toPyObject()) < int(other.data(33).toPyObject()) -from threading import Thread -class UISignaler(Thread,QThread): +class UISignaler(QThread): def __init__(self, parent=None): - Thread.__init__(self, parent) QThread.__init__(self, parent) def run(self): while True: - try: - command, data = shared.UISignalQueue.get() - if command == 'writeNewAddressToTable': - label, address, streamNumber = data - self.emit(SIGNAL( - "writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), label, address, str(streamNumber)) - elif command == 'updateStatusBar': - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"), data) - elif command == 'updateSentItemStatusByHash': - hash, message = data - self.emit(SIGNAL( - "updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), hash, message) - elif command == 'updateSentItemStatusByAckdata': - ackData, message = data - self.emit(SIGNAL( - "updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"), ackData, message) - elif command == 'displayNewInboxMessage': - inventoryHash, toAddress, fromAddress, subject, body = data - self.emit(SIGNAL( - "displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), - inventoryHash, toAddress, fromAddress, subject, body) - elif command == 'displayNewSentMessage': - toAddress, fromLabel, fromAddress, subject, message, ackdata = data - self.emit(SIGNAL( - "displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), - toAddress, fromLabel, fromAddress, subject, message, ackdata) - elif command == 'updateNetworkStatusTab': - self.emit(SIGNAL("updateNetworkStatusTab()")) - elif command == 'incrementNumberOfMessagesProcessed': - self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) - elif command == 'incrementNumberOfPubkeysProcessed': - self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()")) - elif command == 'incrementNumberOfBroadcastsProcessed': - self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()")) - elif command == 'setStatusIcon': - self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"), data) - elif command == 'rerenderInboxFromLabels': - self.emit(SIGNAL("rerenderInboxFromLabels()")) - elif command == 'rerenderSubscriptions': - self.emit(SIGNAL("rerenderSubscriptions()")) - elif command == 'removeInboxRowByMsgid': - self.emit(SIGNAL("removeInboxRowByMsgid(PyQt_PyObject)"), data) - else: - sys.stderr.write( - 'Command sent to UISignaler not recognized: %s\n' % command) - except Exception,ex: - # uncaught exception will block gevent - import traceback - traceback.print_exc() - traceback.print_stack() - print ex - pass - -try: - import gevent -except ImportError as ex: - gevent = None -else: - def mainloop(app): - while True: - app.processEvents() - gevent.sleep(0.01) - def testprint(): - #print 'this is running' - gevent.spawn_later(1, testprint) + command, data = shared.UISignalQueue.get() + if command == 'writeNewAddressToTable': + label, address, streamNumber = data + self.emit(SIGNAL( + "writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), label, address, str(streamNumber)) + elif command == 'updateStatusBar': + self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"), data) + elif command == 'updateSentItemStatusByHash': + hash, message = data + self.emit(SIGNAL( + "updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), hash, message) + elif command == 'updateSentItemStatusByAckdata': + ackData, message = data + self.emit(SIGNAL( + "updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"), ackData, message) + elif command == 'displayNewInboxMessage': + inventoryHash, toAddress, fromAddress, subject, body = data + self.emit(SIGNAL( + "displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), + inventoryHash, toAddress, fromAddress, subject, body) + elif command == 'displayNewSentMessage': + toAddress, fromLabel, fromAddress, subject, message, ackdata = data + self.emit(SIGNAL( + "displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), + toAddress, fromLabel, fromAddress, subject, message, ackdata) + elif command == 'updateNetworkStatusTab': + self.emit(SIGNAL("updateNetworkStatusTab()")) + elif command == 'updateNumberOfMessagesProcessed': + self.emit(SIGNAL("updateNumberOfMessagesProcessed()")) + elif command == 'updateNumberOfPubkeysProcessed': + self.emit(SIGNAL("updateNumberOfPubkeysProcessed()")) + elif command == 'updateNumberOfBroadcastsProcessed': + self.emit(SIGNAL("updateNumberOfBroadcastsProcessed()")) + elif command == 'setStatusIcon': + self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"), data) + elif command == 'rerenderInboxFromLabels': + self.emit(SIGNAL("rerenderInboxFromLabels()")) + elif command == 'rerenderSubscriptions': + self.emit(SIGNAL("rerenderSubscriptions()")) + elif command == 'removeInboxRowByMsgid': + self.emit(SIGNAL("removeInboxRowByMsgid(PyQt_PyObject)"), data) + else: + sys.stderr.write( + 'Command sent to UISignaler not recognized: %s\n' % command) def run(): app = QtGui.QApplication(sys.argv) @@ -3472,8 +3456,4 @@ def run(): myapp.notifierInit() if shared.safeConfigGetBoolean('bitmessagesettings', 'dontconnect'): myapp.showConnectDialog() # ask the user if we may connect - if gevent is None: - sys.exit(app.exec_()) - else: - gevent.joinall([gevent.spawn(testprint), gevent.spawn(mainloop, app), gevent.spawn(mainloop, app), gevent.spawn(mainloop, app), gevent.spawn(mainloop, app), gevent.spawn(mainloop, app)]) - print 'done' + sys.exit(app.exec_()) diff --git a/src/class_bgWorker.py b/src/class_bgWorker.py deleted file mode 100644 index c01e26ef..00000000 --- a/src/class_bgWorker.py +++ /dev/null @@ -1,38 +0,0 @@ -#! /usr/bin/python -# -*- coding: utf-8 -*- -# cody by linker.lin@me.com - -__author__ = 'linkerlin' - - -import threading -import Queue -import time - -class bgWorker(threading.Thread): - def __init__(self): - threading.Thread.__init__(self) - self.q = Queue.Queue() - self.setDaemon(True) - - def post(self,job): - self.q.put(job) - - def run(self): - while 1: - job=None - try: - job = self.q.get(block=True) - if job: - job() - except Exception as ex: - print "Error,job exception:",ex.message,type(ex) - time.sleep(0.05) - else: - #print "job: ", job, " done" - pass - finally: - time.sleep(0.05) - -bgworker = bgWorker() -bgworker.start() diff --git a/src/class_receiveDataThread.py b/src/class_receiveDataThread.py index 6adfe490..b5aa5893 100644 --- a/src/class_receiveDataThread.py +++ b/src/class_receiveDataThread.py @@ -256,6 +256,7 @@ class receiveDataThread(threading.Thread): def connectionFullyEstablished(self): self.connectionIsOrWasFullyEstablished = True if not self.initiatedConnection: + shared.clientHasReceivedIncomingConnections = True shared.UISignalQueue.put(('setStatusIcon', 'green')) self.sock.settimeout( 600) # We'll send out a pong every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately. @@ -392,8 +393,9 @@ class receiveDataThread(threading.Thread): objectType, self.streamNumber, data, embeddedTime) shared.inventoryLock.release() self.broadcastinv(self.inventoryHash) + shared.numberOfBroadcastsProcessed += 1 shared.UISignalQueue.put(( - 'incrementNumberOfBroadcastsProcessed', 'no data')) + 'updateNumberOfBroadcastsProcessed', 'no data')) self.processbroadcast( readPosition, data) # When this function returns, we will have either successfully processed this broadcast because we are interested in it, ignored it because we aren't interested in it, or found problem with the broadcast that warranted ignoring it. @@ -760,8 +762,9 @@ class receiveDataThread(threading.Thread): objectType, self.streamNumber, data, embeddedTime) shared.inventoryLock.release() self.broadcastinv(self.inventoryHash) + shared.numberOfMessagesProcessed += 1 shared.UISignalQueue.put(( - 'incrementNumberOfMessagesProcessed', 'no data')) + 'updateNumberOfMessagesProcessed', 'no data')) self.processmsg( readPosition, data) # When this function returns, we will have either successfully processed the message bound for us, ignored it because it isn't bound for us, or found problem with the message that warranted ignoring it. @@ -1178,8 +1181,9 @@ class receiveDataThread(threading.Thread): objectType, self.streamNumber, data, embeddedTime) shared.inventoryLock.release() self.broadcastinv(inventoryHash) + shared.numberOfPubkeysProcessed += 1 shared.UISignalQueue.put(( - 'incrementNumberOfPubkeysProcessed', 'no data')) + 'updateNumberOfPubkeysProcessed', 'no data')) self.processpubkey(data) @@ -1216,7 +1220,7 @@ class receiveDataThread(threading.Thread): if addressVersion == 0: print '(Within processpubkey) addressVersion of 0 doesn\'t make sense.' return - if addressVersion >= 4 or addressVersion == 1: + if addressVersion > 3 or addressVersion == 1: with shared.printLock: print 'This version of Bitmessage cannot handle version', addressVersion, 'addresses.' diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index b29b7f8d..a3d0a0c5 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -535,6 +535,8 @@ class singleWorker(threading.Thread): pubEncryptionKeyBase256 = pubkeyPayload[ readPosition:readPosition + 64] readPosition += 64 + + # Let us fetch the amount of work required by the recipient. if toAddressVersionNumber == 2: requiredAverageProofOfWorkNonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte requiredPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes @@ -569,6 +571,7 @@ class singleWorker(threading.Thread): requiredPayloadLengthExtraBytes) / shared.networkDefaultPayloadLengthExtraBytes)).arg(unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'), localtime(int(time.time()))), 'utf-8'))))) continue + embeddedTime = pack('>Q', (int(time.time()) + random.randrange( -300, 300))) # the current time plus or minus five minutes. We will use this time both for our message and for the ackdata packed within our message. if fromAddressVersionNumber == 2: diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index 6ac0f168..5a911246 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -60,11 +60,10 @@ class sqlThread(threading.Thread): self.cur.execute( '''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( int(time.time()),)) self.conn.commit() - print 'Created messages database file' + logger.info('Created messages database file') except Exception as err: if str(err) == 'table inbox already exists': - with shared.printLock: - print 'Database file already exists.' + logger.debug('Database file already exists.') else: sys.stderr.write( @@ -146,13 +145,13 @@ class sqlThread(threading.Thread): self.cur.execute(item, parameters) if self.cur.fetchall() == []: # The settings table doesn't exist. We need to make it. - print 'In messages.dat database, creating new \'settings\' table.' + logger.debug('In messages.dat database, creating new \'settings\' table.') self.cur.execute( '''CREATE TABLE settings (key text, value blob, UNIQUE(key) ON CONFLICT REPLACE)''' ) self.cur.execute( '''INSERT INTO settings VALUES('version','1')''') self.cur.execute( '''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( int(time.time()),)) - print 'In messages.dat database, removing an obsolete field from the pubkeys table.' + logger.debug('In messages.dat database, removing an obsolete field from the pubkeys table.') self.cur.execute( '''CREATE TEMPORARY TABLE pubkeys_backup(hash blob, transmitdata blob, time int, usedpersonally text, UNIQUE(hash) ON CONFLICT REPLACE);''') self.cur.execute( @@ -163,17 +162,17 @@ class sqlThread(threading.Thread): self.cur.execute( '''INSERT INTO pubkeys SELECT hash, transmitdata, time, usedpersonally FROM pubkeys_backup;''') self.cur.execute( '''DROP TABLE pubkeys_backup;''') - print 'Deleting all pubkeys from inventory. They will be redownloaded and then saved with the correct times.' + logger.debug('Deleting all pubkeys from inventory. They will be redownloaded and then saved with the correct times.') self.cur.execute( '''delete from inventory where objecttype = 'pubkey';''') - print 'replacing Bitmessage announcements mailing list with a new one.' + logger.debug('replacing Bitmessage announcements mailing list with a new one.') self.cur.execute( '''delete from subscriptions where address='BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx' ''') self.cur.execute( '''INSERT INTO subscriptions VALUES('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') - print 'Commiting.' + logger.debug('Commiting.') self.conn.commit() - print 'Vacuuming message.dat. You might notice that the file size gets much smaller.' + logger.debug('Vacuuming message.dat. You might notice that the file size gets much smaller.') self.cur.execute( ''' VACUUM ''') # After code refactoring, the possible status values for sent messages @@ -219,11 +218,11 @@ class sqlThread(threading.Thread): self.cur.execute('''DELETE FROM pubkeys WHERE hash='1234' ''') self.conn.commit() if transmitdata == '': - sys.stderr.write('Problem: The version of SQLite you have cannot store Null values. Please download and install the latest revision of your version of Python (for example, the latest Python 2.7 revision) and try again.\n') - sys.stderr.write('PyBitmessage will now exit very abruptly. You may now see threading errors related to this abrupt exit but the problem you need to solve is related to SQLite.\n\n') + logger.fatal('Problem: The version of SQLite you have cannot store Null values. Please download and install the latest revision of your version of Python (for example, the latest Python 2.7 revision) and try again.\n') + logger.fatal('PyBitmessage will now exit very abruptly. You may now see threading errors related to this abrupt exit but the problem you need to solve is related to SQLite.\n\n') os._exit(0) except Exception as err: - print err + logger.error(err) # Let us check to see the last time we vaccumed the messages.dat file. # If it has been more than a month let's do it now. @@ -234,7 +233,7 @@ class sqlThread(threading.Thread): for row in queryreturn: value, = row if int(value) < int(time.time()) - 2592000: - print 'It has been a long time since the messages.dat file has been vacuumed. Vacuuming now...' + logger.info('It has been a long time since the messages.dat file has been vacuumed. Vacuuming now...') self.cur.execute( ''' VACUUM ''') item = '''update settings set value=? WHERE key='lastvacuumtime';''' parameters = (int(time.time()),) @@ -246,13 +245,11 @@ class sqlThread(threading.Thread): self.conn.commit() elif item == 'exit': self.conn.close() - with shared.printLock: - print 'sqlThread exiting gracefully.' + logger.info('sqlThread exiting gracefully.') return elif item == 'movemessagstoprog': - with shared.printLock: - print 'the sqlThread is moving the messages.dat file to the local program directory.' + logger.debug('the sqlThread is moving the messages.dat file to the local program directory.') self.conn.commit() self.conn.close() @@ -262,8 +259,7 @@ class sqlThread(threading.Thread): self.conn.text_factory = str self.cur = self.conn.cursor() elif item == 'movemessagstoappdata': - with shared.printLock: - print 'the sqlThread is moving the messages.dat file to the Appdata folder.' + logger.debug('the sqlThread is moving the messages.dat file to the Appdata folder.') self.conn.commit() self.conn.close() @@ -284,10 +280,8 @@ class sqlThread(threading.Thread): try: self.cur.execute(item, parameters) except Exception as err: - with shared.printLock: - sys.stderr.write('\nMajor error occurred when trying to execute a SQL statement within the sqlThread. Please tell Atheros about this error message or post it in the forum! Error occurred while trying to execute statement: "' + str( - item) + '" Here are the parameters; you might want to censor this data with asterisks (***) as it can contain private information: ' + str(repr(parameters)) + '\nHere is the actual error message thrown by the sqlThread: ' + str(err) + '\n') - sys.stderr.write('This program shall now abruptly exit!\n') + logger.fatal('Major error occurred when trying to execute a SQL statement within the sqlThread. Please tell Atheros about this error message or post it in the forum! Error occurred while trying to execute statement: "%s" Here are the parameters; you might want to censor this data with asterisks (***) as it can contain private information: %s. Here is the actual error message thrown by the sqlThread: %s', str(item), str(repr(parameters)), str(err)) + logger.fatal('This program shall now abruptly exit!') os._exit(0) diff --git a/src/helper_startup.py b/src/helper_startup.py index 24dc0156..12ea1b84 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -9,80 +9,93 @@ from namecoin import ensureNamecoinOptions storeConfigFilesInSameDirectoryAsProgramByDefault = False # The user may de-select Portable Mode in the settings if they want the config files to stay in the application data folder. def loadConfig(): - # First try to load the config file (the keys.dat file) from the program - # directory - shared.config.read('keys.dat') - try: - shared.config.get('bitmessagesettings', 'settingsversion') - print 'Loading config files from same directory as program' - shared.appdata = '' - except: - # Could not load the keys.dat file in the program directory. Perhaps it - # is in the appdata directory. - shared.appdata = shared.lookupAppdataFolder() - shared.config = ConfigParser.SafeConfigParser() + if shared.appdata: shared.config.read(shared.appdata + 'keys.dat') + #shared.appdata must have been specified as a startup option. try: shared.config.get('bitmessagesettings', 'settingsversion') - print 'Loading existing config files from', shared.appdata + print 'Loading config files from directory specified on startup: ' + shared.appdata + needToCreateKeysFile = False except: - # This appears to be the first time running the program; there is - # no config file (or it cannot be accessed). Create config file. - shared.config.add_section('bitmessagesettings') - shared.config.set('bitmessagesettings', 'settingsversion', '6') - shared.config.set('bitmessagesettings', 'port', '8444') - shared.config.set( - 'bitmessagesettings', 'timeformat', '%%a, %%d %%b %%Y %%I:%%M %%p') - shared.config.set('bitmessagesettings', 'blackwhitelist', 'black') - shared.config.set('bitmessagesettings', 'startonlogon', 'false') - if 'linux' in sys.platform: - shared.config.set( - 'bitmessagesettings', 'minimizetotray', 'false') - # This isn't implimented yet and when True on - # Ubuntu causes Bitmessage to disappear while - # running when minimized. - else: - shared.config.set( - 'bitmessagesettings', 'minimizetotray', 'true') - shared.config.set( - 'bitmessagesettings', 'showtraynotifications', 'true') - shared.config.set('bitmessagesettings', 'startintray', 'false') - shared.config.set('bitmessagesettings', 'socksproxytype', 'none') - shared.config.set( - 'bitmessagesettings', 'sockshostname', 'localhost') - shared.config.set('bitmessagesettings', 'socksport', '9050') - shared.config.set( - 'bitmessagesettings', 'socksauthentication', 'false') - shared.config.set( - 'bitmessagesettings', 'sockslisten', 'false') - shared.config.set('bitmessagesettings', 'socksusername', '') - shared.config.set('bitmessagesettings', 'sockspassword', '') - shared.config.set('bitmessagesettings', 'keysencrypted', 'false') - shared.config.set( - 'bitmessagesettings', 'messagesencrypted', 'false') - shared.config.set('bitmessagesettings', 'defaultnoncetrialsperbyte', str( - shared.networkDefaultProofOfWorkNonceTrialsPerByte)) - shared.config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes', str( - shared.networkDefaultPayloadLengthExtraBytes)) - shared.config.set('bitmessagesettings', 'minimizeonclose', 'false') - shared.config.set( - 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0') - shared.config.set( - 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0') - shared.config.set('bitmessagesettings', 'dontconnect', 'true') - shared.config.set('bitmessagesettings', 'userlocale', 'system') - ensureNamecoinOptions() + needToCreateKeysFile = True - if storeConfigFilesInSameDirectoryAsProgramByDefault: - # Just use the same directory as the program and forget about - # the appdata folder - shared.appdata = '' - print 'Creating new config files in same directory as program.' - else: - print 'Creating new config files in', shared.appdata - if not os.path.exists(shared.appdata): - os.makedirs(shared.appdata) - if not sys.platform.startswith('win'): - os.umask(0o077) - with open(shared.appdata + 'keys.dat', 'wb') as configfile: - shared.config.write(configfile) + else: + shared.config.read('keys.dat') + try: + shared.config.get('bitmessagesettings', 'settingsversion') + print 'Loading config files from same directory as program.' + needToCreateKeysFile = False + shared.appdata = '' + except: + # Could not load the keys.dat file in the program directory. Perhaps it + # is in the appdata directory. + shared.appdata = shared.lookupAppdataFolder() + shared.config.read(shared.appdata + 'keys.dat') + try: + shared.config.get('bitmessagesettings', 'settingsversion') + print 'Loading existing config files from', shared.appdata + needToCreateKeysFile = False + except: + needToCreateKeysFile = True + + if needToCreateKeysFile: + # This appears to be the first time running the program; there is + # no config file (or it cannot be accessed). Create config file. + shared.config.add_section('bitmessagesettings') + shared.config.set('bitmessagesettings', 'settingsversion', '6') + shared.config.set('bitmessagesettings', 'port', '8444') + shared.config.set( + 'bitmessagesettings', 'timeformat', '%%a, %%d %%b %%Y %%I:%%M %%p') + shared.config.set('bitmessagesettings', 'blackwhitelist', 'black') + shared.config.set('bitmessagesettings', 'startonlogon', 'false') + if 'linux' in sys.platform: + shared.config.set( + 'bitmessagesettings', 'minimizetotray', 'false') + # This isn't implimented yet and when True on + # Ubuntu causes Bitmessage to disappear while + # running when minimized. + else: + shared.config.set( + 'bitmessagesettings', 'minimizetotray', 'true') + shared.config.set( + 'bitmessagesettings', 'showtraynotifications', 'true') + shared.config.set('bitmessagesettings', 'startintray', 'false') + shared.config.set('bitmessagesettings', 'socksproxytype', 'none') + shared.config.set( + 'bitmessagesettings', 'sockshostname', 'localhost') + shared.config.set('bitmessagesettings', 'socksport', '9050') + shared.config.set( + 'bitmessagesettings', 'socksauthentication', 'false') + shared.config.set( + 'bitmessagesettings', 'sockslisten', 'false') + shared.config.set('bitmessagesettings', 'socksusername', '') + shared.config.set('bitmessagesettings', 'sockspassword', '') + shared.config.set('bitmessagesettings', 'keysencrypted', 'false') + shared.config.set( + 'bitmessagesettings', 'messagesencrypted', 'false') + shared.config.set('bitmessagesettings', 'defaultnoncetrialsperbyte', str( + shared.networkDefaultProofOfWorkNonceTrialsPerByte)) + shared.config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes', str( + shared.networkDefaultPayloadLengthExtraBytes)) + shared.config.set('bitmessagesettings', 'minimizeonclose', 'false') + shared.config.set( + 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0') + shared.config.set( + 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0') + shared.config.set('bitmessagesettings', 'dontconnect', 'true') + shared.config.set('bitmessagesettings', 'userlocale', 'system') + ensureNamecoinOptions() + + if storeConfigFilesInSameDirectoryAsProgramByDefault: + # Just use the same directory as the program and forget about + # the appdata folder + shared.appdata = '' + print 'Creating new config files in same directory as program.' + else: + print 'Creating new config files in', shared.appdata + if not os.path.exists(shared.appdata): + os.makedirs(shared.appdata) + if not sys.platform.startswith('win'): + os.umask(0o077) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.config.write(configfile) diff --git a/src/namecoin.py b/src/namecoin.py index 9fbf4518..f9565ccf 100644 --- a/src/namecoin.py +++ b/src/namecoin.py @@ -270,7 +270,7 @@ def ensureNamecoinOptions (): nmc.close () except Exception as exc: - print "Failure reading namecoin config file: %s" % str (exc) + print "Could not read the Namecoin config file probably because you don't have Namecoin installed. That's ok; we don't really need it. Detailed error message: %s" % str (exc) # If still nothing found, set empty at least. if (not hasUser): diff --git a/src/shared.py b/src/shared.py index d2b82d11..f20c49e2 100644 --- a/src/shared.py +++ b/src/shared.py @@ -20,6 +20,7 @@ import sys import stat import threading import time +from os import path, environ # Project imports. from addresses import * @@ -64,6 +65,10 @@ successfullyDecryptMessageTimings = [ apiAddressGeneratorReturnQueue = Queue.Queue( ) # The address generator thread uses this queue to get information back to the API thread. ackdataForWhichImWatching = {} +clientHasReceivedIncomingConnections = False #used by API command clientStatus +numberOfMessagesProcessed = 0 +numberOfBroadcastsProcessed = 0 +numberOfPubkeysProcessed = 0 #If changed, these values will cause particularly unexpected behavior: You won't be able to either send or receive messages because the proof of work you do (or demand) won't match that done or demanded by others. Don't change them! networkDefaultProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. @@ -121,7 +126,6 @@ def assembleVersionMessage(remoteHost, remotePort, myStreamNumber): def lookupAppdataFolder(): APPNAME = "PyBitmessage" - from os import path, environ if sys.platform == 'darwin': if "HOME" in environ: dataFolder = path.join(os.environ["HOME"], "Library/Application Support/", APPNAME) + '/' @@ -235,7 +239,7 @@ def reloadMyAddressHashes(): if isEnabled: hasEnabledKeys = True status,addressVersionNumber,streamNumber,hash = decodeAddress(addressInKeysFile) - if addressVersionNumber == 2 or addressVersionNumber == 3: + if addressVersionNumber == 2 or addressVersionNumber == 3 or addressVersionNumber == 4: # Returns a simple 32 bytes of information encoded in 64 Hex characters, # or null if there was an error. privEncryptionKey = decodeWalletImportFormat( @@ -246,7 +250,7 @@ def reloadMyAddressHashes(): myAddressesByHash[hash] = addressInKeysFile else: - logger.error('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2 or 3.\n') + logger.error('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2, 3, or 4.\n') if not keyfileSecure: fixSensitiveFilePermissions(appdata + 'keys.dat', hasEnabledKeys)