Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick workaround for BLOB as TEXT problem (#2247) #2248

Open
wants to merge 3 commits into
base: v0.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions revert_blob_to_text.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

python3 pybitmessage/revert_blob_to_text.py "$@"
74 changes: 59 additions & 15 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import time
from binascii import hexlify, unhexlify
from struct import pack, unpack
import sqlite3

import six
from six.moves import configparser, http_client, xmlrpc_server
Expand Down Expand Up @@ -953,20 +954,32 @@ def HandleGetInboxMessageById(self, hid, readStatus=None):
23, 'Bool expected in readStatus, saw %s instead.'
% type(readStatus))
queryreturn = sqlQuery(
"SELECT read FROM inbox WHERE msgid=?", msgid)
"SELECT read FROM inbox WHERE msgid=?", sqlite3.Binary(msgid))
if len(queryreturn) < 1:
queryreturn = sqlQuery(
"SELECT read FROM inbox WHERE msgid=CAST(? AS TEXT)", msgid)
# UPDATE is slow, only update if status is different
try:
if (queryreturn[0][0] == 1) != readStatus:
sqlExecute(
rowcount = sqlExecute(
"UPDATE inbox set read = ? WHERE msgid=?",
readStatus, msgid)
readStatus, sqlite3.Binary(msgid))
if rowcount < 1:
rowcount = sqlExecute(
"UPDATE inbox set read = ? WHERE msgid=CAST(? AS TEXT)",
readStatus, msgid)
queues.UISignalQueue.put(('changedInboxUnread', None))
except IndexError:
pass
queryreturn = sqlQuery(
"SELECT msgid, toaddress, fromaddress, subject, received, message,"
" encodingtype, read FROM inbox WHERE msgid=?", msgid
" encodingtype, read FROM inbox WHERE msgid=?", sqlite3.Binary(msgid)
)
if len(queryreturn) < 1:
queryreturn = sqlQuery(
"SELECT msgid, toaddress, fromaddress, subject, received, message,"
" encodingtype, read FROM inbox WHERE msgid=CAST(? AS TEXT)", msgid
)
try:
return {"inboxMessage": [
self._dump_inbox_message(*queryreturn[0])]}
Expand Down Expand Up @@ -1035,8 +1048,14 @@ def HandleGetSentMessageById(self, hid):
queryreturn = sqlQuery(
"SELECT msgid, toaddress, fromaddress, subject, lastactiontime,"
" message, encodingtype, status, ackdata FROM sent WHERE msgid=?",
msgid
sqlite3.Binary(msgid)
)
if len(queryreturn) < 1:
queryreturn = sqlQuery(
"SELECT msgid, toaddress, fromaddress, subject, lastactiontime,"
" message, encodingtype, status, ackdata FROM sent WHERE msgid=CAST(? AS TEXT)",
msgid
)
try:
return {"sentMessage": [
self._dump_sent_message(*queryreturn[0])
Expand Down Expand Up @@ -1072,8 +1091,14 @@ def HandleGetSentMessagesByAckData(self, ackData):
queryreturn = sqlQuery(
"SELECT msgid, toaddress, fromaddress, subject, lastactiontime,"
" message, encodingtype, status, ackdata FROM sent"
" WHERE ackdata=?", ackData
" WHERE ackdata=?", sqlite3.Binary(ackData)
)
if len(queryreturn) < 1:
queryreturn = sqlQuery(
"SELECT msgid, toaddress, fromaddress, subject, lastactiontime,"
" message, encodingtype, status, ackdata FROM sent"
" WHERE ackdata=CAST(? AS TEXT)", ackData
)

try:
return {"sentMessage": [
Expand All @@ -1093,7 +1118,9 @@ def HandleTrashMessage(self, msgid):
# Trash if in inbox table
helper_inbox.trash(msgid)
# Trash if in sent table
sqlExecute("UPDATE sent SET folder='trash' WHERE msgid=?", msgid)
rowcount = sqlExecute("UPDATE sent SET folder='trash' WHERE msgid=?", sqlite3.Binary(msgid))
if rowcount < 1:
sqlExecute("UPDATE sent SET folder='trash' WHERE msgid=CAST(? AS TEXT)", msgid)
return 'Trashed message (assuming message existed).'

@command('trashInboxMessage')
Expand All @@ -1107,7 +1134,9 @@ def HandleTrashInboxMessage(self, msgid):
def HandleTrashSentMessage(self, msgid):
"""Trash sent message by msgid (encoded in hex)."""
msgid = self._decode(msgid, "hex")
sqlExecute('''UPDATE sent SET folder='trash' WHERE msgid=?''', msgid)
rowcount = sqlExecute('''UPDATE sent SET folder='trash' WHERE msgid=?''', sqlite3.Binary(msgid))
if rowcount < 1:
sqlExecute('''UPDATE sent SET folder='trash' WHERE msgid=CAST(? AS TEXT)''', msgid)
return 'Trashed sent message (assuming message existed).'

@command('sendMessage')
Expand Down Expand Up @@ -1217,7 +1246,10 @@ def HandleGetStatus(self, ackdata):
raise APIError(15, 'Invalid ackData object size.')
ackdata = self._decode(ackdata, "hex")
queryreturn = sqlQuery(
"SELECT status FROM sent where ackdata=?", ackdata)
"SELECT status FROM sent where ackdata=?", sqlite3.Binary(ackdata))
if len(queryreturn) < 1:
queryreturn = sqlQuery(
"SELECT status FROM sent where ackdata=CAST(? AS TEXT)", ackdata)
try:
return queryreturn[0][0]
except IndexError:
Expand Down Expand Up @@ -1354,7 +1386,9 @@ def HandleTrashSentMessageByAckDAta(self, ackdata):
"""Trash a sent message by ackdata (hex encoded)"""
# This API method should only be used when msgid is not available
ackdata = self._decode(ackdata, "hex")
sqlExecute("UPDATE sent SET folder='trash' WHERE ackdata=?", ackdata)
rowcount = sqlExecute("UPDATE sent SET folder='trash' WHERE ackdata=?", sqlite3.Binary(ackdata))
if rowcount < 1:
sqlExecute("UPDATE sent SET folder='trash' WHERE ackdata=CAST(? AS TEXT)", ackdata)
return 'Trashed sent message (assuming message existed).'

@command('disseminatePubkey')
Expand Down Expand Up @@ -1421,19 +1455,29 @@ def HandleGetMessageDataByDestinationHash(self, requestedHash):
# use it we'll need to fill out a field in our inventory database
# which is blank by default (first20bytesofencryptedmessage).
queryreturn = sqlQuery(
"SELECT hash, payload FROM inventory WHERE tag = ''"
" and objecttype = 2")
"SELECT hash, payload FROM inventory WHERE tag = ?"
" and objecttype = 2", sqlite3.Binary(b""))
if len(queryreturn) < 1:
queryreturn = sqlQuery(
"SELECT hash, payload FROM inventory WHERE tag = CAST(? AS TEXT)"
" and objecttype = 2", b"")
with SqlBulkExecute() as sql:
for hash01, payload in queryreturn:
readPosition = 16 # Nonce length + time length
# Stream Number length
readPosition += decodeVarint(
payload[readPosition:readPosition + 10])[1]
t = (payload[readPosition:readPosition + 32], hash01)
sql.execute("UPDATE inventory SET tag=? WHERE hash=?", *t)
t = (sqlite3.Binary(payload[readPosition:readPosition + 32]), sqlite3.Binary(hash01))
_, rowcount = sql.execute("UPDATE inventory SET tag=? WHERE hash=?", *t)
if rowcount < 1:
t = (sqlite3.Binary(payload[readPosition:readPosition + 32]), hash01)
sql.execute("UPDATE inventory SET tag=? WHERE hash=CAST(? AS TEXT)", *t)

queryreturn = sqlQuery(
"SELECT payload FROM inventory WHERE tag = ?", requestedHash)
"SELECT payload FROM inventory WHERE tag = ?", sqlite3.Binary(requestedHash))
if len(queryreturn) < 1:
queryreturn = sqlQuery(
"SELECT payload FROM inventory WHERE tag = CAST(? AS TEXT)", requestedHash)
return {"receivedMessageDatas": [
{'data': hexlify(payload)} for payload, in queryreturn
]}
Expand Down
41 changes: 32 additions & 9 deletions src/bitmessagecurses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import time
from textwrap import fill
from threading import Timer
import sqlite3

from dialog import Dialog
import helper_sent
Expand Down Expand Up @@ -358,7 +359,9 @@
inbox[inboxcur][1] +
"\"")
data = "" # pyint: disable=redefined-outer-name
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=?", inbox[inboxcur][0])
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=?", sqlite3.Binary(inbox[inboxcur][0]))

