2013-02-18 21:22:48 +01:00
#!/usr/bin/env python2.7
2012-11-19 20:45:05 +01:00
# Copyright (c) 2012 Jonathan Warren
# Copyright (c) 2012 The Bitmessage developers
# Distributed under the MIT/X11 software license. See the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#Right now, PyBitmessage only support connecting to stream 1. It doesn't yet contain logic to expand into further streams.
2013-03-19 18:32:37 +01:00
softwareVersion = ' 0.2.7 '
2012-11-19 20:45:05 +01:00
verbose = 2
maximumAgeOfAnObjectThatIAmWillingToAccept = 216000 #Equals two days and 12 hours.
lengthOfTimeToLeaveObjectsInInventory = 237600 #Equals two days and 18 hours. This should be longer than maximumAgeOfAnObjectThatIAmWillingToAccept so that we don't process messages twice.
2013-02-11 22:28:38 +01:00
lengthOfTimeToHoldOnToAllPubkeys = 2419200 #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.
2012-11-19 20:45:05 +01:00
maximumAgeOfObjectsThatIAdvertiseToOthers = 216000 #Equals two days and 12 hours
maximumAgeOfNodesThatIAdvertiseToOthers = 10800 #Equals three hours
2013-02-26 21:07:51 +01:00
storeConfigFilesInSameDirectoryAsProgramByDefault = False #The user may de-select Portable Mode in the settings if they want the config files to stay in the application data folder.
2013-01-22 20:29:49 +01:00
useVeryEasyProofOfWorkForTesting = False #If you set this to True while on the normal network, you won't be able to send or sometimes receive messages.
2012-11-19 20:45:05 +01:00
import sys
try :
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
except Exception , err :
2012-11-26 01:36:29 +01:00
print ' PyBitmessage requires PyQt. You can download it from http://www.riverbankcomputing.com/software/pyqt/download or by searching Google for \' PyQt Download \' (without quotes). '
2012-11-19 20:45:05 +01:00
print ' Error message: ' , err
sys . exit ( )
import ConfigParser
from bitmessageui import *
from newaddressdialog import *
from newsubscriptiondialog import *
2013-01-24 22:07:24 +01:00
from regenerateaddresses import *
2013-02-26 00:09:32 +01:00
from specialaddressbehavior import *
2012-11-19 20:45:05 +01:00
from settings import *
from about import *
from help import *
from iconglossary import *
from addresses import *
import Queue
from defaultKnownNodes import *
import time
import socket
import threading
2013-03-01 23:44:52 +01:00
#import rsa
#from rsa.bigfile import *
2012-11-19 20:45:05 +01:00
import hashlib
from struct import *
import pickle
import random
import sqlite3
import threading #used for the locks, not for the threads
from time import strftime , localtime
import os
2013-02-26 21:07:51 +01:00
import shutil #used for moving the messages.dat file
2012-12-04 18:11:14 +01:00
import string
2012-12-18 19:09:10 +01:00
import socks
2013-01-21 01:00:46 +01:00
import highlevelcrypto
2013-01-16 17:52:52 +01:00
from pyelliptic . openssl import OpenSSL
import ctypes
from pyelliptic import arithmetic
2013-03-22 18:23:44 +01:00
#The next 3 are used for the API
2013-03-19 18:32:37 +01:00
from SimpleXMLRPCServer import *
import json
from subprocess import call #used when the API must execute an outside program
2012-11-19 20:45:05 +01:00
#For each stream to which we connect, one outgoingSynSender thread will exist and will create 8 connections with peers.
class outgoingSynSender ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
self . selfInitiatedConnectionList = [ ] #This is a list of current connections (the thread pointers at least)
self . alreadyAttemptedConnectionsList = [ ] #This is a list of nodes to which we have already attempted a connection
def setup ( self , streamNumber ) :
self . streamNumber = streamNumber
def run ( self ) :
time . sleep ( 1 )
resetTime = int ( time . time ( ) ) #used below to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect.
while True :
2013-03-22 18:29:06 +01:00
#time.sleep(999999)#I sometimes use this to prevent connections for testing.
2012-11-19 20:45:05 +01:00
if len ( self . selfInitiatedConnectionList ) < 8 : #maximum number of outgoing connections = 8
random . seed ( )
HOST , = random . sample ( knownNodes [ self . streamNumber ] , 1 )
while HOST in self . alreadyAttemptedConnectionsList or HOST in connectedHostsList :
#print 'choosing new sample'
random . seed ( )
HOST , = random . sample ( knownNodes [ self . streamNumber ] , 1 )
time . sleep ( 1 )
#Clear out the alreadyAttemptedConnectionsList every half hour so that this program will again attempt a connection to any nodes, even ones it has already tried.
if ( int ( time . time ( ) ) - resetTime ) > 1800 :
self . alreadyAttemptedConnectionsList = [ ]
resetTime = int ( time . time ( ) )
self . alreadyAttemptedConnectionsList . append ( HOST )
PORT , timeNodeLastSeen = knownNodes [ self . streamNumber ] [ HOST ]
2012-12-18 19:09:10 +01:00
sock = socks . socksocket ( socket . AF_INET , socket . SOCK_STREAM )
2012-12-19 07:22:04 +01:00
sock . settimeout ( 20 )
2012-12-18 19:09:10 +01:00
if config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) == ' none ' :
printLock . acquire ( )
print ' Trying an outgoing connection to ' , HOST , ' : ' , PORT
printLock . release ( )
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
elif config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) == ' SOCKS4a ' :
printLock . acquire ( )
print ' (Using SOCKS4a) Trying an outgoing connection to ' , HOST , ' : ' , PORT
printLock . release ( )
proxytype = socks . PROXY_TYPE_SOCKS4
sockshostname = config . get ( ' bitmessagesettings ' , ' sockshostname ' )
socksport = config . getint ( ' bitmessagesettings ' , ' socksport ' )
rdns = True #Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
if config . getboolean ( ' bitmessagesettings ' , ' socksauthentication ' ) :
socksusername = config . get ( ' bitmessagesettings ' , ' socksusername ' )
sockspassword = config . get ( ' bitmessagesettings ' , ' sockspassword ' )
sock . setproxy ( proxytype , sockshostname , socksport , rdns , socksusername , sockspassword )
else :
sock . setproxy ( proxytype , sockshostname , socksport , rdns )
elif config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) == ' SOCKS5 ' :
printLock . acquire ( )
print ' (Using SOCKS5) Trying an outgoing connection to ' , HOST , ' : ' , PORT
printLock . release ( )
proxytype = socks . PROXY_TYPE_SOCKS5
sockshostname = config . get ( ' bitmessagesettings ' , ' sockshostname ' )
socksport = config . getint ( ' bitmessagesettings ' , ' socksport ' )
rdns = True #Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
if config . getboolean ( ' bitmessagesettings ' , ' socksauthentication ' ) :
socksusername = config . get ( ' bitmessagesettings ' , ' socksusername ' )
sockspassword = config . get ( ' bitmessagesettings ' , ' sockspassword ' )
sock . setproxy ( proxytype , sockshostname , socksport , rdns , socksusername , sockspassword )
else :
sock . setproxy ( proxytype , sockshostname , socksport , rdns )
2012-11-19 20:45:05 +01:00
try :
sock . connect ( ( HOST , PORT ) )
rd = receiveDataThread ( )
self . emit ( SIGNAL ( " passObjectThrough(PyQt_PyObject) " ) , rd )
2013-02-03 06:16:50 +01:00
objectsOfWhichThisRemoteNodeIsAlreadyAware = { }
rd . setup ( sock , HOST , PORT , self . streamNumber , self . selfInitiatedConnectionList , objectsOfWhichThisRemoteNodeIsAlreadyAware )
2012-11-19 20:45:05 +01:00
rd . start ( )
2012-12-10 20:48:00 +01:00
printLock . acquire ( )
2013-03-26 17:19:18 +01:00
print self , ' connected to ' , HOST , ' during an outgoing attempt. '
2012-12-10 20:48:00 +01:00
printLock . release ( )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
sd = sendDataThread ( )
2013-02-03 06:16:50 +01:00
sd . setup ( sock , HOST , PORT , self . streamNumber , objectsOfWhichThisRemoteNodeIsAlreadyAware )
2012-11-19 20:45:05 +01:00
sd . start ( )
sd . sendVersionMessage ( )
2012-12-18 19:09:10 +01:00
except socks . GeneralProxyError , err :
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' Could NOT connect to ' , HOST , ' during outgoing attempt. ' , err
2013-01-22 06:21:32 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
PORT , timeLastSeen = knownNodes [ self . streamNumber ] [ HOST ]
2012-12-18 19:09:10 +01:00
if ( int ( time . time ( ) ) - timeLastSeen ) > 172800 and len ( knownNodes [ self . streamNumber ] ) > 1000 : # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the knownNodes data-structure.
del knownNodes [ self . streamNumber ] [ HOST ]
print ' deleting ' , HOST , ' from knownNodes because it is more than 48 hours old and we could not connect to it. '
except socks . Socks5AuthError , err :
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , " SOCKS5 Authentication problem: " + str ( err ) )
except socks . Socks5Error , err :
pass
print ' SOCKS5 error. (It is possible that the server wants authentication).) ' , str ( err )
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"SOCKS5 error. Server might require authentication. "+str(err))
except socks . Socks4Error , err :
print ' Socks4Error: ' , err
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"SOCKS4 error: "+str(err))
except socket . error , err :
if config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) [ 0 : 5 ] == ' SOCKS ' :
2012-12-18 22:36:37 +01:00
print ' Bitmessage MIGHT be having trouble connecting to the SOCKS server. ' + str ( err )
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Problem: Bitmessage can not connect to the SOCKS server. "+str(err))
2012-12-18 19:09:10 +01:00
else :
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
2012-12-18 19:09:10 +01:00
print ' Could NOT connect to ' , HOST , ' during outgoing attempt. ' , err
2013-01-22 06:21:32 +01:00
printLock . release ( )
2012-12-18 19:09:10 +01:00
PORT , timeLastSeen = knownNodes [ self . streamNumber ] [ HOST ]
if ( int ( time . time ( ) ) - timeLastSeen ) > 172800 and len ( knownNodes [ self . streamNumber ] ) > 1000 : # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the knownNodes data-structure.
2012-11-19 20:45:05 +01:00
del knownNodes [ self . streamNumber ] [ HOST ]
print ' deleting ' , HOST , ' from knownNodes because it is more than 48 hours old and we could not connect to it. '
2012-12-18 19:32:48 +01:00
except Exception , err :
2012-12-18 22:36:37 +01:00
print ' An exception has occurred in the outgoingSynSender thread that was not caught by other exception types: ' , err
2013-02-03 06:16:50 +01:00
time . sleep ( 0.1 )
2012-11-19 20:45:05 +01:00
#Only one singleListener thread will ever exist. It creates the receiveDataThread and sendDataThread for each incoming connection. Note that it cannot set the stream number because it is not known yet- the other node will have to tell us its stream number in a version message. If we don't care about their stream, we will close the connection (within the recversion function of the recieveData thread)
class singleListener ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def run ( self ) :
2012-12-18 19:09:10 +01:00
#We don't want to accept incoming connections if the user is using a SOCKS proxy. If they eventually select proxy 'none' then this will start listening for connections.
while config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) [ 0 : 5 ] == ' SOCKS ' :
time . sleep ( 300 )
2012-11-19 20:45:05 +01:00
2013-02-03 06:16:50 +01:00
print ' Listening for incoming connections. '
2012-11-19 20:45:05 +01:00
HOST = ' ' # Symbolic name meaning all available interfaces
PORT = config . getint ( ' bitmessagesettings ' , ' port ' )
sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM )
#This option apparently avoids the TIME_WAIT state so that we can rebind faster
sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , 1 )
sock . bind ( ( HOST , PORT ) )
sock . listen ( 2 )
2012-12-18 19:09:10 +01:00
self . incomingConnectionList = [ ] #This list isn't used for anything. The reason it exists is because receiveData threads expect that a list be passed to them. They expect this because the outgoingSynSender thread DOES use a similar list to keep track of the number of outgoing connections it has created.
2012-11-19 20:45:05 +01:00
while True :
2013-01-22 06:21:32 +01:00
#We don't want to accept incoming connections if the user is using a SOCKS proxy. If the user eventually select proxy 'none' then this will start listening for connections.
2012-12-18 19:09:10 +01:00
while config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) [ 0 : 5 ] == ' SOCKS ' :
time . sleep ( 10 )
2012-11-19 20:45:05 +01:00
a , ( HOST , PORT ) = sock . accept ( )
2013-01-22 06:21:32 +01:00
#Users are finding that if they run more than one node in the same network (thus with the same public IP), they can not connect with the second node. This is because this section of code won't accept the connection from the same IP. This problem will go away when the Bitmessage network grows beyond being tiny but in the mean time I'll comment out this code section.
2012-12-27 17:26:52 +01:00
""" while HOST in connectedHostsList:
2012-11-19 20:45:05 +01:00
print ' incoming connection is from a host in connectedHostsList (we are already connected to it). Ignoring it. '
a . close ( )
2012-12-27 17:26:52 +01:00
a , ( HOST , PORT ) = sock . accept ( ) """
2012-12-18 19:09:10 +01:00
rd = receiveDataThread ( )
2012-11-19 20:45:05 +01:00
self . emit ( SIGNAL ( " passObjectThrough(PyQt_PyObject) " ) , rd )
2013-02-03 06:16:50 +01:00
objectsOfWhichThisRemoteNodeIsAlreadyAware = { }
rd . setup ( a , HOST , PORT , - 1 , self . incomingConnectionList , objectsOfWhichThisRemoteNodeIsAlreadyAware )
2012-12-27 17:26:52 +01:00
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print self , ' connected to ' , HOST , ' during INCOMING request. '
2012-12-27 17:26:52 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
rd . start ( )
sd = sendDataThread ( )
2013-02-03 06:16:50 +01:00
sd . setup ( a , HOST , PORT , - 1 , objectsOfWhichThisRemoteNodeIsAlreadyAware )
2012-11-19 20:45:05 +01:00
sd . start ( )
#This thread is created either by the synSenderThread(for outgoing connections) or the singleListenerThread(for incoming connectiosn).
class receiveDataThread ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
self . data = ' '
self . verackSent = False
self . verackReceived = False
2013-02-03 06:16:50 +01:00
def setup ( self , sock , HOST , port , streamNumber , selfInitiatedConnectionList , objectsOfWhichThisRemoteNodeIsAlreadyAware ) :
2012-11-19 20:45:05 +01:00
self . sock = sock
self . HOST = HOST
self . PORT = port
self . sock . settimeout ( 600 ) #We'll send out a pong every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately.
self . streamNumber = streamNumber
self . selfInitiatedConnectionList = selfInitiatedConnectionList
self . selfInitiatedConnectionList . append ( self )
2013-01-22 06:21:32 +01:00
self . payloadLength = 0 #This is the protocol payload length thus it doesn't include the 24 byte message header
self . receivedgetbiginv = False #Gets set to true once we receive a getbiginv message from our peer. An abusive peer might request it too much so we use this variable to check whether they have already asked for a big inv message.
2013-02-04 22:49:02 +01:00
self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave = { }
2013-01-22 06:21:32 +01:00
connectedHostsList [ self . HOST ] = 0 #The very fact that this receiveData thread exists shows that we are connected to the remote host. Let's add it to this list so that the outgoingSynSender thread doesn't try to connect to it.
2012-11-19 20:45:05 +01:00
self . connectionIsOrWasFullyEstablished = False #set to true after the remote node and I accept each other's version messages. This is needed to allow the user interface to accurately reflect the current number of connections.
if self . streamNumber == - 1 : #This was an incoming connection. Send out a version message if we accept the other node's version message.
self . initiatedConnection = False
else :
self . initiatedConnection = True
2012-12-19 17:51:51 +01:00
self . ackDataThatWeHaveYetToSend = [ ] #When we receive a message bound for us, we store the acknowledgement that we need to send (the ackdata) here until we are done processing all other data received from this peer.
2013-02-03 06:16:50 +01:00
self . objectsOfWhichThisRemoteNodeIsAlreadyAware = objectsOfWhichThisRemoteNodeIsAlreadyAware
2012-11-19 20:45:05 +01:00
def run ( self ) :
while True :
try :
self . data = self . data + self . sock . recv ( 65536 )
except socket . timeout :
printLock . acquire ( )
2013-01-22 06:21:32 +01:00
print ' Timeout occurred waiting for data. Closing receiveData thread. '
2012-11-19 20:45:05 +01:00
printLock . release ( )
break
except Exception , err :
printLock . acquire ( )
2012-12-27 18:26:06 +01:00
print ' sock.recv error. Closing receiveData thread. ' , err
2012-11-19 20:45:05 +01:00
printLock . release ( )
break
#print 'Received', repr(self.data)
if self . data == " " :
printLock . acquire ( )
2013-01-22 06:21:32 +01:00
print ' Connection closed. Closing receiveData thread. '
2012-11-19 20:45:05 +01:00
printLock . release ( )
break
else :
self . processData ( )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
try :
self . sock . close ( )
except Exception , err :
print ' Within receiveDataThread run(), self.sock.close() failed. ' , err
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
try :
self . selfInitiatedConnectionList . remove ( self )
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
print ' removed self (a receiveDataThread) from ConnectionList '
printLock . release ( )
2012-11-19 20:45:05 +01:00
except :
pass
2013-01-22 06:21:32 +01:00
broadcastToSendDataQueues ( ( 0 , ' shutdown ' , self . HOST ) )
2012-11-19 20:45:05 +01:00
if self . connectionIsOrWasFullyEstablished : #We don't want to decrement the number of connections and show the result if we never incremented it in the first place (which we only do if the connection is fully established- meaning that both nodes accepted each other's version packets.)
connectionsCountLock . acquire ( )
connectionsCount [ self . streamNumber ] - = 1
self . emit ( SIGNAL ( " updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject) " ) , self . streamNumber , connectionsCount [ self . streamNumber ] )
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
print ' Updating network status tab with current connections count: ' , connectionsCount [ self . streamNumber ]
printLock . release ( )
2012-11-19 20:45:05 +01:00
connectionsCountLock . release ( )
2012-12-18 19:09:10 +01:00
try :
del connectedHostsList [ self . HOST ]
except Exception , err :
print ' Could not delete ' , self . HOST , ' from connectedHostsList. ' , err
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def processData ( self ) :
global verbose
2012-12-05 18:47:30 +01:00
#if verbose >= 2:
#printLock.acquire()
#print 'self.data is currently ', repr(self.data)
#printLock.release()
2013-02-12 21:00:04 +01:00
if len ( self . data ) < 20 : #if so little of the data has arrived that we can't even unpack the payload length
2012-11-19 20:45:05 +01:00
pass
elif self . data [ 0 : 4 ] != ' \xe9 \xbe \xb4 \xd9 ' :
if verbose > = 2 :
2012-12-05 18:47:30 +01:00
printLock . acquire ( )
2013-02-04 23:13:10 +01:00
sys . stderr . write ( ' The magic bytes were not correct. First 40 bytes of data: %s \n ' % repr ( self . data [ 0 : 40 ] ) )
2013-03-14 16:58:52 +01:00
print ' self.data: ' , self . data . encode ( ' hex ' )
2012-12-05 18:47:30 +01:00
printLock . release ( )
2013-02-12 21:00:04 +01:00
self . data = " "
2012-12-05 18:47:30 +01:00
else :
self . payloadLength , = unpack ( ' >L ' , self . data [ 16 : 20 ] )
2013-02-18 21:01:47 +01:00
if len ( self . data ) > = self . payloadLength + 24 : #check if the whole message has arrived yet. If it has,...
2012-12-05 18:47:30 +01:00
if self . data [ 20 : 24 ] == hashlib . sha512 ( self . data [ 24 : self . payloadLength + 24 ] ) . digest ( ) [ 0 : 4 ] : #test the checksum in the message. If it is correct...
#print 'message checksum is correct'
#The time we've last seen this node is obviously right now since we just received valid data from it. So update the knownNodes list so that other peers can be made aware of its existance.
if self . initiatedConnection : #The remote port is only something we should share with others if it is the remote node's incoming port (rather than some random operating-system-assigned outgoing port).
knownNodes [ self . streamNumber ] [ self . HOST ] = ( self . PORT , int ( time . time ( ) ) )
2013-02-06 22:17:49 +01:00
if self . payloadLength < = 180000000 : #If the size of the message is greater than 180MB, ignore it. (I get memory errors when processing messages much larger than this though it is concievable that this value will have to be lowered if some systems are less tolarant of large messages.)
remoteCommand = self . data [ 4 : 16 ]
2012-12-05 18:47:30 +01:00
printLock . acquire ( )
2013-03-25 19:13:56 +01:00
print ' remoteCommand ' , repr ( remoteCommand . replace ( ' \x00 ' , ' ' ) ) , ' from ' , self . HOST
2012-12-05 18:47:30 +01:00
printLock . release ( )
2013-02-06 22:17:49 +01:00
if remoteCommand == ' version \x00 \x00 \x00 \x00 \x00 ' :
self . recversion ( )
elif remoteCommand == ' verack \x00 \x00 \x00 \x00 \x00 \x00 ' :
self . recverack ( )
elif remoteCommand == ' addr \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . recaddr ( )
elif remoteCommand == ' getpubkey \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . recgetpubkey ( )
elif remoteCommand == ' pubkey \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . recpubkey ( )
elif remoteCommand == ' inv \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . recinv ( )
elif remoteCommand == ' getdata \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . recgetdata ( )
elif remoteCommand == ' getbiginv \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . sendBigInv ( )
elif remoteCommand == ' msg \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . recmsg ( )
elif remoteCommand == ' broadcast \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . recbroadcast ( )
elif remoteCommand == ' getaddr \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . sendaddr ( )
elif remoteCommand == ' ping \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . sendpong ( )
elif remoteCommand == ' pong \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
pass
elif remoteCommand == ' alert \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
pass
2012-12-05 18:47:30 +01:00
self . data = self . data [ self . payloadLength + 24 : ] #take this message out and then process the next message
if self . data == ' ' :
2013-02-04 22:49:02 +01:00
while len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave ) > 0 :
2012-12-05 18:47:30 +01:00
random . seed ( )
2013-02-04 22:49:02 +01:00
objectHash , = random . sample ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave , 1 )
2012-12-05 18:47:30 +01:00
if objectHash in inventory :
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
2012-12-19 16:38:45 +01:00
print ' Inventory (in memory) already has object listed in inv message. '
2013-01-22 06:21:32 +01:00
printLock . release ( )
2013-02-04 22:49:02 +01:00
del self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave [ objectHash ]
2012-12-05 18:47:30 +01:00
elif isInSqlInventory ( objectHash ) :
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
2012-12-19 16:38:45 +01:00
print ' Inventory (SQL on disk) already has object listed in inv message. '
2013-01-22 06:21:32 +01:00
printLock . release ( )
2013-02-04 22:49:02 +01:00
del self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave [ objectHash ]
2012-12-05 18:47:30 +01:00
else :
2013-01-22 06:21:32 +01:00
#print 'processData function making request for object:', objectHash.encode('hex')
2012-12-05 18:47:30 +01:00
self . sendgetdata ( objectHash )
2013-02-04 22:49:02 +01:00
del self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave [ objectHash ] #It is possible that the remote node doesn't respond with the object. In that case, we'll very likely get it from someone else anyway.
2012-12-05 18:47:30 +01:00
break
2013-02-04 22:49:02 +01:00
if len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave ) > 0 :
2012-12-19 17:51:51 +01:00
printLock . acquire ( )
2013-02-04 22:49:02 +01:00
print ' within processData, number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now ' , len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave )
2012-12-19 17:51:51 +01:00
printLock . release ( )
if len ( self . ackDataThatWeHaveYetToSend ) > 0 :
self . data = self . ackDataThatWeHaveYetToSend . pop ( )
2012-12-05 18:47:30 +01:00
self . processData ( )
else :
print ' Checksum incorrect. Clearing this message. '
self . data = self . data [ self . payloadLength + 24 : ]
2012-11-19 20:45:05 +01:00
2012-11-23 09:22:56 +01:00
def isProofOfWorkSufficient ( self ) :
POW , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( self . data [ 24 : 32 ] + hashlib . sha512 ( self . data [ 32 : 24 + self . payloadLength ] ) . digest ( ) ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
2012-12-05 18:47:30 +01:00
#print 'POW:', POW
2012-11-23 09:22:56 +01:00
#Notice that I have divided the averageProofOfWorkNonceTrialsPerByte by two. This makes the POW requirement easier. This gives us wiggle-room: if we decide that we want to make the POW easier, the change won't obsolete old clients because they already expect a lower POW. If we decide that the current work done by clients feels approperate then we can remove this division by 2 and make the requirement match what is actually done by a sending node. If we want to raise the POW requirement then old nodes will HAVE to upgrade no matter what.
return POW < 2 * * 64 / ( ( self . payloadLength + payloadLengthExtraBytes ) * ( averageProofOfWorkNonceTrialsPerByte / 2 ) )
2012-11-19 20:45:05 +01:00
def sendpong ( self ) :
print ' Sending pong '
2012-11-23 09:22:56 +01:00
self . sock . sendall ( ' \xE9 \xBE \xB4 \xD9 \x70 \x6F \x6E \x67 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xcf \x83 \xe1 \x35 ' )
2012-11-19 20:45:05 +01:00
def recverack ( self ) :
print ' verack received '
self . verackReceived = True
if self . verackSent == True :
#We have thus both sent and received a verack.
self . connectionFullyEstablished ( )
def connectionFullyEstablished ( self ) :
self . connectionIsOrWasFullyEstablished = True
if not self . initiatedConnection :
self . emit ( SIGNAL ( " setStatusIcon(PyQt_PyObject) " ) , ' green ' )
#Update the 'Network Status' tab
connectionsCountLock . acquire ( )
connectionsCount [ self . streamNumber ] + = 1
self . emit ( SIGNAL ( " updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject) " ) , self . streamNumber , connectionsCount [ self . streamNumber ] )
connectionsCountLock . release ( )
remoteNodeIncomingPort , remoteNodeSeenTime = knownNodes [ self . streamNumber ] [ self . HOST ]
2012-11-23 09:22:56 +01:00
printLock . acquire ( )
print ' Connection fully established with ' , self . HOST , remoteNodeIncomingPort
2012-11-19 20:45:05 +01:00
print ' broadcasting addr from within connectionFullyEstablished function. '
2012-11-23 09:22:56 +01:00
printLock . release ( )
self . broadcastaddr ( [ ( int ( time . time ( ) ) , self . streamNumber , 1 , self . HOST , remoteNodeIncomingPort ) ] ) #This lets all of our peers know about this new node.
2013-01-22 06:21:32 +01:00
self . sendaddr ( ) #This is one large addr message to this one peer.
2012-11-19 20:45:05 +01:00
if connectionsCount [ self . streamNumber ] > 150 :
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' We are connected to too many people. Closing connection. '
2013-01-22 06:21:32 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
self . sock . close ( )
return
self . sendBigInv ( )
def sendBigInv ( self ) : #I used capitals in for this function name because there is no such Bitmessage command as 'biginv'.
if self . receivedgetbiginv :
print ' We have already sent a big inv message to this peer. Ignoring request. '
return
else :
self . receivedgetbiginv = True
sqlLock . acquire ( )
#Select all hashes which are younger than two days old and in this stream.
2013-01-04 23:21:33 +01:00
t = ( int ( time . time ( ) ) - maximumAgeOfObjectsThatIAdvertiseToOthers , self . streamNumber )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( ''' SELECT hash FROM inventory WHERE receivedtime>? and streamnumber=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
2013-02-18 21:22:48 +01:00
sqlLock . release ( )
2012-11-19 20:45:05 +01:00
bigInvList = { }
for row in queryreturn :
hash , = row
2013-02-03 06:16:50 +01:00
if hash not in self . objectsOfWhichThisRemoteNodeIsAlreadyAware :
bigInvList [ hash ] = 0
else :
printLock . acquire ( )
print ' Not including an object hash in a big inv message because the remote node is already aware of it. ' #This line is here to check that this feature is working.
printLock . release ( )
2013-01-22 06:21:32 +01:00
#We also have messages in our inventory in memory (which is a python dictionary). Let's fetch those too.
2012-11-19 20:45:05 +01:00
for hash , storedValue in inventory . items ( ) :
2013-02-03 06:16:50 +01:00
if hash not in self . objectsOfWhichThisRemoteNodeIsAlreadyAware :
objectType , streamNumber , payload , receivedTime = storedValue
if streamNumber == self . streamNumber and receivedTime > int ( time . time ( ) ) - maximumAgeOfObjectsThatIAdvertiseToOthers :
bigInvList [ hash ] = 0
else :
printLock . acquire ( )
print ' Not including an object hash in a big inv message because the remote node is already aware of it. ' #This line is here to check that this feature is working.
printLock . release ( )
2012-11-19 20:45:05 +01:00
numberOfObjectsInInvMessage = 0
payload = ' '
2013-01-22 06:21:32 +01:00
#Now let us start appending all of these hashes together. They will be sent out in a big inv message to our new peer.
2012-11-19 20:45:05 +01:00
for hash , storedValue in bigInvList . items ( ) :
payload + = hash
numberOfObjectsInInvMessage + = 1
if numberOfObjectsInInvMessage > = 50000 : #We can only send a max of 50000 items per inv message but we may have more objects to advertise. They must be split up into multiple inv messages.
2013-01-22 06:21:32 +01:00
self . sendinvMessageToJustThisOnePeer ( numberOfObjectsInInvMessage , payload )
2012-11-19 20:45:05 +01:00
payload = ' '
numberOfObjectsInInvMessage = 0
if numberOfObjectsInInvMessage > 0 :
2012-11-23 09:22:56 +01:00
self . sendinvMessageToJustThisOnePeer ( numberOfObjectsInInvMessage , payload )
2013-02-18 21:22:48 +01:00
2012-11-23 09:22:56 +01:00
#Self explanatory. Notice that there is also a broadcastinv function for broadcasting invs to everyone in our stream.
def sendinvMessageToJustThisOnePeer ( self , numberOfObjects , payload ) :
2012-11-19 20:45:05 +01:00
payload = encodeVarint ( numberOfObjects ) + payload
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' inv \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) )
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2013-02-26 23:20:43 +01:00
printLock . acquire ( )
2013-02-03 06:16:50 +01:00
print ' Sending huge inv message with ' , numberOfObjects , ' objects to just this one peer '
2013-02-26 23:20:43 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
self . sock . send ( headerData + payload )
#We have received a broadcast message
def recbroadcast ( self ) :
2013-02-06 22:17:49 +01:00
self . messageProcessingStartTime = time . time ( )
2012-11-19 20:45:05 +01:00
#First we must check to make sure the proof of work is sufficient.
2012-11-23 09:22:56 +01:00
if not self . isProofOfWorkSufficient ( ) :
print ' Proof of work in broadcast message insufficient. '
2012-11-19 20:45:05 +01:00
return
embeddedTime , = unpack ( ' >I ' , self . data [ 32 : 36 ] )
if embeddedTime > ( int ( time . time ( ) ) + 10800 ) : #prevent funny business
print ' The embedded time in this broadcast message is more than three hours in the future. That doesn \' t make sense. Ignoring message. '
return
if embeddedTime < ( int ( time . time ( ) ) - maximumAgeOfAnObjectThatIAmWillingToAccept ) :
print ' The embedded time in this broadcast message is too old. Ignoring message. '
return
2013-02-12 21:00:04 +01:00
if self . payloadLength < 66 : #todo: When version 1 addresses are completely abandoned, this should be changed to 180
2012-11-19 20:45:05 +01:00
print ' The payload length of this broadcast packet is unreasonably low. Someone is probably trying funny business. Ignoring message. '
return
2012-12-18 22:36:37 +01:00
inventoryLock . acquire ( )
2013-02-06 22:17:49 +01:00
self . inventoryHash = calculateInventoryHash ( self . data [ 24 : self . payloadLength + 24 ] )
if self . inventoryHash in inventory :
2012-11-19 20:45:05 +01:00
print ' We have already received this broadcast object. Ignoring. '
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
2012-11-19 20:45:05 +01:00
return
2013-02-06 22:17:49 +01:00
elif isInSqlInventory ( self . inventoryHash ) :
2012-11-19 20:45:05 +01:00
print ' We have already received this broadcast object (it is stored on disk in the SQL inventory). Ignoring it. '
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
2012-11-19 20:45:05 +01:00
return
#It is valid so far. Let's let our peers know about it.
objectType = ' broadcast '
2013-02-06 22:17:49 +01:00
inventory [ self . inventoryHash ] = ( objectType , self . streamNumber , self . data [ 24 : self . payloadLength + 24 ] , embeddedTime )
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
2013-02-06 22:17:49 +01:00
self . broadcastinv ( self . inventoryHash )
2012-11-19 20:45:05 +01:00
self . emit ( SIGNAL ( " incrementNumberOfBroadcastsProcessed() " ) )
2013-02-18 21:22:48 +01:00
2013-02-06 22:17:49 +01:00
self . processbroadcast ( ) #When this function returns, we will have either successfully processed this broadcast because we are interested in it, ignored it because we aren't interested in it, or found problem with the broadcast that warranted ignoring it.
2013-02-18 21:22:48 +01:00
# Let us now set lengthOfTimeWeShouldUseToProcessThisMessage. If we haven't used the specified amount of time, we shall sleep. These values are mostly the same values used for msg messages although broadcast messages are processed faster.
2013-02-06 22:17:49 +01:00
if self . payloadLength > 100000000 : #Size is greater than 100 megabytes
lengthOfTimeWeShouldUseToProcessThisMessage = 100 #seconds.
elif self . payloadLength > 10000000 : #Between 100 and 10 megabytes
lengthOfTimeWeShouldUseToProcessThisMessage = 20 #seconds.
elif self . payloadLength > 1000000 : #Between 10 and 1 megabyte
2013-02-18 21:22:48 +01:00
lengthOfTimeWeShouldUseToProcessThisMessage = 3 #seconds.
2013-02-06 22:17:49 +01:00
else : #Less than 1 megabyte
lengthOfTimeWeShouldUseToProcessThisMessage = .1 #seconds.
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - ( time . time ( ) - self . messageProcessingStartTime )
if sleepTime > 0 :
printLock . acquire ( )
print ' Timing attack mitigation: Sleeping for ' , sleepTime , ' seconds. '
printLock . release ( )
time . sleep ( sleepTime )
printLock . acquire ( )
print ' Total message processing time: ' , time . time ( ) - self . messageProcessingStartTime , ' seconds. '
printLock . release ( )
#A broadcast message has a valid time and POW and requires processing. The recbroadcast function calls this one.
def processbroadcast ( self ) :
2012-11-19 20:45:05 +01:00
readPosition = 36
broadcastVersion , broadcastVersionLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
if broadcastVersion < > 1 :
#Cannot decode incoming broadcast versions higher than 1. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.
return
readPosition + = broadcastVersionLength
2013-03-05 18:21:55 +01:00
beginningOfPubkeyPosition = readPosition #used when we add the pubkey to our pubkey table
2012-11-19 20:45:05 +01:00
sendersAddressVersion , sendersAddressVersionLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
2013-03-01 23:44:52 +01:00
if sendersAddressVersion < = 1 or sendersAddressVersion > = 3 :
2013-01-24 20:52:48 +01:00
#Cannot decode senderAddressVersion higher than 2. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.
2012-11-19 20:45:05 +01:00
return
readPosition + = sendersAddressVersionLength
2013-01-24 20:52:48 +01:00
if sendersAddressVersion == 2 :
sendersStream , sendersStreamLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
if sendersStream < = 0 or sendersStream < > self . streamNumber :
return
readPosition + = sendersStreamLength
behaviorBitfield = self . data [ readPosition : readPosition + 4 ]
readPosition + = 4
sendersPubSigningKey = ' \x04 ' + self . data [ readPosition : readPosition + 64 ]
readPosition + = 64
sendersPubEncryptionKey = ' \x04 ' + self . data [ readPosition : readPosition + 64 ]
readPosition + = 64
2013-03-05 18:21:55 +01:00
endOfPubkeyPosition = readPosition
2013-01-24 20:52:48 +01:00
sendersHash = self . data [ readPosition : readPosition + 20 ]
if sendersHash not in broadcastSendersForWhichImWatching :
2013-02-06 22:17:49 +01:00
#Display timing data
2013-02-05 22:53:56 +01:00
printLock . acquire ( )
2013-02-06 22:17:49 +01:00
print ' Time spent deciding that we are not interested in this broadcast: ' , time . time ( ) - self . messageProcessingStartTime
2013-02-05 22:53:56 +01:00
printLock . release ( )
2013-01-24 20:52:48 +01:00
return
#At this point, this message claims to be from sendersHash and we are interested in it. We still have to hash the public key to make sure it is truly the key that matches the hash, and also check the signiture.
readPosition + = 20
2013-02-06 22:17:49 +01:00
2013-01-24 20:52:48 +01:00
sha = hashlib . new ( ' sha512 ' )
sha . update ( sendersPubSigningKey + sendersPubEncryptionKey )
ripe = hashlib . new ( ' ripemd160 ' )
ripe . update ( sha . digest ( ) )
if ripe . digest ( ) != sendersHash :
#The sender of this message lied.
2013-02-06 22:17:49 +01:00
return
2013-01-24 20:52:48 +01:00
messageEncodingType , messageEncodingTypeLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
if messageEncodingType == 0 :
return
readPosition + = messageEncodingTypeLength
messageLength , messageLengthLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
readPosition + = messageLengthLength
message = self . data [ readPosition : readPosition + messageLength ]
readPosition + = messageLength
readPositionAtBottomOfMessage = readPosition
signatureLength , signatureLengthLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
readPosition + = signatureLengthLength
signature = self . data [ readPosition : readPosition + signatureLength ]
try :
highlevelcrypto . verify ( self . data [ 36 : readPositionAtBottomOfMessage ] , signature , sendersPubSigningKey . encode ( ' hex ' ) )
print ' ECDSA verify passed '
except Exception , err :
print ' ECDSA verify failed ' , err
return
#verify passed
2013-03-05 18:21:55 +01:00
#Let's store the public key in case we want to reply to this person.
#We don't have the correct nonce or time (which would let us send out a pubkey message) so we'll just fill it with 1's. We won't be able to send this pubkey to others (without doing the proof of work ourselves, which this program is programmed to not do.)
t = ( ripe . digest ( ) , False , ' \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF ' + ' \xFF \xFF \xFF \xFF ' + self . data [ beginningOfPubkeyPosition : endOfPubkeyPosition ] , int ( time . time ( ) ) , ' yes ' )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
workerQueue . put ( ( ' newpubkey ' , ( sendersAddressVersion , sendersStream , ripe . digest ( ) ) ) ) #This will check to see whether we happen to be awaiting this pubkey in order to send a message. If we are, it will do the POW and send it.
2013-01-24 20:52:48 +01:00
fromAddress = encodeAddress ( sendersAddressVersion , sendersStream , ripe . digest ( ) )
print ' fromAddress: ' , fromAddress
if messageEncodingType == 2 :
bodyPositionIndex = string . find ( message , ' \n Body: ' )
if bodyPositionIndex > 1 :
subject = message [ 8 : bodyPositionIndex ]
body = message [ bodyPositionIndex + 6 : ]
else :
subject = ' '
body = message
elif messageEncodingType == 1 :
body = message
subject = ' '
elif messageEncodingType == 0 :
print ' messageEncodingType == 0. Doing nothing with the message. '
2012-12-04 18:11:14 +01:00
else :
2013-01-24 20:52:48 +01:00
body = ' Unknown encoding type. \n \n ' + repr ( message )
2012-12-04 18:11:14 +01:00
subject = ' '
2013-01-24 20:52:48 +01:00
toAddress = ' [Broadcast subscribers] '
if messageEncodingType < > 0 :
sqlLock . acquire ( )
2013-02-06 22:17:49 +01:00
t = ( self . inventoryHash , toAddress , fromAddress , subject , int ( time . time ( ) ) , body , ' inbox ' )
2013-01-24 20:52:48 +01:00
sqlSubmitQueue . put ( ''' INSERT INTO inbox VALUES (?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2013-02-26 00:09:32 +01:00
self . emit ( SIGNAL ( " displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . inventoryHash , toAddress , fromAddress , subject , body )
2013-01-24 20:52:48 +01:00
2013-03-19 18:32:37 +01:00
#If we are behaving as an API then we might need to run an outside command to let some program know that a new message has arrived.
if safeConfigGetBoolean ( ' bitmessagesettings ' , ' apienabled ' ) :
try :
apiNotifyPath = config . get ( ' bitmessagesettings ' , ' apinotifypath ' )
except :
apiNotifyPath = ' '
if apiNotifyPath != ' ' :
call ( [ apiNotifyPath , " newBroadcast " ] )
2013-02-05 22:53:56 +01:00
#Display timing data
printLock . acquire ( )
2013-02-06 22:17:49 +01:00
print ' Time spent processing this interesting broadcast: ' , time . time ( ) - self . messageProcessingStartTime
2013-02-05 22:53:56 +01:00
printLock . release ( )
2013-01-24 20:52:48 +01:00
2013-03-01 23:44:52 +01:00
""" elif sendersAddressVersion == 1:
2013-01-24 20:52:48 +01:00
sendersStream , sendersStreamLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
if sendersStream < = 0 :
return
readPosition + = sendersStreamLength
sendersHash = self . data [ readPosition : readPosition + 20 ]
if sendersHash not in broadcastSendersForWhichImWatching :
return
#At this point, this message claims to be from sendersHash and we are interested in it. We still have to hash the public key to make sure it is truly the key that matches the hash, and also check the signiture.
readPosition + = 20
nLength , nLengthLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
if nLength < 1 :
return
readPosition + = nLengthLength
nString = self . data [ readPosition : readPosition + nLength ]
readPosition + = nLength
eLength , eLengthLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
if eLength < 1 :
return
readPosition + = eLengthLength
eString = self . data [ readPosition : readPosition + eLength ]
#We are now ready to hash the public key and verify that its hash matches the hash claimed in the message
readPosition + = eLength
sha = hashlib . new ( ' sha512 ' )
sha . update ( nString + eString )
ripe = hashlib . new ( ' ripemd160 ' )
ripe . update ( sha . digest ( ) )
if ripe . digest ( ) != sendersHash :
#The sender of this message lied.
return
readPositionAtBeginningOfMessageEncodingType = readPosition
messageEncodingType , messageEncodingTypeLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
if messageEncodingType == 0 :
return
readPosition + = messageEncodingTypeLength
messageLength , messageLengthLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
readPosition + = messageLengthLength
message = self . data [ readPosition : readPosition + messageLength ]
readPosition + = messageLength
signature = self . data [ readPosition : readPosition + nLength ]
sendersPubkey = rsa . PublicKey ( convertStringToInt ( nString ) , convertStringToInt ( eString ) )
#print 'senders Pubkey', sendersPubkey
try :
rsa . verify ( self . data [ readPositionAtBeginningOfMessageEncodingType : readPositionAtBeginningOfMessageEncodingType + messageEncodingTypeLength + messageLengthLength + messageLength ] , signature , sendersPubkey )
print ' verify passed '
except Exception , err :
print ' verify failed ' , err
return
#verify passed
fromAddress = encodeAddress ( sendersAddressVersion , sendersStream , ripe . digest ( ) )
print ' fromAddress: ' , fromAddress
if messageEncodingType == 2 :
bodyPositionIndex = string . find ( message , ' \n Body: ' )
if bodyPositionIndex > 1 :
subject = message [ 8 : bodyPositionIndex ]
body = message [ bodyPositionIndex + 6 : ]
else :
subject = ' '
body = message
elif messageEncodingType == 1 :
2012-12-04 18:11:14 +01:00
body = message
2013-01-24 20:52:48 +01:00
subject = ' '
elif messageEncodingType == 0 :
print ' messageEncodingType == 0. Doing nothing with the message. '
else :
body = ' Unknown encoding type. \n \n ' + repr ( message )
subject = ' '
2012-11-19 20:45:05 +01:00
2013-01-24 20:52:48 +01:00
toAddress = ' [Broadcast subscribers] '
if messageEncodingType < > 0 :
sqlLock . acquire ( )
2013-02-06 22:17:49 +01:00
t = ( self . inventoryHash , toAddress , fromAddress , subject , int ( time . time ( ) ) , body , ' inbox ' )
2013-01-24 20:52:48 +01:00
sqlSubmitQueue . put ( ''' INSERT INTO inbox VALUES (?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2013-03-01 23:44:52 +01:00
self . emit ( SIGNAL ( " displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . inventoryHash , toAddress , fromAddress , subject , body ) """
2013-02-06 22:17:49 +01:00
2012-11-19 20:45:05 +01:00
#We have received a msg message.
def recmsg ( self ) :
2013-02-06 22:17:49 +01:00
self . messageProcessingStartTime = time . time ( )
2012-11-19 20:45:05 +01:00
#First we must check to make sure the proof of work is sufficient.
2012-11-23 09:22:56 +01:00
if not self . isProofOfWorkSufficient ( ) :
2012-11-19 20:45:05 +01:00
print ' Proof of work in msg message insufficient. '
return
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
readPosition = 32
embeddedTime , = unpack ( ' >I ' , self . data [ readPosition : readPosition + 4 ] )
if embeddedTime > int ( time . time ( ) ) + 10800 :
print ' The time in the msg message is too new. Ignoring it. Time: ' , embeddedTime
return
if embeddedTime < int ( time . time ( ) ) - maximumAgeOfAnObjectThatIAmWillingToAccept :
print ' The time in the msg message is too old. Ignoring it. Time: ' , embeddedTime
return
readPosition + = 4
2012-12-18 22:36:37 +01:00
streamNumberAsClaimedByMsg , streamNumberAsClaimedByMsgLength = decodeVarint ( self . data [ readPosition : readPosition + 9 ] )
if streamNumberAsClaimedByMsg != self . streamNumber :
2013-02-04 22:49:02 +01:00
print ' The stream number encoded in this msg ( ' + str ( streamNumberAsClaimedByMsg ) + ' ) message does not match the stream number on which it was received. Ignoring it. '
2012-12-18 22:36:37 +01:00
return
readPosition + = streamNumberAsClaimedByMsgLength
2013-02-06 22:17:49 +01:00
self . inventoryHash = calculateInventoryHash ( self . data [ 24 : self . payloadLength + 24 ] )
2012-12-18 22:36:37 +01:00
inventoryLock . acquire ( )
2013-02-06 22:17:49 +01:00
if self . inventoryHash in inventory :
2012-11-19 20:45:05 +01:00
print ' We have already received this msg message. Ignoring. '
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
2012-11-19 20:45:05 +01:00
return
2013-02-06 22:17:49 +01:00
elif isInSqlInventory ( self . inventoryHash ) :
2012-11-19 20:45:05 +01:00
print ' We have already received this msg message (it is stored on disk in the SQL inventory). Ignoring it. '
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
2012-11-19 20:45:05 +01:00
return
#This msg message is valid. Let's let our peers know about it.
objectType = ' msg '
2013-02-06 22:17:49 +01:00
inventory [ self . inventoryHash ] = ( objectType , self . streamNumber , self . data [ 24 : self . payloadLength + 24 ] , embeddedTime )
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
2013-02-06 22:17:49 +01:00
self . broadcastinv ( self . inventoryHash )
2012-11-19 20:45:05 +01:00
self . emit ( SIGNAL ( " incrementNumberOfMessagesProcessed() " ) )
2013-02-06 22:17:49 +01:00
self . processmsg ( readPosition ) #When this function returns, we will have either successfully processed the message bound for us, ignored it because it isn't bound for us, or found problem with the message that warranted ignoring it.
# Let us now set lengthOfTimeWeShouldUseToProcessThisMessage. If we haven't used the specified amount of time, we shall sleep. These values are based on test timings and you may change them at-will.
if self . payloadLength > 100000000 : #Size is greater than 100 megabytes
lengthOfTimeWeShouldUseToProcessThisMessage = 100 #seconds. Actual length of time it took my computer to decrypt and verify the signature of a 100 MB message: 3.7 seconds.
elif self . payloadLength > 10000000 : #Between 100 and 10 megabytes
lengthOfTimeWeShouldUseToProcessThisMessage = 20 #seconds. Actual length of time it took my computer to decrypt and verify the signature of a 10 MB message: 0.53 seconds. Actual length of time it takes in practice when processing a real message: 1.44 seconds.
elif self . payloadLength > 1000000 : #Between 10 and 1 megabyte
lengthOfTimeWeShouldUseToProcessThisMessage = 3 #seconds. Actual length of time it took my computer to decrypt and verify the signature of a 1 MB message: 0.18 seconds. Actual length of time it takes in practice when processing a real message: 0.30 seconds.
else : #Less than 1 megabyte
lengthOfTimeWeShouldUseToProcessThisMessage = .6 #seconds. Actual length of time it took my computer to decrypt and verify the signature of a 100 KB message: 0.15 seconds. Actual length of time it takes in practice when processing a real message: 0.25 seconds.
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - ( time . time ( ) - self . messageProcessingStartTime )
if sleepTime > 0 :
printLock . acquire ( )
print ' Timing attack mitigation: Sleeping for ' , sleepTime , ' seconds. '
printLock . release ( )
time . sleep ( sleepTime )
printLock . acquire ( )
print ' Total message processing time: ' , time . time ( ) - self . messageProcessingStartTime , ' seconds. '
printLock . release ( )
#This section is for my RSA keys (version 1 addresses). If we don't have any version 1 addresses it will never run. This code will soon be removed.
2013-03-01 23:44:52 +01:00
""" initialDecryptionSuccessful = False
2013-02-06 22:17:49 +01:00
infile = cStringIO . StringIO ( self . data [ readPosition : self . payloadLength + 24 ] )
outfile = cStringIO . StringIO ( )
#print 'len(myRSAAddressHashes.items()):', len(myRSAAddressHashes.items())
for key , value in myRSAAddressHashes . items ( ) :
try :
decrypt_bigfile ( infile , outfile , value )
#The initial decryption passed though there is a small chance that the message isn't actually for me. We'll need to check that the 20 zeros are present.
#print 'initial decryption successful using key', repr(key)
initialDecryptionSuccessful = True
printLock . acquire ( )
print ' Initial decryption passed '
printLock . release ( )
break
except Exception , err :
infile . seek ( 0 )
#print 'Exception:', err
#print 'outfile len is:', len(outfile.getvalue()),'data is:', repr(outfile.getvalue())
#print 'Initial decryption failed using key', value
#decryption failed for this key. The message is for someone else (or for a different key of mine).
if initialDecryptionSuccessful and outfile . getvalue ( ) [ : 20 ] == ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' : #this run of 0s allows the true message receiver to identify his message
#This is clearly a message bound for me.
outfile . seek ( 0 )
data = outfile . getvalue ( )
readPosition = 20 #To start reading past the 20 zero bytes
messageVersion , messageVersionLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = messageVersionLength
if messageVersion == 1 :
bitfieldBehavior = data [ readPosition : readPosition + 4 ]
readPosition + = 4
sendersAddressVersionNumber , sendersAddressVersionNumberLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
if sendersAddressVersionNumber == 1 :
readPosition + = sendersAddressVersionNumberLength
sendersStreamNumber , sendersStreamNumberLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
if sendersStreamNumber == 0 :
print ' sendersStreamNumber = 0. Ignoring message '
else :
readPosition + = sendersStreamNumberLength
sendersNLength , sendersNLengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = sendersNLengthLength
sendersN = data [ readPosition : readPosition + sendersNLength ]
readPosition + = sendersNLength
sendersELength , sendersELengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = sendersELengthLength
sendersE = data [ readPosition : readPosition + sendersELength ]
readPosition + = sendersELength
endOfThePublicKeyPosition = readPosition
messageEncodingType , messageEncodingTypeLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = messageEncodingTypeLength
print ' Message Encoding Type: ' , messageEncodingType
messageLength , messageLengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
print ' message length: ' , messageLength
readPosition + = messageLengthLength
message = data [ readPosition : readPosition + messageLength ]
#print 'First 150 characters of message:', repr(message[:150])
readPosition + = messageLength
ackLength , ackLengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
#print 'ackLength:', ackLength
readPosition + = ackLengthLength
ackData = data [ readPosition : readPosition + ackLength ]
readPosition + = ackLength
payloadSigniture = data [ readPosition : readPosition + sendersNLength ] #We're using the length of the sender's n because it should match the signiture size.
sendersPubkey = rsa . PublicKey ( convertStringToInt ( sendersN ) , convertStringToInt ( sendersE ) )
print ' sender \' s Pubkey ' , sendersPubkey
#Check the cryptographic signiture
verifyPassed = False
try :
rsa . verify ( data [ : - len ( payloadSigniture ) ] , payloadSigniture , sendersPubkey )
print ' verify passed '
verifyPassed = True
except Exception , err :
print ' verify failed ' , err
if verifyPassed :
#calculate the fromRipe.
sha = hashlib . new ( ' sha512 ' )
sha . update ( sendersN + sendersE )
ripe = hashlib . new ( ' ripemd160 ' )
ripe . update ( sha . digest ( ) )
#Let's store the public key in case we want to reply to this person.
#We don't have the correct nonce in order to send out a pubkey message so we'll just fill it with 1's. We won't be able to send this pubkey to others (without doing the proof of work ourselves, which this program is programmed to not do.)
2013-02-11 22:28:38 +01:00
t = ( ripe . digest ( ) , False , ' \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF ' + data [ 20 + messageVersionLength : endOfThePublicKeyPosition ] , int ( time . time ( ) ) , ' yes ' )
2013-02-06 22:17:49 +01:00
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?,?) ''' )
2013-02-06 22:17:49 +01:00
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
blockMessage = False #Gets set to True if the user shouldn't see the message according to black or white lists.
fromAddress = encodeAddress ( sendersAddressVersionNumber , sendersStreamNumber , ripe . digest ( ) )
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' : #If we are using a blacklist
t = ( fromAddress , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' SELECT label, enabled FROM blacklist where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
for row in queryreturn :
label , enabled = row
if enabled :
print ' Message ignored because address is in blacklist. '
blockMessage = True
else : #We're using a whitelist
t = ( fromAddress , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' SELECT label, enabled FROM whitelist where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn == [ ] :
print ' Message ignored because address not in whitelist. '
blockMessage = True
for row in queryreturn : #It could be in the whitelist but disabled. Let's check.
label , enabled = row
if not enabled :
print ' Message ignored because address in whitelist but not enabled. '
blockMessage = True
if not blockMessage :
print ' fromAddress: ' , fromAddress
print ' First 150 characters of message: ' , repr ( message [ : 150 ] )
#Look up the destination address (my address) based on the destination ripe hash.
#I realize that I could have a data structure devoted to this task, or maintain an indexed table
#in the sql database, but I would prefer to minimize the number of data structures this program
#uses. Searching linearly through the user's short list of addresses doesn't take very long anyway.
configSections = config . sections ( )
for addressInKeysFile in configSections :
if addressInKeysFile < > ' bitmessagesettings ' :
status , addressVersionNumber , streamNumber , hash = decodeAddress ( addressInKeysFile )
if hash == key :
toAddress = addressInKeysFile
toLabel = config . get ( addressInKeysFile , ' label ' )
if toLabel == ' ' :
toLabel = addressInKeysFile
break
if messageEncodingType == 2 :
bodyPositionIndex = string . find ( message , ' \n Body: ' )
if bodyPositionIndex > 1 :
subject = message [ 8 : bodyPositionIndex ]
body = message [ bodyPositionIndex + 6 : ]
else :
subject = ' '
body = message
elif messageEncodingType == 1 :
body = message
subject = ' '
elif messageEncodingType == 0 :
print ' messageEncodingType == 0. Doing nothing with the message. They probably just sent it so that we would store their public key or send their ack data for them. '
else :
body = ' Unknown encoding type. \n \n ' + repr ( message )
subject = ' '
print ' within recmsg, self.inventoryHash is ' , repr ( self . inventoryHash )
if messageEncodingType < > 0 :
sqlLock . acquire ( )
t = ( self . inventoryHash , toAddress , fromAddress , subject , int ( time . time ( ) ) , body , ' inbox ' )
sqlSubmitQueue . put ( ''' INSERT INTO inbox VALUES (?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2013-02-26 00:09:32 +01:00
self . emit ( SIGNAL ( " displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . inventoryHash , toAddress , fromAddress , subject , body )
2013-02-06 22:17:49 +01:00
#Now let us worry about the acknowledgement data
#We'll need to make sure that our client will properly process the ackData; if the packet is malformed, it might cause us to clear out self.data and an attacker could use that behavior to determine that we decoded this message.
ackDataValidThusFar = True
if len ( ackData ) < 24 :
print ' The length of ackData is unreasonably short. Not sending ackData. '
ackDataValidThusFar = False
if ackData [ 0 : 4 ] != ' \xe9 \xbe \xb4 \xd9 ' :
print ' Ackdata magic bytes were wrong. Not sending ackData. '
ackDataValidThusFar = False
if ackDataValidThusFar :
ackDataPayloadLength , = unpack ( ' >L ' , ackData [ 16 : 20 ] )
if len ( ackData ) - 24 != ackDataPayloadLength : #This ackData includes the protocol header which is not counted in the payload length.
print ' ackData payload length doesn \' t match the payload length specified in the header. Not sending ackdata. '
ackDataValidThusFar = False
if ackDataValidThusFar :
print ' ackData is valid. Will process it. '
self . ackDataThatWeHaveYetToSend . append ( ackData ) #When we have processed all data, the processData function will pop the ackData out and process it as if it is a message received from our peer.
else :
print ' This program cannot decode messages from addresses with versions higher than 1. Ignoring. '
statusbar = ' This program cannot decode messages from addresses with versions higher than 1. Ignoring it. '
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , statusbar )
else :
statusbar = ' Error: Cannot decode incoming msg versions higher than 1. Assuming the sender isn \' being silly, you should upgrade Bitmessage. Ignoring message. '
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , statusbar )
else :
printLock . acquire ( )
2013-02-06 22:28:18 +01:00
print ' Could not decrypt with any RSA keys if you have any. '
2013-02-06 22:17:49 +01:00
printLock . release ( )
infile . close ( )
2013-03-01 23:44:52 +01:00
outfile . close ( ) """
2013-02-06 22:17:49 +01:00
#A msg message has a valid time and POW and requires processing. The recmsg function calls this one.
def processmsg ( self , readPosition ) :
initialDecryptionSuccessful = False
2012-11-19 20:45:05 +01:00
#Let's check whether this is a message acknowledgement bound for us.
if self . data [ readPosition : 24 + self . payloadLength ] in ackdataForWhichImWatching :
printLock . acquire ( )
2012-11-23 09:22:56 +01:00
print ' This msg IS an acknowledgement bound for me. '
2012-11-19 20:45:05 +01:00
printLock . release ( )
del ackdataForWhichImWatching [ self . data [ readPosition : 24 + self . payloadLength ] ]
t = ( ' ackreceived ' , self . data [ readPosition : 24 + self . payloadLength ] )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ' UPDATE sent SET status=? WHERE ackdata=? ' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
self . emit ( SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , self . data [ readPosition : 24 + self . payloadLength ] , ' Acknowledgement of the message received just now. ' )
return
else :
printLock . acquire ( )
2012-11-23 09:22:56 +01:00
print ' This was NOT an acknowledgement bound for me. ' #Msg potential ack data:', repr(self.data[readPosition:24+self.payloadLength])
#print 'ackdataForWhichImWatching', ackdataForWhichImWatching
2012-11-19 20:45:05 +01:00
printLock . release ( )
#This is not an acknowledgement bound for me. See if it is a message bound for me by trying to decrypt it with my private keys.
2013-01-18 23:38:09 +01:00
for key , cryptorObject in myECAddressHashes . items ( ) :
try :
data = cryptorObject . decrypt ( self . data [ readPosition : self . payloadLength + 24 ] )
toRipe = key #This is the RIPE hash of my pubkeys. We need this below to compare to the destination_ripe included in the encrypted data.
initialDecryptionSuccessful = True
print ' EC decryption successful using key associated with ripe hash: ' , key . encode ( ' hex ' )
break
except Exception , err :
pass
#print 'cryptorObject.decrypt Exception:', err
2013-02-05 22:53:56 +01:00
if not initialDecryptionSuccessful :
#This is not a message bound for me.
printLock . acquire ( )
2013-02-06 22:17:49 +01:00
print ' Length of time program spent failing to decrypt this message: ' , time . time ( ) - self . messageProcessingStartTime , ' seconds. '
2013-02-05 22:53:56 +01:00
printLock . release ( )
else :
2013-01-18 23:38:09 +01:00
#This is a message bound for me.
readPosition = 0
messageVersion , messageVersionLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = messageVersionLength
if messageVersion != 1 :
print ' Cannot understand message versions other than one. Ignoring message. '
return
sendersAddressVersionNumber , sendersAddressVersionNumberLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = sendersAddressVersionNumberLength
if sendersAddressVersionNumber == 0 :
print ' Cannot understand sendersAddressVersionNumber = 0. Ignoring message. '
return
if sendersAddressVersionNumber > = 3 :
print ' Sender \' s address version number ' , sendersAddressVersionNumber , ' not yet supported. Ignoring message. '
return
if len ( data ) < 170 :
print ' Length of the unencrypted data is unreasonably short. Sanity check failed. Ignoring message. '
return
sendersStreamNumber , sendersStreamNumberLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
2013-01-24 17:38:12 +01:00
if sendersStreamNumber == 0 :
print ' sender \' s stream number is 0. Ignoring message. '
return
2013-01-18 23:38:09 +01:00
readPosition + = sendersStreamNumberLength
behaviorBitfield = data [ readPosition : readPosition + 4 ]
readPosition + = 4
pubSigningKey = ' \x04 ' + data [ readPosition : readPosition + 64 ]
readPosition + = 64
pubEncryptionKey = ' \x04 ' + data [ readPosition : readPosition + 64 ]
readPosition + = 64
endOfThePublicKeyPosition = readPosition #needed for when we store the pubkey in our database of pubkeys for later use.
if toRipe != data [ readPosition : readPosition + 20 ] :
printLock . acquire ( )
print ' The original sender of this message did not send it to you. Someone is attempting a Surreptitious Forwarding Attack. '
print ' See: http://tools.ietf.org/html/draft-ietf-smime-sender-auth-00 '
print ' your toRipe: ' , toRipe . encode ( ' hex ' )
print ' embedded destination toRipe: ' , data [ readPosition : readPosition + 20 ] . encode ( ' hex ' )
printLock . release ( )
return
readPosition + = 20
messageEncodingType , messageEncodingTypeLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = messageEncodingTypeLength
messageLength , messageLengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = messageLengthLength
message = data [ readPosition : readPosition + messageLength ]
#print 'First 150 characters of message:', repr(message[:150])
readPosition + = messageLength
ackLength , ackLengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = ackLengthLength
ackData = data [ readPosition : readPosition + ackLength ]
readPosition + = ackLength
positionOfBottomOfAckData = readPosition #needed to mark the end of what is covered by the signature
signatureLength , signatureLengthLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
readPosition + = signatureLengthLength
signature = data [ readPosition : readPosition + signatureLength ]
try :
highlevelcrypto . verify ( data [ : positionOfBottomOfAckData ] , signature , pubSigningKey . encode ( ' hex ' ) )
print ' ECDSA verify passed '
except Exception , err :
print ' ECDSA verify failed ' , err
return
printLock . acquire ( )
2013-01-24 23:45:36 +01:00
print ' As a matter of intellectual curiosity, here is the Bitcoin address associated with the keys owned by the other person: ' , calculateBitcoinAddressFromPubkey ( pubSigningKey ) , ' ..and here is the testnet address: ' , calculateTestnetAddressFromPubkey ( pubSigningKey ) , ' . The other person must take their private signing key from Bitmessage and import it into Bitcoin (or a service like Blockchain.info) for it to be of any use. Do not use this unless you know what you are doing. '
2013-01-18 23:38:09 +01:00
printLock . release ( )
#calculate the fromRipe.
sha = hashlib . new ( ' sha512 ' )
sha . update ( pubSigningKey + pubEncryptionKey )
ripe = hashlib . new ( ' ripemd160 ' )
ripe . update ( sha . digest ( ) )
#Let's store the public key in case we want to reply to this person.
#We don't have the correct nonce or time (which would let us send out a pubkey message) so we'll just fill it with 1's. We won't be able to send this pubkey to others (without doing the proof of work ourselves, which this program is programmed to not do.)
2013-02-11 22:28:38 +01:00
t = ( ripe . digest ( ) , False , ' \xFF \xFF \xFF \xFF \xFF \xFF \xFF \xFF ' + ' \xFF \xFF \xFF \xFF ' + data [ messageVersionLength : endOfThePublicKeyPosition ] , int ( time . time ( ) ) , ' yes ' )
2013-01-18 23:38:09 +01:00
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?,?) ''' )
2013-01-18 23:38:09 +01:00
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2013-03-01 23:44:52 +01:00
workerQueue . put ( ( ' newpubkey ' , ( sendersAddressVersionNumber , sendersStreamNumber , ripe . digest ( ) ) ) ) #This will check to see whether we happen to be awaiting this pubkey in order to send a message. If we are, it will do the POW and send it.
2013-01-18 23:38:09 +01:00
blockMessage = False #Gets set to True if the user shouldn't see the message according to black or white lists.
fromAddress = encodeAddress ( sendersAddressVersionNumber , sendersStreamNumber , ripe . digest ( ) )
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' : #If we are using a blacklist
t = ( fromAddress , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' SELECT label, enabled FROM blacklist where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
for row in queryreturn :
label , enabled = row
if enabled :
2013-02-26 00:09:32 +01:00
printLock . acquire ( )
2013-01-18 23:38:09 +01:00
print ' Message ignored because address is in blacklist. '
2013-02-26 00:09:32 +01:00
printLock . release ( )
2013-01-18 23:38:09 +01:00
blockMessage = True
else : #We're using a whitelist
t = ( fromAddress , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' SELECT label, enabled FROM whitelist where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn == [ ] :
print ' Message ignored because address not in whitelist. '
blockMessage = True
for row in queryreturn : #It could be in the whitelist but disabled. Let's check.
label , enabled = row
if not enabled :
print ' Message ignored because address in whitelist but not enabled. '
blockMessage = True
if not blockMessage :
print ' fromAddress: ' , fromAddress
print ' First 150 characters of message: ' , repr ( message [ : 150 ] )
#Look up the destination address (my address) based on the destination ripe hash.
#I realize that I could have a data structure devoted to this task, or maintain an indexed table
#in the sql database, but I would prefer to minimize the number of data structures this program
#uses. Searching linearly through the user's short list of addresses doesn't take very long anyway.
configSections = config . sections ( )
for addressInKeysFile in configSections :
if addressInKeysFile < > ' bitmessagesettings ' :
status , addressVersionNumber , streamNumber , hash = decodeAddress ( addressInKeysFile )
if hash == key :
toAddress = addressInKeysFile
toLabel = config . get ( addressInKeysFile , ' label ' )
if toLabel == ' ' :
toLabel = addressInKeysFile
if messageEncodingType == 2 :
bodyPositionIndex = string . find ( message , ' \n Body: ' )
if bodyPositionIndex > 1 :
subject = message [ 8 : bodyPositionIndex ]
body = message [ bodyPositionIndex + 6 : ]
else :
subject = ' '
body = message
elif messageEncodingType == 1 :
body = message
subject = ' '
elif messageEncodingType == 0 :
print ' messageEncodingType == 0. Doing nothing with the message. They probably just sent it so that we would store their public key or send their ack data for them. '
else :
body = ' Unknown encoding type. \n \n ' + repr ( message )
subject = ' '
if messageEncodingType < > 0 :
sqlLock . acquire ( )
2013-02-06 22:17:49 +01:00
t = ( self . inventoryHash , toAddress , fromAddress , subject , int ( time . time ( ) ) , body , ' inbox ' )
2013-01-18 23:38:09 +01:00
sqlSubmitQueue . put ( ''' INSERT INTO inbox VALUES (?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2013-02-26 00:09:32 +01:00
self . emit ( SIGNAL ( " displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . inventoryHash , toAddress , fromAddress , subject , body )
2013-03-19 18:32:37 +01:00
#If we are behaving as an API then we might need to run an outside command to let some program know that a new message has arrived.
if safeConfigGetBoolean ( ' bitmessagesettings ' , ' apienabled ' ) :
try :
apiNotifyPath = config . get ( ' bitmessagesettings ' , ' apinotifypath ' )
except :
apiNotifyPath = ' '
if apiNotifyPath != ' ' :
call ( [ apiNotifyPath , " newMessage " ] )
2013-02-26 00:09:32 +01:00
#Let us now check and see whether our receiving address is behaving as a mailing list
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( toAddress , ' mailinglist ' ) :
2013-02-26 00:09:32 +01:00
try :
mailingListName = config . get ( toAddress , ' mailinglistname ' )
except :
mailingListName = ' '
#Let us send out this message as a broadcast
subject = self . addMailingListNameToSubject ( subject , mailingListName )
#Let us now send this message out as a broadcast
message = ' Message ostensibly from ' + fromAddress + ' : \n \n ' + body
fromAddress = toAddress #The fromAddress for the broadcast is the toAddress (my address) for the msg message we are currently processing.
ackdata = OpenSSL . rand ( 32 ) #We don't actually need the ackdata for acknowledgement since this is a broadcast message but we can use it to update the user interface when the POW is done generating.
toAddress = ' [Broadcast subscribers] '
ripe = ' '
sqlLock . acquire ( )
t = ( ' ' , toAddress , ripe , fromAddress , subject , message , ackdata , int ( time . time ( ) ) , ' broadcastpending ' , 1 , 1 , ' sent ' )
sqlSubmitQueue . put ( ''' INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
self . emit ( SIGNAL ( " displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , toAddress , ' [Broadcast subscribers] ' , fromAddress , subject , message , ackdata )
2013-03-19 18:32:37 +01:00
workerQueue . put ( ( ' sendbroadcast ' , ( fromAddress , subject , message ) ) )
2013-02-03 06:16:50 +01:00
#Now let's consider sending the acknowledgement. We'll need to make sure that our client will properly process the ackData; if the packet is malformed, we could clear out self.data and an attacker could use that behavior to determine that we were capable of decoding this message.
2013-01-18 23:38:09 +01:00
ackDataValidThusFar = True
if len ( ackData ) < 24 :
print ' The length of ackData is unreasonably short. Not sending ackData. '
ackDataValidThusFar = False
2013-01-22 06:21:32 +01:00
elif ackData [ 0 : 4 ] != ' \xe9 \xbe \xb4 \xd9 ' :
2013-01-18 23:38:09 +01:00
print ' Ackdata magic bytes were wrong. Not sending ackData. '
ackDataValidThusFar = False
if ackDataValidThusFar :
ackDataPayloadLength , = unpack ( ' >L ' , ackData [ 16 : 20 ] )
if len ( ackData ) - 24 != ackDataPayloadLength :
print ' ackData payload length doesn \' t match the payload length specified in the header. Not sending ackdata. '
ackDataValidThusFar = False
if ackDataValidThusFar :
print ' ackData is valid. Will process it. '
self . ackDataThatWeHaveYetToSend . append ( ackData ) #When we have processed all data, the processData function will pop the ackData out and process it as if it is a message received from our peer.
2013-02-05 22:53:56 +01:00
#Display timing data
2013-02-06 22:17:49 +01:00
timeRequiredToAttemptToDecryptMessage = time . time ( ) - self . messageProcessingStartTime
2013-02-05 22:53:56 +01:00
successfullyDecryptMessageTimings . append ( timeRequiredToAttemptToDecryptMessage )
sum = 0
for item in successfullyDecryptMessageTimings :
sum + = item
printLock . acquire ( )
print ' Time to decrypt this message successfully: ' , timeRequiredToAttemptToDecryptMessage
print ' Average time for all message decryption successes since startup: ' , sum / len ( successfullyDecryptMessageTimings )
printLock . release ( )
2013-02-26 00:09:32 +01:00
def addMailingListNameToSubject ( self , subject , mailingListName ) :
subject = subject . strip ( )
if subject [ : 3 ] == ' Re: ' or subject [ : 3 ] == ' RE: ' :
subject = subject [ 3 : ] . strip ( )
if ' [ ' + mailingListName + ' ] ' in subject :
return subject
else :
return ' [ ' + mailingListName + ' ] ' + subject
2012-11-19 20:45:05 +01:00
#We have received a pubkey
def recpubkey ( self ) :
2013-02-11 22:28:38 +01:00
self . pubkeyProcessingStartTime = time . time ( )
2013-02-12 21:00:04 +01:00
if self . payloadLength < 146 : #sanity check
2013-01-18 23:38:09 +01:00
return
2012-12-18 22:36:37 +01:00
#We must check to make sure the proof of work is sufficient.
if not self . isProofOfWorkSufficient ( ) :
print ' Proof of work in pubkey message insufficient. '
return
2012-11-19 20:45:05 +01:00
readPosition = 24 #for the message header
readPosition + = 8 #for the nonce
2013-02-11 22:28:38 +01:00
embeddedTime , = unpack ( ' >I ' , self . data [ readPosition : readPosition + 4 ] )
if embeddedTime < int ( time . time ( ) ) - lengthOfTimeToHoldOnToAllPubkeys - 86400 : #If the pubkey is more than a month old then reject it. (the 86400 is included to give an extra day of wiggle-room. If the wiggle-room is actually of any use, everyone on the network will delete this pubkey from their database the next time the cleanerThread cleans anyway- except for the node that actually wants the pubkey.)
printLock . acquire ( )
2013-02-26 00:09:32 +01:00
print ' The embedded time in this pubkey message is too old. Ignoring. Embedded time is: ' , embeddedTime
2013-02-11 22:28:38 +01:00
printLock . release ( )
return
if embeddedTime > int ( time . time ( ) ) + 10800 :
printLock . acquire ( )
print ' The embedded time in this pubkey message more than several hours in the future. This is irrational. Ignoring message. '
printLock . release ( )
return
2013-01-18 23:38:09 +01:00
readPosition + = 4 #for the time
2012-11-19 20:45:05 +01:00
addressVersion , varintLength = decodeVarint ( self . data [ readPosition : readPosition + 10 ] )
readPosition + = varintLength
streamNumber , varintLength = decodeVarint ( self . data [ readPosition : readPosition + 10 ] )
readPosition + = varintLength
2013-01-18 23:38:09 +01:00
if self . streamNumber != streamNumber :
print ' stream number embedded in this pubkey doesn \' t match our stream number. Ignoring. '
return
2012-11-19 20:45:05 +01:00
2013-02-04 22:49:02 +01:00
inventoryHash = calculateInventoryHash ( self . data [ 24 : self . payloadLength + 24 ] )
inventoryLock . acquire ( )
if inventoryHash in inventory :
print ' We have already received this pubkey. Ignoring it. '
inventoryLock . release ( )
return
elif isInSqlInventory ( inventoryHash ) :
print ' We have already received this pubkey (it is stored on disk in the SQL inventory). Ignoring it. '
inventoryLock . release ( )
return
2013-01-18 23:38:09 +01:00
objectType = ' pubkey '
inventory [ inventoryHash ] = ( objectType , self . streamNumber , self . data [ 24 : self . payloadLength + 24 ] , int ( time . time ( ) ) )
inventoryLock . release ( )
self . broadcastinv ( inventoryHash )
self . emit ( SIGNAL ( " incrementNumberOfPubkeysProcessed() " ) )
2012-11-19 20:45:05 +01:00
2013-02-11 22:28:38 +01:00
self . processpubkey ( )
lengthOfTimeWeShouldUseToProcessThisMessage = .2
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - ( time . time ( ) - self . pubkeyProcessingStartTime )
if sleepTime > 0 :
#printLock.acquire()
#print 'Timing attack mitigation: Sleeping for', sleepTime ,'seconds.'
#printLock.release()
time . sleep ( sleepTime )
#printLock.acquire()
#print 'Total pubkey processing time:', time.time()- self.pubkeyProcessingStartTime, 'seconds.'
#printLock.release()
def processpubkey ( self ) :
readPosition = 24 #for the message header
readPosition + = 8 #for the nonce
2013-02-18 21:22:48 +01:00
embeddedTime , = unpack ( ' >I ' , self . data [ readPosition : readPosition + 4 ] )
2013-02-11 22:28:38 +01:00
readPosition + = 4 #for the time
addressVersion , varintLength = decodeVarint ( self . data [ readPosition : readPosition + 10 ] )
readPosition + = varintLength
streamNumber , varintLength = decodeVarint ( self . data [ readPosition : readPosition + 10 ] )
readPosition + = varintLength
2013-01-18 23:38:09 +01:00
if addressVersion == 0 :
2013-02-11 22:28:38 +01:00
print ' (Within processpubkey) addressVersion of 0 doesn \' t make sense. '
2013-01-18 23:38:09 +01:00
return
2013-03-01 23:44:52 +01:00
if addressVersion > = 3 or addressVersion == 1 :
2013-01-18 23:38:09 +01:00
printLock . acquire ( )
print ' This version of Bitmessage cannot handle version ' , addressVersion , ' addresses. '
printLock . release ( )
return
if addressVersion == 2 :
if self . payloadLength < 146 : #sanity check. This is the minimum possible length.
print ' payloadLength less than 146. Sanity check failed. '
return
bitfieldBehaviors = self . data [ readPosition : readPosition + 4 ]
readPosition + = 4
publicSigningKey = self . data [ readPosition : readPosition + 64 ]
#Is it possible for a public key to be invalid such that trying to encrypt or sign with it will cause an error? If it is, we should probably test these keys here.
readPosition + = 64
publicEncryptionKey = self . data [ readPosition : readPosition + 64 ]
if len ( publicEncryptionKey ) < 64 :
print ' publicEncryptionKey length less than 64. Sanity check failed. '
return
sha = hashlib . new ( ' sha512 ' )
sha . update ( ' \x04 ' + publicSigningKey + ' \x04 ' + publicEncryptionKey )
ripeHasher = hashlib . new ( ' ripemd160 ' )
ripeHasher . update ( sha . digest ( ) )
ripe = ripeHasher . digest ( )
2012-11-19 20:45:05 +01:00
2013-01-18 23:38:09 +01:00
printLock . acquire ( )
2013-02-03 06:16:50 +01:00
print ' within recpubkey, addressVersion: ' , addressVersion , ' , streamNumber: ' , streamNumber
2013-01-18 23:38:09 +01:00
print ' ripe ' , ripe . encode ( ' hex ' )
print ' publicSigningKey in hex: ' , publicSigningKey . encode ( ' hex ' )
print ' publicEncryptionKey in hex: ' , publicEncryptionKey . encode ( ' hex ' )
printLock . release ( )
2013-02-11 22:28:38 +01:00
t = ( ripe , )
2013-01-18 23:38:09 +01:00
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
sqlSubmitQueue . put ( ''' SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally= ' yes ' ''' )
2013-01-18 23:38:09 +01:00
sqlSubmitQueue . put ( t )
2013-02-11 22:28:38 +01:00
queryreturn = sqlReturnQueue . get ( )
2013-01-18 23:38:09 +01:00
sqlLock . release ( )
2013-02-11 22:28:38 +01:00
if queryreturn != [ ] : #if this pubkey is already in our database and if we have used it personally:
print ' We HAVE used this pubkey personally. Updating time. '
t = ( ripe , True , self . data [ 24 : 24 + self . payloadLength ] , embeddedTime , ' yes ' )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
printLock . acquire ( )
printLock . release ( )
workerQueue . put ( ( ' newpubkey ' , ( addressVersion , streamNumber , ripe ) ) )
else :
print ' We have NOT used this pubkey personally. Inserting in database. '
2013-03-05 18:21:55 +01:00
t = ( ripe , True , self . data [ 24 : 24 + self . payloadLength ] , embeddedTime , ' no ' ) #This will also update the embeddedTime.
2013-02-11 22:28:38 +01:00
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
printLock . acquire ( )
printLock . release ( )
workerQueue . put ( ( ' newpubkey ' , ( addressVersion , streamNumber , ripe ) ) )
2013-01-18 23:38:09 +01:00
2013-02-04 22:49:02 +01:00
#This code which deals with old RSA addresses will soon be removed.
2013-03-01 23:44:52 +01:00
""" elif addressVersion == 1:
2013-01-18 23:38:09 +01:00
nLength , varintLength = decodeVarint ( self . data [ readPosition : readPosition + 10 ] )
readPosition + = varintLength
nString = self . data [ readPosition : readPosition + nLength ]
readPosition + = nLength
eLength , varintLength = decodeVarint ( self . data [ readPosition : readPosition + 10 ] )
readPosition + = varintLength
eString = self . data [ readPosition : readPosition + eLength ]
readPosition + = eLength
sha = hashlib . new ( ' sha512 ' )
sha . update ( nString + eString )
ripeHasher = hashlib . new ( ' ripemd160 ' )
ripeHasher . update ( sha . digest ( ) )
ripe = ripeHasher . digest ( )
print ' within recpubkey, addressVersion ' , addressVersion
print ' streamNumber ' , streamNumber
print ' ripe ' , repr ( ripe )
print ' n= ' , convertStringToInt ( nString )
print ' e= ' , convertStringToInt ( eString )
2013-02-11 22:28:38 +01:00
t = ( ripe , )
2013-01-18 23:38:09 +01:00
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
sqlSubmitQueue . put ( ''' SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally= ' yes ' ''' )
2013-01-18 23:38:09 +01:00
sqlSubmitQueue . put ( t )
2013-02-11 22:28:38 +01:00
queryreturn = sqlReturnQueue . get ( )
2013-01-18 23:38:09 +01:00
sqlLock . release ( )
2013-02-11 22:28:38 +01:00
if queryreturn != [ ] : #if this pubkey is already in our database and if we have used it personally:
print ' We HAVE used this pubkey personally. Updating time. '
t = ( ripe , True , self . data [ 24 : 24 + self . payloadLength ] , int ( time . time ( ) ) , ' yes ' )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
printLock . acquire ( )
print ' added foreign pubkey into our database '
printLock . release ( )
workerQueue . put ( ( ' newpubkey ' , ( addressVersion , streamNumber , ripe ) ) )
else :
print ' We have NOT used this pubkey personally. Inserting in database. '
t = ( ripe , True , self . data [ 24 : 24 + self . payloadLength ] , int ( time . time ( ) ) , ' no ' )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
printLock . acquire ( )
print ' added foreign pubkey into our database '
printLock . release ( )
2013-03-01 23:44:52 +01:00
workerQueue . put ( ( ' newpubkey ' , ( addressVersion , streamNumber , ripe ) ) ) """
2012-11-19 20:45:05 +01:00
#We have received a getpubkey message
def recgetpubkey ( self ) :
2012-12-18 22:36:37 +01:00
if not self . isProofOfWorkSufficient ( ) :
print ' Proof of work in getpubkey message insufficient. '
return
2013-01-04 23:21:33 +01:00
embeddedTime , = unpack ( ' >I ' , self . data [ 32 : 36 ] )
if embeddedTime > int ( time . time ( ) ) + 10800 :
print ' The time in this getpubkey message is too new. Ignoring it. Time: ' , embeddedTime
return
if embeddedTime < int ( time . time ( ) ) - maximumAgeOfAnObjectThatIAmWillingToAccept :
print ' The time in this getpubkey message is too old. Ignoring it. Time: ' , embeddedTime
return
2013-02-07 22:31:15 +01:00
addressVersionNumber , addressVersionLength = decodeVarint ( self . data [ 36 : 42 ] )
streamNumber , streamNumberLength = decodeVarint ( self . data [ 36 + addressVersionLength : 42 + addressVersionLength ] )
if streamNumber < > self . streamNumber :
print ' The streamNumber ' , streamNumber , ' doesn \' t match our stream number: ' , self . streamNumber
return
2012-11-19 20:45:05 +01:00
inventoryHash = calculateInventoryHash ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-07 22:31:15 +01:00
inventoryLock . acquire ( )
2012-11-19 20:45:05 +01:00
if inventoryHash in inventory :
print ' We have already received this getpubkey request. Ignoring it. '
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
return
2012-11-19 20:45:05 +01:00
elif isInSqlInventory ( inventoryHash ) :
print ' We have already received this getpubkey request (it is stored on disk in the SQL inventory). Ignoring it. '
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
return
2013-02-03 06:16:50 +01:00
self . objectsOfWhichThisRemoteNodeIsAlreadyAware [ inventoryHash ] = 0
objectType = ' getpubkey '
2013-01-04 23:21:33 +01:00
inventory [ inventoryHash ] = ( objectType , self . streamNumber , self . data [ 24 : self . payloadLength + 24 ] , embeddedTime )
2012-12-18 22:36:37 +01:00
inventoryLock . release ( )
2013-01-04 23:21:33 +01:00
#This getpubkey request is valid so far. Forward to peers.
2013-02-03 06:16:50 +01:00
self . broadcastinv ( inventoryHash )
2013-02-07 22:31:15 +01:00
2013-01-18 23:38:09 +01:00
if addressVersionNumber == 0 :
print ' The addressVersionNumber of the pubkey request is zero. That doesn \' t make any sense. Ignoring it. '
return
2013-03-01 23:44:52 +01:00
elif addressVersionNumber == 1 :
print ' The addressVersionNumber of the pubkey request is 1 which isn \' t supported anymore. Ignoring it. '
return
2013-01-18 23:38:09 +01:00
elif addressVersionNumber > 2 :
print ' The addressVersionNumber of the pubkey request is too high. Can \' t understand. Ignoring it. '
2013-01-04 23:21:33 +01:00
return
2013-01-18 23:38:09 +01:00
print ' the hash requested in this getpubkey request is: ' , self . data [ 36 + addressVersionLength + streamNumberLength : 56 + addressVersionLength + streamNumberLength ] . encode ( ' hex ' )
2013-01-22 20:29:49 +01:00
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
t = ( self . data [ 36 + addressVersionLength + streamNumberLength : 56 + addressVersionLength + streamNumberLength ] , int ( time . time ( ) ) - lengthOfTimeToHoldOnToAllPubkeys ) #this prevents SQL injection
sqlSubmitQueue . put ( ''' SELECT hash, transmitdata, time FROM pubkeys WHERE hash=? AND havecorrectnonce=1 AND time>? ''' )
2013-01-22 20:29:49 +01:00
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn != [ ] :
2013-01-18 23:38:09 +01:00
for row in queryreturn :
2013-02-11 22:28:38 +01:00
hash , payload , timeEncodedInPubkey = row
2013-02-04 22:49:02 +01:00
printLock . acquire ( )
print ' We have the requested pubkey stored in our database of pubkeys. Sending it. '
printLock . release ( )
2013-01-18 23:38:09 +01:00
inventoryHash = calculateInventoryHash ( payload )
objectType = ' pubkey '
2013-02-11 22:38:05 +01:00
inventory [ inventoryHash ] = ( objectType , self . streamNumber , payload , timeEncodedInPubkey ) #If the time embedded in this pubkey is more than 3 days old then this object isn't going to last very long in the inventory- the cleanerThread is going to come along and move it from the inventory in memory to the SQL inventory and then delete it from the SQL inventory. It should still find its way back to the original requestor if he is online however.
2013-02-18 21:22:48 +01:00
self . broadcastinv ( inventoryHash )
2013-02-11 22:28:38 +01:00
else : #the pubkey is not in our database of pubkeys. Let's check if the requested key is ours (which would mean we should do the POW, put it in the pubkey table, and broadcast out the pubkey.)
2013-01-22 20:29:49 +01:00
if self . data [ 36 + addressVersionLength + streamNumberLength : 56 + addressVersionLength + streamNumberLength ] in myECAddressHashes : #if this address hash is one of mine
2013-02-11 22:28:38 +01:00
printLock . acquire ( )
2013-01-22 20:29:49 +01:00
print ' Found getpubkey-requested-hash in my list of EC hashes. Telling Worker thread to do the POW for a pubkey message and send it out. '
2013-02-11 22:28:38 +01:00
printLock . release ( )
2013-01-22 20:29:49 +01:00
myAddress = encodeAddress ( addressVersionNumber , streamNumber , self . data [ 36 + addressVersionLength + streamNumberLength : 56 + addressVersionLength + streamNumberLength ] )
workerQueue . put ( ( ' doPOWForMyV2Pubkey ' , myAddress ) )
2012-12-18 22:36:37 +01:00
2013-02-04 22:49:02 +01:00
#This code which deals with old RSA addresses will soon be removed.
2013-02-11 22:28:38 +01:00
""" elif self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength] in myRSAAddressHashes:
2013-01-22 20:29:49 +01:00
print ' Found getpubkey requested hash in my list of RSA hashes. '
2012-12-18 22:36:37 +01:00
payload = ' \x00 \x00 \x00 \x01 ' #bitfield of features supported by me (see the wiki).
payload + = self . data [ 36 : 36 + addressVersionLength + streamNumberLength ]
#print int(config.get(encodeAddress(addressVersionNumber,streamNumber,self.data[36+addressVersionLength+streamNumberLength:56+addressVersionLength+streamNumberLength]), 'n'))
nString = convertIntToString ( int ( config . get ( encodeAddress ( addressVersionNumber , streamNumber , self . data [ 36 + addressVersionLength + streamNumberLength : 56 + addressVersionLength + streamNumberLength ] ) , ' n ' ) ) )
eString = convertIntToString ( config . getint ( encodeAddress ( addressVersionNumber , streamNumber , self . data [ 36 + addressVersionLength + streamNumberLength : 56 + addressVersionLength + streamNumberLength ] ) , ' e ' ) )
payload + = encodeVarint ( len ( nString ) )
payload + = nString
payload + = encodeVarint ( len ( eString ) )
payload + = eString
nonce = 0
trialValue = 99999999999999999999
target = 2 * * 64 / ( ( len ( payload ) + payloadLengthExtraBytes + 8 ) * averageProofOfWorkNonceTrialsPerByte )
print ' (For pubkey message) Doing proof of work... '
initialHash = hashlib . sha512 ( payload ) . digest ( )
while trialValue > target :
nonce + = 1
trialValue , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( pack ( ' >Q ' , nonce ) + initialHash ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
print ' (For pubkey message) Found proof of work ' , trialValue , ' Nonce: ' , nonce
payload = pack ( ' >Q ' , nonce ) + payload
t = ( self . data [ 36 + addressVersionLength + streamNumberLength : 56 + addressVersionLength + streamNumberLength ] , True , payload , int ( time . time ( ) ) + 1209600 ) #after two weeks (1,209,600 seconds), we may remove our own pub key from our database. It will be regenerated and put back in the database if it is requested.
2013-01-22 20:29:49 +01:00
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
#** pubkeys insert query not yet fixed! **
2012-12-18 22:36:37 +01:00
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?) ''' )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
2013-01-04 23:21:33 +01:00
sqlLock . release ( )
2013-01-22 20:29:49 +01:00
inventoryHash = calculateInventoryHash ( payload )
2012-11-19 20:45:05 +01:00
objectType = ' pubkey '
2013-01-22 20:29:49 +01:00
inventory [ inventoryHash ] = ( objectType , self . streamNumber , payload , int ( time . time ( ) ) )
2013-02-11 22:28:38 +01:00
self . broadcastinv ( inventoryHash ) """
2013-01-22 20:29:49 +01:00
else :
printLock . acquire ( )
print ' This getpubkey request is not for any of my keys. '
printLock . release ( )
2013-01-04 23:21:33 +01:00
2012-11-19 20:45:05 +01:00
#We have received an inv message
def recinv ( self ) :
numberOfItemsInInv , lengthOfVarint = decodeVarint ( self . data [ 24 : 34 ] )
if numberOfItemsInInv == 1 : #we'll just request this data from the person who advertised the object.
for i in range ( numberOfItemsInInv ) :
2013-02-12 21:00:04 +01:00
if len ( self . data [ 24 + lengthOfVarint + ( 32 * i ) : 56 + lengthOfVarint + ( 32 * i ) ] ) == 32 : #The length of an inventory hash should be 32. If it isn't 32 then the remote node is either badly programmed or behaving nefariously.
self . objectsOfWhichThisRemoteNodeIsAlreadyAware [ self . data [ 24 + lengthOfVarint + ( 32 * i ) : 56 + lengthOfVarint + ( 32 * i ) ] ] = 0
if self . data [ 24 + lengthOfVarint + ( 32 * i ) : 56 + lengthOfVarint + ( 32 * i ) ] in inventory :
printLock . acquire ( )
print ' Inventory (in memory) has inventory item already. '
printLock . release ( )
elif isInSqlInventory ( self . data [ 24 + lengthOfVarint + ( 32 * i ) : 56 + lengthOfVarint + ( 32 * i ) ] ) :
print ' Inventory (SQL on disk) has inventory item already. '
else :
self . sendgetdata ( self . data [ 24 + lengthOfVarint + ( 32 * i ) : 56 + lengthOfVarint + ( 32 * i ) ] )
2012-11-19 20:45:05 +01:00
else :
print ' inv message lists ' , numberOfItemsInInv , ' objects. '
for i in range ( numberOfItemsInInv ) : #upon finishing dealing with an incoming message, the receiveDataThread will request a random object from the peer. This way if we get multiple inv messages from multiple peers which list mostly the same objects, we will make getdata requests for different random objects from the various peers.
2013-02-12 21:00:04 +01:00
if len ( self . data [ 24 + lengthOfVarint + ( 32 * i ) : 56 + lengthOfVarint + ( 32 * i ) ] ) == 32 : #The length of an inventory hash should be 32. If it isn't 32 then the remote node is either badly programmed or behaving nefariously.
self . objectsOfWhichThisRemoteNodeIsAlreadyAware [ self . data [ 24 + lengthOfVarint + ( 32 * i ) : 56 + lengthOfVarint + ( 32 * i ) ] ] = 0
self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave [ self . data [ 24 + lengthOfVarint + ( 32 * i ) : 56 + lengthOfVarint + ( 32 * i ) ] ] = 0
2013-02-04 22:49:02 +01:00
2012-11-19 20:45:05 +01:00
#Send a getdata message to our peer to request the object with the given hash
def sendgetdata ( self , hash ) :
2013-01-22 06:21:32 +01:00
print ' sending getdata to retrieve object with hash: ' , hash . encode ( ' hex ' )
2012-11-19 20:45:05 +01:00
payload = ' \x01 ' + hash
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' getdata \x00 \x00 \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) ) #payload length. Note that we add an extra 8 for the nonce.
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2013-02-26 23:20:43 +01:00
try :
self . sock . send ( headerData + payload )
except Exception , err :
if not ' Bad file descriptor ' in err :
printLock . acquire ( )
sys . stderr . write ( ' sock.send error: %s \n ' % err )
printLock . release ( )
2012-11-19 20:45:05 +01:00
#We have received a getdata request from our peer
def recgetdata ( self ) :
value , lengthOfVarint = decodeVarint ( self . data [ 24 : 34 ] )
#print 'Number of items in getdata request:', value
try :
2013-02-04 22:49:02 +01:00
for i in xrange ( value ) :
2012-11-19 20:45:05 +01:00
hash = self . data [ 24 + lengthOfVarint + ( i * 32 ) : 56 + lengthOfVarint + ( i * 32 ) ]
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
print ' received getdata request for item: ' , hash . encode ( ' hex ' )
printLock . release ( )
2012-11-19 20:45:05 +01:00
#print 'inventory is', inventory
if hash in inventory :
objectType , streamNumber , payload , receivedTime = inventory [ hash ]
self . sendData ( objectType , payload )
else :
t = ( hash , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' select objecttype, payload from inventory where hash=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn < > [ ] :
for row in queryreturn :
objectType , payload = row
self . sendData ( objectType , payload )
else :
print ' Someone asked for an object with a getdata which is not in either our memory inventory or our SQL inventory. That shouldn \' t have happened. '
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
except :
pass #someone is probably trying to cause a program error by, for example, making a request for 10 items but only including the hashes for 5.
#Our peer has requested (in a getdata message) that we send an object.
def sendData ( self , objectType , payload ) :
if objectType == ' pubkey ' :
print ' sending pubkey '
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' pubkey \x00 \x00 \x00 \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) ) #payload length. Note that we add an extra 8 for the nonce.
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2012-11-19 20:45:05 +01:00
self . sock . send ( headerData + payload )
2013-02-03 06:16:50 +01:00
elif objectType == ' getpubkey ' :
print ' sending getpubkey '
2012-11-19 20:45:05 +01:00
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' getpubkey \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) ) #payload length. Note that we add an extra 8 for the nonce.
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2012-11-19 20:45:05 +01:00
self . sock . send ( headerData + payload )
elif objectType == ' msg ' :
print ' sending msg '
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' msg \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) ) #payload length. Note that we add an extra 8 for the nonce.
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2012-11-19 20:45:05 +01:00
self . sock . send ( headerData + payload )
elif objectType == ' broadcast ' :
print ' sending broadcast '
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' broadcast \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) ) #payload length. Note that we add an extra 8 for the nonce.
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2012-11-19 20:45:05 +01:00
self . sock . send ( headerData + payload )
2013-02-03 21:50:19 +01:00
elif objectType == ' getpubkey ' or objectType == ' pubkeyrequest ' :
2012-11-19 20:45:05 +01:00
print ' sending getpubkey '
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' getpubkey \x00 \x00 \x00 ' #version command
headerData + = pack ( ' >L ' , len ( payload ) ) #payload length
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ 0 : 4 ]
2012-11-19 20:45:05 +01:00
self . sock . send ( headerData + payload )
else :
sys . stderr . write ( ' Error: sendData has been asked to send a strange objectType: %s \n ' % str ( objectType ) )
#Send an inv message with just one hash to all of our peers
def broadcastinv ( self , hash ) :
2012-12-05 18:47:30 +01:00
printLock . acquire ( )
2013-01-18 23:38:09 +01:00
print ' broadcasting inv with hash: ' , hash . encode ( ' hex ' )
2012-12-05 18:47:30 +01:00
printLock . release ( )
2013-02-03 06:16:50 +01:00
broadcastToSendDataQueues ( ( self . streamNumber , ' sendinv ' , hash ) )
2012-12-05 18:47:30 +01:00
2012-11-19 20:45:05 +01:00
#We have received an addr message.
def recaddr ( self ) :
listOfAddressDetailsToBroadcastToPeers = [ ]
numberOfAddressesIncluded = 0
numberOfAddressesIncluded , lengthOfNumberOfAddresses = decodeVarint ( self . data [ 24 : 29 ] )
if verbose > = 1 :
2013-02-26 23:20:43 +01:00
printLock . acquire ( )
2012-12-10 20:48:00 +01:00
print ' addr message contains ' , numberOfAddressesIncluded , ' IP addresses. '
2013-02-26 23:20:43 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
#print 'lengthOfNumberOfAddresses', lengthOfNumberOfAddresses
2013-03-05 17:05:05 +01:00
if numberOfAddressesIncluded > 1000 or numberOfAddressesIncluded == 0 :
2012-11-19 20:45:05 +01:00
return
2013-03-12 21:03:16 +01:00
if self . payloadLength < lengthOfNumberOfAddresses + ( 34 * numberOfAddressesIncluded ) :
print ' addr message does not contain enough data. Ignoring. '
return
2012-11-19 20:45:05 +01:00
needToWriteKnownNodesToDisk = False
for i in range ( 0 , numberOfAddressesIncluded ) :
try :
if self . data [ 40 + lengthOfNumberOfAddresses + ( 34 * i ) : 52 + lengthOfNumberOfAddresses + ( 34 * i ) ] != ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' :
printLock . acquire ( )
print ' Skipping IPv6 address. ' , repr ( self . data [ 40 + lengthOfNumberOfAddresses + ( 34 * i ) : 56 + lengthOfNumberOfAddresses + ( 34 * i ) ] )
printLock . release ( )
continue
#print repr(self.data[6+lengthOfNumberOfAddresses+(34*i):18+lengthOfNumberOfAddresses+(34*i)])
except Exception , err :
if verbose > = 2 :
printLock . acquire ( )
sys . stderr . write ( ' ERROR TRYING TO UNPACK recaddr (to test for an IPv6 address). Message: %s \n ' % str ( err ) )
printLock . release ( )
break #giving up on unpacking any more. We should still be connected however.
try :
recaddrStream , = unpack ( ' >I ' , self . data [ 28 + lengthOfNumberOfAddresses + ( 34 * i ) : 32 + lengthOfNumberOfAddresses + ( 34 * i ) ] )
except Exception , err :
if verbose > = 2 :
printLock . acquire ( )
sys . stderr . write ( ' ERROR TRYING TO UNPACK recaddr (recaddrStream). Message: %s \n ' % str ( err ) )
printLock . release ( )
break #giving up on unpacking any more. We should still be connected however.
2013-03-12 21:03:16 +01:00
if recaddrStream == 0 :
continue
2013-03-14 18:47:35 +01:00
if recaddrStream != self . streamNumber and recaddrStream != ( self . streamNumber * 2 ) and recaddrStream != ( ( self . streamNumber * 2 ) + 1 ) : #if the embedded stream number is not in my stream or either of my child streams then ignore it. Someone might be trying funny business.
continue
2012-11-19 20:45:05 +01:00
try :
recaddrServices , = unpack ( ' >Q ' , self . data [ 32 + lengthOfNumberOfAddresses + ( 34 * i ) : 40 + lengthOfNumberOfAddresses + ( 34 * i ) ] )
except Exception , err :
if verbose > = 2 :
printLock . acquire ( )
sys . stderr . write ( ' ERROR TRYING TO UNPACK recaddr (recaddrServices). Message: %s \n ' % str ( err ) )
printLock . release ( )
break #giving up on unpacking any more. We should still be connected however.
try :
recaddrPort , = unpack ( ' >H ' , self . data [ 56 + lengthOfNumberOfAddresses + ( 34 * i ) : 58 + lengthOfNumberOfAddresses + ( 34 * i ) ] )
except Exception , err :
if verbose > = 2 :
printLock . acquire ( )
sys . stderr . write ( ' ERROR TRYING TO UNPACK recaddr (recaddrPort). Message: %s \n ' % str ( err ) )
printLock . release ( )
break #giving up on unpacking any more. We should still be connected however.
#print 'Within recaddr(): IP', recaddrIP, ', Port', recaddrPort, ', i', i
hostFromAddrMessage = socket . inet_ntoa ( self . data [ 52 + lengthOfNumberOfAddresses + ( 34 * i ) : 56 + lengthOfNumberOfAddresses + ( 34 * i ) ] )
2012-12-05 18:47:30 +01:00
#print 'hostFromAddrMessage', hostFromAddrMessage
2013-03-19 18:32:37 +01:00
if self . data [ 52 + lengthOfNumberOfAddresses + ( 34 * i ) ] == ' \x7F ' :
print ' Ignoring IP address in loopback range: ' , hostFromAddrMessage
2012-12-18 19:09:10 +01:00
continue
2013-03-25 19:13:56 +01:00
if self . data [ 52 + lengthOfNumberOfAddresses + ( 34 * i ) ] == ' \x0A ' :
print ' Ignoring IP address in private range: ' , hostFromAddrMessage
continue
if self . data [ 52 + lengthOfNumberOfAddresses + ( 34 * i ) : 52 + lengthOfNumberOfAddresses + ( 34 * i ) + 2 ] == ' \xC0 A8 ' :
print ' Ignoring IP address in private range: ' , hostFromAddrMessage
continue
2012-11-19 20:45:05 +01:00
timeSomeoneElseReceivedMessageFromThisNode , = unpack ( ' >I ' , self . data [ 24 + lengthOfNumberOfAddresses + ( 34 * i ) : 28 + lengthOfNumberOfAddresses + ( 34 * i ) ] ) #This is the 'time' value in the received addr message.
2013-03-14 18:47:35 +01:00
if recaddrStream not in knownNodes : #knownNodes is a dictionary of dictionaries with one outer dictionary for each stream. If the outer stream dictionary doesn't exist yet then we must make it.
2013-03-12 21:03:16 +01:00
knownNodes [ recaddrStream ] = { }
2012-11-19 20:45:05 +01:00
if hostFromAddrMessage not in knownNodes [ recaddrStream ] :
2012-12-05 18:47:30 +01:00
if len ( knownNodes [ recaddrStream ] ) < 20000 and timeSomeoneElseReceivedMessageFromThisNode > ( int ( time . time ( ) ) - 10800 ) and timeSomeoneElseReceivedMessageFromThisNode < ( int ( time . time ( ) ) + 10800 ) : #If we have more than 20000 nodes in our list already then just forget about adding more. Also, make sure that the time that someone else received a message from this node is within three hours from now.
2012-11-19 20:45:05 +01:00
knownNodes [ recaddrStream ] [ hostFromAddrMessage ] = ( recaddrPort , timeSomeoneElseReceivedMessageFromThisNode )
2013-03-12 21:03:16 +01:00
print ' added new node ' , hostFromAddrMessage , ' to knownNodes in stream ' , recaddrStream
2012-11-19 20:45:05 +01:00
needToWriteKnownNodesToDisk = True
hostDetails = ( timeSomeoneElseReceivedMessageFromThisNode , recaddrStream , recaddrServices , hostFromAddrMessage , recaddrPort )
listOfAddressDetailsToBroadcastToPeers . append ( hostDetails )
else :
PORT , timeLastReceivedMessageFromThisNode = knownNodes [ recaddrStream ] [ hostFromAddrMessage ] #PORT in this case is either the port we used to connect to the remote node, or the port that was specified by someone else in a past addr message.
if ( timeLastReceivedMessageFromThisNode < timeSomeoneElseReceivedMessageFromThisNode ) and ( timeSomeoneElseReceivedMessageFromThisNode < int ( time . time ( ) ) ) :
knownNodes [ recaddrStream ] [ hostFromAddrMessage ] = ( PORT , timeSomeoneElseReceivedMessageFromThisNode )
if PORT != recaddrPort :
2012-12-05 18:47:30 +01:00
print ' Strange occurance: The port specified in an addr message ' , str ( recaddrPort ) , ' does not match the port ' , str ( PORT ) , ' that this program (or some other peer) used to connect to it ' , str ( hostFromAddrMessage ) , ' . Perhaps they changed their port or are using a strange NAT configuration. '
2012-11-19 20:45:05 +01:00
if needToWriteKnownNodesToDisk : #Runs if any nodes were new to us. Also, share those nodes with our peers.
output = open ( appdata + ' knownnodes.dat ' , ' wb ' )
pickle . dump ( knownNodes , output )
output . close ( )
self . broadcastaddr ( listOfAddressDetailsToBroadcastToPeers )
2013-02-26 23:20:43 +01:00
printLock . acquire ( )
2013-03-12 21:03:16 +01:00
print ' knownNodes currently has ' , len ( knownNodes [ self . streamNumber ] ) , ' nodes for this stream. '
2013-02-26 23:20:43 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
#Function runs when we want to broadcast an addr message to all of our peers. Runs when we learn of nodes that we didn't previously know about and want to share them with our peers.
def broadcastaddr ( self , listOfAddressDetailsToBroadcastToPeers ) :
numberOfAddressesInAddrMessage = len ( listOfAddressDetailsToBroadcastToPeers )
payload = ' '
for hostDetails in listOfAddressDetailsToBroadcastToPeers :
timeLastReceivedMessageFromThisNode , streamNumber , services , host , port = hostDetails
payload + = pack ( ' >I ' , timeLastReceivedMessageFromThisNode )
payload + = pack ( ' >I ' , streamNumber )
payload + = pack ( ' >q ' , services ) #service bit flags offered by this node
payload + = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + socket . inet_aton ( host )
payload + = pack ( ' >H ' , port ) #remote port
payload = encodeVarint ( numberOfAddressesInAddrMessage ) + payload
datatosend = ' \xE9 \xBE \xB4 \xD9 addr \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
datatosend = datatosend + pack ( ' >L ' , len ( payload ) ) #payload length
2012-11-23 01:06:42 +01:00
datatosend = datatosend + hashlib . sha512 ( payload ) . digest ( ) [ 0 : 4 ]
2012-11-19 20:45:05 +01:00
datatosend = datatosend + payload
if verbose > = 2 :
printLock . acquire ( )
2013-01-22 06:21:32 +01:00
print ' Broadcasting addr with ' , numberOfAddressesInAddrMessage , ' entries. '
2012-11-19 20:45:05 +01:00
printLock . release ( )
2013-02-03 06:16:50 +01:00
broadcastToSendDataQueues ( ( self . streamNumber , ' sendaddr ' , datatosend ) )
2012-11-19 20:45:05 +01:00
#Send a big addr message to our peer
def sendaddr ( self ) :
addrsInMyStream = { }
addrsInChildStreamLeft = { }
addrsInChildStreamRight = { }
2012-12-18 22:36:37 +01:00
#print 'knownNodes', knownNodes
2012-11-19 20:45:05 +01:00
#We are going to share a maximum number of 1000 addrs with our peer. 500 from this stream, 250 from the left child stream, and 250 from the right child stream.
if len ( knownNodes [ self . streamNumber ] ) > 0 :
for i in range ( 500 ) :
random . seed ( )
HOST , = random . sample ( knownNodes [ self . streamNumber ] , 1 )
2013-03-25 19:13:56 +01:00
if self . isHostInPrivateIPRange ( HOST ) :
continue
2012-11-19 20:45:05 +01:00
addrsInMyStream [ HOST ] = knownNodes [ self . streamNumber ] [ HOST ]
if len ( knownNodes [ self . streamNumber * 2 ] ) > 0 :
for i in range ( 250 ) :
random . seed ( )
HOST , = random . sample ( knownNodes [ self . streamNumber * 2 ] , 1 )
2013-03-25 19:13:56 +01:00
if self . isHostInPrivateIPRange ( HOST ) :
continue
2012-11-19 20:45:05 +01:00
addrsInChildStreamLeft [ HOST ] = knownNodes [ self . streamNumber * 2 ] [ HOST ]
if len ( knownNodes [ ( self . streamNumber * 2 ) + 1 ] ) > 0 :
for i in range ( 250 ) :
random . seed ( )
HOST , = random . sample ( knownNodes [ ( self . streamNumber * 2 ) + 1 ] , 1 )
2013-03-25 19:13:56 +01:00
if self . isHostInPrivateIPRange ( HOST ) :
continue
2012-11-19 20:45:05 +01:00
addrsInChildStreamRight [ HOST ] = knownNodes [ ( self . streamNumber * 2 ) + 1 ] [ HOST ]
numberOfAddressesInAddrMessage = 0
payload = ' '
2013-03-05 20:50:31 +01:00
#print 'addrsInMyStream.items()', addrsInMyStream.items()
2012-11-19 20:45:05 +01:00
for HOST , value in addrsInMyStream . items ( ) :
PORT , timeLastReceivedMessageFromThisNode = value
if timeLastReceivedMessageFromThisNode > ( int ( time . time ( ) ) - maximumAgeOfNodesThatIAdvertiseToOthers ) : #If it is younger than 3 hours old..
numberOfAddressesInAddrMessage + = 1
payload + = pack ( ' >I ' , timeLastReceivedMessageFromThisNode )
payload + = pack ( ' >I ' , self . streamNumber )
payload + = pack ( ' >q ' , 1 ) #service bit flags offered by this node
payload + = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + socket . inet_aton ( HOST )
payload + = pack ( ' >H ' , PORT ) #remote port
for HOST , value in addrsInChildStreamLeft . items ( ) :
PORT , timeLastReceivedMessageFromThisNode = value
if timeLastReceivedMessageFromThisNode > ( int ( time . time ( ) ) - maximumAgeOfNodesThatIAdvertiseToOthers ) : #If it is younger than 3 hours old..
numberOfAddressesInAddrMessage + = 1
payload + = pack ( ' >I ' , timeLastReceivedMessageFromThisNode )
payload + = pack ( ' >I ' , self . streamNumber * 2 )
payload + = pack ( ' >q ' , 1 ) #service bit flags offered by this node
payload + = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + socket . inet_aton ( HOST )
payload + = pack ( ' >H ' , PORT ) #remote port
for HOST , value in addrsInChildStreamRight . items ( ) :
PORT , timeLastReceivedMessageFromThisNode = value
if timeLastReceivedMessageFromThisNode > ( int ( time . time ( ) ) - maximumAgeOfNodesThatIAdvertiseToOthers ) : #If it is younger than 3 hours old..
numberOfAddressesInAddrMessage + = 1
payload + = pack ( ' >I ' , timeLastReceivedMessageFromThisNode )
payload + = pack ( ' >I ' , ( self . streamNumber * 2 ) + 1 )
payload + = pack ( ' >q ' , 1 ) #service bit flags offered by this node
payload + = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + socket . inet_aton ( HOST )
payload + = pack ( ' >H ' , PORT ) #remote port
payload = encodeVarint ( numberOfAddressesInAddrMessage ) + payload
datatosend = ' \xE9 \xBE \xB4 \xD9 addr \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
datatosend = datatosend + pack ( ' >L ' , len ( payload ) ) #payload length
2012-11-23 01:06:42 +01:00
datatosend = datatosend + hashlib . sha512 ( payload ) . digest ( ) [ 0 : 4 ]
2012-11-19 20:45:05 +01:00
datatosend = datatosend + payload
if verbose > = 2 :
printLock . acquire ( )
2013-01-22 06:21:32 +01:00
print ' Sending addr with ' , numberOfAddressesInAddrMessage , ' entries. '
2012-11-19 20:45:05 +01:00
printLock . release ( )
self . sock . send ( datatosend )
#We have received a version message
def recversion ( self ) :
if self . payloadLength < 83 :
2012-11-26 20:18:52 +01:00
#This version message is unreasonably short. Forget it.
2012-11-19 20:45:05 +01:00
return
2012-11-26 20:18:52 +01:00
elif not self . verackSent : #There is a potential exploit if we don't check to make sure that we have not already received and accepted a version message: An attacker could connect directly to us, send a msg message with the ackdata set to an invalid version message which would cause us to close the connection to the attacker thus proving that we were able to decode the message. Checking the connectionIsOrWasFullyEstablished variable would also suffice.
2012-11-19 20:45:05 +01:00
self . remoteProtocolVersion , = unpack ( ' >L ' , self . data [ 24 : 28 ] )
#print 'remoteProtocolVersion', self.remoteProtocolVersion
self . myExternalIP = socket . inet_ntoa ( self . data [ 64 : 68 ] )
#print 'myExternalIP', self.myExternalIP
self . remoteNodeIncomingPort , = unpack ( ' >H ' , self . data [ 94 : 96 ] )
#print 'remoteNodeIncomingPort', self.remoteNodeIncomingPort
#print 'self.data[96:104]', repr(self.data[96:104])
#print 'eightBytesOfRandomDataUsedToDetectConnectionsToSelf', repr(eightBytesOfRandomDataUsedToDetectConnectionsToSelf)
useragentLength , lengthOfUseragentVarint = decodeVarint ( self . data [ 104 : 108 ] )
2013-01-24 17:38:12 +01:00
readPosition = 104 + lengthOfUseragentVarint
useragent = self . data [ readPosition : readPosition + useragentLength ]
readPosition + = useragentLength
2012-11-19 20:45:05 +01:00
numberOfStreamsInVersionMessage , lengthOfNumberOfStreamsInVersionMessage = decodeVarint ( self . data [ readPosition : ] )
readPosition + = lengthOfNumberOfStreamsInVersionMessage
self . streamNumber , lengthOfRemoteStreamNumber = decodeVarint ( self . data [ readPosition : ] )
2013-01-22 06:21:32 +01:00
printLock . acquire ( )
2013-01-24 17:38:12 +01:00
print ' Remote node useragent: ' , useragent , ' stream number: ' , self . streamNumber
2013-01-22 06:21:32 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
if self . streamNumber != 1 :
self . sock . close ( )
printLock . acquire ( )
2013-02-21 01:56:21 +01:00
print ' Closed connection to ' , self . HOST , ' because they are interested in stream ' , self . streamNumber , ' . '
2012-11-19 20:45:05 +01:00
printLock . release ( )
self . data = ' '
return
2013-02-21 01:56:21 +01:00
#If this was an incoming connection, then the sendData thread doesn't know the stream. We have to set it.
if not self . initiatedConnection :
broadcastToSendDataQueues ( ( 0 , ' setStreamNumber ' , ( self . HOST , self . streamNumber ) ) )
2012-11-19 20:45:05 +01:00
if self . data [ 96 : 104 ] == eightBytesOfRandomDataUsedToDetectConnectionsToSelf :
self . sock . close ( )
printLock . acquire ( )
print ' Closing connection to myself: ' , self . HOST
printLock . release ( )
self . data = ' '
return
knownNodes [ self . streamNumber ] [ self . HOST ] = ( self . remoteNodeIncomingPort , int ( time . time ( ) ) )
output = open ( appdata + ' knownnodes.dat ' , ' wb ' )
pickle . dump ( knownNodes , output )
output . close ( )
#I've commented out this code because it should be up to the newer node to decide whether their protocol version is incompatiable with the remote node's version.
''' if self.remoteProtocolVersion > 1:
print ' The remote node ' ' s protocol version is too new for this program to understand. Disconnecting. It is: ' , self . remoteProtocolVersion
self . sock . close ( )
self . selfInitiatedConnectionList . remove ( self )
else : '''
self . sendverack ( )
if self . initiatedConnection == False :
self . sendversion ( )
#Sends a version message
def sendversion ( self ) :
global softwareVersion
payload = ' '
payload + = pack ( ' >L ' , 1 ) #protocol version.
payload + = pack ( ' >q ' , 1 ) #bitflags of the services I offer.
payload + = pack ( ' >q ' , int ( time . time ( ) ) )
2013-02-12 21:00:04 +01:00
payload + = pack ( ' >q ' , 1 ) #boolservices offered by the remote node. This data is ignored by the remote host because how could We know what Their services are without them telling us?
2012-11-19 20:45:05 +01:00
payload + = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + socket . inet_aton ( self . HOST )
payload + = pack ( ' >H ' , self . PORT ) #remote IPv6 and port
payload + = pack ( ' >q ' , 1 ) #bitflags of the services I offer.
payload + = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + pack ( ' >L ' , 2130706433 ) # = 127.0.0.1. This will be ignored by the remote host. The actual remote connected IP will be used.
payload + = pack ( ' >H ' , config . getint ( ' bitmessagesettings ' , ' port ' ) ) #my external IPv6 and port
random . seed ( )
payload + = eightBytesOfRandomDataUsedToDetectConnectionsToSelf
userAgent = ' /PyBitmessage: ' + softwareVersion + ' / ' #Length of userAgent must be less than 253.
payload + = pack ( ' >B ' , len ( userAgent ) ) #user agent string length. If the user agent is more than 252 bytes long, this code isn't going to work.
payload + = userAgent
payload + = encodeVarint ( 1 ) #The number of streams about which I care. PyBitmessage currently only supports 1.
payload + = encodeVarint ( self . streamNumber )
datatosend = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
datatosend = datatosend + ' version \x00 \x00 \x00 \x00 \x00 ' #version command
datatosend = datatosend + pack ( ' >L ' , len ( payload ) ) #payload length
2012-11-23 01:06:42 +01:00
datatosend = datatosend + hashlib . sha512 ( payload ) . digest ( ) [ 0 : 4 ]
2012-11-19 20:45:05 +01:00
datatosend = datatosend + payload
printLock . acquire ( )
2013-01-24 17:38:12 +01:00
print ' Sending version message '
2012-11-19 20:45:05 +01:00
printLock . release ( )
self . sock . send ( datatosend )
#self.versionSent = 1
#Sends a verack message
def sendverack ( self ) :
2013-01-25 19:29:31 +01:00
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' Sending verack '
2013-01-25 19:29:31 +01:00
printLock . release ( )
2012-11-23 09:22:56 +01:00
self . sock . sendall ( ' \xE9 \xBE \xB4 \xD9 \x76 \x65 \x72 \x61 \x63 \x6B \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xcf \x83 \xe1 \x35 ' )
#cf 83 e1 35
2012-11-19 20:45:05 +01:00
self . verackSent = True
if self . verackReceived == True :
self . connectionFullyEstablished ( )
2013-03-25 19:13:56 +01:00
def isHostInPrivateIPRange ( self , host ) :
if host [ : 3 ] == ' 10. ' :
return True
if host [ : 4 ] == ' 172. ' :
if host [ 6 ] == ' . ' :
if int ( host [ 4 : 6 ] ) > = 16 and int ( host [ 4 : 6 ] ) < = 31 :
return True
if host [ : 8 ] == ' 192.168. ' :
return True
return False
2012-11-19 20:45:05 +01:00
#Every connection to a peer has a sendDataThread (and also a receiveDataThread).
class sendDataThread ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
self . mailbox = Queue . Queue ( )
sendDataQueues . append ( self . mailbox )
self . data = ' '
2013-02-03 06:16:50 +01:00
def setup ( self , sock , HOST , PORT , streamNumber , objectsOfWhichThisRemoteNodeIsAlreadyAware ) :
2012-11-19 20:45:05 +01:00
self . sock = sock
self . HOST = HOST
self . PORT = PORT
self . streamNumber = streamNumber
self . lastTimeISentData = int ( time . time ( ) ) #If this value increases beyond five minutes ago, we'll send a pong message to keep the connection alive.
2013-02-03 06:16:50 +01:00
self . objectsOfWhichThisRemoteNodeIsAlreadyAware = objectsOfWhichThisRemoteNodeIsAlreadyAware
2012-11-19 20:45:05 +01:00
printLock . acquire ( )
2013-01-22 06:21:32 +01:00
print ' The streamNumber of this sendDataThread (ID: ' , id ( self ) , ' ) at setup() is ' , self . streamNumber
2012-11-19 20:45:05 +01:00
printLock . release ( )
def sendVersionMessage ( self ) :
#Note that there is another copy of this version-sending code in the receiveData class which would need to be changed if you make changes here.
global softwareVersion
payload = ' '
payload + = pack ( ' >L ' , 1 ) #protocol version.
payload + = pack ( ' >q ' , 1 ) #bitflags of the services I offer.
payload + = pack ( ' >q ' , int ( time . time ( ) ) )
payload + = pack ( ' >q ' , 1 ) #boolservices of remote connection. How can I even know this for sure? This is probably ignored by the remote host.
payload + = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + socket . inet_aton ( self . HOST )
payload + = pack ( ' >H ' , self . PORT ) #remote IPv6 and port
payload + = pack ( ' >q ' , 1 ) #bitflags of the services I offer.
payload + = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xFF \xFF ' + pack ( ' >L ' , 2130706433 ) # = 127.0.0.1. This will be ignored by the remote host. The actual remote connected IP will be used.
payload + = pack ( ' >H ' , config . getint ( ' bitmessagesettings ' , ' port ' ) ) #my external IPv6 and port
random . seed ( )
payload + = eightBytesOfRandomDataUsedToDetectConnectionsToSelf
userAgent = ' /PyBitmessage: ' + softwareVersion + ' / ' #Length of userAgent must be less than 253.
payload + = pack ( ' >B ' , len ( userAgent ) ) #user agent string length. If the user agent is more than 252 bytes long, this code isn't going to work.
payload + = userAgent
payload + = encodeVarint ( 1 ) #The number of streams about which I care. PyBitmessage currently only supports 1 per connection.
payload + = encodeVarint ( self . streamNumber )
datatosend = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
datatosend = datatosend + ' version \x00 \x00 \x00 \x00 \x00 ' #version command
datatosend = datatosend + pack ( ' >L ' , len ( payload ) ) #payload length
2012-11-23 01:06:42 +01:00
datatosend = datatosend + hashlib . sha512 ( payload ) . digest ( ) [ 0 : 4 ]
2012-11-19 20:45:05 +01:00
datatosend = datatosend + payload
printLock . acquire ( )
print ' Sending version packet: ' , repr ( datatosend )
printLock . release ( )
self . sock . send ( datatosend )
self . versionSent = 1
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def run ( self ) :
while True :
deststream , command , data = self . mailbox . get ( )
2012-12-05 18:47:30 +01:00
#printLock.acquire()
#print 'sendDataThread, destream:', deststream, ', Command:', command, ', ID:',id(self), ', HOST:', self.HOST
#printLock.release()
2012-11-19 20:45:05 +01:00
if deststream == self . streamNumber or deststream == 0 :
if command == ' shutdown ' :
if data == self . HOST or data == ' all ' :
printLock . acquire ( )
2012-11-23 09:22:56 +01:00
print ' sendDataThread thread (associated with ' , self . HOST , ' ) ID: ' , id ( self ) , ' shutting down now. '
2012-11-19 20:45:05 +01:00
self . sock . close ( )
sendDataQueues . remove ( self . mailbox )
print ' len of sendDataQueues ' , len ( sendDataQueues )
printLock . release ( )
break
2012-12-05 18:47:30 +01:00
#When you receive an incoming connection, a sendDataThread is created even though you don't yet know what stream number the remote peer is interested in. They will tell you in a version message and if you too are interested in that stream then you will continue on with the connection and will set the streamNumber of this send data thread here:
2012-11-19 20:45:05 +01:00
elif command == ' setStreamNumber ' :
hostInMessage , specifiedStreamNumber = data
if hostInMessage == self . HOST :
printLock . acquire ( )
2012-11-23 09:22:56 +01:00
print ' setting the stream number in the sendData thread (ID: ' , id ( self ) , ' ) to ' , specifiedStreamNumber
2012-11-19 20:45:05 +01:00
printLock . release ( )
self . streamNumber = specifiedStreamNumber
2013-02-03 06:16:50 +01:00
elif command == ' sendaddr ' :
2012-11-19 20:45:05 +01:00
try :
2013-01-25 20:39:22 +01:00
#To prevent some network analysis, 'leak' the data out to our peer after waiting a random amount of time unless we have a long list of messages in our queue to send.
2013-02-03 06:16:50 +01:00
random . seed ( )
time . sleep ( random . randrange ( 0 , 10 ) )
2012-11-19 20:45:05 +01:00
self . sock . sendall ( data )
self . lastTimeISentData = int ( time . time ( ) )
except :
print ' self.sock.sendall failed '
self . sock . close ( )
sendDataQueues . remove ( self . mailbox )
print ' sendDataThread thread ' , self , ' ending now '
break
2013-02-03 06:16:50 +01:00
elif command == ' sendinv ' :
if data not in self . objectsOfWhichThisRemoteNodeIsAlreadyAware :
payload = ' \x01 ' + data
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' inv \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) )
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2013-02-03 06:16:50 +01:00
#To prevent some network analysis, 'leak' the data out to our peer after waiting a random amount of time
random . seed ( )
time . sleep ( random . randrange ( 0 , 10 ) )
try :
self . sock . sendall ( headerData + payload )
self . lastTimeISentData = int ( time . time ( ) )
except :
print ' self.sock.sendall failed '
self . sock . close ( )
sendDataQueues . remove ( self . mailbox )
print ' sendDataThread thread ' , self , ' ending now '
break
2012-11-19 20:45:05 +01:00
elif command == ' pong ' :
if self . lastTimeISentData < ( int ( time . time ( ) ) - 298 ) :
#Send out a pong message to keep the connection alive.
2012-12-05 18:47:30 +01:00
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' Sending pong to ' , self . HOST , ' to keep connection alive. '
2012-12-05 18:47:30 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
try :
2012-11-23 09:22:56 +01:00
self . sock . sendall ( ' \xE9 \xBE \xB4 \xD9 \x70 \x6F \x6E \x67 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xcf \x83 \xe1 \x35 ' )
2012-11-19 20:45:05 +01:00
self . lastTimeISentData = int ( time . time ( ) )
except :
print ' self.sock.send pong failed '
self . sock . close ( )
sendDataQueues . remove ( self . mailbox )
print ' sendDataThread thread ' , self , ' ending now '
break
2012-12-05 18:47:30 +01:00
else :
printLock . acquire ( )
print ' sendDataThread ID: ' , id ( self ) , ' ignoring command ' , command , ' because it is not in stream ' , deststream
printLock . release ( )
2012-11-19 20:45:05 +01:00
#Wen you want to command a sendDataThread to do something, like shutdown or send some data, this function puts your data into the queues for each of the sendDataThreads. The sendDataThreads are responsible for putting their queue into (and out of) the sendDataQueues list.
def broadcastToSendDataQueues ( data ) :
#print 'running broadcastToSendDataQueues'
for q in sendDataQueues :
q . put ( ( data ) )
def flushInventory ( ) :
#Note that the singleCleanerThread clears out the inventory dictionary from time to time, although it only clears things that have been in the dictionary for a long time. This clears the inventory dictionary Now.
sqlLock . acquire ( )
for hash , storedValue in inventory . items ( ) :
objectType , streamNumber , payload , receivedTime = storedValue
t = ( hash , objectType , streamNumber , payload , receivedTime )
sqlSubmitQueue . put ( ''' INSERT INTO inventory VALUES (?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
del inventory [ hash ]
sqlLock . release ( )
def isInSqlInventory ( hash ) :
t = ( hash , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' select hash from inventory where hash=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn == [ ] :
return False
else :
return True
def convertIntToString ( n ) :
a = __builtins__ . hex ( n )
if a [ - 1 : ] == ' L ' :
a = a [ : - 1 ]
if ( len ( a ) % 2 ) == 0 :
return a [ 2 : ] . decode ( ' hex ' )
else :
return ( ' 0 ' + a [ 2 : ] ) . decode ( ' hex ' )
def convertStringToInt ( s ) :
return int ( s . encode ( ' hex ' ) , 16 )
2013-01-18 23:38:09 +01:00
def decodeWalletImportFormat ( WIFstring ) :
fullString = arithmetic . changebase ( WIFstring , 58 , 256 )
privkey = fullString [ : - 4 ]
if fullString [ - 4 : ] != hashlib . sha256 ( hashlib . sha256 ( privkey ) . digest ( ) ) . digest ( ) [ : 4 ] :
sys . stderr . write ( ' Major problem! When trying to decode one of your private keys, the checksum failed. Here is the PRIVATE key: %s \n ' % str ( WIFstring ) )
return " "
else :
#checksum passed
if privkey [ 0 ] == ' \x80 ' :
return privkey [ 1 : ]
else :
sys . stderr . write ( ' Major problem! When trying to decode one of your private keys, the checksum passed but the key doesn \' t begin with hex 80. Here is the PRIVATE key: %s \n ' % str ( WIFstring ) )
return " "
def reloadMyAddressHashes ( ) :
printLock . acquire ( )
2013-01-21 01:00:46 +01:00
print ' reloading keys from keys.dat file '
2013-01-18 23:38:09 +01:00
printLock . release ( )
myRSAAddressHashes . clear ( )
2013-02-18 21:01:47 +01:00
myECAddressHashes . clear ( )
2013-01-18 23:38:09 +01:00
#myPrivateKeys.clear()
configSections = config . sections ( )
for addressInKeysFile in configSections :
if addressInKeysFile < > ' bitmessagesettings ' :
isEnabled = config . getboolean ( addressInKeysFile , ' enabled ' )
if isEnabled :
status , addressVersionNumber , streamNumber , hash = decodeAddress ( addressInKeysFile )
if addressVersionNumber == 2 :
privEncryptionKey = decodeWalletImportFormat ( config . get ( addressInKeysFile , ' privencryptionkey ' ) ) . encode ( ' hex ' ) #returns a simple 32 bytes of information encoded in 64 Hex characters, or null if there was an error
if len ( privEncryptionKey ) == 64 : #It is 32 bytes encoded as 64 hex characters
myECAddressHashes [ hash ] = highlevelcrypto . makeCryptor ( privEncryptionKey )
elif addressVersionNumber == 1 :
n = config . getint ( addressInKeysFile , ' n ' )
e = config . getint ( addressInKeysFile , ' e ' )
d = config . getint ( addressInKeysFile , ' d ' )
p = config . getint ( addressInKeysFile , ' p ' )
q = config . getint ( addressInKeysFile , ' q ' )
myRSAAddressHashes [ hash ] = rsa . PrivateKey ( n , e , d , p , q )
#This function expects that pubkey begin with \x04
def calculateBitcoinAddressFromPubkey ( pubkey ) :
if len ( pubkey ) != 65 :
print ' Could not calculate Bitcoin address from pubkey because function was passed a pubkey that was ' , len ( pubkey ) , ' bytes long rather than 65. '
return " error "
ripe = hashlib . new ( ' ripemd160 ' )
sha = hashlib . new ( ' sha256 ' )
sha . update ( pubkey )
ripe . update ( sha . digest ( ) )
ripeWithProdnetPrefix = ' \x00 ' + ripe . digest ( )
checksum = hashlib . sha256 ( hashlib . sha256 ( ripeWithProdnetPrefix ) . digest ( ) ) . digest ( ) [ : 4 ]
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
numberOfZeroBytesOnBinaryBitcoinAddress = 0
while binaryBitcoinAddress [ 0 ] == ' \x00 ' :
numberOfZeroBytesOnBinaryBitcoinAddress + = 1
binaryBitcoinAddress = binaryBitcoinAddress [ 1 : ]
base58encoded = arithmetic . changebase ( binaryBitcoinAddress , 256 , 58 )
return " 1 " * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
def calculateTestnetAddressFromPubkey ( pubkey ) :
if len ( pubkey ) != 65 :
print ' Could not calculate Bitcoin address from pubkey because function was passed a pubkey that was ' , len ( pubkey ) , ' bytes long rather than 65. '
return " error "
ripe = hashlib . new ( ' ripemd160 ' )
sha = hashlib . new ( ' sha256 ' )
sha . update ( pubkey )
ripe . update ( sha . digest ( ) )
ripeWithProdnetPrefix = ' \x6F ' + ripe . digest ( )
checksum = hashlib . sha256 ( hashlib . sha256 ( ripeWithProdnetPrefix ) . digest ( ) ) . digest ( ) [ : 4 ]
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
numberOfZeroBytesOnBinaryBitcoinAddress = 0
while binaryBitcoinAddress [ 0 ] == ' \x00 ' :
numberOfZeroBytesOnBinaryBitcoinAddress + = 1
binaryBitcoinAddress = binaryBitcoinAddress [ 1 : ]
base58encoded = arithmetic . changebase ( binaryBitcoinAddress , 256 , 58 )
return " 1 " * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
2013-03-19 18:32:37 +01:00
def safeConfigGetBoolean ( section , field ) :
try :
if config . getboolean ( section , field ) :
return True
else :
return False
except :
return False
2013-02-26 21:07:51 +01:00
def lookupAppdataFolder ( ) :
APPNAME = " PyBitmessage "
from os import path , environ
if sys . platform == ' darwin ' :
if " HOME " in environ :
appdata = path . join ( os . environ [ " HOME " ] , " Library/Application support/ " , APPNAME ) + ' / '
else :
print ' Could not find home folder, please report this message and your OS X version to the BitMessage Github. '
sys . exit ( )
elif ' win32 ' in sys . platform or ' win64 ' in sys . platform :
appdata = path . join ( environ [ ' APPDATA ' ] , APPNAME ) + ' \\ '
else :
appdata = path . expanduser ( path . join ( " ~ " , " . " + APPNAME + " / " ) )
return appdata
2013-01-18 23:38:09 +01:00
2012-11-19 20:45:05 +01:00
#This thread exists because SQLITE3 is so un-threadsafe that we must submit queries to it and it puts results back in a different queue. They won't let us just use locks.
class sqlThread ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
def run ( self ) :
self . conn = sqlite3 . connect ( appdata + ' messages.dat ' )
self . conn . text_factory = str
self . cur = self . conn . cursor ( )
try :
self . cur . execute ( ''' CREATE TABLE inbox (msgid blob, toaddress text, fromaddress text, subject text, received text, message text, folder text, UNIQUE(msgid) ON CONFLICT REPLACE) ''' )
self . cur . execute ( ''' CREATE TABLE sent (msgid blob, toaddress text, toripe blob, fromaddress text, subject text, message text, ackdata blob, lastactiontime integer, status text, pubkeyretrynumber integer, msgretrynumber integer, folder text) ''' )
self . cur . execute ( ''' CREATE TABLE subscriptions (label text, address text, enabled bool) ''' )
self . cur . execute ( ''' CREATE TABLE addressbook (label text, address text) ''' )
self . cur . execute ( ''' CREATE TABLE blacklist (label text, address text, enabled bool) ''' )
self . cur . execute ( ''' CREATE TABLE whitelist (label text, address text, enabled bool) ''' )
2013-02-18 21:22:48 +01:00
#Explanation of what is in the pubkeys table:
# The hash is the RIPEMD160 hash that is encoded in the Bitmessage address.
2013-02-11 22:28:38 +01:00
# If you or someone else did the POW for this pubkey, then havecorrectnonce will be true. If you received the pubkey in a msg message then havecorrectnonce will be false. You won't have the correct nonce and won't be able to send the message to peers if they request the pubkey.
# transmitdata is literally the data that was included in the Bitmessage pubkey message when it arrived, except for the 24 byte protocol header- ie, it starts with the POW nonce.
# time is the time that the pubkey was broadcast on the network same as with every other type of Bitmessage object.
# usedpersonally is set to "yes" if we have used the key personally. This keeps us from deleting it because we may want to reply to a message in the future. This field is not a bool because we may need more flexability in the future and it doesn't take up much more space anyway.
2013-02-12 21:00:04 +01:00
self . cur . execute ( ''' CREATE TABLE pubkeys (hash blob, havecorrectnonce bool, transmitdata blob, time blob, usedpersonally text, UNIQUE(hash, havecorrectnonce) ON CONFLICT REPLACE) ''' )
2012-11-19 20:45:05 +01:00
self . cur . execute ( ''' CREATE TABLE inventory (hash blob, objecttype text, streamnumber int, payload blob, receivedtime integer, UNIQUE(hash) ON CONFLICT REPLACE) ''' )
2013-01-04 23:21:33 +01:00
self . cur . execute ( ''' CREATE TABLE knownnodes (timelastseen int, stream int, services blob, host blob, port blob, UNIQUE(host, stream, port) ON CONFLICT REPLACE) ''' ) #This table isn't used in the program yet but I have a feeling that we'll need it.
2013-02-12 21:00:04 +01:00
self . cur . execute ( ''' INSERT INTO subscriptions VALUES( ' Bitmessage new releases/announcements ' , ' BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx ' ,1) ''' )
2012-11-19 20:45:05 +01:00
self . conn . commit ( )
print ' Created messages database file '
except Exception , err :
if str ( err ) == ' table inbox already exists ' :
print ' Database file already exists. '
else :
sys . stderr . write ( ' ERROR trying to create database file (message.dat). Error message: %s \n ' % str ( err ) )
sys . exit ( )
2013-02-11 22:28:38 +01:00
2013-02-12 21:00:04 +01:00
#People running earlier versions of PyBitmessage do not have the usedpersonally field in their pubkeys table. Let's add it.
2013-02-11 22:28:38 +01:00
if config . getint ( ' bitmessagesettings ' , ' settingsversion ' ) == 2 :
item = ''' ALTER TABLE pubkeys ADD usedpersonally text DEFAULT ' no ' '''
parameters = ' '
self . cur . execute ( item , parameters )
self . conn . commit ( )
config . set ( ' bitmessagesettings ' , ' settingsversion ' , ' 3 ' )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2012-11-19 20:45:05 +01:00
try :
testpayload = ' \x00 \x00 '
2013-02-11 22:28:38 +01:00
t = ( ' 1234 ' , ' True ' , testpayload , ' 12345678 ' , ' no ' )
self . cur . execute ( ''' INSERT INTO pubkeys VALUES(?,?,?,?,?) ''' , t )
2012-11-19 20:45:05 +01:00
self . conn . commit ( )
self . cur . execute ( ''' SELECT transmitdata FROM pubkeys WHERE hash= ' 1234 ' ''' )
queryreturn = self . cur . fetchall ( )
for row in queryreturn :
transmitdata , = row
self . cur . execute ( ''' DELETE FROM pubkeys WHERE hash= ' 1234 ' ''' )
self . conn . commit ( )
if transmitdata == ' ' :
2012-12-18 19:09:10 +01:00
sys . stderr . write ( ' Problem: The version of SQLite you have cannot store Null values. Please download and install the latest revision of your version of Python (for example, the latest Python 2.7 revision) and try again. \n ' )
sys . stderr . write ( ' PyBitmessage will now exist very abruptly. You may now see threading errors related to this abrupt exit but the problem you need to solve is related to SQLite. \n \n ' )
2012-11-19 20:45:05 +01:00
sys . exit ( )
except Exception , err :
print err
while True :
item = sqlSubmitQueue . get ( )
parameters = sqlSubmitQueue . get ( )
#print 'item', item
#print 'parameters', parameters
self . cur . execute ( item , parameters )
sqlReturnQueue . put ( self . cur . fetchall ( ) )
sqlSubmitQueue . task_done ( )
self . conn . commit ( )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
''' The singleCleaner class is a timer-driven thread that cleans data structures to free memory, resends messages when a remote node doesn ' t respond, and sends pong messages to keep connections alive if the network isn ' t busy.
It cleans these data structures in memory :
inventory ( moves data to the on - disk sql database )
It cleans these tables on the disk :
inventory ( clears data more than 2 days and 12 hours old )
2013-02-12 21:00:04 +01:00
pubkeys ( clears pubkeys older than 4 weeks old which we have not used personally )
2012-11-19 20:45:05 +01:00
It resends messages when there has been no response :
2013-03-14 18:47:35 +01:00
resends getpubkey messages in 4 days ( then 8 days , then 16 days , etc . . . )
resends msg messages in 4 days ( then 8 days , then 16 days , etc . . . )
2012-11-19 20:45:05 +01:00
'''
class singleCleaner ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
def run ( self ) :
timeWeLastClearedInventoryAndPubkeysTables = 0
while True :
time . sleep ( 300 )
sqlLock . acquire ( )
2013-03-11 17:37:01 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , " Doing housekeeping (Flushing inventory in memory to disk...) " )
2012-11-19 20:45:05 +01:00
for hash , storedValue in inventory . items ( ) :
objectType , streamNumber , payload , receivedTime = storedValue
if int ( time . time ( ) ) - 3600 > receivedTime :
t = ( hash , objectType , streamNumber , payload , receivedTime )
sqlSubmitQueue . put ( ''' INSERT INTO inventory VALUES (?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
del inventory [ hash ]
2013-03-11 17:37:01 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , " " )
2012-11-19 20:45:05 +01:00
sqlLock . release ( )
broadcastToSendDataQueues ( ( 0 , ' pong ' , ' no data ' ) ) #commands the sendData threads to send out a pong message if they haven't sent anything else in the last five minutes. The socket timeout-time is 10 minutes.
if timeWeLastClearedInventoryAndPubkeysTables < int ( time . time ( ) ) - 7380 :
timeWeLastClearedInventoryAndPubkeysTables = int ( time . time ( ) )
#inventory (moves data from the inventory data structure to the on-disk sql database)
sqlLock . acquire ( )
#inventory (clears data more than 2 days and 12 hours old)
t = ( int ( time . time ( ) ) - lengthOfTimeToLeaveObjectsInInventory , )
sqlSubmitQueue . put ( ''' DELETE FROM inventory WHERE receivedtime<? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
2013-02-18 21:22:48 +01:00
#pubkeys
2013-02-11 22:28:38 +01:00
t = ( int ( time . time ( ) ) - lengthOfTimeToHoldOnToAllPubkeys , )
sqlSubmitQueue . put ( ''' DELETE FROM pubkeys WHERE time<? AND usedpersonally= ' no ' ''' )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
t = ( )
2013-01-31 19:35:36 +01:00
sqlSubmitQueue . put ( ''' select toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, status, pubkeyretrynumber, msgretrynumber FROM sent WHERE ((status= ' findingpubkey ' OR status= ' sentmessage ' ) AND folder= ' sent ' ) ''' ) #If the message's folder='trash' then we'll ignore it.
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
for row in queryreturn :
toaddress , toripe , fromaddress , subject , message , ackdata , lastactiontime , status , pubkeyretrynumber , msgretrynumber = row
if status == ' findingpubkey ' :
if int ( time . time ( ) ) - lastactiontime > ( maximumAgeOfAnObjectThatIAmWillingToAccept * ( 2 * * ( pubkeyretrynumber ) ) ) :
print ' It has been a long time and we haven \' t heard a response to our getpubkey request. Sending again. '
try :
2013-01-31 19:35:36 +01:00
del neededPubkeys [ toripe ] #We need to take this entry out of the neededPubkeys structure because the workerQueue checks to see whether the entry is already present and will not do the POW and send the message because it assumes that it has already done it recently.
2012-11-19 20:45:05 +01:00
except :
pass
workerQueue . put ( ( ' sendmessage ' , toaddress ) )
2013-02-26 00:09:32 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , " Doing work necessary to again attempt to request a public key... " )
2012-11-19 20:45:05 +01:00
t = ( int ( time . time ( ) ) , pubkeyretrynumber + 1 , toripe )
sqlSubmitQueue . put ( ''' UPDATE sent SET lastactiontime=?, pubkeyretrynumber=? WHERE toripe=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
#self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),toripe,'Public key requested again. ' + strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
else : # status == sentmessage
if int ( time . time ( ) ) - lastactiontime > ( maximumAgeOfAnObjectThatIAmWillingToAccept * ( 2 * * ( msgretrynumber ) ) ) :
print ' It has been a long time and we haven \' t heard an acknowledgement to our msg. Sending again. '
t = ( int ( time . time ( ) ) , msgretrynumber + 1 , ' findingpubkey ' , ackdata )
sqlSubmitQueue . put ( ''' UPDATE sent SET lastactiontime=?, msgretrynumber=?, status=? WHERE ackdata=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
#self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Message sent again because the acknowledgement was never received. ' + strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
workerQueue . put ( ( ' sendmessage ' , toaddress ) )
2013-02-26 00:09:32 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , " Doing work necessary to again attempt to deliver a message... " )
2012-11-19 20:45:05 +01:00
sqlLock . release ( )
2013-02-26 00:09:32 +01:00
2012-11-19 20:45:05 +01:00
#This thread, of which there is only one, does the heavy lifting: calculating POWs.
class singleWorker ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
def run ( self ) :
sqlLock . acquire ( )
2013-01-31 19:35:36 +01:00
sqlSubmitQueue . put ( ''' SELECT toripe FROM sent WHERE (status=? AND folder= ' sent ' ) ''' )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( ( ' findingpubkey ' , ) )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
for row in queryreturn :
toripe , = row
neededPubkeys [ toripe ] = 0
self . sendBroadcast ( ) #just in case there are any proof of work tasks for Broadcasts that have yet to be sent.
#Now let us see if there are any proofs of work for msg messages that we have yet to complete..
sqlLock . acquire ( )
t = ( ' doingpow ' , )
sqlSubmitQueue . put ( ' SELECT toripe FROM sent WHERE status=? ' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
for row in queryreturn :
toripe , = row
self . sendMsg ( toripe )
while True :
command , data = workerQueue . get ( )
#statusbar = 'The singleWorker thread is working on work.'
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar)
if command == ' sendmessage ' :
toAddress = data
toStatus , toAddressVersionNumber , toStreamNumber , toRipe = decodeAddress ( toAddress )
#print 'message type', type(message)
#print repr(message.toUtf8())
#print str(message.toUtf8())
sqlLock . acquire ( )
sqlSubmitQueue . put ( ' SELECT * FROM pubkeys WHERE hash=? ' )
sqlSubmitQueue . put ( ( toRipe , ) )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
#print 'queryreturn', queryreturn
if queryreturn == [ ] :
#We'll need to request the pub key because we don't have it.
if not toRipe in neededPubkeys :
neededPubkeys [ toRipe ] = 0
2013-01-18 23:38:09 +01:00
print ' requesting pubkey: ' , toRipe . encode ( ' hex ' )
2012-11-19 20:45:05 +01:00
self . requestPubKey ( toAddressVersionNumber , toStreamNumber , toRipe )
else :
print ' We have already requested this pubkey (the ripe hash is in neededPubkeys). We will re-request again soon. '
self . emit ( SIGNAL ( " updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject) " ) , toRipe , ' Public key was requested earlier. Receiver must be offline. Will retry. ' )
else :
print ' We already have the necessary public key. '
2013-02-12 21:00:04 +01:00
self . sendMsg ( toRipe ) #by calling this function, we are asserting that we already have the pubkey for toRipe
2012-11-19 20:45:05 +01:00
elif command == ' sendbroadcast ' :
print ' Within WorkerThread, processing sendbroadcast command. '
fromAddress , subject , message = data
self . sendBroadcast ( )
2013-01-22 20:29:49 +01:00
elif command == ' doPOWForMyV2Pubkey ' :
self . doPOWForMyV2Pubkey ( data )
2012-11-19 20:45:05 +01:00
elif command == ' newpubkey ' :
toAddressVersion , toStreamNumber , toRipe = data
if toRipe in neededPubkeys :
print ' We have been awaiting the arrival of this pubkey. '
del neededPubkeys [ toRipe ]
self . sendMsg ( toRipe )
else :
2013-01-18 23:38:09 +01:00
print ' We don \' t need this pub key. We didn \' t ask for it. Pubkey hash: ' , toRipe . encode ( ' hex ' )
2013-03-19 18:32:37 +01:00
else :
printLock . acquire ( )
sys . stderr . write ( ' Probable programming error: The command sent to the workerThread is weird. It is: %s \n ' % command )
printLock . release ( )
2012-11-19 20:45:05 +01:00
workerQueue . task_done ( )
2013-01-22 20:29:49 +01:00
def doPOWForMyV2Pubkey ( self , myAddress ) : #This function also broadcasts out the pubkey message once it is done with the POW
status , addressVersionNumber , streamNumber , hash = decodeAddress ( myAddress )
2013-02-11 22:28:38 +01:00
embeddedTime = int ( time . time ( ) ) + random . randrange ( - 300 , 300 ) #the current time plus or minus five minutes
payload = pack ( ' >I ' , ( embeddedTime ) )
2013-01-22 20:29:49 +01:00
payload + = encodeVarint ( 2 ) #Address version number
payload + = encodeVarint ( streamNumber )
payload + = ' \x00 \x00 \x00 \x01 ' #bitfield of features supported by me (see the wiki).
2013-02-12 21:00:04 +01:00
try :
privSigningKeyBase58 = config . get ( myAddress , ' privsigningkey ' )
privEncryptionKeyBase58 = config . get ( myAddress , ' privencryptionkey ' )
except Exception , err :
printLock . acquire ( )
sys . stderr . write ( ' Error within doPOWForMyV2Pubkey. Could not read the keys from the keys.dat file for a requested address. %s \n ' % err )
printLock . release ( )
return
2013-01-22 20:29:49 +01:00
privSigningKeyHex = decodeWalletImportFormat ( privSigningKeyBase58 ) . encode ( ' hex ' )
privEncryptionKeyHex = decodeWalletImportFormat ( privEncryptionKeyBase58 ) . encode ( ' hex ' )
pubSigningKey = highlevelcrypto . privToPub ( privSigningKeyHex ) . decode ( ' hex ' )
pubEncryptionKey = highlevelcrypto . privToPub ( privEncryptionKeyHex ) . decode ( ' hex ' )
payload + = pubSigningKey [ 1 : ]
payload + = pubEncryptionKey [ 1 : ]
2013-02-12 21:00:04 +01:00
#Do the POW for this pubkey message
2013-01-22 20:29:49 +01:00
nonce = 0
trialValue = 99999999999999999999
target = 2 * * 64 / ( ( len ( payload ) + payloadLengthExtraBytes + 8 ) * averageProofOfWorkNonceTrialsPerByte )
print ' (For pubkey message) Doing proof of work... '
initialHash = hashlib . sha512 ( payload ) . digest ( )
while trialValue > target :
nonce + = 1
trialValue , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( pack ( ' >Q ' , nonce ) + initialHash ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
print ' (For pubkey message) Found proof of work ' , trialValue , ' Nonce: ' , nonce
payload = pack ( ' >Q ' , nonce ) + payload
2013-02-11 22:28:38 +01:00
t = ( hash , True , payload , embeddedTime , ' no ' )
2013-01-22 20:29:49 +01:00
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
sqlSubmitQueue . put ( ''' INSERT INTO pubkeys VALUES (?,?,?,?,?) ''' )
2013-01-22 20:29:49 +01:00
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
inventoryHash = calculateInventoryHash ( payload )
objectType = ' pubkey '
2013-02-11 22:28:38 +01:00
inventory [ inventoryHash ] = ( objectType , streamNumber , payload , embeddedTime )
2013-01-22 20:29:49 +01:00
printLock . acquire ( )
2013-01-25 19:16:03 +01:00
print ' broadcasting inv with hash: ' , inventoryHash . encode ( ' hex ' )
2013-01-22 20:29:49 +01:00
printLock . release ( )
2013-02-03 06:16:50 +01:00
broadcastToSendDataQueues ( ( streamNumber , ' sendinv ' , inventoryHash ) )
2013-02-15 21:23:34 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , " " )
2013-01-22 20:29:49 +01:00
2012-11-19 20:45:05 +01:00
def sendBroadcast ( self ) :
sqlLock . acquire ( )
t = ( ' broadcastpending ' , )
sqlSubmitQueue . put ( ' SELECT fromaddress, subject, message, ackdata FROM sent WHERE status=? ' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
for row in queryreturn :
fromaddress , subject , body , ackdata = row
status , addressVersionNumber , streamNumber , ripe = decodeAddress ( fromaddress )
2013-01-24 20:52:48 +01:00
if addressVersionNumber == 2 :
#We need to convert our private keys to public keys in order to include them.
2013-03-19 18:32:37 +01:00
try :
privSigningKeyBase58 = config . get ( fromaddress , ' privsigningkey ' )
privEncryptionKeyBase58 = config . get ( fromaddress , ' privencryptionkey ' )
except :
self . emit ( SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , ackdata , ' Error! Could not find sender address (your address) in the keys.dat file. ' )
continue
2012-11-19 20:45:05 +01:00
2013-01-24 20:52:48 +01:00
privSigningKeyHex = decodeWalletImportFormat ( privSigningKeyBase58 ) . encode ( ' hex ' )
privEncryptionKeyHex = decodeWalletImportFormat ( privEncryptionKeyBase58 ) . encode ( ' hex ' )
2012-11-19 20:45:05 +01:00
2013-01-24 20:52:48 +01:00
pubSigningKey = highlevelcrypto . privToPub ( privSigningKeyHex ) . decode ( ' hex ' ) #At this time these pubkeys are 65 bytes long because they include the encoding byte which we won't be sending in the broadcast message.
2013-02-18 21:22:48 +01:00
pubEncryptionKey = highlevelcrypto . privToPub ( privEncryptionKeyHex ) . decode ( ' hex ' )
2013-02-11 22:28:38 +01:00
payload = pack ( ' >I ' , ( int ( time . time ( ) ) + random . randrange ( - 300 , 300 ) ) ) #the current time plus or minus five minutes
2013-01-24 20:52:48 +01:00
payload + = encodeVarint ( 1 ) #broadcast version
payload + = encodeVarint ( addressVersionNumber )
payload + = encodeVarint ( streamNumber )
payload + = ' \x00 \x00 \x00 \x01 ' #behavior bitfield
payload + = pubSigningKey [ 1 : ]
2013-02-18 21:22:48 +01:00
payload + = pubEncryptionKey [ 1 : ]
2013-01-24 20:52:48 +01:00
payload + = ripe
payload + = ' \x02 ' #message encoding type
payload + = encodeVarint ( len ( ' Subject: ' + subject + ' \n ' + ' Body: ' + body ) ) #Type 2 is simple UTF-8 message encoding.
payload + = ' Subject: ' + subject + ' \n ' + ' Body: ' + body
2013-02-18 21:22:48 +01:00
2013-01-24 20:52:48 +01:00
signature = highlevelcrypto . sign ( payload , privSigningKeyHex )
payload + = encodeVarint ( len ( signature ) )
payload + = signature
2013-02-18 21:22:48 +01:00
2013-01-24 20:52:48 +01:00
nonce = 0
trialValue = 99999999999999999999
target = 2 * * 64 / ( ( len ( payload ) + payloadLengthExtraBytes + 8 ) * averageProofOfWorkNonceTrialsPerByte )
print ' (For broadcast message) Doing proof of work... '
2013-03-19 18:32:37 +01:00
self . emit ( SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , ackdata , ' Doing work necessary to send broadcast... ' )
2013-01-24 20:52:48 +01:00
initialHash = hashlib . sha512 ( payload ) . digest ( )
while trialValue > target :
nonce + = 1
trialValue , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( pack ( ' >Q ' , nonce ) + initialHash ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
print ' (For broadcast message) Found proof of work ' , trialValue , ' Nonce: ' , nonce
2012-11-19 20:45:05 +01:00
2013-01-24 20:52:48 +01:00
payload = pack ( ' >Q ' , nonce ) + payload
2012-11-19 20:45:05 +01:00
2013-01-24 20:52:48 +01:00
inventoryHash = calculateInventoryHash ( payload )
objectType = ' broadcast '
inventory [ inventoryHash ] = ( objectType , streamNumber , payload , int ( time . time ( ) ) )
print ' sending inv (within sendBroadcast function) '
2013-02-03 06:16:50 +01:00
broadcastToSendDataQueues ( ( streamNumber , ' sendinv ' , inventoryHash ) )
2013-01-24 20:52:48 +01:00
self . emit ( SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , ackdata , ' Broadcast sent at ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( time . time ( ) ) ) ) )
#Update the status of the message in the 'sent' table to have a 'broadcastsent' status
sqlLock . acquire ( )
t = ( ' broadcastsent ' , int ( time . time ( ) ) , fromaddress , subject , body , ' broadcastpending ' )
sqlSubmitQueue . put ( ' UPDATE sent SET status=?, lastactiontime=? WHERE fromaddress=? AND subject=? AND message=? AND status=? ' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
2013-02-18 21:22:48 +01:00
2013-03-04 22:22:06 +01:00
""" elif addressVersionNumber == 1: #This whole section can be taken out soon because we aren ' t supporting v1 addresses for much longer.
2013-01-24 20:52:48 +01:00
messageToTransmit = ' \x02 ' #message encoding type
messageToTransmit + = encodeVarint ( len ( ' Subject: ' + subject + ' \n ' + ' Body: ' + body ) ) #Type 2 is simple UTF-8 message encoding.
messageToTransmit + = ' Subject: ' + subject + ' \n ' + ' Body: ' + body
#We need the all the integers for our private key in order to sign our message, and we need our public key to send with the message.
n = config . getint ( fromaddress , ' n ' )
e = config . getint ( fromaddress , ' e ' )
d = config . getint ( fromaddress , ' d ' )
p = config . getint ( fromaddress , ' p ' )
q = config . getint ( fromaddress , ' q ' )
nString = convertIntToString ( n )
eString = convertIntToString ( e )
#myPubkey = rsa.PublicKey(n,e)
myPrivatekey = rsa . PrivateKey ( n , e , d , p , q )
#The payload of the broadcast message starts with a POW, but that will be added later.
payload = pack ( ' >I ' , ( int ( time . time ( ) ) ) )
payload + = encodeVarint ( 1 ) #broadcast version
payload + = encodeVarint ( addressVersionNumber )
payload + = encodeVarint ( streamNumber )
payload + = ripe
payload + = encodeVarint ( len ( nString ) )
payload + = nString
payload + = encodeVarint ( len ( eString ) )
payload + = eString
payload + = messageToTransmit
signature = rsa . sign ( messageToTransmit , myPrivatekey , ' SHA-512 ' )
#print 'signature', signature.encode('hex')
payload + = signature
2012-11-19 20:45:05 +01:00
2013-01-24 20:52:48 +01:00
#print 'nString', repr(nString)
#print 'eString', repr(eString)
nonce = 0
trialValue = 99999999999999999999
target = 2 * * 64 / ( ( len ( payload ) + payloadLengthExtraBytes + 8 ) * averageProofOfWorkNonceTrialsPerByte )
print ' (For broadcast message) Doing proof of work... '
initialHash = hashlib . sha512 ( payload ) . digest ( )
while trialValue > target :
nonce + = 1
trialValue , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( pack ( ' >Q ' , nonce ) + initialHash ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
print ' (For broadcast message) Found proof of work ' , trialValue , ' Nonce: ' , nonce
payload = pack ( ' >Q ' , nonce ) + payload
inventoryHash = calculateInventoryHash ( payload )
objectType = ' broadcast '
inventory [ inventoryHash ] = ( objectType , streamNumber , payload , int ( time . time ( ) ) )
print ' sending inv (within sendBroadcast function) '
2013-02-03 06:16:50 +01:00
broadcastToSendDataQueues ( ( streamNumber , ' sendinv ' , inventoryHash ) )
2013-01-24 20:52:48 +01:00
self . emit ( SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , ackdata , ' Broadcast sent at ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( time . time ( ) ) ) ) )
#Update the status of the message in the 'sent' table to have a 'broadcastsent' status
sqlLock . acquire ( )
t = ( ' broadcastsent ' , int ( time . time ( ) ) , fromaddress , subject , body , ' broadcastpending ' )
sqlSubmitQueue . put ( ' UPDATE sent SET status=?, lastactiontime=? WHERE fromaddress=? AND subject=? AND message=? AND status=? ' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
2013-03-04 22:22:06 +01:00
sqlLock . release ( ) """
2013-01-24 20:52:48 +01:00
else :
printLock . acquire ( )
print ' In the singleWorker thread, the sendBroadcast function doesn \' t understand the address version '
printLock . release ( )
2012-11-19 20:45:05 +01:00
def sendMsg ( self , toRipe ) :
sqlLock . acquire ( )
2013-02-18 21:22:48 +01:00
t = ( ' doingpow ' , ' findingpubkey ' , toRipe )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( ' UPDATE sent SET status=? WHERE status=? AND toripe=? ' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
t = ( ' doingpow ' , toRipe )
sqlSubmitQueue . put ( ' SELECT toaddress, fromaddress, subject, message, ackdata FROM sent WHERE status=? AND toripe=? ' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
for row in queryreturn :
toaddress , fromaddress , subject , message , ackdata = row
2013-01-04 23:21:33 +01:00
ackdataForWhichImWatching [ ackdata ] = 0
2013-01-18 23:38:09 +01:00
toStatus , toAddressVersionNumber , toStreamNumber , toHash = decodeAddress ( toaddress )
fromStatus , fromAddressVersionNumber , fromStreamNumber , fromHash = decodeAddress ( fromaddress )
2012-11-19 20:45:05 +01:00
self . emit ( SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , ackdata , ' Doing work necessary to send the message. ' )
printLock . acquire ( )
2013-01-24 22:07:24 +01:00
print ' Found a message in our database that needs to be sent with this pubkey. '
2012-11-19 20:45:05 +01:00
print ' First 150 characters of message: ' , message [ : 150 ]
printLock . release ( )
2013-02-11 22:28:38 +01:00
embeddedTime = pack ( ' >I ' , ( int ( time . time ( ) ) + random . randrange ( - 300 , 300 ) ) ) #the current time plus or minus five minutes. We will use this time both for our message and for the ackdata packed within our message.
2013-01-18 23:38:09 +01:00
if fromAddressVersionNumber == 2 :
payload = ' \x01 ' #Message version.
payload + = encodeVarint ( fromAddressVersionNumber )
payload + = encodeVarint ( fromStreamNumber )
2013-02-07 22:31:15 +01:00
payload + = ' \x00 \x00 \x00 \x01 ' #Bitfield of features and behaviors that can be expected from me. (See https://bitmessage.org/wiki/Protocol_specification#Pubkey_bitfield_features )
2013-01-18 23:38:09 +01:00
#We need to convert our private keys to public keys in order to include them.
2013-03-19 18:32:37 +01:00
try :
privSigningKeyBase58 = config . get ( fromaddress , ' privsigningkey ' )
privEncryptionKeyBase58 = config . get ( fromaddress , ' privencryptionkey ' )
except :
self . emit ( SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , ackdata , ' Error! Could not find sender address (your address) in the keys.dat file. ' )
continue
2013-01-18 23:38:09 +01:00
privSigningKeyHex = decodeWalletImportFormat ( privSigningKeyBase58 ) . encode ( ' hex ' )
privEncryptionKeyHex = decodeWalletImportFormat ( privEncryptionKeyBase58 ) . encode ( ' hex ' )
pubSigningKey = highlevelcrypto . privToPub ( privSigningKeyHex ) . decode ( ' hex ' )
pubEncryptionKey = highlevelcrypto . privToPub ( privEncryptionKeyHex ) . decode ( ' hex ' )
2013-02-07 22:31:15 +01:00
payload + = pubSigningKey [ 1 : ] #The \x04 on the beginning of the public keys are not sent. This way there is only one acceptable way to encode and send a public key.
2013-01-18 23:38:09 +01:00
payload + = pubEncryptionKey [ 1 : ]
2013-02-07 22:31:15 +01:00
payload + = toHash #This hash will be checked by the receiver of the message to verify that toHash belongs to them. This prevents a Surreptitious Forwarding Attack.
payload + = ' \x02 ' #Type 2 is simple UTF-8 message encoding as specified on the Protocol Specification on the Bitmessage Wiki.
2013-01-18 23:38:09 +01:00
messageToTransmit = ' Subject: ' + subject + ' \n ' + ' Body: ' + message
payload + = encodeVarint ( len ( messageToTransmit ) )
payload + = messageToTransmit
2013-02-11 22:28:38 +01:00
fullAckPayload = self . generateFullAckMessage ( ackdata , toStreamNumber , embeddedTime ) #The fullAckPayload is a normal msg protocol message with the proof of work already completed that the receiver of this message can easily send out.
2013-01-18 23:38:09 +01:00
payload + = encodeVarint ( len ( fullAckPayload ) )
payload + = fullAckPayload
signature = highlevelcrypto . sign ( payload , privSigningKeyHex )
payload + = encodeVarint ( len ( signature ) )
payload + = signature
2013-03-04 22:22:06 +01:00
""" elif fromAddressVersionNumber == 1: #This code is for old version 1 (RSA) addresses. It will soon be removed.
2013-01-18 23:38:09 +01:00
payload = ' \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' #this run of nulls allows the true message receiver to identify his message
payload + = ' \x01 ' #Message version.
payload + = ' \x00 \x00 \x00 \x01 '
2013-02-18 21:22:48 +01:00
2013-01-18 23:38:09 +01:00
payload + = encodeVarint ( fromAddressVersionNumber )
payload + = encodeVarint ( fromStreamNumber )
2012-11-19 20:45:05 +01:00
2013-01-25 19:29:31 +01:00
try :
sendersN = convertIntToString ( config . getint ( fromaddress , ' n ' ) )
except :
printLock . acquire ( )
print ' Error: Could not find ' , fromaddress , ' in our keys.dat file. You must have deleted it. Aborting the send. '
printLock . release ( )
return
2013-01-18 23:38:09 +01:00
payload + = encodeVarint ( len ( sendersN ) )
payload + = sendersN
2012-11-19 20:45:05 +01:00
2013-01-18 23:38:09 +01:00
sendersE = convertIntToString ( config . getint ( fromaddress , ' e ' ) )
payload + = encodeVarint ( len ( sendersE ) )
payload + = sendersE
2012-11-19 20:45:05 +01:00
2013-01-18 23:38:09 +01:00
payload + = ' \x02 ' #Type 2 is simple UTF-8 message encoding.
messageToTransmit = ' Subject: ' + subject + ' \n ' + ' Body: ' + message
payload + = encodeVarint ( len ( messageToTransmit ) )
payload + = messageToTransmit
2012-11-19 20:45:05 +01:00
2013-01-18 23:38:09 +01:00
#Later, if anyone impliments clients that don't send the ack_data, then we should probably check here to make sure that the receiver will make use of this ack_data and not attach it if not.
2013-02-11 22:28:38 +01:00
fullAckPayload = self . generateFullAckMessage ( ackdata , toStreamNumber , embeddedTime )
2013-01-18 23:38:09 +01:00
payload + = encodeVarint ( len ( fullAckPayload ) )
payload + = fullAckPayload
sendersPrivKey = rsa . PrivateKey ( config . getint ( fromaddress , ' n ' ) , config . getint ( fromaddress , ' e ' ) , config . getint ( fromaddress , ' d ' ) , config . getint ( fromaddress , ' p ' ) , config . getint ( fromaddress , ' q ' ) )
2012-11-19 20:45:05 +01:00
2013-03-04 22:22:06 +01:00
payload + = rsa . sign ( payload , sendersPrivKey , ' SHA-512 ' ) """
2012-11-19 20:45:05 +01:00
2013-01-18 23:38:09 +01:00
#We have assembled the data that will be encrypted. Now let us fetch the recipient's public key out of our database and do the encryption.
2012-11-19 20:45:05 +01:00
2013-01-18 23:38:09 +01:00
if toAddressVersionNumber == 2 :
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
sqlSubmitQueue . put ( ' SELECT transmitdata FROM pubkeys WHERE hash=? ' )
2013-01-18 23:38:09 +01:00
sqlSubmitQueue . put ( ( toRipe , ) )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
2012-11-19 20:45:05 +01:00
2013-01-18 23:38:09 +01:00
for row in queryreturn :
2013-02-11 22:28:38 +01:00
pubkeyPayload , = row
2013-01-18 23:38:09 +01:00
2013-02-07 22:31:15 +01:00
#The pubkey is stored the way we originally received it which means that we need to read beyond things like the nonce and time to get to the public keys.
2013-01-18 23:38:09 +01:00
readPosition = 8 #to bypass the nonce
readPosition + = 4 #to bypass the embedded time
readPosition + = 1 #to bypass the address version whose length is definitely 1
streamNumber , streamNumberLength = decodeVarint ( pubkeyPayload [ readPosition : readPosition + 10 ] )
readPosition + = streamNumberLength
behaviorBitfield = pubkeyPayload [ readPosition : readPosition + 4 ]
readPosition + = 4 #to bypass the bitfield of behaviors
#pubSigningKeyBase256 = pubkeyPayload[readPosition:readPosition+64] #We don't use this key for anything here.
readPosition + = 64
pubEncryptionKeyBase256 = pubkeyPayload [ readPosition : readPosition + 64 ]
readPosition + = 64
encrypted = highlevelcrypto . encrypt ( payload , " 04 " + pubEncryptionKeyBase256 . encode ( ' hex ' ) )
2013-03-04 22:22:06 +01:00
""" elif toAddressVersionNumber == 1:
2013-01-18 23:38:09 +01:00
sqlLock . acquire ( )
2013-02-11 22:28:38 +01:00
sqlSubmitQueue . put ( ' SELECT transmitdata FROM pubkeys WHERE hash=? ' )
2013-01-18 23:38:09 +01:00
sqlSubmitQueue . put ( ( toRipe , ) )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
for row in queryreturn :
2013-02-11 22:28:38 +01:00
pubkeyPayload , = row
2013-01-18 23:38:09 +01:00
readPosition = 8 #to bypass the nonce
behaviorBitfield = pubkeyPayload [ 8 : 12 ]
readPosition + = 4 #to bypass the bitfield of behaviors
addressVersion , addressVersionLength = decodeVarint ( pubkeyPayload [ readPosition : readPosition + 10 ] )
readPosition + = addressVersionLength
streamNumber , streamNumberLength = decodeVarint ( pubkeyPayload [ readPosition : readPosition + 10 ] )
readPosition + = streamNumberLength
nLength , nLengthLength = decodeVarint ( pubkeyPayload [ readPosition : readPosition + 10 ] )
readPosition + = nLengthLength
n = convertStringToInt ( pubkeyPayload [ readPosition : readPosition + nLength ] )
readPosition + = nLength
eLength , eLengthLength = decodeVarint ( pubkeyPayload [ readPosition : readPosition + 10 ] )
readPosition + = eLengthLength
e = convertStringToInt ( pubkeyPayload [ readPosition : readPosition + eLength ] )
receiversPubkey = rsa . PublicKey ( n , e )
infile = cStringIO . StringIO ( payload )
outfile = cStringIO . StringIO ( )
#print 'Encrypting using public key:', receiversPubkey
encrypt_bigfile ( infile , outfile , receiversPubkey )
encrypted = outfile . getvalue ( )
infile . close ( )
2013-03-04 22:22:06 +01:00
outfile . close ( ) """
2012-11-19 20:45:05 +01:00
nonce = 0
2012-11-20 22:34:13 +01:00
trialValue = 99999999999999999999
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
encodedStreamNumber = encodeVarint ( toStreamNumber )
#We are now dropping the unencrypted data in payload since it has already been encrypted and replacing it with the encrypted payload that we will send out.
payload = embeddedTime + encodedStreamNumber + encrypted
target = 2 * * 64 / ( ( len ( payload ) + payloadLengthExtraBytes + 8 ) * averageProofOfWorkNonceTrialsPerByte )
2012-11-20 22:34:13 +01:00
print ' (For msg message) Doing proof of work. Target: ' , target
powStartTime = time . time ( )
2012-11-23 09:22:56 +01:00
initialHash = hashlib . sha512 ( payload ) . digest ( )
2012-11-19 20:45:05 +01:00
while trialValue > target :
nonce + = 1
2012-11-23 09:22:56 +01:00
trialValue , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( pack ( ' >Q ' , nonce ) + initialHash ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
2012-11-19 20:45:05 +01:00
print ' (For msg message) Found proof of work ' , trialValue , ' Nonce: ' , nonce
2013-03-19 18:32:37 +01:00
try :
print ' POW took ' , int ( time . time ( ) - powStartTime ) , ' seconds. ' , nonce / ( time . time ( ) - powStartTime ) , ' nonce trials per second. '
except :
pass
2012-11-19 20:45:05 +01:00
payload = pack ( ' >Q ' , nonce ) + payload
inventoryHash = calculateInventoryHash ( payload )
objectType = ' msg '
inventory [ inventoryHash ] = ( objectType , toStreamNumber , payload , int ( time . time ( ) ) )
self . emit ( SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , ackdata , ' Message sent. Waiting on acknowledgement. Sent on ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( time . time ( ) ) ) ) )
print ' sending inv (within sendmsg function) '
2013-02-03 06:16:50 +01:00
broadcastToSendDataQueues ( ( streamNumber , ' sendinv ' , inventoryHash ) )
2012-11-19 20:45:05 +01:00
#Update the status of the message in the 'sent' table to have a 'sent' status
sqlLock . acquire ( )
t = ( ' sentmessage ' , toaddress , fromaddress , subject , message , ' doingpow ' )
sqlSubmitQueue . put ( ' UPDATE sent SET status=? WHERE toaddress=? AND fromaddress=? AND subject=? AND message=? AND status=? ' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
2013-02-11 22:28:38 +01:00
t = ( toRipe , )
sqlSubmitQueue . put ( ''' UPDATE pubkeys SET usedpersonally= ' yes ' WHERE hash=? ''' )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
def requestPubKey ( self , addressVersionNumber , streamNumber , ripe ) :
payload = pack ( ' >I ' , int ( time . time ( ) ) )
payload + = encodeVarint ( addressVersionNumber )
payload + = encodeVarint ( streamNumber )
payload + = ripe
2013-02-04 22:49:02 +01:00
printLock . acquire ( )
2013-01-18 23:38:09 +01:00
print ' making request for pubkey with ripe: ' , ripe . encode ( ' hex ' )
2013-02-04 22:49:02 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
nonce = 0
2012-11-20 22:34:13 +01:00
trialValue = 99999999999999999999
2012-11-19 20:45:05 +01:00
#print 'trial value', trialValue
2013-02-18 21:01:47 +01:00
statusbar = ' Doing the computations necessary to request the recipient \' s public key. '
2012-11-19 20:45:05 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , statusbar )
self . emit ( SIGNAL ( " updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject) " ) , ripe , ' Doing work necessary to request public key. ' )
print ' Doing proof-of-work necessary to send getpubkey message. '
target = 2 * * 64 / ( ( len ( payload ) + payloadLengthExtraBytes + 8 ) * averageProofOfWorkNonceTrialsPerByte )
2012-11-23 09:22:56 +01:00
initialHash = hashlib . sha512 ( payload ) . digest ( )
2012-11-19 20:45:05 +01:00
while trialValue > target :
nonce + = 1
2012-11-23 09:22:56 +01:00
trialValue , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( pack ( ' >Q ' , nonce ) + initialHash ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
2013-02-04 22:49:02 +01:00
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' Found proof of work ' , trialValue , ' Nonce: ' , nonce
2013-02-04 22:49:02 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
payload = pack ( ' >Q ' , nonce ) + payload
inventoryHash = calculateInventoryHash ( payload )
objectType = ' getpubkey '
inventory [ inventoryHash ] = ( objectType , streamNumber , payload , int ( time . time ( ) ) )
print ' sending inv (for the getpubkey message) '
2013-02-03 06:16:50 +01:00
broadcastToSendDataQueues ( ( streamNumber , ' sendinv ' , inventoryHash ) )
2012-11-19 20:45:05 +01:00
2013-02-18 21:01:47 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , ' Broacasting the public key request. This program will auto-retry if they are offline. ' )
2012-11-19 20:45:05 +01:00
self . emit ( SIGNAL ( " updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject) " ) , ripe , ' Sending public key request. Waiting for reply. Requested at ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( time . time ( ) ) ) ) )
2013-02-11 22:28:38 +01:00
def generateFullAckMessage ( self , ackdata , toStreamNumber , embeddedTime ) :
2012-11-19 20:45:05 +01:00
nonce = 0
2012-11-20 22:34:13 +01:00
trialValue = 99999999999999999999
2012-11-19 20:45:05 +01:00
encodedStreamNumber = encodeVarint ( toStreamNumber )
payload = embeddedTime + encodedStreamNumber + ackdata
target = 2 * * 64 / ( ( len ( payload ) + payloadLengthExtraBytes + 8 ) * averageProofOfWorkNonceTrialsPerByte )
2012-11-23 09:22:56 +01:00
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' (For ack message) Doing proof of work... '
2012-11-23 09:22:56 +01:00
printLock . release ( )
powStartTime = time . time ( )
initialHash = hashlib . sha512 ( payload ) . digest ( )
2012-11-19 20:45:05 +01:00
while trialValue > target :
nonce + = 1
2012-11-23 09:22:56 +01:00
trialValue , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( pack ( ' >Q ' , nonce ) + initialHash ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' (For ack message) Found proof of work ' , trialValue , ' Nonce: ' , nonce
2013-03-19 18:32:37 +01:00
try :
print ' POW took ' , int ( time . time ( ) - powStartTime ) , ' seconds. ' , nonce / ( time . time ( ) - powStartTime ) , ' nonce trials per second. '
except :
pass
2012-11-23 09:22:56 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
payload = pack ( ' >Q ' , nonce ) + payload
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' msg \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) )
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2012-11-19 20:45:05 +01:00
return headerData + payload
class addressGenerator ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
2013-01-16 17:52:52 +01:00
def setup ( self , addressVersionNumber , streamNumber , label = " (no label) " , numberOfAddressesToMake = 1 , deterministicPassphrase = " " , eighteenByteRipe = False ) :
self . addressVersionNumber = addressVersionNumber
2012-11-19 20:45:05 +01:00
self . streamNumber = streamNumber
self . label = label
2013-01-16 17:52:52 +01:00
self . numberOfAddressesToMake = numberOfAddressesToMake
self . deterministicPassphrase = deterministicPassphrase
self . eighteenByteRipe = eighteenByteRipe
2012-11-19 20:45:05 +01:00
def run ( self ) :
2013-01-16 17:52:52 +01:00
if self . addressVersionNumber == 2 :
2012-11-19 20:45:05 +01:00
2013-01-16 17:52:52 +01:00
if self . deterministicPassphrase == " " :
statusbar = ' Generating one new address '
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , statusbar )
#This next section is a little bit strange. We're going to generate keys over and over until we
#find one that starts with either \x00 or \x00\x00. Then when we pack them into a Bitmessage address,
2013-02-18 21:22:48 +01:00
#we won't store the \x00 or \x00\x00 bytes thus making the address shorter.
2013-01-21 01:00:46 +01:00
startTime = time . time ( )
numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix = 0
2013-02-07 22:31:15 +01:00
potentialPrivSigningKey = OpenSSL . rand ( 32 )
potentialPubSigningKey = self . pointMult ( potentialPrivSigningKey )
2013-01-16 17:52:52 +01:00
while True :
2013-01-21 01:00:46 +01:00
numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix + = 1
2013-01-16 17:52:52 +01:00
potentialPrivEncryptionKey = OpenSSL . rand ( 32 )
potentialPubEncryptionKey = self . pointMult ( potentialPrivEncryptionKey )
#print 'potentialPubSigningKey', potentialPubSigningKey.encode('hex')
#print 'potentialPubEncryptionKey', potentialPubEncryptionKey.encode('hex')
ripe = hashlib . new ( ' ripemd160 ' )
sha = hashlib . new ( ' sha512 ' )
sha . update ( potentialPubSigningKey + potentialPubEncryptionKey )
ripe . update ( sha . digest ( ) )
#print 'potential ripe.digest', ripe.digest().encode('hex')
if self . eighteenByteRipe :
if ripe . digest ( ) [ : 2 ] == ' \x00 \x00 ' :
break
else :
if ripe . digest ( ) [ : 1 ] == ' \x00 ' :
break
2013-01-24 22:07:24 +01:00
print ' Generated address with ripe digest: ' , ripe . digest ( ) . encode ( ' hex ' )
print ' Address generator calculated ' , numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix , ' addresses at ' , numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix / ( time . time ( ) - startTime ) , ' addresses per second before finding one with the correct ripe-prefix. '
2013-01-16 17:52:52 +01:00
if ripe . digest ( ) [ : 2 ] == ' \x00 \x00 ' :
address = encodeAddress ( 2 , self . streamNumber , ripe . digest ( ) [ 2 : ] )
elif ripe . digest ( ) [ : 1 ] == ' \x00 ' :
address = encodeAddress ( 2 , self . streamNumber , ripe . digest ( ) [ 1 : ] )
2013-01-18 23:38:09 +01:00
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Finished generating address. Writing to keys.dat')
2013-01-16 17:52:52 +01:00
#An excellent way for us to store our keys is in Wallet Import Format. Let us convert now.
#https://en.bitcoin.it/wiki/Wallet_import_format
privSigningKey = ' \x80 ' + potentialPrivSigningKey
checksum = hashlib . sha256 ( hashlib . sha256 ( privSigningKey ) . digest ( ) ) . digest ( ) [ 0 : 4 ]
privSigningKeyWIF = arithmetic . changebase ( privSigningKey + checksum , 256 , 58 )
2013-01-24 22:07:24 +01:00
#print 'privSigningKeyWIF',privSigningKeyWIF
2013-01-16 17:52:52 +01:00
privEncryptionKey = ' \x80 ' + potentialPrivEncryptionKey
checksum = hashlib . sha256 ( hashlib . sha256 ( privEncryptionKey ) . digest ( ) ) . digest ( ) [ 0 : 4 ]
privEncryptionKeyWIF = arithmetic . changebase ( privEncryptionKey + checksum , 256 , 58 )
2013-01-24 22:07:24 +01:00
#print 'privEncryptionKeyWIF',privEncryptionKeyWIF
2013-01-16 17:52:52 +01:00
config . add_section ( address )
config . set ( address , ' label ' , self . label )
config . set ( address , ' enabled ' , ' true ' )
config . set ( address , ' decoy ' , ' false ' )
config . set ( address , ' privSigningKey ' , privSigningKeyWIF )
config . set ( address , ' privEncryptionKey ' , privEncryptionKeyWIF )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2013-03-19 18:32:37 +01:00
#It may be the case that this address is being generated as a result of a call to the API. Let us put the result in the necessary queue.
apiAddressGeneratorReturnQueue . put ( address )
2013-01-16 17:52:52 +01:00
2013-02-15 21:24:55 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , ' Done generating address. Doing work necessary to broadcast it... ' )
2013-01-16 17:52:52 +01:00
self . emit ( SIGNAL ( " writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . label , address , str ( self . streamNumber ) )
2013-01-18 23:38:09 +01:00
reloadMyAddressHashes ( )
2013-02-15 21:23:34 +01:00
workerQueue . put ( ( ' doPOWForMyV2Pubkey ' , address ) )
2013-01-16 17:52:52 +01:00
else : #There is something in the deterministicPassphrase variable thus we are going to do this deterministically.
2013-01-18 23:38:09 +01:00
statusbar = ' Generating ' + str ( self . numberOfAddressesToMake ) + ' new addresses. '
2013-01-16 17:52:52 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , statusbar )
2013-01-18 23:38:09 +01:00
signingKeyNonce = 0
encryptionKeyNonce = 1
2013-03-19 18:32:37 +01:00
listOfNewAddressesToSendOutThroughTheAPI = [ ] #We fill out this list no matter what although we only need it if we end up passing the info to the API.
2013-01-18 23:38:09 +01:00
for i in range ( self . numberOfAddressesToMake ) :
#This next section is a little bit strange. We're going to generate keys over and over until we
2013-02-12 21:00:04 +01:00
#find one that has a RIPEMD hash that starts with either \x00 or \x00\x00. Then when we pack them
#into a Bitmessage address, we won't store the \x00 or \x00\x00 bytes thus making the address shorter.
2013-01-21 01:00:46 +01:00
startTime = time . time ( )
numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix = 0
2013-01-18 23:38:09 +01:00
while True :
2013-01-21 02:17:39 +01:00
numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix + = 1
2013-01-18 23:38:09 +01:00
potentialPrivSigningKey = hashlib . sha512 ( self . deterministicPassphrase + encodeVarint ( signingKeyNonce ) ) . digest ( ) [ : 32 ]
potentialPrivEncryptionKey = hashlib . sha512 ( self . deterministicPassphrase + encodeVarint ( encryptionKeyNonce ) ) . digest ( ) [ : 32 ]
potentialPubSigningKey = self . pointMult ( potentialPrivSigningKey )
potentialPubEncryptionKey = self . pointMult ( potentialPrivEncryptionKey )
#print 'potentialPubSigningKey', potentialPubSigningKey.encode('hex')
#print 'potentialPubEncryptionKey', potentialPubEncryptionKey.encode('hex')
signingKeyNonce + = 2
encryptionKeyNonce + = 2
ripe = hashlib . new ( ' ripemd160 ' )
sha = hashlib . new ( ' sha512 ' )
sha . update ( potentialPubSigningKey + potentialPubEncryptionKey )
ripe . update ( sha . digest ( ) )
#print 'potential ripe.digest', ripe.digest().encode('hex')
if self . eighteenByteRipe :
if ripe . digest ( ) [ : 2 ] == ' \x00 \x00 ' :
break
else :
if ripe . digest ( ) [ : 1 ] == ' \x00 ' :
break
print ' ripe.digest ' , ripe . digest ( ) . encode ( ' hex ' )
2013-01-21 01:00:46 +01:00
print ' Address generator calculated ' , numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix , ' addresses at ' , numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix / ( time . time ( ) - startTime ) , ' keys per second. '
2013-01-18 23:38:09 +01:00
if ripe . digest ( ) [ : 2 ] == ' \x00 \x00 ' :
address = encodeAddress ( 2 , self . streamNumber , ripe . digest ( ) [ 2 : ] )
elif ripe . digest ( ) [ : 1 ] == ' \x00 ' :
address = encodeAddress ( 2 , self . streamNumber , ripe . digest ( ) [ 1 : ] )
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Finished generating address. Writing to keys.dat')
#An excellent way for us to store our keys is in Wallet Import Format. Let us convert now.
#https://en.bitcoin.it/wiki/Wallet_import_format
privSigningKey = ' \x80 ' + potentialPrivSigningKey
checksum = hashlib . sha256 ( hashlib . sha256 ( privSigningKey ) . digest ( ) ) . digest ( ) [ 0 : 4 ]
privSigningKeyWIF = arithmetic . changebase ( privSigningKey + checksum , 256 , 58 )
privEncryptionKey = ' \x80 ' + potentialPrivEncryptionKey
checksum = hashlib . sha256 ( hashlib . sha256 ( privEncryptionKey ) . digest ( ) ) . digest ( ) [ 0 : 4 ]
privEncryptionKeyWIF = arithmetic . changebase ( privEncryptionKey + checksum , 256 , 58 )
try :
config . add_section ( address )
print ' self.label ' , self . label
config . set ( address , ' label ' , self . label )
config . set ( address , ' enabled ' , ' true ' )
config . set ( address , ' decoy ' , ' false ' )
config . set ( address , ' privSigningKey ' , privSigningKeyWIF )
config . set ( address , ' privEncryptionKey ' , privEncryptionKeyWIF )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
self . emit ( SIGNAL ( " writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . label , address , str ( self . streamNumber ) )
2013-03-19 18:32:37 +01:00
listOfNewAddressesToSendOutThroughTheAPI . append ( address )
2013-01-18 23:38:09 +01:00
except :
print address , ' already exists. Not adding it again. '
2013-03-19 18:32:37 +01:00
#It may be the case that this address is being generated as a result of a call to the API. Let us put the result in the necessary queue.
apiAddressGeneratorReturnQueue . put ( listOfNewAddressesToSendOutThroughTheAPI )
2013-01-18 23:38:09 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , ' Done generating address ' )
reloadMyAddressHashes ( )
2013-02-18 21:22:48 +01:00
2013-02-04 22:49:02 +01:00
#This code which deals with old RSA addresses will soon be removed.
2013-03-01 23:44:52 +01:00
""" elif self.addressVersionNumber == 1:
2013-01-16 17:52:52 +01:00
statusbar = ' Generating new ' + str ( config . getint ( ' bitmessagesettings ' , ' bitstrength ' ) ) + ' bit RSA key. This takes a minute on average. If you want to generate multiple addresses now, you can; they will queue. '
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , statusbar )
( pubkey , privkey ) = rsa . newkeys ( config . getint ( ' bitmessagesettings ' , ' bitstrength ' ) )
print privkey [ ' n ' ]
print privkey [ ' e ' ]
print privkey [ ' d ' ]
print privkey [ ' p ' ]
print privkey [ ' q ' ]
sha = hashlib . new ( ' sha512 ' )
#sha.update(str(pubkey.n)+str(pubkey.e))
sha . update ( convertIntToString ( pubkey . n ) + convertIntToString ( pubkey . e ) )
ripe = hashlib . new ( ' ripemd160 ' )
ripe . update ( sha . digest ( ) )
address = encodeAddress ( 1 , self . streamNumber , ripe . digest ( ) )
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , ' Finished generating address. Writing to keys.dat ' )
config . add_section ( address )
config . set ( address , ' label ' , self . label )
config . set ( address , ' enabled ' , ' true ' )
config . set ( address , ' decoy ' , ' false ' )
config . set ( address , ' n ' , str ( privkey [ ' n ' ] ) )
config . set ( address , ' e ' , str ( privkey [ ' e ' ] ) )
config . set ( address , ' d ' , str ( privkey [ ' d ' ] ) )
config . set ( address , ' p ' , str ( privkey [ ' p ' ] ) )
config . set ( address , ' q ' , str ( privkey [ ' q ' ] ) )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2012-11-19 20:45:05 +01:00
2013-01-16 17:52:52 +01:00
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , ' Done generating address ' )
self . emit ( SIGNAL ( " writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . label , address , str ( self . streamNumber ) )
2013-03-01 23:44:52 +01:00
reloadMyAddressHashes ( ) """
2013-01-18 23:38:09 +01:00
2013-02-04 22:49:02 +01:00
#Does an EC point multiplication; turns a private key into a public key.
2013-01-16 17:52:52 +01:00
def pointMult ( self , secret ) :
2013-02-18 21:22:48 +01:00
#ctx = OpenSSL.BN_CTX_new() #This value proved to cause Seg Faults on Linux. It turns out that it really didn't speed up EC_POINT_mul anyway.
2013-01-16 17:52:52 +01:00
k = OpenSSL . EC_KEY_new_by_curve_name ( OpenSSL . get_curve ( ' secp256k1 ' ) )
priv_key = OpenSSL . BN_bin2bn ( secret , 32 , 0 )
group = OpenSSL . EC_KEY_get0_group ( k )
pub_key = OpenSSL . EC_POINT_new ( group )
2013-01-21 01:00:46 +01:00
OpenSSL . EC_POINT_mul ( group , pub_key , priv_key , None , None , None )
2013-01-16 17:52:52 +01:00
OpenSSL . EC_KEY_set_private_key ( k , priv_key )
OpenSSL . EC_KEY_set_public_key ( k , pub_key )
#print 'priv_key',priv_key
#print 'pub_key',pub_key
size = OpenSSL . i2o_ECPublicKey ( k , 0 )
mb = ctypes . create_string_buffer ( size )
OpenSSL . i2o_ECPublicKey ( k , ctypes . byref ( ctypes . pointer ( mb ) ) )
#print 'mb.raw', mb.raw.encode('hex'), 'length:', len(mb.raw)
#print 'mb.raw', mb.raw, 'length:', len(mb.raw)
OpenSSL . EC_POINT_free ( pub_key )
2013-01-21 01:00:46 +01:00
#OpenSSL.BN_CTX_free(ctx)
2013-01-16 17:52:52 +01:00
OpenSSL . BN_free ( priv_key )
OpenSSL . EC_KEY_free ( k )
return mb . raw
2012-11-19 20:45:05 +01:00
2013-03-19 18:32:37 +01:00
#This is one of several classes that constitute the API
2013-03-22 18:23:44 +01:00
#This class was written by Vaibhav Bhatia. Modified by Jonathan Warren (Atheros).
2013-03-19 18:32:37 +01:00
#http://code.activestate.com/recipes/501148-xmlrpc-serverclient-which-does-cookie-handling-and/
class MySimpleXMLRPCRequestHandler ( SimpleXMLRPCRequestHandler ) :
def do_POST ( self ) :
#Handles the HTTP POST request.
#Attempts to interpret all HTTP POST requests as XML-RPC calls,
#which are forwarded to the server's _dispatch method for handling.
#Note: this method is the same as in SimpleXMLRPCRequestHandler,
#just hacked to handle cookies
# Check that the path is legal
if not self . is_rpc_path_valid ( ) :
self . report_404 ( )
return
try :
# Get arguments by reading body of request.
# We read this in chunks to avoid straining
# socket.read(); around the 10 or 15Mb mark, some platforms
# begin to have problems (bug #792570).
max_chunk_size = 10 * 1024 * 1024
size_remaining = int ( self . headers [ " content-length " ] )
L = [ ]
while size_remaining :
chunk_size = min ( size_remaining , max_chunk_size )
L . append ( self . rfile . read ( chunk_size ) )
size_remaining - = len ( L [ - 1 ] )
data = ' ' . join ( L )
# In previous versions of SimpleXMLRPCServer, _dispatch
# could be overridden in this class, instead of in
# SimpleXMLRPCDispatcher. To maintain backwards compatibility,
# check to see if a subclass implements _dispatch and dispatch
# using that method if present.
response = self . server . _marshaled_dispatch (
data , getattr ( self , ' _dispatch ' , None )
)
except : # This should only happen if the module is buggy
# internal error, report as HTTP server error
self . send_response ( 500 )
self . end_headers ( )
else :
# got a valid XML RPC response
self . send_response ( 200 )
self . send_header ( " Content-type " , " text/xml " )
self . send_header ( " Content-length " , str ( len ( response ) ) )
# HACK :start -> sends cookies here
if self . cookies :
for cookie in self . cookies :
self . send_header ( ' Set-Cookie ' , cookie . output ( header = ' ' ) )
# HACK :end
self . end_headers ( )
self . wfile . write ( response )
# shut down the connection
self . wfile . flush ( )
self . connection . shutdown ( 1 )
def APIAuthenticateClient ( self ) :
if self . headers . has_key ( ' Authorization ' ) :
# handle Basic authentication
( enctype , encstr ) = self . headers . get ( ' Authorization ' ) . split ( )
2013-03-22 18:23:44 +01:00
( emailid , password ) = encstr . decode ( ' base64 ' ) . split ( ' : ' )
if emailid == config . get ( ' bitmessagesettings ' , ' apiusername ' ) and password == config . get ( ' bitmessagesettings ' , ' apipassword ' ) :
return True
else :
return False
2013-03-19 18:32:37 +01:00
else :
2013-03-22 18:23:44 +01:00
print ' Authentication failed because header lacks Authentication field '
2013-03-19 18:32:37 +01:00
time . sleep ( 2 )
2013-03-22 18:23:44 +01:00
return False
2013-03-19 18:32:37 +01:00
2013-03-22 18:23:44 +01:00
return False
2013-03-19 18:32:37 +01:00
def _dispatch ( self , method , params ) :
self . cookies = [ ]
validuser = self . APIAuthenticateClient ( )
if not validuser :
time . sleep ( 2 )
2013-03-22 18:23:44 +01:00
return " RPC Username or password incorrect or HTTP header lacks authentication at all. "
2013-03-19 18:32:37 +01:00
# handle request
if method == ' helloWorld ' :
( a , b ) = params
return a + ' - ' + b
elif method == ' add ' :
( a , b ) = params
return a + b
2013-03-19 19:39:24 +01:00
elif method == ' statusBar ' :
2013-03-19 18:32:37 +01:00
message , = params
apiSignalQueue . put ( ( ' updateStatusBar ' , message ) )
elif method == ' listAddresses ' :
data = ' { " addresses " :[ '
configSections = config . sections ( )
for addressInKeysFile in configSections :
if addressInKeysFile < > ' bitmessagesettings ' :
status , addressVersionNumber , streamNumber , hash = decodeAddress ( addressInKeysFile )
data
if len ( data ) > 20 :
data + = ' , '
data + = json . dumps ( { ' label ' : config . get ( addressInKeysFile , ' label ' ) , ' address ' : addressInKeysFile , ' stream ' : streamNumber , ' enabled ' : config . getboolean ( addressInKeysFile , ' enabled ' ) } , indent = 4 , separators = ( ' , ' , ' : ' ) )
data + = ' ]} '
return data
elif method == ' createRandomAddress ' :
if len ( params ) == 0 :
return ' API Error 0000: I need parameters! '
elif len ( params ) == 1 :
label , = params
eighteenByteRipe = False
elif len ( params ) == 2 :
label , eighteenByteRipe = params
2013-03-19 21:17:54 +01:00
label = label . decode ( ' base64 ' )
2013-03-19 18:32:37 +01:00
apiAddressGeneratorReturnQueue . queue . clear ( )
apiSignalQueue . put ( ( ' createRandomAddress ' , ( label , eighteenByteRipe ) ) ) #params should be a twopul which equals (eighteenByteRipe, label)
return apiAddressGeneratorReturnQueue . get ( )
elif method == ' createDeterministicAddresses ' :
if len ( params ) == 0 :
return ' API Error 0000: I need parameters! '
elif len ( params ) == 1 :
passphrase , = params
numberOfAddresses = 1
addressVersionNumber = 0
streamNumber = 0
eighteenByteRipe = False
elif len ( params ) == 2 :
passphrase , numberOfAddresses = params
addressVersionNumber = 0
streamNumber = 0
eighteenByteRipe = False
elif len ( params ) == 3 :
passphrase , numberOfAddresses , addressVersionNumber = params
streamNumber = 0
eighteenByteRipe = False
elif len ( params ) == 4 :
passphrase , numberOfAddresses , addressVersionNumber , streamNumber = params
eighteenByteRipe = False
elif len ( params ) == 5 :
passphrase , numberOfAddresses , addressVersionNumber , streamNumber , eighteenByteRipe = params
if len ( passphrase ) == 0 :
2013-03-19 20:12:19 +01:00
return ' API Error 0001: the specified passphrase is blank. '
2013-03-19 21:17:54 +01:00
passphrase = passphrase . decode ( ' base64 ' )
2013-03-19 18:32:37 +01:00
if addressVersionNumber == 0 : #0 means "just use the proper addressVersionNumber"
addressVersionNumber == 2
if addressVersionNumber != 2 :
2013-03-19 20:12:19 +01:00
return ' API Error 0002: the address version number currently must be 2 (or 0 which means auto-select). Others aren \' t supported. '
2013-03-19 18:32:37 +01:00
if streamNumber == 0 : #0 means "just use the most available stream"
streamNumber = 1
if streamNumber != 1 :
2013-03-19 20:12:19 +01:00
return ' API Error 0003: the stream number must be 1 (or 0 which means auto-select). Others aren \' t supported. '
2013-03-19 18:32:37 +01:00
if numberOfAddresses == 0 :
2013-03-19 20:12:19 +01:00
return ' API Error 0004: Why would you ask me to generate 0 addresses for you? '
2013-03-19 18:32:37 +01:00
if numberOfAddresses > 9999 :
2013-03-19 20:12:19 +01:00
return ' API Error 0005: You have (accidentially?) specified too many addresses to make. Maximum 9999. This check only exists to prevent mischief; if you really want to create more addresses than this, contact the Bitmessage developers and we can modify the check or you can do it yourself by searching the source code for this message. '
2013-03-19 18:32:37 +01:00
apiAddressGeneratorReturnQueue . queue . clear ( )
print ' about to send numberOfAddresses ' , numberOfAddresses
apiSignalQueue . put ( ( ' createDeterministicAddresses ' , ( passphrase , numberOfAddresses , addressVersionNumber , streamNumber , eighteenByteRipe ) ) )
data = ' { " addresses " :[ '
queueReturn = apiAddressGeneratorReturnQueue . get ( )
for item in queueReturn :
if len ( data ) > 20 :
data + = ' , '
data + = " \" " + item + " \" "
data + = ' ]} '
return data
elif method == ' getAllInboxMessages ' :
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' SELECT msgid, toaddress, fromaddress, subject, received, message FROM inbox where folder= ' inbox ' ORDER BY received ''' )
sqlSubmitQueue . put ( ' ' )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
data = ' { " inboxMessages " :[ '
for row in queryreturn :
msgid , toAddress , fromAddress , subject , received , message , = row
if len ( data ) > 25 :
data + = ' , '
data + = json . dumps ( { ' msgid ' : msgid . encode ( ' hex ' ) , ' toAddress ' : toAddress , ' fromAddress ' : fromAddress , ' subject ' : subject . encode ( ' base64 ' ) , ' message ' : message . encode ( ' base64 ' ) , ' encodingType ' : 2 , ' receivedTime ' : received } , indent = 4 , separators = ( ' , ' , ' : ' ) )
data + = ' ]} '
return data
elif method == ' trashMessage ' :
2013-03-19 20:12:19 +01:00
if len ( params ) == 0 :
return ' API Error 0000: I need parameters! '
2013-03-19 18:32:37 +01:00
msgid = params [ 0 ] . decode ( ' hex ' )
t = ( msgid , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' UPDATE inbox SET folder= ' trash ' WHERE msgid=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
apiSignalQueue . put ( ( ' updateStatusBar ' , ' Per API: Trashed message (assuming message existed). UI not updated. ' ) )
return ' Trashed message (assuming message existed). UI not updated. To double check, run getAllInboxMessages to see that the message disappeared, or restart Bitmessage and look in the normal Bitmessage GUI. '
elif method == ' sendMessage ' :
2013-03-19 20:12:19 +01:00
if len ( params ) == 0 :
return ' API Error 0000: I need parameters! '
elif len ( params ) == 4 :
2013-03-19 18:32:37 +01:00
toAddress , fromAddress , subject , message = params
encodingType = 2
2013-03-19 20:12:19 +01:00
elif len ( params ) == 5 :
2013-03-19 18:32:37 +01:00
toAddress , fromAddress , subject , message , encodingType = params
if encodingType != 2 :
2013-03-19 20:12:19 +01:00
return ' API Error 0006: The encoding type must be 2 because that is the only one this program currently supports. '
2013-03-19 18:32:37 +01:00
subject = subject . decode ( ' base64 ' )
message = message . decode ( ' base64 ' )
status , addressVersionNumber , streamNumber , toRipe = decodeAddress ( toAddress )
if status < > ' success ' :
printLock . acquire ( )
2013-03-19 20:12:19 +01:00
print ' API Error 0007: Could not decode address: ' , toAddress , ' : ' , status
2013-03-19 18:32:37 +01:00
printLock . release ( )
if status == ' checksumfailed ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0008: Checksum failed for address: ' + toAddress
2013-03-19 18:32:37 +01:00
if status == ' invalidcharacters ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0009: Invalid characters in address: ' + toAddress
2013-03-19 18:32:37 +01:00
if status == ' versiontoohigh ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0010: Address version number too high (or zero) in address: ' + toAddress
2013-03-19 18:32:37 +01:00
if addressVersionNumber != 2 :
2013-03-19 20:12:19 +01:00
return ' API Error 0011: the address version number currently must be 2. Others aren \' t supported. Check the toAddress. '
2013-03-19 18:32:37 +01:00
if streamNumber != 1 :
2013-03-19 20:12:19 +01:00
return ' API Error 0012: the stream number must be 1. Others aren \' t supported. Check the toAddress. '
2013-03-19 18:32:37 +01:00
status , addressVersionNumber , streamNumber , fromRipe = decodeAddress ( fromAddress )
if status < > ' success ' :
printLock . acquire ( )
2013-03-19 20:12:19 +01:00
print ' API Error 0007: Could not decode address: ' , fromAddress , ' : ' , status
2013-03-19 18:32:37 +01:00
printLock . release ( )
if status == ' checksumfailed ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0008: Checksum failed for address: ' + fromAddress
2013-03-19 18:32:37 +01:00
if status == ' invalidcharacters ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0009: Invalid characters in address: ' + fromAddress
2013-03-19 18:32:37 +01:00
if status == ' versiontoohigh ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0010: Address version number too high (or zero) in address: ' + fromAddress
2013-03-19 18:32:37 +01:00
if addressVersionNumber != 2 :
2013-03-19 20:12:19 +01:00
return ' API Error 0011: the address version number currently must be 2. Others aren \' t supported. Check the fromAddress. '
2013-03-19 18:32:37 +01:00
if streamNumber != 1 :
2013-03-19 20:12:19 +01:00
return ' API Error 0012: the stream number must be 1. Others aren \' t supported. Check the fromAddress. '
2013-03-19 18:32:37 +01:00
toAddress = addBMIfNotPresent ( toAddress )
fromAddress = addBMIfNotPresent ( fromAddress )
try :
fromAddressEnabled = config . getboolean ( fromAddress , ' enabled ' )
except :
2013-03-19 20:12:19 +01:00
return ' API Error 0013: could not find your fromAddress in the keys.dat file. '
2013-03-19 18:32:37 +01:00
if not fromAddressEnabled :
2013-03-19 20:12:19 +01:00
return ' API Error 0014: your fromAddress is disabled. Cannot send. '
2013-03-19 18:32:37 +01:00
ackdata = OpenSSL . rand ( 32 )
sqlLock . acquire ( )
t = ( ' ' , toAddress , toRipe , fromAddress , subject , message , ackdata , int ( time . time ( ) ) , ' findingpubkey ' , 1 , 1 , ' sent ' )
sqlSubmitQueue . put ( ''' INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
toLabel = ' '
t = ( toAddress , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' select label from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn < > [ ] :
for row in queryreturn :
toLabel , = row
apiSignalQueue . put ( ( ' displayNewSentMessage ' , ( toAddress , toLabel , fromAddress , subject , message , ackdata ) ) )
workerQueue . put ( ( ' sendmessage ' , toAddress ) )
return ackdata . encode ( ' hex ' )
elif method == ' sendBroadcast ' :
2013-03-19 20:12:19 +01:00
if len ( params ) == 0 :
return ' API Error 0000: I need parameters! '
2013-03-19 18:32:37 +01:00
if len ( params ) == 3 :
fromAddress , subject , message = params
encodingType = 2
2013-03-19 20:12:19 +01:00
elif len ( params ) == 4 :
2013-03-19 18:32:37 +01:00
fromAddress , subject , message , encodingType = params
if encodingType != 2 :
2013-03-19 20:12:19 +01:00
return ' API Error 0006: The encoding type must be 2 because that is the only one this program currently supports. '
2013-03-19 18:32:37 +01:00
subject = subject . decode ( ' base64 ' )
message = message . decode ( ' base64 ' )
status , addressVersionNumber , streamNumber , fromRipe = decodeAddress ( fromAddress )
if status < > ' success ' :
printLock . acquire ( )
2013-03-19 20:12:19 +01:00
print ' API Error 0007: Could not decode address: ' , fromAddress , ' : ' , status
2013-03-19 18:32:37 +01:00
printLock . release ( )
if status == ' checksumfailed ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0008: Checksum failed for address: ' + fromAddress
2013-03-19 18:32:37 +01:00
if status == ' invalidcharacters ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0009: Invalid characters in address: ' + fromAddress
2013-03-19 18:32:37 +01:00
if status == ' versiontoohigh ' :
2013-03-19 20:12:19 +01:00
return ' API Error 0010: Address version number too high (or zero) in address: ' + fromAddress
2013-03-19 18:32:37 +01:00
if addressVersionNumber != 2 :
2013-03-19 20:12:19 +01:00
return ' API Error 0011: the address version number currently must be 2. Others aren \' t supported. Check the fromAddress. '
2013-03-19 18:32:37 +01:00
if streamNumber != 1 :
2013-03-19 20:12:19 +01:00
return ' API Error 0012: the stream number must be 1. Others aren \' t supported. Check the fromAddress. '
2013-03-19 18:32:37 +01:00
fromAddress = addBMIfNotPresent ( fromAddress )
try :
fromAddressEnabled = config . getboolean ( fromAddress , ' enabled ' )
except :
2013-03-19 20:12:19 +01:00
return ' API Error 0013: could not find your fromAddress in the keys.dat file. '
2013-03-19 18:32:37 +01:00
ackdata = OpenSSL . rand ( 32 )
toAddress = ' [Broadcast subscribers] '
ripe = ' '
sqlLock . acquire ( )
t = ( ' ' , toAddress , ripe , fromAddress , subject , message , ackdata , int ( time . time ( ) ) , ' broadcastpending ' , 1 , 1 , ' sent ' )
sqlSubmitQueue . put ( ''' INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
toLabel = ' [Broadcast subscribers] '
apiSignalQueue . put ( ( ' displayNewSentMessage ' , ( toAddress , toLabel , fromAddress , subject , message , ackdata ) ) )
workerQueue . put ( ( ' sendbroadcast ' , ( fromAddress , subject , message ) ) )
return ackdata . encode ( ' hex ' )
else :
return ' Invalid Method: %s ' % method
#This thread, of which there is only one, runs the API.
class singleAPI ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
def run ( self ) :
se = SimpleXMLRPCServer ( ( config . get ( ' bitmessagesettings ' , ' apiinterface ' ) , config . getint ( ' bitmessagesettings ' , ' apiport ' ) ) , MySimpleXMLRPCRequestHandler , True , True )
se . register_introspection_functions ( )
se . serve_forever ( )
#The MySimpleXMLRPCRequestHandler class cannot emit signals (or at least I don't know how) because it is not a QT thread. It therefore puts data in a queue which this thread monitors and emits the signals on its behalf.
class singleAPISignalHandler ( QThread ) :
def __init__ ( self , parent = None ) :
QThread . __init__ ( self , parent )
def run ( self ) :
while True :
command , data = apiSignalQueue . get ( )
if command == ' updateStatusBar ' :
self . emit ( SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , data )
elif command == ' createRandomAddress ' :
label , eighteenByteRipe = data
streamNumberForAddress = 1
self . addressGenerator = addressGenerator ( )
self . addressGenerator . setup ( 2 , streamNumberForAddress , label , 1 , " " , eighteenByteRipe )
self . emit ( SIGNAL ( " passAddressGeneratorObjectThrough(PyQt_PyObject) " ) , self . addressGenerator )
self . addressGenerator . start ( )
elif command == ' createDeterministicAddresses ' :
passphrase , numberOfAddresses , addressVersionNumber , streamNumber , eighteenByteRipe = data
self . addressGenerator = addressGenerator ( )
self . addressGenerator . setup ( addressVersionNumber , streamNumber , ' unused API address ' , numberOfAddresses , passphrase , eighteenByteRipe )
self . emit ( SIGNAL ( " passAddressGeneratorObjectThrough(PyQt_PyObject) " ) , self . addressGenerator )
self . addressGenerator . start ( )
elif command == ' displayNewSentMessage ' :
toAddress , toLabel , fromAddress , subject , message , ackdata = data
self . emit ( SIGNAL ( " displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , toAddress , toLabel , fromAddress , subject , message , ackdata )
2012-11-19 20:45:05 +01:00
class iconGlossaryDialog ( QtGui . QDialog ) :
def __init__ ( self , parent ) :
QtGui . QWidget . __init__ ( self , parent )
self . ui = Ui_iconGlossaryDialog ( )
2013-02-18 21:22:48 +01:00
self . ui . setupUi ( self )
2012-11-19 20:45:05 +01:00
self . parent = parent
self . ui . labelPortNumber . setText ( ' You are using TCP port ' + str ( config . getint ( ' bitmessagesettings ' , ' port ' ) ) + ' . (This can be changed in the settings). ' )
2012-12-18 22:36:37 +01:00
QtGui . QWidget . resize ( self , QtGui . QWidget . sizeHint ( self ) )
2012-11-19 20:45:05 +01:00
class helpDialog ( QtGui . QDialog ) :
def __init__ ( self , parent ) :
QtGui . QWidget . __init__ ( self , parent )
self . ui = Ui_helpDialog ( )
self . ui . setupUi ( self )
self . parent = parent
self . ui . labelHelpURI . setOpenExternalLinks ( True )
2012-12-21 16:55:29 +01:00
QtGui . QWidget . resize ( self , QtGui . QWidget . sizeHint ( self ) )
2012-11-19 20:45:05 +01:00
class aboutDialog ( QtGui . QDialog ) :
def __init__ ( self , parent ) :
QtGui . QWidget . __init__ ( self , parent )
self . ui = Ui_aboutDialog ( )
self . ui . setupUi ( self )
self . parent = parent
self . ui . labelVersion . setText ( ' version ' + softwareVersion )
2013-01-24 22:07:24 +01:00
class regenerateAddressesDialog ( QtGui . QDialog ) :
def __init__ ( self , parent ) :
QtGui . QWidget . __init__ ( self , parent )
self . ui = Ui_regenerateAddressesDialog ( )
self . ui . setupUi ( self )
self . parent = parent
QtGui . QWidget . resize ( self , QtGui . QWidget . sizeHint ( self ) )
2012-11-19 20:45:05 +01:00
class settingsDialog ( QtGui . QDialog ) :
def __init__ ( self , parent ) :
QtGui . QWidget . __init__ ( self , parent )
self . ui = Ui_settingsDialog ( )
self . ui . setupUi ( self )
self . parent = parent
self . ui . checkBoxStartOnLogon . setChecked ( config . getboolean ( ' bitmessagesettings ' , ' startonlogon ' ) )
self . ui . checkBoxMinimizeToTray . setChecked ( config . getboolean ( ' bitmessagesettings ' , ' minimizetotray ' ) )
self . ui . checkBoxShowTrayNotifications . setChecked ( config . getboolean ( ' bitmessagesettings ' , ' showtraynotifications ' ) )
self . ui . checkBoxStartInTray . setChecked ( config . getboolean ( ' bitmessagesettings ' , ' startintray ' ) )
2013-02-26 21:07:51 +01:00
if appdata == ' ' :
self . ui . checkBoxPortableMode . setChecked ( True )
2012-11-19 20:45:05 +01:00
if ' darwin ' in sys . platform :
self . ui . checkBoxStartOnLogon . setDisabled ( True )
2012-12-18 19:09:10 +01:00
self . ui . checkBoxMinimizeToTray . setDisabled ( True )
self . ui . checkBoxShowTrayNotifications . setDisabled ( True )
self . ui . checkBoxStartInTray . setDisabled ( True )
self . ui . labelSettingsNote . setText ( ' Options have been disabled because they either arn \' t applicable or because they haven \' t yet been implimented for your operating system. ' )
2012-11-19 20:45:05 +01:00
elif ' linux ' in sys . platform :
2012-12-18 19:09:10 +01:00
self . ui . checkBoxStartOnLogon . setDisabled ( True )
2013-01-21 02:17:39 +01:00
self . ui . checkBoxMinimizeToTray . setDisabled ( True )
self . ui . checkBoxStartInTray . setDisabled ( True )
2012-12-18 19:09:10 +01:00
self . ui . labelSettingsNote . setText ( ' Options have been disabled because they either arn \' t applicable or because they haven \' t yet been implimented for your operating system. ' )
#On the Network settings tab:
self . ui . lineEditTCPPort . setText ( str ( config . get ( ' bitmessagesettings ' , ' port ' ) ) )
self . ui . checkBoxAuthentication . setChecked ( config . getboolean ( ' bitmessagesettings ' , ' socksauthentication ' ) )
if str ( config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) ) == ' none ' :
self . ui . comboBoxProxyType . setCurrentIndex ( 0 )
self . ui . lineEditSocksHostname . setEnabled ( False )
self . ui . lineEditSocksPort . setEnabled ( False )
self . ui . lineEditSocksUsername . setEnabled ( False )
self . ui . lineEditSocksPassword . setEnabled ( False )
self . ui . checkBoxAuthentication . setEnabled ( False )
elif str ( config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) ) == ' SOCKS4a ' :
self . ui . comboBoxProxyType . setCurrentIndex ( 1 )
self . ui . lineEditTCPPort . setEnabled ( False )
elif str ( config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) ) == ' SOCKS5 ' :
self . ui . comboBoxProxyType . setCurrentIndex ( 2 )
self . ui . lineEditTCPPort . setEnabled ( False )
2013-02-18 21:22:48 +01:00
2012-12-18 19:09:10 +01:00
self . ui . lineEditSocksHostname . setText ( str ( config . get ( ' bitmessagesettings ' , ' sockshostname ' ) ) )
self . ui . lineEditSocksPort . setText ( str ( config . get ( ' bitmessagesettings ' , ' socksport ' ) ) )
self . ui . lineEditSocksUsername . setText ( str ( config . get ( ' bitmessagesettings ' , ' socksusername ' ) ) )
self . ui . lineEditSocksPassword . setText ( str ( config . get ( ' bitmessagesettings ' , ' sockspassword ' ) ) )
QtCore . QObject . connect ( self . ui . comboBoxProxyType , QtCore . SIGNAL ( " currentIndexChanged(int) " ) , self . comboBoxProxyTypeChanged )
2013-02-27 06:11:04 +01:00
QtGui . QWidget . resize ( self , QtGui . QWidget . sizeHint ( self ) )
2012-12-18 19:09:10 +01:00
def comboBoxProxyTypeChanged ( self , comboBoxIndex ) :
if comboBoxIndex == 0 :
self . ui . lineEditSocksHostname . setEnabled ( False )
self . ui . lineEditSocksPort . setEnabled ( False )
self . ui . lineEditSocksUsername . setEnabled ( False )
self . ui . lineEditSocksPassword . setEnabled ( False )
self . ui . checkBoxAuthentication . setEnabled ( False )
self . ui . lineEditTCPPort . setEnabled ( True )
elif comboBoxIndex == 1 or comboBoxIndex == 2 :
self . ui . lineEditSocksHostname . setEnabled ( True )
self . ui . lineEditSocksPort . setEnabled ( True )
self . ui . checkBoxAuthentication . setEnabled ( True )
if self . ui . checkBoxAuthentication . isChecked ( ) :
self . ui . lineEditSocksUsername . setEnabled ( True )
self . ui . lineEditSocksPassword . setEnabled ( True )
self . ui . lineEditTCPPort . setEnabled ( False )
2012-11-19 20:45:05 +01:00
2013-02-26 00:09:32 +01:00
class SpecialAddressBehaviorDialog ( QtGui . QDialog ) :
def __init__ ( self , parent ) :
QtGui . QWidget . __init__ ( self , parent )
self . ui = Ui_SpecialAddressBehaviorDialog ( )
self . ui . setupUi ( self )
self . parent = parent
currentRow = parent . ui . tableWidgetYourIdentities . currentRow ( )
addressAtCurrentRow = str ( parent . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . text ( ) )
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( addressAtCurrentRow , ' mailinglist ' ) :
2013-02-26 00:09:32 +01:00
self . ui . radioButtonBehaviorMailingList . click ( )
else :
self . ui . radioButtonBehaveNormalAddress . click ( )
try :
mailingListName = config . get ( addressAtCurrentRow , ' mailinglistname ' )
except :
mailingListName = ' '
self . ui . lineEditMailingListName . setText ( unicode ( mailingListName , ' utf-8 ' ) )
QtGui . QWidget . resize ( self , QtGui . QWidget . sizeHint ( self ) )
2012-11-19 20:45:05 +01:00
class NewSubscriptionDialog ( QtGui . QDialog ) :
def __init__ ( self , parent ) :
QtGui . QWidget . __init__ ( self , parent )
2013-02-26 00:09:32 +01:00
self . ui = Ui_NewSubscriptionDialog ( )
self . ui . setupUi ( self )
2012-11-19 20:45:05 +01:00
self . parent = parent
QtCore . QObject . connect ( self . ui . lineEditSubscriptionAddress , QtCore . SIGNAL ( " textChanged(QString) " ) , self . subscriptionAddressChanged )
def subscriptionAddressChanged ( self , QString ) :
status , a , b , c = decodeAddress ( str ( QString ) )
if status == ' missingbm ' :
self . ui . labelSubscriptionAddressCheck . setText ( ' The address should start with ' ' BM- ' ' ' )
elif status == ' checksumfailed ' :
self . ui . labelSubscriptionAddressCheck . setText ( ' The address is not typed or copied correctly (the checksum failed). ' )
elif status == ' versiontoohigh ' :
self . ui . labelSubscriptionAddressCheck . setText ( ' The version number of this address is higher than this software can support. Please upgrade Bitmessage. ' )
elif status == ' invalidcharacters ' :
self . ui . labelSubscriptionAddressCheck . setText ( ' The address contains invalid characters. ' )
elif status == ' success ' :
self . ui . labelSubscriptionAddressCheck . setText ( ' Address is valid. ' )
class NewAddressDialog ( QtGui . QDialog ) :
def __init__ ( self , parent ) :
QtGui . QWidget . __init__ ( self , parent )
2013-02-18 21:22:48 +01:00
self . ui = Ui_NewAddressDialog ( )
self . ui . setupUi ( self )
2012-11-19 20:45:05 +01:00
self . parent = parent
row = 1
2013-02-04 22:49:02 +01:00
#Let's fill out the 'existing address' combo box with addresses from the 'Your Identities' tab.
2012-11-19 20:45:05 +01:00
while self . parent . ui . tableWidgetYourIdentities . item ( row - 1 , 1 ) :
self . ui . radioButtonExisting . click ( )
#print self.parent.ui.tableWidgetYourIdentities.item(row-1,1).text()
self . ui . comboBoxExisting . addItem ( self . parent . ui . tableWidgetYourIdentities . item ( row - 1 , 1 ) . text ( ) )
row + = 1
2013-01-16 17:52:52 +01:00
self . ui . groupBoxDeterministic . setHidden ( True )
QtGui . QWidget . resize ( self , QtGui . QWidget . sizeHint ( self ) )
2012-11-19 20:45:05 +01:00
class MyForm ( QtGui . QMainWindow ) :
def __init__ ( self , parent = None ) :
QtGui . QWidget . __init__ ( self , parent )
2013-01-18 23:38:09 +01:00
self . ui = Ui_MainWindow ( )
self . ui . setupUi ( self )
2012-11-19 20:45:05 +01:00
2013-01-18 23:38:09 +01:00
#Ask the user if we may delete their old version 1 addresses if they have any.
configSections = config . sections ( )
for addressInKeysFile in configSections :
if addressInKeysFile < > ' bitmessagesettings ' :
status , addressVersionNumber , streamNumber , hash = decodeAddress ( addressInKeysFile )
if addressVersionNumber == 1 :
displayMsg = " One of your addresses, " + addressInKeysFile + " , is an old version 1 address. Version 1 addresses are no longer supported. May we delete it now? "
reply = QtGui . QMessageBox . question ( self , ' Message ' , displayMsg , QtGui . QMessageBox . Yes , QtGui . QMessageBox . No )
if reply == QtGui . QMessageBox . Yes :
config . remove_section ( addressInKeysFile )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
#Configure Bitmessage to start on startup (or remove the configuration) based on the setting in the keys.dat file
2012-12-18 19:09:10 +01:00
if ' win32 ' in sys . platform or ' win64 ' in sys . platform :
2012-11-19 20:45:05 +01:00
#Auto-startup for Windows
RUN_PATH = " HKEY_CURRENT_USER \\ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Run "
self . settings = QSettings ( RUN_PATH , QSettings . NativeFormat )
self . settings . remove ( " PyBitmessage " ) #In case the user moves the program and the registry entry is no longer valid, this will delete the old registry entry.
if config . getboolean ( ' bitmessagesettings ' , ' startonlogon ' ) :
self . settings . setValue ( " PyBitmessage " , sys . argv [ 0 ] )
elif ' darwin ' in sys . platform :
#startup for mac
pass
elif ' linux ' in sys . platform :
#startup for linux
pass
self . trayIcon = QtGui . QSystemTrayIcon ( self )
2012-11-26 01:36:29 +01:00
self . trayIcon . setIcon ( QtGui . QIcon ( ' :/newPrefix/images/can-icon-16px.png ' ) )
2012-11-19 20:45:05 +01:00
traySignal = " activated(QSystemTrayIcon::ActivationReason) "
QtCore . QObject . connect ( self . trayIcon , QtCore . SIGNAL ( traySignal ) , self . __icon_activated )
menu = QtGui . QMenu ( )
self . exitAction = menu . addAction ( " Exit " , self . close )
self . trayIcon . setContextMenu ( menu )
2012-12-18 19:09:10 +01:00
#I'm currently under the impression that Mac users have different expectations for the tray icon. They don't necessairly expect it to open the main window when clicked and they still expect a program showing a tray icon to also be in the dock.
if ' darwin ' in sys . platform :
self . trayIcon . show ( )
2012-11-19 20:45:05 +01:00
#FILE MENU and other buttons
QtCore . QObject . connect ( self . ui . actionExit , QtCore . SIGNAL ( " triggered() " ) , self . close )
QtCore . QObject . connect ( self . ui . actionManageKeys , QtCore . SIGNAL ( " triggered() " ) , self . click_actionManageKeys )
2013-01-24 22:07:24 +01:00
QtCore . QObject . connect ( self . ui . actionRegenerateDeterministicAddresses , QtCore . SIGNAL ( " triggered() " ) , self . click_actionRegenerateDeterministicAddresses )
2012-11-19 20:45:05 +01:00
QtCore . QObject . connect ( self . ui . pushButtonNewAddress , QtCore . SIGNAL ( " clicked() " ) , self . click_NewAddressDialog )
QtCore . QObject . connect ( self . ui . comboBoxSendFrom , QtCore . SIGNAL ( " activated(int) " ) , self . redrawLabelFrom )
QtCore . QObject . connect ( self . ui . pushButtonAddAddressBook , QtCore . SIGNAL ( " clicked() " ) , self . click_pushButtonAddAddressBook )
QtCore . QObject . connect ( self . ui . pushButtonAddSubscription , QtCore . SIGNAL ( " clicked() " ) , self . click_pushButtonAddSubscription )
QtCore . QObject . connect ( self . ui . pushButtonAddBlacklist , QtCore . SIGNAL ( " clicked() " ) , self . click_pushButtonAddBlacklist )
QtCore . QObject . connect ( self . ui . pushButtonSend , QtCore . SIGNAL ( " clicked() " ) , self . click_pushButtonSend )
QtCore . QObject . connect ( self . ui . pushButtonLoadFromAddressBook , QtCore . SIGNAL ( " clicked() " ) , self . click_pushButtonLoadFromAddressBook )
QtCore . QObject . connect ( self . ui . radioButtonBlacklist , QtCore . SIGNAL ( " clicked() " ) , self . click_radioButtonBlacklist )
QtCore . QObject . connect ( self . ui . radioButtonWhitelist , QtCore . SIGNAL ( " clicked() " ) , self . click_radioButtonWhitelist )
QtCore . QObject . connect ( self . ui . pushButtonStatusIcon , QtCore . SIGNAL ( " clicked() " ) , self . click_pushButtonStatusIcon )
QtCore . QObject . connect ( self . ui . actionSettings , QtCore . SIGNAL ( " triggered() " ) , self . click_actionSettings )
QtCore . QObject . connect ( self . ui . actionAbout , QtCore . SIGNAL ( " triggered() " ) , self . click_actionAbout )
QtCore . QObject . connect ( self . ui . actionHelp , QtCore . SIGNAL ( " triggered() " ) , self . click_actionHelp )
#Popup menu for the Inbox tab
self . ui . inboxContextMenuToolbar = QtGui . QToolBar ( )
# Actions
2012-11-29 18:37:07 +01:00
self . actionReply = self . ui . inboxContextMenuToolbar . addAction ( " Reply " , self . on_action_InboxReply )
self . actionAddSenderToAddressBook = self . ui . inboxContextMenuToolbar . addAction ( " Add sender to your Address Book " , self . on_action_InboxAddSenderToAddressBook )
2012-11-19 20:45:05 +01:00
self . actionTrashInboxMessage = self . ui . inboxContextMenuToolbar . addAction ( " Move to Trash " , self . on_action_InboxTrash )
self . ui . tableWidgetInbox . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu )
self . connect ( self . ui . tableWidgetInbox , QtCore . SIGNAL ( ' customContextMenuRequested(const QPoint&) ' ) , self . on_context_menuInbox )
self . popMenuInbox = QtGui . QMenu ( self )
self . popMenuInbox . addAction ( self . actionReply )
self . popMenuInbox . addAction ( self . actionAddSenderToAddressBook )
self . popMenuInbox . addSeparator ( )
self . popMenuInbox . addAction ( self . actionTrashInboxMessage )
#Popup menu for the Your Identities tab
self . ui . addressContextMenuToolbar = QtGui . QToolBar ( )
# Actions
2012-11-29 18:37:07 +01:00
self . actionNew = self . ui . addressContextMenuToolbar . addAction ( " New " , self . on_action_YourIdentitiesNew )
self . actionEnable = self . ui . addressContextMenuToolbar . addAction ( " Enable " , self . on_action_YourIdentitiesEnable )
self . actionDisable = self . ui . addressContextMenuToolbar . addAction ( " Disable " , self . on_action_YourIdentitiesDisable )
self . actionClipboard = self . ui . addressContextMenuToolbar . addAction ( " Copy address to clipboard " , self . on_action_YourIdentitiesClipboard )
2013-02-26 00:09:32 +01:00
self . actionSpecialAddressBehavior = self . ui . addressContextMenuToolbar . addAction ( " Special address behavior... " , self . on_action_SpecialAddressBehaviorDialog )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetYourIdentities . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu )
self . connect ( self . ui . tableWidgetYourIdentities , QtCore . SIGNAL ( ' customContextMenuRequested(const QPoint&) ' ) , self . on_context_menuYourIdentities )
self . popMenu = QtGui . QMenu ( self )
self . popMenu . addAction ( self . actionNew )
self . popMenu . addSeparator ( )
self . popMenu . addAction ( self . actionClipboard )
self . popMenu . addSeparator ( )
self . popMenu . addAction ( self . actionEnable )
self . popMenu . addAction ( self . actionDisable )
2013-02-26 00:09:32 +01:00
self . popMenu . addAction ( self . actionSpecialAddressBehavior )
2012-11-19 20:45:05 +01:00
#Popup menu for the Address Book page
self . ui . addressBookContextMenuToolbar = QtGui . QToolBar ( )
# Actions
self . actionAddressBookSend = self . ui . addressBookContextMenuToolbar . addAction ( " Send message to this address " , self . on_action_AddressBookSend )
2013-02-19 13:42:42 +01:00
self . actionAddressBookClipboard = self . ui . addressBookContextMenuToolbar . addAction ( " Copy address to clipboard " , self . on_action_AddressBookClipboard )
self . actionAddressBookNew = self . ui . addressBookContextMenuToolbar . addAction ( " Add New Address " , self . on_action_AddressBookNew )
self . actionAddressBookDelete = self . ui . addressBookContextMenuToolbar . addAction ( " Delete " , self . on_action_AddressBookDelete )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetAddressBook . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu )
self . connect ( self . ui . tableWidgetAddressBook , QtCore . SIGNAL ( ' customContextMenuRequested(const QPoint&) ' ) , self . on_context_menuAddressBook )
self . popMenuAddressBook = QtGui . QMenu ( self )
self . popMenuAddressBook . addAction ( self . actionAddressBookSend )
self . popMenuAddressBook . addAction ( self . actionAddressBookClipboard )
2013-02-19 14:38:25 +01:00
self . popMenuAddressBook . addSeparator ( )
self . popMenuAddressBook . addAction ( self . actionAddressBookNew )
self . popMenuAddressBook . addAction ( self . actionAddressBookDelete )
2012-11-19 20:45:05 +01:00
#Popup menu for the Subscriptions page
self . ui . subscriptionsContextMenuToolbar = QtGui . QToolBar ( )
# Actions
self . actionsubscriptionsNew = self . ui . subscriptionsContextMenuToolbar . addAction ( " New " , self . on_action_SubscriptionsNew )
self . actionsubscriptionsDelete = self . ui . subscriptionsContextMenuToolbar . addAction ( " Delete " , self . on_action_SubscriptionsDelete )
self . actionsubscriptionsClipboard = self . ui . subscriptionsContextMenuToolbar . addAction ( " Copy address to clipboard " , self . on_action_SubscriptionsClipboard )
self . ui . tableWidgetSubscriptions . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu )
self . connect ( self . ui . tableWidgetSubscriptions , QtCore . SIGNAL ( ' customContextMenuRequested(const QPoint&) ' ) , self . on_context_menuSubscriptions )
self . popMenuSubscriptions = QtGui . QMenu ( self )
self . popMenuSubscriptions . addAction ( self . actionsubscriptionsNew )
self . popMenuSubscriptions . addAction ( self . actionsubscriptionsDelete )
self . popMenuSubscriptions . addSeparator ( )
self . popMenuSubscriptions . addAction ( self . actionsubscriptionsClipboard )
2013-01-31 19:35:36 +01:00
#Popup menu for the Sent page
self . ui . sentContextMenuToolbar = QtGui . QToolBar ( )
# Actions
self . actionTrashSentMessage = self . ui . sentContextMenuToolbar . addAction ( " Move to Trash " , self . on_action_SentTrash )
2013-02-26 00:09:32 +01:00
self . actionSentClipboard = self . ui . sentContextMenuToolbar . addAction ( " Copy destination address to clipboard " , self . on_action_SentClipboard )
2013-01-31 19:35:36 +01:00
self . ui . tableWidgetSent . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu )
self . connect ( self . ui . tableWidgetSent , QtCore . SIGNAL ( ' customContextMenuRequested(const QPoint&) ' ) , self . on_context_menuSent )
self . popMenuSent = QtGui . QMenu ( self )
2013-02-26 00:09:32 +01:00
self . popMenuSent . addAction ( self . actionSentClipboard )
2013-01-31 19:35:36 +01:00
self . popMenuSent . addAction ( self . actionTrashSentMessage )
2012-11-19 20:45:05 +01:00
2013-02-26 00:09:32 +01:00
#Popup menu for the Blacklist page
self . ui . blacklistContextMenuToolbar = QtGui . QToolBar ( )
# Actions
self . actionBlacklistNew = self . ui . blacklistContextMenuToolbar . addAction ( " Add new entry " , self . on_action_BlacklistNew )
self . actionBlacklistDelete = self . ui . blacklistContextMenuToolbar . addAction ( " Delete " , self . on_action_BlacklistDelete )
self . actionBlacklistClipboard = self . ui . blacklistContextMenuToolbar . addAction ( " Copy address to clipboard " , self . on_action_BlacklistClipboard )
self . actionBlacklistEnable = self . ui . blacklistContextMenuToolbar . addAction ( " Enable " , self . on_action_BlacklistEnable )
self . actionBlacklistDisable = self . ui . blacklistContextMenuToolbar . addAction ( " Disable " , self . on_action_BlacklistDisable )
self . ui . tableWidgetBlacklist . setContextMenuPolicy ( QtCore . Qt . CustomContextMenu )
self . connect ( self . ui . tableWidgetBlacklist , QtCore . SIGNAL ( ' customContextMenuRequested(const QPoint&) ' ) , self . on_context_menuBlacklist )
self . popMenuBlacklist = QtGui . QMenu ( self )
#self.popMenuBlacklist.addAction( self.actionBlacklistNew )
self . popMenuBlacklist . addAction ( self . actionBlacklistDelete )
self . popMenuBlacklist . addSeparator ( )
self . popMenuBlacklist . addAction ( self . actionBlacklistClipboard )
self . popMenuBlacklist . addSeparator ( )
self . popMenuBlacklist . addAction ( self . actionBlacklistEnable )
self . popMenuBlacklist . addAction ( self . actionBlacklistDisable )
2012-11-19 20:45:05 +01:00
#Initialize the user's list of addresses on the 'Your Identities' tab.
configSections = config . sections ( )
for addressInKeysFile in configSections :
if addressInKeysFile < > ' bitmessagesettings ' :
isEnabled = config . getboolean ( addressInKeysFile , ' enabled ' )
newItem = QtGui . QTableWidgetItem ( unicode ( config . get ( addressInKeysFile , ' label ' ) , ' utf-8) ' ) )
if not isEnabled :
newItem . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
self . ui . tableWidgetYourIdentities . insertRow ( 0 )
self . ui . tableWidgetYourIdentities . setItem ( 0 , 0 , newItem )
newItem = QtGui . QTableWidgetItem ( addressInKeysFile )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
if not isEnabled :
newItem . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( addressInKeysFile , ' mailinglist ' ) :
newItem . setTextColor ( QtGui . QColor ( 137 , 04 , 177 ) ) #magenta
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetYourIdentities . setItem ( 0 , 1 , newItem )
newItem = QtGui . QTableWidgetItem ( str ( addressStream ( addressInKeysFile ) ) )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
if not isEnabled :
newItem . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
self . ui . tableWidgetYourIdentities . setItem ( 0 , 2 , newItem )
if isEnabled :
status , addressVersionNumber , streamNumber , hash = decodeAddress ( addressInKeysFile )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
self . sqlLookup = sqlThread ( )
self . sqlLookup . start ( )
2013-01-18 23:38:09 +01:00
reloadMyAddressHashes ( )
2012-11-19 20:45:05 +01:00
self . reloadBroadcastSendersForWhichImWatching ( )
#Load inbox from messages database file
sqlSubmitQueue . put ( ''' SELECT msgid, toaddress, fromaddress, subject, received, message FROM inbox where folder= ' inbox ' ORDER BY received ''' )
sqlSubmitQueue . put ( ' ' )
queryreturn = sqlReturnQueue . get ( )
for row in queryreturn :
msgid , toAddress , fromAddress , subject , received , message , = row
try :
if toAddress == ' [Broadcast subscribers] ' :
toLabel = ' [Broadcast subscribers] '
else :
toLabel = config . get ( toAddress , ' label ' )
except :
toLabel = ' '
if toLabel == ' ' :
toLabel = toAddress
fromLabel = ' '
t = ( fromAddress , )
sqlSubmitQueue . put ( ''' select label from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
if queryreturn < > [ ] :
for row in queryreturn :
fromLabel , = row
self . ui . tableWidgetInbox . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( unicode ( toLabel , ' utf-8 ' ) )
2012-11-29 18:37:07 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2012-11-19 20:45:05 +01:00
newItem . setData ( Qt . UserRole , str ( toAddress ) )
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( toAddress , ' mailinglist ' ) :
newItem . setTextColor ( QtGui . QColor ( 137 , 04 , 177 ) )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetInbox . setItem ( 0 , 0 , newItem )
if fromLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( fromAddress , ' utf-8 ' ) )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( fromLabel , ' utf-8 ' ) )
2012-11-29 18:37:07 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2012-11-19 20:45:05 +01:00
newItem . setData ( Qt . UserRole , str ( fromAddress ) )
2013-02-27 18:03:21 +01:00
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetInbox . setItem ( 0 , 1 , newItem )
newItem = QtGui . QTableWidgetItem ( unicode ( subject , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , unicode ( message , ' utf-8) ' ) )
2012-11-29 18:37:07 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetInbox . setItem ( 0 , 2 , newItem )
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( received ) ) ) )
2012-11-19 20:45:05 +01:00
newItem . setData ( Qt . UserRole , QByteArray ( msgid ) )
2012-12-10 20:48:00 +01:00
newItem . setData ( 33 , int ( received ) )
2012-11-29 18:37:07 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetInbox . setItem ( 0 , 3 , newItem )
#self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(0,2).data(Qt.UserRole).toPyObject())
#Load Sent items from database
2013-01-31 19:35:36 +01:00
sqlSubmitQueue . put ( ''' SELECT toaddress, fromaddress, subject, message, status, ackdata, lastactiontime FROM sent where folder = ' sent ' ORDER BY lastactiontime ''' )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( ' ' )
queryreturn = sqlReturnQueue . get ( )
for row in queryreturn :
toAddress , fromAddress , subject , message , status , ackdata , lastactiontime = row
try :
fromLabel = config . get ( fromAddress , ' label ' )
except :
fromLabel = ' '
if fromLabel == ' ' :
fromLabel = fromAddress
toLabel = ' '
t = ( toAddress , )
sqlSubmitQueue . put ( ''' select label from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
if queryreturn < > [ ] :
for row in queryreturn :
toLabel , = row
self . ui . tableWidgetSent . insertRow ( 0 )
if toLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( toAddress , ' utf-8 ' ) )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( toLabel , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , str ( toAddress ) )
2012-11-29 18:37:07 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetSent . setItem ( 0 , 0 , newItem )
if fromLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( fromAddress , ' utf-8 ' ) )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( fromLabel , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , str ( fromAddress ) )
2012-11-29 18:37:07 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetSent . setItem ( 0 , 1 , newItem )
newItem = QtGui . QTableWidgetItem ( unicode ( subject , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , unicode ( message , ' utf-8) ' ) )
2012-11-29 18:37:07 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetSent . setItem ( 0 , 2 , newItem )
if status == ' findingpubkey ' :
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( ' Waiting on their public key. Will request it again soon. ' )
2012-11-19 20:45:05 +01:00
elif status == ' sentmessage ' :
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( ' Message sent. Waiting on acknowledgement. Sent at ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( lastactiontime ) ) )
2012-11-19 20:45:05 +01:00
elif status == ' doingpow ' :
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( ' Need to do work to send message. Work is queued. ' )
2012-11-19 20:45:05 +01:00
elif status == ' ackreceived ' :
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( ' Acknowledgement of the message received ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( lastactiontime ) ) ) )
2012-11-19 20:45:05 +01:00
elif status == ' broadcastpending ' :
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( ' Doing the work necessary to send broadcast... ' )
2012-11-19 20:45:05 +01:00
elif status == ' broadcastsent ' :
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( ' Broadcast on ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( lastactiontime ) ) ) )
2012-11-19 20:45:05 +01:00
else :
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( ' Unknown status. ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( lastactiontime ) ) ) )
2013-01-31 19:35:36 +01:00
newItem . setData ( Qt . UserRole , QByteArray ( ackdata ) )
2012-12-10 20:48:00 +01:00
newItem . setData ( 33 , int ( lastactiontime ) )
2012-11-29 18:37:07 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetSent . setItem ( 0 , 3 , newItem )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
#Initialize the address book
sqlSubmitQueue . put ( ' SELECT * FROM addressbook ' )
sqlSubmitQueue . put ( ' ' )
queryreturn = sqlReturnQueue . get ( )
for row in queryreturn :
label , address = row
self . ui . tableWidgetAddressBook . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( unicode ( label , ' utf-8 ' ) )
self . ui . tableWidgetAddressBook . setItem ( 0 , 0 , newItem )
newItem = QtGui . QTableWidgetItem ( address )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetAddressBook . setItem ( 0 , 1 , newItem )
#Initialize the Subscriptions
sqlSubmitQueue . put ( ' SELECT label, address FROM subscriptions ' )
sqlSubmitQueue . put ( ' ' )
queryreturn = sqlReturnQueue . get ( )
for row in queryreturn :
label , address = row
self . ui . tableWidgetSubscriptions . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( unicode ( label , ' utf-8 ' ) )
self . ui . tableWidgetSubscriptions . setItem ( 0 , 0 , newItem )
newItem = QtGui . QTableWidgetItem ( address )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetSubscriptions . setItem ( 0 , 1 , newItem )
#Initialize the Blacklist or Whitelist
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' :
self . loadBlackWhiteList ( )
else :
self . ui . tabWidget . setTabText ( 6 , ' Whitelist ' )
self . ui . radioButtonWhitelist . click ( )
self . loadBlackWhiteList ( )
#Initialize the ackdataForWhichImWatching data structure using data from the sql database.
sqlSubmitQueue . put ( ''' SELECT ackdata FROM sent where (status= ' sentmessage ' OR status= ' doingpow ' ) ''' )
sqlSubmitQueue . put ( ' ' )
queryreturn = sqlReturnQueue . get ( )
for row in queryreturn :
ackdata , = row
2013-01-16 17:52:52 +01:00
print ' Watching for ackdata ' , ackdata . encode ( ' hex ' )
2012-11-19 20:45:05 +01:00
ackdataForWhichImWatching [ ackdata ] = 0
QtCore . QObject . connect ( self . ui . tableWidgetYourIdentities , QtCore . SIGNAL ( " itemChanged(QTableWidgetItem *) " ) , self . tableWidgetYourIdentitiesItemChanged )
QtCore . QObject . connect ( self . ui . tableWidgetAddressBook , QtCore . SIGNAL ( " itemChanged(QTableWidgetItem *) " ) , self . tableWidgetAddressBookItemChanged )
QtCore . QObject . connect ( self . ui . tableWidgetSubscriptions , QtCore . SIGNAL ( " itemChanged(QTableWidgetItem *) " ) , self . tableWidgetSubscriptionsItemChanged )
2012-12-05 18:47:30 +01:00
QtCore . QObject . connect ( self . ui . tableWidgetInbox , QtCore . SIGNAL ( " itemSelectionChanged () " ) , self . tableWidgetInboxItemClicked )
QtCore . QObject . connect ( self . ui . tableWidgetSent , QtCore . SIGNAL ( " itemSelectionChanged () " ) , self . tableWidgetSentItemClicked )
2012-11-19 20:45:05 +01:00
#Put the colored icon on the status bar
#self.ui.pushButtonStatusIcon.setIcon(QIcon(":/newPrefix/images/yellowicon.png"))
self . statusbar = self . statusBar ( )
self . statusbar . insertPermanentWidget ( 0 , self . ui . pushButtonStatusIcon )
self . ui . labelStartupTime . setText ( ' Since startup on ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( time . time ( ) ) ) ) )
self . numberOfMessagesProcessed = 0
self . numberOfBroadcastsProcessed = 0
self . numberOfPubkeysProcessed = 0
#Below this point, it would be good if all of the necessary global data structures were initialized.
self . rerenderComboBoxSendFrom ( )
2012-11-29 11:39:39 +01:00
2012-11-19 20:45:05 +01:00
self . listOfOutgoingSynSenderThreads = [ ] #if we don't maintain this list, the threads will get garbage-collected.
self . connectToStream ( 1 )
self . singleListenerThread = singleListener ( )
self . singleListenerThread . start ( )
QtCore . QObject . connect ( self . singleListenerThread , QtCore . SIGNAL ( " passObjectThrough(PyQt_PyObject) " ) , self . connectObjectToSignals )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
self . singleCleanerThread = singleCleaner ( )
self . singleCleanerThread . start ( )
QtCore . QObject . connect ( self . singleCleanerThread , QtCore . SIGNAL ( " updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject) " ) , self . updateSentItemStatusByHash )
2013-02-18 21:01:47 +01:00
QtCore . QObject . connect ( self . singleCleanerThread , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
2012-11-19 20:45:05 +01:00
self . workerThread = singleWorker ( )
self . workerThread . start ( )
QtCore . QObject . connect ( self . workerThread , QtCore . SIGNAL ( " updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject) " ) , self . updateSentItemStatusByHash )
QtCore . QObject . connect ( self . workerThread , QtCore . SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , self . updateSentItemStatusByAckdata )
2013-02-15 21:23:34 +01:00
QtCore . QObject . connect ( self . workerThread , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
2012-11-19 20:45:05 +01:00
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( ' bitmessagesettings ' , ' apienabled ' ) :
try :
apiNotifyPath = config . get ( ' bitmessagesettings ' , ' apinotifypath ' )
except :
apiNotifyPath = ' '
if apiNotifyPath != ' ' :
printLock . acquire ( )
print ' Trying to call ' , apiNotifyPath
printLock . release ( )
call ( [ apiNotifyPath , " startingUp " ] )
2013-03-22 17:34:12 +01:00
self . singleAPIThread = singleAPI ( )
self . singleAPIThread . start ( )
2013-03-19 18:32:37 +01:00
self . singleAPISignalHandlerThread = singleAPISignalHandler ( )
self . singleAPISignalHandlerThread . start ( )
QtCore . QObject . connect ( self . singleAPISignalHandlerThread , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
QtCore . QObject . connect ( self . singleAPISignalHandlerThread , QtCore . SIGNAL ( " passAddressGeneratorObjectThrough(PyQt_PyObject) " ) , self . connectObjectToAddressGeneratorSignals )
QtCore . QObject . connect ( self . singleAPISignalHandlerThread , QtCore . SIGNAL ( " displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . displayNewSentMessage )
2012-11-19 20:45:05 +01:00
def click_actionManageKeys ( self ) :
2012-12-18 19:09:10 +01:00
if ' darwin ' in sys . platform or ' linux ' in sys . platform :
2013-02-26 21:07:51 +01:00
if appdata == ' ' :
reply = QtGui . QMessageBox . information ( self , ' keys.dat? ' , ' You may manage your keys by editing the keys.dat file stored in the same directory as this program. It is important that you back up this file. ' , QMessageBox . Ok )
else :
QtGui . QMessageBox . information ( self , ' keys.dat? ' , ' You may manage your keys by editing the keys.dat file stored in \n ' + appdata + ' \n It is important that you back up this file. ' , QMessageBox . Ok )
2012-12-18 19:09:10 +01:00
elif sys . platform == ' win32 ' or sys . platform == ' win64 ' :
2013-02-26 21:07:51 +01:00
if appdata == ' ' :
reply = QtGui . QMessageBox . question ( self , ' Open keys.dat? ' , ' You may manage your keys by editing the keys.dat file stored in the same directory as this program. It 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.) ' , QtGui . QMessageBox . Yes , QtGui . QMessageBox . No )
else :
reply = QtGui . QMessageBox . question ( self , ' Open keys.dat? ' , ' You may manage your keys by editing the keys.dat file stored in \n ' + appdata + ' \n It 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.) ' , QtGui . QMessageBox . Yes , QtGui . QMessageBox . No )
2012-12-18 19:09:10 +01:00
if reply == QtGui . QMessageBox . Yes :
self . openKeysFile ( )
2013-02-18 21:22:48 +01:00
2013-01-24 22:07:24 +01:00
def click_actionRegenerateDeterministicAddresses ( self ) :
self . regenerateAddressesDialogInstance = regenerateAddressesDialog ( self )
if self . regenerateAddressesDialogInstance . exec_ ( ) :
if self . regenerateAddressesDialogInstance . ui . lineEditPassphrase . text ( ) == " " :
QMessageBox . about ( self , " bad passphrase " , " You must type your passphrase. If you don \' t have one then this is not the form for you. " )
else :
streamNumberForAddress = int ( self . regenerateAddressesDialogInstance . ui . lineEditStreamNumber . text ( ) )
addressVersionNumber = int ( self . regenerateAddressesDialogInstance . ui . lineEditAddressVersionNumber . text ( ) )
self . addressGenerator = addressGenerator ( )
self . addressGenerator . setup ( addressVersionNumber , streamNumberForAddress , " unused address " , self . regenerateAddressesDialogInstance . ui . spinBoxNumberOfAddressesToMake . value ( ) , self . regenerateAddressesDialogInstance . ui . lineEditPassphrase . text ( ) . toUtf8 ( ) , self . regenerateAddressesDialogInstance . ui . checkBoxEighteenByteRipe . isChecked ( ) )
QtCore . QObject . connect ( self . addressGenerator , SIGNAL ( " writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . writeNewAddressToTable )
QtCore . QObject . connect ( self . addressGenerator , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
self . addressGenerator . start ( )
self . ui . tabWidget . setCurrentIndex ( 3 )
2012-11-19 20:45:05 +01:00
def openKeysFile ( self ) :
if ' linux ' in sys . platform :
2013-02-26 21:07:51 +01:00
subprocess . call ( [ " xdg-open " , appdata + ' keys.dat ' ] )
2012-11-19 20:45:05 +01:00
else :
2013-02-26 21:07:51 +01:00
os . startfile ( appdata + ' keys.dat ' )
2012-11-19 20:45:05 +01:00
def changeEvent ( self , event ) :
2012-12-18 19:09:10 +01:00
if config . getboolean ( ' bitmessagesettings ' , ' minimizetotray ' ) and not ' darwin ' in sys . platform :
2012-11-19 20:45:05 +01:00
if event . type ( ) == QtCore . QEvent . WindowStateChange :
if self . windowState ( ) & QtCore . Qt . WindowMinimized :
self . hide ( )
self . trayIcon . show ( )
#self.hidden = True
2012-12-18 19:09:10 +01:00
if ' win32 ' in sys . platform or ' win64 ' in sys . platform :
2012-11-19 20:45:05 +01:00
self . setWindowFlags ( Qt . ToolTip )
elif event . oldState ( ) & QtCore . Qt . WindowMinimized :
#The window state has just been changed to Normal/Maximised/FullScreen
pass
#QtGui.QWidget.changeEvent(self, event)
def __icon_activated ( self , reason ) :
if reason == QtGui . QSystemTrayIcon . Trigger :
2012-12-18 19:09:10 +01:00
if ' linux ' in sys . platform :
self . trayIcon . hide ( )
self . setWindowFlags ( Qt . Window )
self . show ( )
elif ' win32 ' in sys . platform or ' win64 ' in sys . platform :
self . trayIcon . hide ( )
self . setWindowFlags ( Qt . Window )
self . show ( )
2012-11-19 20:45:05 +01:00
self . setWindowState ( self . windowState ( ) & ~ QtCore . Qt . WindowMinimized | QtCore . Qt . WindowActive )
self . activateWindow ( )
2012-12-18 19:09:10 +01:00
elif ' darwin ' in sys . platform :
#self.trayIcon.hide() #this line causes a segmentation fault
#self.setWindowFlags(Qt.Window)
#self.show()
self . setWindowState ( self . windowState ( ) & ~ QtCore . Qt . WindowMinimized | QtCore . Qt . WindowActive )
self . activateWindow ( )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def incrementNumberOfMessagesProcessed ( self ) :
self . numberOfMessagesProcessed + = 1
self . ui . labelMessageCount . setText ( ' Processed ' + str ( self . numberOfMessagesProcessed ) + ' person-to-person messages. ' )
def incrementNumberOfBroadcastsProcessed ( self ) :
self . numberOfBroadcastsProcessed + = 1
self . ui . labelBroadcastCount . setText ( ' Processed ' + str ( self . numberOfBroadcastsProcessed ) + ' broadcast messages. ' )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def incrementNumberOfPubkeysProcessed ( self ) :
self . numberOfPubkeysProcessed + = 1
self . ui . labelPubkeyCount . setText ( ' Processed ' + str ( self . numberOfPubkeysProcessed ) + ' public keys. ' )
def updateNetworkStatusTab ( self , streamNumber , connectionCount ) :
global statusIconColor
2012-11-23 09:22:56 +01:00
#print 'updating network status tab'
2012-11-19 20:45:05 +01:00
totalNumberOfConnectionsFromAllStreams = 0 #One would think we could use len(sendDataQueues) for this, but sendData threads don't remove themselves from sendDataQueues fast enough for len(sendDataQueues) to be accurate here.
for currentRow in range ( self . ui . tableWidgetConnectionCount . rowCount ( ) ) :
rowStreamNumber = int ( self . ui . tableWidgetConnectionCount . item ( currentRow , 0 ) . text ( ) )
if streamNumber == rowStreamNumber :
self . ui . tableWidgetConnectionCount . item ( currentRow , 1 ) . setText ( str ( connectionCount ) )
totalNumberOfConnectionsFromAllStreams + = connectionCount
self . ui . labelTotalConnections . setText ( ' Total Connections: ' + str ( totalNumberOfConnectionsFromAllStreams ) )
if totalNumberOfConnectionsFromAllStreams > 0 and statusIconColor == ' red ' : #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.
self . setStatusIcon ( ' yellow ' )
elif totalNumberOfConnectionsFromAllStreams == 0 :
self . setStatusIcon ( ' red ' )
def setStatusIcon ( self , color ) :
global statusIconColor
2012-11-23 09:22:56 +01:00
#print 'setting status icon color'
2012-11-19 20:45:05 +01:00
if color == ' red ' :
self . ui . pushButtonStatusIcon . setIcon ( QIcon ( " :/newPrefix/images/redicon.png " ) )
statusIconColor = ' red '
if color == ' yellow ' :
2012-12-10 21:16:42 +01:00
if self . statusBar ( ) . currentMessage ( ) == ' Warning: You are currently not connected. Bitmessage will do the work necessary to send the message but it won \' t send until you connect. ' :
self . statusBar ( ) . showMessage ( ' ' )
2012-11-19 20:45:05 +01:00
self . ui . pushButtonStatusIcon . setIcon ( QIcon ( " :/newPrefix/images/yellowicon.png " ) )
statusIconColor = ' yellow '
if color == ' green ' :
2012-12-10 21:16:42 +01:00
if self . statusBar ( ) . currentMessage ( ) == ' Warning: You are currently not connected. Bitmessage will do the work necessary to send the message but it won \' t send until you connect. ' :
self . statusBar ( ) . showMessage ( ' ' )
2012-11-19 20:45:05 +01:00
self . ui . pushButtonStatusIcon . setIcon ( QIcon ( " :/newPrefix/images/greenicon.png " ) )
statusIconColor = ' green '
def updateSentItemStatusByHash ( self , toRipe , textToDisplay ) :
for i in range ( self . ui . tableWidgetSent . rowCount ( ) ) :
toAddress = str ( self . ui . tableWidgetSent . item ( i , 0 ) . data ( Qt . UserRole ) . toPyObject ( ) )
status , addressVersionNumber , streamNumber , ripe = decodeAddress ( toAddress )
if ripe == toRipe :
self . ui . tableWidgetSent . item ( i , 3 ) . setText ( unicode ( textToDisplay , ' utf-8 ' ) )
def updateSentItemStatusByAckdata ( self , ackdata , textToDisplay ) :
for i in range ( self . ui . tableWidgetSent . rowCount ( ) ) :
toAddress = str ( self . ui . tableWidgetSent . item ( i , 0 ) . data ( Qt . UserRole ) . toPyObject ( ) )
tableAckdata = self . ui . tableWidgetSent . item ( i , 3 ) . data ( Qt . UserRole ) . toPyObject ( )
status , addressVersionNumber , streamNumber , ripe = decodeAddress ( toAddress )
if ackdata == tableAckdata :
self . ui . tableWidgetSent . item ( i , 3 ) . setText ( unicode ( textToDisplay , ' utf-8 ' ) )
def rerenderInboxFromLabels ( self ) :
for i in range ( self . ui . tableWidgetInbox . rowCount ( ) ) :
addressToLookup = str ( self . ui . tableWidgetInbox . item ( i , 1 ) . data ( Qt . UserRole ) . toPyObject ( ) )
fromLabel = ' '
t = ( addressToLookup , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' select label from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn < > [ ] :
for row in queryreturn :
fromLabel , = row
self . ui . tableWidgetInbox . item ( i , 1 ) . setText ( unicode ( fromLabel , ' utf-8 ' ) )
else :
#It might be a broadcast message. We should check for that label.
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' select label from subscriptions where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn < > [ ] :
for row in queryreturn :
fromLabel , = row
self . ui . tableWidgetInbox . item ( i , 1 ) . setText ( unicode ( fromLabel , ' utf-8 ' ) )
def rerenderInboxToLabels ( self ) :
for i in range ( self . ui . tableWidgetInbox . rowCount ( ) ) :
toAddress = str ( self . ui . tableWidgetInbox . item ( i , 0 ) . data ( Qt . UserRole ) . toPyObject ( ) )
try :
toLabel = config . get ( toAddress , ' label ' )
except :
toLabel = ' '
if toLabel == ' ' :
toLabel = toAddress
self . ui . tableWidgetInbox . item ( i , 0 ) . setText ( unicode ( toLabel , ' utf-8 ' ) )
2013-02-27 18:03:21 +01:00
#Set the color according to whether it is the address of a mailing list or not.
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( toAddress , ' mailinglist ' ) :
self . ui . tableWidgetInbox . item ( i , 0 ) . setTextColor ( QtGui . QColor ( 137 , 04 , 177 ) )
else :
2013-02-27 18:03:21 +01:00
self . ui . tableWidgetInbox . item ( i , 0 ) . setTextColor ( QtGui . QColor ( 0 , 0 , 0 ) )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def rerenderSentFromLabels ( self ) :
for i in range ( self . ui . tableWidgetSent . rowCount ( ) ) :
fromAddress = str ( self . ui . tableWidgetSent . item ( i , 1 ) . data ( Qt . UserRole ) . toPyObject ( ) )
try :
fromLabel = config . get ( fromAddress , ' label ' )
except :
fromLabel = ' '
if fromLabel == ' ' :
fromLabel = fromAddress
self . ui . tableWidgetSent . item ( i , 1 ) . setText ( unicode ( fromLabel , ' utf-8 ' ) )
def rerenderSentToLabels ( self ) :
for i in range ( self . ui . tableWidgetSent . rowCount ( ) ) :
addressToLookup = str ( self . ui . tableWidgetSent . item ( i , 0 ) . data ( Qt . UserRole ) . toPyObject ( ) )
toLabel = ' '
t = ( addressToLookup , )
sqlSubmitQueue . put ( ''' select label from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
if queryreturn < > [ ] :
for row in queryreturn :
toLabel , = row
self . ui . tableWidgetSent . item ( i , 0 ) . setText ( unicode ( toLabel , ' utf-8 ' ) )
2013-01-18 23:38:09 +01:00
2012-11-19 20:45:05 +01:00
def click_pushButtonSend ( self ) :
self . statusBar ( ) . showMessage ( ' ' )
toAddresses = str ( self . ui . lineEditTo . text ( ) )
fromAddress = str ( self . ui . labelFrom . text ( ) )
subject = str ( self . ui . lineEditSubject . text ( ) . toUtf8 ( ) )
message = str ( self . ui . textEditMessage . document ( ) . toPlainText ( ) . toUtf8 ( ) )
if self . ui . radioButtonSpecific . isChecked ( ) : #To send a message to specific people (rather than broadcast)
toAddressesList = [ s . strip ( ) for s in toAddresses . replace ( ' , ' , ' ; ' ) . split ( ' ; ' ) ]
2012-12-19 20:49:40 +01:00
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.
2012-11-19 20:45:05 +01:00
for toAddress in toAddressesList :
if toAddress < > ' ' :
status , addressVersionNumber , streamNumber , ripe = decodeAddress ( toAddress )
if status < > ' success ' :
2013-01-18 23:38:09 +01:00
printLock . acquire ( )
2013-02-26 00:09:32 +01:00
print ' Error: Could not decode ' , toAddress , ' : ' , status
2013-01-18 23:38:09 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
if status == ' missingbm ' :
self . statusBar ( ) . showMessage ( ' Error: Bitmessage addresses start with BM- Please check ' + toAddress )
if status == ' checksumfailed ' :
self . statusBar ( ) . showMessage ( ' Error: The address ' + toAddress + ' is not typed or copied correctly. Please check it. ' )
if status == ' invalidcharacters ' :
self . statusBar ( ) . showMessage ( ' Error: The address ' + toAddress + ' contains invalid characters. Please check it. ' )
if status == ' versiontoohigh ' :
self . statusBar ( ) . showMessage ( ' Error: The address version in ' + toAddress + ' is too high. Either you need to upgrade your Bitmessage software or your acquaintance is being clever. ' )
2013-01-24 18:23:37 +01:00
elif fromAddress == ' ' :
2013-02-12 21:00:04 +01:00
self . statusBar ( ) . showMessage ( ' Error: You must specify a From address. If you don \' t have one, go to the \' Your Identities \' tab. ' )
2013-01-24 18:23:37 +01:00
else :
toAddress = addBMIfNotPresent ( toAddress )
2013-02-13 23:19:08 +01:00
try :
config . get ( toAddress , ' enabled ' )
2013-02-18 21:22:48 +01:00
#The toAddress is one owned by me. We cannot send messages to ourselves without significant changes to the codebase.
2013-02-13 23:19:08 +01:00
QMessageBox . about ( self , " Sending to your address " , " Error: One of the addresses to which you are sending a message, " + toAddress + " , is yours. Unfortunately the Bitmessage client cannot process its own messages. Please try running a second client on a different computer or within a VM. " )
continue
except :
pass
2013-01-24 18:19:24 +01:00
if addressVersionNumber > 2 or addressVersionNumber == 0 :
2013-01-24 18:25:48 +01:00
QMessageBox . about ( self , " Address version number " , " Concerning the address " + toAddress + " , Bitmessage cannot understand address version numbers of " + str ( addressVersionNumber ) + " . Perhaps upgrade Bitmessage to the latest version. " )
2013-01-24 18:19:24 +01:00
continue
if streamNumber > 1 or streamNumber == 0 :
2013-01-24 20:52:48 +01:00
QMessageBox . about ( self , " Stream number " , " Concerning the address " + toAddress + " , Bitmessage cannot handle stream numbers of " + str ( streamNumber ) + " . Perhaps upgrade Bitmessage to the latest version. " )
2013-01-24 18:19:24 +01:00
continue
2012-11-19 20:45:05 +01:00
self . statusBar ( ) . showMessage ( ' ' )
2013-01-24 17:38:12 +01:00
try :
if connectionsCount [ streamNumber ] == 0 :
self . statusBar ( ) . showMessage ( ' Warning: You are currently not connected. Bitmessage will do the work necessary to send the message but it won \' t send until you connect. ' )
except :
self . statusBar ( ) . showMessage ( ' Warning: The address uses a stream number currently not supported by this Bitmessage version. Perhaps upgrade. ' )
2013-01-22 20:29:49 +01:00
ackdata = OpenSSL . rand ( 32 )
2012-11-19 20:45:05 +01:00
sqlLock . acquire ( )
t = ( ' ' , toAddress , ripe , fromAddress , subject , message , ackdata , int ( time . time ( ) ) , ' findingpubkey ' , 1 , 1 , ' sent ' )
sqlSubmitQueue . put ( ''' INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2013-03-19 18:32:37 +01:00
""" try:
2012-11-19 20:45:05 +01:00
fromLabel = config . get ( fromAddress , ' label ' )
except :
fromLabel = ' '
if fromLabel == ' ' :
2013-03-19 18:32:37 +01:00
fromLabel = fromAddress """
2012-11-19 20:45:05 +01:00
toLabel = ' '
t = ( toAddress , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' select label from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn < > [ ] :
for row in queryreturn :
toLabel , = row
2013-03-19 18:32:37 +01:00
self . displayNewSentMessage ( toAddress , toLabel , fromAddress , subject , message , ackdata )
workerQueue . put ( ( ' sendmessage ' , toAddress ) )
2012-11-19 20:45:05 +01:00
2013-03-19 18:32:37 +01:00
""" self.ui.tableWidgetSent.insertRow(0)
2012-11-19 20:45:05 +01:00
if toLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( toAddress , ' utf-8 ' ) )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( toLabel , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , str ( toAddress ) )
self . ui . tableWidgetSent . setItem ( 0 , 0 , newItem )
if fromLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( fromAddress , ' utf-8 ' ) )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( fromLabel , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , str ( fromAddress ) )
self . ui . tableWidgetSent . setItem ( 0 , 1 , newItem )
newItem = QtGui . QTableWidgetItem ( unicode ( subject , ' utf-8) ' ) )
newItem . setData ( Qt . UserRole , unicode ( message , ' utf-8) ' ) )
self . ui . tableWidgetSent . setItem ( 0 , 2 , newItem )
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( ' Just pressed ' ' send ' ' ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( time . time ( ) ) ) ) )
2013-01-31 19:35:36 +01:00
newItem . setData ( Qt . UserRole , QByteArray ( ackdata ) )
2012-12-10 20:48:00 +01:00
newItem . setData ( 33 , int ( time . time ( ) ) )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetSent . setItem ( 0 , 3 , newItem )
2013-03-19 18:32:37 +01:00
self . ui . textEditSentMessage . setText ( self . ui . tableWidgetSent . item ( 0 , 2 ) . data ( Qt . UserRole ) . toPyObject ( ) ) """
2012-11-19 20:45:05 +01:00
2013-02-26 00:09:32 +01:00
self . ui . comboBoxSendFrom . setCurrentIndex ( 0 )
2012-11-19 20:45:05 +01:00
self . ui . labelFrom . setText ( ' ' )
2013-02-26 00:09:32 +01:00
self . ui . lineEditTo . setText ( ' ' )
self . ui . lineEditSubject . setText ( ' ' )
self . ui . textEditMessage . setText ( ' ' )
2012-11-19 20:45:05 +01:00
self . ui . tabWidget . setCurrentIndex ( 2 )
2013-02-04 22:49:02 +01:00
self . ui . tableWidgetSent . setCurrentCell ( 0 , 0 )
2012-11-19 20:45:05 +01:00
else :
self . statusBar ( ) . showMessage ( ' Your \' To \' field is empty. ' )
else : #User selected 'Broadcast'
if fromAddress == ' ' :
self . statusBar ( ) . showMessage ( ' Error: You must specify a From address. If you don \' t have one, go to the \' Your Identities \' tab. ' )
else :
self . statusBar ( ) . showMessage ( ' ' )
#We don't actually need the ackdata for acknowledgement since this is a broadcast message, but we can use it to update the user interface when the POW is done generating.
2013-01-24 20:52:48 +01:00
ackdata = OpenSSL . rand ( 32 )
2012-11-19 20:45:05 +01:00
toAddress = ' [Broadcast subscribers] '
ripe = ' '
sqlLock . acquire ( )
t = ( ' ' , toAddress , ripe , fromAddress , subject , message , ackdata , int ( time . time ( ) ) , ' broadcastpending ' , 1 , 1 , ' sent ' )
sqlSubmitQueue . put ( ''' INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
workerQueue . put ( ( ' sendbroadcast ' , ( fromAddress , subject , message ) ) )
try :
fromLabel = config . get ( fromAddress , ' label ' )
except :
fromLabel = ' '
if fromLabel == ' ' :
fromLabel = fromAddress
toLabel = ' [Broadcast subscribers] '
self . ui . tableWidgetSent . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( unicode ( toLabel , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , str ( toAddress ) )
self . ui . tableWidgetSent . setItem ( 0 , 0 , newItem )
if fromLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( fromAddress , ' utf-8 ' ) )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( fromLabel , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , str ( fromAddress ) )
self . ui . tableWidgetSent . setItem ( 0 , 1 , newItem )
newItem = QtGui . QTableWidgetItem ( unicode ( subject , ' utf-8) ' ) )
newItem . setData ( Qt . UserRole , unicode ( message , ' utf-8) ' ) )
self . ui . tableWidgetSent . setItem ( 0 , 2 , newItem )
#newItem = QtGui.QTableWidgetItem('Doing work necessary to send broadcast...'+strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
2013-03-19 18:32:37 +01:00
newItem = myTableWidgetItem ( ' Work is queued. ' )
2013-02-05 22:53:56 +01:00
newItem . setData ( Qt . UserRole , QByteArray ( ackdata ) )
newItem . setData ( 33 , int ( time . time ( ) ) )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetSent . setItem ( 0 , 3 , newItem )
self . ui . textEditSentMessage . setText ( self . ui . tableWidgetSent . item ( 0 , 2 ) . data ( Qt . UserRole ) . toPyObject ( ) )
2013-02-26 00:09:32 +01:00
self . ui . comboBoxSendFrom . setCurrentIndex ( 0 )
2012-11-19 20:45:05 +01:00
self . ui . labelFrom . setText ( ' ' )
2013-02-26 00:09:32 +01:00
self . ui . lineEditTo . setText ( ' ' )
self . ui . lineEditSubject . setText ( ' ' )
self . ui . textEditMessage . setText ( ' ' )
2012-11-19 20:45:05 +01:00
self . ui . tabWidget . setCurrentIndex ( 2 )
2013-02-04 22:49:02 +01:00
self . ui . tableWidgetSent . setCurrentCell ( 0 , 0 )
2012-11-19 20:45:05 +01:00
def click_pushButtonLoadFromAddressBook ( self ) :
self . ui . tabWidget . setCurrentIndex ( 5 )
for i in range ( 4 ) :
time . sleep ( 0.1 )
self . statusBar ( ) . showMessage ( ' ' )
time . sleep ( 0.1 )
self . statusBar ( ) . showMessage ( ' Right click an entry in your address book and select \' Send message to this address \' . ' )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def redrawLabelFrom ( self , index ) :
self . ui . labelFrom . setText ( self . ui . comboBoxSendFrom . itemData ( index ) . toPyObject ( ) )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def rerenderComboBoxSendFrom ( self ) :
self . ui . comboBoxSendFrom . clear ( )
self . ui . labelFrom . setText ( ' ' )
configSections = config . sections ( )
for addressInKeysFile in configSections :
if addressInKeysFile < > ' bitmessagesettings ' :
isEnabled = config . getboolean ( addressInKeysFile , ' enabled ' ) #I realize that this is poor programming practice but I don't care. It's easier for others to read.
if isEnabled :
self . ui . comboBoxSendFrom . insertItem ( 0 , unicode ( config . get ( addressInKeysFile , ' label ' ) , ' utf-8 ' ) , addressInKeysFile )
2012-12-04 18:11:14 +01:00
self . ui . comboBoxSendFrom . insertItem ( 0 , ' ' , ' ' )
if ( self . ui . comboBoxSendFrom . count ( ) == 2 ) :
self . ui . comboBoxSendFrom . setCurrentIndex ( 1 )
self . redrawLabelFrom ( self . ui . comboBoxSendFrom . currentIndex ( ) )
else :
self . ui . comboBoxSendFrom . setCurrentIndex ( 0 )
2012-11-19 20:45:05 +01:00
def connectToStream ( self , streamNumber ) :
connectionsCount [ streamNumber ] = 0
#Add a line to the Connection Count table on the Network Status tab with a 'zero' connection count. This will be updated as necessary by another function.
self . ui . tableWidgetConnectionCount . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( str ( streamNumber ) )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetConnectionCount . setItem ( 0 , 0 , newItem )
newItem = QtGui . QTableWidgetItem ( ' 0 ' )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetConnectionCount . setItem ( 0 , 1 , newItem )
a = outgoingSynSender ( )
self . listOfOutgoingSynSenderThreads . append ( a )
QtCore . QObject . connect ( a , QtCore . SIGNAL ( " passObjectThrough(PyQt_PyObject) " ) , self . connectObjectToSignals )
2012-12-18 19:09:10 +01:00
QtCore . QObject . connect ( a , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
2012-11-19 20:45:05 +01:00
a . setup ( streamNumber )
a . start ( )
def connectObjectToSignals ( self , object ) :
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
2013-02-26 00:09:32 +01:00
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . displayNewInboxMessage )
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . displayNewSentMessage )
2012-11-19 20:45:05 +01:00
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject) " ) , self . updateSentItemStatusByHash )
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject) " ) , self . updateSentItemStatusByAckdata )
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject) " ) , self . updateNetworkStatusTab )
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " incrementNumberOfMessagesProcessed() " ) , self . incrementNumberOfMessagesProcessed )
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " incrementNumberOfPubkeysProcessed() " ) , self . incrementNumberOfPubkeysProcessed )
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " incrementNumberOfBroadcastsProcessed() " ) , self . incrementNumberOfBroadcastsProcessed )
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " setStatusIcon(PyQt_PyObject) " ) , self . setStatusIcon )
2013-03-19 18:32:37 +01:00
#This function exists because of the API. The API thread starts an address generator thread and must somehow connect the address generator's signals to the QApplication thread. This function is used to connect the slots and signals.
def connectObjectToAddressGeneratorSignals ( self , object ) :
QtCore . QObject . connect ( object , SIGNAL ( " writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . writeNewAddressToTable )
QtCore . QObject . connect ( object , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
2013-02-26 00:09:32 +01:00
#This function is called by the processmsg function when that function 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 ) :
try :
fromLabel = config . get ( fromAddress , ' label ' )
except :
fromLabel = ' '
if fromLabel == ' ' :
fromLabel = fromAddress
self . ui . tableWidgetSent . insertRow ( 0 )
2013-03-19 18:32:37 +01:00
if toLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( toAddress , ' utf-8 ' ) )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( toLabel , ' utf-8 ' ) )
2013-02-26 00:09:32 +01:00
newItem . setData ( Qt . UserRole , str ( toAddress ) )
self . ui . tableWidgetSent . setItem ( 0 , 0 , newItem )
if fromLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( fromAddress , ' utf-8 ' ) )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( fromLabel , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , str ( fromAddress ) )
self . ui . tableWidgetSent . setItem ( 0 , 1 , newItem )
newItem = QtGui . QTableWidgetItem ( unicode ( subject , ' utf-8) ' ) )
newItem . setData ( Qt . UserRole , unicode ( message , ' utf-8) ' ) )
self . ui . tableWidgetSent . setItem ( 0 , 2 , newItem )
#newItem = QtGui.QTableWidgetItem('Doing work necessary to send broadcast...'+strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))))
2013-03-19 18:32:37 +01:00
newItem = myTableWidgetItem ( ' Work is queued. ' + strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( time . time ( ) ) ) ) )
2013-02-26 00:09:32 +01:00
newItem . setData ( Qt . UserRole , QByteArray ( ackdata ) )
newItem . setData ( 33 , int ( time . time ( ) ) )
self . ui . tableWidgetSent . setItem ( 0 , 3 , newItem )
self . ui . textEditSentMessage . setText ( self . ui . tableWidgetSent . item ( 0 , 2 ) . data ( Qt . UserRole ) . toPyObject ( ) )
def displayNewInboxMessage ( self , inventoryHash , toAddress , fromAddress , subject , message ) :
''' print ' test signals displayNewInboxMessage '
2012-11-19 20:45:05 +01:00
print ' toAddress ' , toAddress
print ' fromAddress ' , fromAddress
print ' message ' , message '''
fromLabel = ' '
sqlLock . acquire ( )
t = ( fromAddress , )
sqlSubmitQueue . put ( ''' select label from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn < > [ ] :
for row in queryreturn :
fromLabel , = row
else :
#There might be a label in the subscriptions table
sqlLock . acquire ( )
t = ( fromAddress , )
sqlSubmitQueue . put ( ''' select label from subscriptions where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn < > [ ] :
for row in queryreturn :
fromLabel , = row
try :
if toAddress == ' [Broadcast subscribers] ' :
toLabel = ' [Broadcast subscribers] '
else :
toLabel = config . get ( toAddress , ' label ' )
except :
toLabel = ' '
if toLabel == ' ' :
toLabel = toAddress
#msgid, toaddress, fromaddress, subject, received, message = row
newItem = QtGui . QTableWidgetItem ( unicode ( toLabel , ' utf-8 ' ) )
newItem . setData ( Qt . UserRole , str ( toAddress ) )
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( str ( toAddress ) , ' mailinglist ' ) :
newItem . setTextColor ( QtGui . QColor ( 137 , 04 , 177 ) )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetInbox . insertRow ( 0 )
self . ui . tableWidgetInbox . setItem ( 0 , 0 , newItem )
if fromLabel == ' ' :
newItem = QtGui . QTableWidgetItem ( unicode ( fromAddress , ' utf-8 ' ) )
if config . getboolean ( ' bitmessagesettings ' , ' showtraynotifications ' ) :
self . trayIcon . showMessage ( ' New Message ' , ' New message from ' + fromAddress , 1 , 2000 )
else :
newItem = QtGui . QTableWidgetItem ( unicode ( fromLabel , ' utf-8 ' ) )
if config . getboolean ( ' bitmessagesettings ' , ' showtraynotifications ' ) :
self . trayIcon . showMessage ( ' New Message ' , ' New message from ' + fromLabel , 1 , 2000 )
newItem . setData ( Qt . UserRole , str ( fromAddress ) )
self . ui . tableWidgetInbox . setItem ( 0 , 1 , newItem )
newItem = QtGui . QTableWidgetItem ( unicode ( subject , ' utf-8) ' ) )
newItem . setData ( Qt . UserRole , unicode ( message , ' utf-8) ' ) )
self . ui . tableWidgetInbox . setItem ( 0 , 2 , newItem )
2012-12-10 20:48:00 +01:00
newItem = myTableWidgetItem ( strftime ( config . get ( ' bitmessagesettings ' , ' timeformat ' ) , localtime ( int ( time . time ( ) ) ) ) )
2012-11-28 23:49:53 +01:00
newItem . setData ( Qt . UserRole , QByteArray ( inventoryHash ) )
2012-12-10 20:48:00 +01:00
newItem . setData ( 33 , int ( time . time ( ) ) )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetInbox . setItem ( 0 , 3 , newItem )
self . ui . textEditInboxMessage . setText ( self . ui . tableWidgetInbox . item ( 0 , 2 ) . data ( Qt . UserRole ) . toPyObject ( ) )
2013-02-04 22:49:02 +01:00
self . ui . tableWidgetInbox . setCurrentCell ( 0 , 0 )
2012-11-19 20:45:05 +01:00
def click_pushButtonAddAddressBook ( self ) :
self . NewSubscriptionDialogInstance = NewSubscriptionDialog ( self )
if self . NewSubscriptionDialogInstance . exec_ ( ) :
if self . NewSubscriptionDialogInstance . ui . labelSubscriptionAddressCheck . text ( ) == ' Address is valid. ' :
#First we must check to see if the address is already in the address book. The user cannot add it again or else it will cause problems when updating and deleting the entry.
sqlLock . acquire ( )
2012-12-19 20:49:40 +01:00
t = ( addBMIfNotPresent ( str ( self . NewSubscriptionDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) ) , )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( ''' select * from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn == [ ] :
self . ui . tableWidgetAddressBook . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( unicode ( self . NewSubscriptionDialogInstance . ui . newsubscriptionlabel . text ( ) . toUtf8 ( ) , ' utf-8 ' ) )
self . ui . tableWidgetAddressBook . setItem ( 0 , 0 , newItem )
2012-12-19 20:49:40 +01:00
newItem = QtGui . QTableWidgetItem ( addBMIfNotPresent ( self . NewSubscriptionDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) )
2012-11-19 20:45:05 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetAddressBook . setItem ( 0 , 1 , newItem )
2012-12-19 20:49:40 +01:00
t = ( str ( self . NewSubscriptionDialogInstance . ui . newsubscriptionlabel . text ( ) . toUtf8 ( ) ) , addBMIfNotPresent ( str ( self . NewSubscriptionDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) ) )
2012-11-19 20:45:05 +01:00
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' INSERT INTO addressbook VALUES (?,?) ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
self . rerenderInboxFromLabels ( )
else :
self . statusBar ( ) . showMessage ( ' Error: You cannot add the same address to your address book twice. Try renaming the existing one if you want. ' )
else :
self . statusBar ( ) . showMessage ( ' The address you entered was invalid. Ignoring it. ' )
def click_pushButtonAddSubscription ( self ) :
self . NewSubscriptionDialogInstance = NewSubscriptionDialog ( self )
if self . NewSubscriptionDialogInstance . exec_ ( ) :
if self . NewSubscriptionDialogInstance . ui . labelSubscriptionAddressCheck . text ( ) == ' Address is valid. ' :
#First we must check to see if the address is already in the address book. The user cannot add it again or else it will cause problems when updating and deleting the entry.
sqlLock . acquire ( )
2012-12-19 20:49:40 +01:00
t = ( addBMIfNotPresent ( str ( self . NewSubscriptionDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) ) , )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( ''' select * from subscriptions where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn == [ ] :
self . ui . tableWidgetSubscriptions . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( unicode ( self . NewSubscriptionDialogInstance . ui . newsubscriptionlabel . text ( ) . toUtf8 ( ) , ' utf-8 ' ) )
self . ui . tableWidgetSubscriptions . setItem ( 0 , 0 , newItem )
2012-12-19 20:49:40 +01:00
newItem = QtGui . QTableWidgetItem ( addBMIfNotPresent ( self . NewSubscriptionDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) )
2012-11-19 20:45:05 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetSubscriptions . setItem ( 0 , 1 , newItem )
2012-12-19 20:49:40 +01:00
t = ( str ( self . NewSubscriptionDialogInstance . ui . newsubscriptionlabel . text ( ) . toUtf8 ( ) ) , addBMIfNotPresent ( str ( self . NewSubscriptionDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) ) , True )
2012-11-19 20:45:05 +01:00
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' INSERT INTO subscriptions VALUES (?,?,?) ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
self . rerenderInboxFromLabels ( )
self . reloadBroadcastSendersForWhichImWatching ( )
else :
self . statusBar ( ) . showMessage ( ' Error: You cannot add the same address to your subsciptions twice. Perhaps rename the existing one if you want. ' )
else :
self . statusBar ( ) . showMessage ( ' The address you entered was invalid. Ignoring it. ' )
def loadBlackWhiteList ( self ) :
#Initialize the Blacklist or Whitelist table
listType = config . get ( ' bitmessagesettings ' , ' blackwhitelist ' )
if listType == ' black ' :
2013-02-26 00:09:32 +01:00
sqlSubmitQueue . put ( ''' SELECT label, address, enabled FROM blacklist ''' )
2012-11-19 20:45:05 +01:00
else :
2013-02-26 00:09:32 +01:00
sqlSubmitQueue . put ( ''' SELECT label, address, enabled FROM whitelist ''' )
2012-11-19 20:45:05 +01:00
sqlSubmitQueue . put ( ' ' )
queryreturn = sqlReturnQueue . get ( )
for row in queryreturn :
2013-02-26 00:09:32 +01:00
label , address , enabled = row
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetBlacklist . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( unicode ( label , ' utf-8 ' ) )
2013-02-26 00:09:32 +01:00
if not enabled :
newItem . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetBlacklist . setItem ( 0 , 0 , newItem )
newItem = QtGui . QTableWidgetItem ( address )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
2013-02-26 00:09:32 +01:00
if not enabled :
newItem . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetBlacklist . setItem ( 0 , 1 , newItem )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def click_pushButtonStatusIcon ( self ) :
print ' click_pushButtonStatusIcon '
self . iconGlossaryInstance = iconGlossaryDialog ( self )
if self . iconGlossaryInstance . exec_ ( ) :
pass
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def click_actionHelp ( self ) :
self . helpDialogInstance = helpDialog ( self )
self . helpDialogInstance . exec_ ( )
def click_actionAbout ( self ) :
self . aboutDialogInstance = aboutDialog ( self )
self . aboutDialogInstance . exec_ ( )
def click_actionSettings ( self ) :
2012-12-18 19:09:10 +01:00
global statusIconColor
2013-02-26 21:07:51 +01:00
global appdata
2012-11-19 20:45:05 +01:00
self . settingsDialogInstance = settingsDialog ( self )
if self . settingsDialogInstance . exec_ ( ) :
config . set ( ' bitmessagesettings ' , ' startonlogon ' , str ( self . settingsDialogInstance . ui . checkBoxStartOnLogon . isChecked ( ) ) )
config . set ( ' bitmessagesettings ' , ' minimizetotray ' , str ( self . settingsDialogInstance . ui . checkBoxMinimizeToTray . isChecked ( ) ) )
config . set ( ' bitmessagesettings ' , ' showtraynotifications ' , str ( self . settingsDialogInstance . ui . checkBoxShowTrayNotifications . isChecked ( ) ) )
config . set ( ' bitmessagesettings ' , ' startintray ' , str ( self . settingsDialogInstance . ui . checkBoxStartInTray . isChecked ( ) ) )
if int ( config . get ( ' bitmessagesettings ' , ' port ' ) ) != int ( self . settingsDialogInstance . ui . lineEditTCPPort . text ( ) ) :
QMessageBox . about ( self , " Restart " , " You must restart Bitmessage for the port number change to take effect. " )
config . set ( ' bitmessagesettings ' , ' port ' , str ( self . settingsDialogInstance . ui . lineEditTCPPort . text ( ) ) )
2012-12-18 19:09:10 +01:00
if config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) == ' none ' and str ( self . settingsDialogInstance . ui . comboBoxProxyType . currentText ( ) ) [ 0 : 5 ] == ' SOCKS ' :
if statusIconColor != ' red ' :
QMessageBox . about ( self , " Restart " , " Bitmessage will use your proxy from now on now but you may want to manually restart Bitmessage now to close existing connections. " )
if config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) [ 0 : 5 ] == ' SOCKS ' and str ( self . settingsDialogInstance . ui . comboBoxProxyType . currentText ( ) ) == ' none ' :
self . statusBar ( ) . showMessage ( ' ' )
config . set ( ' bitmessagesettings ' , ' socksproxytype ' , str ( self . settingsDialogInstance . ui . comboBoxProxyType . currentText ( ) ) )
config . set ( ' bitmessagesettings ' , ' socksauthentication ' , str ( self . settingsDialogInstance . ui . checkBoxAuthentication . isChecked ( ) ) )
config . set ( ' bitmessagesettings ' , ' sockshostname ' , str ( self . settingsDialogInstance . ui . lineEditSocksHostname . text ( ) ) )
config . set ( ' bitmessagesettings ' , ' socksport ' , str ( self . settingsDialogInstance . ui . lineEditSocksPort . text ( ) ) )
config . set ( ' bitmessagesettings ' , ' socksusername ' , str ( self . settingsDialogInstance . ui . lineEditSocksUsername . text ( ) ) )
config . set ( ' bitmessagesettings ' , ' sockspassword ' , str ( self . settingsDialogInstance . ui . lineEditSocksPassword . text ( ) ) )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2012-12-18 19:09:10 +01:00
if ' win32 ' in sys . platform or ' win64 ' in sys . platform :
2012-11-19 20:45:05 +01:00
#Auto-startup for Windows
RUN_PATH = " HKEY_CURRENT_USER \\ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Run "
self . settings = QSettings ( RUN_PATH , QSettings . NativeFormat )
if config . getboolean ( ' bitmessagesettings ' , ' startonlogon ' ) :
self . settings . setValue ( " PyBitmessage " , sys . argv [ 0 ] )
else :
self . settings . remove ( " PyBitmessage " )
elif ' darwin ' in sys . platform :
#startup for mac
pass
elif ' linux ' in sys . platform :
#startup for linux
pass
2013-02-26 21:07:51 +01:00
if appdata != ' ' and self . settingsDialogInstance . ui . checkBoxPortableMode . isChecked ( ) : #If we are NOT using portable mode now but the user selected that we should...
config . set ( ' bitmessagesettings ' , ' movemessagstoprog ' , ' true ' ) #Tells bitmessage to move the messages.dat file to the program directory the next time the program starts.
#Write the keys.dat file to disk in the new location
with open ( ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
#Write the knownnodes.dat file to disk in the new location
output = open ( ' knownnodes.dat ' , ' wb ' )
pickle . dump ( knownNodes , output )
output . close ( )
os . remove ( appdata + ' keys.dat ' )
os . remove ( appdata + ' knownnodes.dat ' )
appdata = ' '
QMessageBox . about ( self , " Restart " , " Bitmessage has moved most of your config files to the program directory but you must restart Bitmessage to move the last file (the file which holds messages). " )
if appdata == ' ' and not self . settingsDialogInstance . ui . checkBoxPortableMode . isChecked ( ) : #If we ARE using portable mode now but the user selected that we shouldn't...
appdata = lookupAppdataFolder ( )
if not os . path . exists ( appdata ) :
os . makedirs ( appdata )
config . set ( ' bitmessagesettings ' , ' movemessagstoappdata ' , ' true ' ) #Tells bitmessage to move the messages.dat file to the appdata directory the next time the program starts.
#Write the keys.dat file to disk in the new location
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
#Write the knownnodes.dat file to disk in the new location
output = open ( appdata + ' knownnodes.dat ' , ' wb ' )
pickle . dump ( knownNodes , output )
output . close ( )
os . remove ( ' keys.dat ' )
os . remove ( ' knownnodes.dat ' )
QMessageBox . about ( self , " Restart " , " Bitmessage has moved most of your config files to the application data directory but you must restart Bitmessage to move the last file (the file which holds messages). " )
2012-11-19 20:45:05 +01:00
def click_radioButtonBlacklist ( self ) :
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' white ' :
config . set ( ' bitmessagesettings ' , ' blackwhitelist ' , ' black ' )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
#self.ui.tableWidgetBlacklist.clearContents()
self . ui . tableWidgetBlacklist . setRowCount ( 0 )
self . loadBlackWhiteList ( )
self . ui . tabWidget . setTabText ( 6 , ' Blacklist ' )
def click_radioButtonWhitelist ( self ) :
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' :
config . set ( ' bitmessagesettings ' , ' blackwhitelist ' , ' white ' )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
#self.ui.tableWidgetBlacklist.clearContents()
self . ui . tableWidgetBlacklist . setRowCount ( 0 )
self . loadBlackWhiteList ( )
self . ui . tabWidget . setTabText ( 6 , ' Whitelist ' )
def click_pushButtonAddBlacklist ( self ) :
self . NewBlacklistDialogInstance = NewSubscriptionDialog ( self )
if self . NewBlacklistDialogInstance . exec_ ( ) :
if self . NewBlacklistDialogInstance . ui . labelSubscriptionAddressCheck . text ( ) == ' Address is valid. ' :
#First we must check to see if the address is already in the address book. The user cannot add it again or else it will cause problems when updating and deleting the entry.
sqlLock . acquire ( )
2012-12-19 20:49:40 +01:00
t = ( addBMIfNotPresent ( str ( self . NewBlacklistDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) ) , )
2012-11-19 20:45:05 +01:00
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' :
sqlSubmitQueue . put ( ''' select * from blacklist where address=? ''' )
else :
sqlSubmitQueue . put ( ''' select * from whitelist where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn == [ ] :
self . ui . tableWidgetBlacklist . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( unicode ( self . NewBlacklistDialogInstance . ui . newsubscriptionlabel . text ( ) . toUtf8 ( ) , ' utf-8 ' ) )
self . ui . tableWidgetBlacklist . setItem ( 0 , 0 , newItem )
2012-12-19 20:49:40 +01:00
newItem = QtGui . QTableWidgetItem ( addBMIfNotPresent ( self . NewBlacklistDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) )
2012-11-19 20:45:05 +01:00
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetBlacklist . setItem ( 0 , 1 , newItem )
2012-12-19 20:49:40 +01:00
t = ( str ( self . NewBlacklistDialogInstance . ui . newsubscriptionlabel . text ( ) . toUtf8 ( ) ) , addBMIfNotPresent ( str ( self . NewBlacklistDialogInstance . ui . lineEditSubscriptionAddress . text ( ) ) ) , True )
2012-11-19 20:45:05 +01:00
sqlLock . acquire ( )
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' :
sqlSubmitQueue . put ( ''' INSERT INTO blacklist VALUES (?,?,?) ''' )
else :
sqlSubmitQueue . put ( ''' INSERT INTO whitelist VALUES (?,?,?) ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
else :
self . statusBar ( ) . showMessage ( ' Error: You cannot add the same address to your list twice. Perhaps rename the existing one if you want. ' )
else :
self . statusBar ( ) . showMessage ( ' The address you entered was invalid. Ignoring it. ' )
2013-02-26 00:09:32 +01:00
def on_action_SpecialAddressBehaviorDialog ( self ) :
self . dialog = SpecialAddressBehaviorDialog ( self )
# For Modal dialogs
if self . dialog . exec_ ( ) :
currentRow = self . ui . tableWidgetYourIdentities . currentRow ( )
addressAtCurrentRow = str ( self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . text ( ) )
if self . dialog . ui . radioButtonBehaveNormalAddress . isChecked ( ) :
config . set ( str ( addressAtCurrentRow ) , ' mailinglist ' , ' false ' )
2013-02-27 18:03:21 +01:00
#Set the color to either black or grey
if config . getboolean ( addressAtCurrentRow , ' enabled ' ) :
self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 0 , 0 , 0 ) )
else :
self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
2013-02-26 00:09:32 +01:00
else :
config . set ( str ( addressAtCurrentRow ) , ' mailinglist ' , ' true ' )
config . set ( str ( addressAtCurrentRow ) , ' mailinglistname ' , str ( self . dialog . ui . lineEditMailingListName . text ( ) . toUtf8 ( ) ) )
2013-02-27 18:03:21 +01:00
self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 137 , 04 , 177 ) )
2013-02-26 00:09:32 +01:00
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2013-02-27 18:03:21 +01:00
self . rerenderInboxToLabels ( )
2013-02-26 00:09:32 +01:00
2012-11-19 20:45:05 +01:00
def click_NewAddressDialog ( self ) :
self . dialog = NewAddressDialog ( self )
# For Modal dialogs
if self . dialog . exec_ ( ) :
2013-01-16 17:52:52 +01:00
#self.dialog.ui.buttonBox.enabled = False
if self . dialog . ui . radioButtonRandomAddress . isChecked ( ) :
if self . dialog . ui . radioButtonMostAvailable . isChecked ( ) :
streamNumberForAddress = 1
else :
#User selected 'Use the same stream as an existing address.'
streamNumberForAddress = addressStream ( self . dialog . ui . comboBoxExisting . currentText ( ) )
self . addressGenerator = addressGenerator ( )
self . addressGenerator . setup ( 2 , streamNumberForAddress , str ( self . dialog . ui . newaddresslabel . text ( ) . toUtf8 ( ) ) , 1 , " " , self . dialog . ui . checkBoxEighteenByteRipe . isChecked ( ) )
QtCore . QObject . connect ( self . addressGenerator , SIGNAL ( " writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . writeNewAddressToTable )
QtCore . QObject . connect ( self . addressGenerator , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
self . addressGenerator . start ( )
2012-11-19 20:45:05 +01:00
else :
2013-01-16 17:52:52 +01:00
if self . dialog . ui . lineEditPassphrase . text ( ) != self . dialog . ui . lineEditPassphraseAgain . text ( ) :
QMessageBox . about ( self , " Passphrase mismatch " , " The passphrase you entered twice doesn \' t match. Try again. " )
elif self . dialog . ui . lineEditPassphrase . text ( ) == " " :
QMessageBox . about ( self , " Choose a passphrase " , " You really do need a passphrase. " )
else :
streamNumberForAddress = 1 #this will eventually have to be replaced by logic to determine the most available stream number.
self . addressGenerator = addressGenerator ( )
self . addressGenerator . setup ( 2 , streamNumberForAddress , " unused address " , self . dialog . ui . spinBoxNumberOfAddressesToMake . value ( ) , self . dialog . ui . lineEditPassphrase . text ( ) . toUtf8 ( ) , self . dialog . ui . checkBoxEighteenByteRipe . isChecked ( ) )
QtCore . QObject . connect ( self . addressGenerator , SIGNAL ( " writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject) " ) , self . writeNewAddressToTable )
QtCore . QObject . connect ( self . addressGenerator , QtCore . SIGNAL ( " updateStatusBar(PyQt_PyObject) " ) , self . updateStatusBar )
self . addressGenerator . start ( )
2012-11-19 20:45:05 +01:00
else :
2013-01-16 17:52:52 +01:00
print ' new address dialog box rejected '
2012-11-19 20:45:05 +01:00
def closeEvent ( self , event ) :
''' quit_msg = " Are you sure you want to exit Bitmessage? "
reply = QtGui . QMessageBox . question ( self , ' Message ' ,
quit_msg , QtGui . QMessageBox . Yes , QtGui . QMessageBox . No )
if reply == QtGui . QMessageBox . Yes :
event . accept ( )
else :
event . ignore ( ) '''
2013-02-04 22:49:02 +01:00
broadcastToSendDataQueues ( ( 0 , ' shutdown ' , ' all ' ) )
2013-02-06 22:17:49 +01:00
printLock . acquire ( )
2013-02-04 22:49:02 +01:00
print ' Closing. Flushing inventory in memory out to disk... '
2013-02-06 22:17:49 +01:00
printLock . release ( )
2013-02-04 22:49:02 +01:00
self . statusBar ( ) . showMessage ( ' Flushing inventory in memory out to disk. ' )
flushInventory ( )
2012-11-19 20:45:05 +01:00
#This one last useless query will guarantee that the previous query committed before we close the program.
sqlLock . acquire ( )
sqlSubmitQueue . put ( ' SELECT address FROM subscriptions ' )
sqlSubmitQueue . put ( ' ' )
sqlReturnQueue . get ( )
sqlLock . release ( )
self . statusBar ( ) . showMessage ( ' Saving the knownNodes list of peers to disk... ' )
output = open ( appdata + ' knownnodes.dat ' , ' wb ' )
pickle . dump ( knownNodes , output )
output . close ( )
self . trayIcon . hide ( )
2013-02-06 22:17:49 +01:00
printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' Done. '
2013-02-06 22:17:49 +01:00
printLock . release ( )
2012-11-19 20:45:05 +01:00
self . statusBar ( ) . showMessage ( ' All done. Closing user interface... ' )
event . accept ( )
raise SystemExit
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
2012-11-29 18:37:07 +01:00
def on_action_InboxReply ( self ) :
2012-11-19 20:45:05 +01:00
currentInboxRow = self . ui . tableWidgetInbox . currentRow ( )
toAddressAtCurrentInboxRow = str ( self . ui . tableWidgetInbox . item ( currentInboxRow , 0 ) . data ( Qt . UserRole ) . toPyObject ( ) )
fromAddressAtCurrentInboxRow = str ( self . ui . tableWidgetInbox . item ( currentInboxRow , 1 ) . data ( Qt . UserRole ) . toPyObject ( ) )
2013-01-24 20:52:48 +01:00
2013-02-18 21:22:48 +01:00
2013-01-24 20:52:48 +01:00
if toAddressAtCurrentInboxRow == ' [Broadcast subscribers] ' :
self . ui . labelFrom . setText ( ' ' )
else :
if not config . get ( toAddressAtCurrentInboxRow , ' enabled ' ) :
self . statusBar ( ) . showMessage ( ' Error: The address from which you are trying to send is disabled. Enable it from the \' Your Identities \' tab first. ' )
return
self . ui . labelFrom . setText ( toAddressAtCurrentInboxRow )
2012-11-19 20:45:05 +01:00
self . ui . lineEditTo . setText ( str ( fromAddressAtCurrentInboxRow ) )
2012-12-04 18:11:14 +01:00
self . ui . comboBoxSendFrom . setCurrentIndex ( 0 )
#self.ui.comboBoxSendFrom.setEditText(str(self.ui.tableWidgetInbox.item(currentInboxRow,0).text))
2012-11-29 18:37:07 +01:00
self . ui . textEditMessage . setText ( ' \n \n ------------------------------------------------------ \n ' + self . ui . tableWidgetInbox . item ( currentInboxRow , 2 ) . data ( Qt . UserRole ) . toPyObject ( ) )
2012-12-05 17:29:28 +01:00
if self . ui . tableWidgetInbox . item ( currentInboxRow , 2 ) . text ( ) [ 0 : 3 ] == ' Re: ' :
2013-02-26 00:09:32 +01:00
self . ui . lineEditSubject . setText ( self . ui . tableWidgetInbox . item ( currentInboxRow , 2 ) . text ( ) )
2012-11-29 18:37:07 +01:00
else :
2012-12-05 17:29:28 +01:00
self . ui . lineEditSubject . setText ( ' Re: ' + self . ui . tableWidgetInbox . item ( currentInboxRow , 2 ) . text ( ) )
2012-11-29 18:37:07 +01:00
self . ui . radioButtonSpecific . setChecked ( True )
2012-11-19 20:45:05 +01:00
self . ui . tabWidget . setCurrentIndex ( 1 )
2012-11-29 18:37:07 +01:00
def on_action_InboxAddSenderToAddressBook ( self ) :
2012-11-19 20:45:05 +01:00
currentInboxRow = self . ui . tableWidgetInbox . currentRow ( )
#self.ui.tableWidgetInbox.item(currentRow,1).data(Qt.UserRole).toPyObject()
addressAtCurrentInboxRow = str ( self . ui . tableWidgetInbox . item ( currentInboxRow , 1 ) . data ( Qt . UserRole ) . toPyObject ( ) )
#Let's make sure that it isn't already in the address book
sqlLock . acquire ( )
t = ( addressAtCurrentInboxRow , )
sqlSubmitQueue . put ( ''' select * from addressbook where address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
if queryreturn == [ ] :
self . ui . tableWidgetAddressBook . insertRow ( 0 )
newItem = QtGui . QTableWidgetItem ( ' --New entry. Change label in Address Book.-- ' )
self . ui . tableWidgetAddressBook . setItem ( 0 , 0 , newItem )
newItem = QtGui . QTableWidgetItem ( addressAtCurrentInboxRow )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetAddressBook . setItem ( 0 , 1 , newItem )
t = ( ' --New entry. Change label in Address Book.-- ' , addressAtCurrentInboxRow )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' INSERT INTO addressbook VALUES (?,?) ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
self . ui . tabWidget . setCurrentIndex ( 5 )
self . ui . tableWidgetAddressBook . setCurrentCell ( 0 , 0 )
self . statusBar ( ) . showMessage ( ' Entry added to the Address Book. Edit the label to your liking. ' )
else :
self . statusBar ( ) . showMessage ( ' Error: You cannot add the same address to your address book twice. Try renaming the existing one if you want. ' )
2013-01-31 19:35:36 +01:00
#Send item on the Inbox tab to trash
2012-11-19 20:45:05 +01:00
def on_action_InboxTrash ( self ) :
currentRow = self . ui . tableWidgetInbox . currentRow ( )
inventoryHashToTrash = str ( self . ui . tableWidgetInbox . item ( currentRow , 3 ) . data ( Qt . UserRole ) . toPyObject ( ) )
t = ( inventoryHashToTrash , )
sqlLock . acquire ( )
#sqlSubmitQueue.put('''delete from inbox where msgid=?''')
sqlSubmitQueue . put ( ''' UPDATE inbox SET folder= ' trash ' WHERE msgid=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2013-03-05 20:50:31 +01:00
self . ui . textEditInboxMessage . setText ( " " )
2012-11-19 20:45:05 +01:00
self . ui . tableWidgetInbox . removeRow ( currentRow )
self . statusBar ( ) . showMessage ( ' Moved item to trash. There is no user interface to view your trash, but it is still on disk if you are desperate to get it back. ' )
2013-01-31 19:35:36 +01:00
#Send item on the Sent tab to trash
def on_action_SentTrash ( self ) :
currentRow = self . ui . tableWidgetSent . currentRow ( )
ackdataToTrash = str ( self . ui . tableWidgetSent . item ( currentRow , 3 ) . data ( Qt . UserRole ) . toPyObject ( ) )
t = ( ackdataToTrash , )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' UPDATE sent SET folder= ' trash ' WHERE ackdata=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2013-03-06 19:42:44 +01:00
self . ui . textEditSentMessage . setText ( " " )
2013-01-31 19:35:36 +01:00
self . ui . tableWidgetSent . removeRow ( currentRow )
self . statusBar ( ) . showMessage ( ' Moved item to trash. There is no user interface to view your trash, but it is still on disk if you are desperate to get it back. ' )
2013-02-26 00:09:32 +01:00
def on_action_SentClipboard ( self ) :
currentRow = self . ui . tableWidgetSent . currentRow ( )
addressAtCurrentRow = str ( self . ui . tableWidgetSent . item ( currentRow , 0 ) . data ( Qt . UserRole ) . toPyObject ( ) )
clipboard = QtGui . QApplication . clipboard ( )
clipboard . setText ( str ( addressAtCurrentRow ) )
2013-01-31 19:35:36 +01:00
2012-11-19 20:45:05 +01:00
#Group of functions for the Address Book dialog box
def on_action_AddressBookNew ( self ) :
self . click_pushButtonAddAddressBook ( )
def on_action_AddressBookDelete ( self ) :
currentRow = self . ui . tableWidgetAddressBook . currentRow ( )
labelAtCurrentRow = self . ui . tableWidgetAddressBook . item ( currentRow , 0 ) . text ( ) . toUtf8 ( )
addressAtCurrentRow = self . ui . tableWidgetAddressBook . item ( currentRow , 1 ) . text ( )
t = ( str ( labelAtCurrentRow ) , str ( addressAtCurrentRow ) )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' DELETE FROM addressbook WHERE label=? AND address=? ''' )
sqlSubmitQueue . put ( t )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
self . ui . tableWidgetAddressBook . removeRow ( currentRow )
self . rerenderInboxFromLabels ( )
self . rerenderSentToLabels ( )
self . reloadBroadcastSendersForWhichImWatching ( )
def on_action_AddressBookClipboard ( self ) :
currentRow = self . ui . tableWidgetAddressBook . currentRow ( )
addressAtCurrentRow = self . ui . tableWidgetAddressBook . item ( currentRow , 1 ) . text ( )
clipboard = QtGui . QApplication . clipboard ( )
clipboard . setText ( str ( addressAtCurrentRow ) )
def on_action_AddressBookSend ( self ) :
currentRow = self . ui . tableWidgetAddressBook . currentRow ( )
addressAtCurrentRow = self . ui . tableWidgetAddressBook . item ( currentRow , 1 ) . text ( )
if self . ui . lineEditTo . text ( ) == ' ' :
self . ui . lineEditTo . setText ( str ( addressAtCurrentRow ) )
else :
self . ui . lineEditTo . setText ( str ( self . ui . lineEditTo . text ( ) ) + ' ; ' + str ( addressAtCurrentRow ) )
self . statusBar ( ) . showMessage ( ' You have added the address to the \' To \' field on the \' Send \' tab. You may add more recipients if you want. When you are done, go to the \' Send \' tab. ' )
def on_context_menuAddressBook ( self , point ) :
self . popMenuAddressBook . exec_ ( self . ui . tableWidgetAddressBook . mapToGlobal ( point ) )
#Group of functions for the Subscriptions dialog box
def on_action_SubscriptionsNew ( self ) :
self . click_pushButtonAddSubscription ( )
def on_action_SubscriptionsDelete ( self ) :
print ' clicked Delete '
currentRow = self . ui . tableWidgetSubscriptions . currentRow ( )
labelAtCurrentRow = self . ui . tableWidgetSubscriptions . item ( currentRow , 0 ) . text ( ) . toUtf8 ( )
addressAtCurrentRow = self . ui . tableWidgetSubscriptions . item ( currentRow , 1 ) . text ( )
t = ( str ( labelAtCurrentRow ) , str ( addressAtCurrentRow ) )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ''' DELETE FROM subscriptions WHERE label=? AND address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
self . ui . tableWidgetSubscriptions . removeRow ( currentRow )
self . rerenderInboxFromLabels ( )
2013-03-03 07:54:06 +01:00
self . reloadBroadcastSendersForWhichImWatching ( )
2012-11-19 20:45:05 +01:00
def on_action_SubscriptionsClipboard ( self ) :
currentRow = self . ui . tableWidgetSubscriptions . currentRow ( )
addressAtCurrentRow = self . ui . tableWidgetSubscriptions . item ( currentRow , 1 ) . text ( )
clipboard = QtGui . QApplication . clipboard ( )
clipboard . setText ( str ( addressAtCurrentRow ) )
def on_context_menuSubscriptions ( self , point ) :
self . popMenuSubscriptions . exec_ ( self . ui . tableWidgetSubscriptions . mapToGlobal ( point ) )
2013-02-26 00:09:32 +01:00
#Group of functions for the Blacklist dialog box
def on_action_BlacklistNew ( self ) :
self . click_pushButtonAddBlacklist ( )
def on_action_BlacklistDelete ( self ) :
print ' clicked Delete '
currentRow = self . ui . tableWidgetBlacklist . currentRow ( )
labelAtCurrentRow = self . ui . tableWidgetBlacklist . item ( currentRow , 0 ) . text ( ) . toUtf8 ( )
addressAtCurrentRow = self . ui . tableWidgetBlacklist . item ( currentRow , 1 ) . text ( )
t = ( str ( labelAtCurrentRow ) , str ( addressAtCurrentRow ) )
sqlLock . acquire ( )
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' :
sqlSubmitQueue . put ( ''' DELETE FROM blacklist WHERE label=? AND address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
else :
sqlSubmitQueue . put ( ''' DELETE FROM whitelist WHERE label=? AND address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
self . ui . tableWidgetBlacklist . removeRow ( currentRow )
def on_action_BlacklistClipboard ( self ) :
currentRow = self . ui . tableWidgetBlacklist . currentRow ( )
addressAtCurrentRow = self . ui . tableWidgetBlacklist . item ( currentRow , 1 ) . text ( )
clipboard = QtGui . QApplication . clipboard ( )
clipboard . setText ( str ( addressAtCurrentRow ) )
def on_context_menuBlacklist ( self , point ) :
self . popMenuBlacklist . exec_ ( self . ui . tableWidgetBlacklist . mapToGlobal ( point ) )
def on_action_BlacklistEnable ( self ) :
currentRow = self . ui . tableWidgetBlacklist . currentRow ( )
addressAtCurrentRow = self . ui . tableWidgetBlacklist . item ( currentRow , 1 ) . text ( )
self . ui . tableWidgetBlacklist . item ( currentRow , 0 ) . setTextColor ( QtGui . QColor ( 0 , 0 , 0 ) )
self . ui . tableWidgetBlacklist . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 0 , 0 , 0 ) )
t = ( str ( addressAtCurrentRow ) , )
sqlLock . acquire ( )
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' :
sqlSubmitQueue . put ( ''' UPDATE blacklist SET enabled=1 WHERE address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
else :
sqlSubmitQueue . put ( ''' UPDATE whitelist SET enabled=1 WHERE address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
def on_action_BlacklistDisable ( self ) :
currentRow = self . ui . tableWidgetBlacklist . currentRow ( )
addressAtCurrentRow = self . ui . tableWidgetBlacklist . item ( currentRow , 1 ) . text ( )
self . ui . tableWidgetBlacklist . item ( currentRow , 0 ) . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
self . ui . tableWidgetBlacklist . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
t = ( str ( addressAtCurrentRow ) , )
sqlLock . acquire ( )
if config . get ( ' bitmessagesettings ' , ' blackwhitelist ' ) == ' black ' :
sqlSubmitQueue . put ( ''' UPDATE blacklist SET enabled=0 WHERE address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
else :
sqlSubmitQueue . put ( ''' UPDATE whitelist SET enabled=0 WHERE address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
2012-11-19 20:45:05 +01:00
#Group of functions for the Your Identities dialog box
2012-11-29 18:37:07 +01:00
def on_action_YourIdentitiesNew ( self ) :
2012-11-19 20:45:05 +01:00
self . click_NewAddressDialog ( )
2012-11-29 18:37:07 +01:00
def on_action_YourIdentitiesEnable ( self ) :
2012-11-19 20:45:05 +01:00
currentRow = self . ui . tableWidgetYourIdentities . currentRow ( )
2013-02-27 18:03:21 +01:00
addressAtCurrentRow = str ( self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . text ( ) )
config . set ( addressAtCurrentRow , ' enabled ' , ' true ' )
2012-11-19 20:45:05 +01:00
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
self . ui . tableWidgetYourIdentities . item ( currentRow , 0 ) . setTextColor ( QtGui . QColor ( 0 , 0 , 0 ) )
self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 0 , 0 , 0 ) )
self . ui . tableWidgetYourIdentities . item ( currentRow , 2 ) . setTextColor ( QtGui . QColor ( 0 , 0 , 0 ) )
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( addressAtCurrentRow , ' mailinglist ' ) :
self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 137 , 04 , 177 ) )
2013-01-18 23:38:09 +01:00
reloadMyAddressHashes ( )
2012-11-29 18:37:07 +01:00
def on_action_YourIdentitiesDisable ( self ) :
2012-11-19 20:45:05 +01:00
currentRow = self . ui . tableWidgetYourIdentities . currentRow ( )
2013-02-27 18:03:21 +01:00
addressAtCurrentRow = str ( self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . text ( ) )
2012-11-19 20:45:05 +01:00
config . set ( str ( addressAtCurrentRow ) , ' enabled ' , ' false ' )
self . ui . tableWidgetYourIdentities . item ( currentRow , 0 ) . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
self . ui . tableWidgetYourIdentities . item ( currentRow , 2 ) . setTextColor ( QtGui . QColor ( 128 , 128 , 128 ) )
2013-03-19 18:32:37 +01:00
if safeConfigGetBoolean ( addressAtCurrentRow , ' mailinglist ' ) :
self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . setTextColor ( QtGui . QColor ( 137 , 04 , 177 ) )
2012-11-19 20:45:05 +01:00
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2013-01-18 23:38:09 +01:00
reloadMyAddressHashes ( )
2012-11-29 18:37:07 +01:00
def on_action_YourIdentitiesClipboard ( self ) :
2012-11-19 20:45:05 +01:00
currentRow = self . ui . tableWidgetYourIdentities . currentRow ( )
addressAtCurrentRow = self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . text ( )
clipboard = QtGui . QApplication . clipboard ( )
clipboard . setText ( str ( addressAtCurrentRow ) )
def on_context_menuYourIdentities ( self , point ) :
self . popMenu . exec_ ( self . ui . tableWidgetYourIdentities . mapToGlobal ( point ) )
def on_context_menuInbox ( self , point ) :
self . popMenuInbox . exec_ ( self . ui . tableWidgetInbox . mapToGlobal ( point ) )
2013-01-31 19:35:36 +01:00
def on_context_menuSent ( self , point ) :
self . popMenuSent . exec_ ( self . ui . tableWidgetSent . mapToGlobal ( point ) )
2012-11-19 20:45:05 +01:00
def tableWidgetInboxItemClicked ( self ) :
currentRow = self . ui . tableWidgetInbox . currentRow ( )
2013-03-05 20:50:31 +01:00
if currentRow > = 0 :
self . ui . textEditInboxMessage . setText ( self . ui . tableWidgetInbox . item ( currentRow , 2 ) . data ( Qt . UserRole ) . toPyObject ( ) )
2012-11-19 20:45:05 +01:00
def tableWidgetSentItemClicked ( self ) :
2013-03-06 19:42:44 +01:00
currentRow = self . ui . tableWidgetSent . currentRow ( )
if currentRow > = 0 :
2012-11-19 20:45:05 +01:00
self . ui . textEditSentMessage . setText ( self . ui . tableWidgetSent . item ( currentRow , 2 ) . data ( Qt . UserRole ) . toPyObject ( ) )
def tableWidgetYourIdentitiesItemChanged ( self ) :
currentRow = self . ui . tableWidgetYourIdentities . currentRow ( )
2012-12-10 20:48:00 +01:00
if currentRow > = 0 :
2012-11-19 20:45:05 +01:00
addressAtCurrentRow = self . ui . tableWidgetYourIdentities . item ( currentRow , 1 ) . text ( )
config . set ( str ( addressAtCurrentRow ) , ' label ' , str ( self . ui . tableWidgetYourIdentities . item ( currentRow , 0 ) . text ( ) . toUtf8 ( ) ) )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2012-12-10 20:48:00 +01:00
self . rerenderComboBoxSendFrom ( )
#self.rerenderInboxFromLabels()
self . rerenderInboxToLabels ( )
self . rerenderSentFromLabels ( )
#self.rerenderSentToLabels()
2012-11-19 20:45:05 +01:00
def tableWidgetAddressBookItemChanged ( self ) :
currentRow = self . ui . tableWidgetAddressBook . currentRow ( )
sqlLock . acquire ( )
2012-12-18 19:09:10 +01:00
if currentRow > = 0 :
2012-11-19 20:45:05 +01:00
addressAtCurrentRow = self . ui . tableWidgetAddressBook . item ( currentRow , 1 ) . text ( )
t = ( str ( self . ui . tableWidgetAddressBook . item ( currentRow , 0 ) . text ( ) . toUtf8 ( ) ) , str ( addressAtCurrentRow ) )
sqlSubmitQueue . put ( ''' UPDATE addressbook set label=? WHERE address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
self . rerenderInboxFromLabels ( )
self . rerenderSentToLabels ( )
def tableWidgetSubscriptionsItemChanged ( self ) :
currentRow = self . ui . tableWidgetSubscriptions . currentRow ( )
sqlLock . acquire ( )
2012-12-18 19:09:10 +01:00
if currentRow > = 0 :
2012-11-19 20:45:05 +01:00
addressAtCurrentRow = self . ui . tableWidgetSubscriptions . item ( currentRow , 1 ) . text ( )
t = ( str ( self . ui . tableWidgetSubscriptions . item ( currentRow , 0 ) . text ( ) . toUtf8 ( ) ) , str ( addressAtCurrentRow ) )
sqlSubmitQueue . put ( ''' UPDATE subscriptions set label=? WHERE address=? ''' )
sqlSubmitQueue . put ( t )
sqlReturnQueue . get ( )
sqlLock . release ( )
self . rerenderInboxFromLabels ( )
2013-02-18 21:22:48 +01:00
self . rerenderSentToLabels ( )
2012-11-19 20:45:05 +01:00
def writeNewAddressToTable ( self , label , address , streamNumber ) :
self . ui . tableWidgetYourIdentities . insertRow ( 0 )
self . ui . tableWidgetYourIdentities . setItem ( 0 , 0 , QtGui . QTableWidgetItem ( unicode ( label , ' utf-8 ' ) ) )
newItem = QtGui . QTableWidgetItem ( address )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetYourIdentities . setItem ( 0 , 1 , newItem )
newItem = QtGui . QTableWidgetItem ( streamNumber )
newItem . setFlags ( QtCore . Qt . ItemIsSelectable | QtCore . Qt . ItemIsEnabled )
self . ui . tableWidgetYourIdentities . setItem ( 0 , 2 , newItem )
self . rerenderComboBoxSendFrom ( )
def updateStatusBar ( self , data ) :
2013-02-26 00:09:32 +01:00
if data != " " :
printLock . acquire ( )
print ' Status bar: ' , data
printLock . release ( )
2012-11-19 20:45:05 +01:00
self . statusBar ( ) . showMessage ( data )
def reloadBroadcastSendersForWhichImWatching ( self ) :
broadcastSendersForWhichImWatching . clear ( )
sqlLock . acquire ( )
sqlSubmitQueue . put ( ' SELECT address FROM subscriptions ' )
sqlSubmitQueue . put ( ' ' )
queryreturn = sqlReturnQueue . get ( )
sqlLock . release ( )
for row in queryreturn :
address , = row
status , addressVersionNumber , streamNumber , hash = decodeAddress ( address )
broadcastSendersForWhichImWatching [ hash ] = 0
2012-12-10 20:48:00 +01:00
#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 ( QTableWidgetItem ) :
def __lt__ ( self , other ) :
return int ( self . data ( 33 ) . toPyObject ( ) ) < int ( other . data ( 33 ) . toPyObject ( ) )
2012-11-19 20:45:05 +01:00
sendDataQueues = [ ] #each sendData thread puts its queue in this list.
2013-01-18 23:38:09 +01:00
myRSAAddressHashes = { }
myECAddressHashes = { }
2012-11-19 20:45:05 +01:00
#myPrivateKeys = {}
inventory = { } #of objects (like msg payloads and pubkey payloads) Does not include protocol headers (the first 24 bytes of each packet).
workerQueue = Queue . Queue ( )
sqlSubmitQueue = Queue . Queue ( ) #SQLITE3 is so thread-unsafe that they won't even let you call it from different threads using your own locks. SQL objects can only be called from one thread.
sqlReturnQueue = Queue . Queue ( )
sqlLock = threading . Lock ( )
printLock = threading . Lock ( )
ackdataForWhichImWatching = { }
broadcastSendersForWhichImWatching = { }
statusIconColor = ' red '
connectionsCount = { } #Used for the 'network status' tab.
connectionsCountLock = threading . Lock ( )
2012-12-18 22:36:37 +01:00
inventoryLock = threading . Lock ( ) #Guarantees that two receiveDataThreads don't receive and process the same message concurrently (probably sent by a malicious individual)
2012-11-19 20:45:05 +01:00
eightBytesOfRandomDataUsedToDetectConnectionsToSelf = pack ( ' >Q ' , random . randrange ( 1 , 18446744073709551615 ) )
connectedHostsList = { } #List of hosts to which we are connected. Used to guarantee that the outgoingSynSender thread won't connect to the same remote node twice.
neededPubkeys = { }
2013-02-05 22:53:56 +01:00
successfullyDecryptMessageTimings = [ ] #A list of the amounts of time it took to successfully decrypt msg messages
2013-03-19 18:32:37 +01:00
apiSignalQueue = Queue . Queue ( ) #The singleAPI thread uses this queue to pass messages to a QT thread which can emit signals to do things like display a message in the UI.
apiAddressGeneratorReturnQueue = Queue . Queue ( ) #The address generator thread uses this queue to get information back to the API thread.
2012-11-19 20:45:05 +01:00
2012-11-23 09:22:56 +01:00
#These constants are not at the top because if changed they will cause particularly unexpected behavior: You won't be able to either send or receive messages because the proof of work you do (or demand) won't match that done or demanded by others. Don't change them!
averageProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work.
payloadLengthExtraBytes = 14000 #To make sending short messages a little more difficult, this value is added to the payload length for use in calculating the proof of work target.
2012-11-19 20:45:05 +01:00
2013-01-22 20:29:49 +01:00
if useVeryEasyProofOfWorkForTesting :
2013-02-06 22:17:49 +01:00
averageProofOfWorkNonceTrialsPerByte = averageProofOfWorkNonceTrialsPerByte / 16
payloadLengthExtraBytes = payloadLengthExtraBytes / 7000
2013-01-18 23:38:09 +01:00
2012-11-19 20:45:05 +01:00
if __name__ == " __main__ " :
2012-11-29 12:35:48 +01:00
# Check the Major version, the first element in the array
2012-11-29 18:37:07 +01:00
if sqlite3 . sqlite_version_info [ 0 ] < 3 :
2012-11-19 20:45:05 +01:00
print ' This program requires sqlite version 3 or higher because 2 and lower cannot store NULL values. I see version: ' , sqlite3 . sqlite_version_info
sys . exit ( )
2013-02-26 21:07:51 +01:00
#First try to load the config file (the keys.dat file) from the program directory
2012-11-19 20:45:05 +01:00
config = ConfigParser . SafeConfigParser ( )
2013-02-26 21:07:51 +01:00
config . read ( ' keys.dat ' )
2012-11-19 20:45:05 +01:00
try :
config . get ( ' bitmessagesettings ' , ' settingsversion ' )
2013-02-26 21:07:51 +01:00
#settingsFileExistsInProgramDirectory = True
print ' Loading config files from same directory as program '
appdata = ' '
2012-11-19 20:45:05 +01:00
except :
2013-02-26 21:07:51 +01:00
#Could not load the keys.dat file in the program directory. Perhaps it is in the appdata directory.
appdata = lookupAppdataFolder ( )
#if not os.path.exists(appdata):
# os.makedirs(appdata)
2012-11-19 20:45:05 +01:00
2013-02-26 21:07:51 +01:00
config = ConfigParser . SafeConfigParser ( )
config . read ( appdata + ' keys.dat ' )
try :
config . get ( ' bitmessagesettings ' , ' settingsversion ' )
print ' Loading existing config files from ' , appdata
except :
#This appears to be the first time running the program; there is no config file (or it cannot be accessed). Create config file.
config . add_section ( ' bitmessagesettings ' )
config . set ( ' bitmessagesettings ' , ' settingsversion ' , ' 1 ' )
config . set ( ' bitmessagesettings ' , ' port ' , ' 8444 ' )
config . set ( ' bitmessagesettings ' , ' timeformat ' , ' %% a, %% d %% b %% Y %% I: %% M %% p ' )
config . set ( ' bitmessagesettings ' , ' blackwhitelist ' , ' black ' )
config . set ( ' bitmessagesettings ' , ' startonlogon ' , ' false ' )
if ' linux ' in sys . platform :
config . set ( ' bitmessagesettings ' , ' minimizetotray ' , ' false ' ) #This isn't implimented yet and when True on Ubuntu causes Bitmessage to disappear while running when minimized.
else :
config . set ( ' bitmessagesettings ' , ' minimizetotray ' , ' true ' )
config . set ( ' bitmessagesettings ' , ' showtraynotifications ' , ' true ' )
config . set ( ' bitmessagesettings ' , ' startintray ' , ' false ' )
if storeConfigFilesInSameDirectoryAsProgramByDefault :
#Just use the same directory as the program and forget about the appdata folder
appdata = ' '
print ' Creating new config files in same directory as program. '
else :
print ' Creating new config files in ' , appdata
if not os . path . exists ( appdata ) :
os . makedirs ( appdata )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2012-11-19 20:45:05 +01:00
2012-12-18 19:09:10 +01:00
if config . getint ( ' bitmessagesettings ' , ' settingsversion ' ) == 1 :
2013-02-11 22:28:38 +01:00
config . set ( ' bitmessagesettings ' , ' settingsversion ' , ' 3 ' ) #If the settings version is equal to 2 then the sqlThread will modify the pubkeys table and change the settings version to 3.
2012-12-18 19:09:10 +01:00
config . set ( ' bitmessagesettings ' , ' socksproxytype ' , ' none ' )
config . set ( ' bitmessagesettings ' , ' sockshostname ' , ' localhost ' )
config . set ( ' bitmessagesettings ' , ' socksport ' , ' 9050 ' )
config . set ( ' bitmessagesettings ' , ' socksauthentication ' , ' false ' )
config . set ( ' bitmessagesettings ' , ' socksusername ' , ' ' )
config . set ( ' bitmessagesettings ' , ' sockspassword ' , ' ' )
config . set ( ' bitmessagesettings ' , ' keysencrypted ' , ' false ' )
config . set ( ' bitmessagesettings ' , ' messagesencrypted ' , ' false ' )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
2013-02-26 21:07:51 +01:00
#Let us now see if we should move the messages.dat file. There is an option in the settings to switch 'Portable Mode' on or off. Most of the files are moved instantly, but the messages.dat file cannot be moved while it is open. Now that it is not open we can move it now!
try :
config . getboolean ( ' bitmessagesettings ' , ' movemessagstoprog ' )
#If we have reached this point then we must move the messages.dat file from the appdata folder to the program folder
print ' Moving messages.dat from its old location in the application data folder to its new home along side the program. '
shutil . move ( lookupAppdataFolder ( ) + ' messages.dat ' , ' messages.dat ' )
config . remove_option ( ' bitmessagesettings ' , ' movemessagstoprog ' )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
except :
pass
try :
config . getboolean ( ' bitmessagesettings ' , ' movemessagstoappdata ' )
#If we have reached this point then we must move the messages.dat file from the appdata folder to the program folder
print ' Moving messages.dat from its old location next to the program to its new home in the application data folder. '
shutil . move ( ' messages.dat ' , lookupAppdataFolder ( ) + ' messages.dat ' )
config . remove_option ( ' bitmessagesettings ' , ' movemessagstoappdata ' )
with open ( appdata + ' keys.dat ' , ' wb ' ) as configfile :
config . write ( configfile )
except :
pass
2012-11-19 20:45:05 +01:00
try :
pickleFile = open ( appdata + ' knownnodes.dat ' , ' rb ' )
knownNodes = pickle . load ( pickleFile )
pickleFile . close ( )
except :
createDefaultKnownNodes ( appdata )
pickleFile = open ( appdata + ' knownnodes.dat ' , ' rb ' )
knownNodes = pickle . load ( pickleFile )
pickleFile . close ( )
2013-02-11 22:28:38 +01:00
if config . getint ( ' bitmessagesettings ' , ' settingsversion ' ) > 3 :
2012-11-19 20:45:05 +01:00
print ' Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage. '
raise SystemExit
2013-02-04 22:49:02 +01:00
#DNS bootstrap. This could be programmed to use the SOCKS proxy to do the DNS lookup some day but for now we will just rely on the entries in defaultKnownNodes.py. Hopefully either they are up to date or the user has run Bitmessage recently without SOCKS turned on and received good bootstrap nodes using that method.
if config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) == ' none ' :
try :
for item in socket . getaddrinfo ( ' bootstrap8080.bitmessage.org ' , 80 ) :
print ' Adding ' , item [ 4 ] [ 0 ] , ' to knownNodes based on DNS boostrap method '
knownNodes [ 1 ] [ item [ 4 ] [ 0 ] ] = ( 8080 , int ( time . time ( ) ) )
except :
print ' bootstrap8080.bitmessage.org DNS bootstraping failed. '
try :
for item in socket . getaddrinfo ( ' bootstrap8444.bitmessage.org ' , 80 ) :
print ' Adding ' , item [ 4 ] [ 0 ] , ' to knownNodes based on DNS boostrap method '
knownNodes [ 1 ] [ item [ 4 ] [ 0 ] ] = ( 8444 , int ( time . time ( ) ) )
except :
print ' bootstrap8444.bitmessage.org DNS bootstrapping failed. '
else :
print ' DNS bootstrap skipped because SOCKS is used. '
2013-01-18 23:38:09 +01:00
2012-11-19 20:45:05 +01:00
app = QtGui . QApplication ( sys . argv )
app . setStyleSheet ( " QStatusBar::item { border: 0px solid black } " )
myapp = MyForm ( )
myapp . show ( )
if config . getboolean ( ' bitmessagesettings ' , ' startintray ' ) :
myapp . hide ( )
myapp . trayIcon . show ( )
#self.hidden = True
#self.setWindowState(self.windowState() & QtCore.Qt.WindowMinimized)
#self.hide()
2012-12-18 19:09:10 +01:00
if ' win32 ' in sys . platform or ' win64 ' in sys . platform :
2012-11-19 20:45:05 +01:00
myapp . setWindowFlags ( Qt . ToolTip )
2013-02-18 21:22:48 +01:00
2012-11-23 09:22:56 +01:00
sys . exit ( app . exec_ ( ) )
# So far, the Bitmessage protocol, this client, the Wiki, and the forums
# are all a one-man operation. Bitcoin tips are quite appreciated!
2012-11-29 11:39:39 +01:00
# 1H5XaDA6fYENLbknwZyjiYXYPQaFjjLX2u