From 1d3f650bba33d241d6cc6909c17c11084f6176de Mon Sep 17 00:00:00 2001 From: moneromooo Date: Mon, 26 Jan 2015 22:18:18 +0000 Subject: [PATCH] Starting networks is now via command line, and parameterized --- tipbot.py | 43 +++++++++++++++++++++++++++++++++----- tipbot/config.py | 15 ++++++++++--- tipbot/modules/freenode.py | 5 +++-- tipbot/modules/irc.py | 31 ++++++++++++++++++++------- tipbot/utils.py | 13 +++++++++--- 5 files changed, 86 insertions(+), 21 deletions(-) diff --git a/tipbot.py b/tipbot.py index df6d755..c19275f 100644 --- a/tipbot.py +++ b/tipbot.py @@ -22,6 +22,7 @@ import httplib import time import string import importlib +import re import tipbot.coinspecs as coinspecs import tipbot.config as config from tipbot.log import log_error, log_warn, log_info, log_log @@ -36,6 +37,7 @@ disabled = False selected_coin = None modulenames = [] +start_networks = [] argc = 1 while argc < len(sys.argv): arg = sys.argv[argc] @@ -60,7 +62,24 @@ while argc < len(sys.argv): log_error('Usage: tipbot.py [-m|--module modulename]* -c|--coin ') exit(1) argc = argc+1 - modulenames.append(sys.argv[argc]) + arg = sys.argv[argc] + if not arg in modulenames: + modulenames.append(arg) + elif arg == "-n" or arg == "--network": + if argc+1 == len(sys.argv): + log_error('Usage: tipbot.py [-m|--module modulename]* -c|--coin ') + exit(1) + argc = argc+1 + arg = sys.argv[argc] + if re.match('[^:]+:.+',arg): + parts=arg.split(':',1) + if not parts[1] in modulenames: + modulenames.append(parts[1]) + start_networks.append({'name':parts[0],'type':parts[1]}) + else: + if not arg in modulenames: + modulenames.append(arg) + start_networks.append({'type':arg}) elif arg == "-h" or arg == "--help": log_info('Usage: tipbot.py [-m|--module modulename]* -c|--coin ') exit(0) @@ -361,10 +380,24 @@ MigrateRedis() InitScanBlockHeight() # TODO: make this be created when the module is loaded -irc = sys.modules["freenode"].FreenodeNetwork() -irc.set_callbacks(OnCommandProxy,OnIdentified) -if irc.connect(config.irc_network,config.irc_port,config.tipbot_name,GetPassword(),config.irc_send_delay): - AddNetwork(irc) +for network_setup in start_networks: + network_type=network_setup['type'] + if 'name' in network_setup: + network_name=network_setup['name'] + log_info('Starting "%s" %s network' % (network_name, network_type)) + else: + network_name=network_type + log_info('Starting %s network' % network_type) + + name=network_name or network_type + try: + network=registered_networks[network_type](name=name) + network.set_callbacks(OnCommandProxy,OnIdentified) + if network.connect(): + AddNetwork(network) + except Exception,e: + log_error('Error starting %s network: %s' % (name,str(e))) + exit(1) while len(networks)>0: for network in networks: diff --git a/tipbot/config.py b/tipbot/config.py index 2b74346..2aaae07 100644 --- a/tipbot/config.py +++ b/tipbot/config.py @@ -18,9 +18,6 @@ irc_send_delay = 0.4 irc_welcome_line = 'Welcome to the freenode Internet Relay Chat Network' irc_channels = ['#txtptest000'] irc_timeout_seconds = 600 -irc_use_ssl = True -irc_use_sasl = True -irc_sasl_name = "monero-tipbot" redis_host="127.0.0.1" redis_port=7777 @@ -47,6 +44,18 @@ silent_invalid_commands = { 'freenode': ['price','worth','net','pools','calc'] } +network_config = { + 'freenode': { + 'host': 'irc.freenode.net', + 'port': 6697, + 'login': tipbot_name, + 'delay': irc_send_delay, + 'ssl': True, + 'sasl': True, + 'sasl_name': 'monero-tipbot', + }, +} + dice_min_multiplier=1.1 dice_max_multiplier=10 dice_edge = 0.01 diff --git a/tipbot/modules/freenode.py b/tipbot/modules/freenode.py index 6fd908b..ae0c241 100644 --- a/tipbot/modules/freenode.py +++ b/tipbot/modules/freenode.py @@ -18,8 +18,8 @@ from tipbot.command_manager import * from irc import * class FreenodeNetwork(IRCNetwork): - def __init__(self): - IRCNetwork.__init__(self,"freenode") + def __init__(self,name): + IRCNetwork.__init__(self,name) def login(self): self.send_to("nickserv", "IDENTIFY %s" % self.password) @@ -52,3 +52,4 @@ class FreenodeNetwork(IRCNetwork): log_error('ACC line not as expected...') return True +RegisterNetwork("freenode",FreenodeNetwork) diff --git a/tipbot/modules/irc.py b/tipbot/modules/irc.py index 5b87c71..6b36ed9 100644 --- a/tipbot/modules/irc.py +++ b/tipbot/modules/irc.py @@ -44,7 +44,21 @@ class IRCNetwork(Network): self.quitting = False self.buffered_data = "" - def connect(self,host,port,login,password,delay): + def connect(self): + try: + cfg=config.network_config[self.name] + host=cfg['host'] + port=cfg['port'] + login=cfg['login'] + password=GetPassword(self.name) + delay=cfg['delay'] + self.use_ssl=cfg['ssl'] + self.use_sasl=cfg['sasl'] + if self.use_sasl: + self.sasl_name=cfg['sasl_name'] + except Exception,e: + log_error('Configuration not found for %s: %s' % (self.name, str(e))) + return False return self._connect(host,port,login,password,delay) def disconnect(self): @@ -170,7 +184,7 @@ class IRCNetwork(Network): if data.find ( config.irc_welcome_line ) != -1: self.userstable = dict() self.registered_users.clear() - if not config.irc_use_sasl: + if not self.use_sasl: self.login() for chan in config.irc_channels: self.join(chan) @@ -182,8 +196,8 @@ class IRCNetwork(Network): return True if data.startswith('AUTHENTICATE +'): - if config.irc_use_sasl: - authstring = config.irc_sasl_name + chr(0) + config.irc_sasl_name + chr(0) + self.password + if self.use_sasl: + authstring = self.sasl_name + chr(0) + self.sasl_name + chr(0) + self.password self._irc_sendmsg('AUTHENTICATE %s' % base64.b64encode(authstring)) else: log_warn('Got AUTHENTICATE while not using SASL') @@ -370,13 +384,13 @@ class IRCNetwork(Network): log("IRCSEND",msg) def _irc_recv(self,size,flags=None): - if config.irc_use_ssl: + if self.use_ssl: return self.sslirc.read(size) else: return self.irc.recv(size,flags) def _irc_send(self,data): - if config.irc_use_ssl: + if self.use_ssl: return self.sslirc.write(data) else: return self.irc.send(data) @@ -447,7 +461,7 @@ class IRCNetwork(Network): self.registered_users=set() try: self.irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) - if config.irc_use_ssl: + if self.use_ssl: try: raise RuntimeError('') self.irc_ssl_context = ssl.create_default_context() @@ -461,7 +475,7 @@ class IRCNetwork(Network): log_error( 'Error initializing IRC: %s' % str(e)) return False self._log_IRCRECV(self._irc_recv(4096)) - if config.irc_use_sasl: + if self.use_sasl: self._irc_sendmsg('CAP REQ :sasl') else: self._irc_sendmsg ( 'PASS *********') @@ -518,3 +532,4 @@ RegisterCommand({ 'admin': True, 'help': "Makes %s part from a channel" % (config.tipbot_name) }) +RegisterNetwork("irc",IRCNetwork) diff --git a/tipbot/utils.py b/tipbot/utils.py index c8f8671..d93bc72 100644 --- a/tipbot/utils.py +++ b/tipbot/utils.py @@ -19,22 +19,26 @@ import tipbot.coinspecs as coinspecs from tipbot.log import log_error, log_warn, log_info, log_log from tipbot.redisdb import * +registered_networks=dict() networks=[] cached_tipbot_balance=None cached_tipbot_unlocked_balance=None cached_tipbot_balance_timestamp=None -def GetPassword(): +def GetPassword(name): try: f = open('tipbot-password.txt', 'r') for p in f: p = p.strip("\r\n") - f.close() - return p + parts=p.split(':') + if parts[0]==name: + return parts[1] except Exception,e: log_error('could not fetch password: %s' % str(e)) raise return "xxx" + finally: + f.close() def IsParamPresent(parms,idx): return len(parms) > idx @@ -243,6 +247,9 @@ def IdentityFromString(link,s): def NickFromIdentity(identity): return identity.split(':')[1] +def RegisterNetwork(name,type): + registered_networks[name]=type + def AddNetwork(network): networks.append(network)