From 973ea35177327d2d7ab025372a21b5a43cf40f50 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Mon, 22 Apr 2013 16:01:41 -0400 Subject: [PATCH 01/33] don't use pubkeys table to send pubkeys to peers now that we maintain them in the inventory table for 28 days anyway --- src/bitmessagemain.py | 80 ++++++++++++++++++-------------------- src/messages.dat reader.py | 11 +++++- 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 409b1ad2..ff384f79 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -786,7 +786,7 @@ class receiveDataThread(QThread): 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. - for key, cryptorObject in myECAddressHashes.items(): + for key, cryptorObject in myECCryptorObjects.items(): try: unencryptedData = cryptorObject.decrypt(encryptedData[readPosition:]) 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. @@ -915,19 +915,10 @@ class receiveDataThread(QThread): 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 + toAddress = myAddressesByHash[toRipe] #Look up my address based on the RIPE hash. + toLabel = config.get(toAddress, 'label') + if toLabel == '': + toLabel = addressInKeysFile if messageEncodingType == 2: bodyPositionIndex = string.find(message,'\nBody:') @@ -1233,7 +1224,7 @@ class receiveDataThread(QThread): return print 'the hash requested in this getpubkey request is:', requestedHash.encode('hex') - sqlLock.acquire() + """sqlLock.acquire() t = (requestedHash,int(time.time())-lengthOfTimeToHoldOnToAllPubkeys) #this prevents SQL injection sqlSubmitQueue.put('''SELECT hash, transmitdata, time FROM pubkeys WHERE hash=? AND havecorrectnonce=1 AND time>?''') sqlSubmitQueue.put(t) @@ -1248,17 +1239,23 @@ class receiveDataThread(QThread): inventoryHash = calculateInventoryHash(payload) objectType = 'pubkey' 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. - self.broadcastinv(inventoryHash) - 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.) - if requestedHash in myECAddressHashes: #if this address hash is one of mine + self.broadcastinv(inventoryHash)""" + #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.) + if requestedHash in myAddressesByHash: #if this address hash is one of mine + try: + lastPubkeySendTime = int(config.get(myAddressesByHash[requestedHash],'lastpubkeysendtime')) + except: + lastPubkeySendTime = 0 + print 'lastPubkeySendTime is',lastPubkeySendTime + if lastPubkeySendTime < time.time()-lengthOfTimeToHoldOnToAllPubkeys: #If the last time we sent our pubkey was 28 days ago printLock.acquire() 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.' printLock.release() workerQueue.put(('doPOWForMyV2Pubkey',requestedHash)) - else: - printLock.acquire() - print 'This getpubkey request is not for any of my keys.' - printLock.release() + else: + printLock.acquire() + print 'This getpubkey request is not for any of my keys.' + printLock.release() #We have received an inv message @@ -1930,8 +1927,8 @@ def reloadMyAddressHashes(): printLock.acquire() print 'reloading keys from keys.dat file' printLock.release() - myRSAAddressHashes.clear() - myECAddressHashes.clear() + myECCryptorObjects.clear() + myAddressesByHash.clear() #myPrivateKeys.clear() configSections = config.sections() for addressInKeysFile in configSections: @@ -1942,14 +1939,10 @@ def reloadMyAddressHashes(): 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) + myECCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey) + myAddressesByHash[hash] = addressInKeysFile + else: + sys.stderr.write('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2.\n') #This function expects that pubkey begin with \x04 def calculateBitcoinAddressFromPubkey(pubkey): @@ -1992,10 +1985,7 @@ def calculateTestnetAddressFromPubkey(pubkey): def safeConfigGetBoolean(section,field): try: - if config.getboolean(section,field): - return True - else: - return False + return config.getboolean(section,field) except: return False @@ -2339,14 +2329,15 @@ class singleWorker(QThread): def doPOWForMyV2Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW #Look up my stream number based on my address hash - configSections = config.sections() + """configSections = config.sections() for addressInKeysFile in configSections: if addressInKeysFile <> 'bitmessagesettings': status,addressVersionNumber,streamNumber,hashFromThisParticularAddress = decodeAddress(addressInKeysFile) if hash == hashFromThisParticularAddress: myAddress = addressInKeysFile - break - + break""" + myAddress = myAddressesByHash[hash] + status,addressVersionNumber,streamNumber,hash = decodeAddress(myAddress) embeddedTime = int(time.time()+random.randrange(-300, 300)) #the current time plus or minus five minutes payload = pack('>I',(embeddedTime)) payload += encodeVarint(addressVersionNumber) #Address version number @@ -2382,13 +2373,13 @@ class singleWorker(QThread): print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce payload = pack('>Q',nonce) + payload - t = (hash,True,payload,embeddedTime,'no') + """t = (hash,True,payload,embeddedTime,'no') sqlLock.acquire() sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') sqlSubmitQueue.put(t) queryreturn = sqlReturnQueue.get() sqlSubmitQueue.put('commit') - sqlLock.release() + sqlLock.release()""" inventoryHash = calculateInventoryHash(payload) objectType = 'pubkey' @@ -2399,6 +2390,9 @@ class singleWorker(QThread): printLock.release() broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") + config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) + with open(appdata + 'keys.dat', 'wb') as configfile: + config.write(configfile) def sendBroadcast(self): sqlLock.acquire() @@ -5139,8 +5133,8 @@ class myTableWidgetItem(QTableWidgetItem): selfInitiatedConnections = {} #This is a list of current connections (the thread pointers at least) alreadyAttemptedConnectionsList = {} #This is a list of nodes to which we have already attempted a connection sendDataQueues = [] #each sendData thread puts its queue in this list. -myRSAAddressHashes = {} -myECAddressHashes = {} +myECCryptorObjects = {} +myAddressesByHash = {} #The key in this dictionary is the hash encoded in an address and value is the address itself. #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() diff --git a/src/messages.dat reader.py b/src/messages.dat reader.py index 45c608ea..7dc7a660 100644 --- a/src/messages.dat reader.py +++ b/src/messages.dat reader.py @@ -96,13 +96,22 @@ def markAllInboxMessagesAsUnread(): conn.commit() print 'done' +def vacuum(): + item = '''VACUUM''' + parameters = '' + cur.execute(item, parameters) + output = cur.fetchall() + conn.commit() + print 'done' + #takeInboxMessagesOutOfTrash() #takeSentMessagesOutOfTrash() #markAllInboxMessagesAsUnread() -readInbox() +#readInbox() #readSent() #readPubkeys() #readSubscriptions() #readInventory() +vacuum() #will defragment and clean empty space from the messages.dat file. From db906e2b111c8515805223aea3f914808a27d536 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Mon, 22 Apr 2013 16:39:43 -0400 Subject: [PATCH 02/33] Display privacy warning when Broadcast is selected on Send tab --- src/bitmessagemain.py | 4 +- src/bitmessageui.py | 126 +++++++++++++------------ src/bitmessageui.ui | 212 ++++++++++++++++++++++++------------------ 3 files changed, 193 insertions(+), 149 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index ff384f79..5863eeaf 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -72,7 +72,7 @@ class outgoingSynSender(QThread): time.sleep(1) global alreadyAttemptedConnectionsListResetTime while True: - #time.sleep(999999)#I sometimes use this to prevent connections for testing. + time.sleep(999999)#I sometimes use this to prevent connections for testing. if len(selfInitiatedConnections[self.streamNumber]) < 8: #maximum number of outgoing connections = 8 random.seed() HOST, = random.sample(knownNodes[self.streamNumber], 1) @@ -3439,6 +3439,8 @@ class MyForm(QtGui.QMainWindow): if 'darwin' in sys.platform: self.trayIcon.show() + self.ui.labelSendBroadcastWarning.setVisible(False) + #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) diff --git a/src/bitmessageui.py b/src/bitmessageui.py index 55e65e6c..20e5b973 100644 --- a/src/bitmessageui.py +++ b/src/bitmessageui.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'bitmessageui.ui' # -# Created: Mon Apr 08 11:57:15 2013 +# Created: Mon Apr 22 16:34:47 2013 # by: PyQt4 UI code generator 4.9.4 # # WARNING! All changes made in this file will be lost! @@ -81,59 +81,66 @@ class Ui_MainWindow(object): self.send.setObjectName(_fromUtf8("send")) self.gridLayout_2 = QtGui.QGridLayout(self.send) self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2")) - self.lineEditTo = QtGui.QLineEdit(self.send) - self.lineEditTo.setObjectName(_fromUtf8("lineEditTo")) - self.gridLayout_2.addWidget(self.lineEditTo, 3, 1, 1, 1) - self.label_3 = QtGui.QLabel(self.send) - self.label_3.setObjectName(_fromUtf8("label_3")) - self.gridLayout_2.addWidget(self.label_3, 4, 0, 1, 1) - spacerItem = QtGui.QSpacerItem(20, 297, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) - self.gridLayout_2.addItem(spacerItem, 6, 0, 1, 1) - self.label_2 = QtGui.QLabel(self.send) - self.label_2.setObjectName(_fromUtf8("label_2")) - self.gridLayout_2.addWidget(self.label_2, 2, 0, 1, 1) - self.comboBoxSendFrom = QtGui.QComboBox(self.send) - self.comboBoxSendFrom.setObjectName(_fromUtf8("comboBoxSendFrom")) - self.gridLayout_2.addWidget(self.comboBoxSendFrom, 2, 1, 1, 1) - self.label_4 = QtGui.QLabel(self.send) - self.label_4.setObjectName(_fromUtf8("label_4")) - self.gridLayout_2.addWidget(self.label_4, 5, 0, 1, 1) - self.label = QtGui.QLabel(self.send) - self.label.setObjectName(_fromUtf8("label")) - self.gridLayout_2.addWidget(self.label, 3, 0, 1, 1) - self.radioButtonBroadcast = QtGui.QRadioButton(self.send) - self.radioButtonBroadcast.setObjectName(_fromUtf8("radioButtonBroadcast")) - self.gridLayout_2.addWidget(self.radioButtonBroadcast, 1, 1, 1, 3) self.pushButtonLoadFromAddressBook = QtGui.QPushButton(self.send) font = QtGui.QFont() font.setPointSize(7) self.pushButtonLoadFromAddressBook.setFont(font) self.pushButtonLoadFromAddressBook.setObjectName(_fromUtf8("pushButtonLoadFromAddressBook")) self.gridLayout_2.addWidget(self.pushButtonLoadFromAddressBook, 3, 2, 1, 2) - spacerItem1 = QtGui.QSpacerItem(28, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) - self.gridLayout_2.addItem(spacerItem1, 6, 6, 1, 1) - self.radioButtonSpecific = QtGui.QRadioButton(self.send) - self.radioButtonSpecific.setChecked(True) - self.radioButtonSpecific.setObjectName(_fromUtf8("radioButtonSpecific")) - self.gridLayout_2.addWidget(self.radioButtonSpecific, 0, 1, 1, 1) - spacerItem2 = QtGui.QSpacerItem(20, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) - self.gridLayout_2.addItem(spacerItem2, 3, 4, 1, 1) - self.pushButtonSend = QtGui.QPushButton(self.send) - self.pushButtonSend.setObjectName(_fromUtf8("pushButtonSend")) - self.gridLayout_2.addWidget(self.pushButtonSend, 7, 5, 1, 1) - spacerItem3 = QtGui.QSpacerItem(192, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) - self.gridLayout_2.addItem(spacerItem3, 7, 4, 1, 1) - self.lineEditSubject = QtGui.QLineEdit(self.send) - self.lineEditSubject.setText(_fromUtf8("")) - self.lineEditSubject.setObjectName(_fromUtf8("lineEditSubject")) - self.gridLayout_2.addWidget(self.lineEditSubject, 4, 1, 1, 5) - self.textEditMessage = QtGui.QTextEdit(self.send) - self.textEditMessage.setObjectName(_fromUtf8("textEditMessage")) - self.gridLayout_2.addWidget(self.textEditMessage, 5, 1, 2, 5) + self.label_4 = QtGui.QLabel(self.send) + self.label_4.setObjectName(_fromUtf8("label_4")) + self.gridLayout_2.addWidget(self.label_4, 5, 0, 1, 1) + self.comboBoxSendFrom = QtGui.QComboBox(self.send) + self.comboBoxSendFrom.setMinimumSize(QtCore.QSize(300, 0)) + self.comboBoxSendFrom.setObjectName(_fromUtf8("comboBoxSendFrom")) + self.gridLayout_2.addWidget(self.comboBoxSendFrom, 2, 1, 1, 1) + self.label_3 = QtGui.QLabel(self.send) + self.label_3.setObjectName(_fromUtf8("label_3")) + self.gridLayout_2.addWidget(self.label_3, 4, 0, 1, 1) self.labelFrom = QtGui.QLabel(self.send) self.labelFrom.setText(_fromUtf8("")) self.labelFrom.setObjectName(_fromUtf8("labelFrom")) self.gridLayout_2.addWidget(self.labelFrom, 2, 2, 1, 3) + self.radioButtonSpecific = QtGui.QRadioButton(self.send) + self.radioButtonSpecific.setChecked(True) + self.radioButtonSpecific.setObjectName(_fromUtf8("radioButtonSpecific")) + self.gridLayout_2.addWidget(self.radioButtonSpecific, 0, 1, 1, 1) + self.lineEditTo = QtGui.QLineEdit(self.send) + self.lineEditTo.setObjectName(_fromUtf8("lineEditTo")) + self.gridLayout_2.addWidget(self.lineEditTo, 3, 1, 1, 1) + self.textEditMessage = QtGui.QTextEdit(self.send) + self.textEditMessage.setObjectName(_fromUtf8("textEditMessage")) + self.gridLayout_2.addWidget(self.textEditMessage, 5, 1, 2, 5) + self.label = QtGui.QLabel(self.send) + self.label.setObjectName(_fromUtf8("label")) + self.gridLayout_2.addWidget(self.label, 3, 0, 1, 1) + self.label_2 = QtGui.QLabel(self.send) + self.label_2.setObjectName(_fromUtf8("label_2")) + self.gridLayout_2.addWidget(self.label_2, 2, 0, 1, 1) + spacerItem = QtGui.QSpacerItem(20, 297, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) + self.gridLayout_2.addItem(spacerItem, 6, 0, 1, 1) + self.radioButtonBroadcast = QtGui.QRadioButton(self.send) + self.radioButtonBroadcast.setObjectName(_fromUtf8("radioButtonBroadcast")) + self.gridLayout_2.addWidget(self.radioButtonBroadcast, 1, 1, 1, 3) + self.lineEditSubject = QtGui.QLineEdit(self.send) + self.lineEditSubject.setText(_fromUtf8("")) + self.lineEditSubject.setObjectName(_fromUtf8("lineEditSubject")) + self.gridLayout_2.addWidget(self.lineEditSubject, 4, 1, 1, 5) + spacerItem1 = QtGui.QSpacerItem(20, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.gridLayout_2.addItem(spacerItem1, 3, 4, 1, 1) + self.pushButtonSend = QtGui.QPushButton(self.send) + self.pushButtonSend.setObjectName(_fromUtf8("pushButtonSend")) + self.gridLayout_2.addWidget(self.pushButtonSend, 7, 5, 1, 1) + self.labelSendBroadcastWarning = QtGui.QLabel(self.send) + self.labelSendBroadcastWarning.setEnabled(True) + sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Preferred) + sizePolicy.setHorizontalStretch(0) + sizePolicy.setVerticalStretch(0) + sizePolicy.setHeightForWidth(self.labelSendBroadcastWarning.sizePolicy().hasHeightForWidth()) + self.labelSendBroadcastWarning.setSizePolicy(sizePolicy) + self.labelSendBroadcastWarning.setIndent(-1) + self.labelSendBroadcastWarning.setObjectName(_fromUtf8("labelSendBroadcastWarning")) + self.gridLayout_2.addWidget(self.labelSendBroadcastWarning, 7, 1, 1, 4) icon2 = QtGui.QIcon() icon2.addPixmap(QtGui.QPixmap(_fromUtf8(":/newPrefix/images/send.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.tabWidget.addTab(self.send, icon2, _fromUtf8("")) @@ -179,8 +186,8 @@ class Ui_MainWindow(object): self.pushButtonNewAddress = QtGui.QPushButton(self.youridentities) self.pushButtonNewAddress.setObjectName(_fromUtf8("pushButtonNewAddress")) self.gridLayout_3.addWidget(self.pushButtonNewAddress, 0, 0, 1, 1) - spacerItem4 = QtGui.QSpacerItem(689, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) - self.gridLayout_3.addItem(spacerItem4, 0, 1, 1, 1) + spacerItem2 = QtGui.QSpacerItem(689, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.gridLayout_3.addItem(spacerItem2, 0, 1, 1, 1) self.tableWidgetYourIdentities = QtGui.QTableWidget(self.youridentities) self.tableWidgetYourIdentities.setFrameShadow(QtGui.QFrame.Sunken) self.tableWidgetYourIdentities.setLineWidth(1) @@ -223,8 +230,8 @@ class Ui_MainWindow(object): self.pushButtonAddSubscription = QtGui.QPushButton(self.subscriptions) self.pushButtonAddSubscription.setObjectName(_fromUtf8("pushButtonAddSubscription")) self.gridLayout_4.addWidget(self.pushButtonAddSubscription, 1, 0, 1, 1) - spacerItem5 = QtGui.QSpacerItem(689, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) - self.gridLayout_4.addItem(spacerItem5, 1, 1, 1, 1) + spacerItem3 = QtGui.QSpacerItem(689, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.gridLayout_4.addItem(spacerItem3, 1, 1, 1, 1) self.tableWidgetSubscriptions = QtGui.QTableWidget(self.subscriptions) self.tableWidgetSubscriptions.setAlternatingRowColors(True) self.tableWidgetSubscriptions.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) @@ -257,8 +264,8 @@ class Ui_MainWindow(object): self.pushButtonAddAddressBook = QtGui.QPushButton(self.addressbook) self.pushButtonAddAddressBook.setObjectName(_fromUtf8("pushButtonAddAddressBook")) self.gridLayout_5.addWidget(self.pushButtonAddAddressBook, 1, 0, 1, 1) - spacerItem6 = QtGui.QSpacerItem(689, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) - self.gridLayout_5.addItem(spacerItem6, 1, 1, 1, 1) + spacerItem4 = QtGui.QSpacerItem(689, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.gridLayout_5.addItem(spacerItem4, 1, 1, 1, 1) self.tableWidgetAddressBook = QtGui.QTableWidget(self.addressbook) self.tableWidgetAddressBook.setAlternatingRowColors(True) self.tableWidgetAddressBook.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) @@ -293,8 +300,8 @@ class Ui_MainWindow(object): self.pushButtonAddBlacklist = QtGui.QPushButton(self.blackwhitelist) self.pushButtonAddBlacklist.setObjectName(_fromUtf8("pushButtonAddBlacklist")) self.gridLayout_6.addWidget(self.pushButtonAddBlacklist, 2, 0, 1, 1) - spacerItem7 = QtGui.QSpacerItem(689, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) - self.gridLayout_6.addItem(spacerItem7, 2, 1, 1, 1) + spacerItem5 = QtGui.QSpacerItem(689, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.gridLayout_6.addItem(spacerItem5, 2, 1, 1, 1) self.tableWidgetBlacklist = QtGui.QTableWidget(self.blackwhitelist) self.tableWidgetBlacklist.setAlternatingRowColors(True) self.tableWidgetBlacklist.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) @@ -418,6 +425,8 @@ class Ui_MainWindow(object): self.retranslateUi(MainWindow) self.tabWidget.setCurrentIndex(0) QtCore.QObject.connect(self.radioButtonSpecific, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.lineEditTo.setEnabled) + QtCore.QObject.connect(self.radioButtonSpecific, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.labelSendBroadcastWarning.hide) + QtCore.QObject.connect(self.radioButtonBroadcast, QtCore.SIGNAL(_fromUtf8("clicked()")), self.labelSendBroadcastWarning.show) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): @@ -432,19 +441,20 @@ class Ui_MainWindow(object): item = self.tableWidgetInbox.horizontalHeaderItem(3) item.setText(QtGui.QApplication.translate("MainWindow", "Received", None, QtGui.QApplication.UnicodeUTF8)) self.tabWidget.setTabText(self.tabWidget.indexOf(self.inbox), QtGui.QApplication.translate("MainWindow", "Inbox", None, QtGui.QApplication.UnicodeUTF8)) - self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Subject:", None, QtGui.QApplication.UnicodeUTF8)) - self.label_2.setText(QtGui.QApplication.translate("MainWindow", "From:", None, QtGui.QApplication.UnicodeUTF8)) - self.label_4.setText(QtGui.QApplication.translate("MainWindow", "Message:", None, QtGui.QApplication.UnicodeUTF8)) - self.label.setText(QtGui.QApplication.translate("MainWindow", "To:", None, QtGui.QApplication.UnicodeUTF8)) - self.radioButtonBroadcast.setText(QtGui.QApplication.translate("MainWindow", "Broadcast to everyone who is subscribed to your address", None, QtGui.QApplication.UnicodeUTF8)) self.pushButtonLoadFromAddressBook.setText(QtGui.QApplication.translate("MainWindow", "Load from Address book", None, QtGui.QApplication.UnicodeUTF8)) + self.label_4.setText(QtGui.QApplication.translate("MainWindow", "Message:", None, QtGui.QApplication.UnicodeUTF8)) + self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Subject:", None, QtGui.QApplication.UnicodeUTF8)) self.radioButtonSpecific.setText(QtGui.QApplication.translate("MainWindow", "Send to one or more specific people", None, QtGui.QApplication.UnicodeUTF8)) - self.pushButtonSend.setText(QtGui.QApplication.translate("MainWindow", "Send", None, QtGui.QApplication.UnicodeUTF8)) self.textEditMessage.setHtml(QtGui.QApplication.translate("MainWindow", "\n" "\n" "


", None, QtGui.QApplication.UnicodeUTF8)) + self.label.setText(QtGui.QApplication.translate("MainWindow", "To:", None, QtGui.QApplication.UnicodeUTF8)) + self.label_2.setText(QtGui.QApplication.translate("MainWindow", "From:", None, QtGui.QApplication.UnicodeUTF8)) + self.radioButtonBroadcast.setText(QtGui.QApplication.translate("MainWindow", "Broadcast to everyone who is subscribed to your address", None, QtGui.QApplication.UnicodeUTF8)) + self.pushButtonSend.setText(QtGui.QApplication.translate("MainWindow", "Send", None, QtGui.QApplication.UnicodeUTF8)) + self.labelSendBroadcastWarning.setText(QtGui.QApplication.translate("MainWindow", "Be aware that broadcasts are only encrypted with your address. Anyone who knows your address can read them.", None, QtGui.QApplication.UnicodeUTF8)) self.tabWidget.setTabText(self.tabWidget.indexOf(self.send), QtGui.QApplication.translate("MainWindow", "Send", None, QtGui.QApplication.UnicodeUTF8)) self.tableWidgetSent.setSortingEnabled(True) item = self.tableWidgetSent.horizontalHeaderItem(0) diff --git a/src/bitmessageui.ui b/src/bitmessageui.ui index 2ee477c1..e1b1e319 100644 --- a/src/bitmessageui.ui +++ b/src/bitmessageui.ui @@ -152,8 +152,34 @@ Send - - + + + + + 7 + + + + Load from Address book + + + + + + + Message: + + + + + + + + 300 + 0 + + + @@ -162,6 +188,51 @@ + + + + + + + + + + + Send to one or more specific people + + + true + + + + + + + + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> + + + + + + + To: + + + + + + + From: + + + @@ -175,30 +246,6 @@ - - - - From: - - - - - - - - - - Message: - - - - - - - To: - - - @@ -206,38 +253,10 @@ - - - - - 7 - - + + - Load from Address book - - - - - - - Qt::Horizontal - - - - 28 - 20 - - - - - - - - Send to one or more specific people - - - true + @@ -261,41 +280,22 @@ - - - - Qt::Horizontal + + + + true - - - 192 - 20 - + + + 0 + 0 + - - - - - + Be aware that broadcasts are only encrypted with your address. Anyone who knows your address can read them. - - - - - - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> -<html><head><meta name="qrichtext" content="1" /><style type="text/css"> -p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> - - - - - - - + + -1 @@ -1018,8 +1018,40 @@ p, li { white-space: pre-wrap; } 60 - 133 - 149 + 175 + 147 + + + + + radioButtonSpecific + clicked(bool) + labelSendBroadcastWarning + hide() + + + 95 + 59 + + + 129 + 528 + + + + + radioButtonBroadcast + clicked() + labelSendBroadcastWarning + show() + + + 108 + 84 + + + 177 + 519 From 6737a21d1c5a94985e951380e564367323e970f8 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Tue, 23 Apr 2013 15:59:10 -0400 Subject: [PATCH 03/33] add .dat files to .gitignore --- .gitignore | 1 + src/bitmessagemain.py | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 34cba6df..36fbec0d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ **pyc +**dat \ No newline at end of file diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 5863eeaf..cc6ac791 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -72,7 +72,7 @@ class outgoingSynSender(QThread): time.sleep(1) global alreadyAttemptedConnectionsListResetTime while True: - time.sleep(999999)#I sometimes use this to prevent connections for testing. + #time.sleep(999999)#I sometimes use this to prevent connections for testing. if len(selfInitiatedConnections[self.streamNumber]) < 8: #maximum number of outgoing connections = 8 random.seed() HOST, = random.sample(knownNodes[self.streamNumber], 1) @@ -1142,8 +1142,6 @@ class receiveDataThread(QThread): sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - printLock.acquire() - printLock.release() workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) else: print 'We have NOT used this pubkey personally. Inserting in database.' @@ -2161,7 +2159,6 @@ class singleCleaner(QThread): timeWeLastClearedInventoryAndPubkeysTables = 0 while True: - time.sleep(300) sqlLock.acquire() self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing housekeeping (Flushing inventory in memory to disk...)") for hash, storedValue in inventory.items(): @@ -2223,7 +2220,7 @@ class singleCleaner(QThread): self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing work necessary to again attempt to deliver a message...") sqlSubmitQueue.put('commit') sqlLock.release() - + time.sleep(300) #This thread, of which there is only one, does the heavy lifting: calculating POWs. class singleWorker(QThread): From 9bac0b5311c99525065380474de94c10c88c3303 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Wed, 24 Apr 2013 15:48:46 -0400 Subject: [PATCH 04/33] First bit of code necessary for version 3 addresses --- src/addresses.py | 4 +- src/bitmessagemain.py | 284 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 232 insertions(+), 56 deletions(-) diff --git a/src/addresses.py b/src/addresses.py index 402f3a7d..6700868d 100644 --- a/src/addresses.py +++ b/src/addresses.py @@ -162,7 +162,7 @@ def decodeAddress(address): #print 'addressVersionNumber', addressVersionNumber #print 'bytesUsedByVersionNumber', bytesUsedByVersionNumber - if addressVersionNumber > 2: + if addressVersionNumber > 3: print 'cannot decode address version numbers this high' status = 'versiontoohigh' return status,0,0,0 @@ -176,7 +176,7 @@ def decodeAddress(address): status = 'success' if addressVersionNumber == 1: return status,addressVersionNumber,streamNumber,data[-24:-4] - elif addressVersionNumber == 2: + elif addressVersionNumber == 2 or addressVersionNumber == 3: if len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) == 19: return status,addressVersionNumber,streamNumber,'\x00'+data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4] elif len(data[bytesUsedByVersionNumber+bytesUsedByStreamNumber:-4]) == 20: diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index cc6ac791..309121bf 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -409,8 +409,8 @@ class receiveDataThread(QThread): def isProofOfWorkSufficient(self,data): POW, = unpack('>Q',hashlib.sha512(hashlib.sha512(data[:8]+ hashlib.sha512(data[8:]).digest()).digest()).digest()[0:8]) #print 'POW:', POW - #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 / ((len(data)+payloadLengthExtraBytes) * (averageProofOfWorkNonceTrialsPerByte/2)) + #Notice that I have divided the networkDefaultAverageProofOfWorkNonceTrialsPerByte 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 / ((len(data)+networkDefaultPayloadLengthExtraBytes) * (networkDefaultAverageProofOfWorkNonceTrialsPerByte/2)) def sendpong(self): print 'Sending pong' @@ -814,7 +814,7 @@ class receiveDataThread(QThread): if sendersAddressVersionNumber == 0: print 'Cannot understand sendersAddressVersionNumber = 0. Ignoring message.' return - if sendersAddressVersionNumber >= 3: + if sendersAddressVersionNumber >= 4: print 'Sender\'s address version number', sendersAddressVersionNumber, ' not yet supported. Ignoring message.' return if len(unencryptedData) < 170: @@ -831,6 +831,13 @@ class receiveDataThread(QThread): readPosition += 64 pubEncryptionKey = '\x04' + unencryptedData[readPosition:readPosition+64] readPosition += 64 + if sendersAddressVersionNumber >= 3: + requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + readPosition += varintLength + print 'sender\'s requiredAverageProofOfWorkNonceTrialsPerByte is', requiredAverageProofOfWorkNonceTrialsPerByte + requiredPayloadLengthExtraBytes, varintLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + readPosition += varintLength + print 'sender\'s requiredPayloadLengthExtraBytes is', requiredPayloadLengthExtraBytes endOfThePublicKeyPosition = readPosition #needed for when we store the pubkey in our database of pubkeys for later use. if toRipe != unencryptedData[readPosition:readPosition+20]: printLock.acquire() @@ -1096,7 +1103,7 @@ class receiveDataThread(QThread): if addressVersion == 0: print '(Within processpubkey) addressVersion of 0 doesn\'t make sense.' return - if addressVersion >= 3 or addressVersion == 1: + if addressVersion >= 4 or addressVersion == 1: printLock.acquire() print 'This version of Bitmessage cannot handle version', addressVersion,'addresses.' printLock.release() @@ -1155,6 +1162,74 @@ class receiveDataThread(QThread): printLock.acquire() printLock.release() workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) + if addressVersion == 3: + if len(data) < 170: #sanity check. + print '(within processpubkey) payloadLength less than 170. Sanity check failed.' + return + bitfieldBehaviors = data[readPosition:readPosition+4] + readPosition += 4 + publicSigningKey = '\x04'+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 = '\x04'+data[readPosition:readPosition+64] + """if len(publicEncryptionKey) < 64: + print 'publicEncryptionKey length less than 64. Sanity check failed.' + return""" + readPosition += 64 + specifiedNonceTrialsPerByte, specifiedNonceTrialsPerByteLength = decodeVarint(data[readPosition:readPosition+10]) + readPosition += specifiedNonceTrialsPerByteLength + specifiedPayloadLengthExtraBytes, specifiedPayloadLengthExtraBytesLength = decodeVarint(data[readPosition:readPosition+10]) + readPosition += specifiedPayloadLengthExtraBytesLength + signatureLength, signatureLengthLength = decodeVarint(data[readPosition:readPosition+10]) + signature = data[readPosition:readPosition+signatureLengthLength] + try: + highlevelcrypto.verify(data[8:readPosition],signature,publicSigningKey.encode('hex')) + print 'ECDSA verify passed (within processpubkey)' + except Exception, err: + print 'ECDSA verify failed (within processpubkey)', err + return + + sha = hashlib.new('sha512') + sha.update(publicSigningKey+publicEncryptionKey) + ripeHasher = hashlib.new('ripemd160') + ripeHasher.update(sha.digest()) + ripe = ripeHasher.digest() + + printLock.acquire() + print 'within recpubkey, addressVersion:', addressVersion, ', streamNumber:', streamNumber + print 'ripe', ripe.encode('hex') + print 'publicSigningKey in hex:', publicSigningKey.encode('hex') + print 'publicEncryptionKey in hex:', publicEncryptionKey.encode('hex') + printLock.release() + + t = (ripe,) + sqlLock.acquire() + sqlSubmitQueue.put('''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''') + sqlSubmitQueue.put(t) + queryreturn = sqlReturnQueue.get() + sqlLock.release() + 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,data,embeddedTime,'yes') + sqlLock.acquire() + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put(t) + sqlReturnQueue.get() + sqlSubmitQueue.put('commit') + sqlLock.release() + workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) + else: + print 'We have NOT used this pubkey personally. Inserting in database.' + t = (ripe,True,data,embeddedTime,'no') #This will also update the embeddedTime. + sqlLock.acquire() + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put(t) + sqlReturnQueue.get() + sqlSubmitQueue.put('commit') + sqlLock.release() + printLock.acquire() + printLock.release() + workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) #We have received a getpubkey message @@ -1181,7 +1256,7 @@ class receiveDataThread(QThread): if embeddedTime < int(time.time())-maximumAgeOfAnObjectThatIAmWillingToAccept: print 'The time in this getpubkey message is too old. Ignoring it. Time:', embeddedTime return - addressVersionNumber, addressVersionLength = decodeVarint(data[readPosition:readPosition+10]) + requestedAddressVersionNumber, addressVersionLength = decodeVarint(data[readPosition:readPosition+10]) readPosition += addressVersionLength streamNumber, streamNumberLength = decodeVarint(data[readPosition:readPosition+10]) if streamNumber <> self.streamNumber: @@ -1206,14 +1281,14 @@ class receiveDataThread(QThread): #This getpubkey request is valid so far. Forward to peers. self.broadcastinv(inventoryHash) - if addressVersionNumber == 0: - print 'The addressVersionNumber of the pubkey request is zero. That doesn\'t make any sense. Ignoring it.' + if requestedAddressVersionNumber == 0: + print 'The requestedAddressVersionNumber of the pubkey request is zero. That doesn\'t make any sense. Ignoring it.' return - elif addressVersionNumber == 1: - print 'The addressVersionNumber of the pubkey request is 1 which isn\'t supported anymore. Ignoring it.' + elif requestedAddressVersionNumber == 1: + print 'The requestedAddressVersionNumber of the pubkey request is 1 which isn\'t supported anymore. Ignoring it.' return - elif addressVersionNumber > 2: - print 'The addressVersionNumber of the pubkey request is too high. Can\'t understand. Ignoring it.' + elif requestedAddressVersionNumber > 3: + print 'The requestedAddressVersionNumber of the pubkey request is too high. Can\'t understand. Ignoring it.' return requestedHash = data[readPosition:readPosition+20] @@ -1240,16 +1315,27 @@ class receiveDataThread(QThread): self.broadcastinv(inventoryHash)""" #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.) if requestedHash in myAddressesByHash: #if this address hash is one of mine + if decodeAddress(myAddressesByHash[requestedHash])[1] != requestedAddressVersionNumber: + printLock.acquire() + sys.stderr.write('(Within the recgetpubkey function) Someone requested one of my pubkeys but the requestedAddressVersionNumber doesn\'t match my actual address version number. That shouldn\'t have happened. Ignoring.\n') + printLock.release() + return try: lastPubkeySendTime = int(config.get(myAddressesByHash[requestedHash],'lastpubkeysendtime')) except: lastPubkeySendTime = 0 - print 'lastPubkeySendTime is',lastPubkeySendTime if lastPubkeySendTime < time.time()-lengthOfTimeToHoldOnToAllPubkeys: #If the last time we sent our pubkey was 28 days ago printLock.acquire() 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.' printLock.release() - workerQueue.put(('doPOWForMyV2Pubkey',requestedHash)) + if requestedAddressVersionNumber == 2: + workerQueue.put(('doPOWForMyV2Pubkey',requestedHash)) + elif requestedAddressVersionNumber == 3: + workerQueue.put(('doPOWForMyV3Pubkey',requestedHash)) + else: + printLock.acquire() + print 'Found getpubkey-requested-hash in my list of EC hashes BUT we already sent it recently. Ignoring request. The lastPubkeySendTime is:',lastPubkeySendTime + printLock.release() else: printLock.acquire() print 'This getpubkey request is not for any of my keys.' @@ -1934,13 +2020,13 @@ def reloadMyAddressHashes(): isEnabled = config.getboolean(addressInKeysFile, 'enabled') if isEnabled: status,addressVersionNumber,streamNumber,hash = decodeAddress(addressInKeysFile) - if addressVersionNumber == 2: + if addressVersionNumber == 2 or addressVersionNumber == 3: 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 myECCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey) myAddressesByHash[hash] = addressInKeysFile else: - sys.stderr.write('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2.\n') + sys.stderr.write('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2 or 3.\n') #This function expects that pubkey begin with \x04 def calculateBitcoinAddressFromPubkey(pubkey): @@ -2307,6 +2393,8 @@ class singleWorker(QThread): self.sendBroadcast() elif command == 'doPOWForMyV2Pubkey': self.doPOWForMyV2Pubkey(data) + elif command == 'doPOWForMyV3Pubkey': + self.doPOWForMyV3Pubkey(data) elif command == 'newpubkey': toAddressVersion,toStreamNumber,toRipe = data if toRipe in neededPubkeys: @@ -2361,7 +2449,80 @@ class singleWorker(QThread): #Do the POW for this pubkey message nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+payloadLengthExtraBytes+8) * averageProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) + 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 = (hash,True,payload,embeddedTime,'no') + sqlLock.acquire() + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put(t) + queryreturn = sqlReturnQueue.get() + sqlSubmitQueue.put('commit') + sqlLock.release()""" + + inventoryHash = calculateInventoryHash(payload) + objectType = 'pubkey' + inventory[inventoryHash] = (objectType, streamNumber, payload, embeddedTime) + + printLock.acquire() + print 'broadcasting inv with hash:', inventoryHash.encode('hex') + printLock.release() + broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) + self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") + config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) + with open(appdata + 'keys.dat', 'wb') as configfile: + config.write(configfile) + + def doPOWForMyV3Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW + #Look up my stream number based on my address hash + """configSections = config.sections() + for addressInKeysFile in configSections: + if addressInKeysFile <> 'bitmessagesettings': + status,addressVersionNumber,streamNumber,hashFromThisParticularAddress = decodeAddress(addressInKeysFile) + if hash == hashFromThisParticularAddress: + myAddress = addressInKeysFile + break""" + myAddress = myAddressesByHash[hash] + status,addressVersionNumber,streamNumber,hash = decodeAddress(myAddress) + embeddedTime = int(time.time()+random.randrange(-300, 300)) #the current time plus or minus five minutes + payload = pack('>I',(embeddedTime)) + payload += encodeVarint(addressVersionNumber) #Address version number + payload += encodeVarint(streamNumber) + payload += '\x00\x00\x00\x01' #bitfield of features supported by me (see the wiki). + + try: + privSigningKeyBase58 = config.get(myAddress, 'privsigningkey') + privEncryptionKeyBase58 = config.get(myAddress, 'privencryptionkey') + except Exception, err: + printLock.acquire() + sys.stderr.write('Error within doPOWForMyV3Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err) + printLock.release() + return + + 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:] + + payload += encodeVarint(networkDefaultAverageProofOfWorkNonceTrialsPerByte) #this is where we would multiply networkDefaultAverageProofOfWorkNonceTrialsPerByte by some difficulty set by the user. + payload += encodeVarint(networkDefaultPayloadLengthExtraBytes) #this is where we would multiply networkDefaultPayloadLengthExtraBytes by some difficulty set by the user. + signature = highlevelcrypto.sign(payload,privSigningKeyHex) + payload += encodeVarint(len(signature)) + payload += signature + + #Do the POW for this pubkey message + nonce = 0 + trialValue = 99999999999999999999 + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) print '(For pubkey message) Doing proof of work...' initialHash = hashlib.sha512(payload).digest() while trialValue > target: @@ -2434,7 +2595,7 @@ class singleWorker(QThread): nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+payloadLengthExtraBytes+8) * averageProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) print '(For broadcast message) Doing proof of work...' self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') initialHash = hashlib.sha512(payload).digest() @@ -2525,44 +2686,47 @@ class singleWorker(QThread): payload += encodeVarint(len(signature)) payload += signature - """elif fromAddressVersionNumber == 1: #This code is for old version 1 (RSA) addresses. It will soon be removed. - 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' - + if fromAddressVersionNumber == 3: + payload = '\x01' #Message version. payload += encodeVarint(fromAddressVersionNumber) payload += encodeVarint(fromStreamNumber) + 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 ) + #We need to convert our private keys to public keys in order to include them. try: - sendersN = convertIntToString(config.getint(fromaddress, 'n')) + privSigningKeyBase58 = config.get(fromaddress, 'privsigningkey') + privEncryptionKeyBase58 = config.get(fromaddress, 'privencryptionkey') 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 - payload += encodeVarint(len(sendersN)) - payload += sendersN + self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Error! Could not find sender address (your address) in the keys.dat file.') + continue - sendersE = convertIntToString(config.getint(fromaddress, 'e')) - payload += encodeVarint(len(sendersE)) - payload += sendersE + privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') + privEncryptionKeyHex = decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') - payload += '\x02' #Type 2 is simple UTF-8 message encoding. + pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode('hex') + pubEncryptionKey = highlevelcrypto.privToPub(privEncryptionKeyHex).decode('hex') + + 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. + payload += pubEncryptionKey[1:] + payload += encodeVarint(networkDefaultAverageProofOfWorkNonceTrialsPerByte) #this is where we would multiply networkDefaultAverageProofOfWorkNonceTrialsPerByte by some difficulty we demand others meet. + payload += encodeVarint(networkDefaultPayloadLengthExtraBytes) #this is where we would multiply networkDefaultPayloadLengthExtraBytes by some difficulty set by some difficulty we demand others meet. + + 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. messageToTransmit = 'Subject:' + subject + '\n' + 'Body:' + message payload += encodeVarint(len(messageToTransmit)) payload += messageToTransmit - - #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. - fullAckPayload = self.generateFullAckMessage(ackdata,toStreamNumber,embeddedTime) + 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. 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')) + signature = highlevelcrypto.sign(payload,privSigningKeyHex) + payload += encodeVarint(len(signature)) + payload += signature - payload += rsa.sign(payload,sendersPrivKey,'SHA-512')""" #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. - if toAddressVersionNumber == 2: + if toAddressVersionNumber == 2 or toAddressVersionNumber == 3: sqlLock.acquire() sqlSubmitQueue.put('SELECT transmitdata FROM pubkeys WHERE hash=?') sqlSubmitQueue.put((toRipe,)) @@ -2588,6 +2752,18 @@ class singleWorker(QThread): readPosition += 64 pubEncryptionKeyBase256 = pubkeyPayload[readPosition:readPosition+64] readPosition += 64 + if toAddressVersionNumber == 2: + requiredAverageProofOfWorkNonceTrialsPerByte = networkDefaultAverageProofOfWorkNonceTrialsPerByte + requiredPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes + elif toAddressVersionNumber == 3: + requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10]) + readPosition += varintLength + requiredPayloadLengthExtraBytes, varintLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10]) + readPosition += varintLength + if requiredAverageProofOfWorkNonceTrialsPerByte < networkDefaultAverageProofOfWorkNonceTrialsPerByte: #We still have to meet a minimum POW difficulty regardless of what they say is allowed in order to get our message to propagate through the network. + requiredAverageProofOfWorkNonceTrialsPerByte = networkDefaultAverageProofOfWorkNonceTrialsPerByte + if requiredPayloadLengthExtraBytes < networkDefaultPayloadLengthExtraBytes: + requiredPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes encrypted = highlevelcrypto.encrypt(payload,"04"+pubEncryptionKeyBase256.encode('hex')) nonce = 0 @@ -2596,7 +2772,7 @@ class singleWorker(QThread): 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) + target = 2**64 / ((len(payload)+requiredPayloadLengthExtraBytes+8) * requiredAverageProofOfWorkNonceTrialsPerByte) print '(For msg message) Doing proof of work. Target:', target powStartTime = time.time() initialHash = hashlib.sha512(payload).digest() @@ -2647,7 +2823,7 @@ class singleWorker(QThread): 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) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) initialHash = hashlib.sha512(payload).digest() while trialValue > target: nonce += 1 @@ -2671,7 +2847,7 @@ class singleWorker(QThread): trialValue = 99999999999999999999 encodedStreamNumber = encodeVarint(toStreamNumber) payload = embeddedTime + encodedStreamNumber + ackdata - target = 2**64 / ((len(payload)+payloadLengthExtraBytes+8) * averageProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) printLock.acquire() print '(For ack message) Doing proof of work...' printLock.release() @@ -2707,7 +2883,7 @@ class addressGenerator(QThread): self.eighteenByteRipe = eighteenByteRipe def run(self): - if self.addressVersionNumber == 2: + if self.addressVersionNumber == 3: if self.deterministicPassphrase == "": statusbar = 'Generating one new address' @@ -2738,7 +2914,7 @@ class addressGenerator(QThread): break 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.' - address = encodeAddress(2,self.streamNumber,ripe.digest()) + address = encodeAddress(3,self.streamNumber,ripe.digest()) #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. @@ -2768,7 +2944,7 @@ class addressGenerator(QThread): self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address. Doing work necessary to broadcast it...') self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.label,address,str(self.streamNumber)) reloadMyAddressHashes() - workerQueue.put(('doPOWForMyV2Pubkey',ripe.digest())) + workerQueue.put(('doPOWForMyV3Pubkey',ripe.digest())) else: #There is something in the deterministicPassphrase variable thus we are going to do this deterministically. statusbar = 'Generating '+str(self.numberOfAddressesToMake) + ' new addresses.' @@ -2807,7 +2983,7 @@ class addressGenerator(QThread): print 'ripe.digest', ripe.digest().encode('hex') print 'Address generator calculated', numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix, 'addresses at', numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix/(time.time()-startTime),'keys per second.' - address = encodeAddress(2,self.streamNumber,ripe.digest()) + address = encodeAddress(3,self.streamNumber,ripe.digest()) #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. @@ -3219,7 +3395,7 @@ class singleAPISignalHandler(QThread): label, eighteenByteRipe = data streamNumberForAddress = 1 self.addressGenerator = addressGenerator() - self.addressGenerator.setup(2,streamNumberForAddress,label,1,"",eighteenByteRipe) + self.addressGenerator.setup(3,streamNumberForAddress,label,1,"",eighteenByteRipe) self.emit(SIGNAL("passAddressGeneratorObjectThrough(PyQt_PyObject)"),self.addressGenerator) self.addressGenerator.start() elif command == 'createDeterministicAddresses': @@ -4082,7 +4258,7 @@ class MyForm(QtGui.QMainWindow): continue except: pass - if addressVersionNumber > 2 or addressVersionNumber == 0: + if addressVersionNumber > 3 or addressVersionNumber == 0: 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.") continue if streamNumber > 1 or streamNumber == 0: @@ -4638,7 +4814,7 @@ class MyForm(QtGui.QMainWindow): 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()) + self.addressGenerator.setup(3,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() @@ -4650,7 +4826,7 @@ class MyForm(QtGui.QMainWindow): 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()) + self.addressGenerator.setup(3,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() @@ -5158,12 +5334,12 @@ apiAddressGeneratorReturnQueue = Queue.Queue() #The address generator thread use alreadyAttemptedConnectionsListResetTime = int(time.time()) #used to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect. #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. +networkDefaultAverageProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. +networkDefaultPayloadLengthExtraBytes = 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. if useVeryEasyProofOfWorkForTesting: - averageProofOfWorkNonceTrialsPerByte = averageProofOfWorkNonceTrialsPerByte / 16 - payloadLengthExtraBytes = payloadLengthExtraBytes / 7000 + networkDefaultAverageProofOfWorkNonceTrialsPerByte = networkDefaultAverageProofOfWorkNonceTrialsPerByte / 16 + networkDefaultPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes / 7000 if __name__ == "__main__": # Check the Major version, the first element in the array From c1f1b6b72cb655b1771b41664bd7ede7ac5b70a9 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 25 Apr 2013 16:11:00 -0400 Subject: [PATCH 05/33] continued working on v3 addresses --- src/bitmessagemain.py | 169 ++++++++++++++++++++++--------------- src/messages.dat reader.py | 13 ++- src/settings.py | 59 ++++++++++++- src/settings.ui | 121 +++++++++++++++++++++++++- 4 files changed, 285 insertions(+), 77 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 309121bf..7054ee73 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -72,7 +72,7 @@ class outgoingSynSender(QThread): time.sleep(1) global alreadyAttemptedConnectionsListResetTime while True: - #time.sleep(999999)#I sometimes use this to prevent connections for testing. + time.sleep(999999)#I sometimes use this to prevent connections for testing. if len(selfInitiatedConnections[self.streamNumber]) < 8: #maximum number of outgoing connections = 8 random.seed() HOST, = random.sample(knownNodes[self.streamNumber], 1) @@ -260,7 +260,7 @@ class receiveDataThread(QThread): while True: try: - self.data += self.sock.recv(65536) + self.data += self.sock.recv(4096) except socket.timeout: printLock.acquire() print 'Timeout occurred waiting for data. Closing receiveData thread.' @@ -307,6 +307,9 @@ class receiveDataThread(QThread): del connectedHostsList[self.HOST] except Exception, err: print 'Could not delete', self.HOST, 'from connectedHostsList.', err + printLock.acquire() + print 'The size of the connectedHostsList is now:', len(connectedHostsList) + printLock.release() def processData(self): global verbose @@ -409,8 +412,8 @@ class receiveDataThread(QThread): def isProofOfWorkSufficient(self,data): POW, = unpack('>Q',hashlib.sha512(hashlib.sha512(data[:8]+ hashlib.sha512(data[8:]).digest()).digest()).digest()[0:8]) #print 'POW:', POW - #Notice that I have divided the networkDefaultAverageProofOfWorkNonceTrialsPerByte 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 / ((len(data)+networkDefaultPayloadLengthExtraBytes) * (networkDefaultAverageProofOfWorkNonceTrialsPerByte/2)) + #Notice that I have divided the networkDefaultProofOfWorkNonceTrialsPerByte 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 / ((len(data)+networkDefaultPayloadLengthExtraBytes) * (networkDefaultProofOfWorkNonceTrialsPerByte/2)) def sendpong(self): print 'Sending pong' @@ -635,9 +638,9 @@ class receiveDataThread(QThread): #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'+data[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') + t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+data[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) sqlReturnQueue.get() sqlSubmitQueue.put('commit') @@ -815,7 +818,7 @@ class receiveDataThread(QThread): print 'Cannot understand sendersAddressVersionNumber = 0. Ignoring message.' return if sendersAddressVersionNumber >= 4: - print 'Sender\'s address version number', sendersAddressVersionNumber, ' not yet supported. Ignoring message.' + print 'Sender\'s address version number', sendersAddressVersionNumber, 'not yet supported. Ignoring message.' return if len(unencryptedData) < 170: print 'Length of the unencrypted data is unreasonably short. Sanity check failed. Ignoring message.' @@ -879,9 +882,9 @@ class receiveDataThread(QThread): 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.) - t = (ripe.digest(),False,'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+unencryptedData[messageVersionLength:endOfThePublicKeyPosition],int(time.time()),'yes') + t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+unencryptedData[messageVersionLength:endOfThePublicKeyPosition],int(time.time()),'yes') sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) sqlReturnQueue.get() sqlSubmitQueue.put('commit') @@ -1084,13 +1087,13 @@ class receiveDataThread(QThread): lengthOfTimeWeShouldUseToProcessThisMessage = .2 sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - (time.time()- self.pubkeyProcessingStartTime) if sleepTime > 0: - #printLock.acquire() - #print 'Timing attack mitigation: Sleeping for', sleepTime ,'seconds.' - #printLock.release() + 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() + printLock.acquire() + print 'Total pubkey processing time:', time.time()- self.pubkeyProcessingStartTime, 'seconds.' + printLock.release() def processpubkey(self,data): readPosition = 8 #for the nonce @@ -1142,9 +1145,9 @@ class receiveDataThread(QThread): sqlLock.release() 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,data,embeddedTime,'yes') + t = (ripe,data,embeddedTime,'yes') sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) sqlReturnQueue.get() sqlSubmitQueue.put('commit') @@ -1152,15 +1155,13 @@ class receiveDataThread(QThread): workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) else: print 'We have NOT used this pubkey personally. Inserting in database.' - t = (ripe,True,data,embeddedTime,'no') #This will also update the embeddedTime. + t = (ripe,data,embeddedTime,'no') #This will also update the embeddedTime. sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - printLock.acquire() - printLock.release() workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) if addressVersion == 3: if len(data) < 170: #sanity check. @@ -1172,9 +1173,6 @@ class receiveDataThread(QThread): #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 = '\x04'+data[readPosition:readPosition+64] - """if len(publicEncryptionKey) < 64: - print 'publicEncryptionKey length less than 64. Sanity check failed.' - return""" readPosition += 64 specifiedNonceTrialsPerByte, specifiedNonceTrialsPerByteLength = decodeVarint(data[readPosition:readPosition+10]) readPosition += specifiedNonceTrialsPerByteLength @@ -1210,26 +1208,23 @@ class receiveDataThread(QThread): sqlLock.release() 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,data,embeddedTime,'yes') + t = (ripe,data,embeddedTime,'yes') sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) else: print 'We have NOT used this pubkey personally. Inserting in database.' - t = (ripe,True,data,embeddedTime,'no') #This will also update the embeddedTime. + t = (ripe,data,embeddedTime,'no') #This will also update the embeddedTime. sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - printLock.acquire() - printLock.release() - workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) + workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) #We have received a getpubkey message @@ -2145,14 +2140,15 @@ class sqlThread(QThread): self.cur.execute( '''CREATE TABLE whitelist (label text, address text, enabled bool)''' ) #Explanation of what is in the pubkeys table: # The hash is the RIPEMD160 hash that is encoded in the Bitmessage address. - # 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. - self.cur.execute( '''CREATE TABLE pubkeys (hash blob, havecorrectnonce bool, transmitdata blob, time blob, usedpersonally text, UNIQUE(hash, havecorrectnonce) ON CONFLICT REPLACE)''' ) + self.cur.execute( '''CREATE TABLE pubkeys (hash blob, transmitdata blob, time int, usedpersonally text, UNIQUE(hash) ON CONFLICT REPLACE)''' ) self.cur.execute( '''CREATE TABLE inventory (hash blob, objecttype text, streamnumber int, payload blob, receivedtime integer, UNIQUE(hash) ON CONFLICT REPLACE)''' ) 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. self.cur.execute( '''INSERT INTO subscriptions VALUES('Bitmessage new releases/announcements','BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx',1)''') + self.cur.execute( '''CREATE TABLE settings (key blob, value blob, UNIQUE(key) ON CONFLICT REPLACE)''' ) + self.cur.execute( '''INSERT INTO settings VALUES('version','1')''') self.conn.commit() print 'Created messages database file' except Exception, err: @@ -2190,12 +2186,39 @@ class sqlThread(QThread): config.set('bitmessagesettings','settingsversion','4') with open(appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + config.write(configfile) + + if config.getint('bitmessagesettings','settingsversion') == 4: + config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) + config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) + config.set('bitmessagesettings','settingsversion','5') + with open(appdata + 'keys.dat', 'wb') as configfile: + config.write(configfile) + + #From now on, let us keep a 'version' embedded in the messages.dat file so that when we make changes to the database, the database version we are on can stay embedded in the messages.dat file. Let us check to see if the settings table exists yet. + item = '''SELECT name FROM sqlite_master WHERE type='table' AND name='settings';''' + parameters = '' + self.cur.execute(item, parameters) + if self.cur.fetchall() == []: + #The settings table doesn't exist. We need to make it. + print 'In messages.dat database, creating new \'settings\' table.' + self.cur.execute( '''CREATE TABLE settings (key text, value blob, UNIQUE(key) ON CONFLICT REPLACE)''' ) + self.cur.execute( '''INSERT INTO settings VALUES('version','1')''') + print 'In messages.dat database, removing an obsolete field from the pubkeys table.' + self.cur.execute( '''CREATE TEMPORARY TABLE pubkeys_backup(hash blob, transmitdata blob, time int, usedpersonally text, UNIQUE(hash) ON CONFLICT REPLACE);''') + self.cur.execute( '''INSERT INTO pubkeys_backup SELECT hash, transmitdata, time, usedpersonally FROM pubkeys;''') + self.cur.execute( '''DROP TABLE pubkeys''') + self.cur.execute( '''CREATE TABLE pubkeys (hash blob, transmitdata blob, time int, usedpersonally text, UNIQUE(hash) ON CONFLICT REPLACE)''' ) + self.cur.execute( '''INSERT INTO pubkeys SELECT hash, transmitdata, time, usedpersonally FROM pubkeys_backup;''') + self.cur.execute( '''DROP TABLE pubkeys_backup;''') + self.conn.commit() + print 'Vacuuming message.dat. You might notice that the file size gets much smaller.' + self.cur.execute( ''' VACUUM ''') try: testpayload = '\x00\x00' - t = ('1234','True',testpayload,'12345678','no') - self.cur.execute( '''INSERT INTO pubkeys VALUES(?,?,?,?,?)''',t) + t = ('1234',testpayload,'12345678','no') + self.cur.execute( '''INSERT INTO pubkeys VALUES(?,?,?,?)''',t) self.conn.commit() self.cur.execute('''SELECT transmitdata FROM pubkeys WHERE hash='1234' ''') queryreturn = self.cur.fetchall() @@ -2370,7 +2393,7 @@ class singleWorker(QThread): #print repr(message.toUtf8()) #print str(message.toUtf8()) sqlLock.acquire() - sqlSubmitQueue.put('SELECT * FROM pubkeys WHERE hash=?') + sqlSubmitQueue.put('SELECT hash FROM pubkeys WHERE hash=?') sqlSubmitQueue.put((toRipe,)) queryreturn = sqlReturnQueue.get() sqlLock.release() @@ -2449,7 +2472,7 @@ class singleWorker(QThread): #Do the POW for this pubkey message nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) print '(For pubkey message) Doing proof of work...' initialHash = hashlib.sha512(payload).digest() while trialValue > target: @@ -2458,9 +2481,9 @@ class singleWorker(QThread): print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce payload = pack('>Q',nonce) + payload - """t = (hash,True,payload,embeddedTime,'no') + """t = (hash,payload,embeddedTime,'no') sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) queryreturn = sqlReturnQueue.get() sqlSubmitQueue.put('commit') @@ -2480,14 +2503,6 @@ class singleWorker(QThread): config.write(configfile) def doPOWForMyV3Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW - #Look up my stream number based on my address hash - """configSections = config.sections() - for addressInKeysFile in configSections: - if addressInKeysFile <> 'bitmessagesettings': - status,addressVersionNumber,streamNumber,hashFromThisParticularAddress = decodeAddress(addressInKeysFile) - if hash == hashFromThisParticularAddress: - myAddress = addressInKeysFile - break""" myAddress = myAddressesByHash[hash] status,addressVersionNumber,streamNumber,hash = decodeAddress(myAddress) embeddedTime = int(time.time()+random.randrange(-300, 300)) #the current time plus or minus five minutes @@ -2513,8 +2528,8 @@ class singleWorker(QThread): payload += pubSigningKey[1:] payload += pubEncryptionKey[1:] - payload += encodeVarint(networkDefaultAverageProofOfWorkNonceTrialsPerByte) #this is where we would multiply networkDefaultAverageProofOfWorkNonceTrialsPerByte by some difficulty set by the user. - payload += encodeVarint(networkDefaultPayloadLengthExtraBytes) #this is where we would multiply networkDefaultPayloadLengthExtraBytes by some difficulty set by the user. + payload += encodeVarint(config.getint(myAddress,'noncetrialsperbyte')) + payload += encodeVarint(config.getint(myAddress,'payloadlengthextrabytes')) signature = highlevelcrypto.sign(payload,privSigningKeyHex) payload += encodeVarint(len(signature)) payload += signature @@ -2522,7 +2537,7 @@ class singleWorker(QThread): #Do the POW for this pubkey message nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) print '(For pubkey message) Doing proof of work...' initialHash = hashlib.sha512(payload).digest() while trialValue > target: @@ -2531,9 +2546,9 @@ class singleWorker(QThread): print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce payload = pack('>Q',nonce) + payload - """t = (hash,True,payload,embeddedTime,'no') + """t = (hash,payload,embeddedTime,'no') sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) queryreturn = sqlReturnQueue.get() sqlSubmitQueue.put('commit') @@ -2595,7 +2610,7 @@ class singleWorker(QThread): nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) print '(For broadcast message) Doing proof of work...' self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') initialHash = hashlib.sha512(payload).digest() @@ -2624,7 +2639,7 @@ class singleWorker(QThread): sqlLock.release() else: printLock.acquire() - print 'In the singleWorker thread, the sendBroadcast function doesn\'t understand the address version' + sys.stderr.write('Error: In the singleWorker thread, the sendBroadcast function doesn\'t understand the address version.\n') printLock.release() def sendMsg(self,toRipe): @@ -2708,8 +2723,8 @@ class singleWorker(QThread): 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. payload += pubEncryptionKey[1:] - payload += encodeVarint(networkDefaultAverageProofOfWorkNonceTrialsPerByte) #this is where we would multiply networkDefaultAverageProofOfWorkNonceTrialsPerByte by some difficulty we demand others meet. - payload += encodeVarint(networkDefaultPayloadLengthExtraBytes) #this is where we would multiply networkDefaultPayloadLengthExtraBytes by some difficulty set by some difficulty we demand others meet. + payload += encodeVarint(config.getint(fromaddress,'noncetrialsperbyte'))#todo: check and see whether the addressee is in our address book, subscription list, or whitelist and set lower POW requirement if yes. + payload += encodeVarint(config.getint(fromaddress,'payloadlengthextrabytes')) 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. @@ -2753,15 +2768,15 @@ class singleWorker(QThread): pubEncryptionKeyBase256 = pubkeyPayload[readPosition:readPosition+64] readPosition += 64 if toAddressVersionNumber == 2: - requiredAverageProofOfWorkNonceTrialsPerByte = networkDefaultAverageProofOfWorkNonceTrialsPerByte + requiredAverageProofOfWorkNonceTrialsPerByte = networkDefaultProofOfWorkNonceTrialsPerByte requiredPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes elif toAddressVersionNumber == 3: requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10]) readPosition += varintLength requiredPayloadLengthExtraBytes, varintLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10]) readPosition += varintLength - if requiredAverageProofOfWorkNonceTrialsPerByte < networkDefaultAverageProofOfWorkNonceTrialsPerByte: #We still have to meet a minimum POW difficulty regardless of what they say is allowed in order to get our message to propagate through the network. - requiredAverageProofOfWorkNonceTrialsPerByte = networkDefaultAverageProofOfWorkNonceTrialsPerByte + if requiredAverageProofOfWorkNonceTrialsPerByte < networkDefaultProofOfWorkNonceTrialsPerByte: #We still have to meet a minimum POW difficulty regardless of what they say is allowed in order to get our message to propagate through the network. + requiredAverageProofOfWorkNonceTrialsPerByte = networkDefaultProofOfWorkNonceTrialsPerByte if requiredPayloadLengthExtraBytes < networkDefaultPayloadLengthExtraBytes: requiredPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes encrypted = highlevelcrypto.encrypt(payload,"04"+pubEncryptionKeyBase256.encode('hex')) @@ -2773,7 +2788,13 @@ class singleWorker(QThread): #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)+requiredPayloadLengthExtraBytes+8) * requiredAverageProofOfWorkNonceTrialsPerByte) + printLock.acquire() print '(For msg message) Doing proof of work. Target:', target + print 'Using requiredAverageProofOfWorkNonceTrialsPerByte', requiredAverageProofOfWorkNonceTrialsPerByte + print 'Using requiredPayloadLengthExtraBytes =', requiredPayloadLengthExtraBytes + print 'The required total difficulty is', requiredAverageProofOfWorkNonceTrialsPerByte/networkDefaultProofOfWorkNonceTrialsPerByte + print 'The required small message difficulty is', requiredPayloadLengthExtraBytes/networkDefaultPayloadLengthExtraBytes + printLock.release() powStartTime = time.time() initialHash = hashlib.sha512(payload).digest() while trialValue > target: @@ -2823,7 +2844,7 @@ class singleWorker(QThread): 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)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) initialHash = hashlib.sha512(payload).digest() while trialValue > target: nonce += 1 @@ -2847,7 +2868,7 @@ class singleWorker(QThread): trialValue = 99999999999999999999 encodedStreamNumber = encodeVarint(toStreamNumber) payload = embeddedTime + encodedStreamNumber + ackdata - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultAverageProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) printLock.acquire() print '(For ack message) Doing proof of work...' printLock.release() @@ -2933,6 +2954,8 @@ class addressGenerator(QThread): config.set(address,'label',self.label) config.set(address,'enabled','true') config.set(address,'decoy','false') + config.set(address,'noncetrialsperbyte',config.get('bitmessagesettings','defaultnoncetrialsperbyte')) + config.set(address,'payloadlengthextrabytes',config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) config.set(address,'privSigningKey',privSigningKeyWIF) config.set(address,'privEncryptionKey',privEncryptionKeyWIF) with open(appdata + 'keys.dat', 'wb') as configfile: @@ -2998,10 +3021,12 @@ class addressGenerator(QThread): try: config.add_section(address) - print 'self.label', self.label + print 'label:', self.label config.set(address,'label',self.label) config.set(address,'enabled','true') config.set(address,'decoy','false') + config.set(address,'noncetrialsperbyte',config.get('bitmessagesettings','defaultnoncetrialsperbyte')) + config.set(address,'payloadlengthextrabytes',config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) config.set(address,'privSigningKey',privSigningKeyWIF) config.set(address,'privEncryptionKey',privEncryptionKeyWIF) with open(appdata + 'keys.dat', 'wb') as configfile: @@ -3487,6 +3512,9 @@ class settingsDialog(QtGui.QDialog): 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) + + self.ui.lineEditTotalDifficulty.setText(str((float(config.getint('bitmessagesettings', 'defaultnoncetrialsperbyte'))/networkDefaultProofOfWorkNonceTrialsPerByte))) + self.ui.lineEditSmallMessageDifficulty.setText(str((float(config.getint('bitmessagesettings', 'defaultpayloadlengthextrabytes'))/networkDefaultPayloadLengthExtraBytes))) QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) def comboBoxProxyTypeChanged(self,comboBoxIndex): @@ -4666,6 +4694,8 @@ class MyForm(QtGui.QMainWindow): 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())) + config.set('bitmessagesettings', 'defaultnoncetrialsperbyte',str(int(float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text())*networkDefaultProofOfWorkNonceTrialsPerByte))) + config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes',str(int(float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text())*networkDefaultPayloadLengthExtraBytes))) with open(appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) @@ -5309,8 +5339,7 @@ selfInitiatedConnections = {} #This is a list of current connections (the thread alreadyAttemptedConnectionsList = {} #This is a list of nodes to which we have already attempted a connection sendDataQueues = [] #each sendData thread puts its queue in this list. myECCryptorObjects = {} -myAddressesByHash = {} #The key in this dictionary is the hash encoded in an address and value is the address itself. -#myPrivateKeys = {} +myAddressesByHash = {} #The key in this dictionary is the RIPE hash which is encoded in an address and value is the address itself. 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. @@ -5334,11 +5363,11 @@ apiAddressGeneratorReturnQueue = Queue.Queue() #The address generator thread use alreadyAttemptedConnectionsListResetTime = int(time.time()) #used to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect. #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! -networkDefaultAverageProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. +networkDefaultProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. networkDefaultPayloadLengthExtraBytes = 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. if useVeryEasyProofOfWorkForTesting: - networkDefaultAverageProofOfWorkNonceTrialsPerByte = networkDefaultAverageProofOfWorkNonceTrialsPerByte / 16 + networkDefaultProofOfWorkNonceTrialsPerByte = networkDefaultProofOfWorkNonceTrialsPerByte / 16 networkDefaultPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes / 7000 if __name__ == "__main__": @@ -5369,7 +5398,7 @@ if __name__ == "__main__": 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','4') + config.set('bitmessagesettings','settingsversion','5') config.set('bitmessagesettings','port','8444') config.set('bitmessagesettings','timeformat','%%a, %%d %%b %%Y %%I:%%M %%p') config.set('bitmessagesettings','blackwhitelist','black') @@ -5447,7 +5476,7 @@ if __name__ == "__main__": knownNodes = pickle.load(pickleFile) pickleFile.close() - if config.getint('bitmessagesettings', 'settingsversion') > 4: + if config.getint('bitmessagesettings', 'settingsversion') > 5: print 'Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage.' raise SystemExit diff --git a/src/messages.dat reader.py b/src/messages.dat reader.py index 7dc7a660..52f17e21 100644 --- a/src/messages.dat reader.py +++ b/src/messages.dat reader.py @@ -104,6 +104,16 @@ def vacuum(): conn.commit() print 'done' +def temp(): + item = '''SELECT name FROM sqlite_master WHERE type='table' AND name='sent';''' + parameters = '' + cur.execute(item, parameters) + output = cur.fetchall() + if output == []: + print 'no table' + else: + print 'table exists' + #takeInboxMessagesOutOfTrash() #takeSentMessagesOutOfTrash() #markAllInboxMessagesAsUnread() @@ -112,6 +122,7 @@ def vacuum(): #readPubkeys() #readSubscriptions() #readInventory() -vacuum() #will defragment and clean empty space from the messages.dat file. +#vacuum() #will defragment and clean empty space from the messages.dat file. +temp() diff --git a/src/settings.py b/src/settings.py index 7c3b342d..45be7337 100644 --- a/src/settings.py +++ b/src/settings.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'settings.ui' # -# Created: Fri Mar 22 15:43:34 2013 +# Created: Thu Apr 25 16:02:49 2013 # by: PyQt4 UI code generator 4.9.4 # # WARNING! All changes made in this file will be lost! @@ -17,7 +17,7 @@ except AttributeError: class Ui_settingsDialog(object): def setupUi(self, settingsDialog): settingsDialog.setObjectName(_fromUtf8("settingsDialog")) - settingsDialog.resize(476, 340) + settingsDialog.resize(445, 343) self.gridLayout = QtGui.QGridLayout(settingsDialog) self.gridLayout.setObjectName(_fromUtf8("gridLayout")) self.buttonBox = QtGui.QDialogButtonBox(settingsDialog) @@ -125,10 +125,57 @@ class Ui_settingsDialog(object): spacerItem2 = QtGui.QSpacerItem(20, 70, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) self.gridLayout_4.addItem(spacerItem2, 2, 0, 1, 1) self.tabWidgetSettings.addTab(self.tabNetworkSettings, _fromUtf8("")) + self.tab = QtGui.QWidget() + self.tab.setObjectName(_fromUtf8("tab")) + self.gridLayout_6 = QtGui.QGridLayout(self.tab) + self.gridLayout_6.setObjectName(_fromUtf8("gridLayout_6")) + self.label_8 = QtGui.QLabel(self.tab) + self.label_8.setWordWrap(True) + self.label_8.setObjectName(_fromUtf8("label_8")) + self.gridLayout_6.addWidget(self.label_8, 0, 0, 1, 3) + spacerItem3 = QtGui.QSpacerItem(203, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.gridLayout_6.addItem(spacerItem3, 1, 0, 1, 1) + self.label_9 = QtGui.QLabel(self.tab) + self.label_9.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter) + self.label_9.setObjectName(_fromUtf8("label_9")) + self.gridLayout_6.addWidget(self.label_9, 1, 1, 1, 1) + self.lineEditTotalDifficulty = QtGui.QLineEdit(self.tab) + sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) + sizePolicy.setHorizontalStretch(0) + sizePolicy.setVerticalStretch(0) + sizePolicy.setHeightForWidth(self.lineEditTotalDifficulty.sizePolicy().hasHeightForWidth()) + self.lineEditTotalDifficulty.setSizePolicy(sizePolicy) + self.lineEditTotalDifficulty.setMaximumSize(QtCore.QSize(70, 16777215)) + self.lineEditTotalDifficulty.setObjectName(_fromUtf8("lineEditTotalDifficulty")) + self.gridLayout_6.addWidget(self.lineEditTotalDifficulty, 1, 2, 1, 1) + spacerItem4 = QtGui.QSpacerItem(203, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) + self.gridLayout_6.addItem(spacerItem4, 3, 0, 1, 1) + self.label_11 = QtGui.QLabel(self.tab) + self.label_11.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter) + self.label_11.setObjectName(_fromUtf8("label_11")) + self.gridLayout_6.addWidget(self.label_11, 3, 1, 1, 1) + self.lineEditSmallMessageDifficulty = QtGui.QLineEdit(self.tab) + sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) + sizePolicy.setHorizontalStretch(0) + sizePolicy.setVerticalStretch(0) + sizePolicy.setHeightForWidth(self.lineEditSmallMessageDifficulty.sizePolicy().hasHeightForWidth()) + self.lineEditSmallMessageDifficulty.setSizePolicy(sizePolicy) + self.lineEditSmallMessageDifficulty.setMaximumSize(QtCore.QSize(70, 16777215)) + self.lineEditSmallMessageDifficulty.setObjectName(_fromUtf8("lineEditSmallMessageDifficulty")) + self.gridLayout_6.addWidget(self.lineEditSmallMessageDifficulty, 3, 2, 1, 1) + self.label_12 = QtGui.QLabel(self.tab) + self.label_12.setWordWrap(True) + self.label_12.setObjectName(_fromUtf8("label_12")) + self.gridLayout_6.addWidget(self.label_12, 4, 0, 1, 3) + self.label_10 = QtGui.QLabel(self.tab) + self.label_10.setWordWrap(True) + self.label_10.setObjectName(_fromUtf8("label_10")) + self.gridLayout_6.addWidget(self.label_10, 2, 0, 1, 3) + self.tabWidgetSettings.addTab(self.tab, _fromUtf8("")) self.gridLayout.addWidget(self.tabWidgetSettings, 0, 0, 1, 1) self.retranslateUi(settingsDialog) - self.tabWidgetSettings.setCurrentIndex(0) + self.tabWidgetSettings.setCurrentIndex(2) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), settingsDialog.accept) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), settingsDialog.reject) QtCore.QObject.connect(self.checkBoxAuthentication, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.lineEditSocksUsername.setEnabled) @@ -169,4 +216,10 @@ class Ui_settingsDialog(object): self.label_5.setText(QtGui.QApplication.translate("settingsDialog", "Username:", None, QtGui.QApplication.UnicodeUTF8)) self.label_6.setText(QtGui.QApplication.translate("settingsDialog", "Pass:", None, QtGui.QApplication.UnicodeUTF8)) self.tabWidgetSettings.setTabText(self.tabWidgetSettings.indexOf(self.tabNetworkSettings), QtGui.QApplication.translate("settingsDialog", "Network Settings", None, QtGui.QApplication.UnicodeUTF8)) + self.label_8.setText(QtGui.QApplication.translate("settingsDialog", "When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exeption: if you add a friend or aquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. ", None, QtGui.QApplication.UnicodeUTF8)) + self.label_9.setText(QtGui.QApplication.translate("settingsDialog", "Total difficulty:", None, QtGui.QApplication.UnicodeUTF8)) + self.label_11.setText(QtGui.QApplication.translate("settingsDialog", "Small message difficulty:", None, QtGui.QApplication.UnicodeUTF8)) + self.label_12.setText(QtGui.QApplication.translate("settingsDialog", "The \'Small message difficulty\' mostly only affects the difficulty of sending small messages. Doubling this value makes it almost twice as difficult to send a small message but doesn\'t really affect large messages.", None, QtGui.QApplication.UnicodeUTF8)) + self.label_10.setText(QtGui.QApplication.translate("settingsDialog", "The \'Total difficulty\' affects the absolute amount of work the sender must complete. Doubling this value doubles the amount of work.", None, QtGui.QApplication.UnicodeUTF8)) + self.tabWidgetSettings.setTabText(self.tabWidgetSettings.indexOf(self.tab), QtGui.QApplication.translate("settingsDialog", "Demanded difficulty", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/src/settings.ui b/src/settings.ui index 44f2f50e..0edc6e68 100644 --- a/src/settings.ui +++ b/src/settings.ui @@ -6,8 +6,8 @@ 0 0 - 476 - 340 + 445 + 343 @@ -27,7 +27,7 @@ - 0 + 2 @@ -258,6 +258,121 @@ + + + Demanded difficulty + + + + + + When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exeption: if you add a friend or aquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. + + + true + + + + + + + Qt::Horizontal + + + + 203 + 20 + + + + + + + + Total difficulty: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + + 70 + 16777215 + + + + + + + + Qt::Horizontal + + + + 203 + 20 + + + + + + + + Small message difficulty: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + + 70 + 16777215 + + + + + + + + The 'Small message difficulty' mostly only affects the difficulty of sending small messages. Doubling this value makes it almost twice as difficult to send a small message but doesn't really affect large messages. + + + true + + + + + + + The 'Total difficulty' affects the absolute amount of work the sender must complete. Doubling this value doubles the amount of work. + + + true + + + + + From 63e698f562f6dfcfb7667f5e56f886783d807019 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 26 Apr 2013 13:20:30 -0400 Subject: [PATCH 06/33] Implimented broadcast encryption (untested) --- src/bitmessagemain.py | 556 ++++++++++++++++++++++++++----------- src/messages.dat reader.py | 14 +- src/settings.py | 6 +- src/settings.ui | 4 +- 4 files changed, 398 insertions(+), 182 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 7054ee73..88a6e3af 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -72,7 +72,7 @@ class outgoingSynSender(QThread): time.sleep(1) global alreadyAttemptedConnectionsListResetTime while True: - time.sleep(999999)#I sometimes use this to prevent connections for testing. + #time.sleep(999999)#I sometimes use this to prevent connections for testing. if len(selfInitiatedConnections[self.streamNumber]) < 8: #maximum number of outgoing connections = 8 random.seed() HOST, = random.sample(knownNodes[self.streamNumber], 1) @@ -409,11 +409,14 @@ class receiveDataThread(QThread): print 'Checksum incorrect. Clearing this message.' self.data = self.data[self.payloadLength+24:] - def isProofOfWorkSufficient(self,data): + def isProofOfWorkSufficient(self,data,nonceTrialsPerByte=0,payloadLengthExtraBytes=0): + if nonceTrialsPerByte < networkDefaultProofOfWorkNonceTrialsPerByte: + nonceTrialsPerByte = networkDefaultProofOfWorkNonceTrialsPerByte + if payloadLengthExtraBytes < networkDefaultPayloadLengthExtraBytes: + payloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes POW, = unpack('>Q',hashlib.sha512(hashlib.sha512(data[:8]+ hashlib.sha512(data[8:]).digest()).digest()).digest()[0:8]) #print 'POW:', POW - #Notice that I have divided the networkDefaultProofOfWorkNonceTrialsPerByte 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 / ((len(data)+networkDefaultPayloadLengthExtraBytes) * (networkDefaultProofOfWorkNonceTrialsPerByte/2)) + return POW <= 2**64 / ((len(data)+payloadLengthExtraBytes) * (nonceTrialsPerByte)) def sendpong(self): print 'Sending pong' @@ -533,6 +536,14 @@ class receiveDataThread(QThread): if len(data) < 180: print 'The payload length of this broadcast packet is unreasonably low. Someone is probably trying funny business. Ignoring message.' return + #Let us check to make sure the stream number is correct (thus preventing an individual from sending broadcasts out on the wrong streams or all streams). + broadcastVersion, broadcastVersionLength = decodeVarint(data[readPosition:readPosition+10]) + if broadcastVersion >= 2: + streamNumber = decodeVarint(data[readPosition+broadcastVersionLength:readPosition+broadcastVersionLength+10]) + if streamNumber != self.streamNumber: + print 'The stream number encoded in this broadcast message (' + str(streamNumber) + ') does not match the stream number on which it was received. Ignoring it.' + return + inventoryLock.acquire() self.inventoryHash = calculateInventoryHash(data) if self.inventoryHash in inventory: @@ -577,121 +588,249 @@ class receiveDataThread(QThread): #A broadcast message has a valid time and POW and requires processing. The recbroadcast function calls this one. def processbroadcast(self,readPosition,data): broadcastVersion, broadcastVersionLength = decodeVarint(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 - beginningOfPubkeyPosition = readPosition #used when we add the pubkey to our pubkey table - sendersAddressVersion, sendersAddressVersionLength = decodeVarint(data[readPosition:readPosition+9]) - if sendersAddressVersion <= 1 or sendersAddressVersion >=3: - #Cannot decode senderAddressVersion higher than 2. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored. + if broadcastVersion < 1 or broadcastVersion > 2: + #Cannot decode incoming broadcast versions higher than 2. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored. return - readPosition += sendersAddressVersionLength - if sendersAddressVersion == 2: - sendersStream, sendersStreamLength = decodeVarint(data[readPosition:readPosition+9]) - if sendersStream <= 0 or sendersStream <> self.streamNumber: + if broadcastVersion == 1: + beginningOfPubkeyPosition = readPosition #used when we add the pubkey to our pubkey table + sendersAddressVersion, sendersAddressVersionLength = decodeVarint(data[readPosition:readPosition+9]) + if sendersAddressVersion <= 1 or sendersAddressVersion >=3: + #Cannot decode senderAddressVersion higher than 2. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored. return - readPosition += sendersStreamLength - behaviorBitfield = data[readPosition:readPosition+4] - readPosition += 4 - sendersPubSigningKey = '\x04' + data[readPosition:readPosition+64] - readPosition += 64 - sendersPubEncryptionKey = '\x04' + data[readPosition:readPosition+64] - readPosition += 64 - endOfPubkeyPosition = readPosition - sendersHash = data[readPosition:readPosition+20] - if sendersHash not in broadcastSendersForWhichImWatching: - #Display timing data - printLock.acquire() - print 'Time spent deciding that we are not interested in this broadcast:', time.time()- self.messageProcessingStartTime - printLock.release() - 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 + readPosition += sendersAddressVersionLength + if sendersAddressVersion == 2: + sendersStream, sendersStreamLength = decodeVarint(data[readPosition:readPosition+9]) + readPosition += sendersStreamLength + behaviorBitfield = data[readPosition:readPosition+4] + readPosition += 4 + sendersPubSigningKey = '\x04' + data[readPosition:readPosition+64] + readPosition += 64 + sendersPubEncryptionKey = '\x04' + data[readPosition:readPosition+64] + readPosition += 64 + endOfPubkeyPosition = readPosition + sendersHash = data[readPosition:readPosition+20] + if sendersHash not in broadcastSendersForWhichImWatching: + #Display timing data + printLock.acquire() + print 'Time spent deciding that we are not interested in this broadcast:', time.time()- self.messageProcessingStartTime + printLock.release() + 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 - 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. - return - messageEncodingType, messageEncodingTypeLength = decodeVarint(data[readPosition:readPosition+9]) - if messageEncodingType == 0: - return - readPosition += messageEncodingTypeLength - messageLength, messageLengthLength = decodeVarint(data[readPosition:readPosition+9]) - readPosition += messageLengthLength - message = data[readPosition:readPosition+messageLength] - readPosition += messageLength - readPositionAtBottomOfMessage = readPosition - signatureLength, signatureLengthLength = decodeVarint(data[readPosition:readPosition+9]) - readPosition += signatureLengthLength - signature = data[readPosition:readPosition+signatureLength] - try: - highlevelcrypto.verify(data[12:readPositionAtBottomOfMessage],signature,sendersPubSigningKey.encode('hex')) - print 'ECDSA verify passed' - except Exception, err: - print 'ECDSA verify failed', err - return - #verify passed + 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. + return + messageEncodingType, messageEncodingTypeLength = decodeVarint(data[readPosition:readPosition+9]) + if messageEncodingType == 0: + return + readPosition += messageEncodingTypeLength + messageLength, messageLengthLength = decodeVarint(data[readPosition:readPosition+9]) + readPosition += messageLengthLength + message = data[readPosition:readPosition+messageLength] + readPosition += messageLength + readPositionAtBottomOfMessage = readPosition + signatureLength, signatureLengthLength = decodeVarint(data[readPosition:readPosition+9]) + readPosition += signatureLengthLength + signature = data[readPosition:readPosition+signatureLength] + try: + highlevelcrypto.verify(data[12:readPositionAtBottomOfMessage],signature,sendersPubSigningKey.encode('hex')) + print 'ECDSA verify passed' + except Exception, err: + print 'ECDSA verify failed', err + return + #verify passed - #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(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+data[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - 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. - - fromAddress = encodeAddress(sendersAddressVersion,sendersStream,ripe.digest()) - printLock.acquire() - print 'fromAddress:', fromAddress - printLock.release() - if messageEncodingType == 2: - bodyPositionIndex = string.find(message,'\nBody:') - 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.' - else: - body = 'Unknown encoding type.\n\n' + repr(message) - subject = '' - - toAddress = '[Broadcast subscribers]' - if messageEncodingType <> 0: + #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(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+data[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') sqlLock.acquire() - t = (self.inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox',messageEncodingType,0) - sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + 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. - #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"]) + fromAddress = encodeAddress(sendersAddressVersion,sendersStream,ripe.digest()) + printLock.acquire() + print 'fromAddress:', fromAddress + printLock.release() + if messageEncodingType == 2: + bodyPositionIndex = string.find(message,'\nBody:') + 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.' + else: + body = 'Unknown encoding type.\n\n' + repr(message) + subject = '' - #Display timing data - printLock.acquire() - print 'Time spent processing this interesting broadcast:', time.time()- self.messageProcessingStartTime - printLock.release() + toAddress = '[Broadcast subscribers]' + if messageEncodingType <> 0: + sqlLock.acquire() + t = (self.inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox',messageEncodingType,0) + sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') + sqlSubmitQueue.put(t) + sqlReturnQueue.get() + sqlSubmitQueue.put('commit') + sqlLock.release() + self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + + #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"]) + + #Display timing data + printLock.acquire() + print 'Time spent processing this interesting broadcast:', time.time()- self.messageProcessingStartTime + printLock.release() + if broadcastVersion == 2: + cleartextStreamNumber, cleartextStreamNumberLength = decodeVarint(data[readPosition:readPosition+10]) + readPosition += streamNumberLength + initialDecryptionSuccessful = False + + for key, cryptorObject in MyECSubscriptionCryptorObjects.items(): + try: + decryptedData = cryptorObject.decrypt(data[readPosition:]) + toRipe = key #This is the RIPE hash of the sender's pubkey. We need this below to compare to the RIPE hash of the sender's address to verify that it was encrypted by with their key rather than some other key. + 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 + if not initialDecryptionSuccessful: + #This is not a message bound for me. + printLock.acquire() + print 'Length of time program spent failing to decrypt this broadcast:', time.time()- self.messageProcessingStartTime, 'seconds.' + printLock.release() + else: + #This is a broadcast I have decrypted and thus am interested in. + signedBroadcastVersion, readPosition = decodeVarint(decryptedData[:10]) + beginningOfPubkeyPosition = readPosition #used when we add the pubkey to our pubkey table + sendersAddressVersion, sendersAddressVersionLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + if sendersAddressVersion < 2 or sendersAddressVersion > 3: + print 'Cannot decode senderAddressVersion other than 2 or 3. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.' + return + readPosition += sendersAddressVersionLength + sendersStream, sendersStreamLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + if sendersStream != cleartextStreamNumber: + print 'The stream number outside of the encryption on which the POW was completed doesn\'t match the stream number inside the encryption. Ignoring broadcast.' + return + readPosition += sendersStreamLength + behaviorBitfield = decryptedData[readPosition:readPosition+4] + readPosition += 4 + sendersPubSigningKey = '\x04' + decryptedData[readPosition:readPosition+64] + readPosition += 64 + sendersPubEncryptionKey = '\x04' + decryptedData[readPosition:readPosition+64] + readPosition += 64 + if sendersAddressVersion >= 3: + requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(decryptedData[readPosition:readPosition+10]) + readPosition += varintLength + print 'sender\'s requiredAverageProofOfWorkNonceTrialsPerByte is', requiredAverageProofOfWorkNonceTrialsPerByte + requiredPayloadLengthExtraBytes, varintLength = decodeVarint(decryptedData[readPosition:readPosition+10]) + readPosition += varintLength + print 'sender\'s requiredPayloadLengthExtraBytes is', requiredPayloadLengthExtraBytes + endOfPubkeyPosition = readPosition + + sha = hashlib.new('sha512') + sha.update(sendersPubSigningKey+sendersPubEncryptionKey) + ripe = hashlib.new('ripemd160') + ripe.update(sha.digest()) + + messageEncodingType, messageEncodingTypeLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + if messageEncodingType == 0: + return + readPosition += messageEncodingTypeLength + messageLength, messageLengthLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + readPosition += messageLengthLength + message = decryptedData[readPosition:readPosition+messageLength] + readPosition += messageLength + readPositionAtBottomOfMessage = readPosition + signatureLength, signatureLengthLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + readPosition += signatureLengthLength + signature = decryptedData[readPosition:readPosition+signatureLength] + try: + highlevelcrypto.verify(decryptedData[:readPositionAtBottomOfMessage],signature,sendersPubSigningKey.encode('hex')) + print 'ECDSA verify passed' + except Exception, err: + print 'ECDSA verify failed', err + return + #verify passed + + #Let's store the public key in case we want to reply to this person. + t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+decryptedData[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') + sqlLock.acquire() + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + sqlSubmitQueue.put(t) + sqlReturnQueue.get() + sqlSubmitQueue.put('commit') + 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. + + fromAddress = encodeAddress(sendersAddressVersion,sendersStream,ripe.digest()) + printLock.acquire() + print 'fromAddress:', fromAddress + printLock.release() + if messageEncodingType == 2: + bodyPositionIndex = string.find(message,'\nBody:') + 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.' + else: + body = 'Unknown encoding type.\n\n' + repr(message) + subject = '' + + toAddress = '[Broadcast subscribers]' + if messageEncodingType <> 0: + sqlLock.acquire() + t = (self.inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox',messageEncodingType,0) + sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') + sqlSubmitQueue.put(t) + sqlReturnQueue.get() + sqlSubmitQueue.put('commit') + sqlLock.release() + self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + + #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"]) + + #Display timing data + printLock.acquire() + print 'Time spent processing this interesting broadcast:', time.time()- self.messageProcessingStartTime + printLock.release() #We have received a msg message. @@ -791,7 +930,7 @@ class receiveDataThread(QThread): #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. for key, cryptorObject in myECCryptorObjects.items(): try: - unencryptedData = cryptorObject.decrypt(encryptedData[readPosition:]) + decryptedData = cryptorObject.decrypt(encryptedData[readPosition:]) 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') @@ -806,13 +945,22 @@ class receiveDataThread(QThread): printLock.release() else: #This is a message bound for me. + + #If this message is bound for one of my version 3 addresses (or higher), then we must check to make sure it meets our demanded proof of work requirement. + toAddress = myAddressesByHash[toRipe] #Look up my address based on the RIPE hash. + if decodeAddress(toAddress)[1] >= 3:#If the toAddress version number is 3 or higher: + requiredNonceTrialsPerByte = config.getint(toAddress,'noncetrialsperbyte') + requiredPayloadLengthExtraBytes = config.getint(toAddress,'payloadlengthextrabytes') + if not self.isProofOfWorkSufficient(encryptedData,requiredNonceTrialsPerByte,requiredPayloadLengthExtraBytes): + print 'Proof of work in msg message insufficient only because it does not meet our higher requirement.' + return readPosition = 0 - messageVersion, messageVersionLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + messageVersion, messageVersionLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += messageVersionLength if messageVersion != 1: print 'Cannot understand message versions other than one. Ignoring message.' return - sendersAddressVersionNumber, sendersAddressVersionNumberLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + sendersAddressVersionNumber, sendersAddressVersionNumberLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += sendersAddressVersionNumberLength if sendersAddressVersionNumber == 0: print 'Cannot understand sendersAddressVersionNumber = 0. Ignoring message.' @@ -820,54 +968,54 @@ class receiveDataThread(QThread): if sendersAddressVersionNumber >= 4: print 'Sender\'s address version number', sendersAddressVersionNumber, 'not yet supported. Ignoring message.' return - if len(unencryptedData) < 170: + if len(decryptedData) < 170: print 'Length of the unencrypted data is unreasonably short. Sanity check failed. Ignoring message.' return - sendersStreamNumber, sendersStreamNumberLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + sendersStreamNumber, sendersStreamNumberLength = decodeVarint(decryptedData[readPosition:readPosition+10]) if sendersStreamNumber == 0: print 'sender\'s stream number is 0. Ignoring message.' return readPosition += sendersStreamNumberLength - behaviorBitfield = unencryptedData[readPosition:readPosition+4] + behaviorBitfield = decryptedData[readPosition:readPosition+4] readPosition += 4 - pubSigningKey = '\x04' + unencryptedData[readPosition:readPosition+64] + pubSigningKey = '\x04' + decryptedData[readPosition:readPosition+64] readPosition += 64 - pubEncryptionKey = '\x04' + unencryptedData[readPosition:readPosition+64] + pubEncryptionKey = '\x04' + decryptedData[readPosition:readPosition+64] readPosition += 64 if sendersAddressVersionNumber >= 3: - requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += varintLength print 'sender\'s requiredAverageProofOfWorkNonceTrialsPerByte is', requiredAverageProofOfWorkNonceTrialsPerByte - requiredPayloadLengthExtraBytes, varintLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + requiredPayloadLengthExtraBytes, varintLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += varintLength print 'sender\'s requiredPayloadLengthExtraBytes is', requiredPayloadLengthExtraBytes endOfThePublicKeyPosition = readPosition #needed for when we store the pubkey in our database of pubkeys for later use. - if toRipe != unencryptedData[readPosition:readPosition+20]: + if toRipe != decryptedData[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://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html' print 'your toRipe:', toRipe.encode('hex') - print 'embedded destination toRipe:', unencryptedData[readPosition:readPosition+20].encode('hex') + print 'embedded destination toRipe:', decryptedData[readPosition:readPosition+20].encode('hex') printLock.release() return readPosition += 20 - messageEncodingType, messageEncodingTypeLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + messageEncodingType, messageEncodingTypeLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += messageEncodingTypeLength - messageLength, messageLengthLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + messageLength, messageLengthLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += messageLengthLength - message = unencryptedData[readPosition:readPosition+messageLength] + message = decryptedData[readPosition:readPosition+messageLength] #print 'First 150 characters of message:', repr(message[:150]) readPosition += messageLength - ackLength, ackLengthLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + ackLength, ackLengthLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += ackLengthLength - ackData = unencryptedData[readPosition:readPosition+ackLength] + ackData = decryptedData[readPosition:readPosition+ackLength] readPosition += ackLength positionOfBottomOfAckData = readPosition #needed to mark the end of what is covered by the signature - signatureLength, signatureLengthLength = decodeVarint(unencryptedData[readPosition:readPosition+10]) + signatureLength, signatureLengthLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += signatureLengthLength - signature = unencryptedData[readPosition:readPosition+signatureLength] + signature = decryptedData[readPosition:readPosition+signatureLength] try: - highlevelcrypto.verify(unencryptedData[:positionOfBottomOfAckData],signature,pubSigningKey.encode('hex')) + highlevelcrypto.verify(decryptedData[:positionOfBottomOfAckData],signature,pubSigningKey.encode('hex')) print 'ECDSA verify passed' except Exception, err: print 'ECDSA verify failed', err @@ -881,8 +1029,7 @@ class receiveDataThread(QThread): 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.) - t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+unencryptedData[messageVersionLength:endOfThePublicKeyPosition],int(time.time()),'yes') + t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+decryptedData[messageVersionLength:endOfThePublicKeyPosition],int(time.time()),'yes') sqlLock.acquire() sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') sqlSubmitQueue.put(t) @@ -925,7 +1072,6 @@ class receiveDataThread(QThread): print 'fromAddress:', fromAddress print 'First 150 characters of message:', repr(message[:150]) - toAddress = myAddressesByHash[toRipe] #Look up my address based on the RIPE hash. toLabel = config.get(toAddress, 'label') if toLabel == '': toLabel = addressInKeysFile @@ -2068,6 +2214,32 @@ def safeConfigGetBoolean(section,field): except: return False + #Does an EC point multiplication; turns a private key into a public key. +def pointMult(secret): + #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. + 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) + + OpenSSL.EC_POINT_mul(group, pub_key, priv_key, None, None, None) + 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) + #OpenSSL.BN_CTX_free(ctx) + OpenSSL.BN_free(priv_key) + OpenSSL.EC_KEY_free(k) + return mb.raw + def lookupAppdataFolder(): APPNAME = "PyBitmessage" from os import path, environ @@ -2577,7 +2749,7 @@ class singleWorker(QThread): for row in queryreturn: fromaddress, subject, body, ackdata = row status,addressVersionNumber,streamNumber,ripe = decodeAddress(fromaddress) - if addressVersionNumber == 2: + if addressVersionNumber == 2:#todo: and time is less than some date in the future: #We need to convert our private keys to public keys in order to include them. try: privSigningKeyBase58 = config.get(fromaddress, 'privsigningkey') @@ -2629,6 +2801,76 @@ class singleWorker(QThread): self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + #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() + sqlSubmitQueue.put('commit') + sqlLock.release() + elif addressVersionNumber == 3:#todo: or (addressVersionNumber == 2 and time is greater than than some date in the future): + #We need to convert our private keys to public keys in order to include them. + 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 + + privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') + privEncryptionKeyHex = decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') + + 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. + pubEncryptionKey = highlevelcrypto.privToPub(privEncryptionKeyHex).decode('hex') + + payload = pack('>I',(int(time.time())+random.randrange(-300, 300)))#the current time plus or minus five minutes + payload += encodeVarint(2) #broadcast version + payload += encodeVarint(streamNumber) + + dataToEncrypt = encodeVarint(2) #broadcast version + dataToEncrypt += encodeVarint(addressVersionNumber) + dataToEncrypt += encodeVarint(streamNumber) + dataToEncrypt += '\x00\x00\x00\x01' #behavior bitfield + dataToEncrypt += pubSigningKey[1:] + dataToEncrypt += pubEncryptionKey[1:] + if addressVersionNumber >= 3: + dataToEncrypt += encodeVarint(config.getint(fromaddress,'noncetrialsperbyte')) + dataToEncrypt += encodeVarint(config.getint(fromaddress,'payloadlengthextrabytes')) + dataToEncrypt += '\x02' #message encoding type + dataToEncrypt += encodeVarint(len('Subject:' + subject + '\n' + 'Body:' + body)) #Type 2 is simple UTF-8 message encoding. + dataToEncrypt += 'Subject:' + subject + '\n' + 'Body:' + body + + signature = highlevelcrypto.sign(payload,privSigningKeyHex) + dataToEncrypt += encodeVarint(len(signature)) + dataToEncrypt += signature + print 'The string that we will hash to make the privEncryptionKey is', (encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).encode('hex') + privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()[:32] + pubEncryptionKey = pointMult(privEncryptionKey) + print 'length of the pub encrypion key is', len(pubEncryptionKey), 'which should be 65.' + payload += highlevelcrypto.encrypt(dataToEncrypt,pubEncryptionKey.encode('hex')) + + nonce = 0 + trialValue = 99999999999999999999 + target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) + print '(For broadcast message) Doing proof of work...' + self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') + 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)' + broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) + + self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + #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') @@ -2792,8 +3034,8 @@ class singleWorker(QThread): print '(For msg message) Doing proof of work. Target:', target print 'Using requiredAverageProofOfWorkNonceTrialsPerByte', requiredAverageProofOfWorkNonceTrialsPerByte print 'Using requiredPayloadLengthExtraBytes =', requiredPayloadLengthExtraBytes - print 'The required total difficulty is', requiredAverageProofOfWorkNonceTrialsPerByte/networkDefaultProofOfWorkNonceTrialsPerByte - print 'The required small message difficulty is', requiredPayloadLengthExtraBytes/networkDefaultPayloadLengthExtraBytes + print 'The required total difficulty is', float(requiredAverageProofOfWorkNonceTrialsPerByte)/networkDefaultProofOfWorkNonceTrialsPerByte + print 'The required small message difficulty is', float(requiredPayloadLengthExtraBytes)/networkDefaultPayloadLengthExtraBytes printLock.release() powStartTime = time.time() initialHash = hashlib.sha512(payload).digest() @@ -2915,11 +3157,11 @@ class addressGenerator(QThread): startTime = time.time() numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix = 0 potentialPrivSigningKey = OpenSSL.rand(32) - potentialPubSigningKey = self.pointMult(potentialPrivSigningKey) + potentialPubSigningKey = pointMult(potentialPrivSigningKey) while True: numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix += 1 potentialPrivEncryptionKey = OpenSSL.rand(32) - potentialPubEncryptionKey = self.pointMult(potentialPrivEncryptionKey) + potentialPubEncryptionKey = pointMult(potentialPrivEncryptionKey) #print 'potentialPubSigningKey', potentialPubSigningKey.encode('hex') #print 'potentialPubEncryptionKey', potentialPubEncryptionKey.encode('hex') ripe = hashlib.new('ripemd160') @@ -2986,8 +3228,8 @@ class addressGenerator(QThread): numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix += 1 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) + potentialPubSigningKey = pointMult(potentialPrivSigningKey) + potentialPubEncryptionKey = pointMult(potentialPrivEncryptionKey) #print 'potentialPubSigningKey', potentialPubSigningKey.encode('hex') #print 'potentialPubEncryptionKey', potentialPubEncryptionKey.encode('hex') signingKeyNonce += 2 @@ -3043,31 +3285,6 @@ class addressGenerator(QThread): self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address') reloadMyAddressHashes() - #Does an EC point multiplication; turns a private key into a public key. - def pointMult(self,secret): - #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. - 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) - - OpenSSL.EC_POINT_mul(group, pub_key, priv_key, None, None, None) - 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) - #OpenSSL.BN_CTX_free(ctx) - OpenSSL.BN_free(priv_key) - OpenSSL.EC_KEY_free(k) - return mb.raw #This is one of several classes that constitute the API #This class was written by Vaibhav Bhatia. Modified by Jonathan Warren (Atheros). @@ -4694,8 +4911,10 @@ class MyForm(QtGui.QMainWindow): 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())) - config.set('bitmessagesettings', 'defaultnoncetrialsperbyte',str(int(float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text())*networkDefaultProofOfWorkNonceTrialsPerByte))) - config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes',str(int(float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text())*networkDefaultPayloadLengthExtraBytes))) + if float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text()) >= 1: + config.set('bitmessagesettings', 'defaultnoncetrialsperbyte',str(int(float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text())*networkDefaultProofOfWorkNonceTrialsPerByte))) + if float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text()) >= 1: + config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes',str(int(float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text())*networkDefaultPayloadLengthExtraBytes))) with open(appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) @@ -5327,7 +5546,13 @@ class MyForm(QtGui.QMainWindow): for row in queryreturn: address, = row status,addressVersionNumber,streamNumber,hash = decodeAddress(address) - broadcastSendersForWhichImWatching[hash] = 0 + if addressVersionNumber == 2: + broadcastSendersForWhichImWatching[hash] = 0 + #Now, for all addresses, even version 2 addresses, we should create Cryptor objects in a dictionary which we will use to attempt to decrypt encrypted broadcast messages. + print '(Within reloadBroadcastSendersForWhichImWatching) The string that we will hash to make the privEncryptionKey is', (encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).encode('hex') + privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32] + print 'length of privEncryptionKey is', len(privEncryptionKey) + MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) #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): @@ -5339,6 +5564,7 @@ selfInitiatedConnections = {} #This is a list of current connections (the thread alreadyAttemptedConnectionsList = {} #This is a list of nodes to which we have already attempted a connection sendDataQueues = [] #each sendData thread puts its queue in this list. myECCryptorObjects = {} +MyECSubscriptionCryptorObjects = {} myAddressesByHash = {} #The key in this dictionary is the RIPE hash which is encoded in an address and value is the address itself. inventory = {} #of objects (like msg payloads and pubkey payloads) Does not include protocol headers (the first 24 bytes of each packet). workerQueue = Queue.Queue() diff --git a/src/messages.dat reader.py b/src/messages.dat reader.py index 52f17e21..cb94525e 100644 --- a/src/messages.dat reader.py +++ b/src/messages.dat reader.py @@ -104,16 +104,6 @@ def vacuum(): conn.commit() print 'done' -def temp(): - item = '''SELECT name FROM sqlite_master WHERE type='table' AND name='sent';''' - parameters = '' - cur.execute(item, parameters) - output = cur.fetchall() - if output == []: - print 'no table' - else: - print 'table exists' - #takeInboxMessagesOutOfTrash() #takeSentMessagesOutOfTrash() #markAllInboxMessagesAsUnread() @@ -122,7 +112,7 @@ def temp(): #readPubkeys() #readSubscriptions() #readInventory() -#vacuum() #will defragment and clean empty space from the messages.dat file. -temp() +vacuum() #will defragment and clean empty space from the messages.dat file. + diff --git a/src/settings.py b/src/settings.py index 45be7337..2c8baef9 100644 --- a/src/settings.py +++ b/src/settings.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'settings.ui' # -# Created: Thu Apr 25 16:02:49 2013 +# Created: Fri Apr 26 13:19:59 2013 # by: PyQt4 UI code generator 4.9.4 # # WARNING! All changes made in this file will be lost! @@ -175,7 +175,7 @@ class Ui_settingsDialog(object): self.gridLayout.addWidget(self.tabWidgetSettings, 0, 0, 1, 1) self.retranslateUi(settingsDialog) - self.tabWidgetSettings.setCurrentIndex(2) + self.tabWidgetSettings.setCurrentIndex(0) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), settingsDialog.accept) QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), settingsDialog.reject) QtCore.QObject.connect(self.checkBoxAuthentication, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.lineEditSocksUsername.setEnabled) @@ -216,7 +216,7 @@ class Ui_settingsDialog(object): self.label_5.setText(QtGui.QApplication.translate("settingsDialog", "Username:", None, QtGui.QApplication.UnicodeUTF8)) self.label_6.setText(QtGui.QApplication.translate("settingsDialog", "Pass:", None, QtGui.QApplication.UnicodeUTF8)) self.tabWidgetSettings.setTabText(self.tabWidgetSettings.indexOf(self.tabNetworkSettings), QtGui.QApplication.translate("settingsDialog", "Network Settings", None, QtGui.QApplication.UnicodeUTF8)) - self.label_8.setText(QtGui.QApplication.translate("settingsDialog", "When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exeption: if you add a friend or aquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. ", None, QtGui.QApplication.UnicodeUTF8)) + self.label_8.setText(QtGui.QApplication.translate("settingsDialog", "When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exception: if you add a friend or aquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. ", None, QtGui.QApplication.UnicodeUTF8)) self.label_9.setText(QtGui.QApplication.translate("settingsDialog", "Total difficulty:", None, QtGui.QApplication.UnicodeUTF8)) self.label_11.setText(QtGui.QApplication.translate("settingsDialog", "Small message difficulty:", None, QtGui.QApplication.UnicodeUTF8)) self.label_12.setText(QtGui.QApplication.translate("settingsDialog", "The \'Small message difficulty\' mostly only affects the difficulty of sending small messages. Doubling this value makes it almost twice as difficult to send a small message but doesn\'t really affect large messages.", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/src/settings.ui b/src/settings.ui index 0edc6e68..1b8c5eb9 100644 --- a/src/settings.ui +++ b/src/settings.ui @@ -27,7 +27,7 @@ - 2 + 0 @@ -266,7 +266,7 @@ - When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exeption: if you add a friend or aquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. + When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exception: if you add a friend or aquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. true From afd644a97d6ed33a2aa15c43a718719283a13f05 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 26 Apr 2013 13:38:58 -0400 Subject: [PATCH 07/33] Implimented broadcast encryption (testing) --- src/bitmessagemain.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 88a6e3af..a8e66404 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -539,7 +539,7 @@ class receiveDataThread(QThread): #Let us check to make sure the stream number is correct (thus preventing an individual from sending broadcasts out on the wrong streams or all streams). broadcastVersion, broadcastVersionLength = decodeVarint(data[readPosition:readPosition+10]) if broadcastVersion >= 2: - streamNumber = decodeVarint(data[readPosition+broadcastVersionLength:readPosition+broadcastVersionLength+10]) + streamNumber, streamNumberLength = decodeVarint(data[readPosition+broadcastVersionLength:readPosition+broadcastVersionLength+10]) if streamNumber != self.streamNumber: print 'The stream number encoded in this broadcast message (' + str(streamNumber) + ') does not match the stream number on which it was received. Ignoring it.' return @@ -572,7 +572,7 @@ class receiveDataThread(QThread): elif len(data) > 1000000: #Between 10 and 1 megabyte lengthOfTimeWeShouldUseToProcessThisMessage = 3 #seconds. else: #Less than 1 megabyte - lengthOfTimeWeShouldUseToProcessThisMessage = .1 #seconds. + lengthOfTimeWeShouldUseToProcessThisMessage = .6 #seconds. sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - (time.time()- self.messageProcessingStartTime) @@ -613,7 +613,7 @@ class receiveDataThread(QThread): if sendersHash not in broadcastSendersForWhichImWatching: #Display timing data printLock.acquire() - print 'Time spent deciding that we are not interested in this broadcast:', time.time()- self.messageProcessingStartTime + print 'Time spent deciding that we are not interested in this v1 broadcast:', time.time()- self.messageProcessingStartTime printLock.release() 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. @@ -718,9 +718,9 @@ class receiveDataThread(QThread): pass #print 'cryptorObject.decrypt Exception:', err if not initialDecryptionSuccessful: - #This is not a message bound for me. + #This is not a broadcast I am interested in. printLock.acquire() - print 'Length of time program spent failing to decrypt this broadcast:', time.time()- self.messageProcessingStartTime, 'seconds.' + print 'Length of time program spent failing to decrypt this v2 broadcast:', time.time()- self.messageProcessingStartTime, 'seconds.' printLock.release() else: #This is a broadcast I have decrypted and thus am interested in. @@ -2847,7 +2847,6 @@ class singleWorker(QThread): print 'The string that we will hash to make the privEncryptionKey is', (encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).encode('hex') privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()[:32] pubEncryptionKey = pointMult(privEncryptionKey) - print 'length of the pub encrypion key is', len(pubEncryptionKey), 'which should be 65.' payload += highlevelcrypto.encrypt(dataToEncrypt,pubEncryptionKey.encode('hex')) nonce = 0 @@ -5551,7 +5550,6 @@ class MyForm(QtGui.QMainWindow): #Now, for all addresses, even version 2 addresses, we should create Cryptor objects in a dictionary which we will use to attempt to decrypt encrypted broadcast messages. print '(Within reloadBroadcastSendersForWhichImWatching) The string that we will hash to make the privEncryptionKey is', (encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).encode('hex') privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32] - print 'length of privEncryptionKey is', len(privEncryptionKey) MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) #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. From 896b96b7c7e756fb645a11e1658a1fc2ce931321 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 26 Apr 2013 16:07:58 -0400 Subject: [PATCH 08/33] Implimented broadcast encryption (testing) --- src/bitmessagemain.py | 59 ++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index a8e66404..3469f50b 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -590,7 +590,7 @@ class receiveDataThread(QThread): broadcastVersion, broadcastVersionLength = decodeVarint(data[readPosition:readPosition+9]) readPosition += broadcastVersionLength if broadcastVersion < 1 or broadcastVersion > 2: - #Cannot decode incoming broadcast versions higher than 2. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored. + print 'Cannot decode incoming broadcast versions higher than 2. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.' return if broadcastVersion == 1: beginningOfPubkeyPosition = readPosition #used when we add the pubkey to our pubkey table @@ -704,9 +704,8 @@ class receiveDataThread(QThread): printLock.release() if broadcastVersion == 2: cleartextStreamNumber, cleartextStreamNumberLength = decodeVarint(data[readPosition:readPosition+10]) - readPosition += streamNumberLength + readPosition += cleartextStreamNumberLength initialDecryptionSuccessful = False - for key, cryptorObject in MyECSubscriptionCryptorObjects.items(): try: decryptedData = cryptorObject.decrypt(data[readPosition:]) @@ -756,6 +755,12 @@ class receiveDataThread(QThread): ripe = hashlib.new('ripemd160') ripe.update(sha.digest()) + if toRipe != ripe.digest(): + print 'The encryption key used to encrypt this message doesn\'t match the keys inbedded in the message itself. Ignoring message.' + return + else: + print 'The encryption key DOES match the keys in the message.' + messageEncodingType, messageEncodingTypeLength = decodeVarint(decryptedData[readPosition:readPosition+9]) if messageEncodingType == 0: return @@ -2265,6 +2270,27 @@ def isAddressInMyAddressBook(address): sqlLock.release() return queryreturn != [] +def isAddressInMyAddressBookSubscriptionsListOrWhitelist(address): + if isAddressInMyAddressBook(address): + return True + + sqlLock.acquire() + sqlSubmitQueue.put('''SELECT address FROM whitelist where address=? and enabled = '1' ''') + sqlSubmitQueue.put((address,)) + queryreturn = sqlReturnQueue.get() + sqlLock.release() + if queryreturn <> []: + return True + + sqlLock.acquire() + sqlSubmitQueue.put('''select address from subscriptions where address=? and enabled = '1' ''') + sqlSubmitQueue.put((address,)) + queryreturn = sqlReturnQueue.get() + sqlLock.release() + if queryreturn <> []: + return True + return False + def assembleVersionMessage(remoteHost,remotePort,myStreamNumber): global softwareVersion payload = '' @@ -2383,6 +2409,9 @@ class sqlThread(QThread): self.cur.execute( '''CREATE TABLE pubkeys (hash blob, transmitdata blob, time int, usedpersonally text, UNIQUE(hash) ON CONFLICT REPLACE)''' ) self.cur.execute( '''INSERT INTO pubkeys SELECT hash, transmitdata, time, usedpersonally FROM pubkeys_backup;''') self.cur.execute( '''DROP TABLE pubkeys_backup;''') + print 'Deleting all pubkeys from inventory. They will be redownloaded and then saved with the correct times.' + self.cur.execute( '''delete from inventory where objecttype = 'pubkey';''') + print 'Commiting.' self.conn.commit() print 'Vacuuming message.dat. You might notice that the file size gets much smaller.' self.cur.execute( ''' VACUUM ''') @@ -2844,7 +2873,6 @@ class singleWorker(QThread): signature = highlevelcrypto.sign(payload,privSigningKeyHex) dataToEncrypt += encodeVarint(len(signature)) dataToEncrypt += signature - print 'The string that we will hash to make the privEncryptionKey is', (encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).encode('hex') privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+ripe).digest()[:32] pubEncryptionKey = pointMult(privEncryptionKey) payload += highlevelcrypto.encrypt(dataToEncrypt,pubEncryptionKey.encode('hex')) @@ -2964,8 +2992,13 @@ class singleWorker(QThread): 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. payload += pubEncryptionKey[1:] - payload += encodeVarint(config.getint(fromaddress,'noncetrialsperbyte'))#todo: check and see whether the addressee is in our address book, subscription list, or whitelist and set lower POW requirement if yes. - payload += encodeVarint(config.getint(fromaddress,'payloadlengthextrabytes')) + #If the receiver of our message is in our address book, subscriptions list, or whitelist then we will allow them to do the network-minimum proof of work. Let us check to see if the receiver is in any of those lists. + if isAddressInMyAddressBookSubscriptionsListOrWhitelist(toaddress): + payload += encodeVarint(networkDefaultProofOfWorkNonceTrialsPerByte) + payload += encodeVarint(networkDefaultPayloadLengthExtraBytes) + else: + payload += encodeVarint(config.getint(fromaddress,'noncetrialsperbyte')) + payload += encodeVarint(config.getint(fromaddress,'payloadlengthextrabytes')) 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. @@ -3024,17 +3057,11 @@ class singleWorker(QThread): nonce = 0 trialValue = 99999999999999999999 - - 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 + payload = embeddedTime + encodeVarint(toStreamNumber) + encrypted target = 2**64 / ((len(payload)+requiredPayloadLengthExtraBytes+8) * requiredAverageProofOfWorkNonceTrialsPerByte) printLock.acquire() - print '(For msg message) Doing proof of work. Target:', target - print 'Using requiredAverageProofOfWorkNonceTrialsPerByte', requiredAverageProofOfWorkNonceTrialsPerByte - print 'Using requiredPayloadLengthExtraBytes =', requiredPayloadLengthExtraBytes - print 'The required total difficulty is', float(requiredAverageProofOfWorkNonceTrialsPerByte)/networkDefaultProofOfWorkNonceTrialsPerByte - print 'The required small message difficulty is', float(requiredPayloadLengthExtraBytes)/networkDefaultPayloadLengthExtraBytes + print '(For msg message) Doing proof of work. Total required difficulty:', float(requiredAverageProofOfWorkNonceTrialsPerByte)/networkDefaultProofOfWorkNonceTrialsPerByte,'Required small message difficulty:', float(requiredPayloadLengthExtraBytes)/networkDefaultPayloadLengthExtraBytes printLock.release() powStartTime = time.time() initialHash = hashlib.sha512(payload).digest() @@ -3107,8 +3134,7 @@ class singleWorker(QThread): def generateFullAckMessage(self,ackdata,toStreamNumber,embeddedTime): nonce = 0 trialValue = 99999999999999999999 - encodedStreamNumber = encodeVarint(toStreamNumber) - payload = embeddedTime + encodedStreamNumber + ackdata + payload = embeddedTime + encodeVarint(toStreamNumber) + ackdata target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) printLock.acquire() print '(For ack message) Doing proof of work...' @@ -3146,7 +3172,6 @@ class addressGenerator(QThread): def run(self): if self.addressVersionNumber == 3: - if self.deterministicPassphrase == "": statusbar = 'Generating one new address' self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) From d14be90c3bbfbf6892bcc2e55958e78d76794b16 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 26 Apr 2013 17:12:35 -0400 Subject: [PATCH 09/33] Implimented broadcast encryption (testing) --- src/bitmessagemain.py | 251 +++++++++++++++++++++--------------------- 1 file changed, 123 insertions(+), 128 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 3469f50b..7a77fb41 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -721,121 +721,121 @@ class receiveDataThread(QThread): printLock.acquire() print 'Length of time program spent failing to decrypt this v2 broadcast:', time.time()- self.messageProcessingStartTime, 'seconds.' printLock.release() + return + #At this point this is a broadcast I have decrypted and thus am interested in. + signedBroadcastVersion, readPosition = decodeVarint(decryptedData[:10]) + beginningOfPubkeyPosition = readPosition #used when we add the pubkey to our pubkey table + sendersAddressVersion, sendersAddressVersionLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + if sendersAddressVersion < 2 or sendersAddressVersion > 3: + print 'Cannot decode senderAddressVersion other than 2 or 3. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.' + return + readPosition += sendersAddressVersionLength + sendersStream, sendersStreamLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + if sendersStream != cleartextStreamNumber: + print 'The stream number outside of the encryption on which the POW was completed doesn\'t match the stream number inside the encryption. Ignoring broadcast.' + return + readPosition += sendersStreamLength + behaviorBitfield = decryptedData[readPosition:readPosition+4] + readPosition += 4 + sendersPubSigningKey = '\x04' + decryptedData[readPosition:readPosition+64] + readPosition += 64 + sendersPubEncryptionKey = '\x04' + decryptedData[readPosition:readPosition+64] + readPosition += 64 + if sendersAddressVersion >= 3: + requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(decryptedData[readPosition:readPosition+10]) + readPosition += varintLength + print 'sender\'s requiredAverageProofOfWorkNonceTrialsPerByte is', requiredAverageProofOfWorkNonceTrialsPerByte + requiredPayloadLengthExtraBytes, varintLength = decodeVarint(decryptedData[readPosition:readPosition+10]) + readPosition += varintLength + print 'sender\'s requiredPayloadLengthExtraBytes is', requiredPayloadLengthExtraBytes + endOfPubkeyPosition = readPosition + + sha = hashlib.new('sha512') + sha.update(sendersPubSigningKey+sendersPubEncryptionKey) + ripe = hashlib.new('ripemd160') + ripe.update(sha.digest()) + + if toRipe != ripe.digest(): + print 'The encryption key used to encrypt this message doesn\'t match the keys inbedded in the message itself. Ignoring message.' + return else: - #This is a broadcast I have decrypted and thus am interested in. - signedBroadcastVersion, readPosition = decodeVarint(decryptedData[:10]) - beginningOfPubkeyPosition = readPosition #used when we add the pubkey to our pubkey table - sendersAddressVersion, sendersAddressVersionLength = decodeVarint(decryptedData[readPosition:readPosition+9]) - if sendersAddressVersion < 2 or sendersAddressVersion > 3: - print 'Cannot decode senderAddressVersion other than 2 or 3. Assuming the sender isn\' being silly, you should upgrade Bitmessage because this message shall be ignored.' - return - readPosition += sendersAddressVersionLength - sendersStream, sendersStreamLength = decodeVarint(decryptedData[readPosition:readPosition+9]) - if sendersStream != cleartextStreamNumber: - print 'The stream number outside of the encryption on which the POW was completed doesn\'t match the stream number inside the encryption. Ignoring broadcast.' - return - readPosition += sendersStreamLength - behaviorBitfield = decryptedData[readPosition:readPosition+4] - readPosition += 4 - sendersPubSigningKey = '\x04' + decryptedData[readPosition:readPosition+64] - readPosition += 64 - sendersPubEncryptionKey = '\x04' + decryptedData[readPosition:readPosition+64] - readPosition += 64 - if sendersAddressVersion >= 3: - requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(decryptedData[readPosition:readPosition+10]) - readPosition += varintLength - print 'sender\'s requiredAverageProofOfWorkNonceTrialsPerByte is', requiredAverageProofOfWorkNonceTrialsPerByte - requiredPayloadLengthExtraBytes, varintLength = decodeVarint(decryptedData[readPosition:readPosition+10]) - readPosition += varintLength - print 'sender\'s requiredPayloadLengthExtraBytes is', requiredPayloadLengthExtraBytes - endOfPubkeyPosition = readPosition + print 'The encryption key DOES match the keys in the message.' - sha = hashlib.new('sha512') - sha.update(sendersPubSigningKey+sendersPubEncryptionKey) - ripe = hashlib.new('ripemd160') - ripe.update(sha.digest()) + messageEncodingType, messageEncodingTypeLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + if messageEncodingType == 0: + return + readPosition += messageEncodingTypeLength + messageLength, messageLengthLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + readPosition += messageLengthLength + message = decryptedData[readPosition:readPosition+messageLength] + readPosition += messageLength + readPositionAtBottomOfMessage = readPosition + signatureLength, signatureLengthLength = decodeVarint(decryptedData[readPosition:readPosition+9]) + readPosition += signatureLengthLength + signature = decryptedData[readPosition:readPosition+signatureLength] + try: + highlevelcrypto.verify(decryptedData[:readPositionAtBottomOfMessage],signature,sendersPubSigningKey.encode('hex')) + print 'ECDSA verify passed' + except Exception, err: + print 'ECDSA verify failed', err + return + #verify passed - if toRipe != ripe.digest(): - print 'The encryption key used to encrypt this message doesn\'t match the keys inbedded in the message itself. Ignoring message.' - return + #Let's store the public key in case we want to reply to this person. + t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+decryptedData[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') + sqlLock.acquire() + sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + sqlSubmitQueue.put(t) + sqlReturnQueue.get() + sqlSubmitQueue.put('commit') + 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. + + fromAddress = encodeAddress(sendersAddressVersion,sendersStream,ripe.digest()) + printLock.acquire() + print 'fromAddress:', fromAddress + printLock.release() + if messageEncodingType == 2: + bodyPositionIndex = string.find(message,'\nBody:') + if bodyPositionIndex > 1: + subject = message[8:bodyPositionIndex] + body = message[bodyPositionIndex+6:] else: - print 'The encryption key DOES match the keys in the message.' + subject = '' + body = message + elif messageEncodingType == 1: + body = message + subject = '' + elif messageEncodingType == 0: + print 'messageEncodingType == 0. Doing nothing with the message.' + else: + body = 'Unknown encoding type.\n\n' + repr(message) + subject = '' - messageEncodingType, messageEncodingTypeLength = decodeVarint(decryptedData[readPosition:readPosition+9]) - if messageEncodingType == 0: - return - readPosition += messageEncodingTypeLength - messageLength, messageLengthLength = decodeVarint(decryptedData[readPosition:readPosition+9]) - readPosition += messageLengthLength - message = decryptedData[readPosition:readPosition+messageLength] - readPosition += messageLength - readPositionAtBottomOfMessage = readPosition - signatureLength, signatureLengthLength = decodeVarint(decryptedData[readPosition:readPosition+9]) - readPosition += signatureLengthLength - signature = decryptedData[readPosition:readPosition+signatureLength] - try: - highlevelcrypto.verify(decryptedData[:readPositionAtBottomOfMessage],signature,sendersPubSigningKey.encode('hex')) - print 'ECDSA verify passed' - except Exception, err: - print 'ECDSA verify failed', err - return - #verify passed - - #Let's store the public key in case we want to reply to this person. - t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+decryptedData[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') + toAddress = '[Broadcast subscribers]' + if messageEncodingType <> 0: sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + t = (self.inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox',messageEncodingType,0) + sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') sqlSubmitQueue.put(t) sqlReturnQueue.get() sqlSubmitQueue.put('commit') 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. + self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) - fromAddress = encodeAddress(sendersAddressVersion,sendersStream,ripe.digest()) - printLock.acquire() - print 'fromAddress:', fromAddress - printLock.release() - if messageEncodingType == 2: - bodyPositionIndex = string.find(message,'\nBody:') - 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.' - else: - body = 'Unknown encoding type.\n\n' + repr(message) - subject = '' + #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"]) - toAddress = '[Broadcast subscribers]' - if messageEncodingType <> 0: - sqlLock.acquire() - t = (self.inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox',messageEncodingType,0) - sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) - - #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"]) - - #Display timing data - printLock.acquire() - print 'Time spent processing this interesting broadcast:', time.time()- self.messageProcessingStartTime - printLock.release() + #Display timing data + printLock.acquire() + print 'Time spent processing this interesting broadcast:', time.time()- self.messageProcessingStartTime + printLock.release() #We have received a msg message. @@ -950,15 +950,7 @@ class receiveDataThread(QThread): printLock.release() else: #This is a message bound for me. - - #If this message is bound for one of my version 3 addresses (or higher), then we must check to make sure it meets our demanded proof of work requirement. toAddress = myAddressesByHash[toRipe] #Look up my address based on the RIPE hash. - if decodeAddress(toAddress)[1] >= 3:#If the toAddress version number is 3 or higher: - requiredNonceTrialsPerByte = config.getint(toAddress,'noncetrialsperbyte') - requiredPayloadLengthExtraBytes = config.getint(toAddress,'payloadlengthextrabytes') - if not self.isProofOfWorkSufficient(encryptedData,requiredNonceTrialsPerByte,requiredPayloadLengthExtraBytes): - print 'Proof of work in msg message insufficient only because it does not meet our higher requirement.' - return readPosition = 0 messageVersion, messageVersionLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += messageVersionLength @@ -1042,37 +1034,38 @@ class receiveDataThread(QThread): sqlSubmitQueue.put('commit') sqlLock.release() 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. - 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 this message is bound for one of my version 3 addresses (or higher), then we must check to make sure it meets our demanded proof of work requirement. + if decodeAddress(toAddress)[1] >= 3:#If the toAddress version number is 3 or higher: + if not isAddressInMyAddressBookSubscriptionsListOrWhitelist(fromAddress): #If I'm not friendly with this person: + requiredNonceTrialsPerByte = config.getint(toAddress,'noncetrialsperbyte') + requiredPayloadLengthExtraBytes = config.getint(toAddress,'payloadlengthextrabytes') + if not self.isProofOfWorkSufficient(encryptedData,requiredNonceTrialsPerByte,requiredPayloadLengthExtraBytes): + print 'Proof of work in msg message insufficient only because it does not meet our higher requirement.' + return + blockMessage = False #Gets set to True if the user shouldn't see the message according to black or white lists. 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('''SELECT label FROM blacklist where address=? and enabled='1' ''') sqlSubmitQueue.put(t) queryreturn = sqlReturnQueue.get() sqlLock.release() - for row in queryreturn: - label, enabled = row - if enabled: - printLock.acquire() - print 'Message ignored because address is in blacklist.' - printLock.release() - blockMessage = True + if queryreturn != []: + printLock.acquire() + print 'Message ignored because address is in blacklist.' + printLock.release() + blockMessage = True else: #We're using a whitelist t = (fromAddress,) sqlLock.acquire() - sqlSubmitQueue.put('''SELECT label, enabled FROM whitelist where address=?''') + sqlSubmitQueue.put('''SELECT label FROM whitelist where address=? and enabled='1' ''') 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]) @@ -1514,7 +1507,9 @@ class receiveDataThread(QThread): #Send a getdata message to our peer to request the object with the given hash def sendgetdata(self,hash): + printLock.acquire() print 'sending getdata to retrieve object with hash:', hash.encode('hex') + printLock.release() payload = '\x01' + hash headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'getdata\x00\x00\x00\x00\x00' @@ -1990,6 +1985,7 @@ class sendDataThread(QThread): self.HOST = HOST self.PORT = PORT self.streamNumber = streamNumber + self.remoteProtocolVersion = -1 #This must be set using setRemoteProtocolVersion command which is sent through the self.mailbox queue. self.lastTimeISentData = int(time.time()) #If this value increases beyond five minutes ago, we'll send a pong message to keep the connection alive. self.objectsOfWhichThisRemoteNodeIsAlreadyAware = objectsOfWhichThisRemoteNodeIsAlreadyAware printLock.acquire() @@ -2092,7 +2088,7 @@ class sendDataThread(QThread): break else: printLock.acquire() - print 'sendDataThread ID:',id(self),'ignoring command', command,'because it is not in stream',deststream + print 'sendDataThread ID:',id(self),'ignoring command', command,'because the thread is not in stream',deststream printLock.release() @@ -5573,7 +5569,6 @@ class MyForm(QtGui.QMainWindow): if addressVersionNumber == 2: broadcastSendersForWhichImWatching[hash] = 0 #Now, for all addresses, even version 2 addresses, we should create Cryptor objects in a dictionary which we will use to attempt to decrypt encrypted broadcast messages. - print '(Within reloadBroadcastSendersForWhichImWatching) The string that we will hash to make the privEncryptionKey is', (encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).encode('hex') privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32] MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) From e6438a9df3834743f37415b4826380103cabdce4 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 26 Apr 2013 17:58:46 -0400 Subject: [PATCH 10/33] Implimented broadcast encryption (testing completed) --- src/bitmessagemain.py | 7 ++++--- src/specialaddressbehavior.py | 4 ++-- src/specialaddressbehavior.ui | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 7a77fb41..7f734cee 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -758,9 +758,6 @@ class receiveDataThread(QThread): if toRipe != ripe.digest(): print 'The encryption key used to encrypt this message doesn\'t match the keys inbedded in the message itself. Ignoring message.' return - else: - print 'The encryption key DOES match the keys in the message.' - messageEncodingType, messageEncodingTypeLength = decodeVarint(decryptedData[readPosition:readPosition+9]) if messageEncodingType == 0: return @@ -2407,6 +2404,9 @@ class sqlThread(QThread): self.cur.execute( '''DROP TABLE pubkeys_backup;''') print 'Deleting all pubkeys from inventory. They will be redownloaded and then saved with the correct times.' self.cur.execute( '''delete from inventory where objecttype = 'pubkey';''') + #print 'replacing Bitmessage announcements mailing list with a new one.' + #self.cur.execute( '''delete from subscriptions where address='BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx' ''') + #self.cur.execute( '''INSERT INTO subscriptions VALUES('Bitmessage new releases/announcements','BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx',1)''') print 'Commiting.' self.conn.commit() print 'Vacuuming message.dat. You might notice that the file size gets much smaller.' @@ -5558,6 +5558,7 @@ class MyForm(QtGui.QMainWindow): def reloadBroadcastSendersForWhichImWatching(self): broadcastSendersForWhichImWatching.clear() + MyECSubscriptionCryptorObjects.clear() sqlLock.acquire() sqlSubmitQueue.put('SELECT address FROM subscriptions where enabled=1') sqlSubmitQueue.put('') diff --git a/src/specialaddressbehavior.py b/src/specialaddressbehavior.py index 828ba070..78ff890d 100644 --- a/src/specialaddressbehavior.py +++ b/src/specialaddressbehavior.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'specialaddressbehavior.ui' # -# Created: Fri Mar 29 15:02:24 2013 +# Created: Fri Apr 26 17:43:31 2013 # by: PyQt4 UI code generator 4.9.4 # # WARNING! All changes made in this file will be lost! @@ -59,6 +59,6 @@ class Ui_SpecialAddressBehaviorDialog(object): SpecialAddressBehaviorDialog.setWindowTitle(QtGui.QApplication.translate("SpecialAddressBehaviorDialog", "Special Address Behavior", None, QtGui.QApplication.UnicodeUTF8)) self.radioButtonBehaveNormalAddress.setText(QtGui.QApplication.translate("SpecialAddressBehaviorDialog", "Behave as a normal address", None, QtGui.QApplication.UnicodeUTF8)) self.radioButtonBehaviorMailingList.setText(QtGui.QApplication.translate("SpecialAddressBehaviorDialog", "Behave as a pseudo-mailing-list address", None, QtGui.QApplication.UnicodeUTF8)) - self.label.setText(QtGui.QApplication.translate("SpecialAddressBehaviorDialog", "Mail received to a pseudo-mailing-list address will be automatically broadcast to subscribers (and thus will be unencrypted and public).", None, QtGui.QApplication.UnicodeUTF8)) + self.label.setText(QtGui.QApplication.translate("SpecialAddressBehaviorDialog", "Mail received to a pseudo-mailing-list address will be automatically broadcast to subscribers (and thus will be public).", None, QtGui.QApplication.UnicodeUTF8)) self.label_2.setText(QtGui.QApplication.translate("SpecialAddressBehaviorDialog", "Name of the pseudo-mailing-list:", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/src/specialaddressbehavior.ui b/src/specialaddressbehavior.ui index 89402639..eddc1184 100644 --- a/src/specialaddressbehavior.ui +++ b/src/specialaddressbehavior.ui @@ -34,7 +34,7 @@ - Mail received to a pseudo-mailing-list address will be automatically broadcast to subscribers (and thus will be unencrypted and public). + Mail received to a pseudo-mailing-list address will be automatically broadcast to subscribers (and thus will be public). true From 5c4669b39ee1739d7b2cb624e389f877fa29a153 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Mon, 29 Apr 2013 12:46:33 -0400 Subject: [PATCH 11/33] use sock.shutdown() before sock.close() --- src/bitmessagemain.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 7f734cee..2be394e0 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -283,14 +283,15 @@ class receiveDataThread(QThread): try: + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() except Exception, err: - print 'Within receiveDataThread run(), self.sock.close() failed.', err + print 'Within receiveDataThread run(), self.sock.shutdown or .close() failed.', err try: del selfInitiatedConnections[self.streamNumber][self] printLock.acquire() - print 'removed self (a receiveDataThread) from ConnectionList' + print 'removed self (a receiveDataThread) from selfInitiatedConnections' printLock.release() except: pass @@ -449,6 +450,7 @@ class receiveDataThread(QThread): printLock.acquire() print 'We are connected to too many people. Closing connection.' printLock.release() + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() return self.sendBigInv() @@ -1682,11 +1684,11 @@ class receiveDataThread(QThread): if PORT != recaddrPort: 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.' if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers. - output = open(appdata + 'knownnodes.dat', 'wb') knownNodesLock.acquire() + output = open(appdata + 'knownnodes.dat', 'wb') pickle.dump(knownNodes, output) - knownNodesLock.release() output.close() + knownNodesLock.release() self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers) #no longer broadcast printLock.acquire() print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.' @@ -1773,11 +1775,11 @@ class receiveDataThread(QThread): if PORT != recaddrPort: 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.' if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers. - output = open(appdata + 'knownnodes.dat', 'wb') knownNodesLock.acquire() + output = open(appdata + 'knownnodes.dat', 'wb') pickle.dump(knownNodes, output) - knownNodesLock.release() output.close() + knownNodesLock.release() self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers) printLock.acquire() print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.' @@ -1913,6 +1915,7 @@ class receiveDataThread(QThread): print 'Remote node useragent:', useragent, ' stream number:', self.streamNumber printLock.release() if self.streamNumber != 1: + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() printLock.acquire() print 'Closed connection to', self.HOST, 'because they are interested in stream', self.streamNumber,'.' @@ -1922,6 +1925,7 @@ class receiveDataThread(QThread): if not self.initiatedConnection: broadcastToSendDataQueues((0,'setStreamNumber',(self.HOST,self.streamNumber))) if data[72:80] == eightBytesOfRandomDataUsedToDetectConnectionsToSelf: + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() printLock.acquire() print 'Closing connection to myself: ', self.HOST @@ -1933,8 +1937,8 @@ class receiveDataThread(QThread): knownNodes[self.streamNumber][self.HOST] = (self.remoteNodeIncomingPort, int(time.time())) output = open(appdata + 'knownnodes.dat', 'wb') pickle.dump(knownNodes, output) - knownNodesLock.release() output.close() + knownNodesLock.release() self.sendverack() if self.initiatedConnection == False: @@ -2011,6 +2015,7 @@ class sendDataThread(QThread): if data == self.HOST or data == 'all': printLock.acquire() print 'sendDataThread thread (associated with', self.HOST,') ID:',id(self), 'shutting down now.' + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() sendDataQueues.remove(self.mailbox) print 'len of sendDataQueues', len(sendDataQueues) @@ -2045,6 +2050,7 @@ class sendDataThread(QThread): self.lastTimeISentData = int(time.time()) except: print 'self.sock.sendall failed' + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() sendDataQueues.remove(self.mailbox) print 'sendDataThread thread', self, 'ending now' @@ -2064,6 +2070,7 @@ class sendDataThread(QThread): self.lastTimeISentData = int(time.time()) except: print 'self.sock.sendall failed' + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() sendDataQueues.remove(self.mailbox) print 'sendDataThread thread', self, 'ending now' @@ -2079,6 +2086,7 @@ class sendDataThread(QThread): self.lastTimeISentData = int(time.time()) except: print 'self.sock.send pong failed' + self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() sendDataQueues.remove(self.mailbox) print 'sendDataThread thread', self, 'ending now' @@ -5662,6 +5670,8 @@ if __name__ == "__main__": config.set('bitmessagesettings','sockspassword','') config.set('bitmessagesettings','keysencrypted','false') config.set('bitmessagesettings','messagesencrypted','false') + config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) + config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) if storeConfigFilesInSameDirectoryAsProgramByDefault: #Just use the same directory as the program and forget about the appdata folder From 57f602a3734a4a33ff886c465b036c58d2c90948 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Mon, 29 Apr 2013 13:46:09 -0400 Subject: [PATCH 12/33] use sock.shutdown() before sock.close() --- src/bitmessagemain.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 2be394e0..63237e42 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -92,6 +92,8 @@ class outgoingSynSender(QThread): alreadyAttemptedConnectionsListLock.release() PORT, timeNodeLastSeen = knownNodes[self.streamNumber][HOST] sock = socks.socksocket(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.settimeout(20) if config.get('bitmessagesettings', 'socksproxytype') == 'none' and verbose >= 2: printLock.acquire() @@ -283,7 +285,7 @@ class receiveDataThread(QThread): try: - self.sock.shutdown(socket.SHUT_RDWR) + #self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() except Exception, err: print 'Within receiveDataThread run(), self.sock.shutdown or .close() failed.', err @@ -2014,10 +2016,13 @@ class sendDataThread(QThread): if command == 'shutdown': if data == self.HOST or data == 'all': printLock.acquire() - print 'sendDataThread thread (associated with', self.HOST,') ID:',id(self), 'shutting down now.' + print 'sendDataThread (associated with', self.HOST,') ID:',id(self), 'shutting down now.' + printLock.release() self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() + print 'sendDataThread closed socket.' sendDataQueues.remove(self.mailbox) + printLock.acquire() print 'len of sendDataQueues', len(sendDataQueues) printLock.release() break @@ -5124,9 +5129,13 @@ class MyForm(QtGui.QMainWindow): knownNodesLock.acquire() self.statusBar().showMessage('Saving the knownNodes list of peers to disk...') output = open(appdata + 'knownnodes.dat', 'wb') + print 'finished opening knownnodes.dat. Now pickle.dump' pickle.dump(knownNodes, output) + print 'done with pickle.dump. closing output' output.close() knownNodesLock.release() + print 'done closing knownnodes.dat output file.' + self.statusBar().showMessage('Done saving the knownNodes list of peers to disk.') broadcastToSendDataQueues((0, 'shutdown', 'all')) From ddf347ecc56d0658a8413a2cee25a22917301926 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Mon, 29 Apr 2013 14:12:15 -0400 Subject: [PATCH 13/33] test socket.close change --- src/bitmessagemain.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 63237e42..7dbeefee 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -284,11 +284,11 @@ class receiveDataThread(QThread): - try: + """try: #self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() except Exception, err: - print 'Within receiveDataThread run(), self.sock.shutdown or .close() failed.', err + print 'Within receiveDataThread run(), self.sock.shutdown or .close() failed.', err""" try: del selfInitiatedConnections[self.streamNumber][self] @@ -452,8 +452,9 @@ class receiveDataThread(QThread): printLock.acquire() print 'We are connected to too many people. Closing connection.' printLock.release() - self.sock.shutdown(socket.SHUT_RDWR) - self.sock.close() + #self.sock.shutdown(socket.SHUT_RDWR) + #self.sock.close() + broadcastToSendDataQueues((0, 'shutdown', self.HOST)) return self.sendBigInv() @@ -1917,8 +1918,9 @@ class receiveDataThread(QThread): print 'Remote node useragent:', useragent, ' stream number:', self.streamNumber printLock.release() if self.streamNumber != 1: - self.sock.shutdown(socket.SHUT_RDWR) - self.sock.close() + #self.sock.shutdown(socket.SHUT_RDWR) + #self.sock.close() + broadcastToSendDataQueues((0, 'shutdown', self.HOST)) printLock.acquire() print 'Closed connection to', self.HOST, 'because they are interested in stream', self.streamNumber,'.' printLock.release() @@ -1927,8 +1929,9 @@ class receiveDataThread(QThread): if not self.initiatedConnection: broadcastToSendDataQueues((0,'setStreamNumber',(self.HOST,self.streamNumber))) if data[72:80] == eightBytesOfRandomDataUsedToDetectConnectionsToSelf: - self.sock.shutdown(socket.SHUT_RDWR) - self.sock.close() + #self.sock.shutdown(socket.SHUT_RDWR) + #self.sock.close() + broadcastToSendDataQueues((0, 'shutdown', self.HOST)) printLock.acquire() print 'Closing connection to myself: ', self.HOST printLock.release() @@ -2020,7 +2023,6 @@ class sendDataThread(QThread): printLock.release() self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() - print 'sendDataThread closed socket.' sendDataQueues.remove(self.mailbox) printLock.acquire() print 'len of sendDataQueues', len(sendDataQueues) From 32d8a78fc7023db2d25bb1ec443bcb20de28f325 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Mon, 29 Apr 2013 15:20:56 -0400 Subject: [PATCH 14/33] test socket.close change --- src/bitmessagemain.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 7dbeefee..09e8b6eb 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -259,7 +259,9 @@ class receiveDataThread(QThread): self.objectsOfWhichThisRemoteNodeIsAlreadyAware = objectsOfWhichThisRemoteNodeIsAlreadyAware def run(self): - + printLock.acquire() + print 'The size of the connectedHostsList is now', len(connectedHostsList) + printLock.release() while True: try: self.data += self.sock.recv(4096) @@ -444,6 +446,9 @@ class receiveDataThread(QThread): remoteNodeIncomingPort, remoteNodeSeenTime = knownNodes[self.streamNumber][self.HOST] printLock.acquire() print 'Connection fully established with', self.HOST, remoteNodeIncomingPort + print 'ConnectionsCount now:', connectionsCount[self.streamNumber] + print 'The size of the connectedHostsList is now', len(connectedHostsList) + print 'The length of sendDataQueues is now:', len(sendDataQueues) print 'broadcasting addr from within connectionFullyEstablished function.' printLock.release() self.broadcastaddr([(int(time.time()), self.streamNumber, 1, self.HOST, remoteNodeIncomingPort)]) #This lets all of our peers know about this new node. @@ -1984,6 +1989,7 @@ class sendDataThread(QThread): QThread.__init__(self, parent) self.mailbox = Queue.Queue() sendDataQueues.append(self.mailbox) + print 'The length of sendDataQueues is now:', len(sendDataQueues) self.data = '' def setup(self,sock,HOST,PORT,streamNumber,objectsOfWhichThisRemoteNodeIsAlreadyAware): From 8e042930f266c7707b1906deaf34f64c46fdd670 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Mon, 29 Apr 2013 17:20:09 -0400 Subject: [PATCH 15/33] add a simple printLock --- src/bitmessagemain.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 09e8b6eb..ab9a134d 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -1989,7 +1989,9 @@ class sendDataThread(QThread): QThread.__init__(self, parent) self.mailbox = Queue.Queue() sendDataQueues.append(self.mailbox) - print 'The length of sendDataQueues is now:', len(sendDataQueues) + printLock.acquire() + print 'The length of sendDataQueues at sendDataThread init is:', len(sendDataQueues) + printLock.release() self.data = '' def setup(self,sock,HOST,PORT,streamNumber,objectsOfWhichThisRemoteNodeIsAlreadyAware): From 0b78e3663917d293c4ec895d55b57191910d7eef Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Tue, 30 Apr 2013 10:54:30 -0400 Subject: [PATCH 16/33] added extra statements for troubleshooting --- src/bitmessagemain.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index ab9a134d..a806627f 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -260,25 +260,25 @@ class receiveDataThread(QThread): def run(self): printLock.acquire() - print 'The size of the connectedHostsList is now', len(connectedHostsList) + print 'ID of the receiveDataThread is', str(id(self))+'. The size of the connectedHostsList is now', len(connectedHostsList) printLock.release() while True: try: self.data += self.sock.recv(4096) except socket.timeout: printLock.acquire() - print 'Timeout occurred waiting for data. Closing receiveData thread.' + print 'Timeout occurred waiting for data from', self.HOST + '. Closing receiveData thread. (ID:',str(id(self))+ ')' printLock.release() break except Exception, err: printLock.acquire() - print 'sock.recv error. Closing receiveData thread.', err + print 'sock.recv error. Closing receiveData thread (HOST:', self.HOST, 'ID:',str(id(self))+ ').', err printLock.release() break #print 'Received', repr(self.data) if self.data == "": printLock.acquire() - print 'Connection closed. Closing receiveData thread.' + print 'Connection to', self.HOST, 'closed. Closing receiveData thread. (ID:',str(id(self))+ ')' printLock.release() break else: @@ -2003,7 +2003,7 @@ class sendDataThread(QThread): self.lastTimeISentData = int(time.time()) #If this value increases beyond five minutes ago, we'll send a pong message to keep the connection alive. self.objectsOfWhichThisRemoteNodeIsAlreadyAware = objectsOfWhichThisRemoteNodeIsAlreadyAware printLock.acquire() - print 'The streamNumber of this sendDataThread (ID:', id(self),') at setup() is', self.streamNumber + print 'The streamNumber of this sendDataThread (ID:', str(id(self))+') at setup() is', self.streamNumber printLock.release() def sendVersionMessage(self): @@ -2068,7 +2068,7 @@ class sendDataThread(QThread): self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() sendDataQueues.remove(self.mailbox) - print 'sendDataThread thread', self, 'ending now' + print 'sendDataThread thread (ID:',str(id(self))+') ending now. Was connected to', self.HOST break elif command == 'sendinv': if data not in self.objectsOfWhichThisRemoteNodeIsAlreadyAware: @@ -2088,7 +2088,7 @@ class sendDataThread(QThread): self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() sendDataQueues.remove(self.mailbox) - print 'sendDataThread thread', self, 'ending now' + print 'sendDataThread thread (ID:',str(id(self))+') ending now. Was connected to', self.HOST break elif command == 'pong': if self.lastTimeISentData < (int(time.time()) - 298): @@ -2100,11 +2100,11 @@ class sendDataThread(QThread): 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') self.lastTimeISentData = int(time.time()) except: - print 'self.sock.send pong failed' + print 'send pong failed' self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() sendDataQueues.remove(self.mailbox) - print 'sendDataThread thread', self, 'ending now' + print 'sendDataThread thread', self, 'ending now. Was connected to', self.HOST break else: printLock.acquire() From 5d7c5f0c2bd47339559d55cdb76c2d082fb0113c Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Tue, 30 Apr 2013 12:22:47 -0400 Subject: [PATCH 17/33] set hard date for encrypted-broadcast switchover --- src/bitmessagemain.py | 29 +++++++++++++++++++++++------ src/newaddressdialog.py | 4 ++-- src/newaddressdialog.ui | 2 +- src/regenerateaddresses.py | 4 ++-- src/regenerateaddresses.ui | 2 +- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index a806627f..4a2440f1 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -15,6 +15,7 @@ maximumAgeOfObjectsThatIAdvertiseToOthers = 216000 #Equals two days and 12 hours maximumAgeOfNodesThatIAdvertiseToOthers = 10800 #Equals three hours 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. useVeryEasyProofOfWorkForTesting = False #If you set this to True while on the normal network, you won't be able to send or sometimes receive messages. +encryptedBroadcastSwitchoverTime = 1369735200 import sys try: @@ -2360,9 +2361,10 @@ class sqlThread(QThread): self.cur.execute( '''CREATE TABLE pubkeys (hash blob, transmitdata blob, time int, usedpersonally text, UNIQUE(hash) ON CONFLICT REPLACE)''' ) self.cur.execute( '''CREATE TABLE inventory (hash blob, objecttype text, streamnumber int, payload blob, receivedtime integer, UNIQUE(hash) ON CONFLICT REPLACE)''' ) 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. - self.cur.execute( '''INSERT INTO subscriptions VALUES('Bitmessage new releases/announcements','BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx',1)''') + self.cur.execute( '''INSERT INTO subscriptions VALUES('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') self.cur.execute( '''CREATE TABLE settings (key blob, value blob, UNIQUE(key) ON CONFLICT REPLACE)''' ) self.cur.execute( '''INSERT INTO settings VALUES('version','1')''') + self.cur.execute( '''INSERT INTO settings VALUES('lastvacuumtime',?)''',(int(time.time()),)) self.conn.commit() print 'Created messages database file' except Exception, err: @@ -2418,6 +2420,7 @@ class sqlThread(QThread): print 'In messages.dat database, creating new \'settings\' table.' self.cur.execute( '''CREATE TABLE settings (key text, value blob, UNIQUE(key) ON CONFLICT REPLACE)''' ) self.cur.execute( '''INSERT INTO settings VALUES('version','1')''') + self.cur.execute( '''INSERT INTO settings VALUES('lastvacuumtime',?)''',(int(time.time()),)) print 'In messages.dat database, removing an obsolete field from the pubkeys table.' self.cur.execute( '''CREATE TEMPORARY TABLE pubkeys_backup(hash blob, transmitdata blob, time int, usedpersonally text, UNIQUE(hash) ON CONFLICT REPLACE);''') self.cur.execute( '''INSERT INTO pubkeys_backup SELECT hash, transmitdata, time, usedpersonally FROM pubkeys;''') @@ -2427,9 +2430,9 @@ class sqlThread(QThread): self.cur.execute( '''DROP TABLE pubkeys_backup;''') print 'Deleting all pubkeys from inventory. They will be redownloaded and then saved with the correct times.' self.cur.execute( '''delete from inventory where objecttype = 'pubkey';''') - #print 'replacing Bitmessage announcements mailing list with a new one.' - #self.cur.execute( '''delete from subscriptions where address='BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx' ''') - #self.cur.execute( '''INSERT INTO subscriptions VALUES('Bitmessage new releases/announcements','BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx',1)''') + print 'replacing Bitmessage announcements mailing list with a new one.' + self.cur.execute( '''delete from subscriptions where address='BM-BbkPSZbzPwpVcYZpU4yHwf9ZPEapN5Zx' ''') + self.cur.execute( '''INSERT INTO subscriptions VALUES('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') print 'Commiting.' self.conn.commit() print 'Vacuuming message.dat. You might notice that the file size gets much smaller.' @@ -2453,6 +2456,20 @@ class sqlThread(QThread): except Exception, err: print err + #Let us check to see the last time we vaccumed the messages.dat file. If it has been more than a month let's do it now. + item = '''SELECT value FROM settings WHERE key='lastvacuumtime';''' + parameters = '' + self.cur.execute(item, parameters) + queryreturn = self.cur.fetchall() + for row in queryreturn: + value, = row + if int(value) < int(time.time()) - 2592000: + print 'It has been a long time since the messages.dat file has been vacuumed. Vacuuming now...' + self.cur.execute( ''' VACUUM ''') + item = '''update settings set value=? WHERE key='lastvacuumtime';''' + parameters = (int(time.time()),) + self.cur.execute(item, parameters) + while True: item = sqlSubmitQueue.get() if item == 'commit': @@ -2797,7 +2814,7 @@ class singleWorker(QThread): for row in queryreturn: fromaddress, subject, body, ackdata = row status,addressVersionNumber,streamNumber,ripe = decodeAddress(fromaddress) - if addressVersionNumber == 2:#todo: and time is less than some date in the future: + if addressVersionNumber == 2 and int(time.time()) < encryptedBroadcastSwitchoverTime: #We need to convert our private keys to public keys in order to include them. try: privSigningKeyBase58 = config.get(fromaddress, 'privsigningkey') @@ -2857,7 +2874,7 @@ class singleWorker(QThread): queryreturn = sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - elif addressVersionNumber == 3:#todo: or (addressVersionNumber == 2 and time is greater than than some date in the future): + elif addressVersionNumber == 3 or int(time.time()) > encryptedBroadcastSwitchoverTime: #We need to convert our private keys to public keys in order to include them. try: privSigningKeyBase58 = config.get(fromaddress, 'privsigningkey') diff --git a/src/newaddressdialog.py b/src/newaddressdialog.py index e7c1ad71..637c3a46 100644 --- a/src/newaddressdialog.py +++ b/src/newaddressdialog.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'newaddressdialog.ui' # -# Created: Fri Jan 25 13:05:18 2013 +# Created: Tue Apr 30 12:21:14 2013 # by: PyQt4 UI code generator 4.9.4 # # WARNING! All changes made in this file will be lost! @@ -169,7 +169,7 @@ class Ui_NewAddressDialog(object): self.radioButtonDeterministicAddress.setText(QtGui.QApplication.translate("NewAddressDialog", "Use a passpharase to make addresses", None, QtGui.QApplication.UnicodeUTF8)) self.checkBoxEighteenByteRipe.setText(QtGui.QApplication.translate("NewAddressDialog", "Spend several minutes of extra computing time to make the address(es) 1 or 2 characters shorter", None, QtGui.QApplication.UnicodeUTF8)) self.groupBoxDeterministic.setTitle(QtGui.QApplication.translate("NewAddressDialog", "Make deterministic addresses", None, QtGui.QApplication.UnicodeUTF8)) - self.label_9.setText(QtGui.QApplication.translate("NewAddressDialog", "Address version number: 2", None, QtGui.QApplication.UnicodeUTF8)) + self.label_9.setText(QtGui.QApplication.translate("NewAddressDialog", "Address version number: 3", None, QtGui.QApplication.UnicodeUTF8)) self.label_8.setText(QtGui.QApplication.translate("NewAddressDialog", "In addition to your passphrase, you must remember these numbers:", None, QtGui.QApplication.UnicodeUTF8)) self.label_6.setText(QtGui.QApplication.translate("NewAddressDialog", "Passphrase", None, QtGui.QApplication.UnicodeUTF8)) self.label_11.setText(QtGui.QApplication.translate("NewAddressDialog", "Number of addresses to make based on your passphrase:", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/src/newaddressdialog.ui b/src/newaddressdialog.ui index 7f9e13af..3da926ae 100644 --- a/src/newaddressdialog.ui +++ b/src/newaddressdialog.ui @@ -99,7 +99,7 @@ The 'Random Number' option is selected by default but deterministic addresses ha - Address version number: 2 + Address version number: 3 diff --git a/src/regenerateaddresses.py b/src/regenerateaddresses.py index e47f6b6e..dfcd9135 100644 --- a/src/regenerateaddresses.py +++ b/src/regenerateaddresses.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'regenerateaddresses.ui' # -# Created: Thu Jan 24 15:52:24 2013 +# Created: Tue Apr 30 12:21:16 2013 # by: PyQt4 UI code generator 4.9.4 # # WARNING! All changes made in this file will be lost! @@ -106,7 +106,7 @@ class Ui_regenerateAddressesDialog(object): self.label_6.setText(QtGui.QApplication.translate("regenerateAddressesDialog", "Passphrase", None, QtGui.QApplication.UnicodeUTF8)) self.label_11.setText(QtGui.QApplication.translate("regenerateAddressesDialog", "Number of addresses to make based on your passphrase:", None, QtGui.QApplication.UnicodeUTF8)) self.label_2.setText(QtGui.QApplication.translate("regenerateAddressesDialog", "Address version Number:", None, QtGui.QApplication.UnicodeUTF8)) - self.lineEditAddressVersionNumber.setText(QtGui.QApplication.translate("regenerateAddressesDialog", "2", None, QtGui.QApplication.UnicodeUTF8)) + self.lineEditAddressVersionNumber.setText(QtGui.QApplication.translate("regenerateAddressesDialog", "3", None, QtGui.QApplication.UnicodeUTF8)) self.label_3.setText(QtGui.QApplication.translate("regenerateAddressesDialog", "Stream number:", None, QtGui.QApplication.UnicodeUTF8)) self.lineEditStreamNumber.setText(QtGui.QApplication.translate("regenerateAddressesDialog", "1", None, QtGui.QApplication.UnicodeUTF8)) self.checkBoxEighteenByteRipe.setText(QtGui.QApplication.translate("regenerateAddressesDialog", "Spend several minutes of extra computing time to make the address(es) 1 or 2 characters shorter", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/src/regenerateaddresses.ui b/src/regenerateaddresses.ui index 2dbecf84..89cdf901 100644 --- a/src/regenerateaddresses.ui +++ b/src/regenerateaddresses.ui @@ -108,7 +108,7 @@ - 2 + 3 From 5b58ff210494edcc987c90cfb3bf72c8cdbae2d2 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Tue, 30 Apr 2013 15:22:36 -0400 Subject: [PATCH 18/33] truncate display of long messages to avoid freezing the UI --- src/bitmessagemain.py | 5 ++++- src/settings.py | 4 ++-- src/settings.ui | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 4a2440f1..e53f57b8 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -5516,7 +5516,10 @@ class MyForm(QtGui.QMainWindow): if decodeAddress(fromAddress)[3] in broadcastSendersForWhichImWatching or isAddressInMyAddressBook(fromAddress): self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) else: - self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) + if len(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) < 30000: + self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject())#Only show the first 30K characters + else: + self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()[:30000]+'\n\nRemainder of the message truncated because it is too long.')#Only show the first 30K characters font = QFont() font.setBold(False) diff --git a/src/settings.py b/src/settings.py index 2c8baef9..3c704251 100644 --- a/src/settings.py +++ b/src/settings.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'settings.ui' # -# Created: Fri Apr 26 13:19:59 2013 +# Created: Tue Apr 30 12:34:58 2013 # by: PyQt4 UI code generator 4.9.4 # # WARNING! All changes made in this file will be lost! @@ -216,7 +216,7 @@ class Ui_settingsDialog(object): self.label_5.setText(QtGui.QApplication.translate("settingsDialog", "Username:", None, QtGui.QApplication.UnicodeUTF8)) self.label_6.setText(QtGui.QApplication.translate("settingsDialog", "Pass:", None, QtGui.QApplication.UnicodeUTF8)) self.tabWidgetSettings.setTabText(self.tabWidgetSettings.indexOf(self.tabNetworkSettings), QtGui.QApplication.translate("settingsDialog", "Network Settings", None, QtGui.QApplication.UnicodeUTF8)) - self.label_8.setText(QtGui.QApplication.translate("settingsDialog", "When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exception: if you add a friend or aquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. ", None, QtGui.QApplication.UnicodeUTF8)) + self.label_8.setText(QtGui.QApplication.translate("settingsDialog", "When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exception: if you add a friend or acquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. ", None, QtGui.QApplication.UnicodeUTF8)) self.label_9.setText(QtGui.QApplication.translate("settingsDialog", "Total difficulty:", None, QtGui.QApplication.UnicodeUTF8)) self.label_11.setText(QtGui.QApplication.translate("settingsDialog", "Small message difficulty:", None, QtGui.QApplication.UnicodeUTF8)) self.label_12.setText(QtGui.QApplication.translate("settingsDialog", "The \'Small message difficulty\' mostly only affects the difficulty of sending small messages. Doubling this value makes it almost twice as difficult to send a small message but doesn\'t really affect large messages.", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/src/settings.ui b/src/settings.ui index 1b8c5eb9..3cef2169 100644 --- a/src/settings.ui +++ b/src/settings.ui @@ -266,7 +266,7 @@ - When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exception: if you add a friend or aquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. + When someone sends you a message, their computer must first complete some work. The difficulty of this work, by default, is 1. You may raise this default for new addresses you create by changing the values here. Any new addresses you create will require senders to meet the higher difficulty. There is one exception: if you add a friend or acquaintance to your address book, Bitmessage will automatically notify them when you next send a message that they need only complete the minimum amount of work: difficulty 1. true From 63f1b6a5c897e53dd1eae312cb825076ad3d5077 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Tue, 30 Apr 2013 15:41:13 -0400 Subject: [PATCH 19/33] truncate display of long messages to avoid freezing the UI --- src/bitmessagemain.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index e53f57b8..14d057ba 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -6,7 +6,7 @@ #Right now, PyBitmessage only support connecting to stream 1. It doesn't yet contain logic to expand into further streams. -softwareVersion = '0.2.8' +softwareVersion = '0.3.0' verbose = 1 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. @@ -5514,12 +5514,15 @@ class MyForm(QtGui.QMainWindow): fromAddress = str(self.ui.tableWidgetInbox.item(currentRow,1).data(Qt.UserRole).toPyObject()) #If we have received this message from either a broadcast address or from someone in our address book, display as HTML if decodeAddress(fromAddress)[3] in broadcastSendersForWhichImWatching or isAddressInMyAddressBook(fromAddress): - self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) + if len(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) < 30000: + self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject())#Only show the first 30K characters + else: + self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()[:30000]+'\n\nDisplay of the remainder of the message truncated because it is too long.')#Only show the first 30K characters else: if len(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) < 30000: self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject())#Only show the first 30K characters else: - self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()[:30000]+'\n\nRemainder of the message truncated because it is too long.')#Only show the first 30K characters + self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()[:30000]+'\n\nDisplay of the remainder of the message truncated because it is too long.')#Only show the first 30K characters font = QFont() font.setBold(False) From 38118818688fe0d6f71d64b190e0a9616df34069 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Tue, 30 Apr 2013 15:52:47 -0400 Subject: [PATCH 20/33] bump version number to 0.3.0 --- Makefile | 2 +- debian.sh | 2 +- debian/files | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 21b9f793..ad8fc52c 100755 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ APP=pybitmessage -VERSION=0.2.8 +VERSION=0.3.0 all: diff --git a/debian.sh b/debian.sh index 4ffe1b87..194d19b2 100755 --- a/debian.sh +++ b/debian.sh @@ -7,7 +7,7 @@ #!/bin/bash APP=pybitmessage -VERSION=0.2.8 +VERSION=0.3.0 ARCH_TYPE=all # Create a source archive diff --git a/debian/files b/debian/files index 471f54a3..881a3f20 100644 --- a/debian/files +++ b/debian/files @@ -1 +1 @@ -pybitmessage_0.2.8-1_all.deb contrib/comm extra +pybitmessage_0.3.0-1_all.deb contrib/comm extra From 08dad3e33d88db5b2d555df6ec46582f1885caa8 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Wed, 1 May 2013 16:06:55 -0400 Subject: [PATCH 21/33] most daemon code done --- src/bitmessagemain.py | 1155 ++++++++++++++++++++++++----------------- 1 file changed, 666 insertions(+), 489 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 14d057ba..71357410 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -18,15 +18,18 @@ useVeryEasyProofOfWorkForTesting = False #If you set this to True while on the n encryptedBroadcastSwitchoverTime = 1369735200 import sys +import ConfigParser + try: from PyQt4.QtCore import * from PyQt4.QtGui import * except Exception, err: - 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).' + 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).' print 'Error message:', err sys.exit() -import ConfigParser + from bitmessageui import * +import ConfigParser from newaddressdialog import * from newsubscriptiondialog import * from regenerateaddresses import * @@ -56,15 +59,25 @@ import highlevelcrypto from pyelliptic.openssl import OpenSSL import ctypes from pyelliptic import arithmetic +import signal #Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully. #The next 3 are used for the API from SimpleXMLRPCServer import * import json from subprocess import call #used when the API must execute an outside program +class iconGlossaryDialog(QtGui.QDialog): + def __init__(self,parent): + QtGui.QWidget.__init__(self, parent) + self.ui = Ui_iconGlossaryDialog() + self.ui.setupUi(self) + self.parent = parent + self.ui.labelPortNumber.setText('You are using TCP port ' + str(config.getint('bitmessagesettings', 'port')) + '. (This can be changed in the settings).') + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + #For each stream to which we connect, several outgoingSynSender threads will exist and will collectively create 8 connections with peers. -class outgoingSynSender(QThread): - def __init__(self, parent = None): - QThread.__init__(self, parent) +class outgoingSynSender(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) def setup(self,streamNumber): self.streamNumber = streamNumber @@ -73,7 +86,7 @@ class outgoingSynSender(QThread): time.sleep(1) global alreadyAttemptedConnectionsListResetTime while True: - #time.sleep(999999)#I sometimes use this to prevent connections for testing. + time.sleep(999999)#I sometimes use this to prevent connections for testing. if len(selfInitiatedConnections[self.streamNumber]) < 8: #maximum number of outgoing connections = 8 random.seed() HOST, = random.sample(knownNodes[self.streamNumber], 1) @@ -135,7 +148,8 @@ class outgoingSynSender(QThread): try: sock.connect((HOST, PORT)) rd = receiveDataThread() - self.emit(SIGNAL("passObjectThrough(PyQt_PyObject)"),rd) + rd.daemon = True # close the main program even if there are threads left + #self.emit(SIGNAL("passObjectThrough(PyQt_PyObject)"),rd) objectsOfWhichThisRemoteNodeIsAlreadyAware = {} rd.setup(sock,HOST,PORT,self.streamNumber,objectsOfWhichThisRemoteNodeIsAlreadyAware) rd.start() @@ -160,7 +174,8 @@ class outgoingSynSender(QThread): knownNodesLock.release() 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)) + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"SOCKS5 Authentication problem: "+str(err)) + UISignalQueue.put(('updateStatusBar',"SOCKS5 Authentication problem: "+str(err))) except socks.Socks5Error, err: pass print 'SOCKS5 error. (It is possible that the server wants authentication).)' ,str(err) @@ -188,9 +203,9 @@ class outgoingSynSender(QThread): time.sleep(0.1) #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) +class singleListener(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) def run(self): @@ -219,7 +234,8 @@ class singleListener(QThread): a.close() a,(HOST,PORT) = sock.accept()""" rd = receiveDataThread() - self.emit(SIGNAL("passObjectThrough(PyQt_PyObject)"),rd) + rd.daemon = True # close the main program even if there are threads left + #self.emit(SIGNAL("passObjectThrough(PyQt_PyObject)"),rd) objectsOfWhichThisRemoteNodeIsAlreadyAware = {} rd.setup(a,HOST,PORT,-1,objectsOfWhichThisRemoteNodeIsAlreadyAware) printLock.acquire() @@ -233,9 +249,9 @@ class singleListener(QThread): #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) +class receiveDataThread(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) self.data = '' self.verackSent = False self.verackReceived = False @@ -304,7 +320,8 @@ class receiveDataThread(QThread): 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]) + #self.emit(SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"),self.streamNumber,connectionsCount[self.streamNumber]) + UISignalQueue.put(('updateNetworkStatusTab',(self.streamNumber,connectionsCount[self.streamNumber]))) printLock.acquire() print 'Updating network status tab with current connections count:', connectionsCount[self.streamNumber] printLock.release() @@ -438,11 +455,13 @@ class receiveDataThread(QThread): def connectionFullyEstablished(self): self.connectionIsOrWasFullyEstablished = True if not self.initiatedConnection: - self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),'green') + #self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),'green') + UISignalQueue.put(('setStatusIcon','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]) + #self.emit(SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"),self.streamNumber,connectionsCount[self.streamNumber]) + UISignalQueue.put(('updateNetworkStatusTab',(self.streamNumber,connectionsCount[self.streamNumber]))) connectionsCountLock.release() remoteNodeIncomingPort, remoteNodeSeenTime = knownNodes[self.streamNumber][self.HOST] printLock.acquire() @@ -570,7 +589,8 @@ class receiveDataThread(QThread): inventory[self.inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) inventoryLock.release() self.broadcastinv(self.inventoryHash) - self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()")) + #self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()")) + UISignalQueue.put(('incrementNumberOfBroadcastsProcessed','no data')) self.processbroadcast(readPosition,data)#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. @@ -698,7 +718,8 @@ class receiveDataThread(QThread): sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + #self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) #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'): @@ -829,7 +850,8 @@ class receiveDataThread(QThread): sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + #self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) #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'): @@ -890,7 +912,9 @@ class receiveDataThread(QThread): inventory[self.inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) inventoryLock.release() self.broadcastinv(self.inventoryHash) - self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) + #self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) + UISignalQueue.put(('incrementNumberOfMessagesProcessed','no data')) + self.processmsg(readPosition,data) #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. @@ -932,7 +956,8 @@ class receiveDataThread(QThread): sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),encryptedData[readPosition:],'Acknowledgement of the message received just now.') + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),encryptedData[readPosition:],'Acknowledgement of the message received just now.') + UISignalQueue.put(('updateSentItemStatusByAckdata',(encryptedData[readPosition:],'Acknowledgement of the message received just now.'))) return else: printLock.acquire() @@ -1106,7 +1131,8 @@ class receiveDataThread(QThread): sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + #self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) + UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) #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'): @@ -1139,7 +1165,8 @@ class receiveDataThread(QThread): sqlSubmitQueue.put('commit') 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) + #self.emit(SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),toAddress,'[Broadcast subscribers]',fromAddress,subject,message,ackdata) + UISignalQueue.put(('displayNewSentMessage',(toAddress,'[Broadcast subscribers]',fromAddress,subject,message,ackdata))) workerQueue.put(('sendbroadcast',(fromAddress,subject,message))) if self.isAckDataValid(ackData): @@ -1232,7 +1259,8 @@ class receiveDataThread(QThread): inventory[inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) inventoryLock.release() self.broadcastinv(inventoryHash) - self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()")) + #self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()")) + UISignalQueue.put(('incrementNumberOfPubkeysProcessed','no data')) self.processpubkey(data) @@ -1985,9 +2013,9 @@ class receiveDataThread(QThread): return False #Every connection to a peer has a sendDataThread (and also a receiveDataThread). -class sendDataThread(QThread): - def __init__(self, parent = None): - QThread.__init__(self, parent) +class sendDataThread(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) self.mailbox = Queue.Queue() sendDataQueues.append(self.mailbox) printLock.acquire() @@ -2236,6 +2264,55 @@ def safeConfigGetBoolean(section,field): except: return False +def signal_handler(signal, frame): + if safeConfigGetBoolean('bitmessagesettings','daemon'): + doCleanShutdown() + sys.exit(0) + else: + print 'Unfortunately you cannot use Ctrl+C when running the UI because the UI captures the signal.' + +def doCleanShutdown(): + UISignalQueue.put(('updateStatusBar','Bitmessage is stuck waiting for the knownNodesLock.')) + knownNodesLock.acquire() + UISignalQueue.put(('updateStatusBar','Saving the knownNodes list of peers to disk...')) + output = open(appdata + 'knownnodes.dat', 'wb') + print 'finished opening knownnodes.dat. Now pickle.dump' + pickle.dump(knownNodes, output) + print 'Completed pickle.dump. Closing output...' + output.close() + knownNodesLock.release() + print 'Finished closing knownnodes.dat output file.' + UISignalQueue.put(('updateStatusBar','Done saving the knownNodes list of peers to disk.')) + + broadcastToSendDataQueues((0, 'shutdown', 'all')) + + printLock.acquire() + print 'Flushing inventory in memory out to disk...' + printLock.release() + UISignalQueue.put(('updateStatusBar','Flushing inventory in memory out to disk. This should normally only take a second...')) + flushInventory() + print 'Finished flushing inventory.' + sqlSubmitQueue.put('exit') + + if safeConfigGetBoolean('bitmessagesettings','daemon'): + printLock.acquire() + print 'Done.' + printLock.release() + os._exit(0) + +def connectToStream(streamNumber): + #self.listOfOutgoingSynSenderThreads = [] #if we don't maintain this list, the threads will get garbage-collected. + connectionsCount[streamNumber] = 0 + selfInitiatedConnections[streamNumber] = {} + + for i in range(32): + a = outgoingSynSender() + #self.listOfOutgoingSynSenderThreads.append(a) + #QtCore.QObject.connect(a, QtCore.SIGNAL("passObjectThrough(PyQt_PyObject)"), self.connectObjectToSignals) + #QtCore.QObject.connect(a, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + a.setup(streamNumber) + a.start() + #Does an EC point multiplication; turns a private key into a public key. def pointMult(secret): #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. @@ -2338,9 +2415,9 @@ def assembleVersionMessage(remoteHost,remotePort,myStreamNumber): return datatosend + payload #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) +class sqlThread(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) def run(self): self.conn = sqlite3.connect(appdata + 'messages.dat' ) @@ -2474,6 +2551,8 @@ class sqlThread(QThread): item = sqlSubmitQueue.get() if item == 'commit': self.conn.commit() + elif item == 'exit': + return else: parameters = sqlSubmitQueue.get() #print 'item', item @@ -2497,16 +2576,17 @@ It resends messages when there has been no response: resends msg messages in 4 days (then 8 days, then 16 days, etc...) ''' -class singleCleaner(QThread): - def __init__(self, parent = None): - QThread.__init__(self, parent) +class singleCleaner(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) def run(self): timeWeLastClearedInventoryAndPubkeysTables = 0 while True: sqlLock.acquire() - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing housekeeping (Flushing inventory in memory to disk...)") + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing housekeeping (Flushing inventory in memory to disk...)") + UISignalQueue.put(('updateStatusBar','Doing housekeeping (Flushing inventory in memory to disk...)')) for hash, storedValue in inventory.items(): objectType, streamNumber, payload, receivedTime = storedValue if int(time.time())- 3600 > receivedTime: @@ -2516,7 +2596,8 @@ class singleCleaner(QThread): sqlReturnQueue.get() del inventory[hash] sqlSubmitQueue.put('commit') - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") + UISignalQueue.put(('updateStatusBar','')) 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: @@ -2550,7 +2631,8 @@ class singleCleaner(QThread): except: pass workerQueue.put(('sendmessage',toaddress)) - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing work necessary to again attempt to request a public key...") + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing work necessary to again attempt to request a public key...") + UISignalQueue.put(('updateStatusBar','Doing work necessary to again attempt to request a public key...')) t = (int(time.time()),pubkeyretrynumber+1,toripe) sqlSubmitQueue.put('''UPDATE sent SET lastactiontime=?, pubkeyretrynumber=? WHERE toripe=?''') sqlSubmitQueue.put(t) @@ -2563,15 +2645,17 @@ class singleCleaner(QThread): sqlSubmitQueue.put(t) sqlReturnQueue.get() workerQueue.put(('sendmessage',toaddress)) - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing work necessary to again attempt to deliver a message...") + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing work necessary to again attempt to deliver a message...") + UISignalQueue.put(('updateStatusBar','Doing work necessary to again attempt to deliver a message...')) sqlSubmitQueue.put('commit') sqlLock.release() time.sleep(300) #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) +class singleWorker(threading.Thread): + def __init__(self): + #QThread.__init__(self, parent) + threading.Thread.__init__(self) def run(self): sqlLock.acquire() @@ -2643,7 +2727,9 @@ class singleWorker(QThread): 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.') + #self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),toRipe,'Public key was requested earlier. Receiver must be offline. Will retry.') + UISignalQueue.put(('updateSentItemStatusByHash',(toRipe,'Public key was requested earlier. Receiver must be offline. Will retry.'))) + else: print 'We already have the necessary public key.' self.sendMsg(toRipe) #by calling this function, we are asserting that we already have the pubkey for toRipe @@ -2734,7 +2820,8 @@ class singleWorker(QThread): print 'broadcasting inv with hash:', inventoryHash.encode('hex') printLock.release() broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") + UISignalQueue.put(('updateStatusBar','')) config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) with open(appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) @@ -2799,7 +2886,8 @@ class singleWorker(QThread): print 'broadcasting inv with hash:', inventoryHash.encode('hex') printLock.release() broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") + UISignalQueue.put(('updateStatusBar','')) config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) with open(appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) @@ -2820,7 +2908,8 @@ class singleWorker(QThread): 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.') + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Error! Could not find sender address (your address) in the keys.dat file.') + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) continue privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') @@ -2849,7 +2938,8 @@ class singleWorker(QThread): trialValue = 99999999999999999999 target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) print '(For broadcast message) Doing proof of work...' - self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) initialHash = hashlib.sha512(payload).digest() while trialValue > target: nonce += 1 @@ -2864,7 +2954,8 @@ class singleWorker(QThread): print 'sending inv (within sendBroadcast function)' broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) - self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) #Update the status of the message in the 'sent' table to have a 'broadcastsent' status sqlLock.acquire() @@ -2880,7 +2971,8 @@ class singleWorker(QThread): 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.') + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Error! Could not find sender address (your address) in the keys.dat file.') + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) continue privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') @@ -2917,7 +3009,8 @@ class singleWorker(QThread): trialValue = 99999999999999999999 target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) print '(For broadcast message) Doing proof of work...' - self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) initialHash = hashlib.sha512(payload).digest() while trialValue > target: nonce += 1 @@ -2932,7 +3025,8 @@ class singleWorker(QThread): print 'sending inv (within sendBroadcast function)' broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) - self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) #Update the status of the message in the 'sent' table to have a 'broadcastsent' status sqlLock.acquire() @@ -2965,7 +3059,8 @@ class singleWorker(QThread): ackdataForWhichImWatching[ackdata] = 0 toStatus,toAddressVersionNumber,toStreamNumber,toHash = decodeAddress(toaddress) fromStatus,fromAddressVersionNumber,fromStreamNumber,fromHash = decodeAddress(fromaddress) - self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send the message.') + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send the message.') + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send the message.'))) printLock.acquire() print 'Found a message in our database that needs to be sent with this pubkey.' print 'First 150 characters of message:', message[:150] @@ -2982,7 +3077,8 @@ class singleWorker(QThread): 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.') + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Error! Could not find sender address (your address) in the keys.dat file.') + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) continue privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') @@ -3017,7 +3113,8 @@ class singleWorker(QThread): 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.') + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Error! Could not find sender address (your address) in the keys.dat file.') + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) continue privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') @@ -3114,7 +3211,8 @@ class singleWorker(QThread): 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 ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Message sent. Waiting on acknowledgement. Sent on ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Message sent. Waiting on acknowledgement. Sent on ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) print 'sending inv (within sendmsg function)' broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) @@ -3145,8 +3243,10 @@ class singleWorker(QThread): trialValue = 99999999999999999999 #print 'trial value', trialValue statusbar = 'Doing the computations necessary to request the recipient\'s public key.' - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) - self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Doing work necessary to request public key.') + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) + UISignalQueue.put(('updateStatusBar',statusbar)) + #self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Doing work necessary to request public key.') + UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Doing work necessary to request public key.'))) print 'Doing proof-of-work necessary to send getpubkey message.' target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) initialHash = hashlib.sha512(payload).digest() @@ -3164,8 +3264,10 @@ class singleWorker(QThread): print 'sending inv (for the getpubkey message)' broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Broacasting the public key request. This program will auto-retry if they are offline.') - self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Broacasting the public key request. This program will auto-retry if they are offline.') + UISignalQueue.put(('updateStatusBar','Broacasting the public key request. This program will auto-retry if they are offline.')) + #self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) def generateFullAckMessage(self,ackdata,toStreamNumber,embeddedTime): nonce = 0 @@ -3194,121 +3296,46 @@ class singleWorker(QThread): headerData += hashlib.sha512(payload).digest()[:4] return headerData + payload -class addressGenerator(QThread): - def __init__(self, parent = None): - QThread.__init__(self, parent) - - def setup(self,addressVersionNumber,streamNumber,label="(no label)",numberOfAddressesToMake=1,deterministicPassphrase="",eighteenByteRipe=False): - self.addressVersionNumber = addressVersionNumber - self.streamNumber = streamNumber - self.label = label - self.numberOfAddressesToMake = numberOfAddressesToMake - self.deterministicPassphrase = deterministicPassphrase - self.eighteenByteRipe = eighteenByteRipe +class addressGenerator(threading.Thread): + def __init__(self): + #QThread.__init__(self, parent) + threading.Thread.__init__(self) def run(self): - if self.addressVersionNumber == 3: - 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, - #we won't store the \x00 or \x00\x00 bytes thus making the address shorter. - startTime = time.time() - numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix = 0 - potentialPrivSigningKey = OpenSSL.rand(32) - potentialPubSigningKey = pointMult(potentialPrivSigningKey) - while True: - numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix += 1 - potentialPrivEncryptionKey = OpenSSL.rand(32) - potentialPubEncryptionKey = 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 - 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.' - address = encodeAddress(3,self.streamNumber,ripe.digest()) - #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) - #print 'privSigningKeyWIF',privSigningKeyWIF - - privEncryptionKey = '\x80'+potentialPrivEncryptionKey - checksum = hashlib.sha256(hashlib.sha256(privEncryptionKey).digest()).digest()[0:4] - privEncryptionKeyWIF = arithmetic.changebase(privEncryptionKey + checksum,256,58) - #print 'privEncryptionKeyWIF',privEncryptionKeyWIF - - config.add_section(address) - config.set(address,'label',self.label) - config.set(address,'enabled','true') - config.set(address,'decoy','false') - config.set(address,'noncetrialsperbyte',config.get('bitmessagesettings','defaultnoncetrialsperbyte')) - config.set(address,'payloadlengthextrabytes',config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) - config.set(address,'privSigningKey',privSigningKeyWIF) - config.set(address,'privEncryptionKey',privEncryptionKeyWIF) - with open(appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) - - #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) - - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address. Doing work necessary to broadcast it...') - self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.label,address,str(self.streamNumber)) - reloadMyAddressHashes() - workerQueue.put(('doPOWForMyV3Pubkey',ripe.digest())) - - else: #There is something in the deterministicPassphrase variable thus we are going to do this deterministically. - statusbar = 'Generating '+str(self.numberOfAddressesToMake) + ' new addresses.' - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) - signingKeyNonce = 0 - encryptionKeyNonce = 1 - listOfNewAddressesToSendOutThroughTheAPI = [] #We fill out this list no matter what although we only need it if we end up passing the info to the API. - - for i in range(self.numberOfAddressesToMake): + while True: + addressVersionNumber,streamNumber,label,numberOfAddressesToMake,deterministicPassphrase,eighteenByteRipe, = addressGeneratorQueue.get() + if addressVersionNumber == 3: + if deterministicPassphrase == "": + #statusbar = 'Generating one new address' + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) + UISignalQueue.put(('updateStatusBar','Generating one new address')) #This next section is a little bit strange. We're going to generate keys over and over until we - #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. + #find one 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. startTime = time.time() numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix = 0 + potentialPrivSigningKey = OpenSSL.rand(32) + potentialPubSigningKey = pointMult(potentialPrivSigningKey) while True: numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix += 1 - potentialPrivSigningKey = hashlib.sha512(self.deterministicPassphrase + encodeVarint(signingKeyNonce)).digest()[:32] - potentialPrivEncryptionKey = hashlib.sha512(self.deterministicPassphrase + encodeVarint(encryptionKeyNonce)).digest()[:32] - potentialPubSigningKey = pointMult(potentialPrivSigningKey) + potentialPrivEncryptionKey = OpenSSL.rand(32) potentialPubEncryptionKey = 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 eighteenByteRipe: if ripe.digest()[:2] == '\x00\x00': break else: if ripe.digest()[:1] == '\x00': break - - print 'ripe.digest', ripe.digest().encode('hex') - print 'Address generator calculated', numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix, 'addresses at', numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix/(time.time()-startTime),'keys per second.' - address = encodeAddress(3,self.streamNumber,ripe.digest()) + 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.' + address = encodeAddress(3,streamNumber,ripe.digest()) #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. @@ -3316,34 +3343,110 @@ class addressGenerator(QThread): privSigningKey = '\x80'+potentialPrivSigningKey checksum = hashlib.sha256(hashlib.sha256(privSigningKey).digest()).digest()[0:4] privSigningKeyWIF = arithmetic.changebase(privSigningKey + checksum,256,58) + #print 'privSigningKeyWIF',privSigningKeyWIF privEncryptionKey = '\x80'+potentialPrivEncryptionKey checksum = hashlib.sha256(hashlib.sha256(privEncryptionKey).digest()).digest()[0:4] privEncryptionKeyWIF = arithmetic.changebase(privEncryptionKey + checksum,256,58) + #print 'privEncryptionKeyWIF',privEncryptionKeyWIF - try: - config.add_section(address) - print 'label:', self.label - config.set(address,'label',self.label) - config.set(address,'enabled','true') - config.set(address,'decoy','false') - config.set(address,'noncetrialsperbyte',config.get('bitmessagesettings','defaultnoncetrialsperbyte')) - config.set(address,'payloadlengthextrabytes',config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) - config.set(address,'privSigningKey',privSigningKeyWIF) - config.set(address,'privEncryptionKey',privEncryptionKeyWIF) - with open(appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + config.add_section(address) + config.set(address,'label',label) + config.set(address,'enabled','true') + config.set(address,'decoy','false') + config.set(address,'noncetrialsperbyte',config.get('bitmessagesettings','defaultnoncetrialsperbyte')) + config.set(address,'payloadlengthextrabytes',config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) + 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)) - listOfNewAddressesToSendOutThroughTheAPI.append(address) - if self.eighteenByteRipe: - reloadMyAddressHashes()#This is necessary here (rather than just at the end) because otherwise if the human generates a large number of new addresses and uses one before they are done generating, the program will receive a getpubkey message and will ignore it. - except: - print address,'already exists. Not adding it again.' - #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) - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address') - reloadMyAddressHashes() + #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) + + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address. Doing work necessary to broadcast it...') + UISignalQueue.put(('updateStatusBar','Done generating address. Doing work necessary to broadcast it...')) + #self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.label,address,str(streamNumber)) + UISignalQueue.put(('writeNewAddressToTable',(label,address,streamNumber))) + reloadMyAddressHashes() + workerQueue.put(('doPOWForMyV3Pubkey',ripe.digest())) + + else: #There is something in the deterministicPassphrase variable thus we are going to do this deterministically. + statusbar = 'Generating '+str(numberOfAddressesToMake) + ' new addresses.' + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) + UISignalQueue.put(('updateStatusBar',statusbar)) + signingKeyNonce = 0 + encryptionKeyNonce = 1 + listOfNewAddressesToSendOutThroughTheAPI = [] #We fill out this list no matter what although we only need it if we end up passing the info to the API. + + for i in range(numberOfAddressesToMake): + #This next section is a little bit strange. We're going to generate keys over and over until we + #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. + startTime = time.time() + numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix = 0 + while True: + numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix += 1 + potentialPrivSigningKey = hashlib.sha512(deterministicPassphrase + encodeVarint(signingKeyNonce)).digest()[:32] + potentialPrivEncryptionKey = hashlib.sha512(deterministicPassphrase + encodeVarint(encryptionKeyNonce)).digest()[:32] + potentialPubSigningKey = pointMult(potentialPrivSigningKey) + potentialPubEncryptionKey = 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 eighteenByteRipe: + if ripe.digest()[:2] == '\x00\x00': + break + else: + if ripe.digest()[:1] == '\x00': + break + + print 'ripe.digest', ripe.digest().encode('hex') + print 'Address generator calculated', numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix, 'addresses at', numberOfAddressesWeHadToMakeBeforeWeFoundOneWithTheCorrectRipePrefix/(time.time()-startTime),'keys per second.' + address = encodeAddress(3,streamNumber,ripe.digest()) + #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 'label:', label + config.set(address,'label',label) + config.set(address,'enabled','true') + config.set(address,'decoy','false') + config.set(address,'noncetrialsperbyte',config.get('bitmessagesettings','defaultnoncetrialsperbyte')) + config.set(address,'payloadlengthextrabytes',config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) + 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)) + UISignalQueue.put(('writeNewAddressToTable',(label,address,str(streamNumber)))) + listOfNewAddressesToSendOutThroughTheAPI.append(address) + if eighteenByteRipe: + reloadMyAddressHashes()#This is necessary here (rather than just at the end) because otherwise if the human generates a large number of new addresses and uses one before they are done generating, the program will receive a getpubkey message and will ignore it. + except: + print address,'already exists. Not adding it again.' + #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) + #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address') + UISignalQueue.put(('updateStatusBar','Done generating address')) + reloadMyAddressHashes() #This is one of several classes that constitute the API @@ -3441,7 +3544,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return a+b elif method == 'statusBar': message, = params - apiSignalQueue.put(('updateStatusBar',message)) + #apiSignalQueue.put(('updateStatusBar',message)) + UISignalQueue.put(('updateStatusBar',message)) elif method == 'listAddresses': data = '{"addresses":[' configSections = config.sections() @@ -3464,7 +3568,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): label, eighteenByteRipe = params label = label.decode('base64') apiAddressGeneratorReturnQueue.queue.clear() - apiSignalQueue.put(('createRandomAddress',(label, eighteenByteRipe))) #params should be a twopul which equals (eighteenByteRipe, label) + streamNumberForAddress = 1 + #apiSignalQueue.put(('createRandomAddress',(label, eighteenByteRipe))) #params should be a twopul which equals (eighteenByteRipe, label) + addressGeneratorQueue.put((3,streamNumberForAddress,label,1,"",eighteenByteRipe)) return apiAddressGeneratorReturnQueue.get() elif method == 'createDeterministicAddresses': if len(params) == 0: @@ -3506,7 +3612,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): 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.' apiAddressGeneratorReturnQueue.queue.clear() print 'about to send numberOfAddresses', numberOfAddresses - apiSignalQueue.put(('createDeterministicAddresses',(passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe))) + #apiSignalQueue.put(('createDeterministicAddresses',(passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe))) + addressGeneratorQueue.put((addressVersionNumber,streamNumber,'unused API address',numberOfAddresses,passphrase,eighteenByteRipe)) data = '{"addresses":[' queueReturn = apiAddressGeneratorReturnQueue.get() for item in queueReturn: @@ -3540,7 +3647,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - apiSignalQueue.put(('updateStatusBar','Per API: Trashed message (assuming message existed). UI not updated.')) + #apiSignalQueue.put(('updateStatusBar','Per API: Trashed message (assuming message existed). UI not updated.')) + UISignalQueue.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': if len(params) == 0: @@ -3664,8 +3772,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): sqlLock.release() toLabel = '[Broadcast subscribers]' - apiSignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) - + #apiSignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) + #self.emit(SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),toAddress,toLabel,fromAddress,subject,message,ackdata) + UISignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) workerQueue.put(('sendbroadcast',(fromAddress,subject,message))) return ackdata.encode('hex') @@ -3674,202 +3783,65 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): 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) +class singleAPI(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) 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): + + +class UISignaler(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(3,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) + command, data = UISignalQueue.get() + if not safeConfigGetBoolean('bitmessagesettings','daemon'): + if command == 'writeNewAddressToTable': + label, address, streamNumber = data + self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),label,address,str(streamNumber)) + elif command == 'updateStatusBar': + self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),data) + elif command == 'updateSentItemStatusByHash': + hash, message = data + self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),hash,message) + elif command == 'updateSentItemStatusByAckdata': + ackData, message = data + self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackData,message) + elif command == 'displayNewInboxMessage': + inventoryHash,toAddress,fromAddress,subject,body = data + self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),inventoryHash,toAddress,fromAddress,subject,body) + elif command == 'displayNewSentMessage': + toAddress,fromLabel,fromAddress,subject,message,ackdata = data + self.emit(SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),toAddress,fromLabel,fromAddress,subject,message,ackdata) + elif command == 'updateNetworkStatusTab': + streamNumber,count = data + self.emit(SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"),streamNumber,count) + elif command == 'incrementNumberOfMessagesProcessed': + self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) + elif command == 'incrementNumberOfPubkeysProcessed': + self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()")) + elif command == 'incrementNumberOfBroadcastsProcessed': + self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()")) + elif command == 'setStatusIcon': + self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),data) + else: + sys.stderr.write('Command sent to UISignaler not recognized: %s\n' % command) -class iconGlossaryDialog(QtGui.QDialog): - def __init__(self,parent): - QtGui.QWidget.__init__(self, parent) - self.ui = Ui_iconGlossaryDialog() - self.ui.setupUi(self) - self.parent = parent - self.ui.labelPortNumber.setText('You are using TCP port ' + str(config.getint('bitmessagesettings', 'port')) + '. (This can be changed in the settings).') - QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) -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) - QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) +#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()) -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) +##cut from here -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)) -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')) - if appdata == '': - self.ui.checkBoxPortableMode.setChecked(True) - if 'darwin' in sys.platform: - self.ui.checkBoxStartOnLogon.setDisabled(True) - 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.') - elif 'linux' in sys.platform: - self.ui.checkBoxStartOnLogon.setDisabled(True) - self.ui.checkBoxMinimizeToTray.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.') - #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) - - 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) - - self.ui.lineEditTotalDifficulty.setText(str((float(config.getint('bitmessagesettings', 'defaultnoncetrialsperbyte'))/networkDefaultProofOfWorkNonceTrialsPerByte))) - self.ui.lineEditSmallMessageDifficulty.setText(str((float(config.getint('bitmessagesettings', 'defaultpayloadlengthextrabytes'))/networkDefaultPayloadLengthExtraBytes))) - QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) - - 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) - -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()) - if safeConfigGetBoolean(addressAtCurrentRow,'mailinglist'): - 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)) - -class NewSubscriptionDialog(QtGui.QDialog): - def __init__(self,parent): - QtGui.QWidget.__init__(self, parent) - self.ui = Ui_NewSubscriptionDialog() - self.ui.setupUi(self) - 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 == 'ripetooshort': - self.ui.labelSubscriptionAddressCheck.setText('Some data encoded in the address is too short.') - elif status == 'ripetoolong': - self.ui.labelSubscriptionAddressCheck.setText('Some data encoded in the address is too long.') - elif status == 'success': - self.ui.labelSubscriptionAddressCheck.setText('Address is valid.') - -class NewAddressDialog(QtGui.QDialog): - def __init__(self, parent): - QtGui.QWidget.__init__(self, parent) - self.ui = Ui_NewAddressDialog() - self.ui.setupUi(self) - self.parent = parent - row = 1 - #Let's fill out the 'existing address' combo box with addresses from the 'Your Identities' tab. - 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 - self.ui.groupBoxDeterministic.setHidden(True) - QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) class MyForm(QtGui.QMainWindow): @@ -4065,8 +4037,8 @@ class MyForm(QtGui.QMainWindow): if isEnabled: status,addressVersionNumber,streamNumber,hash = decodeAddress(addressInKeysFile) - self.sqlLookup = sqlThread() - self.sqlLookup.start() + #self.sqlLookup = sqlThread() + #self.sqlLookup.start() reloadMyAddressHashes() self.reloadBroadcastSendersForWhichImWatching() @@ -4275,43 +4247,38 @@ class MyForm(QtGui.QMainWindow): self.rerenderComboBoxSendFrom() - - - self.connectToStream(1) - - self.singleListenerThread = singleListener() - self.singleListenerThread.start() - QtCore.QObject.connect(self.singleListenerThread, QtCore.SIGNAL("passObjectThrough(PyQt_PyObject)"), self.connectObjectToSignals) + self.UISignalThread = UISignaler() + self.UISignalThread.start() + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.writeNewAddressToTable) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByHash) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByAckdata) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.displayNewInboxMessage) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.displayNewSentMessage) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"), self.updateNetworkStatusTab) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfMessagesProcessed()"), self.incrementNumberOfMessagesProcessed) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfPubkeysProcessed()"), self.incrementNumberOfPubkeysProcessed) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfBroadcastsProcessed()"), self.incrementNumberOfBroadcastsProcessed) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("setStatusIcon(PyQt_PyObject)"), self.setStatusIcon) - self.singleCleanerThread = singleCleaner() - self.singleCleanerThread.start() - QtCore.QObject.connect(self.singleCleanerThread, QtCore.SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByHash) - QtCore.QObject.connect(self.singleCleanerThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + #self.connectToStream(1) - 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) - QtCore.QObject.connect(self.workerThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + #self.singleListenerThread = singleListener() + #self.singleListenerThread.start() + #QtCore.QObject.connect(self.singleListenerThread, QtCore.SIGNAL("passObjectThrough(PyQt_PyObject)"), self.connectObjectToSignals) - 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"]) - self.singleAPIThread = singleAPI() - self.singleAPIThread.start() - 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) + + #self.singleCleanerThread = singleCleaner() + #self.singleCleanerThread.start() + #QtCore.QObject.connect(self.singleCleanerThread, QtCore.SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByHash) + #QtCore.QObject.connect(self.singleCleanerThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + + #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) + #QtCore.QObject.connect(self.workerThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) def tableWidgetInboxKeyPressEvent(self,event): if event.key() == QtCore.Qt.Key_Delete: @@ -4345,11 +4312,12 @@ class MyForm(QtGui.QMainWindow): 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.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() + addressGeneratorQueue.put((addressVersionNumber,streamNumberForAddress,"regenerated deterministic address",self.regenerateAddressesDialogInstance.ui.spinBoxNumberOfAddressesToMake.value(),self.regenerateAddressesDialogInstance.ui.lineEditPassphrase.text().toUtf8(),self.regenerateAddressesDialogInstance.ui.checkBoxEighteenByteRipe.isChecked())) self.ui.tabWidget.setCurrentIndex(3) def openKeysFile(self): @@ -4406,12 +4374,24 @@ class MyForm(QtGui.QMainWindow): def updateNetworkStatusTab(self,streamNumber,connectionCount): global statusIconColor #print 'updating network status tab' - 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. + totalNumberOfConnectionsFromAllStreams = 0 #One would think we could use len(sendDataQueues) for this but the number doesn't always match: just because we have a sendDataThread running doesn't mean that the connection has been fully established (with the exchange of version messages). + foundTheRowThatNeedsUpdating = False for currentRow in range(self.ui.tableWidgetConnectionCount.rowCount()): rowStreamNumber = int(self.ui.tableWidgetConnectionCount.item(currentRow,0).text()) if streamNumber == rowStreamNumber: + foundTheRowThatNeedsUpdating = True self.ui.tableWidgetConnectionCount.item(currentRow,1).setText(str(connectionCount)) totalNumberOfConnectionsFromAllStreams += connectionCount + if foundTheRowThatNeedsUpdating == False: + #Add a line to the table for this stream number and update its count with the current connection count. + 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(str(connectionCount)) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetConnectionCount.setItem(0,1,newItem) + 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') @@ -4594,7 +4574,7 @@ class MyForm(QtGui.QMainWindow): if queryreturn <> []: for row in queryreturn: toLabel, = row - + self.displayNewSentMessage(toAddress,toLabel,fromAddress, subject, message, ackdata) workerQueue.put(('sendmessage',toAddress)) @@ -4693,27 +4673,7 @@ class MyForm(QtGui.QMainWindow): else: self.ui.comboBoxSendFrom.setCurrentIndex(0) - def connectToStream(self,streamNumber): - self.listOfOutgoingSynSenderThreads = [] #if we don't maintain this list, the threads will get garbage-collected. - connectionsCount[streamNumber] = 0 - selfInitiatedConnections[streamNumber] = {} - #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) - - for i in range(32): - a = outgoingSynSender() - self.listOfOutgoingSynSenderThreads.append(a) - QtCore.QObject.connect(a, QtCore.SIGNAL("passObjectThrough(PyQt_PyObject)"), self.connectObjectToSignals) - QtCore.QObject.connect(a, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) - a.setup(streamNumber) - a.start() def connectObjectToSignals(self,object): QtCore.QObject.connect(object, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) @@ -4835,7 +4795,7 @@ class MyForm(QtGui.QMainWindow): newItem.setData(33,int(time.time())) newItem.setFont(font) self.ui.tableWidgetInbox.setItem(0,3,newItem) - + """#If we have received this message from either a broadcast address or from someone in our address book, display as HTML if decodeAddress(fromAddress)[3] in broadcastSendersForWhichImWatching or isAddressInMyAddressBook(fromAddress): self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(0,2).data(Qt.UserRole).toPyObject()) @@ -5013,7 +4973,7 @@ class MyForm(QtGui.QMainWindow): 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) + 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: @@ -5122,11 +5082,12 @@ class MyForm(QtGui.QMainWindow): #User selected 'Use the same stream as an existing address.' streamNumberForAddress = addressStream(self.dialog.ui.comboBoxExisting.currentText()) - self.addressGenerator = addressGenerator() - self.addressGenerator.setup(3,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() + #self.addressGenerator = addressGenerator() + #self.addressGenerator.setup(3,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() + addressGeneratorQueue.put((3,streamNumberForAddress,str(self.dialog.ui.newaddresslabel.text().toUtf8()),1,"",self.dialog.ui.checkBoxEighteenByteRipe.isChecked())) else: 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.") @@ -5134,11 +5095,12 @@ class MyForm(QtGui.QMainWindow): 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(3,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() + #self.addressGenerator = addressGenerator() + #self.addressGenerator.setup(3,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() + addressGeneratorQueue.put((3,streamNumberForAddress,"unused deterministic address",self.dialog.ui.spinBoxNumberOfAddressesToMake.value(),self.dialog.ui.lineEditPassphrase.text().toUtf8(),self.dialog.ui.checkBoxEighteenByteRipe.isChecked())) else: print 'new address dialog box rejected' @@ -5151,42 +5113,11 @@ class MyForm(QtGui.QMainWindow): event.accept() else: event.ignore()''' - - self.statusBar().showMessage('Bitmessage is stuck waiting for the knownNodesLock.') - knownNodesLock.acquire() - self.statusBar().showMessage('Saving the knownNodes list of peers to disk...') - output = open(appdata + 'knownnodes.dat', 'wb') - print 'finished opening knownnodes.dat. Now pickle.dump' - pickle.dump(knownNodes, output) - print 'done with pickle.dump. closing output' - output.close() - knownNodesLock.release() - print 'done closing knownnodes.dat output file.' - self.statusBar().showMessage('Done saving the knownNodes list of peers to disk.') - - broadcastToSendDataQueues((0, 'shutdown', 'all')) - - printLock.acquire() - print 'Closing. Flushing inventory in memory out to disk...' - printLock.release() - self.statusBar().showMessage('Flushing inventory in memory out to disk. This should normally only take a second...') - flushInventory() - - #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() - + doCleanShutdown() self.trayIcon.hide() - printLock.acquire() - print 'Done.' - printLock.release() self.statusBar().showMessage('All done. Closing user interface...') event.accept() - print 'done. (passed event.accept())' - #raise SystemExit + print 'Done. (passed event.accept())' os._exit(0) def on_action_InboxMessageForceHtml(self): @@ -5280,7 +5211,7 @@ class MyForm(QtGui.QMainWindow): #Send item on the Sent tab to trash def on_action_SentTrash(self): while self.ui.tableWidgetSent.selectedIndexes() != []: - currentRow = self.ui.tableWidgetSent.selectedIndexes()[0].row() + currentRow = self.ui.tableWidgetSent.selectedIndexes()[0].row() ackdataToTrash = str(self.ui.tableWidgetSent.item(currentRow,3).data(Qt.UserRole).toPyObject()) t = (ackdataToTrash,) sqlLock.acquire() @@ -5296,7 +5227,7 @@ class MyForm(QtGui.QMainWindow): self.ui.tableWidgetSent.selectRow(currentRow) else: self.ui.tableWidgetSent.selectRow(currentRow-1) - + def on_action_SentClipboard(self): currentRow = self.ui.tableWidgetSent.currentRow() addressAtCurrentRow = str(self.ui.tableWidgetSent.item(currentRow,0).data(Qt.UserRole).toPyObject()) @@ -5539,7 +5470,7 @@ class MyForm(QtGui.QMainWindow): sqlReturnQueue.get() sqlSubmitQueue.put('commit') sqlLock.release() - + def tableWidgetSentItemClicked(self): currentRow = self.ui.tableWidgetSent.currentRow() if currentRow >= 0: @@ -5623,10 +5554,190 @@ class MyForm(QtGui.QMainWindow): privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32] MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) -#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()) + + +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) + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + +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) + +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)) + +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')) + if appdata == '': + self.ui.checkBoxPortableMode.setChecked(True) + if 'darwin' in sys.platform: + self.ui.checkBoxStartOnLogon.setDisabled(True) + 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 aren\'t applicable or because they haven\'t yet been implimented for your operating system.') + elif 'linux' in sys.platform: + self.ui.checkBoxStartOnLogon.setDisabled(True) + self.ui.checkBoxMinimizeToTray.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.') + #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) + + 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) + + self.ui.lineEditTotalDifficulty.setText(str((float(config.getint('bitmessagesettings', 'defaultnoncetrialsperbyte'))/networkDefaultProofOfWorkNonceTrialsPerByte))) + self.ui.lineEditSmallMessageDifficulty.setText(str((float(config.getint('bitmessagesettings', 'defaultpayloadlengthextrabytes'))/networkDefaultPayloadLengthExtraBytes))) + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + + 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) + +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()) + if safeConfigGetBoolean(addressAtCurrentRow,'mailinglist'): + 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)) + +class NewSubscriptionDialog(QtGui.QDialog): + def __init__(self,parent): + QtGui.QWidget.__init__(self, parent) + self.ui = Ui_NewSubscriptionDialog() + self.ui.setupUi(self) + 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 == 'ripetooshort': + self.ui.labelSubscriptionAddressCheck.setText('Some data encoded in the address is too short.') + elif status == 'ripetoolong': + self.ui.labelSubscriptionAddressCheck.setText('Some data encoded in the address is too long.') + elif status == 'success': + self.ui.labelSubscriptionAddressCheck.setText('Address is valid.') + +class NewAddressDialog(QtGui.QDialog): + def __init__(self, parent): + QtGui.QWidget.__init__(self, parent) + self.ui = Ui_NewAddressDialog() + self.ui.setupUi(self) + self.parent = parent + row = 1 + #Let's fill out the 'existing address' combo box with addresses from the 'Your Identities' tab. + 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 + self.ui.groupBoxDeterministic.setHidden(True) + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + +#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(3,streamNumberForAddress,label,1,"",eighteenByteRipe) + #self.emit(SIGNAL("passAddressGeneratorObjectThrough(PyQt_PyObject)"),self.addressGenerator) + #self.addressGenerator.start() + addressGeneratorQueue.put((3,streamNumberForAddress,label,1,"",eighteenByteRipe)) + 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() + addressGeneratorQueue.put((addressVersionNumber,streamNumber,'unused API address',numberOfAddresses,passphrase,eighteenByteRipe)) + 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)""" + + selfInitiatedConnections = {} #This is a list of current connections (the thread pointers at least) @@ -5656,6 +5767,8 @@ successfullyDecryptMessageTimings = [] #A list of the amounts of time it took to 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. alreadyAttemptedConnectionsListResetTime = int(time.time()) #used to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect. +UISignalQueue = Queue.Queue() +addressGeneratorQueue = Queue.Queue() #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! networkDefaultProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. @@ -5666,6 +5779,8 @@ if useVeryEasyProofOfWorkForTesting: networkDefaultPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes / 7000 if __name__ == "__main__": + signal.signal(signal.SIGINT, signal_handler) + #signal.signal(signal.SIGINT, signal.SIG_DFL) # Check the Major version, the first element in the array if sqlite3.sqlite_version_info[0] < 3: print 'This program requires sqlite version 3 or higher because 2 and lower cannot store NULL values. I see version:', sqlite3.sqlite_version_info @@ -5777,6 +5892,16 @@ if __name__ == "__main__": print 'Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage.' raise SystemExit + if not safeConfigGetBoolean('bitmessagesettings','daemon'): + try: + #from PyQt4.QtCore import * + #from PyQt4.QtGui import * + pass + except Exception, err: + print 'PyBitmessage requires PyQt unless you want to run it as a daemon and interact with it using the API. You can download PyQt from http://www.riverbankcomputing.com/software/pyqt/download or by searching Google for \'PyQt Download\'. If you want to run in daemon mode, see https://bitmessage.org/wiki/Daemon' + print 'Error message:', err + sys.exit() + #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: @@ -5794,21 +5919,73 @@ if __name__ == "__main__": else: print 'DNS bootstrap skipped because SOCKS is used.' - app = QtGui.QApplication(sys.argv) - app.setStyleSheet("QStatusBar::item { border: 0px solid black }") - myapp = MyForm() - myapp.show() + #Start the address generation thread + addressGeneratorThread = addressGenerator() + addressGeneratorThread.daemon = True # close the main program even if there are threads left + addressGeneratorThread.start() - if config.getboolean('bitmessagesettings', 'startintray'): - myapp.hide() - myapp.trayIcon.show() - #self.hidden = True - #self.setWindowState(self.windowState() & QtCore.Qt.WindowMinimized) - #self.hide() - if 'win32' in sys.platform or 'win64' in sys.platform: - myapp.setWindowFlags(Qt.ToolTip) + #Start the thread that calculates POWs + singleWorkerThread = singleWorker() + singleWorkerThread.daemon = True # close the main program even if there are threads left + singleWorkerThread.start() + + #Start the SQL thread + sqlLookup = sqlThread() + sqlLookup.daemon = False # DON'T close the main program even if there are threads left. The closeEvent should command this thread to exit gracefully. + sqlLookup.start() + + #Start the cleanerThread + singleCleanerThread = singleCleaner() + singleCleanerThread.daemon = True # close the main program even if there are threads left + singleCleanerThread.start() + + singleListenerThread = singleListener() + singleListenerThread.daemon = True # close the main program even if there are threads left + singleListenerThread.start() + + 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"]) + singleAPIThread = singleAPI() + singleAPIThread.daemon = True #close the main program even if there are threads left + singleAPIThread.start() + #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) + + connectToStream(1) + + if not safeConfigGetBoolean('bitmessagesettings','daemon'): + 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() + if 'win32' in sys.platform or 'win64' in sys.platform: + myapp.setWindowFlags(Qt.ToolTip) + + sys.exit(app.exec_()) + else: + print 'Running as a daemon. You can use Ctrl+C to exit.' + while True: + time.sleep(2) + print 'running still..' - 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! From 0bc47120631753cd7c5a4f2df778a6f9ab0c0b10 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 2 May 2013 11:53:54 -0400 Subject: [PATCH 22/33] Continued daemon mode implementation --- src/addresses.py | 2 + src/bitmessagemain.py | 3834 ++++++++-------------------------- src/bitmessageqt/__init__.py | 1913 +++++++++++++++++ src/shared.py | 185 ++ 4 files changed, 2991 insertions(+), 2943 deletions(-) create mode 100644 src/bitmessageqt/__init__.py create mode 100644 src/shared.py diff --git a/src/addresses.py b/src/addresses.py index 6700868d..a6a571f6 100644 --- a/src/addresses.py +++ b/src/addresses.py @@ -2,6 +2,8 @@ import hashlib from struct import * from pyelliptic import arithmetic + + #There is another copy of this function in Bitmessagemain.py def convertIntToString(n): a = __builtins__.hex(n) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 71357410..0f1a93df 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -18,28 +18,12 @@ useVeryEasyProofOfWorkForTesting = False #If you set this to True while on the n encryptedBroadcastSwitchoverTime = 1369735200 import sys -import ConfigParser -try: - from PyQt4.QtCore import * - from PyQt4.QtGui import * -except Exception, err: - 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).' - print 'Error message:', err - sys.exit() - -from bitmessageui import * import ConfigParser -from newaddressdialog import * -from newsubscriptiondialog import * -from regenerateaddresses import * -from specialaddressbehavior import * -from settings import * -from about import * -from help import * -from iconglossary import * -from addresses import * import Queue +from addresses import * +#from shared import * +import shared from defaultKnownNodes import * import time import socket @@ -49,9 +33,8 @@ from struct import * import pickle import random import sqlite3 -import threading #used for the locks, not for the threads +import threading from time import strftime, localtime, gmtime -import os import shutil #used for moving the messages.dat file import string import socks @@ -65,14 +48,6 @@ from SimpleXMLRPCServer import * import json from subprocess import call #used when the API must execute an outside program -class iconGlossaryDialog(QtGui.QDialog): - def __init__(self,parent): - QtGui.QWidget.__init__(self, parent) - self.ui = Ui_iconGlossaryDialog() - self.ui.setupUi(self) - self.parent = parent - self.ui.labelPortNumber.setText('You are using TCP port ' + str(config.getint('bitmessagesettings', 'port')) + '. (This can be changed in the settings).') - QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) #For each stream to which we connect, several outgoingSynSender threads will exist and will collectively create 8 connections with peers. class outgoingSynSender(threading.Thread): @@ -86,16 +61,16 @@ class outgoingSynSender(threading.Thread): time.sleep(1) global alreadyAttemptedConnectionsListResetTime while True: - time.sleep(999999)#I sometimes use this to prevent connections for testing. + #time.sleep(999999)#I sometimes use this to prevent connections for testing. if len(selfInitiatedConnections[self.streamNumber]) < 8: #maximum number of outgoing connections = 8 random.seed() - HOST, = random.sample(knownNodes[self.streamNumber], 1) + HOST, = random.sample(shared.knownNodes[self.streamNumber], 1) alreadyAttemptedConnectionsListLock.acquire() while HOST in alreadyAttemptedConnectionsList or HOST in connectedHostsList: alreadyAttemptedConnectionsListLock.release() #print 'choosing new sample' random.seed() - HOST, = random.sample(knownNodes[self.streamNumber], 1) + HOST, = random.sample(shared.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 (time.time() - alreadyAttemptedConnectionsListResetTime) > 1800: @@ -104,43 +79,43 @@ class outgoingSynSender(threading.Thread): alreadyAttemptedConnectionsListLock.acquire() alreadyAttemptedConnectionsList[HOST] = 0 alreadyAttemptedConnectionsListLock.release() - PORT, timeNodeLastSeen = knownNodes[self.streamNumber][HOST] + PORT, timeNodeLastSeen = shared.knownNodes[self.streamNumber][HOST] sock = socks.socksocket(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.settimeout(20) - if config.get('bitmessagesettings', 'socksproxytype') == 'none' and verbose >= 2: - printLock.acquire() + if shared.config.get('bitmessagesettings', 'socksproxytype') == 'none' and verbose >= 2: + shared.printLock.acquire() print 'Trying an outgoing connection to', HOST, ':', PORT - printLock.release() + shared.printLock.release() #sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - elif config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS4a': + elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS4a': if verbose >= 2: - printLock.acquire() + shared.printLock.acquire() print '(Using SOCKS4a) Trying an outgoing connection to', HOST, ':', PORT - printLock.release() + shared.printLock.release() proxytype = socks.PROXY_TYPE_SOCKS4 - sockshostname = config.get('bitmessagesettings', 'sockshostname') - socksport = config.getint('bitmessagesettings', 'socksport') + sockshostname = shared.config.get('bitmessagesettings', 'sockshostname') + socksport = shared.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') + if shared.config.getboolean('bitmessagesettings', 'socksauthentication'): + socksusername = shared.config.get('bitmessagesettings', 'socksusername') + sockspassword = shared.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': + elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS5': if verbose >= 2: - printLock.acquire() + shared.printLock.acquire() print '(Using SOCKS5) Trying an outgoing connection to', HOST, ':', PORT - printLock.release() + shared.printLock.release() proxytype = socks.PROXY_TYPE_SOCKS5 - sockshostname = config.get('bitmessagesettings', 'sockshostname') - socksport = config.getint('bitmessagesettings', 'socksport') + sockshostname = shared.config.get('bitmessagesettings', 'sockshostname') + socksport = shared.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') + if shared.config.getboolean('bitmessagesettings', 'socksauthentication'): + socksusername = shared.config.get('bitmessagesettings', 'socksusername') + sockspassword = shared.config.get('bitmessagesettings', 'sockspassword') sock.setproxy(proxytype, sockshostname, socksport, rdns, socksusername, sockspassword) else: sock.setproxy(proxytype, sockshostname, socksport, rdns) @@ -153,9 +128,9 @@ class outgoingSynSender(threading.Thread): objectsOfWhichThisRemoteNodeIsAlreadyAware = {} rd.setup(sock,HOST,PORT,self.streamNumber,objectsOfWhichThisRemoteNodeIsAlreadyAware) rd.start() - printLock.acquire() + shared.printLock.acquire() print self, 'connected to', HOST, 'during an outgoing attempt.' - printLock.release() + shared.printLock.release() sd = sendDataThread() sd.setup(sock,HOST,PORT,self.streamNumber,objectsOfWhichThisRemoteNodeIsAlreadyAware) @@ -164,18 +139,18 @@ class outgoingSynSender(threading.Thread): except socks.GeneralProxyError, err: if verbose >= 2: - printLock.acquire() + shared.printLock.acquire() print 'Could NOT connect to', HOST, 'during outgoing attempt.', err - printLock.release() - 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. - knownNodesLock.acquire() - del knownNodes[self.streamNumber][HOST] - knownNodesLock.release() - print 'deleting ', HOST, 'from knownNodes because it is more than 48 hours old and we could not connect to it.' + shared.printLock.release() + PORT, timeLastSeen = shared.knownNodes[self.streamNumber][HOST] + if (int(time.time())-timeLastSeen) > 172800 and len(shared.knownNodes[self.streamNumber]) > 1000: # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the shared.knownNodes data-structure. + shared.knownNodesLock.acquire() + del shared.knownNodes[self.streamNumber][HOST] + shared.knownNodesLock.release() + print 'deleting ', HOST, 'from shared.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)) - UISignalQueue.put(('updateStatusBar',"SOCKS5 Authentication problem: "+str(err))) + shared.UISignalQueue.put(('updateStatusBar',"SOCKS5 Authentication problem: "+str(err))) except socks.Socks5Error, err: pass print 'SOCKS5 error. (It is possible that the server wants authentication).)' ,str(err) @@ -184,19 +159,19 @@ class outgoingSynSender(threading.Thread): 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': + if shared.config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS': 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)) else: if verbose >= 1: - printLock.acquire() + shared.printLock.acquire() print 'Could NOT connect to', HOST, 'during outgoing attempt.', err - printLock.release() - 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. - knownNodesLock.acquire() - del knownNodes[self.streamNumber][HOST] - knownNodesLock.release() + shared.printLock.release() + PORT, timeLastSeen = shared.knownNodes[self.streamNumber][HOST] + if (int(time.time())-timeLastSeen) > 172800 and len(shared.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. + shared.knownNodesLock.acquire() + del shared.knownNodes[self.streamNumber][HOST] + shared.knownNodesLock.release() print 'deleting ', HOST, 'from knownNodes because it is more than 48 hours old and we could not connect to it.' except Exception, err: sys.stderr.write('An exception has occurred in the outgoingSynSender thread that was not caught by other exception types: %s\n' % err) @@ -210,12 +185,14 @@ class singleListener(threading.Thread): def run(self): #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': + while shared.config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS': time.sleep(300) + shared.printLock.acquire() print 'Listening for incoming connections.' + shared.printLock.release() HOST = '' # Symbolic name meaning all available interfaces - PORT = config.getint('bitmessagesettings', 'port') + PORT = shared.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) @@ -225,7 +202,7 @@ class singleListener(threading.Thread): while True: #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. - while config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS': + while shared.config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS': time.sleep(10) a,(HOST,PORT) = sock.accept() #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. @@ -238,9 +215,9 @@ class singleListener(threading.Thread): #self.emit(SIGNAL("passObjectThrough(PyQt_PyObject)"),rd) objectsOfWhichThisRemoteNodeIsAlreadyAware = {} rd.setup(a,HOST,PORT,-1,objectsOfWhichThisRemoteNodeIsAlreadyAware) - printLock.acquire() + shared.printLock.acquire() print self, 'connected to', HOST,'during INCOMING request.' - printLock.release() + shared.printLock.release() rd.start() sd = sendDataThread() @@ -276,27 +253,27 @@ class receiveDataThread(threading.Thread): self.objectsOfWhichThisRemoteNodeIsAlreadyAware = objectsOfWhichThisRemoteNodeIsAlreadyAware def run(self): - printLock.acquire() + shared.printLock.acquire() print 'ID of the receiveDataThread is', str(id(self))+'. The size of the connectedHostsList is now', len(connectedHostsList) - printLock.release() + shared.printLock.release() while True: try: self.data += self.sock.recv(4096) except socket.timeout: - printLock.acquire() + shared.printLock.acquire() print 'Timeout occurred waiting for data from', self.HOST + '. Closing receiveData thread. (ID:',str(id(self))+ ')' - printLock.release() + shared.printLock.release() break except Exception, err: - printLock.acquire() + shared.printLock.acquire() print 'sock.recv error. Closing receiveData thread (HOST:', self.HOST, 'ID:',str(id(self))+ ').', err - printLock.release() + shared.printLock.release() break #print 'Received', repr(self.data) if self.data == "": - printLock.acquire() + shared.printLock.acquire() print 'Connection to', self.HOST, 'closed. Closing receiveData thread. (ID:',str(id(self))+ ')' - printLock.release() + shared.printLock.release() break else: self.processData() @@ -311,43 +288,43 @@ class receiveDataThread(threading.Thread): try: del selfInitiatedConnections[self.streamNumber][self] - printLock.acquire() + shared.printLock.acquire() print 'removed self (a receiveDataThread) from selfInitiatedConnections' - printLock.release() + shared.printLock.release() except: pass - broadcastToSendDataQueues((0, 'shutdown', self.HOST)) + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) 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]) - UISignalQueue.put(('updateNetworkStatusTab',(self.streamNumber,connectionsCount[self.streamNumber]))) - printLock.acquire() + shared.UISignalQueue.put(('updateNetworkStatusTab',(self.streamNumber,connectionsCount[self.streamNumber]))) + shared.printLock.acquire() print 'Updating network status tab with current connections count:', connectionsCount[self.streamNumber] - printLock.release() + shared.printLock.release() connectionsCountLock.release() try: del connectedHostsList[self.HOST] except Exception, err: print 'Could not delete', self.HOST, 'from connectedHostsList.', err - printLock.acquire() + shared.printLock.acquire() print 'The size of the connectedHostsList is now:', len(connectedHostsList) - printLock.release() + shared.printLock.release() def processData(self): global verbose #if verbose >= 3: - #printLock.acquire() + #shared.printLock.acquire() #print 'self.data is currently ', repr(self.data) - #printLock.release() + #shared.printLock.release() if len(self.data) < 20: #if so little of the data has arrived that we can't even unpack the payload length pass elif self.data[0:4] != '\xe9\xbe\xb4\xd9': if verbose >= 1: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('The magic bytes were not correct. First 40 bytes of data: %s\n' % repr(self.data[0:40])) print 'self.data:', self.data.encode('hex') - printLock.release() + shared.printLock.release() self.data = "" else: self.payloadLength, = unpack('>L',self.data[16:20]) @@ -356,14 +333,14 @@ class receiveDataThread(threading.Thread): #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). - knownNodesLock.acquire() - knownNodes[self.streamNumber][self.HOST] = (self.PORT,int(time.time())) - knownNodesLock.release() + shared.knownNodesLock.acquire() + shared.knownNodes[self.streamNumber][self.HOST] = (self.PORT,int(time.time())) + shared.knownNodesLock.release() 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] - printLock.acquire() + shared.printLock.acquire() print 'remoteCommand', repr(remoteCommand.replace('\x00','')), ' from', self.HOST - printLock.release() + shared.printLock.release() if remoteCommand == 'version\x00\x00\x00\x00\x00': self.recversion(self.data[24:self.payloadLength+24]) elif remoteCommand == 'verack\x00\x00\x00\x00\x00\x00': @@ -398,33 +375,33 @@ class receiveDataThread(threading.Thread): while len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) > 0: random.seed() objectHash, = random.sample(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave, 1) - if objectHash in inventory: - printLock.acquire() + if objectHash in shared.inventory: + shared.printLock.acquire() print 'Inventory (in memory) already has object listed in inv message.' - printLock.release() + shared.printLock.release() del self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave[objectHash] elif isInSqlInventory(objectHash): if verbose >= 3: - printLock.acquire() + shared.printLock.acquire() print 'Inventory (SQL on disk) already has object listed in inv message.' - printLock.release() + shared.printLock.release() del self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave[objectHash] else: self.sendgetdata(objectHash) 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. if len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) == 0: - printLock.acquire() + shared.printLock.acquire() print '(concerning', self.HOST + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) - printLock.release() + shared.printLock.release() break if len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) == 0: - printLock.acquire() + shared.printLock.acquire() print '(concerning', self.HOST + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) - printLock.release() + shared.printLock.release() if len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) > 0: - printLock.acquire() + shared.printLock.acquire() print '(concerning', self.HOST + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) - printLock.release() + shared.printLock.release() if len(self.ackDataThatWeHaveYetToSend) > 0: self.data = self.ackDataThatWeHaveYetToSend.pop() self.processData() @@ -456,30 +433,30 @@ class receiveDataThread(threading.Thread): self.connectionIsOrWasFullyEstablished = True if not self.initiatedConnection: #self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),'green') - UISignalQueue.put(('setStatusIcon','green')) + shared.UISignalQueue.put(('setStatusIcon','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]) - UISignalQueue.put(('updateNetworkStatusTab',(self.streamNumber,connectionsCount[self.streamNumber]))) + shared.UISignalQueue.put(('updateNetworkStatusTab',(self.streamNumber,connectionsCount[self.streamNumber]))) connectionsCountLock.release() - remoteNodeIncomingPort, remoteNodeSeenTime = knownNodes[self.streamNumber][self.HOST] - printLock.acquire() + remoteNodeIncomingPort, remoteNodeSeenTime = shared.knownNodes[self.streamNumber][self.HOST] + shared.printLock.acquire() print 'Connection fully established with', self.HOST, remoteNodeIncomingPort print 'ConnectionsCount now:', connectionsCount[self.streamNumber] print 'The size of the connectedHostsList is now', len(connectedHostsList) - print 'The length of sendDataQueues is now:', len(sendDataQueues) + print 'The length of sendDataQueues is now:', len(shared.sendDataQueues) print 'broadcasting addr from within connectionFullyEstablished function.' - printLock.release() + shared.printLock.release() self.broadcastaddr([(int(time.time()), self.streamNumber, 1, self.HOST, remoteNodeIncomingPort)]) #This lets all of our peers know about this new node. self.sendaddr() #This is one large addr message to this one peer. if not self.initiatedConnection and connectionsCount[self.streamNumber] > 150: - printLock.acquire() + shared.printLock.acquire() print 'We are connected to too many people. Closing connection.' - printLock.release() + shared.printLock.release() #self.sock.shutdown(socket.SHUT_RDWR) #self.sock.close() - broadcastToSendDataQueues((0, 'shutdown', self.HOST)) + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) return self.sendBigInv() @@ -489,32 +466,32 @@ class receiveDataThread(threading.Thread): return else: self.receivedgetbiginv = True - sqlLock.acquire() + shared.sqlLock.acquire() #Select all hashes which are younger than two days old and in this stream. t = (int(time.time())-maximumAgeOfObjectsThatIAdvertiseToOthers,int(time.time())-lengthOfTimeToHoldOnToAllPubkeys,self.streamNumber) - sqlSubmitQueue.put('''SELECT hash FROM inventory WHERE ((receivedtime>? and objecttype<>'pubkey') or (receivedtime>? and objecttype='pubkey')) and streamnumber=?''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlSubmitQueue.put('''SELECT hash FROM inventory WHERE ((receivedtime>? and objecttype<>'pubkey') or (receivedtime>? and objecttype='pubkey')) and streamnumber=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() bigInvList = {} for row in queryreturn: hash, = row if hash not in self.objectsOfWhichThisRemoteNodeIsAlreadyAware: bigInvList[hash] = 0 else: - printLock.acquire() + shared.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() + shared.printLock.release() #We also have messages in our inventory in memory (which is a python dictionary). Let's fetch those too. - for hash, storedValue in inventory.items(): + for hash, storedValue in shared.inventory.items(): 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() + shared.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() + shared.printLock.release() numberOfObjectsInInvMessage = 0 payload = '' #Now let us start appending all of these hashes together. They will be sent out in a big inv message to our new peer. @@ -535,9 +512,9 @@ class receiveDataThread(threading.Thread): headerData += 'inv\x00\x00\x00\x00\x00\x00\x00\x00\x00' headerData += pack('>L',len(payload)) headerData += hashlib.sha512(payload).digest()[:4] - printLock.acquire() + shared.printLock.acquire() print 'Sending huge inv message with', numberOfObjects, 'objects to just this one peer' - printLock.release() + shared.printLock.release() self.sock.sendall(headerData + payload) #We have received a broadcast message @@ -574,23 +551,23 @@ class receiveDataThread(threading.Thread): print 'The stream number encoded in this broadcast message (' + str(streamNumber) + ') does not match the stream number on which it was received. Ignoring it.' return - inventoryLock.acquire() + shared.inventoryLock.acquire() self.inventoryHash = calculateInventoryHash(data) - if self.inventoryHash in inventory: + if self.inventoryHash in shared.inventory: print 'We have already received this broadcast object. Ignoring.' - inventoryLock.release() + shared.inventoryLock.release() return elif isInSqlInventory(self.inventoryHash): print 'We have already received this broadcast object (it is stored on disk in the SQL inventory). Ignoring it.' - inventoryLock.release() + shared.inventoryLock.release() return #It is valid so far. Let's let our peers know about it. objectType = 'broadcast' - inventory[self.inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) - inventoryLock.release() + shared.inventory[self.inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) + shared.inventoryLock.release() self.broadcastinv(self.inventoryHash) #self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()")) - UISignalQueue.put(('incrementNumberOfBroadcastsProcessed','no data')) + shared.UISignalQueue.put(('incrementNumberOfBroadcastsProcessed','no data')) self.processbroadcast(readPosition,data)#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. @@ -608,13 +585,13 @@ class receiveDataThread(threading.Thread): sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - (time.time()- self.messageProcessingStartTime) if sleepTime > 0: - printLock.acquire() + shared.printLock.acquire() print 'Timing attack mitigation: Sleeping for', sleepTime ,'seconds.' - printLock.release() + shared.printLock.release() time.sleep(sleepTime) - printLock.acquire() + shared.printLock.acquire() print 'Total message processing time:', time.time()- self.messageProcessingStartTime, 'seconds.' - printLock.release() + shared.printLock.release() #A broadcast message has a valid time and POW and requires processing. The recbroadcast function calls this one. def processbroadcast(self,readPosition,data): @@ -641,11 +618,11 @@ class receiveDataThread(threading.Thread): readPosition += 64 endOfPubkeyPosition = readPosition sendersHash = data[readPosition:readPosition+20] - if sendersHash not in broadcastSendersForWhichImWatching: + if sendersHash not in shared.broadcastSendersForWhichImWatching: #Display timing data - printLock.acquire() + shared.printLock.acquire() print 'Time spent deciding that we are not interested in this v1 broadcast:', time.time()- self.messageProcessingStartTime - printLock.release() + shared.printLock.release() 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 @@ -680,18 +657,18 @@ class receiveDataThread(threading.Thread): #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(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+data[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - 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. + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + shared.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. fromAddress = encodeAddress(sendersAddressVersion,sendersStream,ripe.digest()) - printLock.acquire() + shared.printLock.acquire() print 'fromAddress:', fromAddress - printLock.release() + shared.printLock.release() if messageEncodingType == 2: bodyPositionIndex = string.find(message,'\nBody:') if bodyPositionIndex > 1: @@ -711,34 +688,34 @@ class receiveDataThread(threading.Thread): toAddress = '[Broadcast subscribers]' if messageEncodingType <> 0: - sqlLock.acquire() + shared.sqlLock.acquire() t = (self.inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox',messageEncodingType,0) - sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() #self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) - UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) + shared.UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) #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'): + if shared.safeConfigGetBoolean('bitmessagesettings','apienabled'): try: - apiNotifyPath = config.get('bitmessagesettings','apinotifypath') + apiNotifyPath = shared.config.get('bitmessagesettings','apinotifypath') except: apiNotifyPath = '' if apiNotifyPath != '': call([apiNotifyPath, "newBroadcast"]) #Display timing data - printLock.acquire() + shared.printLock.acquire() print 'Time spent processing this interesting broadcast:', time.time()- self.messageProcessingStartTime - printLock.release() + shared.printLock.release() if broadcastVersion == 2: cleartextStreamNumber, cleartextStreamNumberLength = decodeVarint(data[readPosition:readPosition+10]) readPosition += cleartextStreamNumberLength initialDecryptionSuccessful = False - for key, cryptorObject in MyECSubscriptionCryptorObjects.items(): + for key, cryptorObject in shared.MyECSubscriptionCryptorObjects.items(): try: decryptedData = cryptorObject.decrypt(data[readPosition:]) toRipe = key #This is the RIPE hash of the sender's pubkey. We need this below to compare to the RIPE hash of the sender's address to verify that it was encrypted by with their key rather than some other key. @@ -750,9 +727,9 @@ class receiveDataThread(threading.Thread): #print 'cryptorObject.decrypt Exception:', err if not initialDecryptionSuccessful: #This is not a broadcast I am interested in. - printLock.acquire() + shared.printLock.acquire() print 'Length of time program spent failing to decrypt this v2 broadcast:', time.time()- self.messageProcessingStartTime, 'seconds.' - printLock.release() + shared.printLock.release() return #At this point this is a broadcast I have decrypted and thus am interested in. signedBroadcastVersion, readPosition = decodeVarint(decryptedData[:10]) @@ -812,18 +789,18 @@ class receiveDataThread(threading.Thread): #Let's store the public key in case we want to reply to this person. t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+decryptedData[beginningOfPubkeyPosition:endOfPubkeyPosition],int(time.time()),'yes') - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - 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. + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + shared.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. fromAddress = encodeAddress(sendersAddressVersion,sendersStream,ripe.digest()) - printLock.acquire() + shared.printLock.acquire() print 'fromAddress:', fromAddress - printLock.release() + shared.printLock.release() if messageEncodingType == 2: bodyPositionIndex = string.find(message,'\nBody:') if bodyPositionIndex > 1: @@ -843,29 +820,29 @@ class receiveDataThread(threading.Thread): toAddress = '[Broadcast subscribers]' if messageEncodingType <> 0: - sqlLock.acquire() + shared.sqlLock.acquire() t = (self.inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox',messageEncodingType,0) - sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() #self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) - UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) + shared.UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) #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'): + if shared.safeConfigGetBoolean('bitmessagesettings','apienabled'): try: - apiNotifyPath = config.get('bitmessagesettings','apinotifypath') + apiNotifyPath = shared.config.get('bitmessagesettings','apinotifypath') except: apiNotifyPath = '' if apiNotifyPath != '': call([apiNotifyPath, "newBroadcast"]) #Display timing data - printLock.acquire() + shared.printLock.acquire() print 'Time spent processing this interesting broadcast:', time.time()- self.messageProcessingStartTime - printLock.release() + shared.printLock.release() #We have received a msg message. @@ -898,22 +875,22 @@ class receiveDataThread(threading.Thread): return readPosition += streamNumberAsClaimedByMsgLength self.inventoryHash = calculateInventoryHash(data) - inventoryLock.acquire() - if self.inventoryHash in inventory: + shared.inventoryLock.acquire() + if self.inventoryHash in shared.inventory: print 'We have already received this msg message. Ignoring.' - inventoryLock.release() + shared.inventoryLock.release() return elif isInSqlInventory(self.inventoryHash): print 'We have already received this msg message (it is stored on disk in the SQL inventory). Ignoring it.' - inventoryLock.release() + shared.inventoryLock.release() return #This msg message is valid. Let's let our peers know about it. objectType = 'msg' - inventory[self.inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) - inventoryLock.release() + shared.inventory[self.inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) + shared.inventoryLock.release() self.broadcastinv(self.inventoryHash) #self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) - UISignalQueue.put(('incrementNumberOfMessagesProcessed','no data')) + shared.UISignalQueue.put(('incrementNumberOfMessagesProcessed','no data')) self.processmsg(readPosition,data) #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. @@ -931,13 +908,13 @@ class receiveDataThread(threading.Thread): sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - (time.time()- self.messageProcessingStartTime) if sleepTime > 0: - printLock.acquire() + shared.printLock.acquire() print 'Timing attack mitigation: Sleeping for', sleepTime ,'seconds.' - printLock.release() + shared.printLock.release() time.sleep(sleepTime) - printLock.acquire() + shared.printLock.acquire() print 'Total message processing time:', time.time()- self.messageProcessingStartTime, 'seconds.' - printLock.release() + shared.printLock.release() #A msg message has a valid time and POW and requires processing. The recmsg function calls this one. @@ -945,28 +922,28 @@ class receiveDataThread(threading.Thread): initialDecryptionSuccessful = False #Let's check whether this is a message acknowledgement bound for us. if encryptedData[readPosition:] in ackdataForWhichImWatching: - printLock.acquire() + shared.printLock.acquire() print 'This msg IS an acknowledgement bound for me.' - printLock.release() + shared.printLock.release() del ackdataForWhichImWatching[encryptedData[readPosition:]] t = ('ackreceived',encryptedData[readPosition:]) - sqlLock.acquire() - sqlSubmitQueue.put('UPDATE sent SET status=? WHERE ackdata=?') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('UPDATE sent SET status=? WHERE ackdata=?') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),encryptedData[readPosition:],'Acknowledgement of the message received just now.') - UISignalQueue.put(('updateSentItemStatusByAckdata',(encryptedData[readPosition:],'Acknowledgement of the message received just now.'))) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(encryptedData[readPosition:],'Acknowledgement of the message received just now.'))) return else: - printLock.acquire() + shared.printLock.acquire() print 'This was NOT an acknowledgement bound for me.' #print 'ackdataForWhichImWatching', ackdataForWhichImWatching - printLock.release() + shared.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. - for key, cryptorObject in myECCryptorObjects.items(): + for key, cryptorObject in shared.myECCryptorObjects.items(): try: decryptedData = cryptorObject.decrypt(encryptedData[readPosition:]) 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. @@ -978,12 +955,12 @@ class receiveDataThread(threading.Thread): #print 'cryptorObject.decrypt Exception:', err if not initialDecryptionSuccessful: #This is not a message bound for me. - printLock.acquire() + shared.printLock.acquire() print 'Length of time program spent failing to decrypt this message:', time.time()- self.messageProcessingStartTime, 'seconds.' - printLock.release() + shared.printLock.release() else: #This is a message bound for me. - toAddress = myAddressesByHash[toRipe] #Look up my address based on the RIPE hash. + toAddress = shared.myAddressesByHash[toRipe] #Look up my address based on the RIPE hash. readPosition = 0 messageVersion, messageVersionLength = decodeVarint(decryptedData[readPosition:readPosition+10]) readPosition += messageVersionLength @@ -1021,12 +998,12 @@ class receiveDataThread(threading.Thread): print 'sender\'s requiredPayloadLengthExtraBytes is', requiredPayloadLengthExtraBytes endOfThePublicKeyPosition = readPosition #needed for when we store the pubkey in our database of pubkeys for later use. if toRipe != decryptedData[readPosition:readPosition+20]: - printLock.acquire() + shared.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://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html' print 'your toRipe:', toRipe.encode('hex') print 'embedded destination toRipe:', decryptedData[readPosition:readPosition+20].encode('hex') - printLock.release() + shared.printLock.release() return readPosition += 20 messageEncodingType, messageEncodingTypeLength = decodeVarint(decryptedData[readPosition:readPosition+10]) @@ -1050,9 +1027,9 @@ class receiveDataThread(threading.Thread): except Exception, err: print 'ECDSA verify failed', err return - printLock.acquire() + shared.printLock.acquire() 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.' - printLock.release() + shared.printLock.release() #calculate the fromRipe. sha = hashlib.new('sha512') sha.update(pubSigningKey+pubEncryptionKey) @@ -1060,42 +1037,42 @@ class receiveDataThread(threading.Thread): ripe.update(sha.digest()) #Let's store the public key in case we want to reply to this person. t = (ripe.digest(),'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'+'\xFF\xFF\xFF\xFF'+decryptedData[messageVersionLength:endOfThePublicKeyPosition],int(time.time()),'yes') - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - 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. + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + shared.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. fromAddress = encodeAddress(sendersAddressVersionNumber,sendersStreamNumber,ripe.digest()) #If this message is bound for one of my version 3 addresses (or higher), then we must check to make sure it meets our demanded proof of work requirement. if decodeAddress(toAddress)[1] >= 3:#If the toAddress version number is 3 or higher: - if not isAddressInMyAddressBookSubscriptionsListOrWhitelist(fromAddress): #If I'm not friendly with this person: - requiredNonceTrialsPerByte = config.getint(toAddress,'noncetrialsperbyte') - requiredPayloadLengthExtraBytes = config.getint(toAddress,'payloadlengthextrabytes') + if not shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(fromAddress): #If I'm not friendly with this person: + requiredNonceTrialsPerByte = shared.config.getint(toAddress,'noncetrialsperbyte') + requiredPayloadLengthExtraBytes = shared.config.getint(toAddress,'payloadlengthextrabytes') if not self.isProofOfWorkSufficient(encryptedData,requiredNonceTrialsPerByte,requiredPayloadLengthExtraBytes): print 'Proof of work in msg message insufficient only because it does not meet our higher requirement.' return blockMessage = False #Gets set to True if the user shouldn't see the message according to black or white lists. - if config.get('bitmessagesettings', 'blackwhitelist') == 'black': #If we are using a blacklist + if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': #If we are using a blacklist t = (fromAddress,) - sqlLock.acquire() - sqlSubmitQueue.put('''SELECT label FROM blacklist where address=? and enabled='1' ''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT label FROM blacklist where address=? and enabled='1' ''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn != []: - printLock.acquire() + shared.printLock.acquire() print 'Message ignored because address is in blacklist.' - printLock.release() + shared.printLock.release() blockMessage = True else: #We're using a whitelist t = (fromAddress,) - sqlLock.acquire() - sqlSubmitQueue.put('''SELECT label FROM whitelist where address=? and enabled='1' ''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT label FROM whitelist where address=? and enabled='1' ''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn == []: print 'Message ignored because address not in whitelist.' blockMessage = True @@ -1103,7 +1080,7 @@ class receiveDataThread(threading.Thread): print 'fromAddress:', fromAddress print 'First 150 characters of message:', repr(message[:150]) - toLabel = config.get(toAddress, 'label') + toLabel = shared.config.get(toAddress, 'label') if toLabel == '': toLabel = addressInKeysFile @@ -1124,29 +1101,29 @@ class receiveDataThread(threading.Thread): body = 'Unknown encoding type.\n\n' + repr(message) subject = '' if messageEncodingType <> 0: - sqlLock.acquire() + shared.sqlLock.acquire() t = (self.inventoryHash,toAddress,fromAddress,subject,int(time.time()),body,'inbox',messageEncodingType,0) - sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() #self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.inventoryHash,toAddress,fromAddress,subject,body) - UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) + shared.UISignalQueue.put(('displayNewInboxMessage',(self.inventoryHash,toAddress,fromAddress,subject,body))) #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'): + if shared.safeConfigGetBoolean('bitmessagesettings','apienabled'): try: - apiNotifyPath = config.get('bitmessagesettings','apinotifypath') + apiNotifyPath = shared.config.get('bitmessagesettings','apinotifypath') except: apiNotifyPath = '' if apiNotifyPath != '': call([apiNotifyPath, "newMessage"]) #Let us now check and see whether our receiving address is behaving as a mailing list - if safeConfigGetBoolean(toAddress,'mailinglist'): + if shared.safeConfigGetBoolean(toAddress,'mailinglist'): try: - mailingListName = config.get(toAddress, 'mailinglistname') + mailingListName = shared.config.get(toAddress, 'mailinglistname') except: mailingListName = '' #Let us send out this message as a broadcast @@ -1157,17 +1134,17 @@ class receiveDataThread(threading.Thread): 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() + shared.sqlLock.acquire() t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'broadcastpending',1,1,'sent',2) - sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.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) - UISignalQueue.put(('displayNewSentMessage',(toAddress,'[Broadcast subscribers]',fromAddress,subject,message,ackdata))) - workerQueue.put(('sendbroadcast',(fromAddress,subject,message))) + shared.UISignalQueue.put(('displayNewSentMessage',(toAddress,'[Broadcast subscribers]',fromAddress,subject,message,ackdata))) + shared.workerQueue.put(('sendbroadcast',(fromAddress,subject,message))) if self.isAckDataValid(ackData): print 'ackData is valid. Will process it.' @@ -1178,10 +1155,10 @@ class receiveDataThread(threading.Thread): sum = 0 for item in successfullyDecryptMessageTimings: sum += item - printLock.acquire() + shared.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() + shared.printLock.release() def isAckDataValid(self,ackData): if len(ackData) < 24: @@ -1228,14 +1205,14 @@ class receiveDataThread(threading.Thread): readPosition += 4 if embeddedTime < int(time.time())-lengthOfTimeToHoldOnToAllPubkeys: - printLock.acquire() + shared.printLock.acquire() print 'The embedded time in this pubkey message is too old. Ignoring. Embedded time is:', embeddedTime - printLock.release() + shared.printLock.release() return if embeddedTime > int(time.time()) + 10800: - printLock.acquire() + shared.printLock.acquire() print 'The embedded time in this pubkey message more than several hours in the future. This is irrational. Ignoring message.' - printLock.release() + shared.printLock.release() return addressVersion, varintLength = decodeVarint(data[readPosition:readPosition+10]) readPosition += varintLength @@ -1246,34 +1223,34 @@ class receiveDataThread(threading.Thread): return inventoryHash = calculateInventoryHash(data) - inventoryLock.acquire() - if inventoryHash in inventory: + shared.inventoryLock.acquire() + if inventoryHash in shared.inventory: print 'We have already received this pubkey. Ignoring it.' - inventoryLock.release() + shared.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() + shared.inventoryLock.release() return objectType = 'pubkey' - inventory[inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) - inventoryLock.release() + shared.inventory[inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) + shared.inventoryLock.release() self.broadcastinv(inventoryHash) #self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()")) - UISignalQueue.put(('incrementNumberOfPubkeysProcessed','no data')) + shared.UISignalQueue.put(('incrementNumberOfPubkeysProcessed','no data')) self.processpubkey(data) lengthOfTimeWeShouldUseToProcessThisMessage = .2 sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - (time.time()- self.pubkeyProcessingStartTime) if sleepTime > 0: - printLock.acquire() + shared.printLock.acquire() print 'Timing attack mitigation: Sleeping for', sleepTime ,'seconds.' - printLock.release() + shared.printLock.release() time.sleep(sleepTime) - printLock.acquire() + shared.printLock.acquire() print 'Total pubkey processing time:', time.time()- self.pubkeyProcessingStartTime, 'seconds.' - printLock.release() + shared.printLock.release() def processpubkey(self,data): readPosition = 8 #for the nonce @@ -1287,9 +1264,9 @@ class receiveDataThread(threading.Thread): print '(Within processpubkey) addressVersion of 0 doesn\'t make sense.' return if addressVersion >= 4 or addressVersion == 1: - printLock.acquire() + shared.printLock.acquire() print 'This version of Bitmessage cannot handle version', addressVersion,'addresses.' - printLock.release() + shared.printLock.release() return if addressVersion == 2: if len(data) < 146: #sanity check. This is the minimum possible length. @@ -1310,39 +1287,39 @@ class receiveDataThread(threading.Thread): ripeHasher.update(sha.digest()) ripe = ripeHasher.digest() - printLock.acquire() + shared.printLock.acquire() print 'within recpubkey, addressVersion:', addressVersion, ', streamNumber:', streamNumber print 'ripe', ripe.encode('hex') print 'publicSigningKey in hex:', publicSigningKey.encode('hex') print 'publicEncryptionKey in hex:', publicEncryptionKey.encode('hex') - printLock.release() + shared.printLock.release() t = (ripe,) - sqlLock.acquire() - sqlSubmitQueue.put('''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() 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,data,embeddedTime,'yes') - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + shared.workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) else: print 'We have NOT used this pubkey personally. Inserting in database.' t = (ripe,data,embeddedTime,'no') #This will also update the embeddedTime. - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + shared.workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) if addressVersion == 3: if len(data) < 170: #sanity check. print '(within processpubkey) payloadLength less than 170. Sanity check failed.' @@ -1373,38 +1350,38 @@ class receiveDataThread(threading.Thread): ripeHasher.update(sha.digest()) ripe = ripeHasher.digest() - printLock.acquire() + shared.printLock.acquire() print 'within recpubkey, addressVersion:', addressVersion, ', streamNumber:', streamNumber print 'ripe', ripe.encode('hex') print 'publicSigningKey in hex:', publicSigningKey.encode('hex') print 'publicEncryptionKey in hex:', publicEncryptionKey.encode('hex') - printLock.release() + shared.printLock.release() t = (ripe,) - sqlLock.acquire() - sqlSubmitQueue.put('''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT usedpersonally FROM pubkeys WHERE hash=? AND usedpersonally='yes' ''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() 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,data,embeddedTime,'yes') - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() else: print 'We have NOT used this pubkey personally. Inserting in database.' t = (ripe,data,embeddedTime,'no') #This will also update the embeddedTime. - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + shared.workerQueue.put(('newpubkey',(addressVersion,streamNumber,ripe))) #We have received a getpubkey message @@ -1440,19 +1417,19 @@ class receiveDataThread(threading.Thread): readPosition += streamNumberLength inventoryHash = calculateInventoryHash(data) - inventoryLock.acquire() - if inventoryHash in inventory: + shared.inventoryLock.acquire() + if inventoryHash in shared.inventory: print 'We have already received this getpubkey request. Ignoring it.' - inventoryLock.release() + shared.inventoryLock.release() return elif isInSqlInventory(inventoryHash): print 'We have already received this getpubkey request (it is stored on disk in the SQL inventory). Ignoring it.' - inventoryLock.release() + shared.inventoryLock.release() return objectType = 'getpubkey' - inventory[inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) - inventoryLock.release() + shared.inventory[inventoryHash] = (objectType, self.streamNumber, data, embeddedTime) + shared.inventoryLock.release() #This getpubkey request is valid so far. Forward to peers. self.broadcastinv(inventoryHash) @@ -1472,49 +1449,49 @@ class receiveDataThread(threading.Thread): return print 'the hash requested in this getpubkey request is:', requestedHash.encode('hex') - """sqlLock.acquire() + """shared.sqlLock.acquire() t = (requestedHash,int(time.time())-lengthOfTimeToHoldOnToAllPubkeys) #this prevents SQL injection - sqlSubmitQueue.put('''SELECT hash, transmitdata, time FROM pubkeys WHERE hash=? AND havecorrectnonce=1 AND time>?''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlSubmitQueue.put('''SELECT hash, transmitdata, time FROM pubkeys WHERE hash=? AND havecorrectnonce=1 AND time>?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn != []: for row in queryreturn: hash, payload, timeEncodedInPubkey = row - printLock.acquire() + shared.printLock.acquire() print 'We have the requested pubkey stored in our database of pubkeys. Sending it.' - printLock.release() + shared.printLock.release() inventoryHash = calculateInventoryHash(payload) objectType = 'pubkey' - 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. + shared.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. self.broadcastinv(inventoryHash)""" #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.) - if requestedHash in myAddressesByHash: #if this address hash is one of mine - if decodeAddress(myAddressesByHash[requestedHash])[1] != requestedAddressVersionNumber: - printLock.acquire() + if requestedHash in shared.myAddressesByHash: #if this address hash is one of mine + if decodeAddress(shared.myAddressesByHash[requestedHash])[1] != requestedAddressVersionNumber: + shared.printLock.acquire() sys.stderr.write('(Within the recgetpubkey function) Someone requested one of my pubkeys but the requestedAddressVersionNumber doesn\'t match my actual address version number. That shouldn\'t have happened. Ignoring.\n') - printLock.release() + shared.printLock.release() return try: - lastPubkeySendTime = int(config.get(myAddressesByHash[requestedHash],'lastpubkeysendtime')) + lastPubkeySendTime = int(shared.config.get(shared.myAddressesByHash[requestedHash],'lastpubkeysendtime')) except: lastPubkeySendTime = 0 if lastPubkeySendTime < time.time()-lengthOfTimeToHoldOnToAllPubkeys: #If the last time we sent our pubkey was 28 days ago - printLock.acquire() + shared.printLock.acquire() 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.' - printLock.release() + shared.printLock.release() if requestedAddressVersionNumber == 2: - workerQueue.put(('doPOWForMyV2Pubkey',requestedHash)) + shared.workerQueue.put(('doPOWForMyV2Pubkey',requestedHash)) elif requestedAddressVersionNumber == 3: - workerQueue.put(('doPOWForMyV3Pubkey',requestedHash)) + shared.workerQueue.put(('doPOWForMyV3Pubkey',requestedHash)) else: - printLock.acquire() + shared.printLock.acquire() print 'Found getpubkey-requested-hash in my list of EC hashes BUT we already sent it recently. Ignoring request. The lastPubkeySendTime is:',lastPubkeySendTime - printLock.release() + shared.printLock.release() else: - printLock.acquire() + shared.printLock.acquire() print 'This getpubkey request is not for any of my keys.' - printLock.release() + shared.printLock.release() #We have received an inv message @@ -1525,10 +1502,10 @@ class receiveDataThread(threading.Thread): return if numberOfItemsInInv == 1: #we'll just request this data from the person who advertised the object. self.objectsOfWhichThisRemoteNodeIsAlreadyAware[data[lengthOfVarint:32+lengthOfVarint]] = 0 - if data[lengthOfVarint:32+lengthOfVarint] in inventory: - printLock.acquire() + if data[lengthOfVarint:32+lengthOfVarint] in shared.inventory: + shared.printLock.acquire() print 'Inventory (in memory) has inventory item already.' - printLock.release() + shared.printLock.release() elif isInSqlInventory(data[lengthOfVarint:32+lengthOfVarint]): print 'Inventory (SQL on disk) has inventory item already.' else: @@ -1543,9 +1520,9 @@ class receiveDataThread(threading.Thread): #Send a getdata message to our peer to request the object with the given hash def sendgetdata(self,hash): - printLock.acquire() + shared.printLock.acquire() print 'sending getdata to retrieve object with hash:', hash.encode('hex') - printLock.release() + shared.printLock.release() payload = '\x01' + hash headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'getdata\x00\x00\x00\x00\x00' @@ -1555,9 +1532,9 @@ class receiveDataThread(threading.Thread): self.sock.sendall(headerData + payload) except Exception, err: #if not 'Bad file descriptor' in err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('sock.send error: %s\n' % err) - printLock.release() + shared.printLock.release() #We have received a getdata request from our peer def recgetdata(self, data): @@ -1567,20 +1544,20 @@ class receiveDataThread(threading.Thread): return for i in xrange(numberOfRequestedInventoryItems): hash = data[lengthOfVarint+(i*32):32+lengthOfVarint+(i*32)] - printLock.acquire() + shared.printLock.acquire() print 'received getdata request for item:', hash.encode('hex') - printLock.release() - #print 'inventory is', inventory - if hash in inventory: - objectType, streamNumber, payload, receivedTime = inventory[hash] + shared.printLock.release() + #print 'inventory is', shared.inventory + if hash in shared.inventory: + objectType, streamNumber, payload, receivedTime = shared.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() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''select objecttype, payload from inventory where hash=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn <> []: for row in queryreturn: objectType, payload = row @@ -1623,10 +1600,10 @@ class receiveDataThread(threading.Thread): #Send an inv message with just one hash to all of our peers def broadcastinv(self,hash): - printLock.acquire() + shared.printLock.acquire() print 'broadcasting inv with hash:', hash.encode('hex') - printLock.release() - broadcastToSendDataQueues((self.streamNumber, 'sendinv', hash)) + shared.printLock.release() + shared.broadcastToSendDataQueues((self.streamNumber, 'sendinv', hash)) #We have received an addr message. @@ -1636,9 +1613,9 @@ class receiveDataThread(threading.Thread): numberOfAddressesIncluded, lengthOfNumberOfAddresses = decodeVarint(data[:10]) if verbose >= 1: - printLock.acquire() + shared.printLock.acquire() print 'addr message contains', numberOfAddressesIncluded, 'IP addresses.' - printLock.release() + shared.printLock.release() if self.remoteProtocolVersion == 1: if numberOfAddressesIncluded > 1000 or numberOfAddressesIncluded == 0: @@ -1651,22 +1628,22 @@ class receiveDataThread(threading.Thread): for i in range(0,numberOfAddressesIncluded): try: if data[16+lengthOfNumberOfAddresses+(34*i):28+lengthOfNumberOfAddresses+(34*i)] != '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF': - printLock.acquire() + shared.printLock.acquire() print 'Skipping IPv6 address.', repr(data[16+lengthOfNumberOfAddresses+(34*i):28+lengthOfNumberOfAddresses+(34*i)]) - printLock.release() + shared.printLock.release() continue except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('ERROR TRYING TO UNPACK recaddr (to test for an IPv6 address). Message: %s\n' % str(err)) - printLock.release() + shared.printLock.release() break #giving up on unpacking any more. We should still be connected however. try: recaddrStream, = unpack('>I',data[4+lengthOfNumberOfAddresses+(34*i):8+lengthOfNumberOfAddresses+(34*i)]) except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrStream). Message: %s\n' % str(err)) - printLock.release() + shared.printLock.release() break #giving up on unpacking any more. We should still be connected however. if recaddrStream == 0: continue @@ -1675,17 +1652,17 @@ class receiveDataThread(threading.Thread): try: recaddrServices, = unpack('>Q',data[8+lengthOfNumberOfAddresses+(34*i):16+lengthOfNumberOfAddresses+(34*i)]) except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrServices). Message: %s\n' % str(err)) - printLock.release() + shared.printLock.release() break #giving up on unpacking any more. We should still be connected however. try: recaddrPort, = unpack('>H',data[32+lengthOfNumberOfAddresses+(34*i):34+lengthOfNumberOfAddresses+(34*i)]) except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrPort). Message: %s\n' % str(err)) - printLock.release() + shared.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(data[28+lengthOfNumberOfAddresses+(34*i):32+lengthOfNumberOfAddresses+(34*i)]) @@ -1700,36 +1677,36 @@ class receiveDataThread(threading.Thread): print 'Ignoring IP address in private range:', hostFromAddrMessage continue timeSomeoneElseReceivedMessageFromThisNode, = unpack('>I',data[lengthOfNumberOfAddresses+(34*i):4+lengthOfNumberOfAddresses+(34*i)]) #This is the 'time' value in the received addr message. - 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. - knownNodesLock.acquire() - knownNodes[recaddrStream] = {} - knownNodesLock.release() - if hostFromAddrMessage not in knownNodes[recaddrStream]: - 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. - knownNodesLock.acquire() - knownNodes[recaddrStream][hostFromAddrMessage] = (recaddrPort, timeSomeoneElseReceivedMessageFromThisNode) - knownNodesLock.release() + if recaddrStream not in shared.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. + shared.knownNodesLock.acquire() + shared.knownNodes[recaddrStream] = {} + shared.knownNodesLock.release() + if hostFromAddrMessage not in shared.knownNodes[recaddrStream]: + if len(shared.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. + shared.knownNodesLock.acquire() + shared.knownNodes[recaddrStream][hostFromAddrMessage] = (recaddrPort, timeSomeoneElseReceivedMessageFromThisNode) + shared.knownNodesLock.release() 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. + PORT, timeLastReceivedMessageFromThisNode = shared.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())): - knownNodesLock.acquire() - knownNodes[recaddrStream][hostFromAddrMessage] = (PORT, timeSomeoneElseReceivedMessageFromThisNode) - knownNodesLock.release() + shared.knownNodesLock.acquire() + shared.knownNodes[recaddrStream][hostFromAddrMessage] = (PORT, timeSomeoneElseReceivedMessageFromThisNode) + shared.knownNodesLock.release() if PORT != recaddrPort: 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.' if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers. - knownNodesLock.acquire() - output = open(appdata + 'knownnodes.dat', 'wb') - pickle.dump(knownNodes, output) + shared.knownNodesLock.acquire() + output = open(shared.appdata + 'knownnodes.dat', 'wb') + pickle.dump(shared.knownNodes, output) output.close() - knownNodesLock.release() + shared.knownNodesLock.release() self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers) #no longer broadcast - printLock.acquire() - print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.' - printLock.release() + shared.printLock.acquire() + print 'knownNodes currently has', len(shared.knownNodes[self.streamNumber]), 'nodes for this stream.' + shared.printLock.release() elif self.remoteProtocolVersion >= 2: #The difference is that in protocol version 2, network addresses use 64 bit times rather than 32 bit times. if numberOfAddressesIncluded > 1000 or numberOfAddressesIncluded == 0: return @@ -1741,22 +1718,22 @@ class receiveDataThread(threading.Thread): for i in range(0,numberOfAddressesIncluded): try: if data[20+lengthOfNumberOfAddresses+(38*i):32+lengthOfNumberOfAddresses+(38*i)] != '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF': - printLock.acquire() + shared.printLock.acquire() print 'Skipping IPv6 address.', repr(data[20+lengthOfNumberOfAddresses+(38*i):32+lengthOfNumberOfAddresses+(38*i)]) - printLock.release() + shared.printLock.release() continue except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('ERROR TRYING TO UNPACK recaddr (to test for an IPv6 address). Message: %s\n' % str(err)) - printLock.release() + shared.printLock.release() break #giving up on unpacking any more. We should still be connected however. try: recaddrStream, = unpack('>I',data[8+lengthOfNumberOfAddresses+(38*i):12+lengthOfNumberOfAddresses+(38*i)]) except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrStream). Message: %s\n' % str(err)) - printLock.release() + shared.printLock.release() break #giving up on unpacking any more. We should still be connected however. if recaddrStream == 0: continue @@ -1765,17 +1742,17 @@ class receiveDataThread(threading.Thread): try: recaddrServices, = unpack('>Q',data[12+lengthOfNumberOfAddresses+(38*i):20+lengthOfNumberOfAddresses+(38*i)]) except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrServices). Message: %s\n' % str(err)) - printLock.release() + shared.printLock.release() break #giving up on unpacking any more. We should still be connected however. try: recaddrPort, = unpack('>H',data[36+lengthOfNumberOfAddresses+(38*i):38+lengthOfNumberOfAddresses+(38*i)]) except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrPort). Message: %s\n' % str(err)) - printLock.release() + shared.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(data[32+lengthOfNumberOfAddresses+(38*i):36+lengthOfNumberOfAddresses+(38*i)]) @@ -1790,37 +1767,37 @@ class receiveDataThread(threading.Thread): print 'Ignoring IP address in private range:', hostFromAddrMessage continue timeSomeoneElseReceivedMessageFromThisNode, = unpack('>Q',data[lengthOfNumberOfAddresses+(38*i):8+lengthOfNumberOfAddresses+(38*i)]) #This is the 'time' value in the received addr message. 64-bit. - 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. - knownNodesLock.acquire() - knownNodes[recaddrStream] = {} - knownNodesLock.release() - if hostFromAddrMessage not in knownNodes[recaddrStream]: - 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. - knownNodesLock.acquire() - knownNodes[recaddrStream][hostFromAddrMessage] = (recaddrPort, timeSomeoneElseReceivedMessageFromThisNode) - knownNodesLock.release() + if recaddrStream not in shared.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. + shared.knownNodesLock.acquire() + shared.knownNodes[recaddrStream] = {} + shared.knownNodesLock.release() + if hostFromAddrMessage not in shared.knownNodes[recaddrStream]: + if len(shared.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. + shared.knownNodesLock.acquire() + shared.knownNodes[recaddrStream][hostFromAddrMessage] = (recaddrPort, timeSomeoneElseReceivedMessageFromThisNode) + shared.knownNodesLock.release() print 'added new node', hostFromAddrMessage, 'to knownNodes in stream', recaddrStream 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. + PORT, timeLastReceivedMessageFromThisNode = shared.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())): - knownNodesLock.acquire() - knownNodes[recaddrStream][hostFromAddrMessage] = (PORT, timeSomeoneElseReceivedMessageFromThisNode) - knownNodesLock.release() + shared.knownNodesLock.acquire() + shared.knownNodes[recaddrStream][hostFromAddrMessage] = (PORT, timeSomeoneElseReceivedMessageFromThisNode) + shared.knownNodesLock.release() if PORT != recaddrPort: 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.' if needToWriteKnownNodesToDisk: #Runs if any nodes were new to us. Also, share those nodes with our peers. - knownNodesLock.acquire() - output = open(appdata + 'knownnodes.dat', 'wb') - pickle.dump(knownNodes, output) + shared.knownNodesLock.acquire() + output = open(shared.appdata + 'knownnodes.dat', 'wb') + pickle.dump(shared.knownNodes, output) output.close() - knownNodesLock.release() + shared.knownNodesLock.release() self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers) - printLock.acquire() - print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.' - printLock.release() + shared.printLock.acquire() + print 'knownNodes currently has', len(shared.knownNodes[self.streamNumber]), 'nodes for this stream.' + shared.printLock.release() #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. @@ -1842,40 +1819,40 @@ class receiveDataThread(threading.Thread): datatosend = datatosend + payload if verbose >= 1: - printLock.acquire() + shared.printLock.acquire() print 'Broadcasting addr with', numberOfAddressesInAddrMessage, 'entries.' - printLock.release() - broadcastToSendDataQueues((self.streamNumber, 'sendaddr', datatosend)) + shared.printLock.release() + shared.broadcastToSendDataQueues((self.streamNumber, 'sendaddr', datatosend)) #Send a big addr message to our peer def sendaddr(self): addrsInMyStream = {} addrsInChildStreamLeft = {} addrsInChildStreamRight = {} - #print 'knownNodes', knownNodes + #print 'knownNodes', shared.knownNodes #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: + if len(shared.knownNodes[self.streamNumber]) > 0: for i in range(500): random.seed() - HOST, = random.sample(knownNodes[self.streamNumber], 1) + HOST, = random.sample(shared.knownNodes[self.streamNumber], 1) if self.isHostInPrivateIPRange(HOST): continue - addrsInMyStream[HOST] = knownNodes[self.streamNumber][HOST] - if len(knownNodes[self.streamNumber*2]) > 0: + addrsInMyStream[HOST] = shared.knownNodes[self.streamNumber][HOST] + if len(shared.knownNodes[self.streamNumber*2]) > 0: for i in range(250): random.seed() - HOST, = random.sample(knownNodes[self.streamNumber*2], 1) + HOST, = random.sample(shared.knownNodes[self.streamNumber*2], 1) if self.isHostInPrivateIPRange(HOST): continue - addrsInChildStreamLeft[HOST] = knownNodes[self.streamNumber*2][HOST] - if len(knownNodes[(self.streamNumber*2)+1]) > 0: + addrsInChildStreamLeft[HOST] = shared.knownNodes[self.streamNumber*2][HOST] + if len(shared.knownNodes[(self.streamNumber*2)+1]) > 0: for i in range(250): random.seed() - HOST, = random.sample(knownNodes[(self.streamNumber*2)+1], 1) + HOST, = random.sample(shared.knownNodes[(self.streamNumber*2)+1], 1) if self.isHostInPrivateIPRange(HOST): continue - addrsInChildStreamRight[HOST] = knownNodes[(self.streamNumber*2)+1][HOST] + addrsInChildStreamRight[HOST] = shared.knownNodes[(self.streamNumber*2)+1][HOST] numberOfAddressesInAddrMessage = 0 payload = '' @@ -1924,9 +1901,9 @@ class receiveDataThread(threading.Thread): datatosend = datatosend + payload if verbose >= 1: - printLock.acquire() + shared.printLock.acquire() print 'Sending addr with', numberOfAddressesInAddrMessage, 'entries.' - printLock.release() + shared.printLock.release() self.sock.sendall(datatosend) #We have received a version message @@ -1948,36 +1925,36 @@ class receiveDataThread(threading.Thread): numberOfStreamsInVersionMessage, lengthOfNumberOfStreamsInVersionMessage = decodeVarint(data[readPosition:]) readPosition += lengthOfNumberOfStreamsInVersionMessage self.streamNumber, lengthOfRemoteStreamNumber = decodeVarint(data[readPosition:]) - printLock.acquire() + shared.printLock.acquire() print 'Remote node useragent:', useragent, ' stream number:', self.streamNumber - printLock.release() + shared.printLock.release() if self.streamNumber != 1: #self.sock.shutdown(socket.SHUT_RDWR) #self.sock.close() - broadcastToSendDataQueues((0, 'shutdown', self.HOST)) - printLock.acquire() + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) + shared.printLock.acquire() print 'Closed connection to', self.HOST, 'because they are interested in stream', self.streamNumber,'.' - printLock.release() + shared.printLock.release() return #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))) + shared.broadcastToSendDataQueues((0,'setStreamNumber',(self.HOST,self.streamNumber))) if data[72:80] == eightBytesOfRandomDataUsedToDetectConnectionsToSelf: #self.sock.shutdown(socket.SHUT_RDWR) #self.sock.close() - broadcastToSendDataQueues((0, 'shutdown', self.HOST)) - printLock.acquire() + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) + shared.printLock.acquire() print 'Closing connection to myself: ', self.HOST - printLock.release() + shared.printLock.release() return - broadcastToSendDataQueues((0,'setRemoteProtocolVersion',(self.HOST,self.remoteProtocolVersion))) + shared.broadcastToSendDataQueues((0,'setRemoteProtocolVersion',(self.HOST,self.remoteProtocolVersion))) - knownNodesLock.acquire() - knownNodes[self.streamNumber][self.HOST] = (self.remoteNodeIncomingPort, int(time.time())) - output = open(appdata + 'knownnodes.dat', 'wb') - pickle.dump(knownNodes, output) + shared.knownNodesLock.acquire() + shared.knownNodes[self.streamNumber][self.HOST] = (self.remoteNodeIncomingPort, int(time.time())) + output = open(shared.appdata + 'knownnodes.dat', 'wb') + pickle.dump(shared.knownNodes, output) output.close() - knownNodesLock.release() + shared.knownNodesLock.release() self.sendverack() if self.initiatedConnection == False: @@ -1985,16 +1962,16 @@ class receiveDataThread(threading.Thread): #Sends a version message def sendversion(self): - printLock.acquire() + shared.printLock.acquire() print 'Sending version message' - printLock.release() + shared.printLock.release() self.sock.sendall(assembleVersionMessage(self.HOST,self.PORT,self.streamNumber)) #Sends a verack message def sendverack(self): - printLock.acquire() + shared.printLock.acquire() print 'Sending verack' - printLock.release() + shared.printLock.release() 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 self.verackSent = True @@ -2017,10 +1994,10 @@ class sendDataThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.mailbox = Queue.Queue() - sendDataQueues.append(self.mailbox) - printLock.acquire() - print 'The length of sendDataQueues at sendDataThread init is:', len(sendDataQueues) - printLock.release() + shared.sendDataQueues.append(self.mailbox) + shared.printLock.acquire() + print 'The length of sendDataQueues at sendDataThread init is:', len(shared.sendDataQueues) + shared.printLock.release() self.data = '' def setup(self,sock,HOST,PORT,streamNumber,objectsOfWhichThisRemoteNodeIsAlreadyAware): @@ -2031,16 +2008,16 @@ class sendDataThread(threading.Thread): self.remoteProtocolVersion = -1 #This must be set using setRemoteProtocolVersion command which is sent through the self.mailbox queue. self.lastTimeISentData = int(time.time()) #If this value increases beyond five minutes ago, we'll send a pong message to keep the connection alive. self.objectsOfWhichThisRemoteNodeIsAlreadyAware = objectsOfWhichThisRemoteNodeIsAlreadyAware - printLock.acquire() + shared.printLock.acquire() print 'The streamNumber of this sendDataThread (ID:', str(id(self))+') at setup() is', self.streamNumber - printLock.release() + shared.printLock.release() def sendVersionMessage(self): datatosend = assembleVersionMessage(self.HOST,self.PORT,self.streamNumber)#the IP and port of the remote host, and my streamNumber. - printLock.acquire() + shared.printLock.acquire() print 'Sending version packet: ', repr(datatosend) - printLock.release() + shared.printLock.release() self.sock.sendall(datatosend) self.versionSent = 1 @@ -2048,43 +2025,43 @@ class sendDataThread(threading.Thread): def run(self): while True: deststream,command,data = self.mailbox.get() - #printLock.acquire() + #shared.printLock.acquire() #print 'sendDataThread, destream:', deststream, ', Command:', command, ', ID:',id(self), ', HOST:', self.HOST - #printLock.release() + #shared.printLock.release() if deststream == self.streamNumber or deststream == 0: if command == 'shutdown': if data == self.HOST or data == 'all': - printLock.acquire() + shared.printLock.acquire() print 'sendDataThread (associated with', self.HOST,') ID:',id(self), 'shutting down now.' - printLock.release() + shared.printLock.release() self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() - sendDataQueues.remove(self.mailbox) - printLock.acquire() - print 'len of sendDataQueues', len(sendDataQueues) - printLock.release() + shared.sendDataQueues.remove(self.mailbox) + shared.printLock.acquire() + print 'len of sendDataQueues', len(shared.sendDataQueues) + shared.printLock.release() break #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: elif command == 'setStreamNumber': hostInMessage, specifiedStreamNumber = data if hostInMessage == self.HOST: - printLock.acquire() + shared.printLock.acquire() print 'setting the stream number in the sendData thread (ID:',id(self), ') to', specifiedStreamNumber - printLock.release() + shared.printLock.release() self.streamNumber = specifiedStreamNumber elif command == 'setRemoteProtocolVersion': hostInMessage, specifiedRemoteProtocolVersion = data if hostInMessage == self.HOST: - printLock.acquire() + shared.printLock.acquire() print 'setting the remote node\'s protocol version in the sendData thread (ID:',id(self), ') to', specifiedRemoteProtocolVersion - printLock.release() + shared.printLock.release() self.remoteProtocolVersion = specifiedRemoteProtocolVersion elif command == 'sendaddr': if self.remoteProtocolVersion == 1: - printLock.acquire() + shared.printLock.acquire() print 'a sendData thread is not sending an addr message to this particular peer ('+self.HOST+') because their protocol version is 1.' - printLock.release() + shared.printLock.release() else: try: #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. @@ -2096,7 +2073,7 @@ class sendDataThread(threading.Thread): print 'self.sock.sendall failed' self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() - sendDataQueues.remove(self.mailbox) + shared.sendDataQueues.remove(self.mailbox) print 'sendDataThread thread (ID:',str(id(self))+') ending now. Was connected to', self.HOST break elif command == 'sendinv': @@ -2116,15 +2093,15 @@ class sendDataThread(threading.Thread): print 'self.sock.sendall failed' self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() - sendDataQueues.remove(self.mailbox) + shared.sendDataQueues.remove(self.mailbox) print 'sendDataThread thread (ID:',str(id(self))+') ending now. Was connected to', self.HOST break elif command == 'pong': if self.lastTimeISentData < (int(time.time()) - 298): #Send out a pong message to keep the connection alive. - printLock.acquire() + shared.printLock.acquire() print 'Sending pong to', self.HOST, 'to keep connection alive.' - printLock.release() + shared.printLock.release() try: 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') self.lastTimeISentData = int(time.time()) @@ -2132,41 +2109,23 @@ class sendDataThread(threading.Thread): print 'send pong failed' self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() - sendDataQueues.remove(self.mailbox) + shared.sendDataQueues.remove(self.mailbox) print 'sendDataThread thread', self, 'ending now. Was connected to', self.HOST break else: - printLock.acquire() + shared.printLock.acquire() print 'sendDataThread ID:',id(self),'ignoring command', command,'because the thread is not in stream',deststream - printLock.release() + shared.printLock.release() -#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] - sqlSubmitQueue.put('commit') - 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() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''select hash from inventory where hash=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn == []: return False else: @@ -2184,40 +2143,7 @@ def convertIntToString(n): def convertStringToInt(s): return int(s.encode('hex'), 16) -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() - print 'reloading keys from keys.dat file' - printLock.release() - myECCryptorObjects.clear() - myAddressesByHash.clear() - #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 or addressVersionNumber == 3: - 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 - myECCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey) - myAddressesByHash[hash] = addressInKeysFile - else: - sys.stderr.write('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2 or 3.\n') #This function expects that pubkey begin with \x04 def calculateBitcoinAddressFromPubkey(pubkey): @@ -2258,47 +2184,16 @@ def calculateTestnetAddressFromPubkey(pubkey): base58encoded = arithmetic.changebase(binaryBitcoinAddress,256,58) return "1"*numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded -def safeConfigGetBoolean(section,field): - try: - return config.getboolean(section,field) - except: - return False + def signal_handler(signal, frame): - if safeConfigGetBoolean('bitmessagesettings','daemon'): - doCleanShutdown() + if shared.safeConfigGetBoolean('bitmessagesettings','daemon'): + shared.doCleanShutdown() sys.exit(0) else: print 'Unfortunately you cannot use Ctrl+C when running the UI because the UI captures the signal.' -def doCleanShutdown(): - UISignalQueue.put(('updateStatusBar','Bitmessage is stuck waiting for the knownNodesLock.')) - knownNodesLock.acquire() - UISignalQueue.put(('updateStatusBar','Saving the knownNodes list of peers to disk...')) - output = open(appdata + 'knownnodes.dat', 'wb') - print 'finished opening knownnodes.dat. Now pickle.dump' - pickle.dump(knownNodes, output) - print 'Completed pickle.dump. Closing output...' - output.close() - knownNodesLock.release() - print 'Finished closing knownnodes.dat output file.' - UISignalQueue.put(('updateStatusBar','Done saving the knownNodes list of peers to disk.')) - broadcastToSendDataQueues((0, 'shutdown', 'all')) - - printLock.acquire() - print 'Flushing inventory in memory out to disk...' - printLock.release() - UISignalQueue.put(('updateStatusBar','Flushing inventory in memory out to disk. This should normally only take a second...')) - flushInventory() - print 'Finished flushing inventory.' - sqlSubmitQueue.put('exit') - - if safeConfigGetBoolean('bitmessagesettings','daemon'): - printLock.acquire() - print 'Done.' - printLock.release() - os._exit(0) def connectToStream(streamNumber): #self.listOfOutgoingSynSenderThreads = [] #if we don't maintain this list, the threads will get garbage-collected. @@ -2339,51 +2234,7 @@ def pointMult(secret): OpenSSL.EC_KEY_free(k) return mb.raw -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 - -def isAddressInMyAddressBook(address): - t = (address,) - sqlLock.acquire() - sqlSubmitQueue.put('''select address from addressbook where address=?''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() - return queryreturn != [] - -def isAddressInMyAddressBookSubscriptionsListOrWhitelist(address): - if isAddressInMyAddressBook(address): - return True - - sqlLock.acquire() - sqlSubmitQueue.put('''SELECT address FROM whitelist where address=? and enabled = '1' ''') - sqlSubmitQueue.put((address,)) - queryreturn = sqlReturnQueue.get() - sqlLock.release() - if queryreturn <> []: - return True - - sqlLock.acquire() - sqlSubmitQueue.put('''select address from subscriptions where address=? and enabled = '1' ''') - sqlSubmitQueue.put((address,)) - queryreturn = sqlReturnQueue.get() - sqlLock.release() - if queryreturn <> []: - return True - return False def assembleVersionMessage(remoteHost,remotePort,myStreamNumber): global softwareVersion @@ -2398,7 +2249,7 @@ def assembleVersionMessage(remoteHost,remotePort,myStreamNumber): 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 + payload += pack('>H',shared.config.getint('bitmessagesettings', 'port'))#my external IPv6 and port random.seed() payload += eightBytesOfRandomDataUsedToDetectConnectionsToSelf @@ -2420,7 +2271,7 @@ class sqlThread(threading.Thread): threading.Thread.__init__(self) def run(self): - self.conn = sqlite3.connect(appdata + 'messages.dat' ) + self.conn = sqlite3.connect(shared.appdata + 'messages.dat' ) self.conn.text_factory = str self.cur = self.conn.cursor() try: @@ -2446,24 +2297,26 @@ class sqlThread(threading.Thread): print 'Created messages database file' except Exception, err: if str(err) == 'table inbox already exists': + shared.printLock.acquire() print 'Database file already exists.' + shared.printLock.release() else: sys.stderr.write('ERROR trying to create database file (message.dat). Error message: %s\n' % str(err)) sys.exit() #People running earlier versions of PyBitmessage do not have the usedpersonally field in their pubkeys table. Let's add it. - if config.getint('bitmessagesettings','settingsversion') == 2: + if shared.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: + shared.config.set('bitmessagesettings','settingsversion','3') + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) #People running earlier versions of PyBitmessage do not have the encodingtype field in their inbox and sent tables or the read field in the inbox table. Let's add them. - if config.getint('bitmessagesettings','settingsversion') == 3: + if shared.config.getint('bitmessagesettings','settingsversion') == 3: item = '''ALTER TABLE inbox ADD encodingtype int DEFAULT '2' ''' parameters = '' self.cur.execute(item, parameters) @@ -2477,15 +2330,15 @@ class sqlThread(threading.Thread): self.cur.execute(item, parameters) self.conn.commit() - config.set('bitmessagesettings','settingsversion','4') - with open(appdata + 'keys.dat', 'wb') as configfile: + shared.config.set('bitmessagesettings','settingsversion','4') + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) - if config.getint('bitmessagesettings','settingsversion') == 4: - config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) - config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) - config.set('bitmessagesettings','settingsversion','5') - with open(appdata + 'keys.dat', 'wb') as configfile: + if shared.config.getint('bitmessagesettings','settingsversion') == 4: + shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) + shared.config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) + shared.config.set('bitmessagesettings','settingsversion','5') + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) #From now on, let us keep a 'version' embedded in the messages.dat file so that when we make changes to the database, the database version we are on can stay embedded in the messages.dat file. Let us check to see if the settings table exists yet. @@ -2548,18 +2401,19 @@ class sqlThread(threading.Thread): self.cur.execute(item, parameters) while True: - item = sqlSubmitQueue.get() + item = shared.sqlSubmitQueue.get() if item == 'commit': self.conn.commit() elif item == 'exit': + print 'sqlThread exiting gracefully.' return else: - parameters = sqlSubmitQueue.get() + parameters = shared.sqlSubmitQueue.get() #print 'item', item #print 'parameters', parameters self.cur.execute(item, parameters) - sqlReturnQueue.put(self.cur.fetchall()) - #sqlSubmitQueue.task_done() + shared.sqlReturnQueue.put(self.cur.fetchall()) + #shared.sqlSubmitQueue.task_done() @@ -2584,71 +2438,74 @@ class singleCleaner(threading.Thread): timeWeLastClearedInventoryAndPubkeysTables = 0 while True: - sqlLock.acquire() + shared.sqlLock.acquire() #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing housekeeping (Flushing inventory in memory to disk...)") - UISignalQueue.put(('updateStatusBar','Doing housekeeping (Flushing inventory in memory to disk...)')) - for hash, storedValue in inventory.items(): + shared.UISignalQueue.put(('updateStatusBar','Doing housekeeping (Flushing inventory in memory to disk...)')) + for hash, storedValue in shared.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] - sqlSubmitQueue.put('commit') + shared.sqlSubmitQueue.put('''INSERT INTO inventory VALUES (?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + del shared.inventory[hash] + shared.sqlSubmitQueue.put('commit') #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") - UISignalQueue.put(('updateStatusBar','')) - 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. + shared.UISignalQueue.put(('updateStatusBar','')) + shared.sqlLock.release() + shared.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 we are running as a daemon then we are going to fill up the UI queue which will never be handled by a UI. We should clear it to save memory. + if shared.safeConfigGetBoolean('bitmessagesettings','daemon'): + shared.UISignalQueue.queue.clear() 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() + shared.sqlLock.acquire() #inventory (clears data more than 2 days and 12 hours old) t = (int(time.time())-lengthOfTimeToLeaveObjectsInInventory,int(time.time())-lengthOfTimeToHoldOnToAllPubkeys) - sqlSubmitQueue.put('''DELETE FROM inventory WHERE (receivedtime'pubkey') OR (receivedtime'pubkey') OR (receivedtime (maximumAgeOfAnObjectThatIAmWillingToAccept * (2 ** (pubkeyretrynumber))): print 'It has been a long time and we haven\'t heard a response to our getpubkey request. Sending again.' try: - 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. + del neededPubkeys[toripe] #We need to take this entry out of the neededPubkeys structure because the shared.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. except: pass - workerQueue.put(('sendmessage',toaddress)) + shared.workerQueue.put(('sendmessage',toaddress)) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing work necessary to again attempt to request a public key...") - UISignalQueue.put(('updateStatusBar','Doing work necessary to again attempt to request a public key...')) + shared.UISignalQueue.put(('updateStatusBar','Doing work necessary to again attempt to request a public key...')) t = (int(time.time()),pubkeyretrynumber+1,toripe) - sqlSubmitQueue.put('''UPDATE sent SET lastactiontime=?, pubkeyretrynumber=? WHERE toripe=?''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() + shared.sqlSubmitQueue.put('''UPDATE sent SET lastactiontime=?, pubkeyretrynumber=? WHERE toripe=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() 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() - workerQueue.put(('sendmessage',toaddress)) + shared.sqlSubmitQueue.put('''UPDATE sent SET lastactiontime=?, msgretrynumber=?, status=? WHERE ackdata=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.workerQueue.put(('sendmessage',toaddress)) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Doing work necessary to again attempt to deliver a message...") - UISignalQueue.put(('updateStatusBar','Doing work necessary to again attempt to deliver a message...')) - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.UISignalQueue.put(('updateStatusBar','Doing work necessary to again attempt to deliver a message...')) + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() time.sleep(300) #This thread, of which there is only one, does the heavy lifting: calculating POWs. @@ -2658,21 +2515,21 @@ class singleWorker(threading.Thread): threading.Thread.__init__(self) def run(self): - sqlLock.acquire() - sqlSubmitQueue.put('''SELECT toripe FROM sent WHERE (status=? AND folder='sent')''') - sqlSubmitQueue.put(('findingpubkey',)) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT toripe FROM sent WHERE (status=? AND folder='sent')''') + shared.sqlSubmitQueue.put(('findingpubkey',)) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: toripe, = row #It is possible for the status of a message in our sent folder (which is also our 'outbox' folder) to have a status of 'findingpubkey' even if we have the pubkey. This can #happen if the worker thread is working on the POW for an earlier message and does not get to the message in question before the user closes Bitmessage. In this case, the #status will still be 'findingpubkey' but Bitmessage will never have checked to see whether it actually already has the pubkey. We should therefore check here. - sqlLock.acquire() - sqlSubmitQueue.put('''SELECT hash FROM pubkeys WHERE hash=? ''') - sqlSubmitQueue.put((toripe,)) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT hash FROM pubkeys WHERE hash=? ''') + shared.sqlSubmitQueue.put((toripe,)) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn != []: #If we have the pubkey then send the message otherwise put the hash in the neededPubkeys data structure so that we will pay attention to it if it comes over the wire. self.sendMsg(toripe) else: @@ -2681,30 +2538,30 @@ class singleWorker(threading.Thread): 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() + shared.sqlLock.acquire() t = ('doingpow',) - sqlSubmitQueue.put('''SELECT toripe FROM sent WHERE status=? and folder='sent' ''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlSubmitQueue.put('''SELECT toripe FROM sent WHERE status=? and folder='sent' ''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: toripe, = row #Evidentially there is a remote possibility that we may, for some reason, no longer have the recipient's pubkey. Let us make sure we still have it or else the sendMsg function will appear to freeze. - sqlLock.acquire() - sqlSubmitQueue.put('''SELECT hash FROM pubkeys WHERE hash=? ''') - sqlSubmitQueue.put((toripe,)) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT hash FROM pubkeys WHERE hash=? ''') + shared.sqlSubmitQueue.put((toripe,)) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn != []: #We have the needed pubkey self.sendMsg(toripe) else: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('For some reason, the status of a message in our outbox is \'doingpow\' even though we lack the pubkey. Here is the RIPE hash of the needed pubkey: %s\n' % toripe.encode('hex')) - printLock.release() + shared.printLock.release() while True: - command, data = workerQueue.get() + command, data = shared.workerQueue.get() #statusbar = 'The singleWorker thread is working on work.' #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) if command == 'sendmessage': @@ -2713,11 +2570,11 @@ class singleWorker(threading.Thread): #print 'message type', type(message) #print repr(message.toUtf8()) #print str(message.toUtf8()) - sqlLock.acquire() - sqlSubmitQueue.put('SELECT hash FROM pubkeys WHERE hash=?') - sqlSubmitQueue.put((toRipe,)) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('SELECT hash FROM pubkeys WHERE hash=?') + shared.sqlSubmitQueue.put((toRipe,)) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() #print 'queryreturn', queryreturn if queryreturn == []: #We'll need to request the pub key because we don't have it. @@ -2728,7 +2585,7 @@ class singleWorker(threading.Thread): 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.') - UISignalQueue.put(('updateSentItemStatusByHash',(toRipe,'Public key was requested earlier. Receiver must be offline. Will retry.'))) + shared.UISignalQueue.put(('updateSentItemStatusByHash',(toRipe,'Public key was requested earlier. Receiver must be offline. Will retry.'))) else: print 'We already have the necessary public key.' @@ -2748,15 +2605,15 @@ class singleWorker(threading.Thread): del neededPubkeys[toRipe] self.sendMsg(toRipe) else: - printLock.acquire() + shared.printLock.acquire() print 'We don\'t need this pub key. We didn\'t ask for it. Pubkey hash:', toRipe.encode('hex') - printLock.release() + shared.printLock.release() else: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('Probable programming error: The command sent to the workerThread is weird. It is: %s\n' % command) - printLock.release() + shared.printLock.release() - workerQueue.task_done() + shared.workerQueue.task_done() def doPOWForMyV2Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW #Look up my stream number based on my address hash @@ -2767,7 +2624,7 @@ class singleWorker(threading.Thread): if hash == hashFromThisParticularAddress: myAddress = addressInKeysFile break""" - myAddress = myAddressesByHash[hash] + myAddress = shared.myAddressesByHash[hash] status,addressVersionNumber,streamNumber,hash = decodeAddress(myAddress) embeddedTime = int(time.time()+random.randrange(-300, 300)) #the current time plus or minus five minutes payload = pack('>I',(embeddedTime)) @@ -2776,16 +2633,16 @@ class singleWorker(threading.Thread): payload += '\x00\x00\x00\x01' #bitfield of features supported by me (see the wiki). try: - privSigningKeyBase58 = config.get(myAddress, 'privsigningkey') - privEncryptionKeyBase58 = config.get(myAddress, 'privencryptionkey') + privSigningKeyBase58 = shared.config.get(myAddress, 'privsigningkey') + privEncryptionKeyBase58 = shared.config.get(myAddress, 'privencryptionkey') except Exception, err: - printLock.acquire() + shared.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() + shared.printLock.release() return - privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') + privSigningKeyHex = shared.decodeWalletImportFormat(privSigningKeyBase58).encode('hex') + privEncryptionKeyHex = shared.decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode('hex') pubEncryptionKey = highlevelcrypto.privToPub(privEncryptionKeyHex).decode('hex') @@ -2805,29 +2662,29 @@ class singleWorker(threading.Thread): payload = pack('>Q',nonce) + payload """t = (hash,payload,embeddedTime,'no') - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release()""" + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release()""" inventoryHash = calculateInventoryHash(payload) objectType = 'pubkey' - inventory[inventoryHash] = (objectType, streamNumber, payload, embeddedTime) + shared.inventory[inventoryHash] = (objectType, streamNumber, payload, embeddedTime) - printLock.acquire() + shared.printLock.acquire() print 'broadcasting inv with hash:', inventoryHash.encode('hex') - printLock.release() - broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) + shared.printLock.release() + shared.broadcastToSendDataQueues((streamNumber, 'sendinv', shared.inventoryHash)) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") - UISignalQueue.put(('updateStatusBar','')) - config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) - with open(appdata + 'keys.dat', 'wb') as configfile: + shared.UISignalQueue.put(('updateStatusBar','')) + shared.config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) def doPOWForMyV3Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW - myAddress = myAddressesByHash[hash] + myAddress = shared.myAddressesByHash[hash] status,addressVersionNumber,streamNumber,hash = decodeAddress(myAddress) embeddedTime = int(time.time()+random.randrange(-300, 300)) #the current time plus or minus five minutes payload = pack('>I',(embeddedTime)) @@ -2836,24 +2693,24 @@ class singleWorker(threading.Thread): payload += '\x00\x00\x00\x01' #bitfield of features supported by me (see the wiki). try: - privSigningKeyBase58 = config.get(myAddress, 'privsigningkey') - privEncryptionKeyBase58 = config.get(myAddress, 'privencryptionkey') + privSigningKeyBase58 = shared.config.get(myAddress, 'privsigningkey') + privEncryptionKeyBase58 = shared.config.get(myAddress, 'privencryptionkey') except Exception, err: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('Error within doPOWForMyV3Pubkey. Could not read the keys from the keys.dat file for a requested address. %s\n' % err) - printLock.release() + shared.printLock.release() return - privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') + privSigningKeyHex = shared.decodeWalletImportFormat(privSigningKeyBase58).encode('hex') + privEncryptionKeyHex = shared.decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode('hex') pubEncryptionKey = highlevelcrypto.privToPub(privEncryptionKeyHex).decode('hex') payload += pubSigningKey[1:] payload += pubEncryptionKey[1:] - payload += encodeVarint(config.getint(myAddress,'noncetrialsperbyte')) - payload += encodeVarint(config.getint(myAddress,'payloadlengthextrabytes')) + payload += encodeVarint(shared.config.getint(myAddress,'noncetrialsperbyte')) + payload += encodeVarint(shared.config.getint(myAddress,'payloadlengthextrabytes')) signature = highlevelcrypto.sign(payload,privSigningKeyHex) payload += encodeVarint(len(signature)) payload += signature @@ -2871,49 +2728,49 @@ class singleWorker(threading.Thread): payload = pack('>Q',nonce) + payload """t = (hash,payload,embeddedTime,'no') - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release()""" + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO pubkeys VALUES (?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release()""" inventoryHash = calculateInventoryHash(payload) objectType = 'pubkey' - inventory[inventoryHash] = (objectType, streamNumber, payload, embeddedTime) + shared.inventory[inventoryHash] = (objectType, streamNumber, payload, embeddedTime) - printLock.acquire() + shared.printLock.acquire() print 'broadcasting inv with hash:', inventoryHash.encode('hex') - printLock.release() - broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) + shared.printLock.release() + shared.broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"") - UISignalQueue.put(('updateStatusBar','')) - config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) - with open(appdata + 'keys.dat', 'wb') as configfile: + shared.UISignalQueue.put(('updateStatusBar','')) + shared.config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) def sendBroadcast(self): - sqlLock.acquire() + shared.sqlLock.acquire() t = ('broadcastpending',) - sqlSubmitQueue.put('''SELECT fromaddress, subject, message, ackdata FROM sent WHERE status=? and folder='sent' ''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlSubmitQueue.put('''SELECT fromaddress, subject, message, ackdata FROM sent WHERE status=? and folder='sent' ''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: fromaddress, subject, body, ackdata = row status,addressVersionNumber,streamNumber,ripe = decodeAddress(fromaddress) if addressVersionNumber == 2 and int(time.time()) < encryptedBroadcastSwitchoverTime: #We need to convert our private keys to public keys in order to include them. try: - privSigningKeyBase58 = config.get(fromaddress, 'privsigningkey') - privEncryptionKeyBase58 = config.get(fromaddress, 'privencryptionkey') + privSigningKeyBase58 = shared.config.get(fromaddress, 'privsigningkey') + privEncryptionKeyBase58 = shared.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.') - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) continue - privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') + privSigningKeyHex = shared.decodeWalletImportFormat(privSigningKeyBase58).encode('hex') + privEncryptionKeyHex = shared.decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') 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. pubEncryptionKey = highlevelcrypto.privToPub(privEncryptionKeyHex).decode('hex') @@ -2939,7 +2796,7 @@ class singleWorker(threading.Thread): target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) print '(For broadcast message) Doing proof of work...' #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) initialHash = hashlib.sha512(payload).digest() while trialValue > target: nonce += 1 @@ -2950,33 +2807,33 @@ class singleWorker(threading.Thread): inventoryHash = calculateInventoryHash(payload) objectType = 'broadcast' - inventory[inventoryHash] = (objectType, streamNumber, payload, int(time.time())) + shared.inventory[inventoryHash] = (objectType, streamNumber, payload, int(time.time())) print 'sending inv (within sendBroadcast function)' - broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) + shared.broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) - #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Broadcast sent on '+unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) #Update the status of the message in the 'sent' table to have a 'broadcastsent' status - sqlLock.acquire() + shared.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() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('UPDATE sent SET status=?, lastactiontime=? WHERE fromaddress=? AND subject=? AND message=? AND status=?') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() elif addressVersionNumber == 3 or int(time.time()) > encryptedBroadcastSwitchoverTime: #We need to convert our private keys to public keys in order to include them. try: - privSigningKeyBase58 = config.get(fromaddress, 'privsigningkey') - privEncryptionKeyBase58 = config.get(fromaddress, 'privencryptionkey') + privSigningKeyBase58 = shared.config.get(fromaddress, 'privsigningkey') + privEncryptionKeyBase58 = shared.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.') - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) continue - privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') + privSigningKeyHex = shared.decodeWalletImportFormat(privSigningKeyBase58).encode('hex') + privEncryptionKeyHex = shared.decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') 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. pubEncryptionKey = highlevelcrypto.privToPub(privEncryptionKeyHex).decode('hex') @@ -2992,8 +2849,8 @@ class singleWorker(threading.Thread): dataToEncrypt += pubSigningKey[1:] dataToEncrypt += pubEncryptionKey[1:] if addressVersionNumber >= 3: - dataToEncrypt += encodeVarint(config.getint(fromaddress,'noncetrialsperbyte')) - dataToEncrypt += encodeVarint(config.getint(fromaddress,'payloadlengthextrabytes')) + dataToEncrypt += encodeVarint(shared.config.getint(fromaddress,'noncetrialsperbyte')) + dataToEncrypt += encodeVarint(shared.config.getint(fromaddress,'payloadlengthextrabytes')) dataToEncrypt += '\x02' #message encoding type dataToEncrypt += encodeVarint(len('Subject:' + subject + '\n' + 'Body:' + body)) #Type 2 is simple UTF-8 message encoding. dataToEncrypt += 'Subject:' + subject + '\n' + 'Body:' + body @@ -3010,7 +2867,7 @@ class singleWorker(threading.Thread): target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) print '(For broadcast message) Doing proof of work...' #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) initialHash = hashlib.sha512(payload).digest() while trialValue > target: nonce += 1 @@ -3021,50 +2878,50 @@ class singleWorker(threading.Thread): inventoryHash = calculateInventoryHash(payload) objectType = 'broadcast' - inventory[inventoryHash] = (objectType, streamNumber, payload, int(time.time())) + shared.inventory[inventoryHash] = (objectType, streamNumber, payload, int(time.time())) print 'sending inv (within sendBroadcast function)' - broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) + shared.broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) - #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Broadcast sent on '+unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Broadcast sent on '+unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Broadcast sent on '+unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) #Update the status of the message in the 'sent' table to have a 'broadcastsent' status - sqlLock.acquire() + shared.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() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('UPDATE sent SET status=?, lastactiontime=? WHERE fromaddress=? AND subject=? AND message=? AND status=?') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() else: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('Error: In the singleWorker thread, the sendBroadcast function doesn\'t understand the address version.\n') - printLock.release() + shared.printLock.release() def sendMsg(self,toRipe): - sqlLock.acquire() + shared.sqlLock.acquire() t = ('doingpow','findingpubkey',toRipe) - sqlSubmitQueue.put('''UPDATE sent SET status=? WHERE status=? AND toripe=? and folder='sent' ''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlSubmitQueue.put('commit') + shared.sqlSubmitQueue.put('''UPDATE sent SET status=? WHERE status=? AND toripe=? and folder='sent' ''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') t = ('doingpow',toRipe) - sqlSubmitQueue.put('''SELECT toaddress, fromaddress, subject, message, ackdata FROM sent WHERE status=? AND toripe=? and folder='sent' ''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlSubmitQueue.put('''SELECT toaddress, fromaddress, subject, message, ackdata FROM sent WHERE status=? AND toripe=? and folder='sent' ''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: toaddress, fromaddress, subject, message, ackdata = row ackdataForWhichImWatching[ackdata] = 0 toStatus,toAddressVersionNumber,toStreamNumber,toHash = decodeAddress(toaddress) fromStatus,fromAddressVersionNumber,fromStreamNumber,fromHash = decodeAddress(fromaddress) #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send the message.') - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send the message.'))) - printLock.acquire() + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send the message.'))) + shared.printLock.acquire() print 'Found a message in our database that needs to be sent with this pubkey.' print 'First 150 characters of message:', message[:150] - printLock.release() + shared.printLock.release() 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. if fromAddressVersionNumber == 2: payload = '\x01' #Message version. @@ -3074,15 +2931,15 @@ class singleWorker(threading.Thread): #We need to convert our private keys to public keys in order to include them. try: - privSigningKeyBase58 = config.get(fromaddress, 'privsigningkey') - privEncryptionKeyBase58 = config.get(fromaddress, 'privencryptionkey') + privSigningKeyBase58 = shared.config.get(fromaddress, 'privsigningkey') + privEncryptionKeyBase58 = shared.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.') - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) continue - privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') + privSigningKeyHex = shared.decodeWalletImportFormat(privSigningKeyBase58).encode('hex') + privEncryptionKeyHex = shared.decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode('hex') pubEncryptionKey = highlevelcrypto.privToPub(privEncryptionKeyHex).decode('hex') @@ -3110,15 +2967,15 @@ class singleWorker(threading.Thread): #We need to convert our private keys to public keys in order to include them. try: - privSigningKeyBase58 = config.get(fromaddress, 'privsigningkey') - privEncryptionKeyBase58 = config.get(fromaddress, 'privencryptionkey') + privSigningKeyBase58 = shared.config.get(fromaddress, 'privsigningkey') + privEncryptionKeyBase58 = shared.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.') - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Error! Could not find sender address (your address) in the keys.dat file.'))) continue - privSigningKeyHex = decodeWalletImportFormat(privSigningKeyBase58).encode('hex') - privEncryptionKeyHex = decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') + privSigningKeyHex = shared.decodeWalletImportFormat(privSigningKeyBase58).encode('hex') + privEncryptionKeyHex = shared.decodeWalletImportFormat(privEncryptionKeyBase58).encode('hex') pubSigningKey = highlevelcrypto.privToPub(privSigningKeyHex).decode('hex') pubEncryptionKey = highlevelcrypto.privToPub(privEncryptionKeyHex).decode('hex') @@ -3126,12 +2983,12 @@ class singleWorker(threading.Thread): 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. payload += pubEncryptionKey[1:] #If the receiver of our message is in our address book, subscriptions list, or whitelist then we will allow them to do the network-minimum proof of work. Let us check to see if the receiver is in any of those lists. - if isAddressInMyAddressBookSubscriptionsListOrWhitelist(toaddress): + if shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(toaddress): payload += encodeVarint(networkDefaultProofOfWorkNonceTrialsPerByte) payload += encodeVarint(networkDefaultPayloadLengthExtraBytes) else: - payload += encodeVarint(config.getint(fromaddress,'noncetrialsperbyte')) - payload += encodeVarint(config.getint(fromaddress,'payloadlengthextrabytes')) + payload += encodeVarint(shared.config.getint(fromaddress,'noncetrialsperbyte')) + payload += encodeVarint(shared.config.getint(fromaddress,'payloadlengthextrabytes')) 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. @@ -3149,15 +3006,15 @@ class singleWorker(threading.Thread): #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. if toAddressVersionNumber == 2 or toAddressVersionNumber == 3: - sqlLock.acquire() - sqlSubmitQueue.put('SELECT transmitdata FROM pubkeys WHERE hash=?') - sqlSubmitQueue.put((toRipe,)) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('SELECT transmitdata FROM pubkeys WHERE hash=?') + shared.sqlSubmitQueue.put((toRipe,)) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn == []: - printLock.acquire() + shared.printLock.acquire() sys.stderr.write('(within sendMsg) The needed pubkey was not found. This should never happen. Aborting send.\n') - printLock.release() + shared.printLock.release() return for row in queryreturn: pubkeyPayload, = row @@ -3193,9 +3050,9 @@ class singleWorker(threading.Thread): #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 + encodeVarint(toStreamNumber) + encrypted target = 2**64 / ((len(payload)+requiredPayloadLengthExtraBytes+8) * requiredAverageProofOfWorkNonceTrialsPerByte) - printLock.acquire() + shared.printLock.acquire() print '(For msg message) Doing proof of work. Total required difficulty:', float(requiredAverageProofOfWorkNonceTrialsPerByte)/networkDefaultProofOfWorkNonceTrialsPerByte,'Required small message difficulty:', float(requiredPayloadLengthExtraBytes)/networkDefaultPayloadLengthExtraBytes - printLock.release() + shared.printLock.release() powStartTime = time.time() initialHash = hashlib.sha512(payload).digest() while trialValue > target: @@ -3210,25 +3067,25 @@ class singleWorker(threading.Thread): 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 ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Message sent. Waiting on acknowledgement. Sent on ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) + shared.inventory[inventoryHash] = (objectType, toStreamNumber, payload, int(time.time())) + #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Message sent. Waiting on acknowledgement. Sent on ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Message sent. Waiting on acknowledgement. Sent on ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) print 'sending inv (within sendmsg function)' - broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) + shared.broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) #Update the status of the message in the 'sent' table to have a 'sent' status - sqlLock.acquire() + shared.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() + shared.sqlSubmitQueue.put('UPDATE sent SET status=? WHERE toaddress=? AND fromaddress=? AND subject=? AND message=? AND status=?') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() t = (toRipe,) - sqlSubmitQueue.put('''UPDATE pubkeys SET usedpersonally='yes' WHERE hash=?''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('''UPDATE pubkeys SET usedpersonally='yes' WHERE hash=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() def requestPubKey(self,addressVersionNumber,streamNumber,ripe): @@ -3236,59 +3093,59 @@ class singleWorker(threading.Thread): payload += encodeVarint(addressVersionNumber) payload += encodeVarint(streamNumber) payload += ripe - printLock.acquire() + shared.printLock.acquire() print 'making request for pubkey with ripe:', ripe.encode('hex') - printLock.release() + shared.printLock.release() nonce = 0 trialValue = 99999999999999999999 #print 'trial value', trialValue statusbar = 'Doing the computations necessary to request the recipient\'s public key.' #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) - UISignalQueue.put(('updateStatusBar',statusbar)) + shared.UISignalQueue.put(('updateStatusBar',statusbar)) #self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Doing work necessary to request public key.') - UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Doing work necessary to request public key.'))) + shared.UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Doing work necessary to request public key.'))) print 'Doing proof-of-work necessary to send getpubkey message.' target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) 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]) - printLock.acquire() + shared.printLock.acquire() print 'Found proof of work', trialValue, 'Nonce:', nonce - printLock.release() + shared.printLock.release() payload = pack('>Q',nonce) + payload inventoryHash = calculateInventoryHash(payload) objectType = 'getpubkey' - inventory[inventoryHash] = (objectType, streamNumber, payload, int(time.time())) + shared.inventory[inventoryHash] = (objectType, streamNumber, payload, int(time.time())) print 'sending inv (for the getpubkey message)' - broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) + shared.broadcastToSendDataQueues((streamNumber, 'sendinv', inventoryHash)) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Broacasting the public key request. This program will auto-retry if they are offline.') - UISignalQueue.put(('updateStatusBar','Broacasting the public key request. This program will auto-retry if they are offline.')) - #self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) + shared.UISignalQueue.put(('updateStatusBar','Broacasting the public key request. This program will auto-retry if they are offline.')) + #self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + shared.UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) def generateFullAckMessage(self,ackdata,toStreamNumber,embeddedTime): nonce = 0 trialValue = 99999999999999999999 payload = embeddedTime + encodeVarint(toStreamNumber) + ackdata target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) - printLock.acquire() + shared.printLock.acquire() print '(For ack message) Doing proof of work...' - printLock.release() + shared.printLock.release() powStartTime = time.time() 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]) - printLock.acquire() + shared.printLock.acquire() print '(For ack message) Found proof of work', trialValue, 'Nonce:', nonce try: print 'POW took', int(time.time()-powStartTime), 'seconds.', nonce/(time.time()-powStartTime), 'nonce trials per second.' except: pass - printLock.release() + shared.printLock.release() payload = pack('>Q',nonce) + payload headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' @@ -3303,12 +3160,12 @@ class addressGenerator(threading.Thread): def run(self): while True: - addressVersionNumber,streamNumber,label,numberOfAddressesToMake,deterministicPassphrase,eighteenByteRipe, = addressGeneratorQueue.get() + addressVersionNumber,streamNumber,label,numberOfAddressesToMake,deterministicPassphrase,eighteenByteRipe, = shared.addressGeneratorQueue.get() if addressVersionNumber == 3: if deterministicPassphrase == "": #statusbar = 'Generating one new address' #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) - UISignalQueue.put(('updateStatusBar','Generating one new address')) + shared.UISignalQueue.put(('updateStatusBar','Generating one new address')) #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, #we won't store the \x00 or \x00\x00 bytes thus making the address shorter. @@ -3351,30 +3208,30 @@ class addressGenerator(threading.Thread): #print 'privEncryptionKeyWIF',privEncryptionKeyWIF config.add_section(address) - config.set(address,'label',label) - config.set(address,'enabled','true') - config.set(address,'decoy','false') - config.set(address,'noncetrialsperbyte',config.get('bitmessagesettings','defaultnoncetrialsperbyte')) - config.set(address,'payloadlengthextrabytes',config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) - config.set(address,'privSigningKey',privSigningKeyWIF) - config.set(address,'privEncryptionKey',privEncryptionKeyWIF) - with open(appdata + 'keys.dat', 'wb') as configfile: + shared.config.set(address,'label',label) + shared.config.set(address,'enabled','true') + shared.config.set(address,'decoy','false') + shared.config.set(address,'noncetrialsperbyte',shared.config.get('bitmessagesettings','defaultnoncetrialsperbyte')) + shared.config.set(address,'payloadlengthextrabytes',shared.config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) + shared.config.set(address,'privSigningKey',privSigningKeyWIF) + shared.config.set(address,'privEncryptionKey',privEncryptionKeyWIF) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) #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) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address. Doing work necessary to broadcast it...') - UISignalQueue.put(('updateStatusBar','Done generating address. Doing work necessary to broadcast it...')) + shared.UISignalQueue.put(('updateStatusBar','Done generating address. Doing work necessary to broadcast it...')) #self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.label,address,str(streamNumber)) - UISignalQueue.put(('writeNewAddressToTable',(label,address,streamNumber))) - reloadMyAddressHashes() - workerQueue.put(('doPOWForMyV3Pubkey',ripe.digest())) + shared.UISignalQueue.put(('writeNewAddressToTable',(label,address,streamNumber))) + shared.reloadMyAddressHashes() + shared.workerQueue.put(('doPOWForMyV3Pubkey',ripe.digest())) else: #There is something in the deterministicPassphrase variable thus we are going to do this deterministically. statusbar = 'Generating '+str(numberOfAddressesToMake) + ' new addresses.' #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) - UISignalQueue.put(('updateStatusBar',statusbar)) + shared.UISignalQueue.put(('updateStatusBar',statusbar)) signingKeyNonce = 0 encryptionKeyNonce = 1 listOfNewAddressesToSendOutThroughTheAPI = [] #We fill out this list no matter what although we only need it if we end up passing the info to the API. @@ -3425,28 +3282,28 @@ class addressGenerator(threading.Thread): try: config.add_section(address) print 'label:', label - config.set(address,'label',label) - config.set(address,'enabled','true') - config.set(address,'decoy','false') - config.set(address,'noncetrialsperbyte',config.get('bitmessagesettings','defaultnoncetrialsperbyte')) - config.set(address,'payloadlengthextrabytes',config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) - config.set(address,'privSigningKey',privSigningKeyWIF) - config.set(address,'privEncryptionKey',privEncryptionKeyWIF) - with open(appdata + 'keys.dat', 'wb') as configfile: + shared.config.set(address,'label',label) + shared.config.set(address,'enabled','true') + shared.config.set(address,'decoy','false') + shared.config.set(address,'noncetrialsperbyte',shared.config.get('bitmessagesettings','defaultnoncetrialsperbyte')) + shared.config.set(address,'payloadlengthextrabytes',shared.config.get('bitmessagesettings','defaultpayloadlengthextrabytes')) + shared.config.set(address,'privSigningKey',privSigningKeyWIF) + shared.config.set(address,'privEncryptionKey',privEncryptionKeyWIF) + with open(shared.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)) - UISignalQueue.put(('writeNewAddressToTable',(label,address,str(streamNumber)))) + shared.UISignalQueue.put(('writeNewAddressToTable',(label,address,str(streamNumber)))) listOfNewAddressesToSendOutThroughTheAPI.append(address) if eighteenByteRipe: - reloadMyAddressHashes()#This is necessary here (rather than just at the end) because otherwise if the human generates a large number of new addresses and uses one before they are done generating, the program will receive a getpubkey message and will ignore it. + shared.reloadMyAddressHashes()#This is necessary here (rather than just at the end) because otherwise if the human generates a large number of new addresses and uses one before they are done generating, the program will receive a getpubkey message and will ignore it. except: print address,'already exists. Not adding it again.' #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) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),'Done generating address') - UISignalQueue.put(('updateStatusBar','Done generating address')) - reloadMyAddressHashes() + shared.UISignalQueue.put(('updateStatusBar','Done generating address')) + shared.reloadMyAddressHashes() #This is one of several classes that constitute the API @@ -3517,7 +3374,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): # handle Basic authentication (enctype, encstr) = self.headers.get('Authorization').split() (emailid, password) = encstr.decode('base64').split(':') - if emailid == config.get('bitmessagesettings', 'apiusername') and password == config.get('bitmessagesettings', 'apipassword'): + if emailid == shared.config.get('bitmessagesettings', 'apiusername') and password == shared.config.get('bitmessagesettings', 'apipassword'): return True else: return False @@ -3545,7 +3402,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): elif method == 'statusBar': message, = params #apiSignalQueue.put(('updateStatusBar',message)) - UISignalQueue.put(('updateStatusBar',message)) + shared.UISignalQueue.put(('updateStatusBar',message)) elif method == 'listAddresses': data = '{"addresses":[' configSections = config.sections() @@ -3555,7 +3412,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): 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 += json.dumps({'label':shared.config.get(addressInKeysFile,'label'),'address':addressInKeysFile,'stream':streamNumber,'enabled':shared.config.getboolean(addressInKeysFile,'enabled')},indent=4, separators=(',', ': ')) data += ']}' return data elif method == 'createRandomAddress': @@ -3570,7 +3427,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): apiAddressGeneratorReturnQueue.queue.clear() streamNumberForAddress = 1 #apiSignalQueue.put(('createRandomAddress',(label, eighteenByteRipe))) #params should be a twopul which equals (eighteenByteRipe, label) - addressGeneratorQueue.put((3,streamNumberForAddress,label,1,"",eighteenByteRipe)) + shared.addressGeneratorQueue.put((3,streamNumberForAddress,label,1,"",eighteenByteRipe)) return apiAddressGeneratorReturnQueue.get() elif method == 'createDeterministicAddresses': if len(params) == 0: @@ -3613,7 +3470,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): apiAddressGeneratorReturnQueue.queue.clear() print 'about to send numberOfAddresses', numberOfAddresses #apiSignalQueue.put(('createDeterministicAddresses',(passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe))) - addressGeneratorQueue.put((addressVersionNumber,streamNumber,'unused API address',numberOfAddresses,passphrase,eighteenByteRipe)) + shared.addressGeneratorQueue.put((addressVersionNumber,streamNumber,'unused API address',numberOfAddresses,passphrase,eighteenByteRipe)) data = '{"addresses":[' queueReturn = apiAddressGeneratorReturnQueue.get() for item in queueReturn: @@ -3623,11 +3480,11 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): 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() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, received, message FROM inbox where folder='inbox' ORDER BY received''') + shared.sqlSubmitQueue.put('') + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() data = '{"inboxMessages":[' for row in queryreturn: msgid, toAddress, fromAddress, subject, received, message, = row @@ -3641,14 +3498,14 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'API Error 0000: I need parameters!' msgid = params[0].decode('hex') t = (msgid,) - sqlLock.acquire() - sqlSubmitQueue.put('''UPDATE inbox SET folder='trash' WHERE msgid=?''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''UPDATE inbox SET folder='trash' WHERE msgid=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() #apiSignalQueue.put(('updateStatusBar','Per API: Trashed message (assuming message existed). UI not updated.')) - UISignalQueue.put(('updateStatusBar','Per API: Trashed message (assuming message existed). UI not updated.')) + shared.UISignalQueue.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': if len(params) == 0: @@ -3664,9 +3521,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): message = message.decode('base64') status,addressVersionNumber,streamNumber,toRipe = decodeAddress(toAddress) if status <> 'success': - printLock.acquire() + shared.printLock.acquire() print 'API Error 0007: Could not decode address:', toAddress, ':', status - printLock.release() + shared.printLock.release() if status == 'checksumfailed': return 'API Error 0008: Checksum failed for address: ' + toAddress if status == 'invalidcharacters': @@ -3679,9 +3536,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'API Error 0012: the stream number must be 1. Others aren\'t supported. Check the toAddress.' status,addressVersionNumber,streamNumber,fromRipe = decodeAddress(fromAddress) if status <> 'success': - printLock.acquire() + shared.printLock.acquire() print 'API Error 0007: Could not decode address:', fromAddress, ':', status - printLock.release() + shared.printLock.release() if status == 'checksumfailed': return 'API Error 0008: Checksum failed for address: ' + fromAddress if status == 'invalidcharacters': @@ -3695,34 +3552,34 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): toAddress = addBMIfNotPresent(toAddress) fromAddress = addBMIfNotPresent(fromAddress) try: - fromAddressEnabled = config.getboolean(fromAddress,'enabled') + fromAddressEnabled = shared.config.getboolean(fromAddress,'enabled') except: return 'API Error 0013: could not find your fromAddress in the keys.dat file.' if not fromAddressEnabled: return 'API Error 0014: your fromAddress is disabled. Cannot send.' ackdata = OpenSSL.rand(32) - sqlLock.acquire() + shared.sqlLock.acquire() t = ('',toAddress,toRipe,fromAddress,subject,message,ackdata,int(time.time()),'findingpubkey',1,1,'sent',2) - sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() toLabel = '' t = (toAddress,) - sqlLock.acquire() - sqlSubmitQueue.put('''select label from addressbook where address=?''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn <> []: for row in queryreturn: toLabel, = row apiSignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) - workerQueue.put(('sendmessage',toAddress)) + shared.workerQueue.put(('sendmessage',toAddress)) return ackdata.encode('hex') @@ -3741,9 +3598,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): status,addressVersionNumber,streamNumber,fromRipe = decodeAddress(fromAddress) if status <> 'success': - printLock.acquire() + shared.printLock.acquire() print 'API Error 0007: Could not decode address:', fromAddress, ':', status - printLock.release() + shared.printLock.release() if status == 'checksumfailed': return 'API Error 0008: Checksum failed for address: ' + fromAddress if status == 'invalidcharacters': @@ -3756,26 +3613,26 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'API Error 0012: the stream number must be 1. Others aren\'t supported. Check the fromAddress.' fromAddress = addBMIfNotPresent(fromAddress) try: - fromAddressEnabled = config.getboolean(fromAddress,'enabled') + fromAddressEnabled = shared.config.getboolean(fromAddress,'enabled') except: return 'API Error 0013: could not find your fromAddress in the keys.dat file.' ackdata = OpenSSL.rand(32) toAddress = '[Broadcast subscribers]' ripe = '' - sqlLock.acquire() + shared.sqlLock.acquire() t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'broadcastpending',1,1,'sent',2) - sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() + shared.sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() toLabel = '[Broadcast subscribers]' #apiSignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) #self.emit(SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),toAddress,toLabel,fromAddress,subject,message,ackdata) - UISignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) - workerQueue.put(('sendbroadcast',(fromAddress,subject,message))) + shared.UISignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) + shared.workerQueue.put(('sendbroadcast',(fromAddress,subject,message))) return ackdata.encode('hex') @@ -3788,1926 +3645,19 @@ class singleAPI(threading.Thread): threading.Thread.__init__(self) def run(self): - se = SimpleXMLRPCServer((config.get('bitmessagesettings', 'apiinterface'),config.getint('bitmessagesettings', 'apiport')), MySimpleXMLRPCRequestHandler, True, True) + se = SimpleXMLRPCServer((shared.config.get('bitmessagesettings', 'apiinterface'),shared.config.getint('bitmessagesettings', 'apiport')), MySimpleXMLRPCRequestHandler, True, True) se.register_introspection_functions() se.serve_forever() -class UISignaler(QThread): - def __init__(self, parent = None): - QThread.__init__(self, parent) - def run(self): - while True: - command, data = UISignalQueue.get() - if not safeConfigGetBoolean('bitmessagesettings','daemon'): - if command == 'writeNewAddressToTable': - label, address, streamNumber = data - self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),label,address,str(streamNumber)) - elif command == 'updateStatusBar': - self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),data) - elif command == 'updateSentItemStatusByHash': - hash, message = data - self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),hash,message) - elif command == 'updateSentItemStatusByAckdata': - ackData, message = data - self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackData,message) - elif command == 'displayNewInboxMessage': - inventoryHash,toAddress,fromAddress,subject,body = data - self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),inventoryHash,toAddress,fromAddress,subject,body) - elif command == 'displayNewSentMessage': - toAddress,fromLabel,fromAddress,subject,message,ackdata = data - self.emit(SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),toAddress,fromLabel,fromAddress,subject,message,ackdata) - elif command == 'updateNetworkStatusTab': - streamNumber,count = data - self.emit(SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"),streamNumber,count) - elif command == 'incrementNumberOfMessagesProcessed': - self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) - elif command == 'incrementNumberOfPubkeysProcessed': - self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()")) - elif command == 'incrementNumberOfBroadcastsProcessed': - self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()")) - elif command == 'setStatusIcon': - self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),data) - else: - sys.stderr.write('Command sent to UISignaler not recognized: %s\n' % command) -#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()) -##cut from here - -class MyForm(QtGui.QMainWindow): - def __init__(self, parent=None): - QtGui.QWidget.__init__(self, parent) - self.ui = Ui_MainWindow() - self.ui.setupUi(self) - - #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 - if 'win32' in sys.platform or 'win64' in sys.platform: - #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) - self.trayIcon.setIcon( QtGui.QIcon(':/newPrefix/images/can-icon-16px.png') ) - 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) - #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() - - self.ui.labelSendBroadcastWarning.setVisible(False) - - #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) - QtCore.QObject.connect(self.ui.actionRegenerateDeterministicAddresses, QtCore.SIGNAL("triggered()"), self.click_actionRegenerateDeterministicAddresses) - 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 - 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) - self.actionTrashInboxMessage = self.ui.inboxContextMenuToolbar.addAction("Move to Trash", self.on_action_InboxTrash) - self.actionForceHtml = self.ui.inboxContextMenuToolbar.addAction("View HTML code as formatted text", self.on_action_InboxMessageForceHtml) - 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.actionForceHtml ) - self.popMenuInbox.addSeparator() - 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 - 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) - self.actionSpecialAddressBehavior = self.ui.addressContextMenuToolbar.addAction("Special address behavior...", self.on_action_SpecialAddressBehaviorDialog) - 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 ) - self.popMenu.addAction( self.actionSpecialAddressBehavior ) - - #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) - 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) - 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 ) - self.popMenuAddressBook.addSeparator() - self.popMenuAddressBook.addAction( self.actionAddressBookNew ) - self.popMenuAddressBook.addAction( self.actionAddressBookDelete ) - - #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.actionsubscriptionsEnable = self.ui.subscriptionsContextMenuToolbar.addAction("Enable", self.on_action_SubscriptionsEnable) - self.actionsubscriptionsDisable = self.ui.subscriptionsContextMenuToolbar.addAction("Disable", self.on_action_SubscriptionsDisable) - 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.actionsubscriptionsEnable ) - self.popMenuSubscriptions.addAction( self.actionsubscriptionsDisable ) - self.popMenuSubscriptions.addSeparator() - self.popMenuSubscriptions.addAction( self.actionsubscriptionsClipboard ) - - #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) - self.actionSentClipboard = self.ui.sentContextMenuToolbar.addAction("Copy destination address to clipboard", self.on_action_SentClipboard) - 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 ) - self.popMenuSent.addAction( self.actionSentClipboard ) - self.popMenuSent.addAction( self.actionTrashSentMessage ) - - - #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 ) - - #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)) - if safeConfigGetBoolean(addressInKeysFile,'mailinglist'): - newItem.setTextColor(QtGui.QColor(137,04,177))#magenta - 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) - - #self.sqlLookup = sqlThread() - #self.sqlLookup.start() - - reloadMyAddressHashes() - self.reloadBroadcastSendersForWhichImWatching() - - self.ui.tableWidgetSent.keyPressEvent = self.tableWidgetSentKeyPressEvent - font = QFont() - font.setBold(True) - #Load inbox from messages database file - sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, received, message, read FROM inbox where folder='inbox' ORDER BY received''') - sqlSubmitQueue.put('') - queryreturn = sqlReturnQueue.get() - for row in queryreturn: - msgid, toAddress, fromAddress, subject, received, message, read = 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 - - if fromLabel == '': #If this address wasn't in our address book.. - t = (fromAddress,) - sqlSubmitQueue.put('''select label from subscriptions 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')) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - if not read: - newItem.setFont(font) - newItem.setData(Qt.UserRole,str(toAddress)) - if safeConfigGetBoolean(toAddress,'mailinglist'): - newItem.setTextColor(QtGui.QColor(137,04,177)) - self.ui.tableWidgetInbox.setItem(0,0,newItem) - if fromLabel == '': - newItem = QtGui.QTableWidgetItem(unicode(fromAddress,'utf-8')) - else: - newItem = QtGui.QTableWidgetItem(unicode(fromLabel,'utf-8')) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - if not read: - newItem.setFont(font) - 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)')) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - if not read: - newItem.setFont(font) - self.ui.tableWidgetInbox.setItem(0,2,newItem) - newItem = myTableWidgetItem(unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(received))),'utf-8')) - newItem.setData(Qt.UserRole,QByteArray(msgid)) - newItem.setData(33,int(received)) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - if not read: - newItem.setFont(font) - self.ui.tableWidgetInbox.setItem(0,3,newItem) - self.ui.tableWidgetInbox.sortItems(3,Qt.DescendingOrder) - - self.ui.tableWidgetInbox.keyPressEvent = self.tableWidgetInboxKeyPressEvent - #Load Sent items from database - sqlSubmitQueue.put('''SELECT toaddress, fromaddress, subject, message, status, ackdata, lastactiontime FROM sent where folder = 'sent' ORDER BY lastactiontime''') - 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)) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - 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)) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - self.ui.tableWidgetSent.setItem(0,1,newItem) - newItem = QtGui.QTableWidgetItem(unicode(subject,'utf-8')) - newItem.setData(Qt.UserRole,unicode(message,'utf-8)')) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - self.ui.tableWidgetSent.setItem(0,2,newItem) - if status == 'findingpubkey': - newItem = myTableWidgetItem('Waiting on their public key. Will request it again soon.') - elif status == 'sentmessage': - newItem = myTableWidgetItem('Message sent. Waiting on acknowledgement. Sent at ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(lastactiontime)),'utf-8')) - elif status == 'doingpow': - newItem = myTableWidgetItem('Need to do work to send message. Work is queued.') - elif status == 'ackreceived': - newItem = myTableWidgetItem('Acknowledgement of the message received ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(lastactiontime))),'utf-8')) - elif status == 'broadcastpending': - newItem = myTableWidgetItem('Doing the work necessary to send broadcast...') - elif status == 'broadcastsent': - newItem = myTableWidgetItem('Broadcast on ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(lastactiontime))),'utf-8')) - else: - newItem = myTableWidgetItem('Unknown status. ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(lastactiontime))),'utf-8')) - newItem.setData(Qt.UserRole,QByteArray(ackdata)) - newItem.setData(33,int(lastactiontime)) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - self.ui.tableWidgetSent.setItem(0,3,newItem) - self.ui.tableWidgetSent.sortItems(3,Qt.DescendingOrder) - - #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, enabled FROM subscriptions') - sqlSubmitQueue.put('') - queryreturn = sqlReturnQueue.get() - for row in queryreturn: - label, address, enabled = row - self.ui.tableWidgetSubscriptions.insertRow(0) - newItem = QtGui.QTableWidgetItem(unicode(label,'utf-8')) - if not enabled: - newItem.setTextColor(QtGui.QColor(128,128,128)) - self.ui.tableWidgetSubscriptions.setItem(0,0,newItem) - newItem = QtGui.QTableWidgetItem(address) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - if not enabled: - newItem.setTextColor(QtGui.QColor(128,128,128)) - 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 - print 'Watching for ackdata', ackdata.encode('hex') - 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) - QtCore.QObject.connect(self.ui.tableWidgetInbox, QtCore.SIGNAL("itemSelectionChanged ()"), self.tableWidgetInboxItemClicked) - QtCore.QObject.connect(self.ui.tableWidgetSent, QtCore.SIGNAL("itemSelectionChanged ()"), self.tableWidgetSentItemClicked) - - #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 ' + unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - 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() - - self.UISignalThread = UISignaler() - self.UISignalThread.start() - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.writeNewAddressToTable) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByHash) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByAckdata) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.displayNewInboxMessage) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.displayNewSentMessage) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"), self.updateNetworkStatusTab) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfMessagesProcessed()"), self.incrementNumberOfMessagesProcessed) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfPubkeysProcessed()"), self.incrementNumberOfPubkeysProcessed) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfBroadcastsProcessed()"), self.incrementNumberOfBroadcastsProcessed) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("setStatusIcon(PyQt_PyObject)"), self.setStatusIcon) - - - #self.connectToStream(1) - - #self.singleListenerThread = singleListener() - #self.singleListenerThread.start() - #QtCore.QObject.connect(self.singleListenerThread, QtCore.SIGNAL("passObjectThrough(PyQt_PyObject)"), self.connectObjectToSignals) - - - #self.singleCleanerThread = singleCleaner() - #self.singleCleanerThread.start() - #QtCore.QObject.connect(self.singleCleanerThread, QtCore.SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByHash) - #QtCore.QObject.connect(self.singleCleanerThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) - - #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) - #QtCore.QObject.connect(self.workerThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) - - def tableWidgetInboxKeyPressEvent(self,event): - if event.key() == QtCore.Qt.Key_Delete: - self.on_action_InboxTrash() - return QtGui.QTableWidget.keyPressEvent(self.ui.tableWidgetInbox, event) - - def tableWidgetSentKeyPressEvent(self,event): - if event.key() == QtCore.Qt.Key_Delete: - self.on_action_SentTrash() - return QtGui.QTableWidget.keyPressEvent(self.ui.tableWidgetSent, event) - - def click_actionManageKeys(self): - if 'darwin' in sys.platform or 'linux' in sys.platform: - 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 + '\nIt is important that you back up this file.', QMessageBox.Ok) - elif sys.platform == 'win32' or sys.platform == 'win64': - 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 + '\nIt is important that you back up this file. Would you like to open the file now? (Be sure to close Bitmessage before making any changes.)', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) - if reply == QtGui.QMessageBox.Yes: - self.openKeysFile() - - 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() - addressGeneratorQueue.put((addressVersionNumber,streamNumberForAddress,"regenerated deterministic address",self.regenerateAddressesDialogInstance.ui.spinBoxNumberOfAddressesToMake.value(),self.regenerateAddressesDialogInstance.ui.lineEditPassphrase.text().toUtf8(),self.regenerateAddressesDialogInstance.ui.checkBoxEighteenByteRipe.isChecked())) - self.ui.tabWidget.setCurrentIndex(3) - - def openKeysFile(self): - if 'linux' in sys.platform: - subprocess.call(["xdg-open", appdata + 'keys.dat']) - else: - os.startfile(appdata + 'keys.dat') - - def changeEvent(self, event): - if config.getboolean('bitmessagesettings', 'minimizetotray') and not 'darwin' in sys.platform: - if event.type() == QtCore.QEvent.WindowStateChange: - if self.windowState() & QtCore.Qt.WindowMinimized: - self.hide() - self.trayIcon.show() - #self.hidden = True - if 'win32' in sys.platform or 'win64' in sys.platform: - 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: - 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() - self.setWindowState(self.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive) - self.activateWindow() - 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() - - 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.') - - def incrementNumberOfPubkeysProcessed(self): - self.numberOfPubkeysProcessed += 1 - self.ui.labelPubkeyCount.setText('Processed ' + str(self.numberOfPubkeysProcessed) + ' public keys.') - - def updateNetworkStatusTab(self,streamNumber,connectionCount): - global statusIconColor - #print 'updating network status tab' - totalNumberOfConnectionsFromAllStreams = 0 #One would think we could use len(sendDataQueues) for this but the number doesn't always match: just because we have a sendDataThread running doesn't mean that the connection has been fully established (with the exchange of version messages). - foundTheRowThatNeedsUpdating = False - for currentRow in range(self.ui.tableWidgetConnectionCount.rowCount()): - rowStreamNumber = int(self.ui.tableWidgetConnectionCount.item(currentRow,0).text()) - if streamNumber == rowStreamNumber: - foundTheRowThatNeedsUpdating = True - self.ui.tableWidgetConnectionCount.item(currentRow,1).setText(str(connectionCount)) - totalNumberOfConnectionsFromAllStreams += connectionCount - if foundTheRowThatNeedsUpdating == False: - #Add a line to the table for this stream number and update its count with the current connection count. - 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(str(connectionCount)) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - self.ui.tableWidgetConnectionCount.setItem(0,1,newItem) - 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 - #print 'setting status icon color' - if color == 'red': - self.ui.pushButtonStatusIcon.setIcon(QIcon(":/newPrefix/images/redicon.png")) - statusIconColor = 'red' - if color == 'yellow': - 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('') - self.ui.pushButtonStatusIcon.setIcon(QIcon(":/newPrefix/images/yellowicon.png")) - statusIconColor = 'yellow' - if color == 'green': - 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('') - 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')) - self.ui.tableWidgetSent.item(i,3).setText(textToDisplay) - - 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')) - self.ui.tableWidgetSent.item(i,3).setText(textToDisplay) - - 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')) - #Set the color according to whether it is the address of a mailing list or not. - if safeConfigGetBoolean(toAddress,'mailinglist'): - self.ui.tableWidgetInbox.item(i,0).setTextColor(QtGui.QColor(137,04,177)) - else: - self.ui.tableWidgetInbox.item(i,0).setTextColor(QtGui.QColor(0,0,0)) - - 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')) - - 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(';')] - 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. - for toAddress in toAddressesList: - if toAddress <> '': - status,addressVersionNumber,streamNumber,ripe = decodeAddress(toAddress) - if status <> 'success': - printLock.acquire() - print 'Error: Could not decode', toAddress, ':', status - printLock.release() - if status == 'missingbm': - self.statusBar().showMessage('Error: Bitmessage addresses start with BM- Please check ' + toAddress) - elif status == 'checksumfailed': - self.statusBar().showMessage('Error: The address ' + toAddress+' is not typed or copied correctly. Please check it.') - elif status == 'invalidcharacters': - self.statusBar().showMessage('Error: The address '+ toAddress+ ' contains invalid characters. Please check it.') - elif 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.') - elif status == 'ripetooshort': - self.statusBar().showMessage('Error: Some data encoded in the address '+ toAddress+ ' is too short. There might be something wrong with the software of your acquaintance.') - elif status == 'ripetoolong': - self.statusBar().showMessage('Error: Some data encoded in the address '+ toAddress+ ' is too long. There might be something wrong with the software of your acquaintance.') - else: - self.statusBar().showMessage('Error: Something is wrong with the address '+ toAddress+ '.') - elif fromAddress == '': - self.statusBar().showMessage('Error: You must specify a From address. If you don\'t have one, go to the \'Your Identities\' tab.') - else: - toAddress = addBMIfNotPresent(toAddress) - try: - config.get(toAddress, 'enabled') - #The toAddress is one owned by me. We cannot send messages to ourselves without significant changes to the codebase. - 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 - if addressVersionNumber > 3 or addressVersionNumber == 0: - 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.") - continue - if streamNumber > 1 or streamNumber == 0: - QMessageBox.about(self, "Stream number", "Concerning the address "+toAddress+", Bitmessage cannot handle stream numbers of "+str(streamNumber)+". Perhaps upgrade Bitmessage to the latest version.") - continue - self.statusBar().showMessage('') - 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.') - ackdata = OpenSSL.rand(32) - sqlLock.acquire() - t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'findingpubkey',1,1,'sent',2) - sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - 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 - - self.displayNewSentMessage(toAddress,toLabel,fromAddress, subject, message, ackdata) - workerQueue.put(('sendmessage',toAddress)) - - self.ui.comboBoxSendFrom.setCurrentIndex(0) - self.ui.labelFrom.setText('') - self.ui.lineEditTo.setText('') - self.ui.lineEditSubject.setText('') - self.ui.textEditMessage.setText('') - self.ui.tabWidget.setCurrentIndex(2) - self.ui.tableWidgetSent.setCurrentCell(0,0) - 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. - ackdata = OpenSSL.rand(32) - toAddress = '[Broadcast subscribers]' - ripe = '' - sqlLock.acquire() - t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'broadcastpending',1,1,'sent',2) - sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - 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...'+ unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - newItem = myTableWidgetItem('Work is queued.') - newItem.setData(Qt.UserRole,QByteArray(ackdata)) - newItem.setData(33,int(time.time())) - self.ui.tableWidgetSent.setItem(0,3,newItem) - - self.ui.textEditSentMessage.setPlainText(self.ui.tableWidgetSent.item(0,2).data(Qt.UserRole).toPyObject()) - - self.ui.comboBoxSendFrom.setCurrentIndex(0) - self.ui.labelFrom.setText('') - self.ui.lineEditTo.setText('') - self.ui.lineEditSubject.setText('') - self.ui.textEditMessage.setText('') - self.ui.tabWidget.setCurrentIndex(2) - self.ui.tableWidgetSent.setCurrentCell(0,0) - - - 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 one or more entries in your address book and select \'Send message to this address\'.') - - def redrawLabelFrom(self,index): - self.ui.labelFrom.setText(self.ui.comboBoxSendFrom.itemData(index).toPyObject()) - - 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) - 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) - - - - def connectObjectToSignals(self,object): - QtCore.QObject.connect(object, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) - 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) - 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) - - #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) - - #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.setSortingEnabled(False) - 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)) - 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...'+ unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - newItem = myTableWidgetItem('Work is queued. '+ unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - newItem.setData(Qt.UserRole,QByteArray(ackdata)) - newItem.setData(33,int(time.time())) - self.ui.tableWidgetSent.setItem(0,3,newItem) - self.ui.textEditSentMessage.setPlainText(self.ui.tableWidgetSent.item(0,2).data(Qt.UserRole).toPyObject()) - self.ui.tableWidgetSent.setSortingEnabled(True) - - def displayNewInboxMessage(self,inventoryHash,toAddress,fromAddress,subject,message): - '''print 'test signals displayNewInboxMessage' - 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 - - font = QFont() - font.setBold(True) - self.ui.tableWidgetInbox.setSortingEnabled(False) - newItem = QtGui.QTableWidgetItem(unicode(toLabel,'utf-8')) - newItem.setFont(font) - newItem.setData(Qt.UserRole,str(toAddress)) - if safeConfigGetBoolean(str(toAddress),'mailinglist'): - newItem.setTextColor(QtGui.QColor(137,04,177)) - 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)) - newItem.setFont(font) - self.ui.tableWidgetInbox.setItem(0,1,newItem) - newItem = QtGui.QTableWidgetItem(unicode(subject,'utf-8)')) - newItem.setData(Qt.UserRole,unicode(message,'utf-8)')) - newItem.setFont(font) - self.ui.tableWidgetInbox.setItem(0,2,newItem) - newItem = myTableWidgetItem(unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) - newItem.setData(Qt.UserRole,QByteArray(inventoryHash)) - newItem.setData(33,int(time.time())) - newItem.setFont(font) - self.ui.tableWidgetInbox.setItem(0,3,newItem) - - """#If we have received this message from either a broadcast address or from someone in our address book, display as HTML - if decodeAddress(fromAddress)[3] in broadcastSendersForWhichImWatching or isAddressInMyAddressBook(fromAddress): - self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(0,2).data(Qt.UserRole).toPyObject()) - else: - self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(0,2).data(Qt.UserRole).toPyObject())""" - self.ui.tableWidgetInbox.setSortingEnabled(True) - - 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() - t = (addBMIfNotPresent(str(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())),) - sqlSubmitQueue.put('''select * from addressbook where address=?''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() - if queryreturn == []: - self.ui.tableWidgetAddressBook.setSortingEnabled(False) - self.ui.tableWidgetAddressBook.insertRow(0) - newItem = QtGui.QTableWidgetItem(unicode(self.NewSubscriptionDialogInstance.ui.newsubscriptionlabel.text().toUtf8(),'utf-8')) - self.ui.tableWidgetAddressBook.setItem(0,0,newItem) - newItem = QtGui.QTableWidgetItem(addBMIfNotPresent(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - self.ui.tableWidgetAddressBook.setItem(0,1,newItem) - self.ui.tableWidgetAddressBook.setSortingEnabled(True) - t = (str(self.NewSubscriptionDialogInstance.ui.newsubscriptionlabel.text().toUtf8()),addBMIfNotPresent(str(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text()))) - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO addressbook VALUES (?,?)''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - self.rerenderInboxFromLabels() - self.rerenderSentToLabels() - 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() - t = (addBMIfNotPresent(str(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())),) - sqlSubmitQueue.put('''select * from subscriptions where address=?''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlLock.release() - if queryreturn == []: - self.ui.tableWidgetSubscriptions.setSortingEnabled(False) - self.ui.tableWidgetSubscriptions.insertRow(0) - newItem = QtGui.QTableWidgetItem(unicode(self.NewSubscriptionDialogInstance.ui.newsubscriptionlabel.text().toUtf8(),'utf-8')) - self.ui.tableWidgetSubscriptions.setItem(0,0,newItem) - newItem = QtGui.QTableWidgetItem(addBMIfNotPresent(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - self.ui.tableWidgetSubscriptions.setItem(0,1,newItem) - self.ui.tableWidgetSubscriptions.setSortingEnabled(True) - t = (str(self.NewSubscriptionDialogInstance.ui.newsubscriptionlabel.text().toUtf8()),addBMIfNotPresent(str(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())),True) - sqlLock.acquire() - sqlSubmitQueue.put('''INSERT INTO subscriptions VALUES (?,?,?)''') - sqlSubmitQueue.put(t) - queryreturn = sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - 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': - sqlSubmitQueue.put('''SELECT label, address, enabled FROM blacklist''') - else: - sqlSubmitQueue.put('''SELECT label, address, enabled FROM whitelist''') - sqlSubmitQueue.put('') - queryreturn = sqlReturnQueue.get() - for row in queryreturn: - label, address, enabled = row - self.ui.tableWidgetBlacklist.insertRow(0) - newItem = QtGui.QTableWidgetItem(unicode(label,'utf-8')) - if not enabled: - newItem.setTextColor(QtGui.QColor(128,128,128)) - self.ui.tableWidgetBlacklist.setItem(0,0,newItem) - newItem = QtGui.QTableWidgetItem(address) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - if not enabled: - newItem.setTextColor(QtGui.QColor(128,128,128)) - self.ui.tableWidgetBlacklist.setItem(0,1,newItem) - - def click_pushButtonStatusIcon(self): - print 'click_pushButtonStatusIcon' - self.iconGlossaryInstance = iconGlossaryDialog(self) - if self.iconGlossaryInstance.exec_(): - pass - - 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): - global statusIconColor - global appdata - 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())) - 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())) - if float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text()) >= 1: - config.set('bitmessagesettings', 'defaultnoncetrialsperbyte',str(int(float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text())*networkDefaultProofOfWorkNonceTrialsPerByte))) - if float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text()) >= 1: - config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes',str(int(float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text())*networkDefaultPayloadLengthExtraBytes))) - - with open(appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) - - if 'win32' in sys.platform or 'win64' in sys.platform: - #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 - - 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 - knownNodesLock.acquire() - output = open('knownnodes.dat', 'wb') - pickle.dump(knownNodes, output) - output.close() - knownNodesLock.release() - 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 - knownNodesLock.acquire() - output = open(appdata + 'knownnodes.dat', 'wb') - pickle.dump(knownNodes, output) - output.close() - knownNodesLock.release() - 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).") - - - 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() - t = (addBMIfNotPresent(str(self.NewBlacklistDialogInstance.ui.lineEditSubscriptionAddress.text())),) - 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.setSortingEnabled(False) - self.ui.tableWidgetBlacklist.insertRow(0) - newItem = QtGui.QTableWidgetItem(unicode(self.NewBlacklistDialogInstance.ui.newsubscriptionlabel.text().toUtf8(),'utf-8')) - self.ui.tableWidgetBlacklist.setItem(0,0,newItem) - newItem = QtGui.QTableWidgetItem(addBMIfNotPresent(self.NewBlacklistDialogInstance.ui.lineEditSubscriptionAddress.text())) - newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) - self.ui.tableWidgetBlacklist.setItem(0,1,newItem) - self.ui.tableWidgetBlacklist.setSortingEnabled(True) - t = (str(self.NewBlacklistDialogInstance.ui.newsubscriptionlabel.text().toUtf8()),addBMIfNotPresent(str(self.NewBlacklistDialogInstance.ui.lineEditSubscriptionAddress.text())),True) - 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() - sqlSubmitQueue.put('commit') - 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.') - - 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') - #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)) - else: - config.set(str(addressAtCurrentRow),'mailinglist','true') - config.set(str(addressAtCurrentRow),'mailinglistname',str(self.dialog.ui.lineEditMailingListName.text().toUtf8())) - self.ui.tableWidgetYourIdentities.item(currentRow,1).setTextColor(QtGui.QColor(137,04,177)) - with open(appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) - self.rerenderInboxToLabels() - - - def click_NewAddressDialog(self): - self.dialog = NewAddressDialog(self) - # For Modal dialogs - if self.dialog.exec_(): - #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(3,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() - addressGeneratorQueue.put((3,streamNumberForAddress,str(self.dialog.ui.newaddresslabel.text().toUtf8()),1,"",self.dialog.ui.checkBoxEighteenByteRipe.isChecked())) - else: - 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(3,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() - addressGeneratorQueue.put((3,streamNumberForAddress,"unused deterministic address",self.dialog.ui.spinBoxNumberOfAddressesToMake.value(),self.dialog.ui.lineEditPassphrase.text().toUtf8(),self.dialog.ui.checkBoxEighteenByteRipe.isChecked())) - else: - print 'new address dialog box rejected' - - 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()''' - doCleanShutdown() - self.trayIcon.hide() - self.statusBar().showMessage('All done. Closing user interface...') - event.accept() - print 'Done. (passed event.accept())' - os._exit(0) - - def on_action_InboxMessageForceHtml(self): - currentInboxRow = self.ui.tableWidgetInbox.currentRow() - lines = self.ui.tableWidgetInbox.item(currentInboxRow,2).data(Qt.UserRole).toPyObject().split('\n') - for i in xrange(len(lines)): - if lines[i].contains('Message ostensibly from '): - lines[i] = '

%s

' % (lines[i]) - elif lines[i] == '------------------------------------------------------': - lines[i] = '
' - content = '' - for i in xrange(len(lines)): - content += lines[i] - content = content.replace('\n\n', '

') - self.ui.textEditInboxMessage.setHtml(QtCore.QString(content)) - - def on_action_InboxReply(self): - 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()) - - 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) - self.ui.lineEditTo.setText(str(fromAddressAtCurrentInboxRow)) - self.ui.comboBoxSendFrom.setCurrentIndex(0) - #self.ui.comboBoxSendFrom.setEditText(str(self.ui.tableWidgetInbox.item(currentInboxRow,0).text)) - self.ui.textEditMessage.setText('\n\n------------------------------------------------------\n'+self.ui.tableWidgetInbox.item(currentInboxRow,2).data(Qt.UserRole).toPyObject()) - if self.ui.tableWidgetInbox.item(currentInboxRow,2).text()[0:3] == 'Re:': - self.ui.lineEditSubject.setText(self.ui.tableWidgetInbox.item(currentInboxRow,2).text()) - else: - self.ui.lineEditSubject.setText('Re: '+self.ui.tableWidgetInbox.item(currentInboxRow,2).text()) - self.ui.radioButtonSpecific.setChecked(True) - self.ui.tabWidget.setCurrentIndex(1) - - def on_action_InboxAddSenderToAddressBook(self): - 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() - sqlSubmitQueue.put('commit') - 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.') - - #Send item on the Inbox tab to trash - def on_action_InboxTrash(self): - while self.ui.tableWidgetInbox.selectedIndexes() != []: - currentRow = self.ui.tableWidgetInbox.selectedIndexes()[0].row() - inventoryHashToTrash = str(self.ui.tableWidgetInbox.item(currentRow,3).data(Qt.UserRole).toPyObject()) - t = (inventoryHashToTrash,) - sqlLock.acquire() - sqlSubmitQueue.put('''UPDATE inbox SET folder='trash' WHERE msgid=?''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlLock.release() - self.ui.textEditInboxMessage.setText("") - self.ui.tableWidgetInbox.removeRow(currentRow) - self.statusBar().showMessage('Moved items 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.') - sqlSubmitQueue.put('commit') - if currentRow == 0: - self.ui.tableWidgetInbox.selectRow(currentRow) - else: - self.ui.tableWidgetInbox.selectRow(currentRow-1) - - #Send item on the Sent tab to trash - def on_action_SentTrash(self): - while self.ui.tableWidgetSent.selectedIndexes() != []: - currentRow = self.ui.tableWidgetSent.selectedIndexes()[0].row() - 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() - self.ui.textEditSentMessage.setPlainText("") - self.ui.tableWidgetSent.removeRow(currentRow) - self.statusBar().showMessage('Moved items 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.') - sqlSubmitQueue.put('commit') - if currentRow == 0: - self.ui.tableWidgetSent.selectRow(currentRow) - else: - self.ui.tableWidgetSent.selectRow(currentRow-1) - - 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)) - - #Group of functions for the Address Book dialog box - def on_action_AddressBookNew(self): - self.click_pushButtonAddAddressBook() - def on_action_AddressBookDelete(self): - while self.ui.tableWidgetAddressBook.selectedIndexes() != []: - currentRow = self.ui.tableWidgetAddressBook.selectedIndexes()[0].row() - 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() - sqlSubmitQueue.put('commit') - sqlLock.release() - self.ui.tableWidgetAddressBook.removeRow(currentRow) - self.rerenderInboxFromLabels() - self.rerenderSentToLabels() - def on_action_AddressBookClipboard(self): - fullStringOfAddresses = '' - listOfSelectedRows = {} - for i in range(len(self.ui.tableWidgetAddressBook.selectedIndexes())): - listOfSelectedRows[self.ui.tableWidgetAddressBook.selectedIndexes()[i].row()] = 0 - for currentRow in listOfSelectedRows: - addressAtCurrentRow = self.ui.tableWidgetAddressBook.item(currentRow,1).text() - if fullStringOfAddresses == '': - fullStringOfAddresses = addressAtCurrentRow - else: - fullStringOfAddresses += ', '+ str(addressAtCurrentRow) - clipboard = QtGui.QApplication.clipboard() - clipboard.setText(fullStringOfAddresses) - def on_action_AddressBookSend(self): - listOfSelectedRows = {} - for i in range(len(self.ui.tableWidgetAddressBook.selectedIndexes())): - listOfSelectedRows[self.ui.tableWidgetAddressBook.selectedIndexes()[i].row()] = 0 - for currentRow in listOfSelectedRows: - 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)) - if listOfSelectedRows == {}: - self.statusBar().showMessage('No addresses selected.') - else: - self.statusBar().showMessage('') - self.ui.tabWidget.setCurrentIndex(1) - 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() - sqlSubmitQueue.put('commit') - sqlLock.release() - self.ui.tableWidgetSubscriptions.removeRow(currentRow) - self.rerenderInboxFromLabels() - self.reloadBroadcastSendersForWhichImWatching() - 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_action_SubscriptionsEnable(self): - 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('''update subscriptions set enabled=1 WHERE label=? AND address=?''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - self.ui.tableWidgetSubscriptions.item(currentRow,0).setTextColor(QtGui.QColor(0,0,0)) - self.ui.tableWidgetSubscriptions.item(currentRow,1).setTextColor(QtGui.QColor(0,0,0)) - self.reloadBroadcastSendersForWhichImWatching() - def on_action_SubscriptionsDisable(self): - 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('''update subscriptions set enabled=0 WHERE label=? AND address=?''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - self.ui.tableWidgetSubscriptions.item(currentRow,0).setTextColor(QtGui.QColor(128,128,128)) - self.ui.tableWidgetSubscriptions.item(currentRow,1).setTextColor(QtGui.QColor(128,128,128)) - self.reloadBroadcastSendersForWhichImWatching() - def on_context_menuSubscriptions(self, point): - self.popMenuSubscriptions.exec_( self.ui.tableWidgetSubscriptions.mapToGlobal(point) ) - - #Group of functions for the Blacklist dialog box - def on_action_BlacklistNew(self): - self.click_pushButtonAddBlacklist() - def on_action_BlacklistDelete(self): - 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() - sqlSubmitQueue.put('commit') - 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() - sqlSubmitQueue.put('commit') - 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() - sqlSubmitQueue.put('commit') - sqlLock.release() - - #Group of functions for the Your Identities dialog box - def on_action_YourIdentitiesNew(self): - self.click_NewAddressDialog() - def on_action_YourIdentitiesEnable(self): - currentRow = self.ui.tableWidgetYourIdentities.currentRow() - addressAtCurrentRow = str(self.ui.tableWidgetYourIdentities.item(currentRow,1).text()) - config.set(addressAtCurrentRow,'enabled','true') - 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)) - if safeConfigGetBoolean(addressAtCurrentRow,'mailinglist'): - self.ui.tableWidgetYourIdentities.item(currentRow,1).setTextColor(QtGui.QColor(137,04,177)) - reloadMyAddressHashes() - def on_action_YourIdentitiesDisable(self): - currentRow = self.ui.tableWidgetYourIdentities.currentRow() - addressAtCurrentRow = str(self.ui.tableWidgetYourIdentities.item(currentRow,1).text()) - 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)) - if safeConfigGetBoolean(addressAtCurrentRow,'mailinglist'): - self.ui.tableWidgetYourIdentities.item(currentRow,1).setTextColor(QtGui.QColor(137,04,177)) - with open(appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) - reloadMyAddressHashes() - def on_action_YourIdentitiesClipboard(self): - 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) ) - def on_context_menuSent(self, point): - self.popMenuSent.exec_( self.ui.tableWidgetSent.mapToGlobal(point) ) - - def tableWidgetInboxItemClicked(self): - currentRow = self.ui.tableWidgetInbox.currentRow() - if currentRow >= 0: - fromAddress = str(self.ui.tableWidgetInbox.item(currentRow,1).data(Qt.UserRole).toPyObject()) - #If we have received this message from either a broadcast address or from someone in our address book, display as HTML - if decodeAddress(fromAddress)[3] in broadcastSendersForWhichImWatching or isAddressInMyAddressBook(fromAddress): - if len(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) < 30000: - self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject())#Only show the first 30K characters - else: - self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()[:30000]+'\n\nDisplay of the remainder of the message truncated because it is too long.')#Only show the first 30K characters - else: - if len(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) < 30000: - self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject())#Only show the first 30K characters - else: - self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()[:30000]+'\n\nDisplay of the remainder of the message truncated because it is too long.')#Only show the first 30K characters - - font = QFont() - font.setBold(False) - self.ui.tableWidgetInbox.item(currentRow,0).setFont(font) - self.ui.tableWidgetInbox.item(currentRow,1).setFont(font) - self.ui.tableWidgetInbox.item(currentRow,2).setFont(font) - self.ui.tableWidgetInbox.item(currentRow,3).setFont(font) - - inventoryHash = str(self.ui.tableWidgetInbox.item(currentRow,3).data(Qt.UserRole).toPyObject()) - t = (inventoryHash,) - sqlLock.acquire() - sqlSubmitQueue.put('''update inbox set read=1 WHERE msgid=?''') - sqlSubmitQueue.put(t) - sqlReturnQueue.get() - sqlSubmitQueue.put('commit') - sqlLock.release() - - def tableWidgetSentItemClicked(self): - currentRow = self.ui.tableWidgetSent.currentRow() - if currentRow >= 0: - self.ui.textEditSentMessage.setPlainText(self.ui.tableWidgetSent.item(currentRow,2).data(Qt.UserRole).toPyObject()) - - def tableWidgetYourIdentitiesItemChanged(self): - currentRow = self.ui.tableWidgetYourIdentities.currentRow() - if currentRow >= 0: - 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) - self.rerenderComboBoxSendFrom() - #self.rerenderInboxFromLabels() - self.rerenderInboxToLabels() - self.rerenderSentFromLabels() - #self.rerenderSentToLabels() - - def tableWidgetAddressBookItemChanged(self): - currentRow = self.ui.tableWidgetAddressBook.currentRow() - sqlLock.acquire() - if currentRow >= 0: - 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() - sqlSubmitQueue.put('commit') - sqlLock.release() - self.rerenderInboxFromLabels() - self.rerenderSentToLabels() - - def tableWidgetSubscriptionsItemChanged(self): - currentRow = self.ui.tableWidgetSubscriptions.currentRow() - sqlLock.acquire() - if currentRow >= 0: - 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() - sqlSubmitQueue.put('commit') - sqlLock.release() - self.rerenderInboxFromLabels() - self.rerenderSentToLabels() - - def writeNewAddressToTable(self,label,address,streamNumber): - self.ui.tableWidgetYourIdentities.setSortingEnabled(False) - 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.ui.tableWidgetYourIdentities.setSortingEnabled(True) - self.rerenderComboBoxSendFrom() - - def updateStatusBar(self,data): - if data != "": - printLock.acquire() - print 'Status bar:', data - printLock.release() - self.statusBar().showMessage(data) - - def reloadBroadcastSendersForWhichImWatching(self): - broadcastSendersForWhichImWatching.clear() - MyECSubscriptionCryptorObjects.clear() - sqlLock.acquire() - sqlSubmitQueue.put('SELECT address FROM subscriptions where enabled=1') - sqlSubmitQueue.put('') - queryreturn = sqlReturnQueue.get() - sqlLock.release() - for row in queryreturn: - address, = row - status,addressVersionNumber,streamNumber,hash = decodeAddress(address) - if addressVersionNumber == 2: - broadcastSendersForWhichImWatching[hash] = 0 - #Now, for all addresses, even version 2 addresses, we should create Cryptor objects in a dictionary which we will use to attempt to decrypt encrypted broadcast messages. - privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32] - MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) - - - -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) - QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) - -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) - -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)) - -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')) - if appdata == '': - self.ui.checkBoxPortableMode.setChecked(True) - if 'darwin' in sys.platform: - self.ui.checkBoxStartOnLogon.setDisabled(True) - 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 aren\'t applicable or because they haven\'t yet been implimented for your operating system.') - elif 'linux' in sys.platform: - self.ui.checkBoxStartOnLogon.setDisabled(True) - self.ui.checkBoxMinimizeToTray.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.') - #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) - - 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) - - self.ui.lineEditTotalDifficulty.setText(str((float(config.getint('bitmessagesettings', 'defaultnoncetrialsperbyte'))/networkDefaultProofOfWorkNonceTrialsPerByte))) - self.ui.lineEditSmallMessageDifficulty.setText(str((float(config.getint('bitmessagesettings', 'defaultpayloadlengthextrabytes'))/networkDefaultPayloadLengthExtraBytes))) - QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) - - 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) - -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()) - if safeConfigGetBoolean(addressAtCurrentRow,'mailinglist'): - 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)) - -class NewSubscriptionDialog(QtGui.QDialog): - def __init__(self,parent): - QtGui.QWidget.__init__(self, parent) - self.ui = Ui_NewSubscriptionDialog() - self.ui.setupUi(self) - 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 == 'ripetooshort': - self.ui.labelSubscriptionAddressCheck.setText('Some data encoded in the address is too short.') - elif status == 'ripetoolong': - self.ui.labelSubscriptionAddressCheck.setText('Some data encoded in the address is too long.') - elif status == 'success': - self.ui.labelSubscriptionAddressCheck.setText('Address is valid.') - -class NewAddressDialog(QtGui.QDialog): - def __init__(self, parent): - QtGui.QWidget.__init__(self, parent) - self.ui = Ui_NewAddressDialog() - self.ui.setupUi(self) - self.parent = parent - row = 1 - #Let's fill out the 'existing address' combo box with addresses from the 'Your Identities' tab. - 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 - self.ui.groupBoxDeterministic.setHidden(True) - QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) - #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): @@ -5725,14 +3675,14 @@ class NewAddressDialog(QtGui.QDialog): #self.addressGenerator.setup(3,streamNumberForAddress,label,1,"",eighteenByteRipe) #self.emit(SIGNAL("passAddressGeneratorObjectThrough(PyQt_PyObject)"),self.addressGenerator) #self.addressGenerator.start() - addressGeneratorQueue.put((3,streamNumberForAddress,label,1,"",eighteenByteRipe)) + shared.addressGeneratorQueue.put((3,streamNumberForAddress,label,1,"",eighteenByteRipe)) 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() - addressGeneratorQueue.put((addressVersionNumber,streamNumber,'unused API address',numberOfAddresses,passphrase,eighteenByteRipe)) + shared.addressGeneratorQueue.put((addressVersionNumber,streamNumber,'unused API address',numberOfAddresses,passphrase,eighteenByteRipe)) 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)""" @@ -5742,33 +3692,22 @@ class NewAddressDialog(QtGui.QDialog): selfInitiatedConnections = {} #This is a list of current connections (the thread pointers at least) alreadyAttemptedConnectionsList = {} #This is a list of nodes to which we have already attempted a connection -sendDataQueues = [] #each sendData thread puts its queue in this list. -myECCryptorObjects = {} -MyECSubscriptionCryptorObjects = {} -myAddressesByHash = {} #The key in this dictionary is the RIPE hash which is encoded in an address and value is the address itself. -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() -knownNodesLock = threading.Lock() + + ackdataForWhichImWatching = {} -broadcastSendersForWhichImWatching = {} -statusIconColor = 'red' + connectionsCount = {} #Used for the 'network status' tab. connectionsCountLock = threading.Lock() alreadyAttemptedConnectionsListLock = threading.Lock() -inventoryLock = threading.Lock() #Guarantees that two receiveDataThreads don't receive and process the same message concurrently (probably sent by a malicious individual) + eightBytesOfRandomDataUsedToDetectConnectionsToSelf = pack('>Q',random.randrange(1, 18446744073709551615)) connectedHostsList = {} #List of hosts to which we are connected. Used to guarantee that the outgoingSynSender threads won't connect to the same remote node twice. neededPubkeys = {} successfullyDecryptMessageTimings = [] #A list of the amounts of time it took to successfully decrypt msg messages -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. +#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. alreadyAttemptedConnectionsListResetTime = int(time.time()) #used to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect. -UISignalQueue = Queue.Queue() -addressGeneratorQueue = Queue.Queue() + #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! networkDefaultProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. @@ -5793,132 +3732,123 @@ if __name__ == "__main__": config.get('bitmessagesettings', 'settingsversion') #settingsFileExistsInProgramDirectory = True print 'Loading config files from same directory as program' - appdata = '' + shared.appdata = '' except: #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) + shared.appdata = shared.lookupAppdataFolder() + #if not os.path.exists(shared.appdata): + # os.makedirs(shared.appdata) - config = ConfigParser.SafeConfigParser() - config.read(appdata + 'keys.dat') + shared.config = ConfigParser.SafeConfigParser() + shared.config.read(shared.appdata + 'keys.dat') try: - config.get('bitmessagesettings', 'settingsversion') - print 'Loading existing config files from', appdata + shared.config.get('bitmessagesettings', 'settingsversion') + print 'Loading existing config files from', shared.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','5') - 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') + shared.config.add_section('bitmessagesettings') + shared.config.set('bitmessagesettings','settingsversion','5') + shared.config.set('bitmessagesettings','port','8444') + shared.config.set('bitmessagesettings','timeformat','%%a, %%d %%b %%Y %%I:%%M %%p') + shared.config.set('bitmessagesettings','blackwhitelist','black') + shared.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. + shared.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') - 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') - config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) - config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) + shared.config.set('bitmessagesettings','minimizetotray','true') + shared.config.set('bitmessagesettings','showtraynotifications','true') + shared.config.set('bitmessagesettings','startintray','false') + shared.config.set('bitmessagesettings','socksproxytype','none') + shared.config.set('bitmessagesettings','sockshostname','localhost') + shared.config.set('bitmessagesettings','socksport','9050') + shared.config.set('bitmessagesettings','socksauthentication','false') + shared.config.set('bitmessagesettings','socksusername','') + shared.config.set('bitmessagesettings','sockspassword','') + shared.config.set('bitmessagesettings','keysencrypted','false') + shared.config.set('bitmessagesettings','messagesencrypted','false') + shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) + shared.config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) if storeConfigFilesInSameDirectoryAsProgramByDefault: #Just use the same directory as the program and forget about the appdata folder - appdata = '' + shared.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) + print 'Creating new config files in', shared.appdata + if not os.path.exists(shared.appdata): + os.makedirs(shared.appdata) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.config.write(configfile) + - if config.getint('bitmessagesettings','settingsversion') == 1: - config.set('bitmessagesettings','settingsversion','4') #If the settings version is equal to 2 or 3 then the sqlThread will modify the pubkeys table and change the settings version to 4. - 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: + if shared.config.getint('bitmessagesettings','settingsversion') == 1: + shared.config.set('bitmessagesettings','settingsversion','4') #If the settings version is equal to 2 or 3 then the sqlThread will modify the pubkeys table and change the settings version to 4. + shared.config.set('bitmessagesettings','socksproxytype','none') + shared.config.set('bitmessagesettings','sockshostname','localhost') + shared.config.set('bitmessagesettings','socksport','9050') + shared.config.set('bitmessagesettings','socksauthentication','false') + shared.config.set('bitmessagesettings','socksusername','') + shared.config.set('bitmessagesettings','sockspassword','') + shared.config.set('bitmessagesettings','keysencrypted','false') + shared.config.set('bitmessagesettings','messagesencrypted','false') + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) #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') + shared.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: + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) except: pass try: - config.getboolean('bitmessagesettings', 'movemessagstoappdata') + shared.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: + with open(shared.appdata + 'keys.dat', 'wb') as configfile: config.write(configfile) except: pass try: - #We shouldn't have to use the knownNodesLock because this had better be the only thread accessing knownNodes right now. - pickleFile = open(appdata + 'knownnodes.dat', 'rb') - knownNodes = pickle.load(pickleFile) + #We shouldn't have to use the shared.knownNodesLock because this had better be the only thread accessing knownNodes right now. + pickleFile = open(shared.appdata + 'knownnodes.dat', 'rb') + shared.knownNodes = pickle.load(pickleFile) pickleFile.close() except: - createDefaultKnownNodes(appdata) - pickleFile = open(appdata + 'knownnodes.dat', 'rb') - knownNodes = pickle.load(pickleFile) + createDefaultKnownNodes(shared.appdata) + pickleFile = open(shared.appdata + 'knownnodes.dat', 'rb') + shared.knownNodes = pickle.load(pickleFile) pickleFile.close() - - if config.getint('bitmessagesettings', 'settingsversion') > 5: + print 'at line 3769, knownNodes is', shared.knownNodes + if shared.config.getint('bitmessagesettings', 'settingsversion') > 5: print 'Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage.' raise SystemExit - if not safeConfigGetBoolean('bitmessagesettings','daemon'): - try: - #from PyQt4.QtCore import * - #from PyQt4.QtGui import * - pass - except Exception, err: - print 'PyBitmessage requires PyQt unless you want to run it as a daemon and interact with it using the API. You can download PyQt from http://www.riverbankcomputing.com/software/pyqt/download or by searching Google for \'PyQt Download\'. If you want to run in daemon mode, see https://bitmessage.org/wiki/Daemon' - print 'Error message:', err - sys.exit() - #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': + if shared.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())) + shared.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())) + shared.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.' - + print 'at line 3790, knownNodes is', shared.knownNodes #Start the address generation thread addressGeneratorThread = addressGenerator() addressGeneratorThread.daemon = True # close the main program even if there are threads left @@ -5943,15 +3873,27 @@ if __name__ == "__main__": singleListenerThread.daemon = True # close the main program even if there are threads left singleListenerThread.start() - if safeConfigGetBoolean('bitmessagesettings','apienabled'): + shared.reloadMyAddressHashes() + shared.reloadBroadcastSendersForWhichImWatching() + + #Initialize the ackdataForWhichImWatching data structure using data from the sql database. + shared.sqlSubmitQueue.put('''SELECT ackdata FROM sent where (status='sentmessage' OR status='doingpow')''') + shared.sqlSubmitQueue.put('') + queryreturn = shared.sqlReturnQueue.get() + for row in queryreturn: + ackdata, = row + print 'Watching for ackdata', ackdata.encode('hex') + ackdataForWhichImWatching[ackdata] = 0 + + if shared.safeConfigGetBoolean('bitmessagesettings','apienabled'): try: - apiNotifyPath = config.get('bitmessagesettings','apinotifypath') + apiNotifyPath = shared.config.get('bitmessagesettings','apinotifypath') except: apiNotifyPath = '' if apiNotifyPath != '': - printLock.acquire() + shared.printLock.acquire() print 'Trying to call', apiNotifyPath - printLock.release() + shared.printLock.release() call([apiNotifyPath, "startingUp"]) singleAPIThread = singleAPI() singleAPIThread.daemon = True #close the main program even if there are threads left @@ -5964,13 +3906,20 @@ if __name__ == "__main__": connectToStream(1) - if not safeConfigGetBoolean('bitmessagesettings','daemon'): - app = QtGui.QApplication(sys.argv) - app.setStyleSheet("QStatusBar::item { border: 0px solid black }") - myapp = MyForm() - myapp.show() + if not shared.safeConfigGetBoolean('bitmessagesettings','daemon'): + try: + from PyQt4.QtCore import * + from PyQt4.QtGui import * + except Exception, err: + print 'PyBitmessage requires PyQt unless you want to run it as a daemon and interact with it using the API. You can download PyQt from http://www.riverbankcomputing.com/software/pyqt/download or by searching Google for \'PyQt Download\'. If you want to run in daemon mode, see https://bitmessage.org/wiki/Daemon' + print 'Error message:', err + sys.exit() - if config.getboolean('bitmessagesettings', 'startintray'): + import bitmessageqt + bitmessageqt.run() + + + if shared.config.getboolean('bitmessagesettings', 'startintray'): myapp.hide() myapp.trayIcon.show() #self.hidden = True @@ -5979,12 +3928,11 @@ if __name__ == "__main__": if 'win32' in sys.platform or 'win64' in sys.platform: myapp.setWindowFlags(Qt.ToolTip) - sys.exit(app.exec_()) + else: print 'Running as a daemon. You can use Ctrl+C to exit.' while True: - time.sleep(2) - print 'running still..' + time.sleep(20) # So far, the Bitmessage protocol, this client, the Wiki, and the forums diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py new file mode 100644 index 00000000..f6b80495 --- /dev/null +++ b/src/bitmessageqt/__init__.py @@ -0,0 +1,1913 @@ +try: + from PyQt4.QtCore import * + from PyQt4.QtGui import * +except Exception, err: + 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).' + print 'Error message:', err + sys.exit() +from addresses import * +import shared +from bitmessageui import * +from newaddressdialog import * +from newsubscriptiondialog import * +from regenerateaddresses import * +from specialaddressbehavior import * +from settings import * +from about import * +from help import * +from iconglossary import * +import sys +from time import strftime, localtime, gmtime +import time +import os +from pyelliptic.openssl import OpenSSL + +class MyForm(QtGui.QMainWindow): + def __init__(self, parent=None): + QtGui.QWidget.__init__(self, parent) + self.ui = Ui_MainWindow() + self.ui.setupUi(self) + + #Ask the user if we may delete their old version 1 addresses if they have any. + configSections = shared.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: + shared.config.remove_section(addressInKeysFile) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.config.write(configfile) + + #Configure Bitmessage to start on startup (or remove the configuration) based on the setting in the keys.dat file + if 'win32' in sys.platform or 'win64' in sys.platform: + #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 shared.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) + self.trayIcon.setIcon( QtGui.QIcon(':/newPrefix/images/can-icon-16px.png') ) + 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) + #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() + + self.ui.labelSendBroadcastWarning.setVisible(False) + + #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) + QtCore.QObject.connect(self.ui.actionRegenerateDeterministicAddresses, QtCore.SIGNAL("triggered()"), self.click_actionRegenerateDeterministicAddresses) + 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 + 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) + self.actionTrashInboxMessage = self.ui.inboxContextMenuToolbar.addAction("Move to Trash", self.on_action_InboxTrash) + self.actionForceHtml = self.ui.inboxContextMenuToolbar.addAction("View HTML code as formatted text", self.on_action_InboxMessageForceHtml) + 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.actionForceHtml ) + self.popMenuInbox.addSeparator() + 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 + 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) + self.actionSpecialAddressBehavior = self.ui.addressContextMenuToolbar.addAction("Special address behavior...", self.on_action_SpecialAddressBehaviorDialog) + 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 ) + self.popMenu.addAction( self.actionSpecialAddressBehavior ) + + #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) + 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) + 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 ) + self.popMenuAddressBook.addSeparator() + self.popMenuAddressBook.addAction( self.actionAddressBookNew ) + self.popMenuAddressBook.addAction( self.actionAddressBookDelete ) + + #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.actionsubscriptionsEnable = self.ui.subscriptionsContextMenuToolbar.addAction("Enable", self.on_action_SubscriptionsEnable) + self.actionsubscriptionsDisable = self.ui.subscriptionsContextMenuToolbar.addAction("Disable", self.on_action_SubscriptionsDisable) + 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.actionsubscriptionsEnable ) + self.popMenuSubscriptions.addAction( self.actionsubscriptionsDisable ) + self.popMenuSubscriptions.addSeparator() + self.popMenuSubscriptions.addAction( self.actionsubscriptionsClipboard ) + + #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) + self.actionSentClipboard = self.ui.sentContextMenuToolbar.addAction("Copy destination address to clipboard", self.on_action_SentClipboard) + 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 ) + self.popMenuSent.addAction( self.actionSentClipboard ) + self.popMenuSent.addAction( self.actionTrashSentMessage ) + + + #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 ) + + #Initialize the user's list of addresses on the 'Your Identities' tab. + configSections = shared.config.sections() + for addressInKeysFile in configSections: + if addressInKeysFile <> 'bitmessagesettings': + isEnabled = shared.config.getboolean(addressInKeysFile, 'enabled') + newItem = QtGui.QTableWidgetItem(unicode(shared.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)) + if shared.safeConfigGetBoolean(addressInKeysFile,'mailinglist'): + newItem.setTextColor(QtGui.QColor(137,04,177))#magenta + 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) + + #self.sqlLookup = sqlThread() + #self.sqlLookup.start() + + self.ui.tableWidgetSent.keyPressEvent = self.tableWidgetSentKeyPressEvent + font = QFont() + font.setBold(True) + #Load inbox from messages database file + shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, received, message, read FROM inbox where folder='inbox' ORDER BY received''') + shared.sqlSubmitQueue.put('') + queryreturn = shared.sqlReturnQueue.get() + for row in queryreturn: + msgid, toAddress, fromAddress, subject, received, message, read = row + + try: + if toAddress == '[Broadcast subscribers]': + toLabel = '[Broadcast subscribers]' + else: + toLabel = shared.config.get(toAddress, 'label') + except: + toLabel = '' + if toLabel == '': + toLabel = toAddress + + fromLabel = '' + t = (fromAddress,) + shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + + if queryreturn <> []: + for row in queryreturn: + fromLabel, = row + + if fromLabel == '': #If this address wasn't in our address book.. + t = (fromAddress,) + shared.sqlSubmitQueue.put('''select label from subscriptions where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + + if queryreturn <> []: + for row in queryreturn: + fromLabel, = row + + self.ui.tableWidgetInbox.insertRow(0) + newItem = QtGui.QTableWidgetItem(unicode(toLabel,'utf-8')) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + if not read: + newItem.setFont(font) + newItem.setData(Qt.UserRole,str(toAddress)) + if shared.safeConfigGetBoolean(toAddress,'mailinglist'): + newItem.setTextColor(QtGui.QColor(137,04,177)) + self.ui.tableWidgetInbox.setItem(0,0,newItem) + if fromLabel == '': + newItem = QtGui.QTableWidgetItem(unicode(fromAddress,'utf-8')) + else: + newItem = QtGui.QTableWidgetItem(unicode(fromLabel,'utf-8')) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + if not read: + newItem.setFont(font) + 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)')) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + if not read: + newItem.setFont(font) + self.ui.tableWidgetInbox.setItem(0,2,newItem) + newItem = myTableWidgetItem(unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(received))),'utf-8')) + newItem.setData(Qt.UserRole,QByteArray(msgid)) + newItem.setData(33,int(received)) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + if not read: + newItem.setFont(font) + self.ui.tableWidgetInbox.setItem(0,3,newItem) + self.ui.tableWidgetInbox.sortItems(3,Qt.DescendingOrder) + + self.ui.tableWidgetInbox.keyPressEvent = self.tableWidgetInboxKeyPressEvent + #Load Sent items from database + shared.sqlSubmitQueue.put('''SELECT toaddress, fromaddress, subject, message, status, ackdata, lastactiontime FROM sent where folder = 'sent' ORDER BY lastactiontime''') + shared.sqlSubmitQueue.put('') + queryreturn = shared.sqlReturnQueue.get() + for row in queryreturn: + toAddress, fromAddress, subject, message, status, ackdata, lastactiontime = row + try: + fromLabel = shared.config.get(fromAddress, 'label') + except: + fromLabel = '' + if fromLabel == '': + fromLabel = fromAddress + + toLabel = '' + t = (toAddress,) + shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.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)) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + 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)) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetSent.setItem(0,1,newItem) + newItem = QtGui.QTableWidgetItem(unicode(subject,'utf-8')) + newItem.setData(Qt.UserRole,unicode(message,'utf-8)')) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetSent.setItem(0,2,newItem) + if status == 'findingpubkey': + newItem = myTableWidgetItem('Waiting on their public key. Will request it again soon.') + elif status == 'sentmessage': + newItem = myTableWidgetItem('Message sent. Waiting on acknowledgement. Sent at ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(lastactiontime)),'utf-8')) + elif status == 'doingpow': + newItem = myTableWidgetItem('Need to do work to send message. Work is queued.') + elif status == 'ackreceived': + newItem = myTableWidgetItem('Acknowledgement of the message received ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(lastactiontime))),'utf-8')) + elif status == 'broadcastpending': + newItem = myTableWidgetItem('Doing the work necessary to send broadcast...') + elif status == 'broadcastsent': + newItem = myTableWidgetItem('Broadcast on ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(lastactiontime))),'utf-8')) + else: + newItem = myTableWidgetItem('Unknown status. ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(lastactiontime))),'utf-8')) + newItem.setData(Qt.UserRole,QByteArray(ackdata)) + newItem.setData(33,int(lastactiontime)) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetSent.setItem(0,3,newItem) + self.ui.tableWidgetSent.sortItems(3,Qt.DescendingOrder) + + #Initialize the address book + shared.sqlSubmitQueue.put('SELECT * FROM addressbook') + shared.sqlSubmitQueue.put('') + queryreturn = shared.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 + shared.sqlSubmitQueue.put('SELECT label, address, enabled FROM subscriptions') + shared.sqlSubmitQueue.put('') + queryreturn = shared.sqlReturnQueue.get() + for row in queryreturn: + label, address, enabled = row + self.ui.tableWidgetSubscriptions.insertRow(0) + newItem = QtGui.QTableWidgetItem(unicode(label,'utf-8')) + if not enabled: + newItem.setTextColor(QtGui.QColor(128,128,128)) + self.ui.tableWidgetSubscriptions.setItem(0,0,newItem) + newItem = QtGui.QTableWidgetItem(address) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + if not enabled: + newItem.setTextColor(QtGui.QColor(128,128,128)) + self.ui.tableWidgetSubscriptions.setItem(0,1,newItem) + + #Initialize the Blacklist or Whitelist + if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': + self.loadBlackWhiteList() + else: + self.ui.tabWidget.setTabText(6,'Whitelist') + self.ui.radioButtonWhitelist.click() + self.loadBlackWhiteList() + + + 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) + QtCore.QObject.connect(self.ui.tableWidgetInbox, QtCore.SIGNAL("itemSelectionChanged ()"), self.tableWidgetInboxItemClicked) + QtCore.QObject.connect(self.ui.tableWidgetSent, QtCore.SIGNAL("itemSelectionChanged ()"), self.tableWidgetSentItemClicked) + + #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 ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + 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() + + self.UISignalThread = UISignaler() + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.writeNewAddressToTable) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByHash) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByAckdata) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.displayNewInboxMessage) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.displayNewSentMessage) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"), self.updateNetworkStatusTab) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfMessagesProcessed()"), self.incrementNumberOfMessagesProcessed) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfPubkeysProcessed()"), self.incrementNumberOfPubkeysProcessed) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfBroadcastsProcessed()"), self.incrementNumberOfBroadcastsProcessed) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("setStatusIcon(PyQt_PyObject)"), self.setStatusIcon) + self.UISignalThread.start() + + #self.connectToStream(1) + + #self.singleListenerThread = singleListener() + #self.singleListenerThread.start() + #QtCore.QObject.connect(self.singleListenerThread, QtCore.SIGNAL("passObjectThrough(PyQt_PyObject)"), self.connectObjectToSignals) + + + #self.singleCleanerThread = singleCleaner() + #self.singleCleanerThread.start() + #QtCore.QObject.connect(self.singleCleanerThread, QtCore.SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByHash) + #QtCore.QObject.connect(self.singleCleanerThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + + #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) + #QtCore.QObject.connect(self.workerThread, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + + def tableWidgetInboxKeyPressEvent(self,event): + if event.key() == QtCore.Qt.Key_Delete: + self.on_action_InboxTrash() + return QtGui.QTableWidget.keyPressEvent(self.ui.tableWidgetInbox, event) + + def tableWidgetSentKeyPressEvent(self,event): + if event.key() == QtCore.Qt.Key_Delete: + self.on_action_SentTrash() + return QtGui.QTableWidget.keyPressEvent(self.ui.tableWidgetSent, event) + + def click_actionManageKeys(self): + if 'darwin' in sys.platform or 'linux' in sys.platform: + if shared.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' + shared.appdata + '\nIt is important that you back up this file.', QMessageBox.Ok) + elif sys.platform == 'win32' or sys.platform == 'win64': + if shared.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' + shared.appdata + '\nIt is important that you back up this file. Would you like to open the file now? (Be sure to close Bitmessage before making any changes.)', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) + if reply == QtGui.QMessageBox.Yes: + self.openKeysFile() + + 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() + shared.addressGeneratorQueue.put((addressVersionNumber,streamNumberForAddress,"regenerated deterministic address",self.regenerateAddressesDialogInstance.ui.spinBoxNumberOfAddressesToMake.value(),self.regenerateAddressesDialogInstance.ui.lineEditPassphrase.text().toUtf8(),self.regenerateAddressesDialogInstance.ui.checkBoxEighteenByteRipe.isChecked())) + self.ui.tabWidget.setCurrentIndex(3) + + def openKeysFile(self): + if 'linux' in sys.platform: + subprocess.call(["xdg-open", shared.appdata + 'keys.dat']) + else: + os.startfile(shared.appdata + 'keys.dat') + + def changeEvent(self, event): + if shared.config.getboolean('bitmessagesettings', 'minimizetotray') and not 'darwin' in sys.platform: + if event.type() == QtCore.QEvent.WindowStateChange: + if self.windowState() & QtCore.Qt.WindowMinimized: + self.hide() + self.trayIcon.show() + #self.hidden = True + if 'win32' in sys.platform or 'win64' in sys.platform: + 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: + 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() + self.setWindowState(self.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive) + self.activateWindow() + 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() + + 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.') + + def incrementNumberOfPubkeysProcessed(self): + self.numberOfPubkeysProcessed += 1 + self.ui.labelPubkeyCount.setText('Processed ' + str(self.numberOfPubkeysProcessed) + ' public keys.') + + def updateNetworkStatusTab(self,streamNumber,connectionCount): + #print 'updating network status tab' + totalNumberOfConnectionsFromAllStreams = 0 #One would think we could use len(sendDataQueues) for this but the number doesn't always match: just because we have a sendDataThread running doesn't mean that the connection has been fully established (with the exchange of version messages). + foundTheRowThatNeedsUpdating = False + for currentRow in range(self.ui.tableWidgetConnectionCount.rowCount()): + rowStreamNumber = int(self.ui.tableWidgetConnectionCount.item(currentRow,0).text()) + if streamNumber == rowStreamNumber: + foundTheRowThatNeedsUpdating = True + self.ui.tableWidgetConnectionCount.item(currentRow,1).setText(str(connectionCount)) + totalNumberOfConnectionsFromAllStreams += connectionCount + if foundTheRowThatNeedsUpdating == False: + #Add a line to the table for this stream number and update its count with the current connection count. + 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(str(connectionCount)) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetConnectionCount.setItem(0,1,newItem) + totalNumberOfConnectionsFromAllStreams += connectionCount + self.ui.labelTotalConnections.setText('Total Connections: ' + str(totalNumberOfConnectionsFromAllStreams)) + if totalNumberOfConnectionsFromAllStreams > 0 and shared.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): + #print 'setting status icon color' + if color == 'red': + self.ui.pushButtonStatusIcon.setIcon(QIcon(":/newPrefix/images/redicon.png")) + shared.statusIconColor = 'red' + if color == 'yellow': + 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('') + self.ui.pushButtonStatusIcon.setIcon(QIcon(":/newPrefix/images/yellowicon.png")) + shared.statusIconColor = 'yellow' + if color == 'green': + 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('') + self.ui.pushButtonStatusIcon.setIcon(QIcon(":/newPrefix/images/greenicon.png")) + shared.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')) + self.ui.tableWidgetSent.item(i,3).setText(textToDisplay) + + 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')) + self.ui.tableWidgetSent.item(i,3).setText(textToDisplay) + + 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,) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.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. + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''select label from subscriptions where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.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 = shared.config.get(toAddress, 'label') + except: + toLabel = '' + if toLabel == '': + toLabel = toAddress + self.ui.tableWidgetInbox.item(i,0).setText(unicode(toLabel,'utf-8')) + #Set the color according to whether it is the address of a mailing list or not. + if shared.safeConfigGetBoolean(toAddress,'mailinglist'): + self.ui.tableWidgetInbox.item(i,0).setTextColor(QtGui.QColor(137,04,177)) + else: + self.ui.tableWidgetInbox.item(i,0).setTextColor(QtGui.QColor(0,0,0)) + + 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 = shared.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,) + shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + + if queryreturn <> []: + for row in queryreturn: + toLabel, = row + self.ui.tableWidgetSent.item(i,0).setText(unicode(toLabel,'utf-8')) + + 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(';')] + 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. + for toAddress in toAddressesList: + if toAddress <> '': + status,addressVersionNumber,streamNumber,ripe = decodeAddress(toAddress) + if status <> 'success': + shared.printLock.acquire() + print 'Error: Could not decode', toAddress, ':', status + shared.printLock.release() + if status == 'missingbm': + self.statusBar().showMessage('Error: Bitmessage addresses start with BM- Please check ' + toAddress) + elif status == 'checksumfailed': + self.statusBar().showMessage('Error: The address ' + toAddress+' is not typed or copied correctly. Please check it.') + elif status == 'invalidcharacters': + self.statusBar().showMessage('Error: The address '+ toAddress+ ' contains invalid characters. Please check it.') + elif 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.') + elif status == 'ripetooshort': + self.statusBar().showMessage('Error: Some data encoded in the address '+ toAddress+ ' is too short. There might be something wrong with the software of your acquaintance.') + elif status == 'ripetoolong': + self.statusBar().showMessage('Error: Some data encoded in the address '+ toAddress+ ' is too long. There might be something wrong with the software of your acquaintance.') + else: + self.statusBar().showMessage('Error: Something is wrong with the address '+ toAddress+ '.') + elif fromAddress == '': + self.statusBar().showMessage('Error: You must specify a From address. If you don\'t have one, go to the \'Your Identities\' tab.') + else: + toAddress = addBMIfNotPresent(toAddress) + try: + shared.config.get(toAddress, 'enabled') + #The toAddress is one owned by me. We cannot send messages to ourselves without significant changes to the codebase. + 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 + if addressVersionNumber > 3 or addressVersionNumber == 0: + 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.") + continue + if streamNumber > 1 or streamNumber == 0: + QMessageBox.about(self, "Stream number", "Concerning the address "+toAddress+", Bitmessage cannot handle stream numbers of "+str(streamNumber)+". Perhaps upgrade Bitmessage to the latest version.") + continue + self.statusBar().showMessage('') + if shared.statusIconColor == 'red': + 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.') + ackdata = OpenSSL.rand(32) + shared.sqlLock.acquire() + t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'findingpubkey',1,1,'sent',2) + shared.sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + + toLabel = '' + t = (toAddress,) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() + if queryreturn <> []: + for row in queryreturn: + toLabel, = row + + self.displayNewSentMessage(toAddress,toLabel,fromAddress, subject, message, ackdata) + shared.workerQueue.put(('sendmessage',toAddress)) + + self.ui.comboBoxSendFrom.setCurrentIndex(0) + self.ui.labelFrom.setText('') + self.ui.lineEditTo.setText('') + self.ui.lineEditSubject.setText('') + self.ui.textEditMessage.setText('') + self.ui.tabWidget.setCurrentIndex(2) + self.ui.tableWidgetSent.setCurrentCell(0,0) + 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. + ackdata = OpenSSL.rand(32) + toAddress = '[Broadcast subscribers]' + ripe = '' + shared.sqlLock.acquire() + t = ('',toAddress,ripe,fromAddress,subject,message,ackdata,int(time.time()),'broadcastpending',1,1,'sent',2) + shared.sqlSubmitQueue.put('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + + shared.workerQueue.put(('sendbroadcast',(fromAddress,subject,message))) + + try: + fromLabel = shared.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...'+ unicode(strftime(config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + newItem = myTableWidgetItem('Work is queued.') + newItem.setData(Qt.UserRole,QByteArray(ackdata)) + newItem.setData(33,int(time.time())) + self.ui.tableWidgetSent.setItem(0,3,newItem) + + self.ui.textEditSentMessage.setPlainText(self.ui.tableWidgetSent.item(0,2).data(Qt.UserRole).toPyObject()) + + self.ui.comboBoxSendFrom.setCurrentIndex(0) + self.ui.labelFrom.setText('') + self.ui.lineEditTo.setText('') + self.ui.lineEditSubject.setText('') + self.ui.textEditMessage.setText('') + self.ui.tabWidget.setCurrentIndex(2) + self.ui.tableWidgetSent.setCurrentCell(0,0) + + + 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 one or more entries in your address book and select \'Send message to this address\'.') + + def redrawLabelFrom(self,index): + self.ui.labelFrom.setText(self.ui.comboBoxSendFrom.itemData(index).toPyObject()) + + def rerenderComboBoxSendFrom(self): + self.ui.comboBoxSendFrom.clear() + self.ui.labelFrom.setText('') + configSections = shared.config.sections() + for addressInKeysFile in configSections: + if addressInKeysFile <> 'bitmessagesettings': + isEnabled = shared.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(shared.config.get(addressInKeysFile, 'label'),'utf-8'),addressInKeysFile) + 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) + + + + def connectObjectToSignals(self,object): + QtCore.QObject.connect(object, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) + 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) + 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) + + #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) + + #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 = shared.config.get(fromAddress, 'label') + except: + fromLabel = '' + if fromLabel == '': + fromLabel = fromAddress + + self.ui.tableWidgetSent.setSortingEnabled(False) + 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)) + 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...'+ unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + newItem = myTableWidgetItem('Work is queued. '+ unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + newItem.setData(Qt.UserRole,QByteArray(ackdata)) + newItem.setData(33,int(time.time())) + self.ui.tableWidgetSent.setItem(0,3,newItem) + self.ui.textEditSentMessage.setPlainText(self.ui.tableWidgetSent.item(0,2).data(Qt.UserRole).toPyObject()) + self.ui.tableWidgetSent.setSortingEnabled(True) + + def displayNewInboxMessage(self,inventoryHash,toAddress,fromAddress,subject,message): + '''print 'test signals displayNewInboxMessage' + print 'toAddress', toAddress + print 'fromAddress', fromAddress + print 'message', message''' + + fromLabel = '' + shared.sqlLock.acquire() + t = (fromAddress,) + shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() + if queryreturn <> []: + for row in queryreturn: + fromLabel, = row + else: + #There might be a label in the subscriptions table + shared.sqlLock.acquire() + t = (fromAddress,) + shared.sqlSubmitQueue.put('''select label from subscriptions where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() + if queryreturn <> []: + for row in queryreturn: + fromLabel, = row + + try: + if toAddress == '[Broadcast subscribers]': + toLabel = '[Broadcast subscribers]' + else: + toLabel = shared.config.get(toAddress, 'label') + except: + toLabel = '' + if toLabel == '': + toLabel = toAddress + + font = QFont() + font.setBold(True) + self.ui.tableWidgetInbox.setSortingEnabled(False) + newItem = QtGui.QTableWidgetItem(unicode(toLabel,'utf-8')) + newItem.setFont(font) + newItem.setData(Qt.UserRole,str(toAddress)) + if shared.safeConfigGetBoolean(str(toAddress),'mailinglist'): + newItem.setTextColor(QtGui.QColor(137,04,177)) + self.ui.tableWidgetInbox.insertRow(0) + self.ui.tableWidgetInbox.setItem(0,0,newItem) + + if fromLabel == '': + newItem = QtGui.QTableWidgetItem(unicode(fromAddress,'utf-8')) + if shared.config.getboolean('bitmessagesettings', 'showtraynotifications'): + self.trayIcon.showMessage('New Message', 'New message from '+ fromAddress, 1, 2000) + else: + newItem = QtGui.QTableWidgetItem(unicode(fromLabel,'utf-8')) + if shared.config.getboolean('bitmessagesettings', 'showtraynotifications'): + self.trayIcon.showMessage('New Message', 'New message from '+fromLabel, 1, 2000) + newItem.setData(Qt.UserRole,str(fromAddress)) + newItem.setFont(font) + self.ui.tableWidgetInbox.setItem(0,1,newItem) + newItem = QtGui.QTableWidgetItem(unicode(subject,'utf-8)')) + newItem.setData(Qt.UserRole,unicode(message,'utf-8)')) + newItem.setFont(font) + self.ui.tableWidgetInbox.setItem(0,2,newItem) + newItem = myTableWidgetItem(unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')) + newItem.setData(Qt.UserRole,QByteArray(inventoryHash)) + newItem.setData(33,int(time.time())) + newItem.setFont(font) + self.ui.tableWidgetInbox.setItem(0,3,newItem) + + """#If we have received this message from either a broadcast address or from someone in our address book, display as HTML + if decodeAddress(fromAddress)[3] in shared.broadcastSendersForWhichImWatching or shared.isAddressInMyAddressBook(fromAddress): + self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(0,2).data(Qt.UserRole).toPyObject()) + else: + self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(0,2).data(Qt.UserRole).toPyObject())""" + self.ui.tableWidgetInbox.setSortingEnabled(True) + + 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. + shared.sqlLock.acquire() + t = (addBMIfNotPresent(str(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())),) + shared.sqlSubmitQueue.put('''select * from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() + if queryreturn == []: + self.ui.tableWidgetAddressBook.setSortingEnabled(False) + self.ui.tableWidgetAddressBook.insertRow(0) + newItem = QtGui.QTableWidgetItem(unicode(self.NewSubscriptionDialogInstance.ui.newsubscriptionlabel.text().toUtf8(),'utf-8')) + self.ui.tableWidgetAddressBook.setItem(0,0,newItem) + newItem = QtGui.QTableWidgetItem(addBMIfNotPresent(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetAddressBook.setItem(0,1,newItem) + self.ui.tableWidgetAddressBook.setSortingEnabled(True) + t = (str(self.NewSubscriptionDialogInstance.ui.newsubscriptionlabel.text().toUtf8()),addBMIfNotPresent(str(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text()))) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO addressbook VALUES (?,?)''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + self.rerenderInboxFromLabels() + self.rerenderSentToLabels() + 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. + shared.sqlLock.acquire() + t = (addBMIfNotPresent(str(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())),) + shared.sqlSubmitQueue.put('''select * from subscriptions where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() + if queryreturn == []: + self.ui.tableWidgetSubscriptions.setSortingEnabled(False) + self.ui.tableWidgetSubscriptions.insertRow(0) + newItem = QtGui.QTableWidgetItem(unicode(self.NewSubscriptionDialogInstance.ui.newsubscriptionlabel.text().toUtf8(),'utf-8')) + self.ui.tableWidgetSubscriptions.setItem(0,0,newItem) + newItem = QtGui.QTableWidgetItem(addBMIfNotPresent(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetSubscriptions.setItem(0,1,newItem) + self.ui.tableWidgetSubscriptions.setSortingEnabled(True) + t = (str(self.NewSubscriptionDialogInstance.ui.newsubscriptionlabel.text().toUtf8()),addBMIfNotPresent(str(self.NewSubscriptionDialogInstance.ui.lineEditSubscriptionAddress.text())),True) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO subscriptions VALUES (?,?,?)''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + self.rerenderInboxFromLabels() + shared.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 = shared.config.get('bitmessagesettings', 'blackwhitelist') + if listType == 'black': + shared.sqlSubmitQueue.put('''SELECT label, address, enabled FROM blacklist''') + else: + shared.sqlSubmitQueue.put('''SELECT label, address, enabled FROM whitelist''') + shared.sqlSubmitQueue.put('') + queryreturn = shared.sqlReturnQueue.get() + for row in queryreturn: + label, address, enabled = row + self.ui.tableWidgetBlacklist.insertRow(0) + newItem = QtGui.QTableWidgetItem(unicode(label,'utf-8')) + if not enabled: + newItem.setTextColor(QtGui.QColor(128,128,128)) + self.ui.tableWidgetBlacklist.setItem(0,0,newItem) + newItem = QtGui.QTableWidgetItem(address) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + if not enabled: + newItem.setTextColor(QtGui.QColor(128,128,128)) + self.ui.tableWidgetBlacklist.setItem(0,1,newItem) + + def click_pushButtonStatusIcon(self): + print 'click_pushButtonStatusIcon' + self.iconGlossaryInstance = iconGlossaryDialog(self) + if self.iconGlossaryInstance.exec_(): + pass + + 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): + self.settingsDialogInstance = settingsDialog(self) + if self.settingsDialogInstance.exec_(): + shared.config.set('bitmessagesettings', 'startonlogon', str(self.settingsDialogInstance.ui.checkBoxStartOnLogon.isChecked())) + shared.config.set('bitmessagesettings', 'minimizetotray', str(self.settingsDialogInstance.ui.checkBoxMinimizeToTray.isChecked())) + shared.config.set('bitmessagesettings', 'showtraynotifications', str(self.settingsDialogInstance.ui.checkBoxShowTrayNotifications.isChecked())) + shared.config.set('bitmessagesettings', 'startintray', str(self.settingsDialogInstance.ui.checkBoxStartInTray.isChecked())) + if int(shared.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.") + shared.config.set('bitmessagesettings', 'port', str(self.settingsDialogInstance.ui.lineEditTCPPort.text())) + if shared.config.get('bitmessagesettings', 'socksproxytype') == 'none' and str(self.settingsDialogInstance.ui.comboBoxProxyType.currentText())[0:5] == 'SOCKS': + if shared.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 shared.config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS' and str(self.settingsDialogInstance.ui.comboBoxProxyType.currentText()) == 'none': + self.statusBar().showMessage('') + shared.config.set('bitmessagesettings', 'socksproxytype', str(self.settingsDialogInstance.ui.comboBoxProxyType.currentText())) + shared.config.set('bitmessagesettings', 'socksauthentication', str(self.settingsDialogInstance.ui.checkBoxAuthentication.isChecked())) + shared.config.set('bitmessagesettings', 'sockshostname', str(self.settingsDialogInstance.ui.lineEditSocksHostname.text())) + shared.config.set('bitmessagesettings', 'socksport', str(self.settingsDialogInstance.ui.lineEditSocksPort.text())) + shared.config.set('bitmessagesettings', 'socksusername', str(self.settingsDialogInstance.ui.lineEditSocksUsername.text())) + shared.config.set('bitmessagesettings', 'sockspassword', str(self.settingsDialogInstance.ui.lineEditSocksPassword.text())) + if float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text()) >= 1: + shared.config.set('bitmessagesettings', 'defaultnoncetrialsperbyte',str(int(float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text())*networkDefaultProofOfWorkNonceTrialsPerByte))) + if float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text()) >= 1: + shared.config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes',str(int(float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text())*networkDefaultPayloadLengthExtraBytes))) + + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.config.write(configfile) + + if 'win32' in sys.platform or 'win64' in sys.platform: + #Auto-startup for Windows + RUN_PATH = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" + self.settings = QSettings(RUN_PATH, QSettings.NativeFormat) + if shared.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 + + if shared.appdata != '' and self.settingsDialogInstance.ui.checkBoxPortableMode.isChecked(): #If we are NOT using portable mode now but the user selected that we should... + shared.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: + shared.config.write(configfile) + #Write the knownnodes.dat file to disk in the new location + shared.knownNodesLock.acquire() + output = open('knownnodes.dat', 'wb') + pickle.dump(shared.knownNodes, output) + output.close() + shared.knownNodesLock.release() + os.remove(shared.appdata + 'keys.dat') + os.remove(shared.appdata + 'knownnodes.dat') + shared.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 shared.appdata == '' and not self.settingsDialogInstance.ui.checkBoxPortableMode.isChecked(): #If we ARE using portable mode now but the user selected that we shouldn't... + shared.appdata = shared.lookupAppdataFolder() + if not os.path.exists(shared.appdata): + os.makedirs(shared.appdata) + shared.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(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.config.write(configfile) + #Write the knownnodes.dat file to disk in the new location + shared.knownNodesLock.acquire() + output = open(shared.appdata + 'knownnodes.dat', 'wb') + pickle.dump(shared.knownNodes, output) + output.close() + shared.knownNodesLock.release() + 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).") + + + def click_radioButtonBlacklist(self): + if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'white': + shared.config.set('bitmessagesettings','blackwhitelist','black') + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.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 shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': + shared.config.set('bitmessagesettings','blackwhitelist','white') + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.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. + shared.sqlLock.acquire() + t = (addBMIfNotPresent(str(self.NewBlacklistDialogInstance.ui.lineEditSubscriptionAddress.text())),) + if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': + shared.sqlSubmitQueue.put('''select * from blacklist where address=?''') + else: + shared.sqlSubmitQueue.put('''select * from whitelist where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() + if queryreturn == []: + self.ui.tableWidgetBlacklist.setSortingEnabled(False) + self.ui.tableWidgetBlacklist.insertRow(0) + newItem = QtGui.QTableWidgetItem(unicode(self.NewBlacklistDialogInstance.ui.newsubscriptionlabel.text().toUtf8(),'utf-8')) + self.ui.tableWidgetBlacklist.setItem(0,0,newItem) + newItem = QtGui.QTableWidgetItem(addBMIfNotPresent(self.NewBlacklistDialogInstance.ui.lineEditSubscriptionAddress.text())) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetBlacklist.setItem(0,1,newItem) + self.ui.tableWidgetBlacklist.setSortingEnabled(True) + t = (str(self.NewBlacklistDialogInstance.ui.newsubscriptionlabel.text().toUtf8()),addBMIfNotPresent(str(self.NewBlacklistDialogInstance.ui.lineEditSubscriptionAddress.text())),True) + shared.sqlLock.acquire() + if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': + shared.sqlSubmitQueue.put('''INSERT INTO blacklist VALUES (?,?,?)''') + else: + shared.sqlSubmitQueue.put('''INSERT INTO whitelist VALUES (?,?,?)''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.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.') + + 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(): + shared.config.set(str(addressAtCurrentRow),'mailinglist','false') + #Set the color to either black or grey + if shared.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)) + else: + shared.config.set(str(addressAtCurrentRow),'mailinglist','true') + shared.config.set(str(addressAtCurrentRow),'mailinglistname',str(self.dialog.ui.lineEditMailingListName.text().toUtf8())) + self.ui.tableWidgetYourIdentities.item(currentRow,1).setTextColor(QtGui.QColor(137,04,177)) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.config.write(configfile) + self.rerenderInboxToLabels() + + + def click_NewAddressDialog(self): + self.dialog = NewAddressDialog(self) + # For Modal dialogs + if self.dialog.exec_(): + #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(3,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() + shared.addressGeneratorQueue.put((3,streamNumberForAddress,str(self.dialog.ui.newaddresslabel.text().toUtf8()),1,"",self.dialog.ui.checkBoxEighteenByteRipe.isChecked())) + else: + 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(3,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() + shared.addressGeneratorQueue.put((3,streamNumberForAddress,"unused deterministic address",self.dialog.ui.spinBoxNumberOfAddressesToMake.value(),self.dialog.ui.lineEditPassphrase.text().toUtf8(),self.dialog.ui.checkBoxEighteenByteRipe.isChecked())) + else: + print 'new address dialog box rejected' + + 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()''' + shared.doCleanShutdown() + self.trayIcon.hide() + self.statusBar().showMessage('All done. Closing user interface...') + event.accept() + print 'Done. (passed event.accept())' + os._exit(0) + + def on_action_InboxMessageForceHtml(self): + currentInboxRow = self.ui.tableWidgetInbox.currentRow() + lines = self.ui.tableWidgetInbox.item(currentInboxRow,2).data(Qt.UserRole).toPyObject().split('\n') + for i in xrange(len(lines)): + if lines[i].contains('Message ostensibly from '): + lines[i] = '

%s

' % (lines[i]) + elif lines[i] == '------------------------------------------------------': + lines[i] = '
' + content = '' + for i in xrange(len(lines)): + content += lines[i] + content = content.replace('\n\n', '

') + self.ui.textEditInboxMessage.setHtml(QtCore.QString(content)) + + def on_action_InboxReply(self): + 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()) + + if toAddressAtCurrentInboxRow == '[Broadcast subscribers]': + self.ui.labelFrom.setText('') + else: + if not shared.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) + self.ui.lineEditTo.setText(str(fromAddressAtCurrentInboxRow)) + self.ui.comboBoxSendFrom.setCurrentIndex(0) + #self.ui.comboBoxSendFrom.setEditText(str(self.ui.tableWidgetInbox.item(currentInboxRow,0).text)) + self.ui.textEditMessage.setText('\n\n------------------------------------------------------\n'+self.ui.tableWidgetInbox.item(currentInboxRow,2).data(Qt.UserRole).toPyObject()) + if self.ui.tableWidgetInbox.item(currentInboxRow,2).text()[0:3] == 'Re:': + self.ui.lineEditSubject.setText(self.ui.tableWidgetInbox.item(currentInboxRow,2).text()) + else: + self.ui.lineEditSubject.setText('Re: '+self.ui.tableWidgetInbox.item(currentInboxRow,2).text()) + self.ui.radioButtonSpecific.setChecked(True) + self.ui.tabWidget.setCurrentIndex(1) + + def on_action_InboxAddSenderToAddressBook(self): + 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 + shared.sqlLock.acquire() + t = (addressAtCurrentInboxRow,) + shared.sqlSubmitQueue.put('''select * from addressbook where address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.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) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''INSERT INTO addressbook VALUES (?,?)''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.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.') + + #Send item on the Inbox tab to trash + def on_action_InboxTrash(self): + while self.ui.tableWidgetInbox.selectedIndexes() != []: + currentRow = self.ui.tableWidgetInbox.selectedIndexes()[0].row() + inventoryHashToTrash = str(self.ui.tableWidgetInbox.item(currentRow,3).data(Qt.UserRole).toPyObject()) + t = (inventoryHashToTrash,) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''UPDATE inbox SET folder='trash' WHERE msgid=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlLock.release() + self.ui.textEditInboxMessage.setText("") + self.ui.tableWidgetInbox.removeRow(currentRow) + self.statusBar().showMessage('Moved items 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.') + shared.sqlSubmitQueue.put('commit') + if currentRow == 0: + self.ui.tableWidgetInbox.selectRow(currentRow) + else: + self.ui.tableWidgetInbox.selectRow(currentRow-1) + + #Send item on the Sent tab to trash + def on_action_SentTrash(self): + while self.ui.tableWidgetSent.selectedIndexes() != []: + currentRow = self.ui.tableWidgetSent.selectedIndexes()[0].row() + ackdataToTrash = str(self.ui.tableWidgetSent.item(currentRow,3).data(Qt.UserRole).toPyObject()) + t = (ackdataToTrash,) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''UPDATE sent SET folder='trash' WHERE ackdata=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlLock.release() + self.ui.textEditSentMessage.setPlainText("") + self.ui.tableWidgetSent.removeRow(currentRow) + self.statusBar().showMessage('Moved items 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.') + shared.sqlSubmitQueue.put('commit') + if currentRow == 0: + self.ui.tableWidgetSent.selectRow(currentRow) + else: + self.ui.tableWidgetSent.selectRow(currentRow-1) + + 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)) + + #Group of functions for the Address Book dialog box + def on_action_AddressBookNew(self): + self.click_pushButtonAddAddressBook() + def on_action_AddressBookDelete(self): + while self.ui.tableWidgetAddressBook.selectedIndexes() != []: + currentRow = self.ui.tableWidgetAddressBook.selectedIndexes()[0].row() + labelAtCurrentRow = self.ui.tableWidgetAddressBook.item(currentRow,0).text().toUtf8() + addressAtCurrentRow = self.ui.tableWidgetAddressBook.item(currentRow,1).text() + t = (str(labelAtCurrentRow),str(addressAtCurrentRow)) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''DELETE FROM addressbook WHERE label=? AND address=?''') + shared.sqlSubmitQueue.put(t) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + self.ui.tableWidgetAddressBook.removeRow(currentRow) + self.rerenderInboxFromLabels() + self.rerenderSentToLabels() + def on_action_AddressBookClipboard(self): + fullStringOfAddresses = '' + listOfSelectedRows = {} + for i in range(len(self.ui.tableWidgetAddressBook.selectedIndexes())): + listOfSelectedRows[self.ui.tableWidgetAddressBook.selectedIndexes()[i].row()] = 0 + for currentRow in listOfSelectedRows: + addressAtCurrentRow = self.ui.tableWidgetAddressBook.item(currentRow,1).text() + if fullStringOfAddresses == '': + fullStringOfAddresses = addressAtCurrentRow + else: + fullStringOfAddresses += ', '+ str(addressAtCurrentRow) + clipboard = QtGui.QApplication.clipboard() + clipboard.setText(fullStringOfAddresses) + def on_action_AddressBookSend(self): + listOfSelectedRows = {} + for i in range(len(self.ui.tableWidgetAddressBook.selectedIndexes())): + listOfSelectedRows[self.ui.tableWidgetAddressBook.selectedIndexes()[i].row()] = 0 + for currentRow in listOfSelectedRows: + 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)) + if listOfSelectedRows == {}: + self.statusBar().showMessage('No addresses selected.') + else: + self.statusBar().showMessage('') + self.ui.tabWidget.setCurrentIndex(1) + 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)) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''DELETE FROM subscriptions WHERE label=? AND address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + self.ui.tableWidgetSubscriptions.removeRow(currentRow) + self.rerenderInboxFromLabels() + shared.reloadBroadcastSendersForWhichImWatching() + 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_action_SubscriptionsEnable(self): + 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)) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''update subscriptions set enabled=1 WHERE label=? AND address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + self.ui.tableWidgetSubscriptions.item(currentRow,0).setTextColor(QtGui.QColor(0,0,0)) + self.ui.tableWidgetSubscriptions.item(currentRow,1).setTextColor(QtGui.QColor(0,0,0)) + shared.reloadBroadcastSendersForWhichImWatching() + def on_action_SubscriptionsDisable(self): + 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)) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''update subscriptions set enabled=0 WHERE label=? AND address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + self.ui.tableWidgetSubscriptions.item(currentRow,0).setTextColor(QtGui.QColor(128,128,128)) + self.ui.tableWidgetSubscriptions.item(currentRow,1).setTextColor(QtGui.QColor(128,128,128)) + shared.reloadBroadcastSendersForWhichImWatching() + def on_context_menuSubscriptions(self, point): + self.popMenuSubscriptions.exec_( self.ui.tableWidgetSubscriptions.mapToGlobal(point) ) + + #Group of functions for the Blacklist dialog box + def on_action_BlacklistNew(self): + self.click_pushButtonAddBlacklist() + def on_action_BlacklistDelete(self): + 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)) + shared.sqlLock.acquire() + if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': + shared.sqlSubmitQueue.put('''DELETE FROM blacklist WHERE label=? AND address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + else: + shared.sqlSubmitQueue.put('''DELETE FROM whitelist WHERE label=? AND address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.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),) + shared.sqlLock.acquire() + if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': + shared.sqlSubmitQueue.put('''UPDATE blacklist SET enabled=1 WHERE address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + else: + shared.sqlSubmitQueue.put('''UPDATE whitelist SET enabled=1 WHERE address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.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),) + shared.sqlLock.acquire() + if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': + shared.sqlSubmitQueue.put('''UPDATE blacklist SET enabled=0 WHERE address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + else: + shared.sqlSubmitQueue.put('''UPDATE whitelist SET enabled=0 WHERE address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + + #Group of functions for the Your Identities dialog box + def on_action_YourIdentitiesNew(self): + self.click_NewAddressDialog() + def on_action_YourIdentitiesEnable(self): + currentRow = self.ui.tableWidgetYourIdentities.currentRow() + addressAtCurrentRow = str(self.ui.tableWidgetYourIdentities.item(currentRow,1).text()) + shared.config.set(addressAtCurrentRow,'enabled','true') + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.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)) + if shared.safeConfigGetBoolean(addressAtCurrentRow,'mailinglist'): + self.ui.tableWidgetYourIdentities.item(currentRow,1).setTextColor(QtGui.QColor(137,04,177)) + shared.reloadMyAddressHashes() + def on_action_YourIdentitiesDisable(self): + currentRow = self.ui.tableWidgetYourIdentities.currentRow() + addressAtCurrentRow = str(self.ui.tableWidgetYourIdentities.item(currentRow,1).text()) + shared.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)) + if shared.safeConfigGetBoolean(addressAtCurrentRow,'mailinglist'): + self.ui.tableWidgetYourIdentities.item(currentRow,1).setTextColor(QtGui.QColor(137,04,177)) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.config.write(configfile) + shared.reloadMyAddressHashes() + def on_action_YourIdentitiesClipboard(self): + 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) ) + def on_context_menuSent(self, point): + self.popMenuSent.exec_( self.ui.tableWidgetSent.mapToGlobal(point) ) + + def tableWidgetInboxItemClicked(self): + currentRow = self.ui.tableWidgetInbox.currentRow() + if currentRow >= 0: + fromAddress = str(self.ui.tableWidgetInbox.item(currentRow,1).data(Qt.UserRole).toPyObject()) + #If we have received this message from either a broadcast address or from someone in our address book, display as HTML + if decodeAddress(fromAddress)[3] in shared.broadcastSendersForWhichImWatching or shared.isAddressInMyAddressBook(fromAddress): + if len(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) < 30000: + self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject())#Only show the first 30K characters + else: + self.ui.textEditInboxMessage.setText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()[:30000]+'\n\nDisplay of the remainder of the message truncated because it is too long.')#Only show the first 30K characters + else: + if len(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()) < 30000: + self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject())#Only show the first 30K characters + else: + self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow,2).data(Qt.UserRole).toPyObject()[:30000]+'\n\nDisplay of the remainder of the message truncated because it is too long.')#Only show the first 30K characters + + font = QFont() + font.setBold(False) + self.ui.tableWidgetInbox.item(currentRow,0).setFont(font) + self.ui.tableWidgetInbox.item(currentRow,1).setFont(font) + self.ui.tableWidgetInbox.item(currentRow,2).setFont(font) + self.ui.tableWidgetInbox.item(currentRow,3).setFont(font) + + inventoryHash = str(self.ui.tableWidgetInbox.item(currentRow,3).data(Qt.UserRole).toPyObject()) + t = (inventoryHash,) + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''update inbox set read=1 WHERE msgid=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + + def tableWidgetSentItemClicked(self): + currentRow = self.ui.tableWidgetSent.currentRow() + if currentRow >= 0: + self.ui.textEditSentMessage.setPlainText(self.ui.tableWidgetSent.item(currentRow,2).data(Qt.UserRole).toPyObject()) + + def tableWidgetYourIdentitiesItemChanged(self): + currentRow = self.ui.tableWidgetYourIdentities.currentRow() + if currentRow >= 0: + addressAtCurrentRow = self.ui.tableWidgetYourIdentities.item(currentRow,1).text() + shared.config.set(str(addressAtCurrentRow),'label',str(self.ui.tableWidgetYourIdentities.item(currentRow,0).text().toUtf8())) + with open(shared.appdata + 'keys.dat', 'wb') as configfile: + shared.config.write(configfile) + self.rerenderComboBoxSendFrom() + #self.rerenderInboxFromLabels() + self.rerenderInboxToLabels() + self.rerenderSentFromLabels() + #self.rerenderSentToLabels() + + def tableWidgetAddressBookItemChanged(self): + currentRow = self.ui.tableWidgetAddressBook.currentRow() + shared.sqlLock.acquire() + if currentRow >= 0: + addressAtCurrentRow = self.ui.tableWidgetAddressBook.item(currentRow,1).text() + t = (str(self.ui.tableWidgetAddressBook.item(currentRow,0).text().toUtf8()),str(addressAtCurrentRow)) + shared.sqlSubmitQueue.put('''UPDATE addressbook set label=? WHERE address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + self.rerenderInboxFromLabels() + self.rerenderSentToLabels() + + def tableWidgetSubscriptionsItemChanged(self): + currentRow = self.ui.tableWidgetSubscriptions.currentRow() + shared.sqlLock.acquire() + if currentRow >= 0: + addressAtCurrentRow = self.ui.tableWidgetSubscriptions.item(currentRow,1).text() + t = (str(self.ui.tableWidgetSubscriptions.item(currentRow,0).text().toUtf8()),str(addressAtCurrentRow)) + shared.sqlSubmitQueue.put('''UPDATE subscriptions set label=? WHERE address=?''') + shared.sqlSubmitQueue.put(t) + shared.sqlReturnQueue.get() + shared.sqlSubmitQueue.put('commit') + shared.sqlLock.release() + self.rerenderInboxFromLabels() + self.rerenderSentToLabels() + + def writeNewAddressToTable(self,label,address,streamNumber): + self.ui.tableWidgetYourIdentities.setSortingEnabled(False) + 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.ui.tableWidgetYourIdentities.setSortingEnabled(True) + self.rerenderComboBoxSendFrom() + + def updateStatusBar(self,data): + if data != "": + shared.printLock.acquire() + print 'Status bar:', data + shared.printLock.release() + self.statusBar().showMessage(data) + + + + + +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) + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + +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) + +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)) + +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(shared.config.getboolean('bitmessagesettings', 'startonlogon')) + self.ui.checkBoxMinimizeToTray.setChecked(shared.config.getboolean('bitmessagesettings', 'minimizetotray')) + self.ui.checkBoxShowTrayNotifications.setChecked(shared.config.getboolean('bitmessagesettings', 'showtraynotifications')) + self.ui.checkBoxStartInTray.setChecked(shared.config.getboolean('bitmessagesettings', 'startintray')) + if shared.appdata == '': + self.ui.checkBoxPortableMode.setChecked(True) + if 'darwin' in sys.platform: + self.ui.checkBoxStartOnLogon.setDisabled(True) + 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 aren\'t applicable or because they haven\'t yet been implimented for your operating system.') + elif 'linux' in sys.platform: + self.ui.checkBoxStartOnLogon.setDisabled(True) + self.ui.checkBoxMinimizeToTray.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.') + #On the Network settings tab: + self.ui.lineEditTCPPort.setText(str(shared.config.get('bitmessagesettings', 'port'))) + self.ui.checkBoxAuthentication.setChecked(shared.config.getboolean('bitmessagesettings', 'socksauthentication')) + if str(shared.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(shared.config.get('bitmessagesettings', 'socksproxytype')) == 'SOCKS4a': + self.ui.comboBoxProxyType.setCurrentIndex(1) + self.ui.lineEditTCPPort.setEnabled(False) + elif str(shared.config.get('bitmessagesettings', 'socksproxytype')) == 'SOCKS5': + self.ui.comboBoxProxyType.setCurrentIndex(2) + self.ui.lineEditTCPPort.setEnabled(False) + + self.ui.lineEditSocksHostname.setText(str(shared.config.get('bitmessagesettings', 'sockshostname'))) + self.ui.lineEditSocksPort.setText(str(shared.config.get('bitmessagesettings', 'socksport'))) + self.ui.lineEditSocksUsername.setText(str(shared.config.get('bitmessagesettings', 'socksusername'))) + self.ui.lineEditSocksPassword.setText(str(shared.config.get('bitmessagesettings', 'sockspassword'))) + QtCore.QObject.connect(self.ui.comboBoxProxyType, QtCore.SIGNAL("currentIndexChanged(int)"), self.comboBoxProxyTypeChanged) + + self.ui.lineEditTotalDifficulty.setText(str((float(shared.config.getint('bitmessagesettings', 'defaultnoncetrialsperbyte'))/networkDefaultProofOfWorkNonceTrialsPerByte))) + self.ui.lineEditSmallMessageDifficulty.setText(str((float(shared.config.getint('bitmessagesettings', 'defaultpayloadlengthextrabytes'))/networkDefaultPayloadLengthExtraBytes))) + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + + 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) + +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()) + if shared.safeConfigGetBoolean(addressAtCurrentRow,'mailinglist'): + self.ui.radioButtonBehaviorMailingList.click() + else: + self.ui.radioButtonBehaveNormalAddress.click() + try: + mailingListName = shared.config.get(addressAtCurrentRow, 'mailinglistname') + except: + mailingListName = '' + self.ui.lineEditMailingListName.setText(unicode(mailingListName,'utf-8')) + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + +class NewSubscriptionDialog(QtGui.QDialog): + def __init__(self,parent): + QtGui.QWidget.__init__(self, parent) + self.ui = Ui_NewSubscriptionDialog() + self.ui.setupUi(self) + 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 == 'ripetooshort': + self.ui.labelSubscriptionAddressCheck.setText('Some data encoded in the address is too short.') + elif status == 'ripetoolong': + self.ui.labelSubscriptionAddressCheck.setText('Some data encoded in the address is too long.') + elif status == 'success': + self.ui.labelSubscriptionAddressCheck.setText('Address is valid.') + +class NewAddressDialog(QtGui.QDialog): + def __init__(self, parent): + QtGui.QWidget.__init__(self, parent) + self.ui = Ui_NewAddressDialog() + self.ui.setupUi(self) + self.parent = parent + row = 1 + #Let's fill out the 'existing address' combo box with addresses from the 'Your Identities' tab. + 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 + self.ui.groupBoxDeterministic.setHidden(True) + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + +class iconGlossaryDialog(QtGui.QDialog): + def __init__(self,parent): + QtGui.QWidget.__init__(self, parent) + self.ui = Ui_iconGlossaryDialog() + self.ui.setupUi(self) + self.parent = parent + self.ui.labelPortNumber.setText('You are using TCP port ' + str(shared.config.getint('bitmessagesettings', 'port')) + '. (This can be changed in the settings).') + QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) + + +#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()) + + +class UISignaler(QThread): + def __init__(self, parent = None): + QThread.__init__(self, parent) + + def run(self): + while True: + command, data = shared.UISignalQueue.get() + if command == 'writeNewAddressToTable': + label, address, streamNumber = data + self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),label,address,str(streamNumber)) + elif command == 'updateStatusBar': + self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),data) + elif command == 'updateSentItemStatusByHash': + hash, message = data + self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),hash,message) + elif command == 'updateSentItemStatusByAckdata': + ackData, message = data + self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackData,message) + elif command == 'displayNewInboxMessage': + inventoryHash,toAddress,fromAddress,subject,body = data + self.emit(SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),inventoryHash,toAddress,fromAddress,subject,body) + elif command == 'displayNewSentMessage': + toAddress,fromLabel,fromAddress,subject,message,ackdata = data + self.emit(SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),toAddress,fromLabel,fromAddress,subject,message,ackdata) + elif command == 'updateNetworkStatusTab': + streamNumber,count = data + self.emit(SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"),streamNumber,count) + elif command == 'incrementNumberOfMessagesProcessed': + self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) + elif command == 'incrementNumberOfPubkeysProcessed': + self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()")) + elif command == 'incrementNumberOfBroadcastsProcessed': + self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()")) + elif command == 'setStatusIcon': + self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),data) + else: + sys.stderr.write('Command sent to UISignaler not recognized: %s\n' % command) + +def run(): + app = QtGui.QApplication(sys.argv) + app.setStyleSheet("QStatusBar::item { border: 0px solid black }") + myapp = MyForm() + myapp.show() + sys.exit(app.exec_()) \ No newline at end of file diff --git a/src/shared.py b/src/shared.py new file mode 100644 index 00000000..3c13a459 --- /dev/null +++ b/src/shared.py @@ -0,0 +1,185 @@ +import threading + +import sys +from addresses import * +import highlevelcrypto +import Queue +import pickle + +myECCryptorObjects = {} +MyECSubscriptionCryptorObjects = {} +myAddressesByHash = {} #The key in this dictionary is the RIPE hash which is encoded in an address and value is the address itself. +broadcastSendersForWhichImWatching = {} +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() +UISignalQueue = Queue.Queue() +addressGeneratorQueue = Queue.Queue() +knownNodesLock = threading.Lock() +knownNodes = {} +sendDataQueues = [] #each sendData thread puts its queue in this list. +inventory = {} #of objects (like msg payloads and pubkey payloads) Does not include protocol headers (the first 24 bytes of each packet). +inventoryLock = threading.Lock() #Guarantees that two receiveDataThreads don't receive and process the same message concurrently (probably sent by a malicious individual) +printLock = threading.Lock() +appdata = '' #holds the location of the application data storage directory +statusIconColor = 'red' + +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 + +def isAddressInMyAddressBook(address): + t = (address,) + sqlLock.acquire() + sqlSubmitQueue.put('''select address from addressbook where address=?''') + sqlSubmitQueue.put(t) + queryreturn = sqlReturnQueue.get() + sqlLock.release() + return queryreturn != [] + +def isAddressInMyAddressBookSubscriptionsListOrWhitelist(address): + if isAddressInMyAddressBook(address): + return True + + sqlLock.acquire() + sqlSubmitQueue.put('''SELECT address FROM whitelist where address=? and enabled = '1' ''') + sqlSubmitQueue.put((address,)) + queryreturn = sqlReturnQueue.get() + sqlLock.release() + if queryreturn <> []: + return True + + sqlLock.acquire() + sqlSubmitQueue.put('''select address from subscriptions where address=? and enabled = '1' ''') + sqlSubmitQueue.put((address,)) + queryreturn = sqlReturnQueue.get() + sqlLock.release() + if queryreturn <> []: + return True + return False + +def safeConfigGetBoolean(section,field): + try: + return config.getboolean(section,field) + except: + return False + +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() + print 'reloading keys from keys.dat file' + printLock.release() + myECCryptorObjects.clear() + myAddressesByHash.clear() + #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 or addressVersionNumber == 3: + 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 + myECCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey) + myAddressesByHash[hash] = addressInKeysFile + else: + sys.stderr.write('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2 or 3.\n') + +def reloadBroadcastSendersForWhichImWatching(): + broadcastSendersForWhichImWatching.clear() + MyECSubscriptionCryptorObjects.clear() + sqlLock.acquire() + sqlSubmitQueue.put('SELECT address FROM subscriptions where enabled=1') + sqlSubmitQueue.put('') + queryreturn = sqlReturnQueue.get() + sqlLock.release() + for row in queryreturn: + address, = row + status,addressVersionNumber,streamNumber,hash = decodeAddress(address) + if addressVersionNumber == 2: + broadcastSendersForWhichImWatching[hash] = 0 + #Now, for all addresses, even version 2 addresses, we should create Cryptor objects in a dictionary which we will use to attempt to decrypt encrypted broadcast messages. + privEncryptionKey = hashlib.sha512(encodeVarint(addressVersionNumber)+encodeVarint(streamNumber)+hash).digest()[:32] + MyECSubscriptionCryptorObjects[hash] = highlevelcrypto.makeCryptor(privEncryptionKey.encode('hex')) + +def doCleanShutdown(): + knownNodesLock.acquire() + UISignalQueue.put(('updateStatusBar','Saving the knownNodes list of peers to disk...')) + output = open(appdata + 'knownnodes.dat', 'wb') + print 'finished opening knownnodes.dat. Now pickle.dump' + pickle.dump(knownNodes, output) + print 'Completed pickle.dump. Closing output...' + output.close() + knownNodesLock.release() + print 'Finished closing knownnodes.dat output file.' + UISignalQueue.put(('updateStatusBar','Done saving the knownNodes list of peers to disk.')) + + broadcastToSendDataQueues((0, 'shutdown', 'all')) + + printLock.acquire() + print 'Flushing inventory in memory out to disk...' + printLock.release() + UISignalQueue.put(('updateStatusBar','Flushing inventory in memory out to disk. This should normally only take a second...')) + flushInventory() + + #This one last useless query will guarantee that the previous flush committed before we close the program. + sqlLock.acquire() + sqlSubmitQueue.put('SELECT address FROM subscriptions') + sqlSubmitQueue.put('') + sqlReturnQueue.get() + sqlLock.release() + print 'Finished flushing inventory.' + sqlSubmitQueue.put('exit') + + if safeConfigGetBoolean('bitmessagesettings','daemon'): + printLock.acquire() + print 'Done.' + printLock.release() + os._exit(0) + +#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] + sqlSubmitQueue.put('commit') + sqlLock.release() \ No newline at end of file From 9a64c265a093b9fdf8a839eca466ba3f34125eaf Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 2 May 2013 12:47:43 -0400 Subject: [PATCH 23/33] Continued daemon mode implementation --- src/bitmessagemain.py | 63 ++++++++++++++++++++++--------------------- src/shared.py | 1 + 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 0f1a93df..c2c536f8 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -2313,7 +2313,7 @@ class sqlThread(threading.Thread): shared.config.set('bitmessagesettings','settingsversion','3') with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) #People running earlier versions of PyBitmessage do not have the encodingtype field in their inbox and sent tables or the read field in the inbox table. Let's add them. if shared.config.getint('bitmessagesettings','settingsversion') == 3: @@ -2332,14 +2332,14 @@ class sqlThread(threading.Thread): shared.config.set('bitmessagesettings','settingsversion','4') with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) if shared.config.getint('bitmessagesettings','settingsversion') == 4: shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) shared.config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) shared.config.set('bitmessagesettings','settingsversion','5') with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) #From now on, let us keep a 'version' embedded in the messages.dat file so that when we make changes to the database, the database version we are on can stay embedded in the messages.dat file. Let us check to see if the settings table exists yet. item = '''SELECT name FROM sqlite_master WHERE type='table' AND name='settings';''' @@ -2617,7 +2617,7 @@ class singleWorker(threading.Thread): def doPOWForMyV2Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW #Look up my stream number based on my address hash - """configSections = config.sections() + """configSections = shared.config.sections() for addressInKeysFile in configSections: if addressInKeysFile <> 'bitmessagesettings': status,addressVersionNumber,streamNumber,hashFromThisParticularAddress = decodeAddress(addressInKeysFile) @@ -2681,7 +2681,7 @@ class singleWorker(threading.Thread): shared.UISignalQueue.put(('updateStatusBar','')) shared.config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) def doPOWForMyV3Pubkey(self,hash): #This function also broadcasts out the pubkey message once it is done with the POW myAddress = shared.myAddressesByHash[hash] @@ -2747,7 +2747,7 @@ class singleWorker(threading.Thread): shared.UISignalQueue.put(('updateStatusBar','')) shared.config.set(myAddress,'lastpubkeysendtime',str(int(time.time()))) with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) def sendBroadcast(self): shared.sqlLock.acquire() @@ -3160,7 +3160,9 @@ class addressGenerator(threading.Thread): def run(self): while True: - addressVersionNumber,streamNumber,label,numberOfAddressesToMake,deterministicPassphrase,eighteenByteRipe, = shared.addressGeneratorQueue.get() + addressVersionNumber,streamNumber,label,numberOfAddressesToMake,deterministicPassphrase,eighteenByteRipe = shared.addressGeneratorQueue.get() + if addressVersionNumber < 3 or addressVersionNumber > 3: + sys.stderr.write('Program error: For some reason the address generator queue has been given a request to create version', addressVersionNumber,' addresses which it cannot do.\n') if addressVersionNumber == 3: if deterministicPassphrase == "": #statusbar = 'Generating one new address' @@ -3207,7 +3209,7 @@ class addressGenerator(threading.Thread): privEncryptionKeyWIF = arithmetic.changebase(privEncryptionKey + checksum,256,58) #print 'privEncryptionKeyWIF',privEncryptionKeyWIF - config.add_section(address) + shared.config.add_section(address) shared.config.set(address,'label',label) shared.config.set(address,'enabled','true') shared.config.set(address,'decoy','false') @@ -3216,7 +3218,7 @@ class addressGenerator(threading.Thread): shared.config.set(address,'privSigningKey',privSigningKeyWIF) shared.config.set(address,'privEncryptionKey',privEncryptionKeyWIF) with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) #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) @@ -3280,7 +3282,7 @@ class addressGenerator(threading.Thread): privEncryptionKeyWIF = arithmetic.changebase(privEncryptionKey + checksum,256,58) try: - config.add_section(address) + shared.config.add_section(address) print 'label:', label shared.config.set(address,'label',label) shared.config.set(address,'enabled','true') @@ -3290,7 +3292,7 @@ class addressGenerator(threading.Thread): shared.config.set(address,'privSigningKey',privSigningKeyWIF) shared.config.set(address,'privEncryptionKey',privEncryptionKeyWIF) with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) #self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.label,address,str(self.streamNumber)) shared.UISignalQueue.put(('writeNewAddressToTable',(label,address,str(streamNumber)))) @@ -3405,7 +3407,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): shared.UISignalQueue.put(('updateStatusBar',message)) elif method == 'listAddresses': data = '{"addresses":[' - configSections = config.sections() + configSections = shared.config.sections() for addressInKeysFile in configSections: if addressInKeysFile <> 'bitmessagesettings': status,addressVersionNumber,streamNumber,hash = decodeAddress(addressInKeysFile) @@ -3456,9 +3458,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'API Error 0001: the specified passphrase is blank.' passphrase = passphrase.decode('base64') if addressVersionNumber == 0: #0 means "just use the proper addressVersionNumber" - addressVersionNumber == 2 - if addressVersionNumber != 2: - return 'API Error 0002: the address version number currently must be 2 (or 0 which means auto-select). Others aren\'t supported.' + addressVersionNumber = 3 + if addressVersionNumber != 3: + return 'API Error 0002: the address version number currently must be 3 (or 0 which means auto-select).', addressVersionNumber,' isn\'t supported.' if streamNumber == 0: #0 means "just use the most available stream" streamNumber = 1 if streamNumber != 1: @@ -3468,7 +3470,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): if numberOfAddresses > 9999: 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.' apiAddressGeneratorReturnQueue.queue.clear() - print 'about to send numberOfAddresses', numberOfAddresses + print 'Requesting that the addressGenerator create', numberOfAddresses, 'addresses.' #apiSignalQueue.put(('createDeterministicAddresses',(passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe))) shared.addressGeneratorQueue.put((addressVersionNumber,streamNumber,'unused API address',numberOfAddresses,passphrase,eighteenByteRipe)) data = '{"addresses":[' @@ -3530,8 +3532,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'API Error 0009: Invalid characters in address: '+ toAddress if status == 'versiontoohigh': return 'API Error 0010: Address version number too high (or zero) in address: ' + toAddress - if addressVersionNumber != 2: - return 'API Error 0011: the address version number currently must be 2. Others aren\'t supported. Check the toAddress.' + if addressVersionNumber < 2 or addressVersionNumber > 3: + return 'API Error 0011: the address version number currently must be 2 or 3. Others aren\'t supported. Check the toAddress.' if streamNumber != 1: return 'API Error 0012: the stream number must be 1. Others aren\'t supported. Check the toAddress.' status,addressVersionNumber,streamNumber,fromRipe = decodeAddress(fromAddress) @@ -3545,8 +3547,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'API Error 0009: Invalid characters in address: '+ fromAddress if status == 'versiontoohigh': return 'API Error 0010: Address version number too high (or zero) in address: ' + fromAddress - if addressVersionNumber != 2: - return 'API Error 0011: the address version number currently must be 2. Others aren\'t supported. Check the fromAddress.' + if addressVersionNumber < 2 or addressVersionNumber > 3: + return 'API Error 0011: the address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.' if streamNumber != 1: return 'API Error 0012: the stream number must be 1. Others aren\'t supported. Check the fromAddress.' toAddress = addBMIfNotPresent(toAddress) @@ -3577,7 +3579,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): if queryreturn <> []: for row in queryreturn: toLabel, = row - apiSignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) + #apiSignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) + shared.UISignalQueue.put(('displayNewSentMessage',(toAddress,toLabel,fromAddress,subject,message,ackdata))) shared.workerQueue.put(('sendmessage',toAddress)) @@ -3726,10 +3729,10 @@ if __name__ == "__main__": sys.exit() #First try to load the config file (the keys.dat file) from the program directory - config = ConfigParser.SafeConfigParser() - config.read('keys.dat') + shared.config = ConfigParser.SafeConfigParser() + shared.config.read('keys.dat') try: - config.get('bitmessagesettings', 'settingsversion') + shared.config.get('bitmessagesettings', 'settingsversion') #settingsFileExistsInProgramDirectory = True print 'Loading config files from same directory as program' shared.appdata = '' @@ -3792,7 +3795,7 @@ if __name__ == "__main__": shared.config.set('bitmessagesettings','keysencrypted','false') shared.config.set('bitmessagesettings','messagesencrypted','false') with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) #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: @@ -3800,9 +3803,9 @@ if __name__ == "__main__": #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') + shared.config.remove_option('bitmessagesettings', 'movemessagstoprog') with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) except: pass try: @@ -3810,9 +3813,9 @@ if __name__ == "__main__": #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') + shared.config.remove_option('bitmessagesettings', 'movemessagstoappdata') with open(shared.appdata + 'keys.dat', 'wb') as configfile: - config.write(configfile) + shared.config.write(configfile) except: pass @@ -3827,7 +3830,6 @@ if __name__ == "__main__": pickleFile = open(shared.appdata + 'knownnodes.dat', 'rb') shared.knownNodes = pickle.load(pickleFile) pickleFile.close() - print 'at line 3769, knownNodes is', shared.knownNodes if shared.config.getint('bitmessagesettings', 'settingsversion') > 5: print 'Bitmessage cannot read future versions of the keys file (keys.dat). Run the newer version of Bitmessage.' raise SystemExit @@ -3848,7 +3850,6 @@ if __name__ == "__main__": print 'bootstrap8444.bitmessage.org DNS bootstrapping failed.' else: print 'DNS bootstrap skipped because SOCKS is used.' - print 'at line 3790, knownNodes is', shared.knownNodes #Start the address generation thread addressGeneratorThread = addressGenerator() addressGeneratorThread.daemon = True # close the main program even if there are threads left diff --git a/src/shared.py b/src/shared.py index 3c13a459..31e3e1ff 100644 --- a/src/shared.py +++ b/src/shared.py @@ -5,6 +5,7 @@ from addresses import * import highlevelcrypto import Queue import pickle +import os myECCryptorObjects = {} MyECSubscriptionCryptorObjects = {} From c5d4f50dbd92a2983babac2038a02da3fa48437b Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 2 May 2013 14:18:24 -0400 Subject: [PATCH 24/33] More daemon related changes --- src/bitmessageqt/__init__.py | 18 ++++++++++++++++++ src/shared.py | 3 +++ 2 files changed, 21 insertions(+) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index f6b80495..6edf4f82 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -222,9 +222,11 @@ class MyForm(QtGui.QMainWindow): font = QFont() font.setBold(True) #Load inbox from messages database file + shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''SELECT msgid, toaddress, fromaddress, subject, received, message, read FROM inbox where folder='inbox' ORDER BY received''') shared.sqlSubmitQueue.put('') queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: msgid, toAddress, fromAddress, subject, received, message, read = row @@ -240,9 +242,11 @@ class MyForm(QtGui.QMainWindow): fromLabel = '' t = (fromAddress,) + shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') shared.sqlSubmitQueue.put(t) queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn <> []: for row in queryreturn: @@ -250,9 +254,11 @@ class MyForm(QtGui.QMainWindow): if fromLabel == '': #If this address wasn't in our address book.. t = (fromAddress,) + shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''select label from subscriptions where address=?''') shared.sqlSubmitQueue.put(t) queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn <> []: for row in queryreturn: @@ -294,9 +300,11 @@ class MyForm(QtGui.QMainWindow): self.ui.tableWidgetInbox.keyPressEvent = self.tableWidgetInboxKeyPressEvent #Load Sent items from database + shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''SELECT toaddress, fromaddress, subject, message, status, ackdata, lastactiontime FROM sent where folder = 'sent' ORDER BY lastactiontime''') shared.sqlSubmitQueue.put('') queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: toAddress, fromAddress, subject, message, status, ackdata, lastactiontime = row try: @@ -308,9 +316,11 @@ class MyForm(QtGui.QMainWindow): toLabel = '' t = (toAddress,) + shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') shared.sqlSubmitQueue.put(t) queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn <> []: for row in queryreturn: @@ -356,9 +366,11 @@ class MyForm(QtGui.QMainWindow): self.ui.tableWidgetSent.sortItems(3,Qt.DescendingOrder) #Initialize the address book + shared.sqlLock.acquire() shared.sqlSubmitQueue.put('SELECT * FROM addressbook') shared.sqlSubmitQueue.put('') queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: label, address = row self.ui.tableWidgetAddressBook.insertRow(0) @@ -369,9 +381,11 @@ class MyForm(QtGui.QMainWindow): self.ui.tableWidgetAddressBook.setItem(0,1,newItem) #Initialize the Subscriptions + shared.sqlLock.acquire() shared.sqlSubmitQueue.put('SELECT label, address, enabled FROM subscriptions') shared.sqlSubmitQueue.put('') queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: label, address, enabled = row self.ui.tableWidgetSubscriptions.insertRow(0) @@ -656,9 +670,11 @@ class MyForm(QtGui.QMainWindow): addressToLookup = str(self.ui.tableWidgetSent.item(i,0).data(Qt.UserRole).toPyObject()) toLabel = '' t = (addressToLookup,) + shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''select label from addressbook where address=?''') shared.sqlSubmitQueue.put(t) queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() if queryreturn <> []: for row in queryreturn: @@ -1035,12 +1051,14 @@ class MyForm(QtGui.QMainWindow): def loadBlackWhiteList(self): #Initialize the Blacklist or Whitelist table listType = shared.config.get('bitmessagesettings', 'blackwhitelist') + shared.sqlLock.acquire() if listType == 'black': shared.sqlSubmitQueue.put('''SELECT label, address, enabled FROM blacklist''') else: shared.sqlSubmitQueue.put('''SELECT label, address, enabled FROM whitelist''') shared.sqlSubmitQueue.put('') queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() for row in queryreturn: label, address, enabled = row self.ui.tableWidgetBlacklist.insertRow(0) diff --git a/src/shared.py b/src/shared.py index 31e3e1ff..681ee970 100644 --- a/src/shared.py +++ b/src/shared.py @@ -115,6 +115,9 @@ def reloadMyAddressHashes(): sys.stderr.write('Error in reloadMyAddressHashes: Can\'t handle address versions other than 2 or 3.\n') def reloadBroadcastSendersForWhichImWatching(): + printLock.acquire() + print 'reloading subscriptions...' + printLock.release() broadcastSendersForWhichImWatching.clear() MyECSubscriptionCryptorObjects.clear() sqlLock.acquire() From de59b4adf17ecda10f3c96b9255de444e8a37ddf Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 2 May 2013 15:39:51 -0400 Subject: [PATCH 25/33] API-related changes --- src/api client.py | 2 +- src/bitmessagemain.py | 22 ++-------------------- src/defaultKnownNodes.py | 8 ++++---- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/src/api client.py b/src/api client.py index 9d33d69e..f3feb098 100644 --- a/src/api client.py +++ b/src/api client.py @@ -30,7 +30,7 @@ print 'Uncomment the next two lines to create a new random address.' print 'Uncomment these next four lines to create new deterministic addresses.' #passphrase = 'asdfasdfqwer'.encode('base64') -#jsonDeterministicAddresses = api.createDeterministicAddresses(passphrase, 2, 2, 1, False) +#jsonDeterministicAddresses = api.createDeterministicAddresses(passphrase, 2, 3, 1, False) #print jsonDeterministicAddresses #print json.loads(jsonDeterministicAddresses) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index c2c536f8..1ec4788e 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -3610,8 +3610,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): return 'API Error 0009: Invalid characters in address: '+ fromAddress if status == 'versiontoohigh': return 'API Error 0010: Address version number too high (or zero) in address: ' + fromAddress - if addressVersionNumber != 2: - return 'API Error 0011: the address version number currently must be 2. Others aren\'t supported. Check the fromAddress.' + if addressVersionNumber < 2 or addressVersionNumber > 3: + return 'API Error 0011: the address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.' if streamNumber != 1: return 'API Error 0012: the stream number must be 1. Others aren\'t supported. Check the fromAddress.' fromAddress = addBMIfNotPresent(fromAddress) @@ -3653,14 +3653,6 @@ class singleAPI(threading.Thread): 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): @@ -3691,18 +3683,12 @@ class singleAPI(threading.Thread): self.emit(SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),toAddress,toLabel,fromAddress,subject,message,ackdata)""" - - selfInitiatedConnections = {} #This is a list of current connections (the thread pointers at least) alreadyAttemptedConnectionsList = {} #This is a list of nodes to which we have already attempted a connection - - ackdataForWhichImWatching = {} - connectionsCount = {} #Used for the 'network status' tab. connectionsCountLock = threading.Lock() alreadyAttemptedConnectionsListLock = threading.Lock() - eightBytesOfRandomDataUsedToDetectConnectionsToSelf = pack('>Q',random.randrange(1, 18446744073709551615)) connectedHostsList = {} #List of hosts to which we are connected. Used to guarantee that the outgoingSynSender threads won't connect to the same remote node twice. neededPubkeys = {} @@ -3711,7 +3697,6 @@ successfullyDecryptMessageTimings = [] #A list of the amounts of time it took to apiAddressGeneratorReturnQueue = Queue.Queue() #The address generator thread uses this queue to get information back to the API thread. alreadyAttemptedConnectionsListResetTime = int(time.time()) #used to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect. - #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! networkDefaultProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. networkDefaultPayloadLengthExtraBytes = 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. @@ -3919,7 +3904,6 @@ if __name__ == "__main__": import bitmessageqt bitmessageqt.run() - if shared.config.getboolean('bitmessagesettings', 'startintray'): myapp.hide() myapp.trayIcon.show() @@ -3928,8 +3912,6 @@ if __name__ == "__main__": #self.hide() if 'win32' in sys.platform or 'win64' in sys.platform: myapp.setWindowFlags(Qt.ToolTip) - - else: print 'Running as a daemon. You can use Ctrl+C to exit.' while True: diff --git a/src/defaultKnownNodes.py b/src/defaultKnownNodes.py index 23570662..0006fba5 100644 --- a/src/defaultKnownNodes.py +++ b/src/defaultKnownNodes.py @@ -10,11 +10,11 @@ def createDefaultKnownNodes(appdata): ############## Stream 1 ################ stream1 = {} - stream1['84.48.88.42'] = (8444,int(time.time())) + stream1['98.28.255.178'] = (8444,int(time.time())) stream1['66.65.120.151'] = (8080,int(time.time())) - stream1['76.180.233.38'] = (8444,int(time.time())) - stream1['74.132.73.137'] = (8444,int(time.time())) - stream1['60.242.109.18'] = (8444,int(time.time())) + stream1['87.236.30.170'] = (8444,int(time.time())) + stream1['163.118.75.118'] = (8444,int(time.time())) + stream1['88.130.32.123'] = (8444,int(time.time())) ############# Stream 2 ################# stream2 = {} From cbca738524ebcc08bba3e15e36907d80e4f0a2d7 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 2 May 2013 15:59:10 -0400 Subject: [PATCH 26/33] further deamon-related changes --- src/bitmessagemain.py | 63 ++++++++++++++---------------------- src/bitmessageqt/__init__.py | 18 ++++++++--- src/shared.py | 4 +++ 3 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 1ec4788e..76dd0d7d 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -410,10 +410,10 @@ class receiveDataThread(threading.Thread): self.data = self.data[self.payloadLength+24:] def isProofOfWorkSufficient(self,data,nonceTrialsPerByte=0,payloadLengthExtraBytes=0): - if nonceTrialsPerByte < networkDefaultProofOfWorkNonceTrialsPerByte: - nonceTrialsPerByte = networkDefaultProofOfWorkNonceTrialsPerByte - if payloadLengthExtraBytes < networkDefaultPayloadLengthExtraBytes: - payloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes + if nonceTrialsPerByte < shared.networkDefaultProofOfWorkNonceTrialsPerByte: + nonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte + if payloadLengthExtraBytes < shared.networkDefaultPayloadLengthExtraBytes: + payloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes POW, = unpack('>Q',hashlib.sha512(hashlib.sha512(data[:8]+ hashlib.sha512(data[8:]).digest()).digest()).digest()[0:8]) #print 'POW:', POW return POW <= 2**64 / ((len(data)+payloadLengthExtraBytes) * (nonceTrialsPerByte)) @@ -2335,8 +2335,8 @@ class sqlThread(threading.Thread): shared.config.write(configfile) if shared.config.getint('bitmessagesettings','settingsversion') == 4: - shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) - shared.config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) + shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(shared.networkDefaultProofOfWorkNonceTrialsPerByte)) + shared.config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(shared.networkDefaultPayloadLengthExtraBytes)) shared.config.set('bitmessagesettings','settingsversion','5') with open(shared.appdata + 'keys.dat', 'wb') as configfile: shared.config.write(configfile) @@ -2652,7 +2652,7 @@ class singleWorker(threading.Thread): #Do the POW for this pubkey message nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) print '(For pubkey message) Doing proof of work...' initialHash = hashlib.sha512(payload).digest() while trialValue > target: @@ -2718,7 +2718,7 @@ class singleWorker(threading.Thread): #Do the POW for this pubkey message nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) print '(For pubkey message) Doing proof of work...' initialHash = hashlib.sha512(payload).digest() while trialValue > target: @@ -2793,7 +2793,7 @@ class singleWorker(threading.Thread): nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) print '(For broadcast message) Doing proof of work...' #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) @@ -2864,7 +2864,7 @@ class singleWorker(threading.Thread): nonce = 0 trialValue = 99999999999999999999 - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) print '(For broadcast message) Doing proof of work...' #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) @@ -2984,8 +2984,8 @@ class singleWorker(threading.Thread): payload += pubEncryptionKey[1:] #If the receiver of our message is in our address book, subscriptions list, or whitelist then we will allow them to do the network-minimum proof of work. Let us check to see if the receiver is in any of those lists. if shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(toaddress): - payload += encodeVarint(networkDefaultProofOfWorkNonceTrialsPerByte) - payload += encodeVarint(networkDefaultPayloadLengthExtraBytes) + payload += encodeVarint(shared.networkDefaultProofOfWorkNonceTrialsPerByte) + payload += encodeVarint(shared.networkDefaultPayloadLengthExtraBytes) else: payload += encodeVarint(shared.config.getint(fromaddress,'noncetrialsperbyte')) payload += encodeVarint(shared.config.getint(fromaddress,'payloadlengthextrabytes')) @@ -3032,17 +3032,17 @@ class singleWorker(threading.Thread): pubEncryptionKeyBase256 = pubkeyPayload[readPosition:readPosition+64] readPosition += 64 if toAddressVersionNumber == 2: - requiredAverageProofOfWorkNonceTrialsPerByte = networkDefaultProofOfWorkNonceTrialsPerByte - requiredPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes + requiredAverageProofOfWorkNonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte + requiredPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes elif toAddressVersionNumber == 3: requiredAverageProofOfWorkNonceTrialsPerByte, varintLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10]) readPosition += varintLength requiredPayloadLengthExtraBytes, varintLength = decodeVarint(pubkeyPayload[readPosition:readPosition+10]) readPosition += varintLength - if requiredAverageProofOfWorkNonceTrialsPerByte < networkDefaultProofOfWorkNonceTrialsPerByte: #We still have to meet a minimum POW difficulty regardless of what they say is allowed in order to get our message to propagate through the network. - requiredAverageProofOfWorkNonceTrialsPerByte = networkDefaultProofOfWorkNonceTrialsPerByte - if requiredPayloadLengthExtraBytes < networkDefaultPayloadLengthExtraBytes: - requiredPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes + if requiredAverageProofOfWorkNonceTrialsPerByte < shared.networkDefaultProofOfWorkNonceTrialsPerByte: #We still have to meet a minimum POW difficulty regardless of what they say is allowed in order to get our message to propagate through the network. + requiredAverageProofOfWorkNonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte + if requiredPayloadLengthExtraBytes < shared.networkDefaultPayloadLengthExtraBytes: + requiredPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes encrypted = highlevelcrypto.encrypt(payload,"04"+pubEncryptionKeyBase256.encode('hex')) nonce = 0 @@ -3051,7 +3051,7 @@ class singleWorker(threading.Thread): payload = embeddedTime + encodeVarint(toStreamNumber) + encrypted target = 2**64 / ((len(payload)+requiredPayloadLengthExtraBytes+8) * requiredAverageProofOfWorkNonceTrialsPerByte) shared.printLock.acquire() - print '(For msg message) Doing proof of work. Total required difficulty:', float(requiredAverageProofOfWorkNonceTrialsPerByte)/networkDefaultProofOfWorkNonceTrialsPerByte,'Required small message difficulty:', float(requiredPayloadLengthExtraBytes)/networkDefaultPayloadLengthExtraBytes + print '(For msg message) Doing proof of work. Total required difficulty:', float(requiredAverageProofOfWorkNonceTrialsPerByte)/shared.networkDefaultProofOfWorkNonceTrialsPerByte,'Required small message difficulty:', float(requiredPayloadLengthExtraBytes)/shared.networkDefaultPayloadLengthExtraBytes shared.printLock.release() powStartTime = time.time() initialHash = hashlib.sha512(payload).digest() @@ -3105,7 +3105,7 @@ class singleWorker(threading.Thread): #self.emit(SIGNAL("updateSentItemStatusByHash(PyQt_PyObject,PyQt_PyObject)"),ripe,'Doing work necessary to request public key.') shared.UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Doing work necessary to request public key.'))) print 'Doing proof-of-work necessary to send getpubkey message.' - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) initialHash = hashlib.sha512(payload).digest() while trialValue > target: nonce += 1 @@ -3130,7 +3130,7 @@ class singleWorker(threading.Thread): nonce = 0 trialValue = 99999999999999999999 payload = embeddedTime + encodeVarint(toStreamNumber) + ackdata - target = 2**64 / ((len(payload)+networkDefaultPayloadLengthExtraBytes+8) * networkDefaultProofOfWorkNonceTrialsPerByte) + target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) shared.printLock.acquire() print '(For ack message) Doing proof of work...' shared.printLock.release() @@ -3697,13 +3697,9 @@ successfullyDecryptMessageTimings = [] #A list of the amounts of time it took to apiAddressGeneratorReturnQueue = Queue.Queue() #The address generator thread uses this queue to get information back to the API thread. alreadyAttemptedConnectionsListResetTime = int(time.time()) #used to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect. -#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! -networkDefaultProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. -networkDefaultPayloadLengthExtraBytes = 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. - if useVeryEasyProofOfWorkForTesting: - networkDefaultProofOfWorkNonceTrialsPerByte = networkDefaultProofOfWorkNonceTrialsPerByte / 16 - networkDefaultPayloadLengthExtraBytes = networkDefaultPayloadLengthExtraBytes / 7000 + shared.networkDefaultProofOfWorkNonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte / 16 + shared.networkDefaultPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes / 7000 if __name__ == "__main__": signal.signal(signal.SIGINT, signal_handler) @@ -3754,8 +3750,8 @@ if __name__ == "__main__": shared.config.set('bitmessagesettings','sockspassword','') shared.config.set('bitmessagesettings','keysencrypted','false') shared.config.set('bitmessagesettings','messagesencrypted','false') - shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(networkDefaultProofOfWorkNonceTrialsPerByte)) - shared.config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(networkDefaultPayloadLengthExtraBytes)) + shared.config.set('bitmessagesettings','defaultnoncetrialsperbyte',str(shared.networkDefaultProofOfWorkNonceTrialsPerByte)) + shared.config.set('bitmessagesettings','defaultpayloadlengthextrabytes',str(shared.networkDefaultPayloadLengthExtraBytes)) if storeConfigFilesInSameDirectoryAsProgramByDefault: #Just use the same directory as the program and forget about the appdata folder @@ -3903,15 +3899,6 @@ if __name__ == "__main__": import bitmessageqt bitmessageqt.run() - - if shared.config.getboolean('bitmessagesettings', 'startintray'): - myapp.hide() - myapp.trayIcon.show() - #self.hidden = True - #self.setWindowState(self.windowState() & QtCore.Qt.WindowMinimized) - #self.hide() - if 'win32' in sys.platform or 'win64' in sys.platform: - myapp.setWindowFlags(Qt.ToolTip) else: print 'Running as a daemon. You can use Ctrl+C to exit.' while True: diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 6edf4f82..13e11ec1 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -1108,9 +1108,9 @@ class MyForm(QtGui.QMainWindow): shared.config.set('bitmessagesettings', 'socksusername', str(self.settingsDialogInstance.ui.lineEditSocksUsername.text())) shared.config.set('bitmessagesettings', 'sockspassword', str(self.settingsDialogInstance.ui.lineEditSocksPassword.text())) if float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text()) >= 1: - shared.config.set('bitmessagesettings', 'defaultnoncetrialsperbyte',str(int(float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text())*networkDefaultProofOfWorkNonceTrialsPerByte))) + shared.config.set('bitmessagesettings', 'defaultnoncetrialsperbyte',str(int(float(self.settingsDialogInstance.ui.lineEditTotalDifficulty.text())*shared.networkDefaultProofOfWorkNonceTrialsPerByte))) if float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text()) >= 1: - shared.config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes',str(int(float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text())*networkDefaultPayloadLengthExtraBytes))) + shared.config.set('bitmessagesettings', 'defaultpayloadlengthextrabytes',str(int(float(self.settingsDialogInstance.ui.lineEditSmallMessageDifficulty.text())*shared.networkDefaultPayloadLengthExtraBytes))) with open(shared.appdata + 'keys.dat', 'wb') as configfile: shared.config.write(configfile) @@ -1764,7 +1764,7 @@ class settingsDialog(QtGui.QDialog): self.ui.checkBoxStartOnLogon.setDisabled(True) self.ui.checkBoxMinimizeToTray.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.') + self.ui.labelSettingsNote.setText('Options have been disabled because they either aren\'t applicable or because they haven\'t yet been implimented for your operating system.') #On the Network settings tab: self.ui.lineEditTCPPort.setText(str(shared.config.get('bitmessagesettings', 'port'))) self.ui.checkBoxAuthentication.setChecked(shared.config.getboolean('bitmessagesettings', 'socksauthentication')) @@ -1788,8 +1788,8 @@ class settingsDialog(QtGui.QDialog): self.ui.lineEditSocksPassword.setText(str(shared.config.get('bitmessagesettings', 'sockspassword'))) QtCore.QObject.connect(self.ui.comboBoxProxyType, QtCore.SIGNAL("currentIndexChanged(int)"), self.comboBoxProxyTypeChanged) - self.ui.lineEditTotalDifficulty.setText(str((float(shared.config.getint('bitmessagesettings', 'defaultnoncetrialsperbyte'))/networkDefaultProofOfWorkNonceTrialsPerByte))) - self.ui.lineEditSmallMessageDifficulty.setText(str((float(shared.config.getint('bitmessagesettings', 'defaultpayloadlengthextrabytes'))/networkDefaultPayloadLengthExtraBytes))) + self.ui.lineEditTotalDifficulty.setText(str((float(shared.config.getint('bitmessagesettings', 'defaultnoncetrialsperbyte'))/shared.networkDefaultProofOfWorkNonceTrialsPerByte))) + self.ui.lineEditSmallMessageDifficulty.setText(str((float(shared.config.getint('bitmessagesettings', 'defaultpayloadlengthextrabytes'))/shared.networkDefaultPayloadLengthExtraBytes))) QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self)) def comboBoxProxyTypeChanged(self,comboBoxIndex): @@ -1928,4 +1928,12 @@ def run(): app.setStyleSheet("QStatusBar::item { border: 0px solid black }") myapp = MyForm() myapp.show() + if shared.config.getboolean('bitmessagesettings', 'startintray'): + myapp.hide() + myapp.trayIcon.show() + #self.hidden = True + #self.setWindowState(self.windowState() & QtCore.Qt.WindowMinimized) + #self.hide() + if 'win32' in sys.platform or 'win64' in sys.platform: + myapp.setWindowFlags(Qt.ToolTip) sys.exit(app.exec_()) \ No newline at end of file diff --git a/src/shared.py b/src/shared.py index 681ee970..1c1bb4ad 100644 --- a/src/shared.py +++ b/src/shared.py @@ -26,6 +26,10 @@ printLock = threading.Lock() appdata = '' #holds the location of the application data storage directory statusIconColor = 'red' +#If changed, these values 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! +networkDefaultProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. +networkDefaultPayloadLengthExtraBytes = 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. + def lookupAppdataFolder(): APPNAME = "PyBitmessage" from os import path, environ From da4cf1f1cb4d05ef89c07ff58f3fdea3c7a8b273 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 2 May 2013 16:05:31 -0400 Subject: [PATCH 27/33] further deamon-related changes --- src/bitmessagemain.py | 6 +++--- src/bitmessageqt/__init__.py | 2 +- src/shared.py | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 76dd0d7d..dd42d824 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -6,7 +6,7 @@ #Right now, PyBitmessage only support connecting to stream 1. It doesn't yet contain logic to expand into further streams. -softwareVersion = '0.3.0' +#The software version variable is now held in shared.py verbose = 1 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. @@ -2237,7 +2237,7 @@ def pointMult(secret): def assembleVersionMessage(remoteHost,remotePort,myStreamNumber): - global softwareVersion + shared.softwareVersion payload = '' payload += pack('>L',2) #protocol version. payload += pack('>q',1) #bitflags of the services I offer. @@ -2253,7 +2253,7 @@ def assembleVersionMessage(remoteHost,remotePort,myStreamNumber): random.seed() payload += eightBytesOfRandomDataUsedToDetectConnectionsToSelf - userAgent = '/PyBitmessage:' + softwareVersion + '/' #Length of userAgent must be less than 253. + userAgent = '/PyBitmessage:' + shared.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. diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 13e11ec1..65d2e64c 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -1732,7 +1732,7 @@ class aboutDialog(QtGui.QDialog): self.ui = Ui_aboutDialog() self.ui.setupUi(self) self.parent = parent - self.ui.labelVersion.setText('version ' + softwareVersion) + self.ui.labelVersion.setText('version ' + shared.softwareVersion) class regenerateAddressesDialog(QtGui.QDialog): def __init__(self,parent): diff --git a/src/shared.py b/src/shared.py index 1c1bb4ad..a4e26682 100644 --- a/src/shared.py +++ b/src/shared.py @@ -1,5 +1,6 @@ -import threading +softwareVersion = '0.3.0' +import threading import sys from addresses import * import highlevelcrypto From b8f44aadb46b64f7503b27f96fe7f74fae97684c Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 2 May 2013 16:55:13 -0400 Subject: [PATCH 28/33] further deamon-related changes --- src/bitmessagemain.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index dd42d824..e5e772b7 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -3850,10 +3850,6 @@ if __name__ == "__main__": singleCleanerThread = singleCleaner() singleCleanerThread.daemon = True # close the main program even if there are threads left singleCleanerThread.start() - - singleListenerThread = singleListener() - singleListenerThread.daemon = True # close the main program even if there are threads left - singleListenerThread.start() shared.reloadMyAddressHashes() shared.reloadBroadcastSendersForWhichImWatching() @@ -3888,6 +3884,10 @@ if __name__ == "__main__": connectToStream(1) + singleListenerThread = singleListener() + singleListenerThread.daemon = True # close the main program even if there are threads left + singleListenerThread.start() + if not shared.safeConfigGetBoolean('bitmessagesettings','daemon'): try: from PyQt4.QtCore import * From 73ec3e6293e7d055b93cda1b90ca5f67a6d1d02b Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 3 May 2013 12:05:57 -0400 Subject: [PATCH 29/33] Use different data structure to maintain the number of connections shown on the Network Status tab --- src/bitmessagemain.py | 57 ++++++++++++++---------------------- src/bitmessageqt/__init__.py | 43 ++++++++++++++++++--------- src/shared.py | 1 + 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index e5e772b7..fb77274f 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -66,7 +66,7 @@ class outgoingSynSender(threading.Thread): random.seed() HOST, = random.sample(shared.knownNodes[self.streamNumber], 1) alreadyAttemptedConnectionsListLock.acquire() - while HOST in alreadyAttemptedConnectionsList or HOST in connectedHostsList: + while HOST in alreadyAttemptedConnectionsList or HOST in shared.connectedHostsList: alreadyAttemptedConnectionsListLock.release() #print 'choosing new sample' random.seed() @@ -206,8 +206,8 @@ class singleListener(threading.Thread): time.sleep(10) a,(HOST,PORT) = sock.accept() #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. - """while HOST in connectedHostsList: - print 'incoming connection is from a host in connectedHostsList (we are already connected to it). Ignoring it.' + """while HOST in shared.connectedHostsList: + print 'incoming connection is from a host in shared.connectedHostsList (we are already connected to it). Ignoring it.' a.close() a,(HOST,PORT) = sock.accept()""" rd = receiveDataThread() @@ -242,7 +242,7 @@ class receiveDataThread(threading.Thread): 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. self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave = {} - 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 an outgoingSynSender thread doesn't try to connect to it. + shared.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 an outgoingSynSender thread doesn't try to connect to it. 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 @@ -254,7 +254,7 @@ class receiveDataThread(threading.Thread): def run(self): shared.printLock.acquire() - print 'ID of the receiveDataThread is', str(id(self))+'. The size of the connectedHostsList is now', len(connectedHostsList) + print 'ID of the receiveDataThread is', str(id(self))+'. The size of the shared.connectedHostsList is now', len(shared.connectedHostsList) shared.printLock.release() while True: try: @@ -294,21 +294,13 @@ class receiveDataThread(threading.Thread): except: pass shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) - 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]) - shared.UISignalQueue.put(('updateNetworkStatusTab',(self.streamNumber,connectionsCount[self.streamNumber]))) - shared.printLock.acquire() - print 'Updating network status tab with current connections count:', connectionsCount[self.streamNumber] - shared.printLock.release() - connectionsCountLock.release() try: - del connectedHostsList[self.HOST] + del shared.connectedHostsList[self.HOST] except Exception, err: - print 'Could not delete', self.HOST, 'from connectedHostsList.', err + print 'Could not delete', self.HOST, 'from shared.connectedHostsList.', err + shared.UISignalQueue.put(('updateNetworkStatusTab','no data')) shared.printLock.acquire() - print 'The size of the connectedHostsList is now:', len(connectedHostsList) + print 'The size of the shared.connectedHostsList is now:', len(shared.connectedHostsList) shared.printLock.release() def processData(self): @@ -434,23 +426,17 @@ class receiveDataThread(threading.Thread): if not self.initiatedConnection: #self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),'green') shared.UISignalQueue.put(('setStatusIcon','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]) - shared.UISignalQueue.put(('updateNetworkStatusTab',(self.streamNumber,connectionsCount[self.streamNumber]))) - connectionsCountLock.release() + shared.UISignalQueue.put(('updateNetworkStatusTab','no data')) remoteNodeIncomingPort, remoteNodeSeenTime = shared.knownNodes[self.streamNumber][self.HOST] shared.printLock.acquire() print 'Connection fully established with', self.HOST, remoteNodeIncomingPort - print 'ConnectionsCount now:', connectionsCount[self.streamNumber] - print 'The size of the connectedHostsList is now', len(connectedHostsList) + print 'The size of the connectedHostsList is now', len(shared.connectedHostsList) print 'The length of sendDataQueues is now:', len(shared.sendDataQueues) print 'broadcasting addr from within connectionFullyEstablished function.' shared.printLock.release() self.broadcastaddr([(int(time.time()), self.streamNumber, 1, self.HOST, remoteNodeIncomingPort)]) #This lets all of our peers know about this new node. self.sendaddr() #This is one large addr message to this one peer. - if not self.initiatedConnection and connectionsCount[self.streamNumber] > 150: + if not self.initiatedConnection and len(shared.connectedHostsList) > 200: shared.printLock.acquire() print 'We are connected to too many people. Closing connection.' shared.printLock.release() @@ -1568,28 +1554,36 @@ class receiveDataThread(threading.Thread): #Our peer has requested (in a getdata message) that we send an object. def sendData(self,objectType,payload): if objectType == 'pubkey': + shared.printLock.acquire() print 'sending pubkey' + shared.printLock.release() headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'pubkey\x00\x00\x00\x00\x00\x00' headerData += pack('>L',len(payload)) #payload length. headerData += hashlib.sha512(payload).digest()[:4] self.sock.sendall(headerData + payload) elif objectType == 'getpubkey' or objectType == 'pubkeyrequest': + shared.printLock.acquire() print 'sending getpubkey' + shared.printLock.release() headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'getpubkey\x00\x00\x00' headerData += pack('>L',len(payload)) #payload length. headerData += hashlib.sha512(payload).digest()[:4] self.sock.sendall(headerData + payload) elif objectType == 'msg': + shared.printLock.acquire() print 'sending msg' + shared.printLock.release() headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' headerData += pack('>L',len(payload)) #payload length. headerData += hashlib.sha512(payload).digest()[:4] self.sock.sendall(headerData + payload) elif objectType == 'broadcast': + shared.printLock.acquire() print 'sending broadcast' + shared.printLock.release() headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'broadcast\x00\x00\x00' headerData += pack('>L',len(payload)) #payload length. @@ -1936,6 +1930,7 @@ class receiveDataThread(threading.Thread): print 'Closed connection to', self.HOST, 'because they are interested in stream', self.streamNumber,'.' shared.printLock.release() return + shared.connectedHostsList[self.HOST] = 1 #We use this data structure to not only keep track of what hosts we are connected to so that we don't try to connect to them again, but also to list the connections count on the Network Status tab. #If this was an incoming connection, then the sendData thread doesn't know the stream. We have to set it. if not self.initiatedConnection: shared.broadcastToSendDataQueues((0,'setStreamNumber',(self.HOST,self.streamNumber))) @@ -2196,19 +2191,14 @@ def signal_handler(signal, frame): def connectToStream(streamNumber): - #self.listOfOutgoingSynSenderThreads = [] #if we don't maintain this list, the threads will get garbage-collected. - connectionsCount[streamNumber] = 0 selfInitiatedConnections[streamNumber] = {} for i in range(32): a = outgoingSynSender() - #self.listOfOutgoingSynSenderThreads.append(a) - #QtCore.QObject.connect(a, QtCore.SIGNAL("passObjectThrough(PyQt_PyObject)"), self.connectObjectToSignals) - #QtCore.QObject.connect(a, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) a.setup(streamNumber) a.start() - #Does an EC point multiplication; turns a private key into a public key. +#Does an EC point multiplication; turns a private key into a public key. def pointMult(secret): #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. k = OpenSSL.EC_KEY_new_by_curve_name(OpenSSL.get_curve('secp256k1')) @@ -3686,11 +3676,8 @@ class singleAPI(threading.Thread): selfInitiatedConnections = {} #This is a list of current connections (the thread pointers at least) alreadyAttemptedConnectionsList = {} #This is a list of nodes to which we have already attempted a connection ackdataForWhichImWatching = {} -connectionsCount = {} #Used for the 'network status' tab. -connectionsCountLock = threading.Lock() alreadyAttemptedConnectionsListLock = threading.Lock() eightBytesOfRandomDataUsedToDetectConnectionsToSelf = pack('>Q',random.randrange(1, 18446744073709551615)) -connectedHostsList = {} #List of hosts to which we are connected. Used to guarantee that the outgoingSynSender threads won't connect to the same remote node twice. neededPubkeys = {} successfullyDecryptMessageTimings = [] #A list of the amounts of time it took to successfully decrypt msg messages #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. diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 65d2e64c..7c8b89bb 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -434,7 +434,7 @@ class MyForm(QtGui.QMainWindow): QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"), self.updateSentItemStatusByAckdata) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("displayNewInboxMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.displayNewInboxMessage) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), self.displayNewSentMessage) - QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"), self.updateNetworkStatusTab) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("updateNetworkStatusTab()"), self.updateNetworkStatusTab) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfMessagesProcessed()"), self.incrementNumberOfMessagesProcessed) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfPubkeysProcessed()"), self.incrementNumberOfPubkeysProcessed) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL("incrementNumberOfBroadcastsProcessed()"), self.incrementNumberOfBroadcastsProcessed) @@ -550,16 +550,32 @@ class MyForm(QtGui.QMainWindow): self.numberOfPubkeysProcessed += 1 self.ui.labelPubkeyCount.setText('Processed ' + str(self.numberOfPubkeysProcessed) + ' public keys.') - def updateNetworkStatusTab(self,streamNumber,connectionCount): + def updateNetworkStatusTab(self): #print 'updating network status tab' totalNumberOfConnectionsFromAllStreams = 0 #One would think we could use len(sendDataQueues) for this but the number doesn't always match: just because we have a sendDataThread running doesn't mean that the connection has been fully established (with the exchange of version messages). - foundTheRowThatNeedsUpdating = False - for currentRow in range(self.ui.tableWidgetConnectionCount.rowCount()): + streamNumberTotals = {} + for host, streamNumber in shared.connectedHostsList.items(): + if not streamNumber in streamNumberTotals: + streamNumberTotals[streamNumber] = 1 + else: + streamNumberTotals[streamNumber] += 1 + + while self.ui.tableWidgetConnectionCount.rowCount() > 0: + self.ui.tableWidgetConnectionCount.removeRow(0) + for streamNumber, connectionCount in streamNumberTotals.items(): + 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(str(connectionCount)) + newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) + self.ui.tableWidgetConnectionCount.setItem(0,1,newItem) + """for currentRow in range(self.ui.tableWidgetConnectionCount.rowCount()): rowStreamNumber = int(self.ui.tableWidgetConnectionCount.item(currentRow,0).text()) if streamNumber == rowStreamNumber: foundTheRowThatNeedsUpdating = True self.ui.tableWidgetConnectionCount.item(currentRow,1).setText(str(connectionCount)) - totalNumberOfConnectionsFromAllStreams += connectionCount + #totalNumberOfConnectionsFromAllStreams += connectionCount if foundTheRowThatNeedsUpdating == False: #Add a line to the table for this stream number and update its count with the current connection count. self.ui.tableWidgetConnectionCount.insertRow(0) @@ -569,11 +585,11 @@ class MyForm(QtGui.QMainWindow): newItem = QtGui.QTableWidgetItem(str(connectionCount)) newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) self.ui.tableWidgetConnectionCount.setItem(0,1,newItem) - totalNumberOfConnectionsFromAllStreams += connectionCount - self.ui.labelTotalConnections.setText('Total Connections: ' + str(totalNumberOfConnectionsFromAllStreams)) - if totalNumberOfConnectionsFromAllStreams > 0 and shared.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. + totalNumberOfConnectionsFromAllStreams += connectionCount""" + self.ui.labelTotalConnections.setText('Total Connections: ' + str(len(shared.connectedHostsList))) + if len(shared.connectedHostsList) > 0 and shared.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: + elif len(shared.connectedHostsList) == 0: self.setStatusIcon('red') def setStatusIcon(self,color): @@ -851,13 +867,13 @@ class MyForm(QtGui.QMainWindow): - def connectObjectToSignals(self,object): + """def connectObjectToSignals(self,object): QtCore.QObject.connect(object, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar) 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) 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("updateNetworkStatusTab()"), 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) @@ -866,7 +882,7 @@ class MyForm(QtGui.QMainWindow): #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) + QtCore.QObject.connect(object, QtCore.SIGNAL("updateStatusBar(PyQt_PyObject)"), self.updateStatusBar)""" #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): @@ -1910,8 +1926,7 @@ class UISignaler(QThread): toAddress,fromLabel,fromAddress,subject,message,ackdata = data self.emit(SIGNAL("displayNewSentMessage(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),toAddress,fromLabel,fromAddress,subject,message,ackdata) elif command == 'updateNetworkStatusTab': - streamNumber,count = data - self.emit(SIGNAL("updateNetworkStatusTab(PyQt_PyObject,PyQt_PyObject)"),streamNumber,count) + self.emit(SIGNAL("updateNetworkStatusTab()")) elif command == 'incrementNumberOfMessagesProcessed': self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) elif command == 'incrementNumberOfPubkeysProcessed': diff --git a/src/shared.py b/src/shared.py index a4e26682..2a138cc7 100644 --- a/src/shared.py +++ b/src/shared.py @@ -26,6 +26,7 @@ inventoryLock = threading.Lock() #Guarantees that two receiveDataThreads don't r printLock = threading.Lock() appdata = '' #holds the location of the application data storage directory statusIconColor = 'red' +connectedHostsList = {} #List of hosts to which we are connected. Used to guarantee that the outgoingSynSender threads won't connect to the same remote node twice. #If changed, these values 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! networkDefaultProofOfWorkNonceTrialsPerByte = 320 #The amount of work that should be performed (and demanded) per byte of the payload. Double this number to double the work. From caf9890bd17e9f55ce89e0843567b000d67f198f Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 3 May 2013 12:24:47 -0400 Subject: [PATCH 30/33] better error handling around sock.sendall --- src/bitmessagemain.py | 82 ++++++++++++++++++++++++------------ src/bitmessageqt/__init__.py | 5 ++- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index fb77274f..adeb657f 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -412,7 +412,13 @@ class receiveDataThread(threading.Thread): def sendpong(self): print 'Sending pong' - 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') + try: + 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') + except Exception, err: + #if not 'Bad file descriptor' in err: + shared.printLock.acquire() + sys.stderr.write('sock.sendall error: %s\n' % err) + shared.printLock.release() def recverack(self): print 'verack received' @@ -501,7 +507,13 @@ class receiveDataThread(threading.Thread): shared.printLock.acquire() print 'Sending huge inv message with', numberOfObjects, 'objects to just this one peer' shared.printLock.release() - self.sock.sendall(headerData + payload) + try: + self.sock.sendall(headerData + payload) + except Exception, err: + #if not 'Bad file descriptor' in err: + shared.printLock.acquire() + sys.stderr.write('sock.sendall error: %s\n' % err) + shared.printLock.release() #We have received a broadcast message def recbroadcast(self,data): @@ -1519,7 +1531,7 @@ class receiveDataThread(threading.Thread): except Exception, err: #if not 'Bad file descriptor' in err: shared.printLock.acquire() - sys.stderr.write('sock.send error: %s\n' % err) + sys.stderr.write('sock.sendall error: %s\n' % err) shared.printLock.release() #We have received a getdata request from our peer @@ -1553,44 +1565,39 @@ class receiveDataThread(threading.Thread): #Our peer has requested (in a getdata message) that we send an object. def sendData(self,objectType,payload): + headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. if objectType == 'pubkey': shared.printLock.acquire() print 'sending pubkey' shared.printLock.release() - headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'pubkey\x00\x00\x00\x00\x00\x00' - headerData += pack('>L',len(payload)) #payload length. - headerData += hashlib.sha512(payload).digest()[:4] - self.sock.sendall(headerData + payload) elif objectType == 'getpubkey' or objectType == 'pubkeyrequest': shared.printLock.acquire() print 'sending getpubkey' shared.printLock.release() - headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'getpubkey\x00\x00\x00' - headerData += pack('>L',len(payload)) #payload length. - headerData += hashlib.sha512(payload).digest()[:4] - self.sock.sendall(headerData + payload) elif objectType == 'msg': shared.printLock.acquire() print 'sending msg' shared.printLock.release() - headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' - headerData += pack('>L',len(payload)) #payload length. - headerData += hashlib.sha512(payload).digest()[:4] - self.sock.sendall(headerData + payload) elif objectType == 'broadcast': shared.printLock.acquire() print 'sending broadcast' shared.printLock.release() - headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'broadcast\x00\x00\x00' - headerData += pack('>L',len(payload)) #payload length. - headerData += hashlib.sha512(payload).digest()[:4] - self.sock.sendall(headerData + payload) else: sys.stderr.write('Error: sendData has been asked to send a strange objectType: %s\n' % str(objectType)) + return + headerData += pack('>L',len(payload)) #payload length. + headerData += hashlib.sha512(payload).digest()[:4] + try: + self.sock.sendall(headerData + payload) + except Exception, err: + #if not 'Bad file descriptor' in err: + shared.printLock.acquire() + sys.stderr.write('sock.sendall error: %s\n' % err) + shared.printLock.release() #Send an inv message with just one hash to all of our peers def broadcastinv(self,hash): @@ -1893,12 +1900,17 @@ class receiveDataThread(threading.Thread): datatosend = datatosend + pack('>L',len(payload)) #payload length datatosend = datatosend + hashlib.sha512(payload).digest()[0:4] datatosend = datatosend + payload - - if verbose >= 1: + try: + self.sock.sendall(datatosend) + if verbose >= 1: + shared.printLock.acquire() + print 'Sending addr with', numberOfAddressesInAddrMessage, 'entries.' + shared.printLock.release() + except Exception, err: + #if not 'Bad file descriptor' in err: shared.printLock.acquire() - print 'Sending addr with', numberOfAddressesInAddrMessage, 'entries.' + sys.stderr.write('sock.sendall error: %s\n' % err) shared.printLock.release() - self.sock.sendall(datatosend) #We have received a version message def recversion(self,data): @@ -1960,14 +1972,26 @@ class receiveDataThread(threading.Thread): shared.printLock.acquire() print 'Sending version message' shared.printLock.release() - self.sock.sendall(assembleVersionMessage(self.HOST,self.PORT,self.streamNumber)) + try: + self.sock.sendall(assembleVersionMessage(self.HOST,self.PORT,self.streamNumber)) + except Exception, err: + #if not 'Bad file descriptor' in err: + shared.printLock.acquire() + sys.stderr.write('sock.sendall error: %s\n' % err) + shared.printLock.release() #Sends a verack message def sendverack(self): shared.printLock.acquire() print 'Sending verack' shared.printLock.release() - 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') + try: + 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') + except Exception, err: + #if not 'Bad file descriptor' in err: + shared.printLock.acquire() + sys.stderr.write('sock.sendall error: %s\n' % err) + shared.printLock.release() #cf 83 e1 35 self.verackSent = True if self.verackReceived == True: @@ -2013,7 +2037,13 @@ class sendDataThread(threading.Thread): shared.printLock.acquire() print 'Sending version packet: ', repr(datatosend) shared.printLock.release() - self.sock.sendall(datatosend) + try: + self.sock.sendall(datatosend) + except Exception, err: + #if not 'Bad file descriptor' in err: + shared.printLock.acquire() + sys.stderr.write('sock.sendall error: %s\n' % err) + shared.printLock.release() self.versionSent = 1 diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 7c8b89bb..7e99db8a 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -564,7 +564,10 @@ class MyForm(QtGui.QMainWindow): self.ui.tableWidgetConnectionCount.removeRow(0) for streamNumber, connectionCount in streamNumberTotals.items(): self.ui.tableWidgetConnectionCount.insertRow(0) - newItem = QtGui.QTableWidgetItem(str(streamNumber)) + if streamNumber == 0: + newItem = QtGui.QTableWidgetItem("?") + else: + newItem = QtGui.QTableWidgetItem(str(streamNumber)) newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) self.ui.tableWidgetConnectionCount.setItem(0,0,newItem) newItem = QtGui.QTableWidgetItem(str(connectionCount)) From 05c49a31cd71f68bd634eea7f29043edc71dfd89 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 3 May 2013 15:53:38 -0400 Subject: [PATCH 31/33] support switching to and from portable mode without restarting --- src/bitmessagemain.py | 56 ++++++++++++++++-------------------- src/bitmessageqt/__init__.py | 11 ++++--- src/shared.py | 8 +++--- 3 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index adeb657f..9f371865 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -300,7 +300,7 @@ class receiveDataThread(threading.Thread): print 'Could not delete', self.HOST, 'from shared.connectedHostsList.', err shared.UISignalQueue.put(('updateNetworkStatusTab','no data')) shared.printLock.acquire() - print 'The size of the shared.connectedHostsList is now:', len(shared.connectedHostsList) + print 'The size of the connectedHostsList is now:', len(shared.connectedHostsList) shared.printLock.release() def processData(self): @@ -2425,8 +2425,29 @@ class sqlThread(threading.Thread): if item == 'commit': self.conn.commit() elif item == 'exit': + self.conn.close() print 'sqlThread exiting gracefully.' return + elif item == 'movemessagstoprog': + shared.printLock.acquire() + print 'the sqlThread is moving the messages.dat file to the local program directory.' + shared.printLock.release() + self.conn.commit() + self.conn.close() + shutil.move(shared.lookupAppdataFolder()+'messages.dat','messages.dat') + self.conn = sqlite3.connect('messages.dat' ) + self.conn.text_factory = str + self.cur = self.conn.cursor() + elif item == 'movemessagstoappdata': + shared.printLock.acquire() + print 'the sqlThread is moving the messages.dat file to the Appdata folder.' + shared.printLock.release() + self.conn.commit() + self.conn.close() + shutil.move('messages.dat',shared.lookupAppdataFolder()+'messages.dat') + self.conn = sqlite3.connect(shared.appdata + 'messages.dat' ) + self.conn.text_factory = str + self.cur = self.conn.cursor() else: parameters = shared.sqlSubmitQueue.get() #print 'item', item @@ -3710,17 +3731,17 @@ alreadyAttemptedConnectionsListLock = threading.Lock() eightBytesOfRandomDataUsedToDetectConnectionsToSelf = pack('>Q',random.randrange(1, 18446744073709551615)) neededPubkeys = {} successfullyDecryptMessageTimings = [] #A list of the amounts of time it took to successfully decrypt msg messages -#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. alreadyAttemptedConnectionsListResetTime = int(time.time()) #used to clear out the alreadyAttemptedConnectionsList periodically so that we will retry connecting to hosts to which we have already tried to connect. if useVeryEasyProofOfWorkForTesting: - shared.networkDefaultProofOfWorkNonceTrialsPerByte = shared.networkDefaultProofOfWorkNonceTrialsPerByte / 16 - shared.networkDefaultPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes / 7000 + shared.networkDefaultProofOfWorkNonceTrialsPerByte = int(shared.networkDefaultProofOfWorkNonceTrialsPerByte / 16) + shared.networkDefaultPayloadLengthExtraBytes = int(shared.networkDefaultPayloadLengthExtraBytes / 7000) if __name__ == "__main__": signal.signal(signal.SIGINT, signal_handler) #signal.signal(signal.SIGINT, signal.SIG_DFL) + # Check the Major version, the first element in the array if sqlite3.sqlite_version_info[0] < 3: print 'This program requires sqlite version 3 or higher because 2 and lower cannot store NULL values. I see version:', sqlite3.sqlite_version_info @@ -3731,15 +3752,11 @@ if __name__ == "__main__": shared.config.read('keys.dat') try: shared.config.get('bitmessagesettings', 'settingsversion') - #settingsFileExistsInProgramDirectory = True print 'Loading config files from same directory as program' shared.appdata = '' except: #Could not load the keys.dat file in the program directory. Perhaps it is in the appdata directory. shared.appdata = shared.lookupAppdataFolder() - #if not os.path.exists(shared.appdata): - # os.makedirs(shared.appdata) - shared.config = ConfigParser.SafeConfigParser() shared.config.read(shared.appdata + 'keys.dat') try: @@ -3795,29 +3812,6 @@ if __name__ == "__main__": with open(shared.appdata + 'keys.dat', 'wb') as configfile: shared.config.write(configfile) - #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: - shared.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') - shared.config.remove_option('bitmessagesettings', 'movemessagstoprog') - with open(shared.appdata + 'keys.dat', 'wb') as configfile: - shared.config.write(configfile) - except: - pass - try: - shared.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') - shared.config.remove_option('bitmessagesettings', 'movemessagstoappdata') - with open(shared.appdata + 'keys.dat', 'wb') as configfile: - shared.config.write(configfile) - except: - pass - - try: #We shouldn't have to use the shared.knownNodesLock because this had better be the only thread accessing knownNodes right now. pickleFile = open(shared.appdata + 'knownnodes.dat', 'rb') diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 7e99db8a..52dba18a 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -21,6 +21,7 @@ from time import strftime, localtime, gmtime import time import os from pyelliptic.openssl import OpenSSL +import pickle class MyForm(QtGui.QMainWindow): def __init__(self, parent=None): @@ -1150,8 +1151,10 @@ class MyForm(QtGui.QMainWindow): pass if shared.appdata != '' and self.settingsDialogInstance.ui.checkBoxPortableMode.isChecked(): #If we are NOT using portable mode now but the user selected that we should... - shared.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 + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('movemessagstoprog') + shared.sqlLock.release() with open('keys.dat', 'wb') as configfile: shared.config.write(configfile) #Write the knownnodes.dat file to disk in the new location @@ -1163,13 +1166,14 @@ class MyForm(QtGui.QMainWindow): os.remove(shared.appdata + 'keys.dat') os.remove(shared.appdata + 'knownnodes.dat') shared.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 shared.appdata == '' and not self.settingsDialogInstance.ui.checkBoxPortableMode.isChecked(): #If we ARE using portable mode now but the user selected that we shouldn't... shared.appdata = shared.lookupAppdataFolder() if not os.path.exists(shared.appdata): os.makedirs(shared.appdata) - shared.config.set('bitmessagesettings','movemessagstoappdata','true') #Tells bitmessage to move the messages.dat file to the appdata directory the next time the program starts. + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('movemessagstoappdata') + shared.sqlLock.release() #Write the keys.dat file to disk in the new location with open(shared.appdata + 'keys.dat', 'wb') as configfile: shared.config.write(configfile) @@ -1181,7 +1185,6 @@ class MyForm(QtGui.QMainWindow): shared.knownNodesLock.release() 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).") def click_radioButtonBlacklist(self): diff --git a/src/shared.py b/src/shared.py index 2a138cc7..0eb7fa18 100644 --- a/src/shared.py +++ b/src/shared.py @@ -37,16 +37,16 @@ def lookupAppdataFolder(): from os import path, environ if sys.platform == 'darwin': if "HOME" in environ: - appdata = path.join(os.environ["HOME"], "Library/Application support/", APPNAME) + '/' + dataFolder = 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) + '\\' + dataFolder = path.join(environ['APPDATA'], APPNAME) + '\\' else: - appdata = path.expanduser(path.join("~", "." + APPNAME + "/")) - return appdata + dataFolder = path.expanduser(path.join("~", "." + APPNAME + "/")) + return dataFolder def isAddressInMyAddressBook(address): t = (address,) From a4beb436a4b79a20c52a13bcc833f42f61f9d2b7 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 3 May 2013 17:26:29 -0400 Subject: [PATCH 32/33] Added API Function: getStatus --- src/api client.py | 21 +++++++++++-------- src/bitmessagemain.py | 48 ++++++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/api client.py b/src/api client.py index f3feb098..474cd065 100644 --- a/src/api client.py +++ b/src/api client.py @@ -3,6 +3,7 @@ import xmlrpclib import json +import time api = xmlrpclib.ServerProxy("http://bradley:password@localhost:8442/") @@ -45,12 +46,16 @@ print 'Uncomment this next line to decode the actual message data in the first m print 'Uncomment this next line in the code to delete a message' #print api.trashMessage('584e5826947242a82cb883c8b39ac4a14959f14c228c0fbe6399f73e2cba5b59') -"""print 'Now let\'s send a message. The example addresses are invalid. You will have to put your own in.' -subject = 'subject!'.encode('base64') -message = 'Hello, this is the message'.encode('base64') -print api.sendMessage('BM-oqmocYzqK74y3qSRi8c3YqyenyEKiMyLB', 'BM-omzGU4MtzSUCQhMNm5kPR6UNrJ4Q4zeFe', subject,message)""" +print 'Uncomment these lines to send a message. The example addresses are invalid; you will have to put your own in.' +#subject = 'subject!'.encode('base64') +#message = 'Hello, this is the message'.encode('base64') +#ackData = api.sendMessage('BM-Gtsm7PUabZecs3qTeXbNPmqx3xtHCSXF', 'BM-2DCutnUZG16WiW3mdAm66jJUSCUv88xLgS', subject,message) +#print 'The ackData is:', ackData +#while True: +# time.sleep(2) +# print 'Current status:', api.getStatus(ackData) -"""print 'Now let\'s send a broadcast. The example address is invalid; you will have to put your own in.' -subject = 'subject within broadcast'.encode('base64') -message = 'Hello, this is the message within a broadcast.'.encode('base64') -print api.sendBroadcast('BM-onf6V1RELPgeNN6xw9yhpAiNiRexSRD4e', subject,message)""" \ No newline at end of file +print 'Uncomment these lines to send a broadcast. The example address is invalid; you will have to put your own in.' +#subject = 'subject within broadcast'.encode('base64') +#message = 'Hello, this is the message within a broadcast.'.encode('base64') +#print api.sendBroadcast('BM-onf6V1RELPgeNN6xw9yhpAiNiRexSRD4e', subject,message) \ No newline at end of file diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 9f371865..d47dd1fd 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -3496,20 +3496,20 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): elif len(params) == 5: passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe = params if len(passphrase) == 0: - return 'API Error 0001: the specified passphrase is blank.' + return 'API Error 0001: The specified passphrase is blank.' passphrase = passphrase.decode('base64') if addressVersionNumber == 0: #0 means "just use the proper addressVersionNumber" addressVersionNumber = 3 if addressVersionNumber != 3: - return 'API Error 0002: the address version number currently must be 3 (or 0 which means auto-select).', addressVersionNumber,' isn\'t supported.' + return 'API Error 0002: The address version number currently must be 3 (or 0 which means auto-select).', addressVersionNumber,' isn\'t supported.' if streamNumber == 0: #0 means "just use the most available stream" streamNumber = 1 if streamNumber != 1: - return 'API Error 0003: the stream number must be 1 (or 0 which means auto-select). Others aren\'t supported.' + return 'API Error 0003: The stream number must be 1 (or 0 which means auto-select). Others aren\'t supported.' if numberOfAddresses == 0: return 'API Error 0004: Why would you ask me to generate 0 addresses for you?' - if numberOfAddresses > 9999: - 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.' + if numberOfAddresses > 999: + return 'API Error 0005: You have (accidentally?) specified too many addresses to make. Maximum 999. 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.' apiAddressGeneratorReturnQueue.queue.clear() print 'Requesting that the addressGenerator create', numberOfAddresses, 'addresses.' #apiSignalQueue.put(('createDeterministicAddresses',(passphrase, numberOfAddresses, addressVersionNumber, streamNumber, eighteenByteRipe))) @@ -3574,9 +3574,9 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): if status == 'versiontoohigh': return 'API Error 0010: Address version number too high (or zero) in address: ' + toAddress if addressVersionNumber < 2 or addressVersionNumber > 3: - return 'API Error 0011: the address version number currently must be 2 or 3. Others aren\'t supported. Check the toAddress.' + return 'API Error 0011: The address version number currently must be 2 or 3. Others aren\'t supported. Check the toAddress.' if streamNumber != 1: - return 'API Error 0012: the stream number must be 1. Others aren\'t supported. Check the toAddress.' + return 'API Error 0012: The stream number must be 1. Others aren\'t supported. Check the toAddress.' status,addressVersionNumber,streamNumber,fromRipe = decodeAddress(fromAddress) if status <> 'success': shared.printLock.acquire() @@ -3589,17 +3589,17 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): if status == 'versiontoohigh': return 'API Error 0010: Address version number too high (or zero) in address: ' + fromAddress if addressVersionNumber < 2 or addressVersionNumber > 3: - return 'API Error 0011: the address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.' + return 'API Error 0011: The address version number currently must be 2 or 3. Others aren\'t supported. Check the fromAddress.' if streamNumber != 1: - return 'API Error 0012: the stream number must be 1. Others aren\'t supported. Check the fromAddress.' + return 'API Error 0012: The stream number must be 1. Others aren\'t supported. Check the fromAddress.' toAddress = addBMIfNotPresent(toAddress) fromAddress = addBMIfNotPresent(fromAddress) try: fromAddressEnabled = shared.config.getboolean(fromAddress,'enabled') except: - return 'API Error 0013: could not find your fromAddress in the keys.dat file.' + return 'API Error 0013: Could not find your fromAddress in the keys.dat file.' if not fromAddressEnabled: - return 'API Error 0014: your fromAddress is disabled. Cannot send.' + return 'API Error 0014: Your fromAddress is disabled. Cannot send.' ackdata = OpenSSL.rand(32) shared.sqlLock.acquire() @@ -3679,7 +3679,31 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): shared.workerQueue.put(('sendbroadcast',(fromAddress,subject,message))) return ackdata.encode('hex') - + elif method == 'getStatus': + if len(params) != 1: + return 'API Error 0000: I need one parameter!' + ackdata, = params + if len(ackdata) != 64: + return 'API Error 0015: The length of ackData should be 32 bytes (encoded in hex thus 64 characters).' + shared.sqlLock.acquire() + shared.sqlSubmitQueue.put('''SELECT status FROM sent where ackdata=?''') + shared.sqlSubmitQueue.put((ackdata.decode('hex'),)) + queryreturn = shared.sqlReturnQueue.get() + shared.sqlLock.release() + if queryreturn == []: + return 'notFound' + for row in queryreturn: + status, = row + if status == 'findingpubkey': + return 'findingPubkey' + if status == 'doingpow': + return 'doingPow' + if status == 'sentmessage': + return 'sentMessage' + if status == 'ackreceived': + return 'ackReceived' + else: + return 'otherStatus: '+status else: return 'Invalid Method: %s'%method From 7ba2a4f18b7a9aee1847c65d6d8a561728ca7730 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Sun, 5 May 2013 17:52:57 -0400 Subject: [PATCH 33/33] Close application if not daemon and PyQt not found --- src/bitmessagemain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index d47dd1fd..0abfcfd8 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -3930,7 +3930,7 @@ if __name__ == "__main__": except Exception, err: print 'PyBitmessage requires PyQt unless you want to run it as a daemon and interact with it using the API. You can download PyQt from http://www.riverbankcomputing.com/software/pyqt/download or by searching Google for \'PyQt Download\'. If you want to run in daemon mode, see https://bitmessage.org/wiki/Daemon' print 'Error message:', err - sys.exit() + os._exit(0) import bitmessageqt bitmessageqt.run()