From 03bb54fc98b5a1244fa510dcb0e97df296ed539e Mon Sep 17 00:00:00 2001 From: Daniel Kraft Date: Fri, 5 Jul 2013 20:08:19 +0200 Subject: [PATCH] Handle options for RPC connection. Handle config options for RPC connection, and also implement loading default user/password from namecoin config file. No UI yet. --- src/bitmessageqt/__init__.py | 2 +- src/helper_startup.py | 3 + src/{class_namecoin.py => namecoin.py} | 78 ++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 5 deletions(-) rename src/{class_namecoin.py => namecoin.py} (61%) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 6374a81f..3064ef4f 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -14,7 +14,7 @@ except ImportError: from addresses import * import shared from bitmessageui import * -from class_namecoin import namecoinConnection +from namecoin import namecoinConnection from newaddressdialog import * from newsubscriptiondialog import * from regenerateaddresses import * diff --git a/src/helper_startup.py b/src/helper_startup.py index e6590b0e..e3df91d6 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -3,6 +3,8 @@ import ConfigParser import sys import os +from namecoin import ensureNamecoinOptions + 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. def loadConfig(): @@ -64,6 +66,7 @@ def loadConfig(): 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0') shared.config.set( 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0') + ensureNamecoinOptions() if storeConfigFilesInSameDirectoryAsProgramByDefault: # Just use the same directory as the program and forget about diff --git a/src/class_namecoin.py b/src/namecoin.py similarity index 61% rename from src/class_namecoin.py rename to src/namecoin.py index f11cd97b..184545f9 100644 --- a/src/class_namecoin.py +++ b/src/namecoin.py @@ -22,6 +22,11 @@ import base64 import json import socket +import sys + +import shared + +configSection = "bitmessagesettings" # Error thrown when the RPC call returns an error. class RPCError (Exception): @@ -40,10 +45,11 @@ class namecoinConnection (object): queryid = 1 def __init__ (self): - self.user = "daniel" - self.password = "password" - self.host = "localhost" - self.port = "8336" + ensureNamecoinOptions () + self.user = shared.config.get (configSection, "namecoinrpcuser") + self.password = shared.config.get (configSection, "namecoinrpcpassword") + self.host = shared.config.get (configSection, "namecoinrpchost") + self.port = shared.config.get (configSection, "namecoinrpcport") # Query for the bitmessage address corresponding to the given identity # string. If it doesn't contain a slash, id/ is prepended. We return @@ -138,3 +144,67 @@ class namecoinConnection (object): except socket.error as exc: raise Exception ("Socket error in RPC connection: %s" % str (exc)) + +# Look up the namecoin data folder. +# FIXME: Check whether this works on other platforms as well! +def lookupNamecoinFolder (): + app = "namecoin" + from os import path, environ + if sys.platform == "darwin": + if "HOME" in environ: + dataFolder = path.join (os.environ["HOME"], + "Library/Application Support/", app) + '/' + 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: + dataFolder = path.join(environ["APPDATA"], app) + "\\" + else: + dataFolder = path.join(environ["HOME"], ".%s" % app) + "/" + + return dataFolder + +# Ensure all namecoin options are set, by setting those to default values +# that aren't there. +def ensureNamecoinOptions (): + if not shared.config.has_option (configSection, "namecoinrpchost"): + shared.config.set (configSection, "namecoinrpchost", "localhost") + if not shared.config.has_option (configSection, "namecoinrpcport"): + shared.config.set (configSection, "namecoinrpcport", "8336") + + hasUser = shared.config.has_option (configSection, "namecoinrpcuser") + hasPass = shared.config.has_option (configSection, "namecoinrpcpassword") + + # Try to read user/password from .namecoin configuration file. + if (not hasUser) or (not hasPass): + try: + nmcFolder = lookupNamecoinFolder () + nmcConfig = nmcFolder + "bitcoin.conf" + nmc = open (nmcConfig, "r") + + while True: + line = nmc.readline () + if line == "": + break + parts = line.split ("=") + if len (parts) == 2: + key = parts[0] + val = parts[1].rstrip () + + if key == "rpcuser" and not hasUser: + shared.config.set (configSection, + "namecoinrpcuser", val) + if key == "rpcpassword" and not hasPass: + shared.config.set (configSection, + "namecoinrpcpassword", val) + + nmc.close () + + except Exception as exc: + print "Failure reading namecoin config file: %s" % str (exc) + if (not hasUser): + shared.config.set (configSection, "namecoinrpcuser", "") + if (not hasPass): + shared.config.set (configSection, "namecoinrpcpassword", "")