Moved files to subdirectory

This commit is contained in:
2022-09-06 12:50:09 +01:00
parent 2c3d83fe9a
commit bd9f9378cf
83 changed files with 0 additions and 0 deletions

17
legacy/utils/cleanup.py Normal file
View File

@@ -0,0 +1,17 @@
from twisted.internet import reactor
import main
from utils.logging.debug import debug
from utils.logging.log import log
def handler(sig, frame):
log("Received SIGINT, cleaning up")
cleanup()
def cleanup():
debug("Flushing Redis database")
main.r.flushdb()
reactor.stop()
# sys.exit(1)

27
legacy/utils/dedup.py Normal file
View File

@@ -0,0 +1,27 @@
from copy import deepcopy
from datetime import datetime
from json import dumps
from siphashc import siphash
import main
from utils.logging.debug import debug
def dedup(numName, b):
c = deepcopy(b)
if "ts" in c.keys():
del c["ts"]
c["approxtime"] = str(datetime.utcnow().timestamp())[: main.config["Tweaks"]["DedupPrecision"]]
castHash = siphash(main.hashKey, dumps(c, sort_keys=True))
del c["approxtime"]
isDuplicate = any(castHash in main.lastEvents[x] for x in main.lastEvents.keys() if not x == numName)
if isDuplicate:
debug("Duplicate: %s" % (c))
return True
if numName in main.lastEvents.keys():
main.lastEvents[numName].insert(0, castHash)
main.lastEvents[numName] = main.lastEvents[numName][0 : main.config["Tweaks"]["MaxHash"]] # noqa
else:
main.lastEvents[numName] = [castHash]
return False

View File

@@ -0,0 +1,163 @@
from datetime import datetime
from twisted.internet import reactor
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet.ssl import DefaultOpenSSLContextFactory
from twisted.words.protocols.irc import IRCClient
import main
from core.relay import sendRelayNotification
from modules import userinfo
from utils.get import getRelay
from utils.logging.log import error, log
from utils.logging.send import sendAll
from utils.parsing import parsen
# TODO: strip out non-relay functionality
class IRCRelay(IRCClient):
def __init__(self, num, relayCommands, user, stage2):
self.isconnected = False
self.buffer = ""
if user is None:
self.user = main.config["Relay"]["User"]
else:
self.user = user.lower()
password = main.config["Relay"]["Password"]
self.nickname = "relay"
self.realname = "relay"
self.username = self.user
self.password = self.user + ":" + password
self.relayCommands = relayCommands
self.num = num
self.stage2 = stage2
self.loop = None
def privmsg(self, user, channel, msg):
nick, ident, host = parsen(user)
for i in main.ZNCErrors:
if i in msg:
error("ZNC issue:", msg)
if nick[0] == main.config["Tweaks"]["ZNC"]["Prefix"]:
nick = nick[1:]
if nick in self.relayCommands.keys():
sendAll("[%s] %s -> %s" % (self.num, nick, msg))
def irc_ERR_PASSWDMISMATCH(self, prefix, params):
log("%s: relay password mismatch" % self.num)
sendAll("%s: relay password mismatch" % self.num)
def sendStage2(self):
# [["user", {"sasl": ["message1", "message2"]}], []]
if not len(self.stage2) == 0:
user = self.stage2[0].pop(0)
commands = self.stage2[0].pop(0)
del self.stage2[0]
deliverRelayCommands(self.num, commands, user, self.stage2)
def signedOn(self):
if not self.isconnected:
self.isconnected = True
# log("signed on as a relay: %s" % self.num)
sleeptime = 0
increment = 0.8
for i in self.relayCommands.keys():
for x in self.relayCommands[i]:
reactor.callLater(
sleeptime,
self.msg,
main.config["Tweaks"]["ZNC"]["Prefix"] + i,
x,
)
sleeptime += increment
increment += 0.8
if self.stage2 is not None:
reactor.callLater(sleeptime, self.sendStage2)
reactor.callLater(sleeptime + 5, self.transport.loseConnection)
return
class IRCRelayFactory(ReconnectingClientFactory):
def __init__(self, net, num=None, relayCommands=None, user=None, stage2=None):
if net is None:
self.num = num
self.net = None
self.name = "relay - %i" % num
self.relay = True
else:
self.name = net + str(num)
self.num = num
self.net = net
self.relay = False
self.client = None
self.maxDelay = main.config["Tweaks"]["Delays"]["MaxDelay"]
self.initialDelay = main.config["Tweaks"]["Delays"]["InitialDelay"]
self.factor = main.config["Tweaks"]["Delays"]["Factor"]
self.jitter = main.config["Tweaks"]["Delays"]["Jitter"]
self.relayCommands, self.user, self.stage2 = relayCommands, user, stage2
def buildProtocol(self, addr):
entry = IRCRelay(self.num, self.relayCommands, self.user, self.stage2)
self.client = entry
return entry
def clientConnectionLost(self, connector, reason):
if not self.relay:
userinfo.delChannels(self.net, self.client.channels)
if self.client is not None:
self.client.isconnected = False
self.client.authenticated = False
self.client.channels = []
error = reason.getErrorMessage()
if not self.relay:
log("%s - %i: connection lost: %s" % (self.net, self.num, error))
sendAll("%s - %i: connection lost: %s" % (self.net, self.num, error))
ctime = str(datetime.now().isoformat())
sendRelayNotification(
{
"type": "conn",
"net": self.net,
"num": self.num,
"status": "lost",
"message": error,
"ts": ctime,
}
)
self.retry(connector)
# ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def clientConnectionFailed(self, connector, reason):
if self.client is not None:
self.client.isconnected = False
self.client.authenticated = False
self.client.channels = []
error = reason.getErrorMessage()
log("%s - %i: connection failed: %s" % (self.net, self.num, error))
if not self.relay:
sendAll("%s - %s: connection failed: %s" % (self.net, self.num, error))
ctime = str(datetime.now().isoformat())
sendRelayNotification(
{
"type": "conn",
"net": self.net,
"num": self.num,
"status": "failed",
"message": error,
"ts": ctime,
}
)
self.retry(connector)
# ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def deliverRelayCommands(num, relayCommands, user=None, stage2=None):
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 = IRCRelayFactory(net=None, num=num, relayCommands=relayCommands, user=user, stage2=stage2)
host, port = getRelay(num)
reactor.connectSSL(host, port, bot, contextFactory)

