From 213519bd9337bce33d8838146bc7c35f063886f6 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Wed, 25 Dec 2019 22:09:17 +0100 Subject: [PATCH 01/28] Blind chain signature verification - also adds serialisation, deserialisation and optional metadata --- src/pyelliptic/blindchain.py | 77 +++++++++++++++++++++++++++++++++ src/pyelliptic/eccblind.py | 82 +++++++++++++++++++++++++++++++++--- src/tests/test_blindsig.py | 48 ++++++++++++++++++++- 3 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 src/pyelliptic/blindchain.py diff --git a/src/pyelliptic/blindchain.py b/src/pyelliptic/blindchain.py new file mode 100644 index 00000000..df1d3049 --- /dev/null +++ b/src/pyelliptic/blindchain.py @@ -0,0 +1,77 @@ +""" +Blind signature chain with a top level CA +""" + +from eccblind import ECCBlind + +try: + import msgpack +except ImportError: + try: + import umsgpack as msgpack + except ImportError: + import fallback.umsgpack.umsgpack as msgpack + +from eccblind import ECCBlind + +def encode_datetime(obj): + """ + Method to format time + """ + return {'Time': int(obj.strftime("%s"))} + + +class ECCBlindChain(object): # pylint: disable=too-many-instance-attributes + """ + # Class for ECC Blind Chain signature functionality + """ + chain = [] + ca = [] + + def __init__(self, chain=None): + if chain is not None: + self.chain = chain + + def serialize(self): + data = msgpack.packb(self.chain) + return data + + @staticmethod + def deserialize(self, chain): + """ + Deserialize the data using msgpack + """ + data = msgpack.unpackb(chain) + return ECCBlindChain(data) + + def add_level(self, pubkey, metadata, signature): + self.chain.append((pubkey, metadata, signature)) + + def add_ca(self, ca): + pubkey, metadata = ECCBlind.deserialize(ca) + self.ca.append(pubkey) + + def verify(self, msg, value): + lastpubkey = None + retval = False + for level in reversed(self.chain): + pubkey, metadata, signature = level + verifier_obj = ECCBlind(pubkey=pubkey, metadata) + if not lastpubkey: + retval = verifier_obj.verify(msg, signature, value) + else: + retval = verifier_obj.verify(lastpubkey, signature, value) + if not reval: + break + lastpubkey = pubkey + if retval: + retval = False + for ca in self.ca: + match = True + for i in range(4): + if lastpubkey[i] != ca[i]: + match = False + break + if match: + return True + return retval diff --git a/src/pyelliptic/eccblind.py b/src/pyelliptic/eccblind.py index 5b9b045e..6c85de25 100644 --- a/src/pyelliptic/eccblind.py +++ b/src/pyelliptic/eccblind.py @@ -10,9 +10,52 @@ http://www.isecure-journal.com/article_39171_47f9ec605dd3918c2793565ec21fcd7a.pd # variable names are based on the math in the paper, so they don't conform # to PEP8 +from hashlib import sha256 +import time + +try: + import msgpack +except ImportError: + try: + import umsgpack as msgpack + except ImportError: + import fallback.umsgpack.umsgpack as msgpack + from .openssl import OpenSSL +class Metadata(object): + """ + Pubkey metadata + """ + def __init__(self, exp=0, value=0): + self.exp = 0 + self.value = 0 + if exp: + self.exp = exp + if value: + self.value = value + + def serialize(self): + if self.exp or self.value: + return [self.exp, self.value] + else: + return [] + + @staticmethod + def deserialize(self, data): + exp, value = data + return Medatadata(exp, value) + + def verify(self, value): + if self.value and value > self.value: + return False + if self.exp: + if time.time() > self.exp: + return False + return True + + class ECCBlind(object): # pylint: disable=too-many-instance-attributes """ Class for ECC blind signature functionality @@ -75,7 +118,21 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes OpenSSL.EC_POINT_get_affine_coordinates_GFp(group, F, x0, y0, ctx) return x0 - def __init__(self, curve="secp256k1", pubkey=None): + @staticmethod + def deserialize(self, data): + pubkey_deserialized, meta = msgpack.unpackb(data) + if meta: + obj = ECCBlind(pubkey=pubkey_deserialized, metadata=meta) + else: + obj = ECCBlind(pubkey=pubkey_deserialized) + return obj + + def serialize(self): + data = (self.pubkey, self.metadata.serialize) + retval = msgpack.packb(data) + return retval + + def __init__(self, curve="secp256k1", pubkey=None, metadata=None): self.ctx = OpenSSL.BN_CTX_new() if pubkey: @@ -101,6 +158,14 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes self.iO = OpenSSL.EC_POINT_new(self.group) OpenSSL.EC_POINT_set_to_infinity(self.group, self.iO) + if metadata: + self._set_metadata(self, metadata) + else: + self.metadata = Metadata() + + def _set_metadata(self, metadata): + self.metadata = Metadata.deserialise(metadata) + def signer_init(self): """ Init signer @@ -119,6 +184,7 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes Requester creates a new signing request """ self.R = R + msghash = sha256(msg) # Requester: 3 random blinding factors self.F = OpenSSL.EC_POINT_new(self.group) @@ -151,7 +217,7 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes # Requester: Blinding (m' = br(m) + a) self.m = OpenSSL.BN_new() - OpenSSL.BN_bin2bn(msg, len(msg), self.m) + OpenSSL.BN_bin2bn(msghash, len(msghash), self.m) self.m_ = OpenSSL.BN_new() OpenSSL.BN_mod_mul(self.m_, self.b, self.r, self.n, self.ctx) @@ -180,14 +246,15 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes self.signature = (s, self.F) return self.signature - def verify(self, msg, signature): + def verify(self, msg, signature, value=0): """ Verify signature with certifier's pubkey """ # convert msg to BIGNUM self.m = OpenSSL.BN_new() - OpenSSL.BN_bin2bn(msg, len(msg), self.m) + msghash = sha256(msg) + OpenSSL.BN_bin2bn(msghash, len(msghash), self.m) # init s, self.F = signature @@ -206,5 +273,8 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes retval = OpenSSL.EC_POINT_cmp(self.group, lhs, rhs, self.ctx) if retval == -1: raise RuntimeError("EC_POINT_cmp returned an error") - else: - return retval == 0 + elif retval != 0: + return False + elif self.metadata: + return self.metadata.verify(value) + return True diff --git a/src/tests/test_blindsig.py b/src/tests/test_blindsig.py index b8d0248a..4760ad56 100644 --- a/src/tests/test_blindsig.py +++ b/src/tests/test_blindsig.py @@ -2,10 +2,12 @@ Test for ECC blind signatures """ import os +import time import unittest from ctypes import cast, c_char_p -from pybitmessage.pyelliptic.eccblind import ECCBlind +from pybitmessage.pyelliptic.eccblind import ECCBlind, Metadata +from pybitmessage.pyelliptic.eccblindchain import ECCBlindChain from pybitmessage.pyelliptic.openssl import OpenSSL @@ -50,3 +52,47 @@ class TestBlindSig(unittest.TestCase): # (5) Verification verifier_obj = ECCBlind(pubkey=signer_obj.pubkey) self.assertTrue(verifier_obj.verify(msg, signature)) + + # Serialization and deserialisation + pk = signer_obj.serialize() + pko = ECCBlind.deserialize(pk) + self.assertTrue(pko.verify(msg, signature)) + + def test_blind_sig_chain(self): + """Test blind signature chain using a random certifier key and a random message""" + + test_levels = 5 + value = 1 + msg = os.urandom(1024) + + chain = ECCBlindChain() + ca = ECCBlind() + signer_obj = ca + signer_pubkey = signer_obj.serialize() + + for level in range(test_levels): + if level == 0: + metadata = Metadata(exp=int(time.time()) + 100, + value=value).serialize() + requester_obj = ECCBlind(pubkey=signer_obj.pubkey, + metadata=metadata) + else: + requester_obj = ECCBlind(pubkey=signer_obj.pubkey) + point_r = signer_obj.signer_init() + + if level == test_levels - 1: + msg_blinded = requester_obj.create_signing_request(point_r, + msg) + else: + msg_blinded = requester.obj.create_signing_request(point_r, + signer_pubkey) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + chain.add_level(signer_obj.pubkey, + signer_obj.metadata.serialize, + signature) + signer_obj = requester_obj + signer_pubkey = requester_obj.serialize() + sigchain = chain.serialize() + verifychain = ECCBlindChain.deserialize(sigchain) + self.assertTrue(verifychain.verify(msg, value)) From ff1f451691c3c66fe15efa7a5e84d4b1f8978247 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Sun, 29 Mar 2020 20:51:55 +0800 Subject: [PATCH 02/28] Blind signature updates - added serializing and deserializing - added a signature chain class `ECCBlindSigChain` - added more tests --- src/pyelliptic/__init__.py | 2 + src/pyelliptic/blindchain.py | 77 -------- src/pyelliptic/eccblind.py | 312 +++++++++++++++++++++----------- src/pyelliptic/eccblindchain.py | 52 ++++++ src/pyelliptic/openssl.py | 82 ++++++++- src/tests/test_blindsig.py | 264 ++++++++++++++++++++++----- src/tests/test_openssl.py | 54 ++++++ 7 files changed, 607 insertions(+), 236 deletions(-) delete mode 100644 src/pyelliptic/blindchain.py create mode 100644 src/pyelliptic/eccblindchain.py create mode 100644 src/tests/test_openssl.py diff --git a/src/pyelliptic/__init__.py b/src/pyelliptic/__init__.py index dbc1b2af..cafa89c9 100644 --- a/src/pyelliptic/__init__.py +++ b/src/pyelliptic/__init__.py @@ -12,6 +12,7 @@ This is an abandoned package maintained inside of the PyBitmessage. from .cipher import Cipher from .ecc import ECC from .eccblind import ECCBlind +from .eccblindchain import ECCBlindChain from .hash import hmac_sha256, hmac_sha512, pbkdf2 from .openssl import OpenSSL @@ -21,6 +22,7 @@ __all__ = [ 'OpenSSL', 'ECC', 'ECCBlind', + 'ECCBlindChain', 'Cipher', 'hmac_sha256', 'hmac_sha512', diff --git a/src/pyelliptic/blindchain.py b/src/pyelliptic/blindchain.py deleted file mode 100644 index df1d3049..00000000 --- a/src/pyelliptic/blindchain.py +++ /dev/null @@ -1,77 +0,0 @@ -""" -Blind signature chain with a top level CA -""" - -from eccblind import ECCBlind - -try: - import msgpack -except ImportError: - try: - import umsgpack as msgpack - except ImportError: - import fallback.umsgpack.umsgpack as msgpack - -from eccblind import ECCBlind - -def encode_datetime(obj): - """ - Method to format time - """ - return {'Time': int(obj.strftime("%s"))} - - -class ECCBlindChain(object): # pylint: disable=too-many-instance-attributes - """ - # Class for ECC Blind Chain signature functionality - """ - chain = [] - ca = [] - - def __init__(self, chain=None): - if chain is not None: - self.chain = chain - - def serialize(self): - data = msgpack.packb(self.chain) - return data - - @staticmethod - def deserialize(self, chain): - """ - Deserialize the data using msgpack - """ - data = msgpack.unpackb(chain) - return ECCBlindChain(data) - - def add_level(self, pubkey, metadata, signature): - self.chain.append((pubkey, metadata, signature)) - - def add_ca(self, ca): - pubkey, metadata = ECCBlind.deserialize(ca) - self.ca.append(pubkey) - - def verify(self, msg, value): - lastpubkey = None - retval = False - for level in reversed(self.chain): - pubkey, metadata, signature = level - verifier_obj = ECCBlind(pubkey=pubkey, metadata) - if not lastpubkey: - retval = verifier_obj.verify(msg, signature, value) - else: - retval = verifier_obj.verify(lastpubkey, signature, value) - if not reval: - break - lastpubkey = pubkey - if retval: - retval = False - for ca in self.ca: - match = True - for i in range(4): - if lastpubkey[i] != ca[i]: - match = False - break - if match: - return True - return retval diff --git a/src/pyelliptic/eccblind.py b/src/pyelliptic/eccblind.py index 6c85de25..a417451e 100644 --- a/src/pyelliptic/eccblind.py +++ b/src/pyelliptic/eccblind.py @@ -10,50 +10,69 @@ http://www.isecure-journal.com/article_39171_47f9ec605dd3918c2793565ec21fcd7a.pd # variable names are based on the math in the paper, so they don't conform # to PEP8 -from hashlib import sha256 import time - -try: - import msgpack -except ImportError: - try: - import umsgpack as msgpack - except ImportError: - import fallback.umsgpack.umsgpack as msgpack +from hashlib import sha256 +from struct import pack, unpack from .openssl import OpenSSL +# first byte in serialisation can contain data +Y_BIT = 0x01 +COMPRESSED_BIT = 0x02 -class Metadata(object): - """ - Pubkey metadata - """ - def __init__(self, exp=0, value=0): - self.exp = 0 - self.value = 0 - if exp: - self.exp = exp - if value: - self.value = value +# formats +BIGNUM = '!32s' +EC = '!B32s' +PUBKEY = '!BB33s' + + +class Expiration(object): + """Expiration of pubkey""" + @staticmethod + def deserialize(val): + """Create an object out of int""" + year = ((val & 0xF0) >> 4) + 2020 + month = val & 0x0F + assert month < 12 + return Expiration(year, month) + + def __init__(self, year, month): + assert isinstance(year, int) + assert year > 2019 and year < 2036 + assert isinstance(month, int) + assert month < 12 + self.year = year + self.month = month + self.exp = year + month / 12.0 def serialize(self): - if self.exp or self.value: - return [self.exp, self.value] - else: - return [] + """Make int out of object""" + return ((self.year - 2020) << 4) + self.month + def verify(self): + """Check if the pubkey has expired""" + now = time.gmtime() + return self.exp >= now.tm_year + (now.tm_mon - 1) / 12.0 + + +class Value(object): + """Value of a pubkey""" @staticmethod - def deserialize(self, data): - exp, value = data - return Medatadata(exp, value) + def deserialize(val): + """Make object out of int""" + return Value(val) + + def __init__(self, value=0xFF): + assert isinstance(value, int) + self.value = value + + def serialize(self): + """Make int out of object""" + return self.value & 0xFF def verify(self, value): - if self.value and value > self.value: - return False - if self.exp: - if time.time() > self.exp: - return False - return True + """Verify against supplied value""" + return value <= self.value class ECCBlind(object): # pylint: disable=too-many-instance-attributes @@ -64,8 +83,8 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes # init k = None R = None - keypair = None F = None + d = None Q = None a = None b = None @@ -76,115 +95,183 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes m_ = None s_ = None signature = None + exp = None + val = None - @staticmethod - def ec_get_random(group, ctx): + def ec_get_random(self): """ - Random point from finite field + Random integer within the EC order """ - order = OpenSSL.BN_new() - OpenSSL.EC_GROUP_get_order(group, order, ctx) - OpenSSL.BN_rand(order, OpenSSL.BN_num_bits(order), 0, 0) - return order + randomnum = OpenSSL.BN_new() + OpenSSL.BN_rand(randomnum, OpenSSL.BN_num_bits(self.n), 0, 0) + return randomnum - @staticmethod - def ec_invert(group, a, ctx): + def ec_invert(self, a): """ ECC inversion """ - order = OpenSSL.BN_new() - OpenSSL.EC_GROUP_get_order(group, order, ctx) - inverse = OpenSSL.BN_mod_inverse(0, a, order, ctx) + inverse = OpenSSL.BN_mod_inverse(0, a, self.n, self.ctx) return inverse - @staticmethod - def ec_gen_keypair(group, ctx): + def ec_gen_keypair(self): """ Generate an ECC keypair + We're using compressed keys """ - d = ECCBlind.ec_get_random(group, ctx) - Q = OpenSSL.EC_POINT_new(group) - OpenSSL.EC_POINT_mul(group, Q, d, 0, 0, 0) + d = self.ec_get_random() + Q = OpenSSL.EC_POINT_new(self.group) + OpenSSL.EC_POINT_mul(self.group, Q, d, 0, 0, 0) return (d, Q) - @staticmethod - def ec_Ftor(F, group, ctx): + def ec_Ftor(self, F): """ x0 coordinate of F """ # F = (x0, y0) x0 = OpenSSL.BN_new() y0 = OpenSSL.BN_new() - OpenSSL.EC_POINT_get_affine_coordinates_GFp(group, F, x0, y0, ctx) + OpenSSL.EC_POINT_get_affine_coordinates(self.group, F, x0, y0, self.ctx) + OpenSSL.BN_free(y0) return x0 - @staticmethod - def deserialize(self, data): - pubkey_deserialized, meta = msgpack.unpackb(data) - if meta: - obj = ECCBlind(pubkey=pubkey_deserialized, metadata=meta) - else: - obj = ECCBlind(pubkey=pubkey_deserialized) - return obj + def _ec_point_serialize(self, point): + """Make an EC point into a string""" + try: + x = OpenSSL.BN_new() + y = OpenSSL.BN_new() + OpenSSL.EC_POINT_get_affine_coordinates( + self.group, point, x, y, 0) + y_byte = (OpenSSL.BN_is_odd(y) & Y_BIT) | COMPRESSED_BIT + l_ = OpenSSL.BN_num_bytes(self.n) + try: + bx = OpenSSL.malloc(0, l_) + OpenSSL.BN_bn2binpad(x, bx, l_) + out = bx.raw + except AttributeError: + # padding manually + bx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(x)) + OpenSSL.BN_bn2bin(x, bx) + out = bx.raw.rjust(l_, chr(0)) + return pack(EC, y_byte, out) - def serialize(self): - data = (self.pubkey, self.metadata.serialize) - retval = msgpack.packb(data) + finally: + OpenSSL.BN_clear_free(x) + OpenSSL.BN_clear_free(y) + + def _ec_point_deserialize(self, data): + """Make a string into an EC point""" + y_bit, x_raw = unpack(EC, data) + x = OpenSSL.BN_bin2bn(x_raw, OpenSSL.BN_num_bytes(self.n), 0) + y_bit &= Y_BIT + retval = OpenSSL.EC_POINT_new(self.group) + OpenSSL.EC_POINT_set_compressed_coordinates(self.group, + retval, + x, + y_bit, + self.ctx) return retval - def __init__(self, curve="secp256k1", pubkey=None, metadata=None): + def _bn_serialize(self, bn): + """Make a string out of BigNum""" + l_ = OpenSSL.BN_num_bytes(self.n) + try: + o = OpenSSL.malloc(0, l_) + OpenSSL.BN_bn2binpad(bn, o, l_) + return o.raw + except AttributeError: + o = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(bn)) + OpenSSL.BN_bn2bin(bn, o) + return o.raw.rjust(l_, chr(0)) + + def _bn_deserialize(self, data): + """Make a BigNum out of string""" + x = OpenSSL.BN_bin2bn(data, OpenSSL.BN_num_bytes(self.n), 0) + return x + + def _init_privkey(self, privkey): + """Initialise private key out of string/bytes""" + self.d = self._bn_deserialize(privkey) + + def privkey(self): + """Make a private key into a string""" + return pack(BIGNUM, self.d) + + def _init_pubkey(self, pubkey): + """Initialise pubkey out of string/bytes""" + unpacked = unpack(PUBKEY, pubkey) + self.expiration = Expiration.deserialize(unpacked[0]) + self.value = Value.deserialize(unpacked[1]) + self.Q = self._ec_point_deserialize(unpacked[2]) + + def pubkey(self): + """Make a pubkey into a string""" + return pack(PUBKEY, self.expiration.serialize(), + self.value.serialize(), + self._ec_point_serialize(self.Q)) + + def __init__(self, curve="secp256k1", pubkey=None, privkey=None, # pylint: disable=too-many-arguments + year=2025, month=11, value=0xFF): self.ctx = OpenSSL.BN_CTX_new() - if pubkey: - self.group, self.G, self.n, self.Q = pubkey - else: - self.group = OpenSSL.EC_GROUP_new_by_curve_name( - OpenSSL.get_curve(curve)) - # Order n - self.n = OpenSSL.BN_new() - OpenSSL.EC_GROUP_get_order(self.group, self.n, self.ctx) + # ECC group + self.group = OpenSSL.EC_GROUP_new_by_curve_name( + OpenSSL.get_curve(curve)) - # Generator G - self.G = OpenSSL.EC_GROUP_get0_generator(self.group) + # Order n + self.n = OpenSSL.BN_new() + OpenSSL.EC_GROUP_get_order(self.group, self.n, self.ctx) - # new keypair - self.keypair = ECCBlind.ec_gen_keypair(self.group, self.ctx) - - self.Q = self.keypair[1] - - self.pubkey = (self.group, self.G, self.n, self.Q) + # Generator G + self.G = OpenSSL.EC_GROUP_get0_generator(self.group) # Identity O (infinity) self.iO = OpenSSL.EC_POINT_new(self.group) OpenSSL.EC_POINT_set_to_infinity(self.group, self.iO) - if metadata: - self._set_metadata(self, metadata) - else: - self.metadata = Metadata() + if privkey: + assert pubkey + # load both pubkey and privkey from bytes + self._init_privkey(privkey) + self._init_pubkey(pubkey) + elif pubkey: + # load pubkey from bytes + self._init_pubkey(pubkey) + else: + # new keypair + self.d, self.Q = self.ec_gen_keypair() + if not year or not month: + now = time.gmtime() + if now.tm_mon == 12: + self.expiration = Expiration(now.tm_year + 1, 1) + else: + self.expiration = Expiration(now.tm_year, now.tm_mon + 1) + else: + self.expiration = Expiration(year, month) + self.value = Value(value) - def _set_metadata(self, metadata): - self.metadata = Metadata.deserialise(metadata) + def __del__(self): + OpenSSL.BN_free(self.n) + OpenSSL.BN_CTX_free(self.ctx) def signer_init(self): """ Init signer """ # Signer: Random integer k - self.k = ECCBlind.ec_get_random(self.group, self.ctx) + self.k = self.ec_get_random() # R = kG self.R = OpenSSL.EC_POINT_new(self.group) OpenSSL.EC_POINT_mul(self.group, self.R, self.k, 0, 0, 0) - return self.R + return self._ec_point_serialize(self.R) def create_signing_request(self, R, msg): """ Requester creates a new signing request """ - self.R = R - msghash = sha256(msg) + self.R = self._ec_point_deserialize(R) + msghash = sha256(msg).digest() # Requester: 3 random blinding factors self.F = OpenSSL.EC_POINT_new(self.group) @@ -194,12 +281,12 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes # F != O while OpenSSL.EC_POINT_cmp(self.group, self.F, self.iO, self.ctx) == 0: - self.a = ECCBlind.ec_get_random(self.group, self.ctx) - self.b = ECCBlind.ec_get_random(self.group, self.ctx) - self.c = ECCBlind.ec_get_random(self.group, self.ctx) + self.a = self.ec_get_random() + self.b = self.ec_get_random() + self.c = self.ec_get_random() # F = b^-1 * R... - self.binv = ECCBlind.ec_invert(self.group, self.b, self.ctx) + self.binv = self.ec_invert(self.b) OpenSSL.EC_POINT_mul(self.group, temp, 0, self.R, self.binv, 0) OpenSSL.EC_POINT_copy(self.F, temp) @@ -213,7 +300,7 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes OpenSSL.EC_POINT_add(self.group, self.F, self.F, temp, 0) # F = (x0, y0) - self.r = ECCBlind.ec_Ftor(self.F, self.group, self.ctx) + self.r = self.ec_Ftor(self.F) # Requester: Blinding (m' = br(m) + a) self.m = OpenSSL.BN_new() @@ -223,43 +310,48 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes OpenSSL.BN_mod_mul(self.m_, self.b, self.r, self.n, self.ctx) OpenSSL.BN_mod_mul(self.m_, self.m_, self.m, self.n, self.ctx) OpenSSL.BN_mod_add(self.m_, self.m_, self.a, self.n, self.ctx) - return self.m_ + return self._bn_serialize(self.m_) def blind_sign(self, m_): """ Signer blind-signs the request """ - self.m_ = m_ + self.m_ = self._bn_deserialize(m_) self.s_ = OpenSSL.BN_new() - OpenSSL.BN_mod_mul(self.s_, self.keypair[0], self.m_, self.n, self.ctx) + OpenSSL.BN_mod_mul(self.s_, self.d, self.m_, self.n, self.ctx) OpenSSL.BN_mod_add(self.s_, self.s_, self.k, self.n, self.ctx) - return self.s_ + OpenSSL.BN_free(self.k) + return self._bn_serialize(self.s_) def unblind(self, s_): """ Requester unblinds the signature """ - self.s_ = s_ + self.s_ = self._bn_deserialize(s_) s = OpenSSL.BN_new() OpenSSL.BN_mod_mul(s, self.binv, self.s_, self.n, self.ctx) OpenSSL.BN_mod_add(s, s, self.c, self.n, self.ctx) + OpenSSL.BN_free(self.a) + OpenSSL.BN_free(self.b) + OpenSSL.BN_free(self.c) self.signature = (s, self.F) - return self.signature + return self._bn_serialize(s) + self._ec_point_serialize(self.F) - def verify(self, msg, signature, value=0): + def verify(self, msg, signature, value=1): """ Verify signature with certifier's pubkey """ # convert msg to BIGNUM self.m = OpenSSL.BN_new() - msghash = sha256(msg) + msghash = sha256(msg).digest() OpenSSL.BN_bin2bn(msghash, len(msghash), self.m) # init - s, self.F = signature + s, self.F = (self._bn_deserialize(signature[0:32]), + self._ec_point_deserialize(signature[32:])) if self.r is None: - self.r = ECCBlind.ec_Ftor(self.F, self.group, self.ctx) + self.r = self.ec_Ftor(self.F) lhs = OpenSSL.EC_POINT_new(self.group) rhs = OpenSSL.EC_POINT_new(self.group) @@ -273,8 +365,10 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes retval = OpenSSL.EC_POINT_cmp(self.group, lhs, rhs, self.ctx) if retval == -1: raise RuntimeError("EC_POINT_cmp returned an error") + elif not self.value.verify(value): + return False + elif not self.expiration.verify(): + return False elif retval != 0: return False - elif self.metadata: - return self.metadata.verify(value) return True diff --git a/src/pyelliptic/eccblindchain.py b/src/pyelliptic/eccblindchain.py new file mode 100644 index 00000000..56e8ce2a --- /dev/null +++ b/src/pyelliptic/eccblindchain.py @@ -0,0 +1,52 @@ +""" +Blind signature chain with a top level CA +""" + +from .eccblind import ECCBlind + + +class ECCBlindChain(object): # pylint: disable=too-few-public-methods + """ + # Class for ECC Blind Chain signature functionality + """ + + def __init__(self, ca=None, chain=None): + self.chain = [] + self.ca = [] + if ca: + for i in range(0, len(ca), 35): + self.ca.append(ca[i:i + 35]) + if chain: + self.chain.append(chain[0:35]) + for i in range(35, len(chain), 100): + if len(chain[i:]) == 65: + self.chain.append(chain[i:i + 65]) + else: + self.chain.append(chain[i:i + 100]) + + def verify(self, msg, value): + """Verify a chain provides supplied message and value""" + parent = None + l_ = 0 + for level in self.chain: + l_ += 1 + pubkey = None + signature = None + if len(level) == 100: + pubkey, signature = (level[0:35], level[35:]) + elif len(level) == 35: + if level not in self.ca: + return False + parent = level + continue + else: + signature = level + verifier_obj = ECCBlind(pubkey=parent) + if pubkey: + if not verifier_obj.verify(pubkey, signature, value): + return False + parent = pubkey + else: + return verifier_obj.verify(msg=msg, signature=signature, + value=value) + return None diff --git a/src/pyelliptic/openssl.py b/src/pyelliptic/openssl.py index 4933ac44..17a8d6d1 100644 --- a/src/pyelliptic/openssl.py +++ b/src/pyelliptic/openssl.py @@ -8,6 +8,7 @@ needed openssl functionality in class _OpenSSL. """ import ctypes import sys + # pylint: disable=protected-access OpenSSL = None @@ -97,6 +98,10 @@ class _OpenSSL(object): self.BN_free.restype = None self.BN_free.argtypes = [ctypes.c_void_p] + self.BN_clear_free = self._lib.BN_clear_free + self.BN_clear_free.restype = None + self.BN_clear_free.argtypes = [ctypes.c_void_p] + self.BN_num_bits = self._lib.BN_num_bits self.BN_num_bits.restype = ctypes.c_int self.BN_num_bits.argtypes = [ctypes.c_void_p] @@ -105,6 +110,15 @@ class _OpenSSL(object): self.BN_bn2bin.restype = ctypes.c_int self.BN_bn2bin.argtypes = [ctypes.c_void_p, ctypes.c_void_p] + try: + self.BN_bn2binpad = self._lib.BN_bn2binpad + self.BN_bn2binpad.restype = ctypes.c_int + self.BN_bn2binpad.argtypes = [ctypes.c_void_p, ctypes.c_void_p, + ctypes.c_int] + except AttributeError: + # optional, we have a workaround + pass + self.BN_bin2bn = self._lib.BN_bin2bn self.BN_bin2bn.restype = ctypes.c_void_p self.BN_bin2bn.argtypes = [ctypes.c_void_p, ctypes.c_int, @@ -147,6 +161,20 @@ class _OpenSSL(object): ctypes.c_void_p, ctypes.c_void_p] + try: + self.EC_POINT_get_affine_coordinates = \ + self._lib.EC_POINT_get_affine_coordinates + except AttributeError: + # OpenSSL docs say only use this for backwards compatibility + self.EC_POINT_get_affine_coordinates = \ + self._lib.EC_POINT_get_affine_coordinates_GF2m + self.EC_POINT_get_affine_coordinates.restype = ctypes.c_int + self.EC_POINT_get_affine_coordinates.argtypes = [ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p] + self.EC_KEY_set_private_key = self._lib.EC_KEY_set_private_key self.EC_KEY_set_private_key.restype = ctypes.c_int self.EC_KEY_set_private_key.argtypes = [ctypes.c_void_p, @@ -171,6 +199,34 @@ class _OpenSSL(object): ctypes.c_void_p, ctypes.c_void_p] + try: + self.EC_POINT_set_affine_coordinates = \ + self._lib.EC_POINT_set_affine_coordinates + except AttributeError: + # OpenSSL docs say only use this for backwards compatibility + self.EC_POINT_set_affine_coordinates = \ + self._lib.EC_POINT_set_affine_coordinates_GF2m + self.EC_POINT_set_affine_coordinates.restype = ctypes.c_int + self.EC_POINT_set_affine_coordinates.argtypes = [ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p] + + try: + self.EC_POINT_set_compressed_coordinates = \ + self._lib.EC_POINT_set_compressed_coordinates + except AttributeError: + # OpenSSL docs say only use this for backwards compatibility + self.EC_POINT_set_compressed_coordinates = \ + self._lib.EC_POINT_set_compressed_coordinates_GF2m + self.EC_POINT_set_compressed_coordinates.restype = ctypes.c_int + self.EC_POINT_set_compressed_coordinates.argtypes = [ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_int, + ctypes.c_void_p] + self.EC_POINT_new = self._lib.EC_POINT_new self.EC_POINT_new.restype = ctypes.c_void_p self.EC_POINT_new.argtypes = [ctypes.c_void_p] @@ -215,10 +271,6 @@ class _OpenSSL(object): self._lib.ECDH_set_method.argtypes = [ctypes.c_void_p, ctypes.c_void_p] - self.BN_CTX_new = self._lib.BN_CTX_new - self._lib.BN_CTX_new.restype = ctypes.c_void_p - self._lib.BN_CTX_new.argtypes = [] - self.ECDH_compute_key = self._lib.ECDH_compute_key self.ECDH_compute_key.restype = ctypes.c_int self.ECDH_compute_key.argtypes = [ctypes.c_void_p, @@ -477,13 +529,19 @@ class _OpenSSL(object): self.BN_cmp.argtypes = [ctypes.c_void_p, ctypes.c_void_p] + try: + self.BN_is_odd = self._lib.BN_is_odd + self.BN_is_odd.restype = ctypes.c_int + self.BN_is_odd.argtypes = [ctypes.c_void_p] + except AttributeError: + # OpenSSL 1.1.0 implements this as a function, but earlier + # versions as macro, so we need to workaround + self.BN_is_odd = self.BN_is_odd_compatible + self.BN_bn2dec = self._lib.BN_bn2dec self.BN_bn2dec.restype = ctypes.c_char_p self.BN_bn2dec.argtypes = [ctypes.c_void_p] - self.BN_CTX_free = self._lib.BN_CTX_free - self.BN_CTX_free.argtypes = [ctypes.c_void_p] - self.EC_GROUP_new_by_curve_name = self._lib.EC_GROUP_new_by_curve_name self.EC_GROUP_new_by_curve_name.restype = ctypes.c_void_p self.EC_GROUP_new_by_curve_name.argtypes = [ctypes.c_int] @@ -600,6 +658,16 @@ class _OpenSSL(object): """ return int((self.BN_num_bits(x) + 7) / 8) + def BN_is_odd_compatible(self, x): + """ + returns if BN is odd + we assume big endianness, and that BN is initialised + """ + length = self.BN_num_bytes(x) + data = self.malloc(0, length) + OpenSSL.BN_bn2bin(x, data) + return ord(data[length - 1]) & 1 + def get_cipher(self, name): """ returns the OpenSSL cipher instance diff --git a/src/tests/test_blindsig.py b/src/tests/test_blindsig.py index 4760ad56..cae16191 100644 --- a/src/tests/test_blindsig.py +++ b/src/tests/test_blindsig.py @@ -2,14 +2,15 @@ Test for ECC blind signatures """ import os -import time import unittest -from ctypes import cast, c_char_p +from hashlib import sha256 -from pybitmessage.pyelliptic.eccblind import ECCBlind, Metadata +from pybitmessage.pyelliptic.eccblind import ECCBlind from pybitmessage.pyelliptic.eccblindchain import ECCBlindChain from pybitmessage.pyelliptic.openssl import OpenSSL +# pylint: disable=protected-access + class TestBlindSig(unittest.TestCase): """ @@ -21,78 +22,255 @@ class TestBlindSig(unittest.TestCase): # (1) Initialization signer_obj = ECCBlind() point_r = signer_obj.signer_init() + self.assertEqual(len(signer_obj.pubkey()), 35) # (2) Request - requester_obj = ECCBlind(pubkey=signer_obj.pubkey) + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) # only 64 byte messages are planned to be used in Bitmessage msg = os.urandom(64) msg_blinded = requester_obj.create_signing_request(point_r, msg) + self.assertEqual(len(msg_blinded), 32) # check - msg_blinded_str = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(msg_blinded)) - OpenSSL.BN_bn2bin(msg_blinded, msg_blinded_str) - self.assertNotEqual(msg, cast(msg_blinded_str, c_char_p).value) + self.assertNotEqual(sha256(msg).digest(), msg_blinded) # (3) Signature Generation signature_blinded = signer_obj.blind_sign(msg_blinded) + assert isinstance(signature_blinded, str) + self.assertEqual(len(signature_blinded), 32) # (4) Extraction signature = requester_obj.unblind(signature_blinded) + assert isinstance(signature, str) + self.assertEqual(len(signature), 65) - # check - signature_blinded_str = OpenSSL.malloc(0, - OpenSSL.BN_num_bytes( - signature_blinded)) - signature_str = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(signature[0])) - OpenSSL.BN_bn2bin(signature_blinded, signature_blinded_str) - OpenSSL.BN_bn2bin(signature[0], signature_str) - self.assertNotEqual(cast(signature_str, c_char_p).value, - cast(signature_blinded_str, c_char_p).value) + self.assertNotEqual(signature, signature_blinded) # (5) Verification - verifier_obj = ECCBlind(pubkey=signer_obj.pubkey) + verifier_obj = ECCBlind(pubkey=signer_obj.pubkey()) self.assertTrue(verifier_obj.verify(msg, signature)) - # Serialization and deserialisation - pk = signer_obj.serialize() - pko = ECCBlind.deserialize(pk) - self.assertTrue(pko.verify(msg, signature)) + def test_is_odd(self): + """Test our implementation of BN_is_odd""" + for _ in range(1024): + obj = ECCBlind() + x = OpenSSL.BN_new() + y = OpenSSL.BN_new() + OpenSSL.EC_POINT_get_affine_coordinates( + obj.group, obj.Q, x, y, 0) + self.assertEqual(OpenSSL.BN_is_odd(y), + OpenSSL.BN_is_odd_compatible(y)) - def test_blind_sig_chain(self): + def test_serialize_ec_point(self): + """Test EC point serialization/deserialization""" + for _ in range(1024): + try: + obj = ECCBlind() + obj2 = ECCBlind() + randompoint = obj.Q + serialized = obj._ec_point_serialize(randompoint) + secondpoint = obj2._ec_point_deserialize(serialized) + x0 = OpenSSL.BN_new() + y0 = OpenSSL.BN_new() + OpenSSL.EC_POINT_get_affine_coordinates(obj.group, + randompoint, x0, + y0, obj.ctx) + x1 = OpenSSL.BN_new() + y1 = OpenSSL.BN_new() + OpenSSL.EC_POINT_get_affine_coordinates(obj2.group, + secondpoint, x1, + y1, obj2.ctx) + + self.assertEqual(OpenSSL.BN_cmp(y0, y1), 0) + self.assertEqual(OpenSSL.BN_cmp(x0, x1), 0) + self.assertEqual(OpenSSL.EC_POINT_cmp(obj.group, randompoint, + secondpoint, 0), 0) + finally: + OpenSSL.BN_free(x0) + OpenSSL.BN_free(x1) + OpenSSL.BN_free(y0) + OpenSSL.BN_free(y1) + del obj + del obj2 + + def test_serialize_bn(self): + """Test Bignum serialization/deserialization""" + for _ in range(1024): + obj = ECCBlind() + obj2 = ECCBlind() + randomnum = obj.d + serialized = obj._bn_serialize(randomnum) + secondnum = obj2._bn_deserialize(serialized) + self.assertEqual(OpenSSL.BN_cmp(randomnum, secondnum), 0) + + def test_blind_sig_many(self): + """Test a lot of blind signatures""" + for _ in range(1024): + self.test_blind_sig() + + def test_blind_sig_value(self): + """Test blind signature value checking""" + signer_obj = ECCBlind(value=5) + point_r = signer_obj.signer_init() + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + msg = os.urandom(64) + msg_blinded = requester_obj.create_signing_request(point_r, msg) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + verifier_obj = ECCBlind(pubkey=signer_obj.pubkey()) + self.assertFalse(verifier_obj.verify(msg, signature, value=8)) + + def test_blind_sig_expiration(self): + """Test blind signature expiration checking""" + signer_obj = ECCBlind(year=2020, month=1) + point_r = signer_obj.signer_init() + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + msg = os.urandom(64) + msg_blinded = requester_obj.create_signing_request(point_r, msg) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + verifier_obj = ECCBlind(pubkey=signer_obj.pubkey()) + self.assertFalse(verifier_obj.verify(msg, signature)) + + def test_blind_sig_chain(self): # pylint: disable=too-many-locals """Test blind signature chain using a random certifier key and a random message""" - test_levels = 5 - value = 1 + test_levels = 4 msg = os.urandom(1024) - chain = ECCBlindChain() ca = ECCBlind() signer_obj = ca - signer_pubkey = signer_obj.serialize() + + output = bytearray() for level in range(test_levels): - if level == 0: - metadata = Metadata(exp=int(time.time()) + 100, - value=value).serialize() - requester_obj = ECCBlind(pubkey=signer_obj.pubkey, - metadata=metadata) - else: - requester_obj = ECCBlind(pubkey=signer_obj.pubkey) + if not level: + output.extend(ca.pubkey()) + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + child_obj = ECCBlind() point_r = signer_obj.signer_init() + pubkey = child_obj.pubkey() if level == test_levels - 1: msg_blinded = requester_obj.create_signing_request(point_r, msg) else: - msg_blinded = requester.obj.create_signing_request(point_r, - signer_pubkey) + msg_blinded = requester_obj.create_signing_request(point_r, + pubkey) signature_blinded = signer_obj.blind_sign(msg_blinded) signature = requester_obj.unblind(signature_blinded) - chain.add_level(signer_obj.pubkey, - signer_obj.metadata.serialize, - signature) - signer_obj = requester_obj - signer_pubkey = requester_obj.serialize() - sigchain = chain.serialize() - verifychain = ECCBlindChain.deserialize(sigchain) - self.assertTrue(verifychain.verify(msg, value)) + if level != test_levels - 1: + output.extend(pubkey) + output.extend(signature) + signer_obj = child_obj + verifychain = ECCBlindChain(ca=ca.pubkey(), chain=str(output)) + self.assertTrue(verifychain.verify(msg=msg, value=1)) + + def test_blind_sig_chain_wrong_ca(self): # pylint: disable=too-many-locals + """Test blind signature chain with an unlisted ca""" + + test_levels = 4 + msg = os.urandom(1024) + + ca = ECCBlind() + fake_ca = ECCBlind() + signer_obj = fake_ca + + output = bytearray() + + for level in range(test_levels): + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + child_obj = ECCBlind() + if not level: + # unlisted CA, but a syntactically valid pubkey + output.extend(fake_ca.pubkey()) + point_r = signer_obj.signer_init() + pubkey = child_obj.pubkey() + + if level == test_levels - 1: + msg_blinded = requester_obj.create_signing_request(point_r, + msg) + else: + msg_blinded = requester_obj.create_signing_request(point_r, + pubkey) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + if level != test_levels - 1: + output.extend(pubkey) + output.extend(signature) + signer_obj = child_obj + verifychain = ECCBlindChain(ca=ca.pubkey(), chain=str(output)) + self.assertFalse(verifychain.verify(msg, 1)) + + def test_blind_sig_chain_wrong_msg(self): # pylint: disable=too-many-locals + """Test blind signature chain with a fake message""" + + test_levels = 4 + msg = os.urandom(1024) + fake_msg = os.urandom(1024) + + ca = ECCBlind() + signer_obj = ca + + output = bytearray() + + for level in range(test_levels): + if not level: + output.extend(ca.pubkey()) + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + child_obj = ECCBlind() + point_r = signer_obj.signer_init() + pubkey = child_obj.pubkey() + + if level == test_levels - 1: + msg_blinded = requester_obj.create_signing_request(point_r, + msg) + else: + msg_blinded = requester_obj.create_signing_request(point_r, + pubkey) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + if level != test_levels - 1: + output.extend(pubkey) + output.extend(signature) + signer_obj = child_obj + verifychain = ECCBlindChain(ca=ca.pubkey(), chain=str(output)) + self.assertFalse(verifychain.verify(fake_msg, 1)) + + def test_blind_sig_chain_wrong_intermediary(self): # pylint: disable=too-many-locals + """Test blind signature chain using a fake intermediary pubkey""" + + test_levels = 4 + msg = os.urandom(1024) + wrong_level = 2 + + ca = ECCBlind() + signer_obj = ca + fake_intermediary = ECCBlind() + + output = bytearray() + + for level in range(test_levels): + if not level: + output.extend(ca.pubkey()) + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + child_obj = ECCBlind() + point_r = signer_obj.signer_init() + pubkey = child_obj.pubkey() + + if level == test_levels - 1: + msg_blinded = requester_obj.create_signing_request(point_r, + msg) + else: + msg_blinded = requester_obj.create_signing_request(point_r, + pubkey) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + if level == wrong_level: + output.extend(fake_intermediary.pubkey()) + elif level != test_levels - 1: + output.extend(pubkey) + output.extend(signature) + signer_obj = child_obj + verifychain = ECCBlindChain(ca=ca.pubkey(), chain=str(output)) + self.assertFalse(verifychain.verify(msg, 1)) diff --git a/src/tests/test_openssl.py b/src/tests/test_openssl.py new file mode 100644 index 00000000..e947fff3 --- /dev/null +++ b/src/tests/test_openssl.py @@ -0,0 +1,54 @@ +""" +Test if OpenSSL is working correctly +""" +import unittest + +from pybitmessage.pyelliptic.openssl import OpenSSL + +try: + OpenSSL.BN_bn2binpad + have_pad = True +except AttributeError: + have_pad = None + + +class TestOpenSSL(unittest.TestCase): + """ + Test cases for OpenSSL + """ + def test_is_odd(self): + """Test BN_is_odd implementation""" + ctx = OpenSSL.BN_CTX_new() + a = OpenSSL.BN_new() + group = OpenSSL.EC_GROUP_new_by_curve_name( + OpenSSL.get_curve("secp256k1")) + OpenSSL.EC_GROUP_get_order(group, a, ctx) + + bad = 0 + for _ in range(1024): + OpenSSL.BN_rand(a, OpenSSL.BN_num_bits(a), 0, 0) + if not OpenSSL.BN_is_odd(a) == OpenSSL.BN_is_odd_compatible(a): + bad += 1 + self.assertEqual(bad, 0) + + @unittest.skipUnless(have_pad, 'Skipping OpenSSL pad test') + def test_padding(self): + """Test an alternatie implementation of bn2binpad""" + + ctx = OpenSSL.BN_CTX_new() + a = OpenSSL.BN_new() + n = OpenSSL.BN_new() + group = OpenSSL.EC_GROUP_new_by_curve_name( + OpenSSL.get_curve("secp256k1")) + OpenSSL.EC_GROUP_get_order(group, n, ctx) + + bad = 0 + for _ in range(1024): + OpenSSL.BN_rand(a, OpenSSL.BN_num_bits(n), 0, 0) + b = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(n)) + c = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(a)) + OpenSSL.BN_bn2binpad(a, b, OpenSSL.BN_num_bytes(n)) + OpenSSL.BN_bn2bin(a, c) + if b.raw != c.raw.rjust(OpenSSL.BN_num_bytes(n), chr(0)): + bad += 1 + self.assertEqual(bad, 0) From 31fc899060824cc1fa31dcaeb74316086a473117 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 28 Apr 2020 18:31:44 +0300 Subject: [PATCH 03/28] Protect stopresending* settings from being overriden by zeroes when lineEditDays and lineEditMonths is blank. Fixes: #1558. --- src/bitmessageqt/settings.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/bitmessageqt/settings.py b/src/bitmessageqt/settings.py index 011d38ed..bab27fbb 100644 --- a/src/bitmessageqt/settings.py +++ b/src/bitmessageqt/settings.py @@ -473,6 +473,8 @@ class SettingsDialog(QtGui.QDialog): " WHERE status='toodifficult'") queues.workerQueue.put(('sendmessage', '')) + stopResendingDefaults = False + # UI setting to stop trying to send messages after X days/months # I'm open to changing this UI to something else if someone has a better idea. if self.lineEditDays.text() == '' and self.lineEditMonths.text() == '': @@ -481,6 +483,7 @@ class SettingsDialog(QtGui.QDialog): self.config.set('bitmessagesettings', 'stopresendingafterxdays', '') self.config.set('bitmessagesettings', 'stopresendingafterxmonths', '') shared.maximumLengthOfTimeToBotherResendingMessages = float('inf') + stopResendingDefaults = True try: days = float(self.lineEditDays.text()) @@ -493,7 +496,7 @@ class SettingsDialog(QtGui.QDialog): self.lineEditMonths.setText("0") months = 0.0 - if days >= 0 and months >= 0: + if days >= 0 and months >= 0 and not stopResendingDefaults: shared.maximumLengthOfTimeToBotherResendingMessages = \ days * 24 * 60 * 60 + months * 60 * 60 * 24 * 365 / 12 if shared.maximumLengthOfTimeToBotherResendingMessages < 432000: From 7fd6200fb14cc287185c33bbeb4e3ac225403b8b Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Mon, 27 Apr 2020 09:41:49 +0800 Subject: [PATCH 04/28] Wine build patch for OpenCL frozen mode - PyOpenCL needs to be patched to work in frozen mode --- buildscripts/winbuild.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buildscripts/winbuild.sh b/buildscripts/winbuild.sh index 415d91f2..da5997bd 100755 --- a/buildscripts/winbuild.sh +++ b/buildscripts/winbuild.sh @@ -124,9 +124,9 @@ function install_pyopencl() else wine python -m pip install pyopencl-2015.1-cp27-none-win32.whl fi + sed -Ei 's/_DEFAULT_INCLUDE_OPTIONS = .*/_DEFAULT_INCLUDE_OPTIONS = [] /' $WINEPREFIX/drive_c/Python27/Lib/site-packages/pyopencl/__init__.py } - function build_dll(){ cd ${BASE_DIR} cd src/bitmsghash From 185ad66ea5556e035cffe5a3c2e55a748d8dcab8 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 5 Nov 2019 18:54:04 +0200 Subject: [PATCH 05/28] Moved most of variables from shared elsewhere (mostly to state) --- src/api.py | 17 +++++++++-------- src/bitmessagecurses/__init__.py | 7 ++++--- src/bitmessagemain.py | 23 +++++++++++------------ src/bitmessageqt/__init__.py | 18 ++++++++---------- src/bitmessageqt/networkstatus.py | 12 ++++++------ src/bitmessageqt/settings.py | 11 +++++------ src/class_objectProcessor.py | 10 +++++----- src/class_singleCleaner.py | 21 ++++++++++++++------- src/class_singleWorker.py | 12 ++++++------ src/network/tcp.py | 8 +++++--- src/shared.py | 27 --------------------------- src/shutdown.py | 5 ++--- src/state.py | 20 ++++++++++++++++++++ src/threads.py | 4 +++- 14 files changed, 98 insertions(+), 97 deletions(-) diff --git a/src/api.py b/src/api.py index 70da0cda..446c6ae4 100644 --- a/src/api.py +++ b/src/api.py @@ -27,6 +27,7 @@ import queues import shared import shutdown import state +import threads from addresses import ( addBMIfNotPresent, calculateInventoryHash, @@ -1206,7 +1207,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): len(encryptedPayload) + requiredPayloadLengthExtraBytes + 8 ) * requiredAverageProofOfWorkNonceTrialsPerByte ) - with shared.printLock: + with threads.printLock: print( '(For msg message via API) Doing proof of work.' 'Total required difficulty:', @@ -1221,7 +1222,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): powStartTime = time.time() initialHash = hashlib.sha512(encryptedPayload).digest() trialValue, nonce = proofofwork.run(target, initialHash) - with shared.printLock: + with threads.printLock: print '(For msg message via API) Found proof of work', trialValue, 'Nonce:', nonce try: print( @@ -1240,7 +1241,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): objectType, toStreamNumber, encryptedPayload, int(time.time()) + TTL, '' ) - with shared.printLock: + with threads.printLock: print 'Broadcasting inv for msg(API disseminatePreEncryptedMsg command):', hexlify(inventoryHash) queues.invQueue.put((toStreamNumber, inventoryHash)) @@ -1294,7 +1295,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): Inventory()[inventoryHash] = ( objectType, pubkeyStreamNumber, payload, int(time.time()) + TTL, '' ) - with shared.printLock: + with threads.printLock: print 'broadcasting inv within API command disseminatePubkey with hash:', hexlify(inventoryHash) queues.invQueue.put((pubkeyStreamNumber, inventoryHash)) @@ -1347,15 +1348,15 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): connections_num = len(network.stats.connectedHostsList()) if connections_num == 0: networkStatus = 'notConnected' - elif shared.clientHasReceivedIncomingConnections: + elif state.clientHasReceivedIncomingConnections: networkStatus = 'connectedAndReceivingIncomingConnections' else: networkStatus = 'connectedButHaveNotReceivedIncomingConnections' return json.dumps({ 'networkConnections': connections_num, - 'numberOfMessagesProcessed': shared.numberOfMessagesProcessed, - 'numberOfBroadcastsProcessed': shared.numberOfBroadcastsProcessed, - 'numberOfPubkeysProcessed': shared.numberOfPubkeysProcessed, + 'numberOfMessagesProcessed': state.numberOfMessagesProcessed, + 'numberOfBroadcastsProcessed': state.numberOfBroadcastsProcessed, + 'numberOfPubkeysProcessed': state.numberOfPubkeysProcessed, 'networkStatus': networkStatus, 'softwareName': 'PyBitmessage', 'softwareVersion': softwareVersion diff --git a/src/bitmessagecurses/__init__.py b/src/bitmessagecurses/__init__.py index d8daeef7..17dba22b 100644 --- a/src/bitmessagecurses/__init__.py +++ b/src/bitmessagecurses/__init__.py @@ -24,6 +24,7 @@ import network.stats import queues import shared import shutdown +import state from addresses import addBMIfNotPresent, decodeAddress from bmconfigparser import BMConfigParser @@ -274,11 +275,11 @@ def drawtab(stdscr): # Uptime and processing data stdscr.addstr(6, 35, "Since startup on " + l10n.formatTimestamp(startuptime, False)) stdscr.addstr(7, 40, "Processed " + str( - shared.numberOfMessagesProcessed).ljust(4) + " person-to-person messages.") + state.numberOfMessagesProcessed).ljust(4) + " person-to-person messages.") stdscr.addstr(8, 40, "Processed " + str( - shared.numberOfBroadcastsProcessed).ljust(4) + " broadcast messages.") + state.numberOfBroadcastsProcessed).ljust(4) + " broadcast messages.") stdscr.addstr(9, 40, "Processed " + str( - shared.numberOfPubkeysProcessed).ljust(4) + " public keys.") + state.numberOfPubkeysProcessed).ljust(4) + " public keys.") # Inventory data stdscr.addstr(11, 35, "Inventory lookups per second: " + str(inventorydata).ljust(3)) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index d6cb289b..d9a7c7f5 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -51,9 +51,8 @@ from network import ( from singleinstance import singleinstance # Synchronous threads from threads import ( - set_thread_name, addressGenerator, objectProcessor, singleCleaner, - singleWorker, sqlThread -) + set_thread_name, printLock, + addressGenerator, objectProcessor, singleCleaner, singleWorker, sqlThread) def connectToStream(streamNumber): @@ -157,7 +156,7 @@ def signal_handler(signum, frame): logger.error("Got signal %i", signum) # there are possible non-UI variants to run bitmessage # which should shutdown especially test-mode - if shared.thisapp.daemon or not state.enableGUI: + if state.thisapp.daemon or not state.enableGUI: shutdown.doCleanShutdown() else: print('# Thread: %s(%d)' % (thread.name, thread.ident)) @@ -233,10 +232,10 @@ class Main(object): ' \'-c\' as a commandline argument.' ) # is the application already running? If yes then exit. - shared.thisapp = singleinstance("", daemon) + state.thisapp = singleinstance("", daemon) if daemon: - with shared.printLock: + with printLock: print('Running as a daemon. Send TERM signal to end.') self.daemonize() @@ -413,7 +412,7 @@ class Main(object): try: if os.fork(): # unlock - shared.thisapp.cleanup() + state.thisapp.cleanup() # wait until grandchild ready while True: time.sleep(1) @@ -423,7 +422,7 @@ class Main(object): pass else: parentPid = os.getpid() - shared.thisapp.lock() # relock + state.thisapp.lock() # relock os.umask(0) try: @@ -434,7 +433,7 @@ class Main(object): try: if os.fork(): # unlock - shared.thisapp.cleanup() + state.thisapp.cleanup() # wait until child ready while True: time.sleep(1) @@ -443,8 +442,8 @@ class Main(object): # fork not implemented pass else: - shared.thisapp.lock() # relock - shared.thisapp.lockPid = None # indicate we're the final child + state.thisapp.lock() # relock + state.thisapp.lockPid = None # indicate we're the final child sys.stdout.flush() sys.stderr.flush() if not sys.platform.startswith('win'): @@ -483,7 +482,7 @@ All parameters are optional. @staticmethod def stop(): """Stop main application""" - with shared.printLock: + with printLock: print('Stopping Bitmessage Deamon.') shutdown.doCleanShutdown() diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 440d36b2..88d41b7f 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -17,10 +17,11 @@ from sqlite3 import register_adapter from PyQt4 import QtCore, QtGui from PyQt4.QtNetwork import QLocalSocket, QLocalServer +import shared +import state from debug import logger from tr import _translate from addresses import decodeAddress, addBMIfNotPresent -import shared from bitmessageui import Ui_MainWindow from bmconfigparser import BMConfigParser import namecoin @@ -730,9 +731,6 @@ class MyForm(settingsmixin.SMainWindow): QtCore.QObject.connect(self.pushButtonStatusIcon, QtCore.SIGNAL( "clicked()"), self.click_pushButtonStatusIcon) - self.numberOfMessagesProcessed = 0 - self.numberOfBroadcastsProcessed = 0 - self.numberOfPubkeysProcessed = 0 self.unreadCount = 0 # Set the icon sizes for the identicons @@ -1668,7 +1666,7 @@ class MyForm(settingsmixin.SMainWindow): if color == 'red': self.pushButtonStatusIcon.setIcon( QtGui.QIcon(":/newPrefix/images/redicon.png")) - shared.statusIconColor = 'red' + state.statusIconColor = 'red' # if the connection is lost then show a notification if self.connected and _notifications_enabled: self.notifierShow( @@ -1694,7 +1692,7 @@ class MyForm(settingsmixin.SMainWindow): self.statusbar.clearMessage() self.pushButtonStatusIcon.setIcon( QtGui.QIcon(":/newPrefix/images/yellowicon.png")) - shared.statusIconColor = 'yellow' + state.statusIconColor = 'yellow' # if a new connection has been established then show a notification if not self.connected and _notifications_enabled: self.notifierShow( @@ -1712,7 +1710,7 @@ class MyForm(settingsmixin.SMainWindow): self.statusbar.clearMessage() self.pushButtonStatusIcon.setIcon( QtGui.QIcon(":/newPrefix/images/greenicon.png")) - shared.statusIconColor = 'green' + state.statusIconColor = 'green' if not self.connected and _notifications_enabled: self.notifierShow( 'Bitmessage', @@ -2119,7 +2117,7 @@ class MyForm(settingsmixin.SMainWindow): "MainWindow", "Concerning the address %1, Bitmessage cannot handle stream numbers of %2. Perhaps upgrade Bitmessage to the latest version.").arg(toAddress).arg(str(streamNumber))) continue self.statusbar.clearMessage() - if shared.statusIconColor == 'red': + if state.statusIconColor == 'red': self.updateStatusBar(_translate( "MainWindow", "Warning: You are currently not connected." @@ -2632,7 +2630,7 @@ class MyForm(settingsmixin.SMainWindow): elif reply == QtGui.QMessageBox.Cancel: return - if shared.statusIconColor == 'red' and not BMConfigParser().safeGetBoolean( + if state.statusIconColor == 'red' and not BMConfigParser().safeGetBoolean( 'bitmessagesettings', 'dontconnect'): reply = QtGui.QMessageBox.question( self, _translate("MainWindow", "Not connected"), @@ -2660,7 +2658,7 @@ class MyForm(settingsmixin.SMainWindow): if waitForConnection: self.updateStatusBar(_translate( "MainWindow", "Waiting for network connection...")) - while shared.statusIconColor == 'red': + while state.statusIconColor == 'red': time.sleep(0.5) QtCore.QCoreApplication.processEvents( QtCore.QEventLoop.AllEvents, 1000 diff --git a/src/bitmessageqt/networkstatus.py b/src/bitmessageqt/networkstatus.py index 6fbf5df6..59b97f77 100644 --- a/src/bitmessageqt/networkstatus.py +++ b/src/bitmessageqt/networkstatus.py @@ -11,7 +11,7 @@ from PyQt4 import QtCore, QtGui import knownnodes import l10n import network.stats -import shared +import state import widgets from inventory import Inventory from network import BMConnectionPool @@ -108,7 +108,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): "Processed %n person-to-person message(s).", None, QtCore.QCoreApplication.CodecForTr, - shared.numberOfMessagesProcessed)) + state.numberOfMessagesProcessed)) def updateNumberOfBroadcastsProcessed(self): """Update the counter for the number of processed broadcasts""" @@ -119,7 +119,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): "Processed %n broadcast message(s).", None, QtCore.QCoreApplication.CodecForTr, - shared.numberOfBroadcastsProcessed)) + state.numberOfBroadcastsProcessed)) def updateNumberOfPubkeysProcessed(self): """Update the counter for the number of processed pubkeys""" @@ -130,7 +130,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): "Processed %n public key(s).", None, QtCore.QCoreApplication.CodecForTr, - shared.numberOfPubkeysProcessed)) + state.numberOfPubkeysProcessed)) def updateNumberOfBytes(self): """ @@ -225,9 +225,9 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): # FYI: The 'singlelistener' thread sets the icon color to green when it # receives an incoming connection, meaning that the user's firewall is # configured correctly. - if self.tableWidgetConnectionCount.rowCount() and shared.statusIconColor == 'red': + if self.tableWidgetConnectionCount.rowCount() and state.statusIconColor == 'red': self.window().setStatusIcon('yellow') - elif self.tableWidgetConnectionCount.rowCount() == 0 and shared.statusIconColor != "red": + elif self.tableWidgetConnectionCount.rowCount() == 0 and state.statusIconColor != "red": self.window().setStatusIcon('red') # timer driven diff --git a/src/bitmessageqt/settings.py b/src/bitmessageqt/settings.py index bab27fbb..1f650fe7 100644 --- a/src/bitmessageqt/settings.py +++ b/src/bitmessageqt/settings.py @@ -11,7 +11,6 @@ import namecoin import openclpow import paths import queues -import shared import state import tempfile import widgets @@ -338,7 +337,7 @@ class SettingsDialog(QtGui.QDialog): proxytype_index = self.comboBoxProxyType.currentIndex() if proxytype_index == 0: - if self._proxy_type and shared.statusIconColor != 'red': + if self._proxy_type and state.statusIconColor != 'red': self.net_restart_needed = True elif self.comboBoxProxyType.currentText() != self._proxy_type: self.net_restart_needed = True @@ -482,7 +481,7 @@ class SettingsDialog(QtGui.QDialog): # default behavior. The input is blank/blank self.config.set('bitmessagesettings', 'stopresendingafterxdays', '') self.config.set('bitmessagesettings', 'stopresendingafterxmonths', '') - shared.maximumLengthOfTimeToBotherResendingMessages = float('inf') + state.maximumLengthOfTimeToBotherResendingMessages = float('inf') stopResendingDefaults = True try: @@ -497,9 +496,9 @@ class SettingsDialog(QtGui.QDialog): months = 0.0 if days >= 0 and months >= 0 and not stopResendingDefaults: - shared.maximumLengthOfTimeToBotherResendingMessages = \ + state.maximumLengthOfTimeToBotherResendingMessages = \ days * 24 * 60 * 60 + months * 60 * 60 * 24 * 365 / 12 - if shared.maximumLengthOfTimeToBotherResendingMessages < 432000: + if state.maximumLengthOfTimeToBotherResendingMessages < 432000: # If the time period is less than 5 hours, we give # zero values to all fields. No message will be sent again. QtGui.QMessageBox.about( @@ -516,7 +515,7 @@ class SettingsDialog(QtGui.QDialog): 'bitmessagesettings', 'stopresendingafterxdays', '0') self.config.set( 'bitmessagesettings', 'stopresendingafterxmonths', '0') - shared.maximumLengthOfTimeToBotherResendingMessages = 0.0 + state.maximumLengthOfTimeToBotherResendingMessages = 0.0 else: self.config.set( 'bitmessagesettings', 'stopresendingafterxdays', str(days)) diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index 824580c2..f435d8d4 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -140,9 +140,9 @@ class objectProcessor(threading.Thread): # bypass nonce and time, retain object type/version/stream + body readPosition = 16 - if data[readPosition:] in shared.ackdataForWhichImWatching: + if data[readPosition:] in state.ackdataForWhichImWatching: logger.info('This object is an acknowledgement bound for me.') - del shared.ackdataForWhichImWatching[data[readPosition:]] + del state.ackdataForWhichImWatching[data[readPosition:]] sqlExecute( 'UPDATE sent SET status=?, lastactiontime=?' ' WHERE ackdata=?', @@ -286,7 +286,7 @@ class objectProcessor(threading.Thread): def processpubkey(self, data): """Process a pubkey object""" pubkeyProcessingStartTime = time.time() - shared.numberOfPubkeysProcessed += 1 + state.numberOfPubkeysProcessed += 1 queues.UISignalQueue.put(( 'updateNumberOfPubkeysProcessed', 'no data')) readPosition = 20 # bypass the nonce, time, and object type @@ -459,7 +459,7 @@ class objectProcessor(threading.Thread): def processmsg(self, data): """Process a message object""" messageProcessingStartTime = time.time() - shared.numberOfMessagesProcessed += 1 + state.numberOfMessagesProcessed += 1 queues.UISignalQueue.put(( 'updateNumberOfMessagesProcessed', 'no data')) readPosition = 20 # bypass the nonce, time, and object type @@ -808,7 +808,7 @@ class objectProcessor(threading.Thread): def processbroadcast(self, data): """Process a broadcast object""" messageProcessingStartTime = time.time() - shared.numberOfBroadcastsProcessed += 1 + state.numberOfBroadcastsProcessed += 1 queues.UISignalQueue.put(( 'updateNumberOfBroadcastsProcessed', 'no data')) inventoryHash = calculateInventoryHash(data) diff --git a/src/class_singleCleaner.py b/src/class_singleCleaner.py index b9fe3d1c..e322574e 100644 --- a/src/class_singleCleaner.py +++ b/src/class_singleCleaner.py @@ -25,7 +25,6 @@ import time import knownnodes import queues -import shared import state import tr from bmconfigparser import BMConfigParser @@ -34,6 +33,13 @@ from inventory import Inventory from network import BMConnectionPool, StoppableThread +#: Equals 4 weeks. You could make this longer if you want +#: but making it shorter would not be advisable because +#: there is a very small possibility that it could keep you +#: from obtaining a needed pubkey for a period of time. +lengthOfTimeToHoldOnToAllPubkeys = 2419200 + + class singleCleaner(StoppableThread): """The singleCleaner thread class""" name = "singleCleaner" @@ -44,7 +50,7 @@ class singleCleaner(StoppableThread): gc.disable() timeWeLastClearedInventoryAndPubkeysTables = 0 try: - shared.maximumLengthOfTimeToBotherResendingMessages = ( + state.maximumLengthOfTimeToBotherResendingMessages = ( float(BMConfigParser().get( 'bitmessagesettings', 'stopresendingafterxdays')) * 24 * 60 * 60 @@ -56,7 +62,7 @@ class singleCleaner(StoppableThread): # Either the user hasn't set stopresendingafterxdays and # stopresendingafterxmonths yet or the options are missing # from the config file. - shared.maximumLengthOfTimeToBotherResendingMessages = float('inf') + state.maximumLengthOfTimeToBotherResendingMessages = float('inf') # initial wait if state.shutdown == 0: @@ -74,7 +80,7 @@ class singleCleaner(StoppableThread): # queue which will never be handled by a UI. We should clear it to # save memory. # FIXME redundant? - if shared.thisapp.daemon or not state.enableGUI: + if state.thisapp.daemon or not state.enableGUI: queues.UISignalQueue.queue.clear() if timeWeLastClearedInventoryAndPubkeysTables < \ int(time.time()) - 7380: @@ -84,7 +90,7 @@ class singleCleaner(StoppableThread): # pubkeys sqlExecute( "DELETE FROM pubkeys WHERE time?)", int(time.time()), int(time.time()) - - shared.maximumLengthOfTimeToBotherResendingMessages + - state.maximumLengthOfTimeToBotherResendingMessages ) for row in queryreturn: if len(row) < 2: @@ -131,7 +137,8 @@ class singleCleaner(StoppableThread): ' is full. Bitmessage will now exit.'), True) )) - if shared.thisapp.daemon or not state.enableGUI: + # FIXME redundant? + if state.thisapp.daemon or not state.enableGUI: os._exit(1) # inv/object tracking diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index 6d7514d4..6e2522d8 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -94,25 +94,25 @@ class singleWorker(StoppableThread): hexlify(privEncryptionKey)) ) - # Initialize the shared.ackdataForWhichImWatching data structure + # Initialize the state.ackdataForWhichImWatching data structure queryreturn = sqlQuery( '''SELECT ackdata FROM sent WHERE status = 'msgsent' ''') for row in queryreturn: ackdata, = row self.logger.info('Watching for ackdata %s', hexlify(ackdata)) - shared.ackdataForWhichImWatching[ackdata] = 0 + state.ackdataForWhichImWatching[ackdata] = 0 # Fix legacy (headerless) watched ackdata to include header - for oldack in shared.ackdataForWhichImWatching: + for oldack in state.ackdataForWhichImWatching: if len(oldack) == 32: # attach legacy header, always constant (msg/1/1) newack = '\x00\x00\x00\x02\x01\x01' + oldack - shared.ackdataForWhichImWatching[newack] = 0 + state.ackdataForWhichImWatching[newack] = 0 sqlExecute( 'UPDATE sent SET ackdata=? WHERE ackdata=?', newack, oldack ) - del shared.ackdataForWhichImWatching[oldack] + del state.ackdataForWhichImWatching[oldack] # give some time for the GUI to start # before we start on existing POW tasks. @@ -864,7 +864,7 @@ class singleWorker(StoppableThread): # if we aren't sending this to ourselves or a chan if not BMConfigParser().has_section(toaddress): - shared.ackdataForWhichImWatching[ackdata] = 0 + state.ackdataForWhichImWatching[ackdata] = 0 queues.UISignalQueue.put(( 'updateSentItemStatusByAckdata', ( ackdata, diff --git a/src/network/tcp.py b/src/network/tcp.py index d611b1ca..18fd2e1a 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -14,7 +14,6 @@ import connectionpool import helper_random import knownnodes import protocol -import shared import state from bmconfigparser import BMConfigParser from helper_random import randomBytes @@ -34,6 +33,9 @@ from queues import invQueue, receiveDataQueue, UISignalQueue logger = logging.getLogger('default') +maximumAgeOfNodesThatIAdvertiseToOthers = 10800 #: Equals three hours + + class TCPConnection(BMProto, TLSDispatcher): # pylint: disable=too-many-instance-attributes """ @@ -136,7 +138,7 @@ class TCPConnection(BMProto, TLSDispatcher): def set_connection_fully_established(self): """Initiate inventory synchronisation.""" if not self.isOutbound and not self.local: - shared.clientHasReceivedIncomingConnections = True + state.clientHasReceivedIncomingConnections = True UISignalQueue.put(('setStatusIcon', 'green')) UISignalQueue.put( ('updateNetworkStatusTab', ( @@ -170,7 +172,7 @@ class TCPConnection(BMProto, TLSDispatcher): filtered = [ (k, v) for k, v in nodes.iteritems() if v["lastseen"] > int(time.time()) - - shared.maximumAgeOfNodesThatIAdvertiseToOthers and + maximumAgeOfNodesThatIAdvertiseToOthers and v["rating"] >= 0 and len(k.host) <= 22 ] # sent 250 only if the remote isn't interested in it diff --git a/src/shared.py b/src/shared.py index beed52ed..072d04c0 100644 --- a/src/shared.py +++ b/src/shared.py @@ -13,7 +13,6 @@ import os import stat import subprocess import sys -import threading from binascii import hexlify # Project imports. @@ -27,19 +26,6 @@ from helper_sql import sqlQuery from pyelliptic import arithmetic -verbose = 1 -# This is obsolete with the change to protocol v3 -# but the singleCleaner thread still hasn't been updated -# so we need this a little longer. -maximumAgeOfAnObjectThatIAmWillingToAccept = 216000 -# Equals 4 weeks. You could make this longer if you want -# but making it shorter would not be advisable because -# there is a very small possibility that it could keep you -# from obtaining a needed pubkey for a period of time. -lengthOfTimeToHoldOnToAllPubkeys = 2419200 -maximumAgeOfNodesThatIAdvertiseToOthers = 10800 # Equals three hours - - myECCryptorObjects = {} MyECSubscriptionCryptorObjects = {} # The key in this dictionary is the RIPE hash which is encoded @@ -48,19 +34,6 @@ myAddressesByHash = {} # The key in this dictionary is the tag generated from the address. myAddressesByTag = {} broadcastSendersForWhichImWatching = {} -printLock = threading.Lock() -statusIconColor = 'red' - -thisapp = None # singleton lock instance - -ackdataForWhichImWatching = {} -# used by API command clientStatus -clientHasReceivedIncomingConnections = False -numberOfMessagesProcessed = 0 -numberOfBroadcastsProcessed = 0 -numberOfPubkeysProcessed = 0 - -maximumLengthOfTimeToBotherResendingMessages = 0 def isAddressInMyAddressBook(address): diff --git a/src/shutdown.py b/src/shutdown.py index dbc2af04..819aa2da 100644 --- a/src/shutdown.py +++ b/src/shutdown.py @@ -4,7 +4,6 @@ import Queue import threading import time -import shared import state from debug import logger from helper_sql import sqlQuery, sqlStoredProcedure @@ -80,9 +79,9 @@ def doCleanShutdown(): except Queue.Empty: break - if shared.thisapp.daemon or not state.enableGUI: # ..fixme:: redundant? + if state.thisapp.daemon or not state.enableGUI: logger.info('Clean shutdown complete.') - shared.thisapp.cleanup() + state.thisapp.cleanup() os._exit(0) # pylint: disable=protected-access else: logger.info('Core shutdown complete.') diff --git a/src/state.py b/src/state.py index 58e1106a..8bfaf7b9 100644 --- a/src/state.py +++ b/src/state.py @@ -39,6 +39,8 @@ sqlReady = False maximumNumberOfHalfOpenConnections = 0 +maximumLengthOfTimeToBotherResendingMessages = 0 + invThread = None addrThread = None downloadThread = None @@ -55,3 +57,21 @@ testmode = False kivy = False association = '' + +clientHasReceivedIncomingConnections = False +"""used by API command clientStatus""" + +numberOfMessagesProcessed = 0 +numberOfBroadcastsProcessed = 0 +numberOfPubkeysProcessed = 0 + +statusIconColor = 'red' +""" +GUI status icon color +.. note:: bad style, refactor it +""" + +ackdataForWhichImWatching = {} + +thisapp = None +"""Singleton instance""" diff --git a/src/threads.py b/src/threads.py index b7471508..ac8bf7a6 100644 --- a/src/threads.py +++ b/src/threads.py @@ -40,7 +40,9 @@ else: threading.Thread._Thread__bootstrap = _thread_name_hack +printLock = threading.Lock() + __all__ = [ "addressGenerator", "objectProcessor", "singleCleaner", "singleWorker", - "sqlThread" + "sqlThread", "printLock" ] From 280095b08f68143d5ca7e3fb836a1a7ade87f63d Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 5 Nov 2019 18:59:50 +0200 Subject: [PATCH 06/28] Moved state.openKeysFile() into bitmessageqt where it's used --- src/bitmessageqt/__init__.py | 13 +++++++++++-- src/shared.py | 8 -------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 88d41b7f..bf391882 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -7,6 +7,7 @@ import locale import os import random import string +import subprocess import sys import textwrap import threading @@ -48,7 +49,6 @@ import paths from proofofwork import getPowType import queues import shutdown -import state from statusbar import BMStatusBar import sound # This is needed for tray icon @@ -73,6 +73,15 @@ def powQueueSize(): return queue_len +def openKeysFile(): + """Open keys file with an external editor""" + keysfile = os.path.join(state.appdata, 'keys.dat') + if 'linux' in sys.platform: + subprocess.call(["xdg-open", keysfile]) + elif sys.platform.startswith('win'): + os.startfile(keysfile) # pylint: disable=no-member + + class MyForm(settingsmixin.SMainWindow): # the maximum frequency of message sounds in seconds @@ -1544,7 +1553,7 @@ class MyForm(settingsmixin.SMainWindow): reply = QtGui.QMessageBox.question(self, _translate("MainWindow", "Open keys.dat?"), _translate( "MainWindow", "You may manage your keys by editing the keys.dat file stored in\n %1 \nIt is important that you back up this file. Would you like to open the file now? (Be sure to close Bitmessage before making any changes.)").arg(state.appdata), QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: - shared.openKeysFile() + openKeysFile() # menu button 'delete all treshed messages' def click_actionDeleteAllTrashedMessages(self): diff --git a/src/shared.py b/src/shared.py index 072d04c0..3a6fbc31 100644 --- a/src/shared.py +++ b/src/shared.py @@ -253,11 +253,3 @@ def fixSensitiveFilePermissions(filename, hasEnabledKeys): except Exception: logger.exception('Keyfile permissions could not be fixed.') raise - - -def openKeysFile(): - """Open keys file with an external editor""" - if 'linux' in sys.platform: - subprocess.call(["xdg-open", state.appdata + 'keys.dat']) - else: - os.startfile(state.appdata + 'keys.dat') From 8684d647a3f9410099f7c10ad84fa2759455de72 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Mon, 18 Nov 2019 14:47:16 +0200 Subject: [PATCH 07/28] Use default digestalg='sha256' in highlevelcrypto.sign() --- src/highlevelcrypto.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/highlevelcrypto.py b/src/highlevelcrypto.py index f392fe4a..f89a31c8 100644 --- a/src/highlevelcrypto.py +++ b/src/highlevelcrypto.py @@ -2,8 +2,8 @@ High level cryptographic functions based on `.pyelliptic` OpenSSL bindings. .. note:: - Upstream pyelliptic was upgraded from SHA1 to SHA256 for signing. - We must upgrade PyBitmessage gracefully. + Upstream pyelliptic was upgraded from SHA1 to SHA256 for signing. We must + `upgrade PyBitmessage gracefully. `_ `More discussion. `_ """ @@ -68,7 +68,7 @@ def sign(msg, hexPrivkey): "digestalg" setting """ digestAlg = BMConfigParser().safeGet( - 'bitmessagesettings', 'digestalg', 'sha1') + 'bitmessagesettings', 'digestalg', 'sha256') if digestAlg == "sha1": # SHA1, this will eventually be deprecated return makeCryptor(hexPrivkey).sign( From c5b77a08fae3a26071a1b7bad2238cb95c04eb88 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Wed, 20 Nov 2019 17:35:51 +0200 Subject: [PATCH 08/28] Moved addresses demo script into tests.test_crypto --- src/addresses.py | 66 ---------------------------------------- src/tests/test_crypto.py | 39 +++++++++++++++++++++++- 2 files changed, 38 insertions(+), 67 deletions(-) diff --git a/src/addresses.py b/src/addresses.py index 0d3d4400..fb86d40c 100644 --- a/src/addresses.py +++ b/src/addresses.py @@ -274,69 +274,3 @@ def addBMIfNotPresent(address): """Prepend BM- to an address if it doesn't already have it""" address = str(address).strip() return address if address[:3] == 'BM-' else 'BM-' + address - - -# TODO: make test case -if __name__ == "__main__": - from pyelliptic import arithmetic - - print( - '\nLet us make an address from scratch. Suppose we generate two' - ' random 32 byte values and call the first one the signing key' - ' and the second one the encryption key:' - ) - privateSigningKey = \ - '93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665' - privateEncryptionKey = \ - '4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a' - print( - '\nprivateSigningKey = %s\nprivateEncryptionKey = %s' % - (privateSigningKey, privateEncryptionKey) - ) - print( - '\nNow let us convert them to public keys by doing' - ' an elliptic curve point multiplication.' - ) - publicSigningKey = arithmetic.privtopub(privateSigningKey) - publicEncryptionKey = arithmetic.privtopub(privateEncryptionKey) - print( - '\npublicSigningKey = %s\npublicEncryptionKey = %s' % - (publicSigningKey, publicEncryptionKey) - ) - - print( - '\nNotice that they both begin with the \\x04 which specifies' - ' the encoding type. This prefix is not send over the wire.' - ' You must strip if off before you send your public key across' - ' the wire, and you must add it back when you receive a public key.' - ) - - publicSigningKeyBinary = \ - arithmetic.changebase(publicSigningKey, 16, 256, minlen=64) - publicEncryptionKeyBinary = \ - arithmetic.changebase(publicEncryptionKey, 16, 256, minlen=64) - - ripe = hashlib.new('ripemd160') - sha = hashlib.new('sha512') - sha.update(publicSigningKeyBinary + publicEncryptionKeyBinary) - - ripe.update(sha.digest()) - addressVersionNumber = 2 - streamNumber = 1 - print( - '\nRipe digest that we will encode in the address: %s' % - hexlify(ripe.digest()) - ) - returnedAddress = \ - encodeAddress(addressVersionNumber, streamNumber, ripe.digest()) - print('Encoded address: %s' % returnedAddress) - status, addressVersionNumber, streamNumber, data = \ - decodeAddress(returnedAddress) - print( - '\nAfter decoding address:\n\tStatus: %s' - '\n\taddressVersionNumber %s' - '\n\tstreamNumber %s' - '\n\tlength of data (the ripe hash): %s' - '\n\tripe data: %s' % - (status, addressVersionNumber, streamNumber, len(data), hexlify(data)) - ) diff --git a/src/tests/test_crypto.py b/src/tests/test_crypto.py index b7eb7177..b53105cb 100644 --- a/src/tests/test_crypto.py +++ b/src/tests/test_crypto.py @@ -6,6 +6,7 @@ import hashlib import unittest from abc import ABCMeta, abstractmethod from binascii import hexlify, unhexlify +from pybitmessage.pyelliptic import arithmetic try: from Crypto.Hash import RIPEMD @@ -20,8 +21,13 @@ sample_pubsigningkey = unhexlify( sample_pubencryptionkey = unhexlify( '044597d59177fc1d89555d38915f581b5ff2286b39d022ca0283d2bdd5c36be5d3c' 'e7b9b97792327851a562752e4b79475d1f51f5a71352482b241227f45ed36a9') - +sample_privatesigningkey = \ + '93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665' +sample_privateencryptionkey = \ + '4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a' sample_ripe = '003cd097eb7f35c87b5dc8b4538c22cb55312a9f' +# stream: 1, version: 2 +sample_address = 'BM-onkVu1KKL2UaUss5Upg9vXmqd3esTmV79' _sha = hashlib.new('sha512') _sha.update(sample_pubsigningkey + sample_pubencryptionkey) @@ -59,3 +65,34 @@ class TestCrypto(RIPEMD160TestCase, unittest.TestCase): @staticmethod def _hashdigest(data): return RIPEMD.RIPEMD160Hash(data).digest() + + +class TestAddresses(unittest.TestCase): + """Test addresses manipulations""" + def test_privtopub(self): + """Generate public keys and check the result""" + self.assertEqual( + arithmetic.privtopub(sample_privatesigningkey), + hexlify(sample_pubsigningkey) + ) + self.assertEqual( + arithmetic.privtopub(sample_privateencryptionkey), + hexlify(sample_pubencryptionkey) + ) + + def test_address(self): + """Create address and check the result""" + from pybitmessage import addresses + from pybitmessage.fallback import RIPEMD160Hash + + sha = hashlib.new('sha512') + sha.update(sample_pubsigningkey + sample_pubencryptionkey) + ripe_hash = RIPEMD160Hash(sha.digest()).digest() + self.assertEqual(ripe_hash, unhexlify(sample_ripe)) + + self.assertEqual( + addresses.encodeAddress(2, 1, ripe_hash), sample_address) + + self.assertEqual( + addresses.decodeAddress(sample_address), + ('success', 2, 1, ripe_hash)) From d09782e53d3f42132532b6e39011cd27e7f41d25 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 24 Jan 2020 18:23:59 +0200 Subject: [PATCH 09/28] Obsolete bitmessagemain.connectToStream(), use BMConnectionPool method --- src/bitmessagemain.py | 30 +++--------------------------- src/helper_startup.py | 19 ++++++++++++++----- src/network/connectionpool.py | 1 + 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index d9a7c7f5..2a279aca 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -38,9 +38,7 @@ import state from bmconfigparser import BMConfigParser from debug import logger # this should go before any threads from helper_startup import ( - isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections, - start_proxyconfig -) + adjustHalfOpenConnectionsLimit, start_proxyconfig) from inventory import Inventory from knownnodes import readKnownNodes # Network objects and threads @@ -55,27 +53,6 @@ from threads import ( addressGenerator, objectProcessor, singleCleaner, singleWorker, sqlThread) -def connectToStream(streamNumber): - """Connect to a stream""" - state.streamsInWhichIAmParticipating.append(streamNumber) - - if isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): - # Some XP and Vista systems can only have 10 outgoing connections - # at a time. - state.maximumNumberOfHalfOpenConnections = 9 - else: - state.maximumNumberOfHalfOpenConnections = 64 - try: - # don't overload Tor - if BMConfigParser().get( - 'bitmessagesettings', 'socksproxytype') != 'none': - state.maximumNumberOfHalfOpenConnections = 4 - except: - pass - - BMConnectionPool().connectToStream(streamNumber) - - def _fixSocket(): if sys.platform.startswith('linux'): socket.SO_BINDTODEVICE = 25 @@ -174,6 +151,7 @@ class Main(object): """Start main application""" # pylint: disable=too-many-statements,too-many-branches,too-many-locals _fixSocket() + adjustHalfOpenConnectionsLimit() config = BMConfigParser() daemon = config.safeGetBoolean('bitmessagesettings', 'daemon') @@ -332,7 +310,7 @@ class Main(object): # start network components if networking is enabled if state.enableNetwork: start_proxyconfig() - BMConnectionPool() + BMConnectionPool().connectToStream(1) asyncoreThread = BMNetworkThread() asyncoreThread.daemon = True asyncoreThread.start() @@ -356,8 +334,6 @@ class Main(object): state.uploadThread.daemon = True state.uploadThread.start() - connectToStream(1) - if config.safeGetBoolean('bitmessagesettings', 'upnp'): import upnp upnpThread = upnp.uPnPThread() diff --git a/src/helper_startup.py b/src/helper_startup.py index 9711c339..fcd12aa4 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -276,19 +276,28 @@ def updateConfig(): config.save() -def isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): - """Check for (mainly XP and Vista) limitations""" +def adjustHalfOpenConnectionsLimit(): + """Check and satisfy half-open connections limit (mainly XP and Vista)""" + if BMConfigParser().safeGet( + 'bitmessagesettings', 'socksproxytype', 'none') != 'none': + state.maximumNumberOfHalfOpenConnections = 4 + return + + is_limited = False try: if sys.platform[0:3] == "win": + # Some XP and Vista systems can only have 10 outgoing + # connections at a time. VER_THIS = StrictVersion(platform.version()) - return ( + is_limited = ( StrictVersion("5.1.2600") <= VER_THIS and StrictVersion("6.0.6000") >= VER_THIS ) - return False - except Exception: + except ValueError: pass + state.maximumNumberOfHalfOpenConnections = 9 if is_limited else 64 + def start_proxyconfig(): """Check socksproxytype and start any proxy configuration plugin""" diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index 6264191d..77eb58e4 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -87,6 +87,7 @@ class BMConnectionPool(object): def connectToStream(self, streamNumber): """Connect to a bitmessage stream""" self.streams.append(streamNumber) + state.streamsInWhichIAmParticipating.append(streamNumber) def getConnectionByAddr(self, addr): """ From 045a2ef4436ef20be1baa9caa69934460f19e881 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 17 May 2019 12:36:35 +0300 Subject: [PATCH 10/28] Update messagelist also if search line cleared --- src/bitmessageqt/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index bf391882..559ad0dd 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -3884,14 +3884,16 @@ class MyForm(settingsmixin.SMainWindow): def inboxSearchLineEditUpdated(self, text): # dynamic search for too short text is slow - if len(str(text)) < 3: + text = str(text) + if 0 < len(text) < 3: return messagelist = self.getCurrentMessagelist() searchOption = self.getCurrentSearchOption() if messagelist: account = self.getCurrentAccount() folder = self.getCurrentFolder() - self.loadMessagelist(messagelist, account, folder, searchOption, str(text)) + self.loadMessagelist( + messagelist, account, folder, searchOption, text) def inboxSearchLineEditReturnPressed(self): logger.debug("Search return pressed") From ef6be53702386cd9b5c53a87cb65bb113773fa31 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 17 May 2019 17:34:55 +0300 Subject: [PATCH 11/28] Do not convert search option because helper_search compares it to the result of _translate() --- src/bitmessageqt/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 559ad0dd..0fdfd915 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -3497,9 +3497,7 @@ class MyForm(settingsmixin.SMainWindow): self.ui.inboxSearchOptionChans, ] if currentIndex >= 0 and currentIndex < len(messagelistList): - return messagelistList[currentIndex].currentText().toUtf8().data() - else: - return None + return messagelistList[currentIndex].currentText() # Group of functions for the Your Identities dialog box def getCurrentItem(self, treeWidget=None): From aba61e57a852b2157a5145c6518bb8a4bb030e32 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 17 May 2019 18:43:06 +0300 Subject: [PATCH 12/28] flake8 and style fixes and docstrings in helper_search --- src/bitmessageqt/__init__.py | 5 +- src/helper_search.py | 147 ++++++++++++++++++++--------------- 2 files changed, 86 insertions(+), 66 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 0fdfd915..8433498f 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -1213,8 +1213,7 @@ class MyForm(settingsmixin.SMainWindow): queryreturn = helper_search.search_sql(xAddress, account, "sent", where, what, False) for row in queryreturn: - toAddress, fromAddress, subject, status, ackdata, lastactiontime = row - self.addMessageListItemSent(tableWidget, toAddress, fromAddress, subject, status, ackdata, lastactiontime) + self.addMessageListItemSent(tableWidget, *row) tableWidget.horizontalHeader().setSortIndicator( 3, QtCore.Qt.DescendingOrder) @@ -1246,7 +1245,7 @@ class MyForm(settingsmixin.SMainWindow): queryreturn = helper_search.search_sql(xAddress, account, folder, where, what, unreadOnly) for row in queryreturn: - msgfolder, msgid, toAddress, fromAddress, subject, received, read = row + toAddress, fromAddress, subject, msgfolder, msgid, received, read = row self.addMessageListItemInbox(tableWidget, msgfolder, msgid, toAddress, fromAddress, subject, received, read) tableWidget.horizontalHeader().setSortIndicator( diff --git a/src/helper_search.py b/src/helper_search.py index 69acec43..9fcb88b5 100644 --- a/src/helper_search.py +++ b/src/helper_search.py @@ -1,92 +1,113 @@ -"""Additional SQL helper for searching messages""" +""" +Additional SQL helper for searching messages. +Used by :mod:`.bitmessageqt`. +""" from helper_sql import sqlQuery - -try: - from PyQt4 import QtGui - haveQt = True -except ImportError: - haveQt = False +from tr import _translate -def search_translate(context, text): - """Translation wrapper""" - if haveQt: - return QtGui.QApplication.translate(context, text) - return text.lower() +def search_sql( + xAddress='toaddress', account=None, folder='inbox', where=None, + what=None, unreadOnly=False +): + """ + Search for messages from given account and folder having search term + in one of it's fields. - -def search_sql(xAddress="toaddress", account=None, folder="inbox", where=None, what=None, unreadOnly=False): - """Perform a search in mailbox tables""" + :param str xAddress: address field checked + ('fromaddress', 'toaddress' or 'both') + :param account: the account which is checked + :type account: :class:`.bitmessageqt.account.BMAccount` + instance + :param str folder: the folder which is checked + :param str where: message field which is checked ('toaddress', + 'fromaddress', 'subject' or 'message'), by default check any field + :param str what: the search term + :param bool unreadOnly: if True, search only for unread messages + :return: all messages where field contains + :rtype: list[list] + """ # pylint: disable=too-many-arguments, too-many-branches - if what is not None and what != "": - what = "%" + what + "%" - if where == search_translate("MainWindow", "To"): - where = "toaddress" - elif where == search_translate("MainWindow", "From"): - where = "fromaddress" - elif where == search_translate("MainWindow", "Subject"): - where = "subject" - elif where == search_translate("MainWindow", "Message"): - where = "message" + if what: + what = '%' + what + '%' + if where == _translate("MainWindow", "To"): + where = 'toaddress' + elif where == _translate("MainWindow", "From"): + where = 'fromaddress' + elif where == _translate("MainWindow", "Subject"): + where = 'subject' + elif where == _translate("MainWindow", "Message"): + where = 'message' else: - where = "toaddress || fromaddress || subject || message" - else: - what = None + where = 'toaddress || fromaddress || subject || message' - if folder == "sent": - sqlStatementBase = ''' - SELECT toaddress, fromaddress, subject, status, ackdata, lastactiontime - FROM sent ''' - else: - sqlStatementBase = '''SELECT folder, msgid, toaddress, fromaddress, subject, received, read - FROM inbox ''' + sqlStatementBase = 'SELECT toaddress, fromaddress, subject, ' + ( + 'status, ackdata, lastactiontime FROM sent ' if folder == 'sent' + else 'folder, msgid, received, read FROM inbox ' + ) sqlStatementParts = [] sqlArguments = [] if account is not None: if xAddress == 'both': - sqlStatementParts.append("(fromaddress = ? OR toaddress = ?)") + sqlStatementParts.append('(fromaddress = ? OR toaddress = ?)') sqlArguments.append(account) sqlArguments.append(account) else: - sqlStatementParts.append(xAddress + " = ? ") + sqlStatementParts.append(xAddress + ' = ? ') sqlArguments.append(account) if folder is not None: - if folder == "new": - folder = "inbox" + if folder == 'new': + folder = 'inbox' unreadOnly = True - sqlStatementParts.append("folder = ? ") + sqlStatementParts.append('folder = ? ') sqlArguments.append(folder) else: - sqlStatementParts.append("folder != ?") - sqlArguments.append("trash") - if what is not None: - sqlStatementParts.append("%s LIKE ?" % (where)) + sqlStatementParts.append('folder != ?') + sqlArguments.append('trash') + if what: + sqlStatementParts.append('%s LIKE ?' % (where)) sqlArguments.append(what) if unreadOnly: - sqlStatementParts.append("read = 0") + sqlStatementParts.append('read = 0') if sqlStatementParts: - sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts) - if folder == "sent": - sqlStatementBase += " ORDER BY lastactiontime" + sqlStatementBase += 'WHERE ' + ' AND '.join(sqlStatementParts) + if folder == 'sent': + sqlStatementBase += ' ORDER BY lastactiontime' return sqlQuery(sqlStatementBase, sqlArguments) -def check_match(toAddress, fromAddress, subject, message, where=None, what=None): - """Check if a single message matches a filter (used when new messages are added to messagelists)""" +def check_match( + toAddress, fromAddress, subject, message, where=None, what=None): + """ + Check if a single message matches a filter (used when new messages + are added to messagelists) + """ # pylint: disable=too-many-arguments - if what is not None and what != "": - if where in (search_translate("MainWindow", "To"), search_translate("MainWindow", "All")): - if what.lower() not in toAddress.lower(): - return False - elif where in (search_translate("MainWindow", "From"), search_translate("MainWindow", "All")): - if what.lower() not in fromAddress.lower(): - return False - elif where in (search_translate("MainWindow", "Subject"), search_translate("MainWindow", "All")): - if what.lower() not in subject.lower(): - return False - elif where in (search_translate("MainWindow", "Message"), search_translate("MainWindow", "All")): - if what.lower() not in message.lower(): - return False + if not what: + return True + + if where in ( + _translate("MainWindow", "To"), _translate("MainWindow", "All") + ): + if what.lower() not in toAddress.lower(): + return False + elif where in ( + _translate("MainWindow", "From"), _translate("MainWindow", "All") + ): + if what.lower() not in fromAddress.lower(): + return False + elif where in ( + _translate("MainWindow", "Subject"), + _translate("MainWindow", "All") + ): + if what.lower() not in subject.lower(): + return False + elif where in ( + _translate("MainWindow", "Message"), + _translate("MainWindow", "All") + ): + if what.lower() not in message.lower(): + return False return True From 778772245298b5bc4622783b8266142c32bb31d1 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 21 May 2019 18:29:16 +0300 Subject: [PATCH 13/28] Search option "To" makes no sense on tab "Subscriptions" Made "Subject" the default search option --- src/bitmessageqt/bitmessageui.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bitmessageqt/bitmessageui.py b/src/bitmessageqt/bitmessageui.py index 30d054d0..f81bf285 100644 --- a/src/bitmessageqt/bitmessageui.py +++ b/src/bitmessageqt/bitmessageui.py @@ -104,6 +104,7 @@ class Ui_MainWindow(object): self.inboxSearchOption.addItem(_fromUtf8("")) self.inboxSearchOption.addItem(_fromUtf8("")) self.inboxSearchOption.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) + self.inboxSearchOption.setCurrentIndex(3) self.horizontalSplitterSearch.addWidget(self.inboxSearchOption) self.horizontalSplitterSearch.handle(1).setEnabled(False) self.horizontalSplitterSearch.setStretchFactor(0, 1) @@ -403,8 +404,8 @@ class Ui_MainWindow(object): self.inboxSearchOptionSubscriptions.addItem(_fromUtf8("")) self.inboxSearchOptionSubscriptions.addItem(_fromUtf8("")) self.inboxSearchOptionSubscriptions.addItem(_fromUtf8("")) - self.inboxSearchOptionSubscriptions.addItem(_fromUtf8("")) self.inboxSearchOptionSubscriptions.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) + self.inboxSearchOptionSubscriptions.setCurrentIndex(2) self.horizontalSplitter_2.addWidget(self.inboxSearchOptionSubscriptions) self.horizontalSplitter_2.handle(1).setEnabled(False) self.horizontalSplitter_2.setStretchFactor(0, 1) @@ -504,6 +505,7 @@ class Ui_MainWindow(object): self.inboxSearchOptionChans.addItem(_fromUtf8("")) self.inboxSearchOptionChans.addItem(_fromUtf8("")) self.inboxSearchOptionChans.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) + self.inboxSearchOptionChans.setCurrentIndex(3) self.horizontalSplitter_6.addWidget(self.inboxSearchOptionChans) self.horizontalSplitter_6.handle(1).setEnabled(False) self.horizontalSplitter_6.setStretchFactor(0, 1) @@ -719,10 +721,9 @@ class Ui_MainWindow(object): self.pushButtonAddSubscription.setText(_translate("MainWindow", "Add new Subscription", None)) self.inboxSearchLineEditSubscriptions.setPlaceholderText(_translate("MainWindow", "Search", None)) self.inboxSearchOptionSubscriptions.setItemText(0, _translate("MainWindow", "All", None)) - self.inboxSearchOptionSubscriptions.setItemText(1, _translate("MainWindow", "To", None)) - self.inboxSearchOptionSubscriptions.setItemText(2, _translate("MainWindow", "From", None)) - self.inboxSearchOptionSubscriptions.setItemText(3, _translate("MainWindow", "Subject", None)) - self.inboxSearchOptionSubscriptions.setItemText(4, _translate("MainWindow", "Message", None)) + self.inboxSearchOptionSubscriptions.setItemText(1, _translate("MainWindow", "From", None)) + self.inboxSearchOptionSubscriptions.setItemText(2, _translate("MainWindow", "Subject", None)) + self.inboxSearchOptionSubscriptions.setItemText(3, _translate("MainWindow", "Message", None)) self.tableWidgetInboxSubscriptions.setSortingEnabled(True) item = self.tableWidgetInboxSubscriptions.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "To", None)) From 2a62fb79cc84124ea8afebe895cd3a5557a93251 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 15 May 2020 13:49:59 +0300 Subject: [PATCH 14/28] Add test for decoding the version message --- src/tests/core.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/tests/core.py b/src/tests/core.py index d56076c3..9132e668 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -12,15 +12,18 @@ import time import unittest import knownnodes +import protocol import state from bmconfigparser import BMConfigParser from helper_msgcoding import MsgEncode, MsgDecode from helper_startup import start_proxyconfig from network import asyncore_pollchoose as asyncore +from network.bmproto import BMProto from network.connectionpool import BMConnectionPool -from network.node import Peer +from network.node import Node, Peer from network.tcp import Socks4aBMConnection, Socks5BMConnection, TCPConnection from queues import excQueue +from version import softwareVersion try: import stem.version as stem_version @@ -216,6 +219,29 @@ class TestCore(unittest.TestCase): % peer.host) self.fail('Failed to connect to at least 3 nodes within 360 sec') + @staticmethod + def _decode_msg(data, pattern): + proto = BMProto() + proto.bm_proto_reset() + proto.payload = data[protocol.Header.size:] + return proto.decode_payload_content(pattern) + + def test_version(self): + """check encoding/decoding of the version message""" + # with single stream + msg = protocol.assembleVersionMessage('127.0.0.1', 8444, [1]) + decoded = self._decode_msg(msg, "IQQiiQlsLv") + peer, _, ua, streams = self._decode_msg(msg, "IQQiiQlsLv")[4:] + self.assertEqual(peer, Node(3, '127.0.0.1', 8444)) + self.assertEqual(ua, '/PyBitmessage:' + softwareVersion + '/') + self.assertEqual(streams, [1]) + # with multiple streams + msg = protocol.assembleVersionMessage('127.0.0.1', 8444, [1, 2, 3]) + decoded = self._decode_msg(msg, "IQQiiQlslv") + peer, _, ua = decoded[4:7] + streams = decoded[7:] + self.assertEqual(streams, [1, 2, 3]) + def run(): """Starts all tests defined in this module""" From d15e614bb197ec195d2486da301be3134354bc09 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Thu, 21 May 2020 01:17:12 +0300 Subject: [PATCH 15/28] Fix streams decoding in BMProto.bm_command_version() --- src/network/bmproto.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 64bde74c..ed8e5924 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -512,9 +512,11 @@ class BMProto(AdvancedDispatcher, ObjectTracker): Incoming version. Parse and log, remember important things, like streams, bitfields, etc. """ + decoded = self.decode_payload_content("IQQiiQlslv") (self.remoteProtocolVersion, self.services, self.timestamp, - self.sockNode, self.peerNode, self.nonce, self.userAgent, - self.streams) = self.decode_payload_content("IQQiiQlsLv") + self.sockNode, self.peerNode, self.nonce, self.userAgent + ) = decoded[:7] + self.streams = decoded[7:] self.nonce = struct.pack('>Q', self.nonce) self.timeOffset = self.timestamp - int(time.time()) logger.debug('remoteProtocolVersion: %i', self.remoteProtocolVersion) From aa333a66a63fb0653f4107f4c9c4d5607e263e7f Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Mon, 26 Feb 2018 13:34:10 +0200 Subject: [PATCH 16/28] Minor style changes: - removed list <-> set conversion in __init__ - tuples instead of lists if changes aren't needed - removed unnecessary variable redefinition in utils - rewrote languagebox module a bit --- src/bitmessageqt/__init__.py | 577 +++++++++++++++++++------------- src/bitmessageqt/languagebox.py | 35 +- src/bitmessageqt/utils.py | 62 ++-- 3 files changed, 397 insertions(+), 277 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 8433498f..50d5cf4d 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -224,19 +224,19 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.tableWidgetInbox, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuInbox) + self.on_context_menuInbox) self.ui.tableWidgetInboxSubscriptions.setContextMenuPolicy( QtCore.Qt.CustomContextMenu) if connectSignal: self.connect(self.ui.tableWidgetInboxSubscriptions, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuInbox) + self.on_context_menuInbox) self.ui.tableWidgetInboxChans.setContextMenuPolicy( QtCore.Qt.CustomContextMenu) if connectSignal: self.connect(self.ui.tableWidgetInboxChans, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuInbox) + self.on_context_menuInbox) def init_identities_popup_menu(self, connectSignal=True): # Popup menu for the Your Identities tab @@ -276,7 +276,7 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.treeWidgetYourIdentities, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuYourIdentities) + self.on_context_menuYourIdentities) # load all gui.menu plugins with prefix 'address' self.menu_plugins = {'address': []} @@ -326,7 +326,7 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.treeWidgetChans, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuChan) + self.on_context_menuChan) def init_addressbook_popup_menu(self, connectSignal=True): # Popup menu for the Address Book page @@ -363,7 +363,7 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.tableWidgetAddressBook, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuAddressBook) + self.on_context_menuAddressBook) def init_subscriptions_popup_menu(self, connectSignal=True): # Actions @@ -392,7 +392,7 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.treeWidgetSubscriptions, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuSubscriptions) + self.on_context_menuSubscriptions) def init_sent_popup_menu(self, connectSignal=True): # Actions @@ -423,13 +423,13 @@ class MyForm(settingsmixin.SMainWindow): treeWidget.header().setSortIndicator( 0, QtCore.Qt.AscendingOrder) # init dictionary - + db = getSortedSubscriptions(True) for address in db: for folder in folders: - if not folder in db[address]: + if folder not in db[address]: db[address][folder] = {} - + if treeWidget.isSortingEnabled(): treeWidget.setSortingEnabled(False) @@ -441,8 +441,8 @@ class MyForm(settingsmixin.SMainWindow): toAddress = widget.address else: toAddress = None - - if not toAddress in db: + + if toAddress not in db: treeWidget.takeTopLevelItem(i) # no increment continue @@ -472,7 +472,7 @@ class MyForm(settingsmixin.SMainWindow): widget.setUnreadCount(unread) db.pop(toAddress, None) i += 1 - + i = 0 for toAddress in db: widget = Ui_SubscriptionWidget(treeWidget, i, toAddress, db[toAddress]["inbox"]['count'], db[toAddress]["inbox"]['label'], db[toAddress]["inbox"]['enabled']) @@ -487,23 +487,22 @@ class MyForm(settingsmixin.SMainWindow): j += 1 widget.setUnreadCount(unread) i += 1 - - treeWidget.setSortingEnabled(True) + treeWidget.setSortingEnabled(True) def rerenderTabTreeMessages(self): self.rerenderTabTree('messages') def rerenderTabTreeChans(self): self.rerenderTabTree('chan') - + def rerenderTabTree(self, tab): if tab == 'messages': treeWidget = self.ui.treeWidgetYourIdentities elif tab == 'chan': treeWidget = self.ui.treeWidgetChans folders = Ui_FolderWidget.folderWeight.keys() - + # sort ascending when creating if treeWidget.topLevelItemCount() == 0: treeWidget.header().setSortIndicator( @@ -511,7 +510,7 @@ class MyForm(settingsmixin.SMainWindow): # init dictionary db = {} enabled = {} - + for toAddress in getSortedAccounts(): isEnabled = BMConfigParser().getboolean( toAddress, 'enabled') @@ -530,7 +529,7 @@ class MyForm(settingsmixin.SMainWindow): db[toAddress] = {} for folder in folders: db[toAddress][folder] = 0 - + enabled[toAddress] = isEnabled # get number of (unread) messages @@ -548,10 +547,10 @@ class MyForm(settingsmixin.SMainWindow): db[None]["sent"] = 0 db[None]["trash"] = 0 enabled[None] = True - + if treeWidget.isSortingEnabled(): treeWidget.setSortingEnabled(False) - + widgets = {} i = 0 while i < treeWidget.topLevelItemCount(): @@ -560,8 +559,8 @@ class MyForm(settingsmixin.SMainWindow): toAddress = widget.address else: toAddress = None - - if not toAddress in db: + + if toAddress not in db: treeWidget.takeTopLevelItem(i) # no increment continue @@ -570,8 +569,9 @@ class MyForm(settingsmixin.SMainWindow): while j < widget.childCount(): subwidget = widget.child(j) try: - subwidget.setUnreadCount(db[toAddress][subwidget.folderName]) - if subwidget.folderName not in ["new", "trash", "sent"]: + subwidget.setUnreadCount( + db[toAddress][subwidget.folderName]) + if subwidget.folderName not in ("new", "trash", "sent"): unread += db[toAddress][subwidget.folderName] db[toAddress].pop(subwidget.folderName, None) except: @@ -587,13 +587,13 @@ class MyForm(settingsmixin.SMainWindow): if toAddress is not None and tab == 'messages' and folder == "new": continue subwidget = Ui_FolderWidget(widget, j, toAddress, f, c) - if subwidget.folderName not in ["new", "trash", "sent"]: + if subwidget.folderName not in ("new", "trash", "sent"): unread += c j += 1 widget.setUnreadCount(unread) db.pop(toAddress, None) i += 1 - + i = 0 for toAddress in db: widget = Ui_AddressWidget(treeWidget, i, toAddress, db[toAddress]["inbox"], enabled[toAddress]) @@ -603,12 +603,12 @@ class MyForm(settingsmixin.SMainWindow): if toAddress is not None and tab == 'messages' and folder == "new": continue subwidget = Ui_FolderWidget(widget, j, toAddress, folder, db[toAddress][folder]) - if subwidget.folderName not in ["new", "trash", "sent"]: + if subwidget.folderName not in ("new", "trash", "sent"): unread += db[toAddress][folder] j += 1 widget.setUnreadCount(unread) i += 1 - + treeWidget.setSortingEnabled(True) def __init__(self, parent=None): @@ -1088,15 +1088,16 @@ class MyForm(settingsmixin.SMainWindow): if sortingEnabled: tableWidget.setSortingEnabled(False) tableWidget.insertRow(0) - for i in range(len(items)): - tableWidget.setItem(0, i, items[i]) + for i, item in enumerate(items): + tableWidget.setItem(0, i, item) if sortingEnabled: tableWidget.setSortingEnabled(True) - def addMessageListItemSent(self, tableWidget, toAddress, fromAddress, subject, status, ackdata, lastactiontime): - acct = accountClass(fromAddress) - if acct is None: - acct = BMAccount(fromAddress) + def addMessageListItemSent( + self, tableWidget, toAddress, fromAddress, subject, + status, ackdata, lastactiontime + ): + acct = accountClass(fromAddress) or BMAccount(fromAddress) acct.parseMessage(toAddress, fromAddress, subject, "") items = [] @@ -1106,25 +1107,32 @@ class MyForm(settingsmixin.SMainWindow): if status == 'awaitingpubkey': statusText = _translate( - "MainWindow", "Waiting for their encryption key. Will request it again soon.") + "MainWindow", + "Waiting for their encryption key. Will request it again soon." + ) elif status == 'doingpowforpubkey': statusText = _translate( - "MainWindow", "Doing work necessary to request encryption key.") + "MainWindow", "Doing work necessary to request encryption key." + ) elif status == 'msgqueued': - statusText = _translate( - "MainWindow", "Queued.") + statusText = _translate("MainWindow", "Queued.") elif status == 'msgsent': - statusText = _translate("MainWindow", "Message sent. Waiting for acknowledgement. Sent at %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", + "Message sent. Waiting for acknowledgement. Sent at %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'msgsentnoackexpected': - statusText = _translate("MainWindow", "Message sent. Sent at %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", "Message sent. Sent at %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'doingmsgpow': statusText = _translate( "MainWindow", "Doing work necessary to send message.") elif status == 'ackreceived': - statusText = _translate("MainWindow", "Acknowledgement of the message received %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", + "Acknowledgement of the message received %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'broadcastqueued': statusText = _translate( "MainWindow", "Broadcast queued.") @@ -1135,16 +1143,24 @@ class MyForm(settingsmixin.SMainWindow): statusText = _translate("MainWindow", "Broadcast on %1").arg( l10n.formatTimestamp(lastactiontime)) elif status == 'toodifficult': - statusText = _translate("MainWindow", "Problem: The work demanded by the recipient is more difficult than you are willing to do. %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", + "Problem: The work demanded by the recipient is more" + " difficult than you are willing to do. %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'badkey': - statusText = _translate("MainWindow", "Problem: The recipient\'s encryption key is no good. Could not encrypt message. %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", + "Problem: The recipient\'s encryption key is no good." + " Could not encrypt message. %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'forcepow': statusText = _translate( - "MainWindow", "Forced difficulty override. Send should start soon.") + "MainWindow", + "Forced difficulty override. Send should start soon.") else: - statusText = _translate("MainWindow", "Unknown status: %1 %2").arg(status).arg( + statusText = _translate( + "MainWindow", "Unknown status: %1 %2").arg(status).arg( l10n.formatTimestamp(lastactiontime)) newItem = myTableWidgetItem(statusText) newItem.setToolTip(statusText) @@ -1156,15 +1172,16 @@ class MyForm(settingsmixin.SMainWindow): self.addMessageListItem(tableWidget, items) return acct - def addMessageListItemInbox(self, tableWidget, msgfolder, msgid, toAddress, fromAddress, subject, received, read): + def addMessageListItemInbox( + self, tableWidget, toAddress, fromAddress, subject, + msgid, received, read + ): font = QtGui.QFont() font.setBold(True) if toAddress == str_broadcast_subscribers: acct = accountClass(fromAddress) else: - acct = accountClass(toAddress) - if acct is None: - acct = accountClass(fromAddress) + acct = accountClass(toAddress) or accountClass(fromAddress) if acct is None: acct = BMAccount(fromAddress) acct.parseMessage(toAddress, fromAddress, subject, "") @@ -1201,16 +1218,11 @@ class MyForm(settingsmixin.SMainWindow): xAddress = 'both' else: tableWidget.setColumnHidden(0, False) - if account is None: - tableWidget.setColumnHidden(1, False) - else: - tableWidget.setColumnHidden(1, True) + tableWidget.setColumnHidden(1, bool(account)) xAddress = 'fromaddress' - tableWidget.setUpdatesEnabled(False) - tableWidget.setSortingEnabled(False) - tableWidget.setRowCount(0) - queryreturn = helper_search.search_sql(xAddress, account, "sent", where, what, False) + queryreturn = helper_search.search_sql( + xAddress, account, "sent", where, what, False) for row in queryreturn: self.addMessageListItemSent(tableWidget, *row) @@ -1218,11 +1230,19 @@ class MyForm(settingsmixin.SMainWindow): tableWidget.horizontalHeader().setSortIndicator( 3, QtCore.Qt.DescendingOrder) tableWidget.setSortingEnabled(True) - tableWidget.horizontalHeaderItem(3).setText(_translate("MainWindow", "Sent", None)) + tableWidget.horizontalHeaderItem(3).setText( + _translate("MainWindow", "Sent")) tableWidget.setUpdatesEnabled(True) # Load messages from database file - def loadMessagelist(self, tableWidget, account, folder="inbox", where="", what="", unreadOnly = False): + def loadMessagelist( + self, tableWidget, account, folder="inbox", where="", what="", + unreadOnly=False + ): + tableWidget.setUpdatesEnabled(False) + tableWidget.setSortingEnabled(False) + tableWidget.setRowCount(0) + if folder == 'sent': self.loadSent(tableWidget, account, where, what) return @@ -1238,21 +1258,21 @@ class MyForm(settingsmixin.SMainWindow): tableWidget.setColumnHidden(0, False) tableWidget.setColumnHidden(1, False) - tableWidget.setUpdatesEnabled(False) - tableWidget.setSortingEnabled(False) - tableWidget.setRowCount(0) + queryreturn = helper_search.search_sql( + xAddress, account, folder, where, what, unreadOnly) - queryreturn = helper_search.search_sql(xAddress, account, folder, where, what, unreadOnly) - for row in queryreturn: - toAddress, fromAddress, subject, msgfolder, msgid, received, read = row - self.addMessageListItemInbox(tableWidget, msgfolder, msgid, toAddress, fromAddress, subject, received, read) + toAddress, fromAddress, subject, _, msgid, received, read = row + self.addMessageListItemInbox( + tableWidget, toAddress, fromAddress, subject, + msgid, received, read) tableWidget.horizontalHeader().setSortIndicator( 3, QtCore.Qt.DescendingOrder) tableWidget.setSortingEnabled(True) tableWidget.selectRow(0) - tableWidget.horizontalHeaderItem(3).setText(_translate("MainWindow", "Received", None)) + tableWidget.horizontalHeaderItem(3).setText( + _translate("MainWindow", "Received")) tableWidget.setUpdatesEnabled(True) # create application indicator @@ -1479,9 +1499,9 @@ class MyForm(settingsmixin.SMainWindow): def handleKeyPress(self, event, focus=None): """This method handles keypress events for all widgets on MyForm""" messagelist = self.getCurrentMessagelist() - folder = self.getCurrentFolder() if event.key() == QtCore.Qt.Key_Delete: - if isinstance(focus, MessageView) or isinstance(focus, QtGui.QTableWidget): + if isinstance(focus, (MessageView, QtGui.QTableWidget)): + folder = self.getCurrentFolder() if folder == "sent": self.on_action_SentTrash() else: @@ -1517,17 +1537,18 @@ class MyForm(settingsmixin.SMainWindow): self.ui.lineEditTo.setFocus() event.ignore() elif event.key() == QtCore.Qt.Key_F: - searchline = self.getCurrentSearchLine(retObj=True) - if searchline: - searchline.setFocus() + try: + self.getCurrentSearchLine(retObj=True).setFocus() + except AttributeError: + pass event.ignore() if not event.isAccepted(): return if isinstance(focus, MessageView): return MessageView.keyPressEvent(focus, event) - elif isinstance(focus, QtGui.QTableWidget): + if isinstance(focus, QtGui.QTableWidget): return QtGui.QTableWidget.keyPressEvent(focus, event) - elif isinstance(focus, QtGui.QTreeWidget): + if isinstance(focus, QtGui.QTreeWidget): return QtGui.QTreeWidget.keyPressEvent(focus, event) # menu button 'manage keys' @@ -1741,7 +1762,7 @@ class MyForm(settingsmixin.SMainWindow): self.drawTrayIcon(iconFileName, self.findInboxUnreadCount()) def calcTrayIcon(self, iconFileName, inboxUnreadCount): - pixmap = QtGui.QPixmap(":/newPrefix/images/"+iconFileName) + pixmap = QtGui.QPixmap(":/newPrefix/images/" + iconFileName) if inboxUnreadCount > 0: # choose font and calculate font parameters fontName = "Lucida" @@ -1753,7 +1774,8 @@ class MyForm(settingsmixin.SMainWindow): rect = fontMetrics.boundingRect(txt) # margins that we add in the top-right corner marginX = 2 - marginY = 0 # it looks like -2 is also ok due to the error of metric + # it looks like -2 is also ok due to the error of metric + marginY = 0 # if it renders too wide we need to change it to a plus symbol if rect.width() > 20: txt = "+" @@ -1793,11 +1815,18 @@ class MyForm(settingsmixin.SMainWindow): return self.unreadCount def updateSentItemStatusByToAddress(self, toAddress, textToDisplay): - for sent in [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans]: + for sent in ( + self.ui.tableWidgetInbox, + self.ui.tableWidgetInboxSubscriptions, + self.ui.tableWidgetInboxChans + ): treeWidget = self.widgetConvert(sent) if self.getCurrentFolder(treeWidget) != "sent": continue - if treeWidget in [self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans] and self.getCurrentAccount(treeWidget) != toAddress: + if treeWidget in ( + self.ui.treeWidgetSubscriptions, + self.ui.treeWidgetChans + ) and self.getCurrentAccount(treeWidget) != toAddress: continue for i in range(sent.rowCount()): @@ -1817,7 +1846,11 @@ class MyForm(settingsmixin.SMainWindow): def updateSentItemStatusByAckdata(self, ackdata, textToDisplay): if type(ackdata) is str: ackdata = QtCore.QByteArray(ackdata) - for sent in [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans]: + for sent in ( + self.ui.tableWidgetInbox, + self.ui.tableWidgetInboxSubscriptions, + self.ui.tableWidgetInboxChans + ): treeWidget = self.widgetConvert(sent) if self.getCurrentFolder(treeWidget) != "sent": continue @@ -1925,11 +1958,13 @@ class MyForm(settingsmixin.SMainWindow): newRows[address] = [label, AccountMixin.NORMAL] completerList = [] - for address in sorted(oldRows, key = lambda x: oldRows[x][2], reverse = True): - if address in newRows: - completerList.append(unicode(newRows[address][0], encoding="UTF-8") + " <" + address + ">") - newRows.pop(address) - else: + for address in sorted( + oldRows, key=lambda x: oldRows[x][2], reverse=True + ): + try: + completerList.append( + newRows.pop(address)[0] + " <" + address + ">") + except KeyError: self.ui.tableWidgetAddressBook.removeRow(oldRows[address][2]) for address in newRows: addRow(address, newRows[address][0], newRows[address][1]) @@ -2002,11 +2037,14 @@ class MyForm(settingsmixin.SMainWindow): acct = accountClass(fromAddress) - if sendMessageToPeople: # To send a message to specific people (rather than broadcast) - toAddressesList = [s.strip() - for s in toAddresses.replace(',', ';').split(';')] - toAddressesList = list(set( - toAddressesList)) # remove duplicate addresses. If the user has one address with a BM- and the same address without the BM-, this will not catch it. They'll send the message to the person twice. + # To send a message to specific people (rather than broadcast) + if sendMessageToPeople: + toAddressesList = set([ + s.strip() for s in toAddresses.replace(',', ';').split(';') + ]) + # remove duplicate addresses. If the user has one address + # with a BM- and the same address without the BM-, this will + # not catch it. They'll send the message to the person twice. for toAddress in toAddressesList: if toAddress != '': # label plus address @@ -2213,7 +2251,7 @@ class MyForm(settingsmixin.SMainWindow): '''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', *t) toLabel = str_broadcast_subscribers - + self.displayNewSentMessage( toAddress, toLabel, fromAddress, subject, message, ackdata) @@ -2314,54 +2352,88 @@ class MyForm(settingsmixin.SMainWindow): # receives a message to an address that is acting as a # pseudo-mailing-list. The message will be broadcast out. This function # puts the message on the 'Sent' tab. - def displayNewSentMessage(self, toAddress, toLabel, fromAddress, subject, message, ackdata): + def displayNewSentMessage( + self, toAddress, toLabel, fromAddress, subject, + message, ackdata): acct = accountClass(fromAddress) acct.parseMessage(toAddress, fromAddress, subject, message) tab = -1 - for sent in [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans]: + for sent in ( + self.ui.tableWidgetInbox, + self.ui.tableWidgetInboxSubscriptions, + self.ui.tableWidgetInboxChans + ): tab += 1 if tab == 1: tab = 2 treeWidget = self.widgetConvert(sent) if self.getCurrentFolder(treeWidget) != "sent": continue - if treeWidget == self.ui.treeWidgetYourIdentities and self.getCurrentAccount(treeWidget) not in (fromAddress, None, False): + if treeWidget == self.ui.treeWidgetYourIdentities \ + and self.getCurrentAccount(treeWidget) not in ( + fromAddress, None, False): continue - elif treeWidget in [self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans] and self.getCurrentAccount(treeWidget) != toAddress: + elif treeWidget in ( + self.ui.treeWidgetSubscriptions, + self.ui.treeWidgetChans + ) and self.getCurrentAccount(treeWidget) != toAddress: continue - elif not helper_search.check_match(toAddress, fromAddress, subject, message, self.getCurrentSearchOption(tab), self.getCurrentSearchLine(tab)): + elif not helper_search.check_match( + toAddress, fromAddress, subject, message, + self.getCurrentSearchOption(tab), + self.getCurrentSearchLine(tab) + ): continue - - self.addMessageListItemSent(sent, toAddress, fromAddress, subject, "msgqueued", ackdata, time.time()) - self.getAccountTextedit(acct).setPlainText(unicode(message, 'utf-8', 'replace')) + + self.addMessageListItemSent( + sent, toAddress, fromAddress, subject, + "msgqueued", ackdata, time.time()) + self.getAccountTextedit(acct).setPlainText(message) sent.setCurrentCell(0, 0) - def displayNewInboxMessage(self, inventoryHash, toAddress, fromAddress, subject, message): - if toAddress == str_broadcast_subscribers: - acct = accountClass(fromAddress) - else: - acct = accountClass(toAddress) + def displayNewInboxMessage( + self, inventoryHash, toAddress, fromAddress, subject, message): + acct = accountClass( + fromAddress if toAddress == str_broadcast_subscribers + else toAddress + ) inbox = self.getAccountMessagelist(acct) - ret = None + ret = treeWidget = None tab = -1 - for treeWidget in [self.ui.treeWidgetYourIdentities, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans]: + for treeWidget in ( + self.ui.treeWidgetYourIdentities, + self.ui.treeWidgetSubscriptions, + self.ui.treeWidgetChans + ): tab += 1 if tab == 1: tab = 2 - tableWidget = self.widgetConvert(treeWidget) - if not helper_search.check_match(toAddress, fromAddress, subject, message, self.getCurrentSearchOption(tab), self.getCurrentSearchLine(tab)): + if not helper_search.check_match( + toAddress, fromAddress, subject, message, + self.getCurrentSearchOption(tab), + self.getCurrentSearchLine(tab) + ): continue - if tableWidget == inbox and self.getCurrentAccount(treeWidget) == acct.address and self.getCurrentFolder(treeWidget) in ["inbox", None]: - ret = self.addMessageListItemInbox(inbox, "inbox", inventoryHash, toAddress, fromAddress, subject, time.time(), 0) - elif treeWidget == self.ui.treeWidgetYourIdentities and self.getCurrentAccount(treeWidget) is None and self.getCurrentFolder(treeWidget) in ["inbox", "new", None]: - ret = self.addMessageListItemInbox(tableWidget, "inbox", inventoryHash, toAddress, fromAddress, subject, time.time(), 0) + tableWidget = self.widgetConvert(treeWidget) + current_account = self.getCurrentAccount(treeWidget) + current_folder = self.getCurrentFolder(treeWidget) + # pylint: disable=too-many-boolean-expressions + if ((tableWidget == inbox + and current_account == acct.address + and current_folder in ("inbox", None)) + or (treeWidget == self.ui.treeWidgetYourIdentities + and current_account is None + and current_folder in ("inbox", "new", None))): + ret = self.addMessageListItemInbox( + tableWidget, toAddress, fromAddress, subject, + inventoryHash, time.time(), False) + if ret is None: acct.parseMessage(toAddress, fromAddress, subject, "") else: acct = ret - # pylint:disable=undefined-loop-variable self.propagateUnreadCount(widget=treeWidget if ret else None) - if BMConfigParser().getboolean( + if BMConfigParser().safeGetBoolean( 'bitmessagesettings', 'showtraynotifications'): self.notifierShow( _translate("MainWindow", "New Message"), @@ -2369,16 +2441,22 @@ class MyForm(settingsmixin.SMainWindow): unicode(acct.fromLabel, 'utf-8')), sound.SOUND_UNKNOWN ) - if self.getCurrentAccount() is not None and ((self.getCurrentFolder(treeWidget) != "inbox" and self.getCurrentFolder(treeWidget) is not None) or self.getCurrentAccount(treeWidget) != acct.address): - # Ubuntu should notify of new message irespective of + if self.getCurrentAccount() is not None and ( + (self.getCurrentFolder(treeWidget) != "inbox" + and self.getCurrentFolder(treeWidget) is not None) + or self.getCurrentAccount(treeWidget) != acct.address): + # Ubuntu should notify of new message irrespective of # whether it's in current message list or not self.indicatorUpdate(True, to_label=acct.toLabel) - # cannot find item to pass here ): - if hasattr(acct, "feedback") \ - and acct.feedback != GatewayAccount.ALL_OK: - if acct.feedback == GatewayAccount.REGISTRATION_DENIED: - dialogs.EmailGatewayDialog( - self, BMConfigParser(), acct).exec_() + + try: + if acct.feedback != GatewayAccount.ALL_OK: + if acct.feedback == GatewayAccount.REGISTRATION_DENIED: + dialogs.EmailGatewayDialog( + self, BMConfigParser(), acct).exec_() + # possible other branches? + except AttributeError: + pass def click_pushButtonAddAddressBook(self, dialog=None): if not dialog: @@ -2609,10 +2687,8 @@ class MyForm(settingsmixin.SMainWindow): ) + "\n\n" + _translate( "MainWindow", "Wait until these tasks finish?"), - QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | - QtGui.QMessageBox.Cancel, - QtGui.QMessageBox.Cancel - ) + QtGui.QMessageBox.Yes | QtGui.QMessageBox.No + | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) if reply == QtGui.QMessageBox.No: waitForPow = False elif reply == QtGui.QMessageBox.Cancel: @@ -2629,10 +2705,8 @@ class MyForm(settingsmixin.SMainWindow): " synchronisation finishes?", None, QtCore.QCoreApplication.CodecForTr, pendingDownload() ), - QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | - QtGui.QMessageBox.Cancel, - QtGui.QMessageBox.Cancel - ) + QtGui.QMessageBox.Yes | QtGui.QMessageBox.No + | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) if reply == QtGui.QMessageBox.Yes: self.wait = waitForSync = True elif reply == QtGui.QMessageBox.Cancel: @@ -2648,10 +2722,8 @@ class MyForm(settingsmixin.SMainWindow): " quit now, it may cause delivery delays. Wait until" " connected and the synchronisation finishes?" ), - QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | - QtGui.QMessageBox.Cancel, - QtGui.QMessageBox.Cancel - ) + QtGui.QMessageBox.Yes | QtGui.QMessageBox.No + | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) if reply == QtGui.QMessageBox.Yes: waitForConnection = True self.wait = waitForSync = True @@ -2845,13 +2917,13 @@ class MyForm(settingsmixin.SMainWindow): # Format predefined text on message reply. def quoted_text(self, message): if not BMConfigParser().safeGetBoolean('bitmessagesettings', 'replybelow'): - return '\n\n------------------------------------------------------\n' + message + return '\n\n------------------------------------------------------\n' + message + + quoteWrapper = textwrap.TextWrapper( + replace_whitespace=False, initial_indent='> ', + subsequent_indent='> ', break_long_words=False, + break_on_hyphens=False) - quoteWrapper = textwrap.TextWrapper(replace_whitespace = False, - initial_indent = '> ', - subsequent_indent = '> ', - break_long_words = False, - break_on_hyphens = False) def quote_line(line): # Do quote empty lines. if line == '' or line.isspace(): @@ -2864,18 +2936,20 @@ class MyForm(settingsmixin.SMainWindow): return quoteWrapper.fill(line) return '\n'.join([quote_line(l) for l in message.splitlines()]) + '\n\n' - def setSendFromComboBox(self, address = None): + def setSendFromComboBox(self, address=None): if address is None: messagelist = self.getCurrentMessagelist() - if messagelist: - currentInboxRow = messagelist.currentRow() - address = messagelist.item( - currentInboxRow, 0).address - for box in [self.ui.comboBoxSendFrom, self.ui.comboBoxSendFromBroadcast]: - listOfAddressesInComboBoxSendFrom = [str(box.itemData(i).toPyObject()) for i in range(box.count())] - if address in listOfAddressesInComboBoxSendFrom: - currentIndex = listOfAddressesInComboBoxSendFrom.index(address) - box.setCurrentIndex(currentIndex) + if not messagelist: + return + currentInboxRow = messagelist.currentRow() + address = messagelist.item(currentInboxRow, 0).address + for box in ( + self.ui.comboBoxSendFrom, self.ui.comboBoxSendFromBroadcast + ): + for i in range(box.count()): + if str(box.itemData(i).toPyObject()) == address: + box.setCurrentIndex(i) + break else: box.setCurrentIndex(0) @@ -2991,7 +3065,7 @@ class MyForm(settingsmixin.SMainWindow): quotedText = self.quoted_text( unicode(messageAtCurrentInboxRow, 'utf-8', 'replace')) widget['message'].setPlainText(quotedText) - if acct.subject[0:3] in ['Re:', 'RE:']: + if acct.subject[0:3] in ('Re:', 'RE:'): widget['subject'].setText( tableWidget.item(currentInboxRow, 2).label) else: @@ -3045,11 +3119,17 @@ class MyForm(settingsmixin.SMainWindow): "Error: You cannot add the same address to your blacklist" " twice. Try renaming the existing one if you want.")) - def deleteRowFromMessagelist(self, row = None, inventoryHash = None, ackData = None, messageLists = None): + def deleteRowFromMessagelist( + self, row=None, inventoryHash=None, ackData=None, messageLists=None + ): if messageLists is None: - messageLists = (self.ui.tableWidgetInbox, self.ui.tableWidgetInboxChans, self.ui.tableWidgetInboxSubscriptions) + messageLists = ( + self.ui.tableWidgetInbox, + self.ui.tableWidgetInboxChans, + self.ui.tableWidgetInboxSubscriptions + ) elif type(messageLists) not in (list, tuple): - messageLists = (messageLists) + messageLists = (messageLists,) for messageList in messageLists: if row is not None: inventoryHash = str(messageList.item(row, 3).data( @@ -3071,17 +3151,18 @@ class MyForm(settingsmixin.SMainWindow): return currentRow = 0 folder = self.getCurrentFolder() - shifted = QtGui.QApplication.queryKeyboardModifiers() & QtCore.Qt.ShiftModifier - tableWidget.setUpdatesEnabled(False); - inventoryHashesToTrash = [] + shifted = QtGui.QApplication.queryKeyboardModifiers() \ + & QtCore.Qt.ShiftModifier + tableWidget.setUpdatesEnabled(False) + inventoryHashesToTrash = set() # ranges in reversed order - for r in sorted(tableWidget.selectedRanges(), key=lambda r: r.topRow())[::-1]: - for i in range(r.bottomRow()-r.topRow()+1): + for r in sorted( + tableWidget.selectedRanges(), key=lambda r: r.topRow() + )[::-1]: + for i in range(r.bottomRow() - r.topRow() + 1): inventoryHashToTrash = str(tableWidget.item( - r.topRow()+i, 3).data(QtCore.Qt.UserRole).toPyObject()) - if inventoryHashToTrash in inventoryHashesToTrash: - continue - inventoryHashesToTrash.append(inventoryHashToTrash) + r.topRow() + i, 3).data(QtCore.Qt.UserRole).toPyObject()) + inventoryHashesToTrash.add(inventoryHashToTrash) currentRow = r.topRow() self.getCurrentMessageTextedit().setText("") tableWidget.model().removeRows(r.topRow(), r.bottomRow()-r.topRow()+1) @@ -3101,22 +3182,24 @@ class MyForm(settingsmixin.SMainWindow): return currentRow = 0 tableWidget.setUpdatesEnabled(False) - inventoryHashesToTrash = [] + inventoryHashesToTrash = set() # ranges in reversed order - for r in sorted(tableWidget.selectedRanges(), key=lambda r: r.topRow())[::-1]: - for i in range(r.bottomRow()-r.topRow()+1): + for r in sorted( + tableWidget.selectedRanges(), key=lambda r: r.topRow() + )[::-1]: + for i in range(r.bottomRow() - r.topRow() + 1): inventoryHashToTrash = str(tableWidget.item( - r.topRow()+i, 3).data(QtCore.Qt.UserRole).toPyObject()) - if inventoryHashToTrash in inventoryHashesToTrash: - continue - inventoryHashesToTrash.append(inventoryHashToTrash) + r.topRow() + i, 3).data(QtCore.Qt.UserRole).toPyObject()) + inventoryHashesToTrash.add(inventoryHashToTrash) currentRow = r.topRow() self.getCurrentMessageTextedit().setText("") - tableWidget.model().removeRows(r.topRow(), r.bottomRow()-r.topRow()+1) + tableWidget.model().removeRows( + r.topRow(), r.bottomRow() - r.topRow() + 1) tableWidget.selectRow(0 if currentRow == 0 else currentRow - 1) idCount = len(inventoryHashesToTrash) - sqlExecuteChunked('''UPDATE inbox SET folder='inbox' WHERE msgid IN({0})''', - idCount, *inventoryHashesToTrash) + sqlExecuteChunked( + "UPDATE inbox SET folder='inbox' WHERE msgid IN({0})", + idCount, *inventoryHashesToTrash) tableWidget.selectRow(0 if currentRow == 0 else currentRow - 1) tableWidget.setUpdatesEnabled(True) self.propagateUnreadCount() @@ -3387,13 +3470,13 @@ class MyForm(settingsmixin.SMainWindow): return None def getCurrentTreeWidget(self): - currentIndex = self.ui.tabWidget.currentIndex(); - treeWidgetList = [ + currentIndex = self.ui.tabWidget.currentIndex() + treeWidgetList = ( self.ui.treeWidgetYourIdentities, False, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans - ] + ) if currentIndex >= 0 and currentIndex < len(treeWidgetList): return treeWidgetList[currentIndex] else: @@ -3411,18 +3494,16 @@ class MyForm(settingsmixin.SMainWindow): return self.ui.treeWidgetYourIdentities def getCurrentMessagelist(self): - currentIndex = self.ui.tabWidget.currentIndex(); - messagelistList = [ + currentIndex = self.ui.tabWidget.currentIndex() + messagelistList = ( self.ui.tableWidgetInbox, False, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans, - ] + ) if currentIndex >= 0 and currentIndex < len(messagelistList): return messagelistList[currentIndex] - else: - return False - + def getAccountMessagelist(self, account): try: if account.type == AccountMixin.CHAN: @@ -3447,16 +3528,14 @@ class MyForm(settingsmixin.SMainWindow): def getCurrentMessageTextedit(self): currentIndex = self.ui.tabWidget.currentIndex() - messagelistList = [ + messagelistList = ( self.ui.textEditInboxMessage, False, self.ui.textEditInboxMessageSubscriptions, self.ui.textEditInboxMessageChans, - ] + ) if currentIndex >= 0 and currentIndex < len(messagelistList): return messagelistList[currentIndex] - else: - return False def getAccountTextedit(self, account): try: @@ -3472,29 +3551,26 @@ class MyForm(settingsmixin.SMainWindow): def getCurrentSearchLine(self, currentIndex=None, retObj=False): if currentIndex is None: currentIndex = self.ui.tabWidget.currentIndex() - messagelistList = [ + messagelistList = ( self.ui.inboxSearchLineEdit, False, self.ui.inboxSearchLineEditSubscriptions, self.ui.inboxSearchLineEditChans, - ] + ) if currentIndex >= 0 and currentIndex < len(messagelistList): - if retObj: - return messagelistList[currentIndex] - else: - return messagelistList[currentIndex].text().toUtf8().data() - else: - return None + return ( + messagelistList[currentIndex] if retObj + else messagelistList[currentIndex].text().toUtf8().data()) def getCurrentSearchOption(self, currentIndex=None): if currentIndex is None: currentIndex = self.ui.tabWidget.currentIndex() - messagelistList = [ + messagelistList = ( self.ui.inboxSearchOption, False, self.ui.inboxSearchOptionSubscriptions, self.ui.inboxSearchOptionChans, - ] + ) if currentIndex >= 0 and currentIndex < len(messagelistList): return messagelistList[currentIndex].currentText() @@ -3600,12 +3676,11 @@ class MyForm(settingsmixin.SMainWindow): tableWidget = self.getCurrentMessagelist() currentColumn = tableWidget.currentColumn() currentRow = tableWidget.currentRow() - if currentColumn not in [0, 1, 2]: # to, from, subject - if self.getCurrentFolder() == "sent": - currentColumn = 0 - else: - currentColumn = 1 - if self.getCurrentFolder() == "sent": + currentFolder = self.getCurrentFolder() + if currentColumn not in (0, 1, 2): # to, from, subject + currentColumn = 0 if currentFolder == "sent" else 1 + + if currentFolder == "sent": myAddress = tableWidget.item(currentRow, 1).data(QtCore.Qt.UserRole) otherAddress = tableWidget.item(currentRow, 0).data(QtCore.Qt.UserRole) else: @@ -3618,18 +3693,18 @@ class MyForm(settingsmixin.SMainWindow): text = str(tableWidget.item(currentRow, currentColumn).label) else: text = tableWidget.item(currentRow, currentColumn).data(QtCore.Qt.UserRole) - text = unicode(str(text), 'utf-8', 'ignore') + clipboard = QtGui.QApplication.clipboard() clipboard.setText(text) - #set avatar functions + # set avatar functions def on_action_TreeWidgetSetAvatar(self): address = self.getCurrentAccount() self.setAvatar(address) def on_action_AddressBookSetAvatar(self): self.on_action_SetAvatar(self.ui.tableWidgetAddressBook) - + def on_action_SetAvatar(self, thisTableWidget): currentRow = thisTableWidget.currentRow() addressAtCurrentRow = thisTableWidget.item( @@ -3639,19 +3714,36 @@ class MyForm(settingsmixin.SMainWindow): thisTableWidget.item( currentRow, 0).setIcon(avatarize(addressAtCurrentRow)) + # TODO: reuse utils def setAvatar(self, addressAtCurrentRow): if not os.path.exists(state.appdata + 'avatars/'): os.makedirs(state.appdata + 'avatars/') hash = hashlib.md5(addBMIfNotPresent(addressAtCurrentRow)).hexdigest() - extensions = ['PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', 'PGM', 'PPM', 'TIFF', 'XBM', 'XPM', 'TGA'] - # http://pyqt.sourceforge.net/Docs/PyQt4/qimagereader.html#supportedImageFormats - names = {'BMP':'Windows Bitmap', 'GIF':'Graphic Interchange Format', 'JPG':'Joint Photographic Experts Group', 'JPEG':'Joint Photographic Experts Group', 'MNG':'Multiple-image Network Graphics', 'PNG':'Portable Network Graphics', 'PBM':'Portable Bitmap', 'PGM':'Portable Graymap', 'PPM':'Portable Pixmap', 'TIFF':'Tagged Image File Format', 'XBM':'X11 Bitmap', 'XPM':'X11 Pixmap', 'SVG':'Scalable Vector Graphics', 'TGA':'Targa Image Format'} + extensions = [ + 'PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', + 'PGM', 'PPM', 'TIFF', 'XBM', 'XPM', 'TGA'] + + names = { + 'BMP': 'Windows Bitmap', + 'GIF': 'Graphic Interchange Format', + 'JPG': 'Joint Photographic Experts Group', + 'JPEG': 'Joint Photographic Experts Group', + 'MNG': 'Multiple-image Network Graphics', + 'PNG': 'Portable Network Graphics', + 'PBM': 'Portable Bitmap', + 'PGM': 'Portable Graymap', + 'PPM': 'Portable Pixmap', + 'TIFF': 'Tagged Image File Format', + 'XBM': 'X11 Bitmap', + 'XPM': 'X11 Pixmap', + 'SVG': 'Scalable Vector Graphics', + 'TGA': 'Targa Image Format'} filters = [] all_images_filter = [] current_files = [] for ext in extensions: - filters += [ names[ext] + ' (*.' + ext.lower() + ')' ] - all_images_filter += [ '*.' + ext.lower() ] + filters += [names[ext] + ' (*.' + ext.lower() + ')'] + all_images_filter += ['*.' + ext.lower()] upper = state.appdata + 'avatars/' + hash + '.' + ext.upper() lower = state.appdata + 'avatars/' + hash + '.' + ext.lower() if os.path.isfile(lower): @@ -3662,28 +3754,34 @@ class MyForm(settingsmixin.SMainWindow): filters[1:1] = ['All files (*.*)'] sourcefile = QtGui.QFileDialog.getOpenFileName( self, _translate("MainWindow", "Set avatar..."), - filter = ';;'.join(filters) + filter=';;'.join(filters) ) # determine the correct filename (note that avatars don't use the suffix) destination = state.appdata + 'avatars/' + hash + '.' + sourcefile.split('.')[-1] exists = QtCore.QFile.exists(destination) if sourcefile == '': # ask for removal of avatar - if exists | (len(current_files)>0): - displayMsg = _translate("MainWindow", "Do you really want to remove this avatar?") + if exists | (len(current_files) > 0): + displayMsg = _translate( + "MainWindow", "Do you really want to remove this avatar?") overwrite = QtGui.QMessageBox.question( - self, 'Message', displayMsg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) + self, 'Message', displayMsg, + QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) else: overwrite = QtGui.QMessageBox.No else: # ask whether to overwrite old avatar - if exists | (len(current_files)>0): - displayMsg = _translate("MainWindow", "You have already set an avatar for this address. Do you really want to overwrite it?") + if exists | (len(current_files) > 0): + displayMsg = _translate( + "MainWindow", + "You have already set an avatar for this address." + " Do you really want to overwrite it?") overwrite = QtGui.QMessageBox.question( - self, 'Message', displayMsg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) + self, 'Message', displayMsg, + QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) else: overwrite = QtGui.QMessageBox.No - + # copy the image file to the appdata folder if (not exists) | (overwrite == QtGui.QMessageBox.Yes): if overwrite == QtGui.QMessageBox.Yes: @@ -3885,8 +3983,8 @@ class MyForm(settingsmixin.SMainWindow): if 0 < len(text) < 3: return messagelist = self.getCurrentMessagelist() - searchOption = self.getCurrentSearchOption() if messagelist: + searchOption = self.getCurrentSearchOption() account = self.getCurrentAccount() folder = self.getCurrentFolder() self.loadMessagelist( @@ -3896,12 +3994,12 @@ class MyForm(settingsmixin.SMainWindow): logger.debug("Search return pressed") searchLine = self.getCurrentSearchLine() messagelist = self.getCurrentMessagelist() - if len(str(searchLine)) < 3: + if messagelist and len(str(searchLine)) < 3: searchOption = self.getCurrentSearchOption() account = self.getCurrentAccount() folder = self.getCurrentFolder() - self.loadMessagelist(messagelist, account, folder, searchOption, searchLine) - if messagelist: + self.loadMessagelist( + messagelist, account, folder, searchOption, searchLine) messagelist.setFocus() def treeWidgetItemClicked(self): @@ -3927,7 +4025,7 @@ class MyForm(settingsmixin.SMainWindow): if (not isinstance(item, Ui_AddressWidget)) or (not self.getCurrentTreeWidget()) or self.getCurrentTreeWidget().currentItem() is None: return # not visible - if (not self.getCurrentItem()) or (not isinstance (self.getCurrentItem(), Ui_AddressWidget)): + if (not self.getCurrentItem()) or (not isinstance(self.getCurrentItem(), Ui_AddressWidget)): return # only currently selected item if item.address != self.getCurrentAccount(): @@ -3935,7 +4033,7 @@ class MyForm(settingsmixin.SMainWindow): # "All accounts" can't be renamed if item.type == AccountMixin.ALL: return - + newLabel = unicode(item.text(0), 'utf-8', 'ignore') oldLabel = item.defaultLabel() @@ -3960,12 +4058,12 @@ class MyForm(settingsmixin.SMainWindow): self.recurDepth -= 1 def tableWidgetInboxItemClicked(self): - folder = self.getCurrentFolder() messageTextedit = self.getCurrentMessageTextedit() if not messageTextedit: return msgid = self.getCurrentMessageId() + folder = self.getCurrentFolder() if msgid: queryreturn = sqlQuery( '''SELECT message FROM %s WHERE %s=?''' % ( @@ -4026,12 +4124,15 @@ class MyForm(settingsmixin.SMainWindow): self.rerenderAddressBook() def updateStatusBar(self, data): - if type(data) is tuple or type(data) is list: - option = data[1] - message = data[0] - else: + try: + option, message = data + except ValueError: option = 0 message = data + except TypeError: + logger.debug( + 'Invalid argument for updateStatusBar!', exc_info=True) + if message != "": logger.info('Status bar: ' + message) @@ -4047,11 +4148,11 @@ class MyForm(settingsmixin.SMainWindow): # Check to see whether we can connect to namecoin. # Hide the 'Fetch Namecoin ID' button if we can't. if BMConfigParser().safeGetBoolean( - 'bitmessagesettings', 'dontconnect' + 'bitmessagesettings', 'dontconnect' ) or self.namecoin.test()[0] == 'failed': logger.warning( - 'There was a problem testing for a Namecoin daemon. Hiding the' - ' Fetch Namecoin ID button') + 'There was a problem testing for a Namecoin daemon.' + ' Hiding the Fetch Namecoin ID button') self.ui.pushButtonFetchNamecoinID.hide() else: self.ui.pushButtonFetchNamecoinID.show() @@ -4161,7 +4262,7 @@ def run(): # myapp.showMigrationWizard(BMConfigParser().get('bitmessagesettings', 'mailchuck')) # except: # myapp.showMigrationWizard(0) - + # only show after wizards and connect dialogs have completed if not BMConfigParser().getboolean('bitmessagesettings', 'startintray'): myapp.show() diff --git a/src/bitmessageqt/languagebox.py b/src/bitmessageqt/languagebox.py index 9032cc42..b97e61d3 100644 --- a/src/bitmessageqt/languagebox.py +++ b/src/bitmessageqt/languagebox.py @@ -1,32 +1,41 @@ import glob import os + from PyQt4 import QtCore, QtGui -from bmconfigparser import BMConfigParser import paths +from bmconfigparser import BMConfigParser + class LanguageBox(QtGui.QComboBox): - languageName = {"system": "System Settings", "eo": "Esperanto", "en_pirate": "Pirate English"} - def __init__(self, parent = None): + languageName = { + "system": "System Settings", "eo": "Esperanto", + "en_pirate": "Pirate English" + } + + def __init__(self, parent=None): super(QtGui.QComboBox, self).__init__(parent) self.populate() def populate(self): self.clear() - localesPath = os.path.join (paths.codePath(), 'translations') - self.addItem(QtGui.QApplication.translate("settingsDialog", "System Settings", "system"), "system") + localesPath = os.path.join(paths.codePath(), 'translations') + self.addItem(QtGui.QApplication.translate( + "settingsDialog", "System Settings", "system"), "system") self.setCurrentIndex(0) self.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically) - for translationFile in sorted(glob.glob(os.path.join(localesPath, "bitmessage_*.qm"))): - localeShort = os.path.split(translationFile)[1].split("_", 1)[1][:-3] - locale = QtCore.QLocale(QtCore.QString(localeShort)) - + for translationFile in sorted( + glob.glob(os.path.join(localesPath, "bitmessage_*.qm")) + ): + localeShort = \ + os.path.split(translationFile)[1].split("_", 1)[1][:-3] if localeShort in LanguageBox.languageName: - self.addItem(LanguageBox.languageName[localeShort], localeShort) - elif locale.nativeLanguageName() == "": - self.addItem(localeShort, localeShort) + self.addItem( + LanguageBox.languageName[localeShort], localeShort) else: - self.addItem(locale.nativeLanguageName(), localeShort) + locale = QtCore.QLocale(localeShort) + self.addItem( + locale.nativeLanguageName() or localeShort, localeShort) configuredLocale = BMConfigParser().safeGet( 'bitmessagesettings', 'userlocale', "system") diff --git a/src/bitmessageqt/utils.py b/src/bitmessageqt/utils.py index 564dbc8b..e118f487 100644 --- a/src/bitmessageqt/utils.py +++ b/src/bitmessageqt/utils.py @@ -1,13 +1,16 @@ -from PyQt4 import QtGui import hashlib import os + +from PyQt4 import QtGui + +import state from addresses import addBMIfNotPresent from bmconfigparser import BMConfigParser -import state str_broadcast_subscribers = '[Broadcast subscribers]' str_chan = '[chan]' + def identiconize(address): size = 48 @@ -28,32 +31,40 @@ def identiconize(address): # the identicons to decrease the risk of attacks where someone creates # an address to mimic someone else's identicon. identiconsuffix = BMConfigParser().get('bitmessagesettings', 'identiconsuffix') - if (identicon_lib[:len('qidenticon')] == 'qidenticon'): - # print identicon_lib + if identicon_lib[:len('qidenticon')] == 'qidenticon': # originally by: # :Author:Shin Adachi # Licesensed under FreeBSD License. # stripped from PIL and uses QT instead (by sendiulo, same license) import qidenticon - hash = hashlib.md5(addBMIfNotPresent(address)+identiconsuffix).hexdigest() - use_two_colors = (identicon_lib[:len('qidenticon_two')] == 'qidenticon_two') - opacity = int(not((identicon_lib == 'qidenticon_x') | (identicon_lib == 'qidenticon_two_x') | (identicon_lib == 'qidenticon_b') | (identicon_lib == 'qidenticon_two_b')))*255 + icon_hash = hashlib.md5( + addBMIfNotPresent(address) + identiconsuffix).hexdigest() + use_two_colors = identicon_lib[:len('qidenticon_two')] == 'qidenticon_two' + opacity = int( + identicon_lib not in ( + 'qidenticon_x', 'qidenticon_two_x', + 'qidenticon_b', 'qidenticon_two_b' + )) * 255 penwidth = 0 - image = qidenticon.render_identicon(int(hash, 16), size, use_two_colors, opacity, penwidth) + image = qidenticon.render_identicon( + int(icon_hash, 16), size, use_two_colors, opacity, penwidth) # filename = './images/identicons/'+hash+'.png' # image.save(filename) idcon = QtGui.QIcon() idcon.addPixmap(image, QtGui.QIcon.Normal, QtGui.QIcon.Off) return idcon elif identicon_lib == 'pydenticon': - # print identicon_lib - # Here you could load pydenticon.py (just put it in the "src" folder of your Bitmessage source) + # Here you could load pydenticon.py + # (just put it in the "src" folder of your Bitmessage source) from pydenticon import Pydenticon # It is not included in the source, because it is licensed under GPLv3 # GPLv3 is a copyleft license that would influence our licensing - # Find the source here: http://boottunes.googlecode.com/svn-history/r302/trunk/src/pydenticon.py - # note that it requires PIL to be installed: http://www.pythonware.com/products/pil/ - idcon_render = Pydenticon(addBMIfNotPresent(address)+identiconsuffix, size*3) + # Find the source here: + # https://github.com/azaghal/pydenticon + # note that it requires pillow (or PIL) to be installed: + # https://python-pillow.org/ + idcon_render = Pydenticon( + addBMIfNotPresent(address) + identiconsuffix, size * 3) rendering = idcon_render._render() data = rendering.convert("RGBA").tostring("raw", "RGBA") qim = QtGui.QImage(data, size, size, QtGui.QImage.Format_ARGB32) @@ -62,32 +73,31 @@ def identiconize(address): idcon.addPixmap(pix, QtGui.QIcon.Normal, QtGui.QIcon.Off) return idcon + def avatarize(address): """ - loads a supported image for the given address' hash form 'avatars' folder - falls back to default avatar if 'default.*' file exists - falls back to identiconize(address) + Loads a supported image for the given address' hash form 'avatars' folder + falls back to default avatar if 'default.*' file exists + falls back to identiconize(address) """ idcon = QtGui.QIcon() - hash = hashlib.md5(addBMIfNotPresent(address)).hexdigest() - str_broadcast_subscribers = '[Broadcast subscribers]' + icon_hash = hashlib.md5(addBMIfNotPresent(address)).hexdigest() if address == str_broadcast_subscribers: # don't hash [Broadcast subscribers] - hash = address - # http://pyqt.sourceforge.net/Docs/PyQt4/qimagereader.html#supportedImageFormats - # print QImageReader.supportedImageFormats () + icon_hash = address + # https://www.riverbankcomputing.com/static/Docs/PyQt4/qimagereader.html#supportedImageFormats # QImageReader.supportedImageFormats () - extensions = ['PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', 'PGM', 'PPM', 'TIFF', 'XBM', 'XPM', 'TGA'] + extensions = [ + 'PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', 'PGM', 'PPM', + 'TIFF', 'XBM', 'XPM', 'TGA'] # try to find a specific avatar for ext in extensions: - lower_hash = state.appdata + 'avatars/' + hash + '.' + ext.lower() - upper_hash = state.appdata + 'avatars/' + hash + '.' + ext.upper() + lower_hash = state.appdata + 'avatars/' + icon_hash + '.' + ext.lower() + upper_hash = state.appdata + 'avatars/' + icon_hash + '.' + ext.upper() if os.path.isfile(lower_hash): - # print 'found avatar of ', address idcon.addFile(lower_hash) return idcon elif os.path.isfile(upper_hash): - # print 'found avatar of ', address idcon.addFile(upper_hash) return idcon # if we haven't found any, try to find a default avatar From 08ff39e1ff58bf67b7cdf46c2a6aa69f98130acd Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 8 Feb 2019 18:50:07 +0200 Subject: [PATCH 17/28] Moved myTableWidgetItem to foldertree.MessageList_TimeWidget --- src/bitmessageqt/__init__.py | 172 ++++++++++++--------------------- src/bitmessageqt/foldertree.py | 71 +++++++++----- 2 files changed, 111 insertions(+), 132 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 50d5cf4d..f2e93ad3 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -31,7 +31,8 @@ from migrationwizard import Ui_MigrationWizard from foldertree import ( AccountMixin, Ui_FolderWidget, Ui_AddressWidget, Ui_SubscriptionWidget, MessageList_AddressWidget, MessageList_SubjectWidget, - Ui_AddressBookWidgetItemLabel, Ui_AddressBookWidgetItemAddress) + Ui_AddressBookWidgetItemLabel, Ui_AddressBookWidgetItemAddress, + MessageList_TimeWidget) import settingsmixin import support from helper_ackPayload import genAckPayload @@ -970,40 +971,30 @@ class MyForm(settingsmixin.SMainWindow): Switch unread for item of msgid and related items in other STableWidgets "All Accounts" and "Chans" """ - related = [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxChans] + status = widget.item(row, 0).unread + if status != unread: + return + + widgets = [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxChans] + rrow = None try: - related.remove(widget) - related = related.pop() + widgets.remove(widget) + related = widgets.pop() except ValueError: - rrow = None - related = [] + pass else: # maybe use instead: # rrow = related.row(msgid), msgid should be QTableWidgetItem # related = related.findItems(msgid, QtCore.Qt.MatchExactly), # returns an empty list - for rrow in xrange(related.rowCount()): - if msgid == str(related.item(rrow, 3).data( - QtCore.Qt.UserRole).toPyObject()): + for rrow in range(related.rowCount()): + if related.item(rrow, 3).data() == msgid: break - else: - rrow = None - status = widget.item(row, 0).unread - if status == unread: - font = QtGui.QFont() - font.setBold(not status) - widget.item(row, 3).setFont(font) - for col in (0, 1, 2): - widget.item(row, col).setUnread(not status) - - try: - related.item(rrow, 3).setFont(font) - except (TypeError, AttributeError): - pass - else: - for col in (0, 1, 2): - related.item(rrow, col).setUnread(not status) + for col in range(widget.columnCount()): + widget.item(row, col).setUnread(not status) + if rrow: + related.item(rrow, col).setUnread(not status) # Here we need to update unread count for: # - all widgets if there is no args @@ -1100,11 +1091,6 @@ class MyForm(settingsmixin.SMainWindow): acct = accountClass(fromAddress) or BMAccount(fromAddress) acct.parseMessage(toAddress, fromAddress, subject, "") - items = [] - MessageList_AddressWidget(items, str(toAddress), unicode(acct.toLabel, 'utf-8')) - MessageList_AddressWidget(items, str(fromAddress), unicode(acct.fromLabel, 'utf-8')) - MessageList_SubjectWidget(items, str(subject), unicode(acct.subject, 'utf-8', 'replace')) - if status == 'awaitingpubkey': statusText = _translate( "MainWindow", @@ -1162,22 +1148,24 @@ class MyForm(settingsmixin.SMainWindow): statusText = _translate( "MainWindow", "Unknown status: %1 %2").arg(status).arg( l10n.formatTimestamp(lastactiontime)) - newItem = myTableWidgetItem(statusText) - newItem.setToolTip(statusText) - newItem.setData(QtCore.Qt.UserRole, QtCore.QByteArray(ackdata)) - newItem.setData(33, int(lastactiontime)) - newItem.setFlags( - QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) - items.append(newItem) + + items = [ + MessageList_AddressWidget( + toAddress, unicode(acct.toLabel, 'utf-8')), + MessageList_AddressWidget( + fromAddress, unicode(acct.fromLabel, 'utf-8')), + MessageList_SubjectWidget( + str(subject), unicode(acct.subject, 'utf-8', 'replace')), + MessageList_TimeWidget( + statusText, False, lastactiontime, ackdata)] self.addMessageListItem(tableWidget, items) + return acct def addMessageListItemInbox( self, tableWidget, toAddress, fromAddress, subject, msgid, received, read ): - font = QtGui.QFont() - font.setBold(True) if toAddress == str_broadcast_subscribers: acct = accountClass(fromAddress) else: @@ -1185,25 +1173,20 @@ class MyForm(settingsmixin.SMainWindow): if acct is None: acct = BMAccount(fromAddress) acct.parseMessage(toAddress, fromAddress, subject, "") - - items = [] - #to - MessageList_AddressWidget(items, toAddress, unicode(acct.toLabel, 'utf-8'), not read) - # from - MessageList_AddressWidget(items, fromAddress, unicode(acct.fromLabel, 'utf-8'), not read) - # subject - MessageList_SubjectWidget(items, str(subject), unicode(acct.subject, 'utf-8', 'replace'), not read) - # time received - time_item = myTableWidgetItem(l10n.formatTimestamp(received)) - time_item.setToolTip(l10n.formatTimestamp(received)) - time_item.setData(QtCore.Qt.UserRole, QtCore.QByteArray(msgid)) - time_item.setData(33, int(received)) - time_item.setFlags( - QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) - if not read: - time_item.setFont(font) - items.append(time_item) + + items = [ + MessageList_AddressWidget( + toAddress, unicode(acct.toLabel, 'utf-8'), not read), + MessageList_AddressWidget( + fromAddress, unicode(acct.fromLabel, 'utf-8'), not read), + MessageList_SubjectWidget( + str(subject), unicode(acct.subject, 'utf-8', 'replace'), + not read), + MessageList_TimeWidget( + l10n.formatTimestamp(received), not read, received, msgid) + ] self.addMessageListItem(tableWidget, items) + return acct # Load Sent items from database @@ -1855,10 +1838,8 @@ class MyForm(settingsmixin.SMainWindow): if self.getCurrentFolder(treeWidget) != "sent": continue for i in range(sent.rowCount()): - toAddress = sent.item( - i, 0).data(QtCore.Qt.UserRole) - tableAckdata = sent.item( - i, 3).data(QtCore.Qt.UserRole).toPyObject() + toAddress = sent.item(i, 0).data(QtCore.Qt.UserRole) + tableAckdata = sent.item(i, 3).data() status, addressVersionNumber, streamNumber, ripe = decodeAddress( toAddress) if ackdata == tableAckdata: @@ -1882,8 +1863,7 @@ class MyForm(settingsmixin.SMainWindow): ): i = None for i in range(inbox.rowCount()): - if msgid == \ - inbox.item(i, 3).data(QtCore.Qt.UserRole).toPyObject(): + if msgid == inbox.item(i, 3).data(): break else: continue @@ -2609,17 +2589,11 @@ class MyForm(settingsmixin.SMainWindow): if idCount == 0: return - font = QtGui.QFont() - font.setBold(False) - msgids = [] for i in range(0, idCount): - msgids.append(str(tableWidget.item( - i, 3).data(QtCore.Qt.UserRole).toPyObject())) - tableWidget.item(i, 0).setUnread(False) - tableWidget.item(i, 1).setUnread(False) - tableWidget.item(i, 2).setUnread(False) - tableWidget.item(i, 3).setFont(font) + msgids.append(tableWidget.item(i, 3).data()) + for col in xrange(tableWidget.columnCount()): + tableWidget.item(i, col).setUnread(False) markread = sqlExecuteChunked( "UPDATE inbox SET read = 1 WHERE msgid IN({0}) AND read=0", @@ -2890,8 +2864,7 @@ class MyForm(settingsmixin.SMainWindow): # modified = 0 for row in tableWidget.selectedIndexes(): currentRow = row.row() - msgid = str(tableWidget.item( - currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + msgid = tableWidget.item(currentRow, 3).data() msgids.add(msgid) # if not tableWidget.item(currentRow, 0).unread: # modified += 1 @@ -2981,8 +2954,7 @@ class MyForm(settingsmixin.SMainWindow): acct = accountClass(toAddressAtCurrentInboxRow) fromAddressAtCurrentInboxRow = tableWidget.item( currentInboxRow, column_from).address - msgid = str(tableWidget.item( - currentInboxRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + msgid = tableWidget.item(currentInboxRow, 3).data() queryreturn = sqlQuery( "SELECT message FROM inbox WHERE msgid=?", msgid ) or sqlQuery("SELECT message FROM sent WHERE ackdata=?", msgid) @@ -3081,7 +3053,6 @@ class MyForm(settingsmixin.SMainWindow): if not tableWidget: return currentInboxRow = tableWidget.currentRow() - # tableWidget.item(currentRow,1).data(Qt.UserRole).toPyObject() addressAtCurrentInboxRow = tableWidget.item( currentInboxRow, 1).data(QtCore.Qt.UserRole) self.ui.tabWidget.setCurrentIndex( @@ -3095,7 +3066,6 @@ class MyForm(settingsmixin.SMainWindow): if not tableWidget: return currentInboxRow = tableWidget.currentRow() - # tableWidget.item(currentRow,1).data(Qt.UserRole).toPyObject() addressAtCurrentInboxRow = tableWidget.item( currentInboxRow, 1).data(QtCore.Qt.UserRole) recipientAddress = tableWidget.item( @@ -3132,16 +3102,15 @@ class MyForm(settingsmixin.SMainWindow): messageLists = (messageLists,) for messageList in messageLists: if row is not None: - inventoryHash = str(messageList.item(row, 3).data( - QtCore.Qt.UserRole).toPyObject()) + inventoryHash = messageList.item(row, 3).data() messageList.removeRow(row) elif inventoryHash is not None: for i in range(messageList.rowCount() - 1, -1, -1): - if messageList.item(i, 3).data(QtCore.Qt.UserRole).toPyObject() == inventoryHash: + if messageList.item(i, 3).data() == inventoryHash: messageList.removeRow(i) elif ackData is not None: for i in range(messageList.rowCount() - 1, -1, -1): - if messageList.item(i, 3).data(QtCore.Qt.UserRole).toPyObject() == ackData: + if messageList.item(i, 3).data() == ackData: messageList.removeRow(i) # Send item on the Inbox tab to trash @@ -3160,12 +3129,12 @@ class MyForm(settingsmixin.SMainWindow): tableWidget.selectedRanges(), key=lambda r: r.topRow() )[::-1]: for i in range(r.bottomRow() - r.topRow() + 1): - inventoryHashToTrash = str(tableWidget.item( - r.topRow() + i, 3).data(QtCore.Qt.UserRole).toPyObject()) - inventoryHashesToTrash.add(inventoryHashToTrash) + inventoryHashesToTrash.add( + tableWidget.item(r.topRow() + i, 3).data()) currentRow = r.topRow() self.getCurrentMessageTextedit().setText("") - tableWidget.model().removeRows(r.topRow(), r.bottomRow()-r.topRow()+1) + tableWidget.model().removeRows( + r.topRow(), r.bottomRow() - r.topRow() + 1) idCount = len(inventoryHashesToTrash) sqlExecuteChunked( ("DELETE FROM inbox" if folder == "trash" or shifted else @@ -3188,9 +3157,8 @@ class MyForm(settingsmixin.SMainWindow): tableWidget.selectedRanges(), key=lambda r: r.topRow() )[::-1]: for i in range(r.bottomRow() - r.topRow() + 1): - inventoryHashToTrash = str(tableWidget.item( - r.topRow() + i, 3).data(QtCore.Qt.UserRole).toPyObject()) - inventoryHashesToTrash.add(inventoryHashToTrash) + inventoryHashesToTrash.add( + tableWidget.item(r.topRow() + i, 3).data()) currentRow = r.topRow() self.getCurrentMessageTextedit().setText("") tableWidget.model().removeRows( @@ -3217,8 +3185,7 @@ class MyForm(settingsmixin.SMainWindow): subjectAtCurrentInboxRow = '' # Retrieve the message data out of the SQL database - msgid = str(tableWidget.item( - currentInboxRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + msgid = tableWidget.item(currentInboxRow, 3).data() queryreturn = sqlQuery( '''select message from inbox where msgid=?''', msgid) if queryreturn != []: @@ -3246,8 +3213,7 @@ class MyForm(settingsmixin.SMainWindow): shifted = QtGui.QApplication.queryKeyboardModifiers() & QtCore.Qt.ShiftModifier while tableWidget.selectedIndexes() != []: currentRow = tableWidget.selectedIndexes()[0].row() - ackdataToTrash = str(tableWidget.item( - currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + ackdataToTrash = tableWidget.item(currentRow, 3).data() sqlExecute( "DELETE FROM sent" if folder == "trash" or shifted else "UPDATE sent SET folder='trash'" @@ -3520,11 +3486,7 @@ class MyForm(settingsmixin.SMainWindow): if messagelist: currentRow = messagelist.currentRow() if currentRow >= 0: - msgid = str(messagelist.item( - currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) - # data is saved at the 4. column of the table... - return msgid - return False + return messagelist.item(currentRow, 3).data() def getCurrentMessageTextedit(self): currentIndex = self.ui.tabWidget.currentIndex() @@ -3967,8 +3929,7 @@ class MyForm(settingsmixin.SMainWindow): # Check to see if this item is toodifficult and display an additional # menu option (Force Send) if it is. if currentRow >= 0: - ackData = str(self.ui.tableWidgetInbox.item( - currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + ackData = self.ui.tableWidgetInbox.item(currentRow, 3).data() queryreturn = sqlQuery('''SELECT status FROM sent where ackdata=?''', ackData) for row in queryreturn: status, = row @@ -4170,15 +4131,6 @@ class MyForm(settingsmixin.SMainWindow): obj.loadSettings() -# In order for the time columns on the Inbox and Sent tabs to be sorted -# correctly (rather than alphabetically), we need to overload the < -# operator and use this class instead of QTableWidgetItem. -class myTableWidgetItem(QtGui.QTableWidgetItem): - - def __lt__(self, other): - return int(self.data(33).toPyObject()) < int(other.data(33).toPyObject()) - - app = None myapp = None diff --git a/src/bitmessageqt/foldertree.py b/src/bitmessageqt/foldertree.py index 2e7e735f..e6d64427 100644 --- a/src/bitmessageqt/foldertree.py +++ b/src/bitmessageqt/foldertree.py @@ -1,8 +1,8 @@ """ -src/bitmessageqt/foldertree.py -============================== +Folder tree and messagelist widgets definitions. """ -# pylint: disable=too-many-arguments,bad-super-call,attribute-defined-outside-init +# pylint: disable=too-many-arguments,bad-super-call +# pylint: disable=attribute-defined-outside-init from cgi import escape @@ -20,6 +20,8 @@ _translate("MainWindow", "new") _translate("MainWindow", "sent") _translate("MainWindow", "trash") +TimestampRole = QtCore.Qt.UserRole + 1 + class AccountMixin(object): """UI-related functionality for accounts""" @@ -334,13 +336,14 @@ class Ui_SubscriptionWidget(Ui_AddressWidget): class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin): """A common abstract class for Table widget item""" - def __init__(self, parent=None, label=None, unread=False): + def __init__(self, label=None, unread=False): super(QtGui.QTableWidgetItem, self).__init__() self.setLabel(label) self.setUnread(unread) self._setup() - if parent is not None: - parent.append(self) + + def _setup(self): + self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) def setLabel(self, label): """Set object label""" @@ -353,7 +356,7 @@ class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin): def data(self, role): """Return object data (QT UI)""" if role in ( - QtCore.Qt.DisplayRole, QtCore.Qt.EditRole, QtCore.Qt.ToolTipRole + QtCore.Qt.DisplayRole, QtCore.Qt.EditRole, QtCore.Qt.ToolTipRole ): return self.label elif role == QtCore.Qt.FontRole: @@ -367,7 +370,9 @@ class BMAddressWidget(BMTableWidgetItem, AccountMixin): """A common class for Table widget item with account""" def _setup(self): + super(BMAddressWidget, self)._setup() self.setEnabled(True) + self.setType() def _getLabel(self): return self.label @@ -387,14 +392,9 @@ class BMAddressWidget(BMTableWidgetItem, AccountMixin): class MessageList_AddressWidget(BMAddressWidget): """Address item in a messagelist""" - def __init__(self, parent, address=None, label=None, unread=False): + def __init__(self, address=None, label=None, unread=False): self.setAddress(address) - super(MessageList_AddressWidget, self).__init__(parent, label, unread) - - def _setup(self): - self.isEnabled = True - self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) - self.setType() + super(MessageList_AddressWidget, self).__init__(label, unread) def setLabel(self, label=None): """Set label""" @@ -443,12 +443,9 @@ class MessageList_AddressWidget(BMAddressWidget): class MessageList_SubjectWidget(BMTableWidgetItem): """Message list subject item""" - def __init__(self, parent, subject=None, label=None, unread=False): + def __init__(self, subject=None, label=None, unread=False): self.setSubject(subject) - super(MessageList_SubjectWidget, self).__init__(parent, label, unread) - - def _setup(self): - self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) + super(MessageList_SubjectWidget, self).__init__(label, unread) def setSubject(self, subject): """Set subject""" @@ -469,6 +466,37 @@ class MessageList_SubjectWidget(BMTableWidgetItem): return super(QtGui.QTableWidgetItem, self).__lt__(other) +# In order for the time columns on the Inbox and Sent tabs to be sorted +# correctly (rather than alphabetically), we need to overload the < +# operator and use this class instead of QTableWidgetItem. +class MessageList_TimeWidget(BMTableWidgetItem): + """ + A subclass of QTableWidgetItem for received (lastactiontime) field. + '<' operator is overloaded to sort by TimestampRole == 33 + msgid is available by QtCore.Qt.UserRole + """ + + def __init__(self, label=None, unread=False, timestamp=None, msgid=''): + super(MessageList_TimeWidget, self).__init__(label, unread) + self.setData(QtCore.Qt.UserRole, QtCore.QByteArray(msgid)) + self.setData(TimestampRole, int(timestamp)) + + def __lt__(self, other): + return self.data(TimestampRole) < other.data(TimestampRole) + + def data(self, role=QtCore.Qt.UserRole): + """ + Returns expected python types for QtCore.Qt.UserRole and TimestampRole + custom roles and super for any Qt role + """ + data = super(MessageList_TimeWidget, self).data(role) + if role == TimestampRole: + return int(data.toPyObject()) + if role == QtCore.Qt.UserRole: + return str(data.toPyObject()) + return data + + class Ui_AddressBookWidgetItem(BMAddressWidget): """Addressbook item""" # pylint: disable=unused-argument @@ -518,8 +546,8 @@ class Ui_AddressBookWidgetItem(BMAddressWidget): class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem): """Addressbook label item""" def __init__(self, address, label, acc_type): - super(Ui_AddressBookWidgetItemLabel, self).__init__(label, acc_type) self.address = address + super(Ui_AddressBookWidgetItemLabel, self).__init__(label, acc_type) def data(self, role): """Return object data""" @@ -530,9 +558,8 @@ class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem): class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem): """Addressbook address item""" def __init__(self, address, label, acc_type): - super(Ui_AddressBookWidgetItemAddress, self).__init__(address, acc_type) self.address = address - self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) + super(Ui_AddressBookWidgetItemAddress, self).__init__(address, acc_type) def data(self, role): """Return object data""" From f3e432140cfe0f76a3150492d66e9d4408c59a72 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Wed, 14 Nov 2018 12:16:11 +0200 Subject: [PATCH 18/28] Proper quit sequence: close MainWindow and quit the app instead of sys.exit() --- src/bitmessageqt/__init__.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index f2e93ad3..f6dc39bf 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -2804,6 +2804,7 @@ class MyForm(settingsmixin.SMainWindow): QtCore.QEventLoop.AllEvents, 1000 ) shutdown.doCleanShutdown() + self.updateStatusBar(_translate( "MainWindow", "Stopping notifications... %1%").arg(90)) self.tray.hide() @@ -2812,20 +2813,21 @@ class MyForm(settingsmixin.SMainWindow): "MainWindow", "Shutdown imminent... %1%").arg(100)) logger.info("Shutdown complete") - super(MyForm, myapp).close() - # return - sys.exit() + self.close() + # FIXME: rewrite loops with timer instead + if self.wait: + self.destroy() + app.quit() - # window close event def closeEvent(self, event): - self.appIndicatorHide() - + """window close event""" + event.ignore() trayonclose = BMConfigParser().safeGetBoolean( 'bitmessagesettings', 'trayonclose') - - event.ignore() - if not trayonclose: - # quit the application + if trayonclose: + self.appIndicatorHide() + else: + # custom quit method self.quit() def on_action_InboxMessageForceHtml(self): @@ -4219,4 +4221,4 @@ def run(): if not BMConfigParser().getboolean('bitmessagesettings', 'startintray'): myapp.show() - sys.exit(app.exec_()) + app.exec_() From 7830ac8de5fc95078e1ac44d06ba3757fe907c8b Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 20 Nov 2018 14:11:18 +0200 Subject: [PATCH 19/28] RetranslateMixin is not needed in most of dialogs --- src/bitmessageqt/address_dialogs.py | 38 ++++++++++++++--------------- src/bitmessageqt/bitmessageui.py | 2 ++ src/bitmessageqt/dialogs.py | 9 +++---- src/bitmessageqt/networkstatus.py | 19 +++++++++------ src/bitmessageqt/newchandialog.py | 6 ++--- 5 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/bitmessageqt/address_dialogs.py b/src/bitmessageqt/address_dialogs.py index 60c10369..7b73152d 100644 --- a/src/bitmessageqt/address_dialogs.py +++ b/src/bitmessageqt/address_dialogs.py @@ -1,9 +1,7 @@ """ -src/bitmessageqt/address_dialogs.py -=================================== - +Dialogs that work with BM address. """ -# pylint: disable=attribute-defined-outside-init +# pylint: disable=attribute-defined-outside-init,too-few-public-methods import hashlib @@ -14,13 +12,11 @@ import widgets from account import AccountMixin, GatewayAccount, MailchuckAccount, accountClass, getSortedAccounts from addresses import addBMIfNotPresent, decodeAddress, encodeVarint from inventory import Inventory -from retranslateui import RetranslateMixin from tr import _translate class AddressCheckMixin(object): """Base address validation class for QT UI""" - # pylint: disable=too-few-public-methods def __init__(self): self.valid = False @@ -33,7 +29,9 @@ class AddressCheckMixin(object): pass def addressChanged(self, QString): - """Address validation callback, performs validation and gives feedback""" + """ + Address validation callback, performs validation and gives feedback + """ status, addressVersion, streamNumber, ripe = decodeAddress( str(QString)) self.valid = status == 'success' @@ -102,8 +100,8 @@ class AddressDataDialog(QtGui.QDialog, AddressCheckMixin): super(AddressDataDialog, self).accept() -class AddAddressDialog(AddressDataDialog, RetranslateMixin): - """QDialog for adding a new address, with validation and translation""" +class AddAddressDialog(AddressDataDialog): + """QDialog for adding a new address""" def __init__(self, parent=None, address=None): super(AddAddressDialog, self).__init__(parent) @@ -113,8 +111,8 @@ class AddAddressDialog(AddressDataDialog, RetranslateMixin): self.lineEditAddress.setText(address) -class NewAddressDialog(QtGui.QDialog, RetranslateMixin): - """QDialog for generating a new address, with translation""" +class NewAddressDialog(QtGui.QDialog): + """QDialog for generating a new address""" def __init__(self, parent=None): super(NewAddressDialog, self).__init__(parent) @@ -175,8 +173,8 @@ class NewAddressDialog(QtGui.QDialog, RetranslateMixin): )) -class NewSubscriptionDialog(AddressDataDialog, RetranslateMixin): - """QDialog for subscribing to an address, with validation and translation""" +class NewSubscriptionDialog(AddressDataDialog): + """QDialog for subscribing to an address""" def __init__(self, parent=None): super(NewSubscriptionDialog, self).__init__(parent) @@ -218,8 +216,8 @@ class NewSubscriptionDialog(AddressDataDialog, RetranslateMixin): )) -class RegenerateAddressesDialog(QtGui.QDialog, RetranslateMixin): - """QDialog for regenerating deterministic addresses, with translation""" +class RegenerateAddressesDialog(QtGui.QDialog): + """QDialog for regenerating deterministic addresses""" def __init__(self, parent=None): super(RegenerateAddressesDialog, self).__init__(parent) widgets.load('regenerateaddresses.ui', self) @@ -227,8 +225,10 @@ class RegenerateAddressesDialog(QtGui.QDialog, RetranslateMixin): QtGui.QWidget.resize(self, QtGui.QWidget.sizeHint(self)) -class SpecialAddressBehaviorDialog(QtGui.QDialog, RetranslateMixin): - """QDialog for special address behaviour (e.g. mailing list functionality), with translation""" +class SpecialAddressBehaviorDialog(QtGui.QDialog): + """ + QDialog for special address behaviour (e.g. mailing list functionality) + """ def __init__(self, parent=None, config=None): super(SpecialAddressBehaviorDialog, self).__init__(parent) @@ -294,8 +294,8 @@ class SpecialAddressBehaviorDialog(QtGui.QDialog, RetranslateMixin): self.parent.rerenderMessagelistToLabels() -class EmailGatewayDialog(QtGui.QDialog, RetranslateMixin): - """QDialog for email gateway control, with translation""" +class EmailGatewayDialog(QtGui.QDialog): + """QDialog for email gateway control""" def __init__(self, parent, config=None, account=None): super(EmailGatewayDialog, self).__init__(parent) widgets.load('emailgateway.ui', self) diff --git a/src/bitmessageqt/bitmessageui.py b/src/bitmessageqt/bitmessageui.py index f81bf285..7f2c8c91 100644 --- a/src/bitmessageqt/bitmessageui.py +++ b/src/bitmessageqt/bitmessageui.py @@ -771,6 +771,8 @@ class Ui_MainWindow(object): self.actionRegenerateDeterministicAddresses.setText(_translate("MainWindow", "Regenerate deterministic addresses", None)) self.actionDeleteAllTrashedMessages.setText(_translate("MainWindow", "Delete all trashed messages", None)) self.actionJoinChan.setText(_translate("MainWindow", "Join / Create chan", None)) + self.updateNetworkSwitchMenuLabel() + import bitmessage_icons_rc diff --git a/src/bitmessageqt/dialogs.py b/src/bitmessageqt/dialogs.py index c667edb1..10701500 100644 --- a/src/bitmessageqt/dialogs.py +++ b/src/bitmessageqt/dialogs.py @@ -13,7 +13,6 @@ from address_dialogs import ( SpecialAddressBehaviorDialog ) from newchandialog import NewChanDialog -from retranslateui import RetranslateMixin from settings import SettingsDialog from tr import _translate from version import softwareVersion @@ -27,7 +26,7 @@ __all__ = [ ] -class AboutDialog(QtGui.QDialog, RetranslateMixin): +class AboutDialog(QtGui.QDialog): """The `About` dialog""" def __init__(self, parent=None): super(AboutDialog, self).__init__(parent) @@ -55,7 +54,7 @@ class AboutDialog(QtGui.QDialog, RetranslateMixin): self.setFixedSize(QtGui.QWidget.sizeHint(self)) -class IconGlossaryDialog(QtGui.QDialog, RetranslateMixin): +class IconGlossaryDialog(QtGui.QDialog): """The `Icon Glossary` dialog, explaining the status icon colors""" def __init__(self, parent=None, config=None): super(IconGlossaryDialog, self).__init__(parent) @@ -71,7 +70,7 @@ class IconGlossaryDialog(QtGui.QDialog, RetranslateMixin): self.setFixedSize(QtGui.QWidget.sizeHint(self)) -class HelpDialog(QtGui.QDialog, RetranslateMixin): +class HelpDialog(QtGui.QDialog): """The `Help` dialog""" def __init__(self, parent=None): super(HelpDialog, self).__init__(parent) @@ -79,7 +78,7 @@ class HelpDialog(QtGui.QDialog, RetranslateMixin): self.setFixedSize(QtGui.QWidget.sizeHint(self)) -class ConnectDialog(QtGui.QDialog, RetranslateMixin): +class ConnectDialog(QtGui.QDialog): """The `Connect` dialog""" def __init__(self, parent=None): super(ConnectDialog, self).__init__(parent) diff --git a/src/bitmessageqt/networkstatus.py b/src/bitmessageqt/networkstatus.py index 59b97f77..e8340298 100644 --- a/src/bitmessageqt/networkstatus.py +++ b/src/bitmessageqt/networkstatus.py @@ -1,7 +1,5 @@ """ -src/bitmessageqt/networkstatus.py -================================= - +Network status tab widget definition. """ import time @@ -34,8 +32,6 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): header.setSortIndicator(0, QtCore.Qt.AscendingOrder) self.startup = time.localtime() - self.labelStartupTime.setText(_translate("networkstatus", "Since startup on %1").arg( - l10n.formatTimestamp(self.startup))) self.UISignalThread = UISignaler.get() # pylint: disable=no-member @@ -240,6 +236,15 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): self.updateNumberOfObjectsToBeSynced() def retranslateUi(self): + """Conventional Qt Designer method for dynamic l10n""" super(NetworkStatus, self).retranslateUi() - self.labelStartupTime.setText(_translate("networkstatus", "Since startup on %1").arg( - l10n.formatTimestamp(self.startup))) + self.labelTotalConnections.setText( + _translate( + "networkstatus", "Total Connections: %1").arg( + str(self.tableWidgetConnectionCount.rowCount()))) + self.labelStartupTime.setText(_translate( + "networkstatus", "Since startup on %1" + ).arg(l10n.formatTimestamp(self.startup))) + self.updateNumberOfMessagesProcessed() + self.updateNumberOfBroadcastsProcessed() + self.updateNumberOfPubkeysProcessed() diff --git a/src/bitmessageqt/newchandialog.py b/src/bitmessageqt/newchandialog.py index 8db486c1..c0629cd7 100644 --- a/src/bitmessageqt/newchandialog.py +++ b/src/bitmessageqt/newchandialog.py @@ -9,13 +9,13 @@ from PyQt4 import QtCore, QtGui import widgets from addresses import addBMIfNotPresent from addressvalidator import AddressValidator, PassPhraseValidator -from queues import UISignalQueue, addressGeneratorQueue, apiAddressGeneratorReturnQueue -from retranslateui import RetranslateMixin +from queues import ( + addressGeneratorQueue, apiAddressGeneratorReturnQueue, UISignalQueue) from tr import _translate from utils import str_chan -class NewChanDialog(QtGui.QDialog, RetranslateMixin): +class NewChanDialog(QtGui.QDialog): """The `New Chan` dialog""" def __init__(self, parent=None): super(NewChanDialog, self).__init__(parent) From 15d44d85d4b85f4bed83be9ae51b9c5cded30145 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Wed, 15 May 2019 13:39:30 +0300 Subject: [PATCH 20/28] Renamed application class and moved most of init statements to __init__ --- src/bitmessageqt/__init__.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index f6dc39bf..a56dab86 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -807,7 +807,7 @@ class MyForm(settingsmixin.SMainWindow): self.rerenderComboBoxSendFrom() self.rerenderComboBoxSendFromBroadcast() - + # Put the TTL slider in the correct spot TTL = BMConfigParser().getint('bitmessagesettings', 'ttl') if TTL < 3600: # an hour @@ -822,6 +822,14 @@ class MyForm(settingsmixin.SMainWindow): self.initSettings() self.resetNamecoinConnection() + self.sqlInit() + self.indicatorInit() + self.notifierInit() + + self.ui.updateNetworkSwitchMenuLabel() + + self._firstrun = BMConfigParser().safeGetBoolean( + 'bitmessagesettings', 'dontconnect') self._contact_selected = None @@ -4121,9 +4129,6 @@ class MyForm(settingsmixin.SMainWindow): self.ui.pushButtonFetchNamecoinID.show() def initSettings(self): - QtCore.QCoreApplication.setOrganizationName("PyBitmessage") - QtCore.QCoreApplication.setOrganizationDomain("bitmessage.org") - QtCore.QCoreApplication.setApplicationName("pybitmessageqt") self.loadSettings() for attr, obj in self.ui.__dict__.iteritems(): if hasattr(obj, "__class__") and \ @@ -4137,7 +4142,7 @@ app = None myapp = None -class MySingleApplication(QtGui.QApplication): +class BitmessageQtApplication(QtGui.QApplication): """ Listener to allow our Qt form to get focus when another instance of the application is open. @@ -4150,8 +4155,12 @@ class MySingleApplication(QtGui.QApplication): uuid = '6ec0149b-96e1-4be1-93ab-1465fb3ebf7c' def __init__(self, *argv): - super(MySingleApplication, self).__init__(*argv) - id = MySingleApplication.uuid + super(BitmessageQtApplication, self).__init__(*argv) + id = BitmessageQtApplication.uuid + + QtCore.QCoreApplication.setOrganizationName("PyBitmessage") + QtCore.QCoreApplication.setOrganizationDomain("bitmessage.org") + QtCore.QCoreApplication.setApplicationName("pybitmessageqt") self.server = None self.is_running = False @@ -4179,6 +4188,8 @@ class MySingleApplication(QtGui.QApplication): self.server.listen(id) self.server.newConnection.connect(self.on_new_connection) + self.setStyleSheet("QStatusBar::item { border: 0px solid black }") + def __del__(self): if self.server: self.server.close() @@ -4191,25 +4202,19 @@ class MySingleApplication(QtGui.QApplication): def init(): global app if not app: - app = MySingleApplication(sys.argv) + app = BitmessageQtApplication(sys.argv) return app def run(): global myapp app = init() - app.setStyleSheet("QStatusBar::item { border: 0px solid black }") myapp = MyForm() - myapp.sqlInit() myapp.appIndicatorInit(app) - myapp.indicatorInit() - myapp.notifierInit() - myapp._firstrun = BMConfigParser().safeGetBoolean( - 'bitmessagesettings', 'dontconnect') + if myapp._firstrun: myapp.showConnectDialog() # ask the user if we may connect - myapp.ui.updateNetworkSwitchMenuLabel() # try: # if BMConfigParser().get('bitmessagesettings', 'mailchuck') < 1: From 36242343c2cd41f937f8bb74aa779ca3715aa659 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Sat, 7 Dec 2019 12:10:31 +0200 Subject: [PATCH 21/28] Style and formatting changes in support --- src/bitmessageqt/support.py | 74 ++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/src/bitmessageqt/support.py b/src/bitmessageqt/support.py index d6d4543d..345ea647 100644 --- a/src/bitmessageqt/support.py +++ b/src/bitmessageqt/support.py @@ -1,33 +1,42 @@ +"""Composing support request message functions.""" + import ctypes -from PyQt4 import QtCore, QtGui import ssl import sys import time +from PyQt4 import QtCore + import account -from bmconfigparser import BMConfigParser -from debug import logger import defaults -from foldertree import AccountMixin -from helper_sql import * -from l10n import getTranslationLanguage -from openclpow import openclAvailable, openclEnabled +import network.stats import paths import proofofwork +import queues +import state +from bmconfigparser import BMConfigParser +from foldertree import AccountMixin +from helper_sql import sqlExecute, sqlQuery +from l10n import getTranslationLanguage +from openclpow import openclEnabled from pyelliptic.openssl import OpenSSL from settings import getSOCKSProxyType -import queues -import network.stats -import state from version import softwareVersion +from tr import _translate + # this is BM support address going to Peter Surda OLD_SUPPORT_ADDRESS = 'BM-2cTkCtMYkrSPwFTpgcBrMrf5d8oZwvMZWK' SUPPORT_ADDRESS = 'BM-2cUdgkDDAahwPAU6oD2A7DnjqZz3hgY832' -SUPPORT_LABEL = 'PyBitmessage support' -SUPPORT_MY_LABEL = 'My new address' +SUPPORT_LABEL = _translate("Support", "PyBitmessage support") +SUPPORT_MY_LABEL = _translate("Support", "My new address") SUPPORT_SUBJECT = 'Support request' -SUPPORT_MESSAGE = '''You can use this message to send a report to one of the PyBitmessage core developers regarding PyBitmessage or the mailchuck.com email service. If you are using PyBitmessage involuntarily, for example because your computer was infected with ransomware, this is not an appropriate venue for resolving such issues. +SUPPORT_MESSAGE = _translate("Support", ''' +You can use this message to send a report to one of the PyBitmessage core \ +developers regarding PyBitmessage or the mailchuck.com email service. \ +If you are using PyBitmessage involuntarily, for example because \ +your computer was infected with ransomware, this is not an appropriate venue \ +for resolving such issues. Please describe what you are trying to do: @@ -37,7 +46,8 @@ Please describe what happens instead: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Please write above this line and if possible, keep the information about your environment below intact. +Please write above this line and if possible, keep the information about your \ +environment below intact. PyBitmessage version: {} Operating system: {} @@ -52,15 +62,18 @@ Locale: {} SOCKS: {} UPnP: {} Connected hosts: {} -''' +''') + def checkAddressBook(myapp): - sqlExecute('''DELETE from addressbook WHERE address=?''', OLD_SUPPORT_ADDRESS) - queryreturn = sqlQuery('''SELECT * FROM addressbook WHERE address=?''', SUPPORT_ADDRESS) + sqlExecute('DELETE from addressbook WHERE address=?', OLD_SUPPORT_ADDRESS) + queryreturn = sqlQuery('SELECT * FROM addressbook WHERE address=?', SUPPORT_ADDRESS) if queryreturn == []: - sqlExecute('''INSERT INTO addressbook VALUES (?,?)''', str(QtGui.QApplication.translate("Support", SUPPORT_LABEL)), SUPPORT_ADDRESS) + sqlExecute( + 'INSERT INTO addressbook VALUES (?,?)', SUPPORT_LABEL, SUPPORT_ADDRESS) myapp.rerenderAddressBook() + def checkHasNormalAddress(): for address in account.getSortedAccounts(): acct = account.accountClass(address) @@ -68,23 +81,31 @@ def checkHasNormalAddress(): return address return False + def createAddressIfNeeded(myapp): if not checkHasNormalAddress(): - queues.addressGeneratorQueue.put(('createRandomAddress', 4, 1, str(QtGui.QApplication.translate("Support", SUPPORT_MY_LABEL)), 1, "", False, defaults.networkDefaultProofOfWorkNonceTrialsPerByte, defaults.networkDefaultPayloadLengthExtraBytes)) + queues.addressGeneratorQueue.put(( + 'createRandomAddress', 4, 1, SUPPORT_MY_LABEL, 1, "", False, + defaults.networkDefaultProofOfWorkNonceTrialsPerByte, + defaults.networkDefaultPayloadLengthExtraBytes + )) while state.shutdown == 0 and not checkHasNormalAddress(): time.sleep(.2) myapp.rerenderComboBoxSendFrom() return checkHasNormalAddress() + def createSupportMessage(myapp): checkAddressBook(myapp) address = createAddressIfNeeded(myapp) if state.shutdown: return - myapp.ui.lineEditSubject.setText(str(QtGui.QApplication.translate("Support", SUPPORT_SUBJECT))) - addrIndex = myapp.ui.comboBoxSendFrom.findData(address, QtCore.Qt.UserRole, QtCore.Qt.MatchFixedString | QtCore.Qt.MatchCaseSensitive) - if addrIndex == -1: # something is very wrong + myapp.ui.lineEditSubject.setText(SUPPORT_SUBJECT) + addrIndex = myapp.ui.comboBoxSendFrom.findData( + address, QtCore.Qt.UserRole, + QtCore.Qt.MatchFixedString | QtCore.Qt.MatchCaseSensitive) + if addrIndex == -1: # something is very wrong return myapp.ui.comboBoxSendFrom.setCurrentIndex(addrIndex) myapp.ui.lineEditTo.setText(SUPPORT_ADDRESS) @@ -107,8 +128,9 @@ def createSupportMessage(myapp): pass architecture = "32" if ctypes.sizeof(ctypes.c_voidp) == 4 else "64" pythonversion = sys.version - - opensslversion = "%s (Python internal), %s (external for PyElliptic)" % (ssl.OPENSSL_VERSION, OpenSSL._version) + + opensslversion = "%s (Python internal), %s (external for PyElliptic)" % ( + ssl.OPENSSL_VERSION, OpenSSL._version) frozen = "N/A" if paths.frozen: @@ -123,7 +145,9 @@ def createSupportMessage(myapp): upnp = BMConfigParser().safeGet('bitmessagesettings', 'upnp', "N/A") connectedhosts = len(network.stats.connectedHostsList()) - myapp.ui.textEditMessage.setText(str(QtGui.QApplication.translate("Support", SUPPORT_MESSAGE)).format(version, os, architecture, pythonversion, opensslversion, frozen, portablemode, cpow, openclpow, locale, socks, upnp, connectedhosts)) + myapp.ui.textEditMessage.setText(unicode(SUPPORT_MESSAGE, 'utf-8').format( + version, os, architecture, pythonversion, opensslversion, frozen, + portablemode, cpow, openclpow, locale, socks, upnp, connectedhosts)) # single msg tab myapp.ui.tabWidgetSend.setCurrentIndex( From 87e3d63340295bccece4d106d1b51bc8235640a0 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 15 May 2020 14:18:22 +0300 Subject: [PATCH 22/28] Minimal fix for #1449, if search line is empty --- src/bitmessageqt/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index a56dab86..7902fa79 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -1240,6 +1240,9 @@ class MyForm(settingsmixin.SMainWindow): if tableWidget == self.ui.tableWidgetInboxSubscriptions: xAddress = "fromaddress" + if not what: + where = _translate("MainWindow", "To") + what = str_broadcast_subscribers else: xAddress = "toaddress" if account is not None: From c69decaab319ec99b5a2fa22c754415696542adb Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Sun, 24 May 2020 14:08:14 +0300 Subject: [PATCH 23/28] Fix unicode bug introduced in 045a2ef --- src/bitmessageqt/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 7902fa79..2f1a6e7f 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -3953,7 +3953,7 @@ class MyForm(settingsmixin.SMainWindow): def inboxSearchLineEditUpdated(self, text): # dynamic search for too short text is slow - text = str(text) + text = text.toUtf8() if 0 < len(text) < 3: return messagelist = self.getCurrentMessagelist() From 3f773c78f70c5a517d26c8fa4d9858c1d4fd3bb6 Mon Sep 17 00:00:00 2001 From: lakshya Date: Mon, 18 May 2020 21:30:13 +0530 Subject: [PATCH 24/28] setting, openssl and bmproto quality fixes --- src/bitmessageqt/settings.py | 49 +++++++++++++++++++----------------- src/network/bmproto.py | 13 +++++----- src/pyelliptic/openssl.py | 4 +-- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/bitmessageqt/settings.py b/src/bitmessageqt/settings.py index 1f650fe7..8e4bf198 100644 --- a/src/bitmessageqt/settings.py +++ b/src/bitmessageqt/settings.py @@ -1,3 +1,6 @@ +""" +This module setting file is for settings +""" import ConfigParser import os import sys @@ -110,7 +113,7 @@ class SettingsDialog(QtGui.QDialog): tempfile.NamedTemporaryFile( dir=paths.lookupExeFolder(), delete=True ).close() # should autodelete - except: + except Exception: self.checkBoxPortableMode.setDisabled(True) if 'darwin' in sys.platform: @@ -407,14 +410,14 @@ class SettingsDialog(QtGui.QDialog): self.config.set( 'bitmessagesettings', 'defaultnoncetrialsperbyte', str(int( - float(self.lineEditTotalDifficulty.text()) * - defaults.networkDefaultProofOfWorkNonceTrialsPerByte))) + float(self.lineEditTotalDifficulty.text()) + * defaults.networkDefaultProofOfWorkNonceTrialsPerByte))) if float(self.lineEditSmallMessageDifficulty.text()) >= 1: self.config.set( 'bitmessagesettings', 'defaultpayloadlengthextrabytes', str(int( - float(self.lineEditSmallMessageDifficulty.text()) * - defaults.networkDefaultPayloadLengthExtraBytes))) + float(self.lineEditSmallMessageDifficulty.text()) + * defaults.networkDefaultPayloadLengthExtraBytes))) if self.comboBoxOpenCL.currentText().toUtf8() != self.config.safeGet( 'bitmessagesettings', 'opencl'): @@ -426,40 +429,40 @@ class SettingsDialog(QtGui.QDialog): acceptableDifficultyChanged = False if ( - float(self.lineEditMaxAcceptableTotalDifficulty.text()) >= 1 or - float(self.lineEditMaxAcceptableTotalDifficulty.text()) == 0 + float(self.lineEditMaxAcceptableTotalDifficulty.text()) >= 1 + or float(self.lineEditMaxAcceptableTotalDifficulty.text()) == 0 ): if self.config.get( 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte' ) != str(int( - float(self.lineEditMaxAcceptableTotalDifficulty.text()) * - defaults.networkDefaultProofOfWorkNonceTrialsPerByte) + float(self.lineEditMaxAcceptableTotalDifficulty.text()) + * defaults.networkDefaultProofOfWorkNonceTrialsPerByte) ): # the user changed the max acceptable total difficulty acceptableDifficultyChanged = True self.config.set( 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', str(int( - float(self.lineEditMaxAcceptableTotalDifficulty.text()) * - defaults.networkDefaultProofOfWorkNonceTrialsPerByte)) + float(self.lineEditMaxAcceptableTotalDifficulty.text()) + * defaults.networkDefaultProofOfWorkNonceTrialsPerByte)) ) if ( - float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) >= 1 or - float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) == 0 + float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) >= 1 + or float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) == 0 ): if self.config.get( 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes' ) != str(int( - float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) * - defaults.networkDefaultPayloadLengthExtraBytes) + float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) + * defaults.networkDefaultPayloadLengthExtraBytes) ): # the user changed the max acceptable small message difficulty acceptableDifficultyChanged = True self.config.set( 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', str(int( - float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) * - defaults.networkDefaultPayloadLengthExtraBytes)) + float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) + * defaults.networkDefaultPayloadLengthExtraBytes)) ) if acceptableDifficultyChanged: # It might now be possible to send msgs which were previously @@ -537,8 +540,8 @@ class SettingsDialog(QtGui.QDialog): self.parent.updateStartOnLogon() if ( - state.appdata != paths.lookupExeFolder() and - self.checkBoxPortableMode.isChecked() + state.appdata != paths.lookupExeFolder() + and self.checkBoxPortableMode.isChecked() ): # If we are NOT using portable mode now but the user selected # that we should... @@ -556,12 +559,12 @@ class SettingsDialog(QtGui.QDialog): try: os.remove(previousAppdataLocation + 'debug.log') os.remove(previousAppdataLocation + 'debug.log.1') - except: + except Exception: pass if ( - state.appdata == paths.lookupExeFolder() and - not self.checkBoxPortableMode.isChecked() + state.appdata == paths.lookupExeFolder() + and not self.checkBoxPortableMode.isChecked() ): # If we ARE using portable mode now but the user selected # that we shouldn't... @@ -579,5 +582,5 @@ class SettingsDialog(QtGui.QDialog): try: os.remove(paths.lookupExeFolder() + 'debug.log') os.remove(paths.lookupExeFolder() + 'debug.log.1') - except: + except Exception: pass diff --git a/src/network/bmproto.py b/src/network/bmproto.py index ed8e5924..637e2ba7 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -441,9 +441,10 @@ class BMProto(AdvancedDispatcher, ObjectTracker): if stream not in state.streamsInWhichIAmParticipating: continue if ( - decodedIP and time.time() - seenTime > 0 and - seenTime > time.time() - ADDRESS_ALIVE and - port > 0 + decodedIP + and time.time() - seenTime > 0 + and seenTime > time.time() - ADDRESS_ALIVE + and port > 0 ): peer = Peer(decodedIP, port) try: @@ -541,8 +542,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker): logger.debug( '%(host)s:%(port)i sending version', self.destination._asdict()) - if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) and - protocol.haveSSL(not self.isOutbound)): + if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) + and protocol.haveSSL(not self.isOutbound)): self.isSSL = True if not self.verackReceived: return True @@ -600,7 +601,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): 'Closed connection to %s because we are already' ' connected to that IP.', self.destination) return False - except: + except Exception: pass if not self.isOutbound: # incoming from a peer we're connected to as outbound, diff --git a/src/pyelliptic/openssl.py b/src/pyelliptic/openssl.py index 17a8d6d1..accaaa94 100644 --- a/src/pyelliptic/openssl.py +++ b/src/pyelliptic/openssl.py @@ -453,7 +453,7 @@ class _OpenSSL(object): try: self.PKCS5_PBKDF2_HMAC = self._lib.PKCS5_PBKDF2_HMAC - except: + except Exception: # The above is not compatible with all versions of OSX. self.PKCS5_PBKDF2_HMAC = self._lib.PKCS5_PBKDF2_HMAC_SHA1 @@ -794,7 +794,7 @@ def loadOpenSSL(): try: OpenSSL = _OpenSSL(library) return - except: + except Exception: pass raise Exception( "Couldn't find and load the OpenSSL library. You must install it.") From 5cfaa9b2fad99e75db4773712cce7a783861c422 Mon Sep 17 00:00:00 2001 From: coolguy-cell Date: Sat, 30 May 2020 18:26:54 +0530 Subject: [PATCH 25/28] fixed CQ for address_dialog python file --- src/bitmessageqt/address_dialogs.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/bitmessageqt/address_dialogs.py b/src/bitmessageqt/address_dialogs.py index 7b73152d..56cf7cc5 100644 --- a/src/bitmessageqt/address_dialogs.py +++ b/src/bitmessageqt/address_dialogs.py @@ -1,7 +1,7 @@ """ Dialogs that work with BM address. """ -# pylint: disable=attribute-defined-outside-init,too-few-public-methods +# pylint: disable=attribute-defined-outside-init,too-few-public-methods,relative-import import hashlib @@ -191,8 +191,8 @@ class NewSubscriptionDialog(AddressDataDialog): else: Inventory().flush() doubleHashOfAddressData = hashlib.sha512(hashlib.sha512( - encodeVarint(addressVersion) + - encodeVarint(streamNumber) + ripe + encodeVarint(addressVersion) + + encodeVarint(streamNumber) + ripe ).digest()).digest() tag = doubleHashOfAddressData[32:] self.recent = Inventory().by_type_and_tag(3, tag) @@ -256,11 +256,7 @@ class SpecialAddressBehaviorDialog(QtGui.QDialog): self.radioButtonBehaviorMailingList.click() else: self.radioButtonBehaveNormalAddress.click() - try: - mailingListName = config.get( - self.address, 'mailinglistname') - except: - mailingListName = '' + mailingListName = config.safeGet(self.address, 'mailinglistname', '') self.lineEditMailingListName.setText( unicode(mailingListName, 'utf-8') ) From b750b02963dae629618a4bf0d0637d45ce516378 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Sat, 30 May 2020 14:18:49 +0300 Subject: [PATCH 26/28] Fix another unicode bug introduced in 3624234. Temporary suppressed pylint no-member warning. Closes: #1633 --- src/bitmessageqt/support.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/bitmessageqt/support.py b/src/bitmessageqt/support.py index 345ea647..4970beba 100644 --- a/src/bitmessageqt/support.py +++ b/src/bitmessageqt/support.py @@ -1,4 +1,5 @@ """Composing support request message functions.""" +# pylint: disable=no-member import ctypes import ssl @@ -70,7 +71,8 @@ def checkAddressBook(myapp): queryreturn = sqlQuery('SELECT * FROM addressbook WHERE address=?', SUPPORT_ADDRESS) if queryreturn == []: sqlExecute( - 'INSERT INTO addressbook VALUES (?,?)', SUPPORT_LABEL, SUPPORT_ADDRESS) + 'INSERT INTO addressbook VALUES (?,?)', + SUPPORT_LABEL.toUtf8(), SUPPORT_ADDRESS) myapp.rerenderAddressBook() @@ -85,7 +87,9 @@ def checkHasNormalAddress(): def createAddressIfNeeded(myapp): if not checkHasNormalAddress(): queues.addressGeneratorQueue.put(( - 'createRandomAddress', 4, 1, SUPPORT_MY_LABEL, 1, "", False, + 'createRandomAddress', 4, 1, + SUPPORT_MY_LABEL.toUtf8(), + 1, "", False, defaults.networkDefaultProofOfWorkNonceTrialsPerByte, defaults.networkDefaultPayloadLengthExtraBytes )) From e37245973742fdd52843d98d0c02f22a3ed2a448 Mon Sep 17 00:00:00 2001 From: coolguy-cell Date: Wed, 3 Jun 2020 16:59:15 +0530 Subject: [PATCH 27/28] fixed CQ for bitmessageqt.languagebox module --- src/bitmessageqt/languagebox.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/bitmessageqt/languagebox.py b/src/bitmessageqt/languagebox.py index b97e61d3..9ff78990 100644 --- a/src/bitmessageqt/languagebox.py +++ b/src/bitmessageqt/languagebox.py @@ -1,3 +1,5 @@ +"""Language Box Module for Locale Settings""" +# pylint: disable=too-few-public-methods,bad-continuation import glob import os @@ -8,6 +10,7 @@ from bmconfigparser import BMConfigParser class LanguageBox(QtGui.QComboBox): + """LanguageBox class for Qt UI""" languageName = { "system": "System Settings", "eo": "Esperanto", "en_pirate": "Pirate English" @@ -18,6 +21,7 @@ class LanguageBox(QtGui.QComboBox): self.populate() def populate(self): + """Populates drop down list with all available languages.""" self.clear() localesPath = os.path.join(paths.codePath(), 'translations') self.addItem(QtGui.QApplication.translate( From e237534335d66748b27fb6251faf6cb72912ebd1 Mon Sep 17 00:00:00 2001 From: coolguy-cell Date: Fri, 5 Jun 2020 18:44:45 +0530 Subject: [PATCH 28/28] fixed CQ for bitmessageqt.networkstatus module --- src/bitmessageqt/networkstatus.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bitmessageqt/networkstatus.py b/src/bitmessageqt/networkstatus.py index e8340298..685d9ff8 100644 --- a/src/bitmessageqt/networkstatus.py +++ b/src/bitmessageqt/networkstatus.py @@ -92,8 +92,8 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): "Object(s) to be synced: %n", None, QtCore.QCoreApplication.CodecForTr, - network.stats.pendingDownload() + - network.stats.pendingUpload())) + network.stats.pendingDownload() + + network.stats.pendingUpload())) def updateNumberOfMessagesProcessed(self): """Update the counter for number of processed messages""" @@ -203,7 +203,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): self.tableWidgetConnectionCount.item(0, 0).setData(QtCore.Qt.UserRole, destination) self.tableWidgetConnectionCount.item(0, 1).setData(QtCore.Qt.UserRole, outbound) else: - if len(BMConnectionPool().inboundConnections) == 0: + if not BMConnectionPool().inboundConnections: self.window().setStatusIcon('yellow') for i in range(self.tableWidgetConnectionCount.rowCount()): if self.tableWidgetConnectionCount.item(i, 0).data(QtCore.Qt.UserRole).toPyObject() != destination: