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.
Daniel Kraft 10 years ago
parent 09c0aa993f
commit 03bb54fc98

@ -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 *

@ -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')
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0')
if storeConfigFilesInSameDirectoryAsProgramByDefault:
# Just use the same directory as the program and forget about

@ -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) + '/'
print ("Could not find home folder, please report this message"
+ " and your OS X version to the BitMessage Github.")
elif "win32" in sys.platform or "win64" in sys.platform:
dataFolder = path.join(environ["APPDATA"], app) + "\\"
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):
nmcFolder = lookupNamecoinFolder ()
nmcConfig = nmcFolder + "bitcoin.conf"
nmc = open (nmcConfig, "r")
while True:
line = nmc.readline ()
if line == "":
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", "")