Have API calls use sql helper

This commit is contained in:
Grant T. Olson 2013-08-26 20:00:53 -04:00 committed by Grant T. Olson
parent edf9101eae
commit 74cd6c24b2

View File

@ -25,6 +25,7 @@ if sys.platform == 'darwin':
sys.exit(0) sys.exit(0)
# Classes # Classes
from helper_sql import *
from class_sqlThread import * from class_sqlThread import *
from class_singleCleaner import * from class_singleCleaner import *
from class_singleWorker import * from class_singleWorker import *
@ -315,12 +316,8 @@ 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': elif method == 'getAllInboxMessages':
shared.sqlLock.acquire() queryreturn = sqlQuery(
shared.sqlSubmitQueue.put(
'''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox where folder='inbox' ORDER BY received''') '''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox where folder='inbox' ORDER BY received''')
shared.sqlSubmitQueue.put('')
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"inboxMessages":[' data = '{"inboxMessages":['
for row in queryreturn: for row in queryreturn:
msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row
@ -333,12 +330,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
data += ']}' data += ']}'
return data return data
elif method == 'getAllInboxMessageIds' or method == 'getAllInboxMessageIDs': elif method == 'getAllInboxMessageIds' or method == 'getAllInboxMessageIDs':
shared.sqlLock.acquire() queryreturn = sqlQuery(
shared.sqlSubmitQueue.put(
'''SELECT msgid FROM inbox where folder='inbox' ORDER BY received''') '''SELECT msgid FROM inbox where folder='inbox' ORDER BY received''')
shared.sqlSubmitQueue.put('')
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"inboxMessageIds":[' data = '{"inboxMessageIds":['
for row in queryreturn: for row in queryreturn:
msgid = row[0] msgid = row[0]
@ -351,12 +344,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
if len(params) == 0: if len(params) == 0:
raise APIError(0, 'I need parameters!') raise APIError(0, 'I need parameters!')
msgid = self._decode(params[0], "hex") msgid = self._decode(params[0], "hex")
v = (msgid,) queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox WHERE msgid=?''', msgid)
shared.sqlLock.acquire()
shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox WHERE msgid=?''')
shared.sqlSubmitQueue.put(v)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"inboxMessage":[' data = '{"inboxMessage":['
for row in queryreturn: for row in queryreturn:
msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row msgid, toAddress, fromAddress, subject, received, message, encodingtype, read = row
@ -366,11 +354,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
data += ']}' data += ']}'
return data return data
elif method == 'getAllSentMessages': elif method == 'getAllSentMessages':
shared.sqlLock.acquire() queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent where folder='sent' ORDER BY lastactiontime''')
shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent where folder='sent' ORDER BY lastactiontime''')
shared.sqlSubmitQueue.put('')
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"sentMessages":[' data = '{"sentMessages":['
for row in queryreturn: for row in queryreturn:
msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row
@ -382,11 +366,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
data += ']}' data += ']}'
return data return data
elif method == 'getAllSentMessageIds' or method == 'getAllSentMessageIDs': elif method == 'getAllSentMessageIds' or method == 'getAllSentMessageIDs':
shared.sqlLock.acquire() queryreturn = sqlQuery('''SELECT msgid FROM sent where folder='sent' ORDER BY lastactiontime''')
shared.sqlSubmitQueue.put('''SELECT msgid FROM sent where folder='sent' ORDER BY lastactiontime''')
shared.sqlSubmitQueue.put('')
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"sentMessageIds":[' data = '{"sentMessageIds":['
for row in queryreturn: for row in queryreturn:
msgid = row[0] msgid = row[0]
@ -399,12 +379,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
if len(params) == 0: if len(params) == 0:
raise APIError(0, 'I need parameters!') raise APIError(0, 'I need parameters!')
toAddress = params[0] toAddress = params[0]
v = (toAddress,) queryReturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype FROM inbox WHERE folder='inbox' AND toAddress=?''', toAddress)
shared.sqlLock.acquire()
shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype FROM inbox WHERE folder='inbox' AND toAddress=?''')
shared.sqlSubmitQueue.put(v)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"inboxMessages":[' data = '{"inboxMessages":['
for row in queryreturn: for row in queryreturn:
msgid, toAddress, fromAddress, subject, received, message, encodingtype = row msgid, toAddress, fromAddress, subject, received, message, encodingtype = row
@ -419,12 +394,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
if len(params) == 0: if len(params) == 0:
raise APIError(0, 'I need parameters!') raise APIError(0, 'I need parameters!')
msgid = self._decode(params[0], "hex") msgid = self._decode(params[0], "hex")
v = (msgid,) queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent WHERE msgid=?''', msgid)
shared.sqlLock.acquire()
shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent WHERE msgid=?''')
shared.sqlSubmitQueue.put(v)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"sentMessage":[' data = '{"sentMessage":['
for row in queryreturn: for row in queryreturn:
msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row
@ -437,12 +407,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
if len(params) == 0: if len(params) == 0:
raise APIError(0, 'I need parameters!') raise APIError(0, 'I need parameters!')
fromAddress = params[0] fromAddress = params[0]
v = (fromAddress,) queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent WHERE folder='sent' AND fromAddress=? ORDER BY lastactiontime''',
shared.sqlLock.acquire() fromAddress)
shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent WHERE folder='sent' AND fromAddress=? ORDER BY lastactiontime''')
shared.sqlSubmitQueue.put(v)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"sentMessages":[' data = '{"sentMessages":['
for row in queryreturn: for row in queryreturn:
msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row
@ -457,12 +423,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
if len(params) == 0: if len(params) == 0:
raise APIError(0, 'I need parameters!') raise APIError(0, 'I need parameters!')
ackData = self._decode(params[0], "hex") ackData = self._decode(params[0], "hex")
v = (ackData,) queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent WHERE ackdata=?''',
shared.sqlLock.acquire() ackData)
shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, lastactiontime, message, encodingtype, status, ackdata FROM sent WHERE ackdata=?''')
shared.sqlSubmitQueue.put(v)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"sentMessage":[' data = '{"sentMessage":['
for row in queryreturn: for row in queryreturn:
msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row msgid, toAddress, fromAddress, subject, lastactiontime, message, encodingtype, status, ackdata = row
@ -480,13 +442,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
helper_inbox.trash(msgid) helper_inbox.trash(msgid)
# Trash if in sent table # Trash if in sent table
t = (msgid,) t = (msgid,)
shared.sqlLock.acquire() sqlExecute('''UPDATE sent SET folder='trash' WHERE msgid=?''', t)
shared.sqlSubmitQueue.put('''UPDATE sent SET folder='trash' WHERE msgid=?''')
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
# shared.UISignalQueue.put(('removeSentRowByMsgid',msgid)) This function doesn't exist yet.
return 'Trashed message (assuming message existed).' return 'Trashed message (assuming message existed).'
elif method == 'trashInboxMessage': elif method == 'trashInboxMessage':
if len(params) == 0: if len(params) == 0:
@ -499,13 +455,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
raise APIError(0, 'I need parameters!') raise APIError(0, 'I need parameters!')
msgid = self._decode(params[0], "hex") msgid = self._decode(params[0], "hex")
t = (msgid,) t = (msgid,)
shared.sqlLock.acquire() sqlExecute('''UPDATE sent SET folder='trash' WHERE msgid=?''', t)
shared.sqlSubmitQueue.put('''UPDATE sent SET folder='trash' WHERE msgid=?''')
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
# shared.UISignalQueue.put(('removeSentRowByMsgid',msgid)) This function doesn't exist yet.
return 'Trashed sent message (assuming message existed).' return 'Trashed sent message (assuming message existed).'
elif method == 'sendMessage': elif method == 'sendMessage':
if len(params) == 0: if len(params) == 0:
@ -568,13 +518,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
helper_sent.insert(t) helper_sent.insert(t)
toLabel = '' toLabel = ''
t = (toAddress,) queryreturn = sqlQuery('''select label from addressbook where address=?''', toAddress)
shared.sqlLock.acquire()
shared.sqlSubmitQueue.put(
'''select label from addressbook where address=?''')
shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
if queryreturn != []: if queryreturn != []:
for row in queryreturn: for row in queryreturn:
toLabel, = row toLabel, = row
@ -643,12 +587,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
if len(ackdata) != 64: if len(ackdata) != 64:
raise APIError(15, '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") ackdata = self._decode(ackdata, "hex")
shared.sqlLock.acquire() queryreturn = sqlQuery(
shared.sqlSubmitQueue.put( '''SELECT status FROM sent where ackdata=?''',
'''SELECT status FROM sent where ackdata=?''') ackdata)
shared.sqlSubmitQueue.put((ackdata,))
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
if queryreturn == []: if queryreturn == []:
return 'notfound' return 'notfound'
for row in queryreturn: for row in queryreturn:
@ -688,23 +629,10 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
raise APIError(12, '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 # First we must check to see if the address is already in the
# subscriptions list. # subscriptions list.
shared.sqlLock.acquire() queryreturn = sqlQuery('''select * from subscriptions where address=?''', address)
t = (address,)
shared.sqlSubmitQueue.put(
'''select * from subscriptions where address=?''')
shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
if queryreturn != []: if queryreturn != []:
raise APIError(16, 'You are already subscribed to that address.') raise APIError(16, 'You are already subscribed to that address.')
t = (label, address, True) sqlExecute('''INSERT INTO subscriptions VALUES (?,?,?)''',label, address, True)
shared.sqlLock.acquire()
shared.sqlSubmitQueue.put(
'''INSERT INTO subscriptions VALUES (?,?,?)''')
shared.sqlSubmitQueue.put(t)
queryreturn = shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
shared.reloadBroadcastSendersForWhichImWatching() shared.reloadBroadcastSendersForWhichImWatching()
shared.UISignalQueue.put(('rerenderInboxFromLabels', '')) shared.UISignalQueue.put(('rerenderInboxFromLabels', ''))
shared.UISignalQueue.put(('rerenderSubscriptions', '')) shared.UISignalQueue.put(('rerenderSubscriptions', ''))
@ -715,24 +643,13 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
raise APIError(0, 'I need 1 parameter!') raise APIError(0, 'I need 1 parameter!')
address, = params address, = params
address = addBMIfNotPresent(address) address = addBMIfNotPresent(address)
t = (address,) sqlExecute('''DELETE FROM subscriptions WHERE address=?''', address)
shared.sqlLock.acquire()
shared.sqlSubmitQueue.put(
'''DELETE FROM subscriptions WHERE address=?''')
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
shared.sqlSubmitQueue.put('commit')
shared.sqlLock.release()
shared.reloadBroadcastSendersForWhichImWatching() shared.reloadBroadcastSendersForWhichImWatching()
shared.UISignalQueue.put(('rerenderInboxFromLabels', '')) shared.UISignalQueue.put(('rerenderInboxFromLabels', ''))
shared.UISignalQueue.put(('rerenderSubscriptions', '')) shared.UISignalQueue.put(('rerenderSubscriptions', ''))
return 'Deleted subscription if it existed.' return 'Deleted subscription if it existed.'
elif method == 'listSubscriptions': elif method == 'listSubscriptions':
shared.sqlLock.acquire() queryreturn = sqlQuery('''SELECT label, address, enabled FROM subscriptions''')
shared.sqlSubmitQueue.put('''SELECT label, address, enabled FROM subscriptions''')
shared.sqlSubmitQueue.put('')
queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release()
data = '{"subscriptions":[' data = '{"subscriptions":['
for row in queryreturn: for row in queryreturn:
label, address, enabled = row label, address, enabled = row
@ -803,26 +720,18 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
# use it we'll need to fill out a field in our inventory database # use it we'll need to fill out a field in our inventory database
# which is blank by default (first20bytesofencryptedmessage). # which is blank by default (first20bytesofencryptedmessage).
parameters = '' parameters = ''
with shared.sqlLock: queryreturn = sqlQuery(
shared.sqlSubmitQueue.put('''SELECT hash, payload FROM inventory WHERE first20bytesofencryptedmessage = '' and objecttype = 'msg' ; ''') '''SELECT hash, payload FROM inventory WHERE first20bytesofencryptedmessage = '' and objecttype = 'msg' ; ''')
shared.sqlSubmitQueue.put(parameters)
queryreturn = shared.sqlReturnQueue.get()
for row in queryreturn: for row in queryreturn:
hash, payload = row hash, payload = row
readPosition = 16 # Nonce length + time length readPosition = 16 # Nonce length + time length
readPosition += decodeVarint(payload[readPosition:readPosition+10])[1] # Stream Number length readPosition += decodeVarint(payload[readPosition:readPosition+10])[1] # Stream Number length
t = (payload[readPosition:readPosition+20],hash) t = (payload[readPosition:readPosition+20],hash)
shared.sqlSubmitQueue.put('''UPDATE inventory SET first20bytesofencryptedmessage=? WHERE hash=?; ''') sqlExecute('''UPDATE inventory SET first20bytesofencryptedmessage=? WHERE hash=?; ''', t)
shared.sqlSubmitQueue.put(t)
shared.sqlReturnQueue.get()
parameters = (requestedHash,) queryreturn = sqlQuery('''SELECT payload FROM inventory WHERE first20bytesofencryptedmessage = ?''',
with shared.sqlLock: requestedHash)
shared.sqlSubmitQueue.put('commit')
shared.sqlSubmitQueue.put('''SELECT payload FROM inventory WHERE first20bytesofencryptedmessage = ?''')
shared.sqlSubmitQueue.put(parameters)
queryreturn = shared.sqlReturnQueue.get()
data = '{"receivedMessageDatas":[' data = '{"receivedMessageDatas":['
for row in queryreturn: for row in queryreturn:
payload, = row payload, = row
@ -840,11 +749,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
if len(requestedHash) != 40: if len(requestedHash) != 40:
raise APIError(19, 'The length of hash should be 20 bytes (encoded in hex thus 40 characters).') raise APIError(19, 'The length of hash should be 20 bytes (encoded in hex thus 40 characters).')
requestedHash = self._decode(requestedHash, "hex") requestedHash = self._decode(requestedHash, "hex")
parameters = (requestedHash,) queryreturn = sqlQuery('''SELECT transmitdata FROM pubkeys WHERE hash = ? ; ''', requestedHash)
with shared.sqlLock:
shared.sqlSubmitQueue.put('''SELECT transmitdata FROM pubkeys WHERE hash = ? ; ''')
shared.sqlSubmitQueue.put(parameters)
queryreturn = shared.sqlReturnQueue.get()
data = '{"pubkey":[' data = '{"pubkey":['
for row in queryreturn: for row in queryreturn:
transmitdata, = row transmitdata, = row