13
legacy/utils/get.py Normal file
View File

@@ -0,0 +1,13 @@
import main
def getRelay(num):
host = main.config["Relay"]["Host"].replace("x", str(num))
port = int(str(main.config["Relay"]["Port"]).replace("x", str(num).zfill(2)))
# user = main.config["Relay"]["User"]
# password = main.config["Relay"]["Password"]
try:
port = int(port)
except ValueError:
return False
return (host, port)

View File

@@ -0,0 +1,25 @@
from os import listdir
from main import CommandMap
from utils.logging.debug import debug
from utils.logging.log import error
def loadCommands(allowDup=False):
for filename in listdir("commands"):
if filename.endswith(".py") and filename != "__init__.py":
commandName = filename[0:-3]
className = commandName.capitalize() + "Command"
# try:
module = __import__("commands.%s" % commandName)
if commandName not in CommandMap:
CommandMap[commandName] = getattr(getattr(module, commandName), className)
debug("Registered command: %s" % commandName)
else:
if allowDup:
CommandMap[commandName] = getattr(getattr(module, commandName), className)
debug("Registered command: %s" % commandName)
error("Duplicate command: %s" % (commandName))
# except Exception as err:
# error("Exception while loading command %s:\n%s" % (commandName, err))

View File

@@ -0,0 +1,25 @@
import sys
from importlib import reload
from os import listdir
from main import CommandMap
from utils.logging.debug import debug
def loadSingle(commandName):
if commandName + ".py" in listdir("commands"):
className = commandName.capitalize() + "Command"
try:
if commandName in CommandMap.keys():
reload(sys.modules["commands." + commandName])
CommandMap[commandName] = getattr(sys.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

View File

@@ -0,0 +1,13 @@
import main
# we need a seperate module to log.py, as log.py is imported by main.py, and we need to access main
# to read the setting
def debug(*data):
if main.config["Debug"]:
print("[DEBUG]", *data)
def trace(*data):
if main.config["Trace"]:
print("[TRACE]", *data)

View File

@@ -0,0 +1,10 @@
def log(*data):
print("[LOG]", *data)
def warn(*data):
print("[WARNING]", *data)
def error(*data):
print("[ERROR]", *data)

View File

@@ -0,0 +1,40 @@
import main
def sendData(addr, data):
main.connections[addr].send(data)
def sendWithPrefix(addr, data, prefix):
toSend = ""
for i in data.split("\n"):
toSend += prefix + " " + i + "\n"
sendData(addr, toSend)
def sendSuccess(addr, data):
sendWithPrefix(addr, data, "[y]")
def sendFailure(addr, data):
sendWithPrefix(addr, data, "[n]")
def sendInfo(addr, data):
sendWithPrefix(addr, data, "[i]")
def sendAll(data):
for i in main.connections:
if main.connections[i].authed:
main.connections[i].send(data)
return
def incorrectUsage(addr, mode):
if mode is None:
sendFailure(addr, "Incorrect usage")
return
if mode in main.help.keys():
sendFailure(addr, "Usage: " + main.help[mode])
return

15
legacy/utils/parsing.py Normal file
View File

@@ -0,0 +1,15 @@
def parsen(user):
step = user.split("!")
nick = step[0]
if len(step) == 2:
step2 = step[1].split("@")
if len(step2) == 2:
ident, host = step2
else:
ident = nick
host = nick
else:
ident = nick
host = nick
return (nick, ident, host)