Add error checking in places, set up automatic relay provisioning and fix starting bots

This commit is contained in:
2019-08-25 21:29:11 +01:00
parent ff74968ff8
commit 2d70d5af11
19 changed files with 242 additions and 262 deletions

View File

@@ -1,14 +1,16 @@
from twisted.internet.ssl import DefaultOpenSSLContextFactory
import json
import modules.alias as alias
from twisted.internet import reactor
from core.bot import IRCBot, IRCBotFactory
from twisted.internet.ssl import DefaultOpenSSLContextFactory
import main
from utils.logging.log import *
from utils.getrelay import getRelay
class Network:
def __init__(self, name, host, port, security, auth):
self.name = name
def __init__(self, net, host, port, security, auth):
self.net = net
self.host = host
self.port = port
self.security = security
@@ -18,40 +20,39 @@ class Network:
self.relays = {}
self.aliases = {}
def add_relay(self, host, port, user, password):
self.last += 1
self.relays[self.last] = {
"host": host,
"port": port,
"user": user,
"password": password,
def add_relay(self, num=None):
if not num:
self.last += 1
num = self.last
self.relays[num] = {
"enabled": False,
"net": self.name,
"id": self.last
"net": self.net,
"id": num
}
self.aliases[self.last] = alias.generate_alias()
return self.last, self.aliases[self.last]["nick"]
self.aliases[num] = alias.generate_alias()
return num, self.aliases[num]["nick"]
def delete_relay(self, id):
del self.relays[id]
del self.aliases[id]
def start_bot(self, relay):
def start_bot(self, num):
# a single name is given to relays in the backend
# e.g. freenode1 for the first relay on freenode network
name = self.name + relay
keyFN = main.certPath+main.config["Key"]
certFN = main.certPath+main.config["Certificate"]
contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace"))
bot = IRCBotFactory(name)
rct = reactor.connectSSL(k, port, bot, contextFactory)
bot = IRCBotFactory(self.net, num)
#host, port = self.relays[num]["host"], self.relays[num]["port"]
host, port = getRelay(num)
rct = reactor.connectSSL(host, port, bot, contextFactory)
name = self.net + str(num)
main.ReactorPool[name] = rct
main.FactoryPool[name] = bot
log("Started bot on relay %s on %s", (relay, self.host))
log("Started bot on relay %s on %s" % (num, self.host))
def start_bots(self):
for relay in self.relays:
if relay["enabled"]:
start_bot(relay)
for num in self.relays.keys():
if self.relays[num]["enabled"]:
self.start_bot(num)