diff --git a/commands/add.py b/commands/add.py deleted file mode 100644 index e2eac5d..0000000 --- a/commands/add.py +++ /dev/null @@ -1,111 +0,0 @@ -import main -import core.helper as helper - -class Add: - def __init__(self, register): - register("add", self.add) - - def add(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): - if authed: - if length > 6: - failure("Too many arguments") - return - - if length > 1: - name = spl[1] - if name.isdigit(): - failure("Network name is all numbers. This will break things.") - return - else: - incUsage("add") - return - if length > 2: - host = spl[2] - if length > 3: - port = spl[3] - if length > 4: - protocol = spl[4] - if length > 5: - nickname = spl[5] - - toFail = False - if length < 6: - if main.config["Default"]["nickname"] == None: - failure("Choose a nickname, or configure one in defaults") - toFail = True - else: - nickname = main.config["Default"]["nickname"] - - if length < 5: - if main.config["Default"]["protocol"] == None: - failure("Choose a protocol, or configure one in defaults") - toFail = True - else: - protocol = main.config["Default"]["protocol"] - - if length < 4: - if main.config["Default"]["port"] == None: - failure("Choose a port, or configure one in defaults") - toFail = True - else: - port = main.config["Default"]["port"] - - if length < 3: - if main.config["Default"]["host"] == None: - failure("Choose a host, or configure one in defaults") - toFail = True - else: - host = main.config["Default"]["host"] - if toFail: - failure("Stopping due to previous error(s)") - return - - if length < 2: - incUsage("add") - return - - if name in main.pool.keys(): - failure("Name already exists: %s" % name) - return - - protocol = protocol.lower() - - if not protocol in ["ssl", "plain"]: - failure("Protocol must be ssl or plain, not %s" % protocol) - return - - if not port.isdigit(): - failure("Port must be an integer, not %s" % port) - return - - main.pool[name] = { "host": host, - "port": port, - "protocol": protocol, - "bind": main.config["Default"]["bind"], - "timeout": main.config["Default"]["timeout"], - "maxdelay": main.config["Default"]["maxdelay"], - "initialdelay": main.config["Default"]["initialdelay"], - "factor": main.config["Default"]["factor"], - "jitter": main.config["Default"]["jitter"], - "nickname": nickname, - "username": main.config["Default"]["username"], - "realname": main.config["Default"]["realname"], - "userinfo": main.config["Default"]["userinfo"], - "finger": main.config["Default"]["finger"], - "version": main.config["Default"]["version"], - "source": main.config["Default"]["source"], - "autojoin": main.config["Default"]["autojoin"], - "authtype": main.config["Default"]["authtype"], - "password": main.config["Default"]["password"], - "authentity": main.config["Default"]["authentity"], - "key": main.config["Default"]["key"], - "certificate": main.config["Default"]["certificate"], - "enabled": main.config["ConnectOnCreate"], - } - if main.config["ConnectOnCreate"] == True: - helper.addBot(name) - success("Successfully created bot") - main.saveConf("pool") - return - else: - incUsage(None) diff --git a/commands/alias.py b/commands/alias.py index 8baa85b..0b127ed 100644 --- a/commands/alias.py +++ b/commands/alias.py @@ -2,8 +2,8 @@ import main from yaml import dump class Alias: - def __init__(self, register): - register("alias", self.alias) + def __init__(self, *args): + self.alias(*args) def alias(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/chans.py b/commands/chans.py index 851b9e8..3f00f43 100644 --- a/commands/chans.py +++ b/commands/chans.py @@ -2,8 +2,8 @@ import main import modules.userinfo as userinfo class Chans: - def __init__(self, register): - register("chans", self.chans) + def __init__(self, *args): + self.chans(*args) def chans(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/cmd.py b/commands/cmd.py new file mode 100644 index 0000000..5908bda --- /dev/null +++ b/commands/cmd.py @@ -0,0 +1,24 @@ +import main +from core.bot import deliverRelayCommands + +class Cmd: + def __init__(self, *args): + self.cmd(*args) + + def cmd(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + if authed: + if length > 4: + if not spl[1] in main.relay.keys(): + failure("No such relay: %s" % spl[1]) + return + + commands = {spl[3]: [" ".join(spl[4:])]} + print(" ".join(spl[4:])) + success("Sending commands to relay %s as user %s" % (spl[1], spl[2])) + deliverRelayCommands(spl[1], commands, user=spl[2]) + return + else: + incUsage("cmd") + return + else: + incUsage(None) diff --git a/commands/delete.py b/commands/del.py similarity index 92% rename from commands/delete.py rename to commands/del.py index 2476363..9d4868a 100644 --- a/commands/delete.py +++ b/commands/del.py @@ -1,8 +1,8 @@ import main -class Delete: - def __init__(self, register): - register("del", self.delete) +class Del: + def __init__(self, *args): + self.delete(*args) def delete(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/disable.py b/commands/disable.py index 681f155..97c5742 100644 --- a/commands/disable.py +++ b/commands/disable.py @@ -1,8 +1,9 @@ import main +from core.bot import deliverRelayCommands class Disable: - def __init__(self, register): - register("disable", self.disable) + def __init__(self, *args): + self.disable(*args) def disable(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: @@ -11,6 +12,11 @@ class Disable: failure("Name does not exist: %s" % spl[1]) return main.pool[spl[1]]["enabled"] = False + user = main.pool[spl[1]]["alias"] + network = main.pool[spl[1]]["network"] + relay = main.pool[spl[1]]["relay"] + commands = {"status": ["Disconnect"]} + deliverRelayCommands(relay, commands, user=user+"/"+network) main.saveConf("pool") if spl[1] in main.ReactorPool.keys(): if spl[1] in main.FactoryPool.keys(): diff --git a/commands/dist.py b/commands/dist.py index 6fd579c..3c94f5d 100644 --- a/commands/dist.py +++ b/commands/dist.py @@ -2,8 +2,8 @@ import main from subprocess import run, PIPE class Dist: - def __init__(self, register): - register("dist", self.dist) + def __init__(self, *args): + self.dist(*args) def dist(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/enable.py b/commands/enable.py index 3fc7a2d..d6a29c5 100644 --- a/commands/enable.py +++ b/commands/enable.py @@ -1,9 +1,10 @@ import main -import core.helper as helper +from core.helper import startBot +from core.bot import deliverRelayCommands class Enable: - def __init__(self, register): - register("enable", self.enable) + def __init__(self, *args): + self.enable(*args) def enable(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: @@ -12,9 +13,14 @@ class Enable: failure("Name does not exist: %s" % spl[1]) return main.pool[spl[1]]["enabled"] = True + user = main.pool[spl[1]]["alias"] + network = main.pool[spl[1]]["network"] + relay = main.pool[spl[1]]["relay"] + commands = {"status": ["Connect"]} + deliverRelayCommands(relay, commands, user=user+"/"+network) main.saveConf("pool") if not spl[1] in main.IRCPool.keys(): - helper.addBot(spl[1]) + startBot(spl[1]) else: pass success("Successfully enabled bot %s" % spl[1]) diff --git a/commands/get.py b/commands/get.py index 3c302b8..51ccd7f 100644 --- a/commands/get.py +++ b/commands/get.py @@ -1,8 +1,8 @@ import main class Get: - def __init__(self, register): - register("get", self.get) + def __init__(self, *args): + self.get(*args) def get(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/help.py b/commands/help.py index 46a9782..6e3e229 100644 --- a/commands/help.py +++ b/commands/help.py @@ -1,8 +1,8 @@ import main class Help: - def __init__(self, register): - register("help", self.help) + def __init__(self, *args): + self.help(*args) def help(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/join.py b/commands/join.py index 3be5708..0b1c223 100644 --- a/commands/join.py +++ b/commands/join.py @@ -1,8 +1,8 @@ import main class Join: - def __init__(self, register): - register("join", self.join) + def __init__(self, *args): + self.join(*args) def join(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/key.py b/commands/key.py index e8fb4db..d6b88a7 100644 --- a/commands/key.py +++ b/commands/key.py @@ -2,8 +2,8 @@ import main import modules.keyword as keyword class Key: - def __init__(self, register): - register("key", self.key) + def __init__(self, *args): + self.key(*args) def key(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/list.py b/commands/list.py index 13be3eb..3edef6d 100644 --- a/commands/list.py +++ b/commands/list.py @@ -2,8 +2,8 @@ import main from yaml import dump class List: - def __init__(self, register): - register("list", self.list) + def __init__(self, *args): + self.list(*args) def list(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/load.py b/commands/load.py index dc35292..8038c5b 100644 --- a/commands/load.py +++ b/commands/load.py @@ -1,8 +1,8 @@ import main class Load: - def __init__(self, register): - register("load", self.load) + def __init__(self, *args): + self.list(*args) def load(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/loadmod.py b/commands/loadmod.py index 18a403e..c5c463f 100644 --- a/commands/loadmod.py +++ b/commands/loadmod.py @@ -2,15 +2,18 @@ import main from utils.loaders.single_loader import loadSingle class Loadmod: - def __init__(self, register): - register("loadmod", self.loadmod) + def __init__(self, *args): + self.loadmod(*args) def loadmod(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: if length == 2: - rtrn = loadSingle(spl[1], main.register) + rtrn = loadSingle(spl[1]) if rtrn == True: - success("Loaded module %s" % spl[1]) + success("Loaded module: %s" % spl[1]) + return + elif rtrn == "RELOAD": + success("Reloaded module: %s" % spl[1]) return else: failure("Error loading module %s: %s" % (spl[1], rtrn)) diff --git a/commands/logout.py b/commands/logout.py index a50ea02..960ac1e 100644 --- a/commands/logout.py +++ b/commands/logout.py @@ -1,8 +1,8 @@ import main class Logout: - def __init__(self, register): - register("logout", self.logout) + def __init__(self, *args): + self.logout(*args) def logout(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/mod.py b/commands/mod.py index 0606c40..d11b33b 100644 --- a/commands/mod.py +++ b/commands/mod.py @@ -2,12 +2,11 @@ import main from yaml import dump class Mod: - def __init__(self, register): - register("mod", self.mod) + def __init__(self, *args): + self.mod(*args) def mod(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: - toUnset = False if length == 2: if not spl[1] in main.pool.keys(): failure("Name does not exist: %s" % spl[1]) @@ -33,51 +32,17 @@ class Mod: failure("No such key: %s" % spl[2]) return - if spl[2] == "protocol": - if not spl[3] in ["ssl", "plain"]: - failure("Protocol must be ssl or plain, not %s" % spl[3]) - return if spl[3] == main.pool[spl[1]][spl[2]]: failure("Value already exists: %s" % spl[3]) return - if spl[3].lower() in ["none", "nil"]: - spl[3] = None - toUnset = True - - if spl[2] in ["port", "timeout", "maxdelay"]: - try: - spl[3] = int(spl[3]) - except: - failure("Value must be an integer, not %s" % spl[3]) - return - - if spl[2] in ["initialdelay", "factor", "jitter"]: - try: - spl[3] = float(spl[3]) - except: - failure("Value must be a floating point integer, not %s" % spl[3]) - return - - if spl[2] == "authtype": - if not toUnset: - if not spl[3] in ["sp", "ns"]: - failure("Authtype must be sp or ns, not %s" % spl[3]) - return if spl[2] == "enabled": failure("Use the enable and disable commands to manage this") return - if spl[2] == "autojoin": - spl[3] = spl[3].split(",") main.pool[spl[1]][spl[2]] = spl[3] - if spl[1] in main.IRCPool.keys(): - main.IRCPool[spl[1]].refresh() main.saveConf("pool") - if toUnset: - success("Successfully unset key %s on %s" % (spl[2], spl[1])) - else: - success("Successfully set key %s to %s on %s" % (spl[2], spl[3], spl[1])) + success("Successfully set key %s to %s on %s" % (spl[2], spl[3], spl[1])) return else: diff --git a/commands/mon.py b/commands/mon.py index 214b363..0a05d7d 100644 --- a/commands/mon.py +++ b/commands/mon.py @@ -5,8 +5,8 @@ from io import StringIO from yaml import dump class Mon: - def __init__(self, register): - register("mon", self.mon) + def __init__(self, *args): + self.mon(*args) def setup_arguments(self, ArgumentParser): self.parser = ArgumentParser(prog="mon", description="Manage monitors. Extremely flexible. All arguments are optional.") diff --git a/commands/msg.py b/commands/msg.py index a3bad7e..3638c28 100644 --- a/commands/msg.py +++ b/commands/msg.py @@ -1,8 +1,8 @@ import main class Msg: - def __init__(self, register): - register("msg", self.msg) + def __init__(self, *args): + self.msg(*args) def msg(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/network.py b/commands/network.py index e78d87d..024042e 100644 --- a/commands/network.py +++ b/commands/network.py @@ -2,8 +2,8 @@ import main from yaml import dump class Network: - def __init__(self, register): - register("network", self.network) + def __init__(self, *args): + self.network(*args) def network(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/part.py b/commands/part.py index a1adacb..67bf539 100644 --- a/commands/part.py +++ b/commands/part.py @@ -1,8 +1,8 @@ import main class Part: - def __init__(self, register): - register("part", self.part) + def __init__(self, *args): + self.part(*args) def part(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/password.py b/commands/pass.py similarity index 87% rename from commands/password.py rename to commands/pass.py index 6f88617..20f4d9a 100644 --- a/commands/password.py +++ b/commands/pass.py @@ -1,8 +1,8 @@ import main -class Password: - def __init__(self, register): - register("pass", self.password) +class Pass: + def __init__(self, *args): + self.password(*args) def password(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/provision.py b/commands/provision.py index e1c2556..a9df1b3 100644 --- a/commands/provision.py +++ b/commands/provision.py @@ -2,8 +2,8 @@ import main from modules import provision class Provision: - def __init__(self, register): - register("provision", self.provision) + def __init__(self, *args): + self.provision(*args) def provision(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/relay.py b/commands/relay.py index 0ad2bf4..894c80b 100644 --- a/commands/relay.py +++ b/commands/relay.py @@ -2,8 +2,8 @@ import main from yaml import dump class Relay: - def __init__(self, register): - register("relay", self.relay) + def __init__(self, *args): + self.relay(*args) def relay(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/save.py b/commands/save.py index 492896b..edd4d1c 100644 --- a/commands/save.py +++ b/commands/save.py @@ -1,8 +1,8 @@ import main class Save: - def __init__(self, register): - register("save", self.save) + def __init__(self, *args): + self.save(*args) def save(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/stats.py b/commands/stats.py index eab9a01..5b08b7e 100644 --- a/commands/stats.py +++ b/commands/stats.py @@ -4,8 +4,8 @@ import modules.userinfo as userinfo from string import digits class Stats: - def __init__(self, register): - register("stats", self.stats) + def __init__(self, *args): + self.stats(*args) def stats(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/users.py b/commands/users.py index a76e20e..9836a41 100644 --- a/commands/users.py +++ b/commands/users.py @@ -2,8 +2,8 @@ import main import modules.userinfo as userinfo class Users: - def __init__(self, register): - register("users", self.users) + def __init__(self, *args): + self.users(*args) def users(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/commands/who.py b/commands/who.py index 9898cfc..7ae4909 100644 --- a/commands/who.py +++ b/commands/who.py @@ -2,8 +2,8 @@ import main import modules.userinfo as userinfo class Who: - def __init__(self, register): - register("who", self.who) + def __init__(self, *args): + self.who(*args) def who(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): if authed: diff --git a/conf/help.json b/conf/help.json index 0c77572..2ecefc7 100644 --- a/conf/help.json +++ b/conf/help.json @@ -23,5 +23,6 @@ "alias": "alias [ ]", "relay": "relay [ ]", "network": "network [
]", - "provision": "provision []" + "provision": "provision []", + "cmd": "cmd " } diff --git a/core/bot.py b/core/bot.py index 3f063c6..2d54174 100644 --- a/core/bot.py +++ b/core/bot.py @@ -95,44 +95,27 @@ class IRCBot(IRCClient): error("Network with all numbers: %s" % name) self.buffer = "" self.name = name - instance = main.pool[name] - - self.nickname = instance["nickname"] - self.realname = instance["realname"] - self.username = instance["username"] - self.userinfo = instance["userinfo"] - self.fingerReply = instance["finger"] - self.versionName = instance["version"] + inst = main.pool[name] + alias = main.alias[inst["alias"]] + relay = main.relay[inst["relay"]] + network = main.network[inst["network"]] + + self.nickname = alias["nick"] + self.realname = alias["realname"] + self.username = inst["alias"]+"/"+inst["network"] + self.password = relay["password"] + self.userinfo = None + self.fingerReply = None + self.versionName = None self.versionNum = None self.versionEnv = None - self.sourceURL = instance["source"] - self.autojoin = instance["autojoin"] + self.sourceURL = None self._who = {} self._getWho = {} self._names = {} - self.authtype = instance["authtype"] - if self.authtype == "ns": - self.authpass = instance["password"] - self.authentity = instance["authentity"] - else: - self.password = instance["password"] - - def refresh(self): - instance = main.pool[self.name] - if not instance["nickname"] == self.nickname: - self.nickname = instance["nickname"] - self.setNick(self.nickname) - - self.userinfo = instance["userinfo"] - self.fingerReply = instance["finger"] - self.versionName = instance["version"] - self.versionNum = None - self.versionEnv = None - self.sourceURL = instance["source"] - def parsen(self, user): step = user.split("!") nick = step[0] @@ -350,10 +333,6 @@ class IRCBot(IRCClient): log("signed on: %s" % self.name) if main.config["Notifications"]["Connection"]: keyword.sendMaster("SIGNON: %s" % self.name) - if self.authtype == "ns": - self.msg(self.authentity, "IDENTIFY %s" % self.nspass) - for i in self.autojoin: - self.join(i) count.event(self.net, "signedon") def joined(self, channel): @@ -454,7 +433,6 @@ class IRCBotFactory(ReconnectingClientFactory): def __init__(self, name, relay=None, relayCommands=None, user=None, stage2=None): if not name == None: self.name = name - self.instance = main.pool[name] self.net = "".join([x for x in self.name if not x in digits]) else: self.name = "Relay to "+relay diff --git a/core/helper.py b/core/helper.py index 570fd2c..fa6af95 100644 --- a/core/helper.py +++ b/core/helper.py @@ -1,39 +1,38 @@ from twisted.internet import reactor from core.bot import IRCBot, IRCBotFactory +from twisted.internet.ssl import DefaultOpenSSLContextFactory import main from utils.logging.log import * -def addBot(name): - instance = main.pool[name] - log("Started bot %s to %s:%s protocol %s nickname %s" % (name, - instance["host"], - instance["port"], - instance["protocol"], - instance["nickname"])) +def startBot(name): + inst = main.pool[name] + relay, alias, network = inst["relay"], inst["alias"], inst["network"] + host = main.relay[relay]["host"] + port = int(main.relay[relay]["port"]) + log("Started bot %s to %s network %s" % (name, relay, network)) - if instance["protocol"] == "ssl": - keyFN = main.certPath+main.config["Key"] - certFN = main.certPath+main.config["Certificate"] - contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"), - certFN.encode("utf-8", "replace")) - if instance["bind"] == None: - bot = IRCBotFactory(name) - rct = reactor.connectSSL(instance["host"], - int(instance["port"]), - bot, contextFactory) + keyFN = main.certPath+main.config["Key"] + certFN = main.certPath+main.config["Certificate"] + contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"), + certFN.encode("utf-8", "replace")) + if "bind" in main.relay[relay].keys(): + bot = IRCBotFactory(name) + rct = reactor.connectSSL(host, + port, + bot, contextFactory, + bindAddress=main.relay[relay]["bind"]) - main.ReactorPool[name] = rct - main.FactoryPool[name] = bot - return - else: + main.ReactorPool[name] = rct + main.FactoryPool[name] = bot + return + else: + bot = IRCBotFactory(name) + rct = reactor.connectSSL(host, + port, + bot, contextFactory) - bot = IRCBotFactory(name) - rct = reactor.connectSSL(instance["host"], - int(instance["port"]), - bot, contextFactory, - bindAddress=instance["bind"]) + main.ReactorPool[name] = rct + main.FactoryPool[name] = bot + return - main.ReactorPool[name] = rct - main.FactoryPool[name] = bot - return diff --git a/core/parser.py b/core/parser.py index a8a001e..64270f8 100644 --- a/core/parser.py +++ b/core/parser.py @@ -22,9 +22,8 @@ def parseCommand(addr, authed, data): else: failure("No text was sent") return - for i in main.CommandMap.keys(): - if spl[0] == i: - main.CommandMap[i](addr, authed, data, obj, spl, success, failure, info, incUsage, length) - return + if spl[0] in main.CommandMap.keys(): + main.CommandMap[spl[0]](addr, authed, data, obj, spl, success, failure, info, incUsage, length) + return incUsage(None) return diff --git a/main.py b/main.py index 75fe1e7..05c8deb 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,6 @@ from json import load, dump, loads -import redis +from redis import StrictRedis from string import digits -from utils.loaders.command_loader import loadCommands from utils.logging.log import * configPath = "conf/" @@ -43,13 +42,6 @@ def liveNets(): networks.add("".join([x for x in i if not x in digits])) return networks -def register(command, function): - if not command in CommandMap: - CommandMap[command] = function - debug("Registered command: %s" % command) - else: - error("Duplicate command: %s" % (command)) - def saveConf(var): with open(configPath+filemap[var][0], "w") as f: dump(globals()[var], f, indent=4) @@ -66,7 +58,6 @@ def initConf(): def initMain(): global r initConf() - loadCommands(register) - r = redis.StrictRedis(unix_socket_path=config["RedisSocket"], db=0) + r = StrictRedis(unix_socket_path=config["RedisSocket"], db=0) diff --git a/modules/provision.py b/modules/provision.py index 8d5f96a..34438c0 100644 --- a/modules/provision.py +++ b/modules/provision.py @@ -1,6 +1,7 @@ import main from core.bot import deliverRelayCommands from utils.logging.log import * +from core.helper import startBot def provisionUserData(relay, alias, nick, altnick, ident, realname, password): commands = {} @@ -81,7 +82,10 @@ def provisionRelayForNetwork(relay, alias, network): else: main.pool[network+i] = {"relay": relay, "alias": alias, - "network": network} + "network": network, + "enabled": main.config["ConnectOnCreate"]} main.saveConf("pool") + if main.config["ConnectOnCreate"]: + startBot(network+i) storedNetwork = True return network+i diff --git a/threshold b/threshold index adab5cf..5ca234a 100755 --- a/threshold +++ b/threshold @@ -1,7 +1,6 @@ #!/usr/bin/env python from twisted.internet import reactor from twisted.internet.ssl import DefaultOpenSSLContextFactory - #from twisted.python import log #from sys import stdout #log.startLogging(stdout) @@ -11,10 +10,12 @@ import main main.initMain() from utils.logging.log import * -import modules.userinfo as userinfo -import core.helper as helper +from utils.loaders.command_loader import loadCommands +from core.helper import startBot from core.server import Server, ServerFactory +loadCommands() + if __name__ == "__main__": listener = ServerFactory() if main.config["Listener"]["UseSSL"] == True: @@ -27,6 +28,6 @@ if __name__ == "__main__": if not "enabled" in main.pool[i]: continue if main.pool[i]["enabled"] == True: - helper.addBot(i) + startBot(i) reactor.run() diff --git a/utils/loaders/command_loader.py b/utils/loaders/command_loader.py index 62bc734..775d267 100644 --- a/utils/loaders/command_loader.py +++ b/utils/loaders/command_loader.py @@ -1,14 +1,21 @@ from os import listdir + from utils.logging.log import * import commands -def loadCommands(func): +from main import CommandMap + +def loadCommands(): for filename in listdir('commands'): if filename.endswith('.py') and filename != "__init__.py": commandName = filename[0:-3] className = commandName.capitalize() try: - __import__('commands.%s' % commandName) - eval('commands.%s.%s(func)' % (commandName, className)) + module = __import__('commands.%s' % commandName) + if not commandName in CommandMap: + CommandMap[commandName] = getattr(getattr(module, commandName), className) + debug("Registered command: %s" % commandName) + else: + error("Duplicate command: %s" % (commandName)) except Exception as err: error("Exception while loading command %s:\n%s" % (commandName, err)) diff --git a/utils/loaders/single_loader.py b/utils/loaders/single_loader.py index 543a6b5..3d891e2 100644 --- a/utils/loaders/single_loader.py +++ b/utils/loaders/single_loader.py @@ -1,17 +1,26 @@ from os import listdir -import main +from importlib import reload +from sys import modules + from utils.logging.log import * import commands -def loadSingle(command, func): - if command+".py" in listdir("commands"): +from main import CommandMap + +def loadSingle(commandName): + if commandName+".py" in listdir("commands"): + className = commandName.capitalize() try: - if command in main.CommandMap.keys(): - return "Cannot reload modules" - else: - className = command.capitalize() - __import__("commands.%s" % command) - eval("commands.%s.%s(func)" % (command, className)) - return True + if commandName in CommandMap.keys(): + reload(modules["commands."+commandName]) + CommandMap[commandName] = getattr(modules["commands."+commandName], className) + debug("Reloaded command: %s" % commandName) + return "RELOAD" + module = __import__('commands.%s' % commandName) + CommandMap[commandName] = getattr(getattr(module, commandName), className) + debug("Registered command: %s" % commandName) + return True + except Exception as err: return err + return False