From e5c065416f9666745e03d2c647736a5c2037fb1e Mon Sep 17 00:00:00 2001
From: Kashiko Koibumi <kashiko@tuta.io>
Date: Thu, 30 May 2024 01:49:57 +0900
Subject: [PATCH 1/2] fix types

---
 src/class_objectProcessor.py |  2 +-
 src/helper_msgcoding.py      | 13 ++++++-------
 2 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py
index 3b555257..a260df76 100644
--- a/src/class_objectProcessor.py
+++ b/src/class_objectProcessor.py
@@ -1055,7 +1055,7 @@ class objectProcessor(threading.Thread):
             logger.info('ackdata checksum wrong. Not sending ackdata.')
             return False
         command = command.rstrip(b'\x00')
-        if command != 'object':
+        if command != b'object':
             return False
         return True
 
diff --git a/src/helper_msgcoding.py b/src/helper_msgcoding.py
index 62214525..bddb535c 100644
--- a/src/helper_msgcoding.py
+++ b/src/helper_msgcoding.py
@@ -2,7 +2,6 @@
 Message encoding end decoding functions
 """
 
-import string
 import zlib
 
 import messagetypes
@@ -100,14 +99,14 @@ class MsgDecode(object):
     def decodeExtended(self, data):
         """Handle extended encoding"""
         dc = zlib.decompressobj()
-        tmp = ""
+        tmp = b""
         while len(tmp) <= config.safeGetInt("zlib", "maxsize"):
             try:
                 got = dc.decompress(
                     data, config.safeGetInt("zlib", "maxsize")
                     + 1 - len(tmp))
                 # EOF
-                if got == "":
+                if got == b"":
                     break
                 tmp += got
                 data = dc.unconsumed_tail
@@ -143,7 +142,7 @@ class MsgDecode(object):
 
     def decodeSimple(self, data):
         """Handle simple encoding"""
-        bodyPositionIndex = string.find(data, '\nBody:')
+        bodyPositionIndex = data.find(b'\nBody:')
         if bodyPositionIndex > 1:
             subject = data[8:bodyPositionIndex]
             # Only save and show the first 500 characters of the subject.
@@ -151,10 +150,10 @@ class MsgDecode(object):
             subject = subject[:500]
             body = data[bodyPositionIndex + 6:]
         else:
-            subject = ''
+            subject = b''
             body = data
         # Throw away any extra lines (headers) after the subject.
         if subject:
             subject = subject.splitlines()[0]
-        self.subject = subject
-        self.body = body
+        self.subject = subject.decode("utf-8", "replace")
+        self.body = body.decode("utf-8", "replace")

From c39dd18212adf09f191f943085975d25212db447 Mon Sep 17 00:00:00 2001
From: Kashiko Koibumi <kashiko@tuta.io>
Date: Thu, 30 May 2024 02:10:02 +0900
Subject: [PATCH 2/2] fix one of database compatibility problems; others
 remained

---
 src/bitmessageqt/__init__.py   | 38 +++++++++++++++++++++++++++++++---
 src/bitmessageqt/foldertree.py |  2 +-
 2 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py
index 1cf83e8d..60ab3c82 100644
--- a/src/bitmessageqt/__init__.py
+++ b/src/bitmessageqt/__init__.py
@@ -16,6 +16,10 @@ from datetime import datetime, timedelta
 from sqlite3 import register_adapter
 import six
 from six.moves import range as xrange
+if six.PY3:
+    from codecs import escape_decode
+if six.PY2:
+    import sqlite3
 
 from unqstr import ustr, unic
 from PyQt4 import QtCore, QtGui
@@ -2943,7 +2947,15 @@ class MyForm(settingsmixin.SMainWindow):
         if not msgid:
             return
         queryreturn = sqlQuery(
-            '''select message from inbox where msgid=?''', msgid)
+            'SELECT message FROM inbox WHERE msgid=?', msgid)
+        # for compatibility
+        if len(queryreturn) < 1:
+            if six.PY3:
+                queryreturn = sqlQuery(
+                    'SELECT message FROM inbox WHERE msgid=CAST(? AS TEXT)', msgid)
+            else:  # assume six.PY2
+                queryreturn = sqlQuery(
+                    'SELECT message FROM inbox WHERE msgid=?', sqlite3.Binary(msgid))
         if queryreturn != []:
             for row in queryreturn:
                 messageText, = row
@@ -3603,7 +3615,11 @@ class MyForm(settingsmixin.SMainWindow):
         if messagelist:
             currentRow = messagelist.currentRow()
             if currentRow >= 0:
-                return messagelist.item(currentRow, 3).data()
+                msgid_str = messagelist.item(currentRow, 3).data()
+                if six.PY3:
+                    return escape_decode(msgid_str)[0][2:-1]
+                else:  # assume six.PY2
+                    return msgid_str
 
     def getCurrentMessageTextedit(self):
         currentIndex = self.ui.tabWidget.currentIndex()
@@ -4147,11 +4163,27 @@ class MyForm(settingsmixin.SMainWindow):
         folder = self.getCurrentFolder()
         if msgid:
             queryreturn = sqlQuery(
-                '''SELECT message FROM %s WHERE %s=?''' % (
+                'SELECT message FROM %s WHERE %s=?' % (
                     ('sent', 'ackdata') if folder == 'sent'
                     else ('inbox', 'msgid')
                 ), msgid
             )
+            # for compatibility
+            if len(queryreturn) < 1:
+                if six.PY3:
+                    queryreturn = sqlQuery(
+                        'SELECT message FROM %s WHERE %s=CAST(? AS TEXT)' % (
+                            ('sent', 'ackdata') if folder == 'sent'
+                            else ('inbox', 'msgid')
+                        ), msgid
+                    )
+                else:  # assume six.PY2
+                    queryreturn = sqlQuery(
+                        'SELECT message FROM %s WHERE %s=?' % (
+                            ('sent', 'ackdata') if folder == 'sent'
+                            else ('inbox', 'msgid')
+                        ), sqlite3.Binary(msgid)
+                    )
 
         try:
             message = queryreturn[-1][0]
diff --git a/src/bitmessageqt/foldertree.py b/src/bitmessageqt/foldertree.py
index 41a5da83..2d3cf01d 100644
--- a/src/bitmessageqt/foldertree.py
+++ b/src/bitmessageqt/foldertree.py
@@ -478,7 +478,7 @@ class MessageList_TimeWidget(BMTableWidgetItem):
 
     def __init__(self, label=None, unread=False, timestamp=None, msgid=b''):
         super(MessageList_TimeWidget, self).__init__(label, unread)
-        self.setData(QtCore.Qt.UserRole, QtCore.QByteArray(msgid))
+        self.setData(QtCore.Qt.UserRole, QtCore.QByteArray(bytes(msgid)))
         self.setData(TimestampRole, int(timestamp))
 
     def __lt__(self, other):