monolith/modules/network.py

58 lines
1.8 KiB
Python

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 *
class Network:
def __init__(self, name, host, port, security, auth):
self.name = name
self.host = host
self.port = port
self.security = security
self.auth = auth
self.last = 0
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,
"enabled": False,
"net": self.name,
"id": self.last
}
self.aliases[self.last] = alias.generate_alias()
return self.last, self.aliases[self.last]["nick"]
def delete_relay(self, id):
del self.relays[id]
del self.aliases[id]
def start_bot(self, relay):
# 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)
main.ReactorPool[name] = rct
main.FactoryPool[name] = bot
log("Started bot on relay %s on %s", (relay, self.host))
def start_bots(self):
for relay in self.relays:
if relay["enabled"]:
start_bot(relay)