From 17533237fe9420579e5a87d025ffa093a5de9478 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 2 Aug 2013 18:35:31 -0400 Subject: [PATCH] some initial work done to support particular android client --- src/bitmessagemain.py | 67 ++++++++++++++++++++++++++++++++++++ src/bitmessageqt/__init__.py | 1 + src/class_sqlThread.py | 10 +++++- src/message_data_reader.py | 41 +++++++++++++++------- 4 files changed, 105 insertions(+), 14 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index b156cd0d..b63cc2b3 100644 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -738,6 +738,73 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): data += json.dumps({'label':label.encode('base64'), 'address': address, 'enabled': enabled == 1}, indent=4, separators=(',',': ')) data += ']}' return data + elif method == 'disseminatePreEncryptedMsg': + # The device issuing this command to PyBitmessage supplies a msg object that has + # already been encrypted and had the necessary proof of work done for it to be + # disseminated to the rest of the Bitmessage network. PyBitmessage accepts this msg + # object and sends it out to the rest of the Bitmessage network as if it had generated the + # message itself. + if len(params) != 1: + return 'API Error 0000: I need 1 parameter!' + encryptedPayload, = params + inventoryHash = calculateInventoryHash(encryptedPayload) + objectType = 'msg' + shared.inventory[inventoryHash] = ( + objectType, toStreamNumber, encryptedPayload, int(time.time())) + with shared.printLock: + print 'Broadcasting inv for msg(API disseminatePreEncryptedMsg command):', inventoryHash.encode('hex') + shared.broadcastToSendDataQueues(( + streamNumber, 'sendinv', inventoryHash)) + elif method == 'disseminatePubkey': + # The device issuing this command to PyBitmessage supplies a pubkey object that has + # already had the necessary proof of work done for it to be disseminated to the rest of the + # Bitmessage network. PyBitmessage accepts this pubkey object and sends it out to the + # rest of the Bitmessage network, as if it had generated the pubkey object itself. + if len(params) != 1: + return 'API Error 0000: I need 1 parameter!' + payload, = params + inventoryHash = calculateInventoryHash(payload) + objectType = 'pubkey' + shared.inventory[inventoryHash] = ( + objectType, streamNumber, payload, int(time.time())) + with shared.printLock: + print 'broadcasting inv within API command disseminatePubkey with hash:', inventoryHash.encode('hex') + shared.broadcastToSendDataQueues(( + streamNumber, 'sendinv', inventoryHash)) + elif method == 'getMessageDataByDestinationRIPEHash': + # Method will eventually be used by a particular Android app. + if len(params) != 1: + return 'API Error 0000: I need 1 parameter!' + hash, = params + #if len(hash) != 40: + # return 'API Error 0019: The length of hash should be 20 bytes (encoded in hex thus 40 characters).' + print repr(hash) + hash = hash.decode('hex') + print repr(hash) + with shared.sqlLock: + shared.sqlSubmitQueue.put('''PRAGMA case_sensitive_like = true''') + shared.sqlSubmitQueue.put('') + queryreturn = shared.sqlReturnQueue.get() + + hash = string.replace(hash,'e','ee') + hash = string.replace(hash,'%','e%') + hash = string.replace(hash,'_','e_') + print 'searching for hash:', repr(hash) + parameters = ('%'+ hash + '%',) + with shared.sqlLock: + shared.sqlSubmitQueue.put('''SELECT payload FROM inventory WHERE hash LIKE ? ESCAPE'e'; ''') + shared.sqlSubmitQueue.put(parameters) + queryreturn = shared.sqlReturnQueue.get() + + data = '{"receivedMessageDatas":[' + for row in queryreturn: + payload, = row + if len(data) > 25: + data += ',' + data += json.dumps({'data':payload.encode('hex')}, indent=4, separators=(',', ': ')) + data += ']}' + return data + elif method == 'clientStatus': return '{ "networkConnections" : "%s" }' % str(len(shared.connectedHostsList)) else: diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 5dd4c0b5..a9a7179a 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -3290,6 +3290,7 @@ def run(): try: translator.load("translations/bitmessage_" + str(locale.getdefaultlocale()[0])) + #translator.load("translations/bitmessage_fr_BE") # test French except: # The above is not compatible with all versions of OSX. translator.load("translations/bitmessage_en_US") # Default to english. diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index e07850e4..48be5e0d 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -189,7 +189,15 @@ class sqlThread(threading.Thread): if not shared.config.has_option('bitmessagesettings', 'sockslisten'): shared.config.set('bitmessagesettings', 'sockslisten', 'false') - + + # Some prewritten code for future use whenever we need to modify the database + """item = '''SELECT value FROM settings WHERE key='version';''' + parameters = '' + self.cur.execute(item, parameters) + if self.cur.fetchall()[0][0] == 1: + do something + increment the version to 2""" + try: testpayload = '\x00\x00' t = ('1234', testpayload, '12345678', 'no') diff --git a/src/message_data_reader.py b/src/message_data_reader.py index c600935d..f6102e02 100644 --- a/src/message_data_reader.py +++ b/src/message_data_reader.py @@ -5,19 +5,10 @@ import sqlite3 from time import strftime, localtime import sys +import shared +import string -APPNAME = "PyBitmessage" -from os import path, environ -if sys.platform == 'darwin': - if "HOME" in environ: - appdata = path.join(environ["HOME"], "Library/Application support/", APPNAME) + '/' - else: - print 'Could not find home folder, please report this message and your OS X version to the BitMessage Github.' - sys.exit() -elif 'win' in sys.platform: - appdata = path.join(environ['APPDATA'], APPNAME) + '\\' -else: - appdata = path.expanduser(path.join("~", "." + APPNAME + "/")) +appdata = shared.lookupAppdataFolder() conn = sqlite3.connect( appdata + 'messages.dat' ) conn.text_factory = str @@ -71,6 +62,29 @@ def readInventory(): hash, objecttype, streamnumber, payload, receivedtime = row print 'Hash:', hash.encode('hex'), objecttype, streamnumber, '\t', payload.encode('hex'), '\t', unicode(strftime('%a, %d %b %Y %I:%M %p',localtime(receivedtime)),'utf-8') +def readInventory2(): + searchValue = ' ' + + item = '''PRAGMA case_sensitive_like = true ''' + parameters = '' + cur.execute(item, parameters) + + searchValue = string.replace(searchValue,'e','ee') + searchValue = string.replace(searchValue,'%','e%') + searchValue = string.replace(searchValue,'_','e_') + + print 'Printing subset of inventory table:' + item = '''SELECT * FROM inventory WHERE hash LIKE ? ESCAPE'e'; ''' + parameters = ('%'+ searchValue + '%',) + print repr(parameters), len(parameters[0]) + cur.execute(item, parameters) + output = cur.fetchall() + print 'Number of results:', len(output) + for row in output[:20]: + hash, objecttype, streamnumber, payload, receivedtime = row + print 'Hash:', hash.encode('hex'), objecttype, streamnumber, '\t', payload.encode('hex'), '\t', unicode(strftime('%a, %d %b %Y %I:%M %p',localtime(receivedtime)),'utf-8') + print 'done' + def takeInboxMessagesOutOfTrash(): item = '''update inbox set folder='inbox' where folder='trash' ''' @@ -107,12 +121,13 @@ def vacuum(): #takeInboxMessagesOutOfTrash() #takeSentMessagesOutOfTrash() #markAllInboxMessagesAsUnread() -readInbox() +#readInbox() #readSent() #readPubkeys() #readSubscriptions() #readInventory() #vacuum() #will defragment and clean empty space from the messages.dat file. +readInventory2()