lots of nonworking code

This commit is contained in:
Luke Rogers 2013-10-13 17:32:08 +13:00
parent ed2eefb9cd
commit d330df5711
9 changed files with 75 additions and 34 deletions

View file

@ -8,7 +8,9 @@ import collections
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import create_engine
from core import config, irc, main, loader
from core import config, irc, main
from core.permissions import PermissionManager
from core.loader import PluginLoader
def clean_name(n):
@ -49,29 +51,36 @@ class Bot(object):
self.start_time = time.time()
self.running = True
self.do_restart = False
self.connections = []
# set up config and logging
self.setup()
self.logger.debug("Bot setup completed.")
# start IRC connections
self.connections = {}
self.connect()
print self.connections
for conn in self.connections:
conn.permissions = PermissionManager(self, conn)
print conn
# run plugin loader
self.plugins = collections.defaultdict(list)
self.threads = {}
self.loader = loader.PluginLoader(self)
self.loader = PluginLoader(self)
def run(self):
"""recieves input from the IRC engine and processes it"""
self.logger.info("Starting main thread.")
while self.running:
for conn in self.connections.itervalues():
for conn in self.connections:
try:
incoming = conn.parsed_queue.get_nowait()
if incoming == StopIteration:
print "StopIteration"
# IRC engine has signalled timeout, so reconnect (ugly)
conn.connection.reconnect()
main.main(self, conn, incoming)
@ -79,7 +88,7 @@ class Bot(object):
pass
# if no messages are in the incoming queue, sleep
while all(connection.parsed_queue.empty() for connection in self.connections.itervalues()):
while self.running and all(c.parsed_queue.empty() for c in self.connections):
time.sleep(.1)
@ -97,7 +106,7 @@ class Bot(object):
self.logger.debug("Created data folder.")
# config
self.config = config.Config(self.logger)
self.config = config.Config(self)
self.logger.debug("Config object created.")
# db
@ -118,13 +127,13 @@ class Bot(object):
self.logger.debug("({}) Creating connection to {}.".format(name, server))
if conf['connection'].get('ssl'):
self.connections[name] = irc.SSLIRC(name, server, nick, conf = conf,
self.connections.append(irc.SSLIRC(name, server, nick, config = conf,
port = port, channels = conf['channels'],
ignore_certificate_errors=conf['connection'].get('ignore_cert', True))
ignore_certificate_errors=conf['connection'].get('ignore_cert', True)))
self.logger.debug("({}) Created SSL connection.".format(name))
else:
self.connections[name] = irc.IRC(name, server, nick, conf = conf,
port = port, channels = conf['channels'])
self.connections.append(irc.IRC(name, server, nick, config = conf,
port = port, channels = conf['channels']))
self.logger.debug("({}) Created connection.".format(name))
@ -153,9 +162,6 @@ class Bot(object):
self.running = False
# wait for the bot loop to stop
time.sleep(1)
def restart(self, reason=None):
"""shuts the bot down and restarts it"""

View file

@ -8,10 +8,11 @@ from watchdog.tricks import Trick
class Config(dict):
def __init__(self, logger, *args, **kwargs):
def __init__(self, bot, *args, **kwargs):
self.filename = "config.json"
self.path = os.path.abspath(self.filename)
self.logger = logger
self.bot = bot
self.logger = bot.logger
self.update(*args, **kwargs)
# populate self with config data
@ -35,6 +36,12 @@ class Config(dict):
self.update(json.load(f))
self.logger.info("Config loaded from file.")
# reload permissions
if self.bot.connections:
for conn in self.bot.connections:
conn.permissions.reload()
def save_config(self):
"""saves the contents of the config dict to the config file"""
json.dump(self, open(self.path, 'w'), sort_keys=True, indent=2)

View file

@ -4,6 +4,8 @@ import time
import threading
import Queue
from core import permissions
from ssl import wrap_socket, CERT_NONE, CERT_REQUIRED, SSLError
irc_prefix_rem = re.compile(r'(.*?) (.*?) (.*)').match
@ -203,10 +205,10 @@ class SSLIRCConnection(IRCConnection):
class IRC(object):
"""handles the IRC protocol"""
def __init__(self, name, server, nick, port=6667, channels=[], conf={}):
def __init__(self, name, server, nick, port=6667, channels=[], config={}):
self.name = name
self.channels = channels
self.conf = conf
self.config = config
self.server = server
self.port = port
self.nick = nick
@ -224,10 +226,10 @@ class IRC(object):
self.connection = self.create_connection()
self.connection.connect()
self.set_pass(self.conf.get('server_password'))
self.set_pass(self.config.get('server_password'))
self.set_nick(self.nick)
self.cmd("USER",
[self.conf.get('user', 'cloudbot'), "3", "*", self.conf.get('realname',
[self.config.get('user', 'cloudbot'), "3", "*", self.config.get('realname',
'CloudBot - http://git.io/cloudbot')])
self.parse_thread = ParseThread(self.input_queue, self.output_queue,
@ -235,6 +237,7 @@ class IRC(object):
self.parse_thread.daemon = True
self.parse_thread.start()
def create_connection(self):
return IRCConnection(self.name, self.server, self.port,
self.input_queue, self.output_queue)
@ -282,10 +285,10 @@ class IRC(object):
class SSLIRC(IRC):
def __init__(self, name, server, nick, port=6667, channels=[], conf={},
def __init__(self, name, server, nick, port=6667, channels=[], config={},
ignore_certificate_errors=True):
self.ignore_cert_errors = ignore_certificate_errors
IRC.__init__(self, name, server, nick, port, channels, conf)
IRC.__init__(self, name, server, nick, port, channels, config)
def create_connection(self):
return SSLIRCConnection(self.name, self.server, self.port, self.input_queue,

View file

@ -64,7 +64,7 @@ class PluginLoader(object):
namespace = {}
eval(code, namespace)
except Exception:
self.bot.logger.error("Error compiling {}.".format(filename))
self.bot.logger.error("Error compiling {}:".format(filename))
self.bot.logger.error(traceback.format_exc())
return

View file

@ -132,7 +132,7 @@ def dispatch(bot, input, kind, func, args, autohelp=False):
return
if not (not autohelp or not args.get('autohelp', True) or input.inp or not (func.__doc__ is not None)):
input.notice(input.conn.conf["command_prefix"] + func.__doc__)
input.notice(input.conn.config["command_prefix"] + func.__doc__)
return
if func._thread:
@ -156,7 +156,7 @@ def match_command(bot, command):
def main(bot, conn, out):
inp = Input(bot, conn, *out)
command_prefix = conn.conf.get('command_prefix', '.')
command_prefix = conn.config.get('command_prefix', '.')
# EVENTS
for func, args in bot.events[inp.command] + bot.events['*']:

18
core/permissions.py Normal file
View file

@ -0,0 +1,18 @@
class PermissionManager(object):
def __init__(self, bot, conn):
self.logger = bot.logger
self.logger.info("Creating permission manager for {}.".format(conn.name))
# stuff
self.bot = bot
self.conn = conn
self.config = conn.config
self.group_perms = {}
self.reload()
def reload(self):
self.logger.error("reload perms stub")
pass