fixed new config engine, moved IRC loading
This commit is contained in:
parent
8eff33b1f7
commit
507c59ad15
4 changed files with 43 additions and 43 deletions
36
cloudbot.py
36
cloudbot.py
|
@ -6,6 +6,7 @@ import os
|
||||||
import Queue
|
import Queue
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
import time
|
||||||
|
|
||||||
sys.path += ['plugins', 'lib', 'core'] # add stuff to the sys.path for easy imports
|
sys.path += ['plugins', 'lib', 'core'] # add stuff to the sys.path for easy imports
|
||||||
os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
|
os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
|
||||||
|
@ -14,36 +15,13 @@ print 'CloudBot DEV <http://git.io/cloudbotirc>'
|
||||||
|
|
||||||
# create new bot object
|
# create new bot object
|
||||||
bot = _bot.Bot("cloudbot")
|
bot = _bot.Bot("cloudbot")
|
||||||
|
bot.logger.debug("Bot initalized.")
|
||||||
print 'Begin Plugin Loading.'
|
|
||||||
print bot.config
|
|
||||||
|
|
||||||
# bootstrap the reloader
|
# bootstrap the reloader
|
||||||
eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
|
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)
|
||||||
|
|
||||||
print 'Connecting to IRC...'
|
|
||||||
|
|
||||||
bot.conns = {}
|
|
||||||
print bot.config
|
|
||||||
|
|
||||||
try:
|
|
||||||
for name, conf in bot.config['connections'].iteritems():
|
|
||||||
# strip all spaces and capitalization from the connection name
|
|
||||||
name = name.replace(" ", "_")
|
|
||||||
name = re.sub('[^A-Za-z0-9_]+', '', name)
|
|
||||||
print 'Connecting to server: %s' % conf['server']
|
|
||||||
if conf.get('ssl'):
|
|
||||||
bot.conns[name] = SSLIRC(name, conf['server'], conf['nick'], conf=conf,
|
|
||||||
port=conf.get('port', 6667), channels=conf['channels'],
|
|
||||||
ignore_certificate_errors=conf.get('ignore_cert', True))
|
|
||||||
else:
|
|
||||||
bot.conns[name] = IRC(name, conf['server'], conf['nick'], conf=conf,
|
|
||||||
port=conf.get('port', 6667), channels=conf['channels'])
|
|
||||||
except Exception as e:
|
|
||||||
print 'ERROR: malformed config file', e
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
bot.persist_dir = os.path.abspath('persist')
|
bot.persist_dir = os.path.abspath('persist')
|
||||||
if not os.path.exists(bot.persist_dir):
|
if not os.path.exists(bot.persist_dir):
|
||||||
|
@ -53,13 +31,13 @@ print 'Connection(s) made, starting main loop.'
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
reload() # these functions only do things
|
reload() # these functions only do things
|
||||||
config() # if changes have occured
|
# if changes have occured
|
||||||
|
|
||||||
for conn in bot.conns.itervalues():
|
for connection in bot.connections.itervalues():
|
||||||
try:
|
try:
|
||||||
out = conn.out.get_nowait()
|
out = connection.out.get_nowait()
|
||||||
main(conn, out)
|
main(connection, out)
|
||||||
except Queue.Empty:
|
except Queue.Empty:
|
||||||
pass
|
pass
|
||||||
while all(conn.out.empty() for conn in bot.conns.itervalues()):
|
while all(connection.out.empty() for connection in bot.connections.itervalues()):
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
|
|
27
core/bot.py
27
core/bot.py
|
@ -1,7 +1,13 @@
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
|
|
||||||
import config
|
import config
|
||||||
|
import irc
|
||||||
|
|
||||||
|
|
||||||
|
def clean_name(n): return re.sub('[^A-Za-z0-9_]+', '', n.replace(" ", "_"))
|
||||||
|
|
||||||
|
|
||||||
class Bot(object):
|
class Bot(object):
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
|
@ -11,8 +17,25 @@ class Bot(object):
|
||||||
|
|
||||||
# set up config and logging
|
# set up config and logging
|
||||||
self.setup()
|
self.setup()
|
||||||
print self.config
|
|
||||||
|
|
||||||
|
# start IRC connections
|
||||||
|
self.connections = {}
|
||||||
|
self.connect()
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
|
||||||
|
for name, conf in self.config['connections'].iteritems():
|
||||||
|
# strip all spaces and capitalization from the connection name
|
||||||
|
name = clean_name(name)
|
||||||
|
self.logger.debug("({}) Creating connection.".format(name))
|
||||||
|
self.logger.debug("({}) Server: {}".format(name, conf['server']))
|
||||||
|
if conf.get('ssl'):
|
||||||
|
self.connections[name] = irc.SSLIRC(name, conf['server'], conf['nick'], conf=conf,
|
||||||
|
port=conf.get('port', 6667), channels=conf['channels'],
|
||||||
|
ignore_certificate_errors=conf.get('ignore_cert', True))
|
||||||
|
else:
|
||||||
|
self.connections[name] = irc.IRC(name, conf['server'], conf['nick'], conf=conf,
|
||||||
|
port=conf.get('port', 6667), channels=conf['channels'])
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
# logging
|
# logging
|
||||||
|
@ -21,7 +44,7 @@ class Bot(object):
|
||||||
|
|
||||||
# logging
|
# logging
|
||||||
self.config = self.get_config()
|
self.config = self.get_config()
|
||||||
self.config.reload()
|
self.config.load_config()
|
||||||
self.logger.debug("Config loaded.")
|
self.logger.debug("Config loaded.")
|
||||||
|
|
||||||
def get_config(self):
|
def get_config(self):
|
||||||
|
|
|
@ -7,11 +7,10 @@ class Config(dict):
|
||||||
self.path = os.path.abspath("{}.json".format(name))
|
self.path = os.path.abspath("{}.json".format(name))
|
||||||
self.update(*args, **kwargs)
|
self.update(*args, **kwargs)
|
||||||
|
|
||||||
def reload(self):
|
def load_config(self):
|
||||||
with open(self.path) as f:
|
with open(self.path) as f:
|
||||||
self = json.load(f)
|
self.update(json.load(f))
|
||||||
print self
|
|
||||||
|
|
||||||
|
|
||||||
def save(self):
|
def save_config(self):
|
||||||
pass
|
pass
|
16
core/irc.py
16
core/irc.py
|
@ -17,13 +17,13 @@ def decode(txt):
|
||||||
|
|
||||||
|
|
||||||
def censor(text):
|
def censor(text):
|
||||||
text = text.replace('\n', '').replace('\r', '')
|
#text = text.replace('\n', '').replace('\r', '')
|
||||||
replacement = '[censored]'
|
#replacement = '[censored]'
|
||||||
if 'censored_strings' in bot.config:
|
#if 'censored_strings' in bot.config:
|
||||||
if bot.config['censored_strings']:
|
# if bot.config['censored_strings']:
|
||||||
words = map(re.escape, bot.config['censored_strings'])
|
# words = map(re.escape, bot.config['censored_strings'])
|
||||||
regex = re.compile('({})'.format("|".join(words)))
|
# regex = re.compile('({})'.format("|".join(words)))
|
||||||
text = regex.sub(replacement, text)
|
# text = regex.sub(replacement, text)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ class IRC(object):
|
||||||
self.set_pass(self.conf.get('server_password'))
|
self.set_pass(self.conf.get('server_password'))
|
||||||
self.set_nick(self.nick)
|
self.set_nick(self.nick)
|
||||||
self.cmd("USER",
|
self.cmd("USER",
|
||||||
[conf.get('user', 'cloudbot'), "3", "*", conf.get('realname',
|
[self.conf.get('user', 'cloudbot'), "3", "*", self.conf.get('realname',
|
||||||
'CloudBot - http://git.io/cloudbot')])
|
'CloudBot - http://git.io/cloudbot')])
|
||||||
|
|
||||||
def parse_loop(self):
|
def parse_loop(self):
|
||||||
|
|
Reference in a new issue