monolith/modules/network.py

59 lines
1.8 KiB
Python
Raw Normal View History

from twisted.internet.ssl import DefaultOpenSSLContextFactory
import json
import modules.alias as alias
from twisted.internet import reactor
from core.bot import IRCBot, IRCBotFactory
import main
from utils.logging.log import *
from utils.getrelay import getRelay
class Network:
def __init__(self, net, host, port, security, auth):
self.net = net
self.host = host
self.port = port
self.security = security
self.auth = auth
self.last = 0
self.relays = {}
self.aliases = {}
def add_relay(self, num=None):
if not num:
self.last += 1
num = self.last
self.relays[num] = {
"enabled": False,
"net": self.net,
"id": num
}
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, num):
# a single name is given to relays in the backend
# e.g. freenode1 for the first relay on freenode network
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(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" % (num, self.host))
def start_bots(self):
for num in self.relays.keys():
if self.relays[num]["enabled"]:
self.start_bot(num)