From 612333a26750240b08011e5d502e8435d07eeb9a Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Mon, 14 Nov 2016 20:23:58 +0100 Subject: [PATCH] Enable support for extended encoding - receiving extended encoding now works - sending works from the GUI by pressing "Shift" while clicking on "Send" - requires https://pypi.python.org/pypi/msgpack-python --- src/bitmessageqt/__init__.py | 6 ++++-- src/class_objectProcessor.py | 37 ++++++++++-------------------------- src/class_singleWorker.py | 25 ++++++++++++------------ 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index ff4bfbcf..03aec111 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -1890,6 +1890,8 @@ class MyForm(settingsmixin.SMainWindow): more work your computer must do to send the message. A Time-To-Live of four or five days is often appropriate."""), QMessageBox.Ok) def click_pushButtonSend(self): + encoding = 3 if QtGui.QApplication.queryKeyboardModifiers() & QtCore.Qt.ShiftModifier else 2 + self.statusBar().clearMessage() if self.ui.tabWidgetSend.currentIndex() == 0: @@ -2022,7 +2024,7 @@ class MyForm(settingsmixin.SMainWindow): 'msgqueued', 0, # retryNumber 'sent', # folder - 2, # encodingtype + encoding, # encodingtype shared.config.getint('bitmessagesettings', 'ttl') ) @@ -2075,7 +2077,7 @@ class MyForm(settingsmixin.SMainWindow): 'broadcastqueued', 0, # retryNumber 'sent', # folder - 2, # encoding type + encoding, # encoding type shared.config.getint('bitmessagesettings', 'ttl') ) sqlExecute( diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index d5fe3a31..c85b58a1 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -17,6 +17,7 @@ import helper_generic from helper_generic import addDataPadding import helper_bitcoin import helper_inbox +import helper_msgcoding import helper_sent from helper_sql import * import tr @@ -482,24 +483,15 @@ class objectProcessor(threading.Thread): if queryreturn == []: logger.info('Message ignored because address not in whitelist.') blockMessage = True - + toLabel = shared.config.get(toAddress, 'label') if toLabel == '': toLabel = toAddress - if messageEncodingType == 2: - subject, body = self.decodeType2Message(message) - logger.info('Message subject (first 100 characters): %s' % repr(subject)[:100]) - elif messageEncodingType == 1: - body = message - subject = '' - elif messageEncodingType == 0: - logger.info('messageEncodingType == 0. Doing nothing with the message. They probably just sent it so that we would store their public key or send their ack data for them.') - subject = '' - body = '' - else: - body = 'Unknown encoding type.\n\n' + repr(message) - subject = '' + decodedMessage = helper_msgcoding.MsgDecode(messageEncodingType, message) + subject = decodedMessage.subject + body = decodedMessage.body + # Let us make sure that we haven't already received this message if helper_inbox.isMessageAlreadyInInbox(sigHash): logger.info('This msg is already in our inbox. Ignoring it.') @@ -562,7 +554,7 @@ class objectProcessor(threading.Thread): 'broadcastqueued', 0, 'sent', - 2, + messageEncodingType, TTL) helper_sent.insert(t) @@ -746,18 +738,9 @@ class objectProcessor(threading.Thread): sendersAddressVersion, sendersStream, calculatedRipe) logger.debug('fromAddress: ' + fromAddress) - if messageEncodingType == 2: - subject, body = self.decodeType2Message(message) - logger.info('Broadcast subject (first 100 characters): %s' % repr(subject)[:100]) - elif messageEncodingType == 1: - body = message - subject = '' - elif messageEncodingType == 0: - logger.info('messageEncodingType == 0. Doing nothing with the message.') - return - else: - body = 'Unknown encoding type.\n\n' + repr(message) - subject = '' + decodedMessage = helper_msgcoding.MsgDecode(messageEncodingType, message) + subject = decodedMessage.subject + body = decodedMessage.body toAddress = '[Broadcast subscribers]' if helper_inbox.isMessageAlreadyInInbox(sigHash): diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index f6fc9a80..377f900b 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -15,6 +15,7 @@ from debug import logger from helper_sql import * import helper_inbox from helper_generic import addDataPadding +import helper_msgcoding from helper_threading import * import l10n from protocol import * @@ -370,10 +371,10 @@ class singleWorker(threading.Thread, StoppableThread): sqlExecute( '''UPDATE sent SET status='broadcastqueued' WHERE status = 'doingbroadcastpow' ''') queryreturn = sqlQuery( - '''SELECT fromaddress, subject, message, ackdata, ttl FROM sent WHERE status=? and folder='sent' ''', 'broadcastqueued') + '''SELECT fromaddress, subject, message, ackdata, ttl, encodingtype FROM sent WHERE status=? and folder='sent' ''', 'broadcastqueued') for row in queryreturn: - fromaddress, subject, body, ackdata, TTL = row + fromaddress, subject, body, ackdata, TTL, encoding = row status, addressVersionNumber, streamNumber, ripe = decodeAddress( fromaddress) if addressVersionNumber <= 1: @@ -436,9 +437,10 @@ class singleWorker(threading.Thread, StoppableThread): if addressVersionNumber >= 3: dataToEncrypt += encodeVarint(shared.config.getint(fromaddress,'noncetrialsperbyte')) dataToEncrypt += encodeVarint(shared.config.getint(fromaddress,'payloadlengthextrabytes')) - dataToEncrypt += '\x02' # message encoding type - dataToEncrypt += encodeVarint(len('Subject:' + subject + '\n' + 'Body:' + body)) #Type 2 is simple UTF-8 message encoding per the documentation on the wiki. - dataToEncrypt += 'Subject:' + subject + '\n' + 'Body:' + body + dataToEncrypt += encodeVarint(encoding) # message encoding type + encodedMessage = helper_msgcoding.MsgEncode({"subject": subject, "body": body}, encoding) + dataToEncrypt += encodeVarint(encodedMessage.length) + dataToEncrypt += encodedMessage.data dataToSign = payload + dataToEncrypt signature = highlevelcrypto.sign( @@ -503,9 +505,9 @@ class singleWorker(threading.Thread, StoppableThread): sqlExecute( '''UPDATE sent SET status='msgqueued' WHERE status IN ('doingpubkeypow', 'doingmsgpow')''') queryreturn = sqlQuery( - '''SELECT toaddress, fromaddress, subject, message, ackdata, status, ttl, retrynumber FROM sent WHERE (status='msgqueued' or status='forcepow') and folder='sent' ''') + '''SELECT toaddress, fromaddress, subject, message, ackdata, status, ttl, retrynumber, encodingtype FROM sent WHERE (status='msgqueued' or status='forcepow') and folder='sent' ''') for row in queryreturn: # while we have a msg that needs some work - toaddress, fromaddress, subject, message, ackdata, status, TTL, retryNumber = row + toaddress, fromaddress, subject, message, ackdata, status, TTL, retryNumber, encoding = row toStatus, toAddressVersionNumber, toStreamNumber, toRipe = decodeAddress( toaddress) fromStatus, fromAddressVersionNumber, fromStreamNumber, fromRipe = decodeAddress( @@ -751,11 +753,10 @@ class singleWorker(threading.Thread, StoppableThread): fromaddress, 'payloadlengthextrabytes')) payload += toRipe # This hash will be checked by the receiver of the message to verify that toRipe belongs to them. This prevents a Surreptitious Forwarding Attack. - payload += '\x02' # Type 2 is simple UTF-8 message encoding as specified on the Protocol Specification on the Bitmessage Wiki. - messageToTransmit = 'Subject:' + \ - subject + '\n' + 'Body:' + message - payload += encodeVarint(len(messageToTransmit)) - payload += messageToTransmit + payload += encodeVarint(encoding) # message encoding type + encodedMessage = helper_msgcoding.MsgEncode({"subject": subject, "body": message}, encoding) + payload += encodeVarint(encodedMessage.length) + payload += encodedMessage.data if shared.config.has_section(toaddress): logger.info('Not bothering to include ackdata because we are sending to ourselves or a chan.') fullAckPayload = ''