Check failure on line 362 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (121 > 119 characters)

Check failure on line 362 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (121 > 119 characters)
if len(ret) < 1:
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=CAST(? AS TEXT)", inbox[inboxcur][0])

Check failure on line 364 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (123 > 119 characters)

Check failure on line 364 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (123 > 119 characters)
if ret != []:
for row in ret:
data, = row
Expand All @@ -367,12 +370,16 @@
for i, item in enumerate(data.split("\n")):
msg += fill(item, replace_whitespace=False) + "\n"
scrollbox(d, unicode(ascii(msg)), 30, 80)
sqlExecute("UPDATE inbox SET read=1 WHERE msgid=?", inbox[inboxcur][0])
rowcount = sqlExecute("UPDATE inbox SET read=1 WHERE msgid=?", sqlite3.Binary(inbox[inboxcur][0]))

Check failure on line 373 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (130 > 119 characters)

Check failure on line 373 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (130 > 119 characters)
if rowcount < 1:
sqlExecute("UPDATE inbox SET read=1 WHERE msgid=CAST(? AS TEXT)", inbox[inboxcur][0])

Check failure on line 375 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (121 > 119 characters)

Check failure on line 375 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (121 > 119 characters)
inbox[inboxcur][7] = 1
else:
scrollbox(d, unicode("Could not fetch message."))
elif t == "2": # Mark unread
sqlExecute("UPDATE inbox SET read=0 WHERE msgid=?", inbox[inboxcur][0])
rowcount = sqlExecute("UPDATE inbox SET read=0 WHERE msgid=?", sqlite3.Binary(inbox[inboxcur][0]))

