Renovate the module system and implement adding and resuming pool instances using the new relay/alias/network system

This commit is contained in:
Mark Veidemanis 2019-01-26 18:58:21 +00:00
parent 4efea3f535
commit 8926cb76ec
37 changed files with 184 additions and 302 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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:

24
commands/cmd.py Normal file
View File

@ -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)

View File

@ -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:

View File

@ -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():

View File

@ -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:

View File

@ -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])

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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))

View File

@ -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:

View File

@ -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:

View File

@ -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.")

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -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:

View File

@ -23,5 +23,6 @@
"alias": "alias <add|del|list> [<alias> <nickname> <altnick> <ident> <realname> <password>]",
"relay": "relay <add|del|list> [<relay> <host> <port> <user> <password>]",
"network": "network <add|del|list> [<name> <address> <port> <ssl|plain> <sasl|ns|none>]",
"provision": "provision <relay> <alias> [<network>]"
"provision": "provision <relay> <alias> [<network>]",
"cmd": "cmd <relay> <user> <entity> <text ...>"
}

View File

@ -95,44 +95,27 @@ class IRCBot(IRCClient):
error("Network with all numbers: %s" % name)
self.buffer = ""
self.name = name
instance = main.pool[name]
inst = main.pool[name]
alias = main.alias[inst["alias"]]
relay = main.relay[inst["relay"]]
network = main.network[inst["network"]]
self.nickname = instance["nickname"]
self.realname = instance["realname"]
self.username = instance["username"]
self.userinfo = instance["userinfo"]
self.fingerReply = instance["finger"]
self.versionName = instance["version"]
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

View File

@ -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

View File

@ -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

13
main.py
View File

@ -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)

View File

@ -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

View File

@ -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()

View File

@ -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))

View File

@ -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