reload connection configurations, make admin setting per-connection

This commit is contained in:
Ryan Hitchman 2014-04-30 13:16:16 -07:00
parent 0750e8d8b0
commit 18159024c7
5 changed files with 40 additions and 56 deletions

32
bot.py
View File

@ -3,6 +3,7 @@
import os import os
import Queue import Queue
import sys import sys
import traceback
import time import time
sys.path += ['plugins'] # so 'import hook' works without duplication sys.path += ['plugins'] # so 'import hook' works without duplication
@ -11,8 +12,11 @@ os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
class Bot(object): class Bot(object):
pass def __init__(self):
self.conns = {}
self.persist_dir = os.path.abspath('persist')
if not os.path.exists(self.persist_dir):
os.mkdir(bot.persist_dir)
bot = Bot() bot = Bot()
@ -23,31 +27,17 @@ eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
os.path.join('core', 'reload.py'), 'exec')) os.path.join('core', 'reload.py'), 'exec'))
reload(init=True) reload(init=True)
config()
if not hasattr(bot, 'config'):
exit()
print 'Connecting to IRC' print 'Connecting to IRC'
bot.conns = {}
try: try:
for name, conf in bot.config['connections'].iteritems(): config()
if conf.get('ssl'): if not hasattr(bot, 'config'):
bot.conns[name] = SSLIRC(conf['server'], conf['nick'], conf=conf, exit()
port=conf.get('port', 6667), channels=conf['channels'],
ignore_certificate_errors=conf.get('ignore_cert', True))
else:
bot.conns[name] = IRC(conf['server'], conf['nick'], conf=conf,
port=conf.get('port', 6667), channels=conf['channels'])
except Exception, e: except Exception, e:
print 'ERROR: malformed config file', e print 'ERROR: malformed config file:', e
traceback.print_exc()
sys.exit() sys.exit()
bot.persist_dir = os.path.abspath('persist')
if not os.path.exists(bot.persist_dir):
os.mkdir(bot.persist_dir)
print 'Running main loop' print 'Running main loop'
while True: while True:

View File

@ -45,6 +45,14 @@ def config():
try: try:
bot.config = json.load(open('config')) bot.config = json.load(open('config'))
bot._config_mtime = config_mtime bot._config_mtime = config_mtime
for name, conf in bot.config['connections'].iteritems():
if name in bot.conns:
bot.conns[name].set_conf(conf)
else:
if conf.get('ssl'):
bot.conns[name] = SSLIRC(conf)
else:
bot.conns[name] = IRC(conf)
except ValueError, e: except ValueError, e:
print 'ERROR: malformed config!', e print 'ERROR: malformed config!', e

View File

@ -130,12 +130,8 @@ class IRC(object):
"handles the IRC protocol" "handles the IRC protocol"
# see the docs/ folder for more information on the protocol # see the docs/ folder for more information on the protocol
def __init__(self, server, nick, port=6667, channels=[], conf={}): def __init__(self, conf):
self.channels = channels self.set_conf(conf)
self.conf = conf
self.server = server
self.port = port
self.nick = nick
self.out = Queue.Queue() # responses from the server are placed here self.out = Queue.Queue() # responses from the server are placed here
# format: [rawline, prefix, command, params, # format: [rawline, prefix, command, params,
@ -144,17 +140,23 @@ class IRC(object):
thread.start_new_thread(self.parse_loop, ()) thread.start_new_thread(self.parse_loop, ())
def set_conf(self, conf):
self.conf = conf
self.nick = self.conf['nick']
self.server = self.conf['server']
def create_connection(self): def create_connection(self):
return crlf_tcp(self.server, self.port) return crlf_tcp(self.server, self.conf.get('port', 6667))
def connect(self): def connect(self):
self.conn = self.create_connection() self.conn = self.create_connection()
thread.start_new_thread(self.conn.run, ()) thread.start_new_thread(self.conn.run, ())
self.set_pass(self.conf.get('server_password')) self.cmd("NICK", [self.nick])
self.set_nick(self.nick)
self.cmd("USER", self.cmd("USER",
[conf.get('user', 'skybot'), "3", "*", conf.get('realname', [self.conf.get('user', 'skybot'), "3", "*", self.conf.get('realname',
'Python bot - http://github.com/rmmh/skybot')]) 'Python bot - http://github.com/rmmh/skybot')])
if 'server_password' in self.conf:
self.cmd("PASS", [self.conf['server_password']])
def parse_loop(self): def parse_loop(self):
while True: while True:
@ -180,13 +182,6 @@ class IRC(object):
if command == "PING": if command == "PING":
self.cmd("PONG", paramlist) self.cmd("PONG", paramlist)
def set_pass(self, password):
if password:
self.cmd("PASS", [password])
def set_nick(self, nick):
self.cmd("NICK", [nick])
def join(self, channel): def join(self, channel):
self.cmd("JOIN", channel.split(" ")) # [chan, password] self.cmd("JOIN", channel.split(" ")) # [chan, password]
@ -206,13 +201,8 @@ class IRC(object):
class FakeIRC(IRC): class FakeIRC(IRC):
def __init__(self, server, nick, port=6667, channels=[], conf={}, fn=""): def __init__(self, conf):
self.channels = channels self.set_conf(conf)
self.conf = conf
self.server = server
self.port = port
self.nick = nick
self.out = Queue.Queue() # responses from the server are placed here self.out = Queue.Queue() # responses from the server are placed here
self.f = open(fn, 'rb') self.f = open(fn, 'rb')
@ -249,10 +239,5 @@ class FakeIRC(IRC):
class SSLIRC(IRC): class SSLIRC(IRC):
def __init__(self, server, nick, port=6667, channels=[], conf={},
ignore_certificate_errors=True):
self.ignore_cert_errors = ignore_certificate_errors
IRC.__init__(self, server, nick, port, channels, conf)
def create_connection(self): def create_connection(self):
return crlf_ssl_tcp(self.server, self.port, self.ignore_cert_errors) return crlf_ssl_tcp(self.server, self.conf.get('port', 6697), self.conf.get('ignore_cert', True))

View File

@ -26,7 +26,7 @@ def get_version():
@hook.event('KICK') @hook.event('KICK')
def rejoin(paraml, conn=None): def rejoin(paraml, conn=None):
if paraml[1] == conn.nick: if paraml[1] == conn.nick:
if paraml[0].lower() in conn.channels: if paraml[0].lower() in conn.conf.get("channels", []):
conn.join(paraml[0]) conn.join(paraml[0])
@ -52,7 +52,7 @@ def onjoin(paraml, conn=None):
conn.cmd('MODE', [conn.nick, mode]) conn.cmd('MODE', [conn.nick, mode])
# join channels # join channels
for channel in conn.channels: for channel in conn.conf.get("channels", []):
conn.join(channel) conn.join(channel)
time.sleep(1) # don't flood JOINs time.sleep(1) # don't flood JOINs

View File

@ -44,10 +44,11 @@ def sieve_suite(bot, input, func, kind, args):
if input.nick.lower() in acl['blacklist-nicks']: if input.nick.lower() in acl['blacklist-nicks']:
return None return None
if args.get('adminonly', False): admins = input.conn.conf.get('admins', [])
admins = bot.config.get('admins', []) input.admin = input.host in admins or input.nick in admins
if input.host not in admins and input.nick not in admins: if args.get('adminonly', False):
if not input.admin:
return None return None
return input return input