Check failure on line 380 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (126 > 119 characters)

Check failure on line 380 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (126 > 119 characters)
if rowcount < 1:
sqlExecute("UPDATE inbox SET read=0 WHERE msgid=CAST(? AS TEXT)", inbox[inboxcur][0])
inbox[inboxcur][7] = 0
elif t == "3": # Reply
curses.curs_set(1)
Expand All @@ -396,7 +403,9 @@
if not m[5][:4] == "Re: ":
subject = "Re: " + m[5]
body = ""
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=?", m[0])
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=?", sqlite3.Binary(m[0]))
if len(ret) < 1:
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=CAST(? AS TEXT)", m[0])
if ret != []:
body = "\n\n------------------------------------------------------\n"
for row in ret:
Expand All @@ -422,7 +431,9 @@
r, t = d.inputbox("Filename", init=inbox[inboxcur][5] + ".txt")
if r == d.DIALOG_OK:
msg = ""
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=?", inbox[inboxcur][0])
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=?", sqlite3.Binary(inbox[inboxcur][0]))

Check failure on line 434 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (125 > 119 characters)

Check failure on line 434 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (125 > 119 characters)
if len(ret) < 1:
ret = sqlQuery("SELECT message FROM inbox WHERE msgid=CAST(? AS TEXT)", inbox[inboxcur][0])

Check failure on line 436 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (127 > 119 characters)

Check failure on line 436 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (127 > 119 characters)
if ret != []:
for row in ret:
msg, = row
Expand All @@ -432,7 +443,9 @@
else:
scrollbox(d, unicode("Could not fetch message."))
elif t == "6": # Move to trash
sqlExecute("UPDATE inbox SET folder='trash' WHERE msgid=?", inbox[inboxcur][0])
rowcount = sqlExecute("UPDATE inbox SET folder='trash' WHERE msgid=?", sqlite3.Binary(inbox[inboxcur][0]))

Check failure on line 446 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (134 > 119 characters)

Check failure on line 446 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (134 > 119 characters)
if rowcount < 1:
sqlExecute("UPDATE inbox SET folder='trash' WHERE msgid=CAST(? AS TEXT)", inbox[inboxcur][0])

Check failure on line 448 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - pycodestyle

E501

line too long (125 > 119 characters)

Check failure on line 448 in src/bitmessagecurses/__init__.py

View check run for this annotation

PyBitmessage Code Quality Checks / Code Quality - flake8

E501

line too long (125 > 119 characters)
del inbox[inboxcur]
scrollbox(d, unicode(
"Message moved to trash. There is no interface to view your trash,"
Expand Down Expand Up @@ -464,7 +477,12 @@
ret = sqlQuery(
"SELECT message FROM sent WHERE subject=? AND ackdata=?",
sentbox[sentcur][4],
sentbox[sentcur][6])
sqlite3.Binary(sentbox[sentcur][6]))
if len(ret) < 1:
ret = sqlQuery(
"SELECT message FROM sent WHERE subject=? AND ackdata=CAST(? AS TEXT)",
sentbox[sentcur][4],
sentbox[sentcur][6])
if ret != []:
for row in ret:
data, = row
Expand All @@ -476,10 +494,15 @@
else:
scrollbox(d, unicode("Could not fetch message."))
elif t == "2": # Move to trash
sqlExecute(
rowcount = sqlExecute(
"UPDATE sent SET folder='trash' WHERE subject=? AND ackdata=?",
sentbox[sentcur][4],
sentbox[sentcur][6])
sqlite3.Binary(sentbox[sentcur][6]))
if rowcount < 1:
rowcount = sqlExecute(
"UPDATE sent SET folder='trash' WHERE subject=? AND ackdata=CAST(? AS TEXT)",
sentbox[sentcur][4],
sentbox[sentcur][6])
del sentbox[sentcur]
scrollbox(d, unicode(
"Message moved to trash. There is no interface to view your trash"
Expand Down
7 changes: 6 additions & 1 deletion src/bitmessagekivy/baseclass/maildetail.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import os
from datetime import datetime
import sqlite3

from kivy.core.clipboard import Clipboard
from kivy.clock import Clock
Expand Down Expand Up @@ -111,7 +112,11 @@ def init_ui(self, dt=0):
elif self.kivy_state.detail_page_type == 'inbox':
data = sqlQuery(
"select toaddress, fromaddress, subject, message, received from inbox"
" where msgid = ?", self.kivy_state.mail_id)
" where msgid = ?", sqlite3.Binary(self.kivy_state.mail_id))
if len(data) < 1:
data = sqlQuery(
"select toaddress, fromaddress, subject, message, received from inbox"
" where msgid = CAST(? AS TEXT)", self.kivy_state.mail_id)
self.assign_mail_details(data)
App.get_running_app().set_mail_detail_header()
except Exception as e: # pylint: disable=unused-variable
Expand Down
Loading