monolith/legacy/core/server.py

56 lines
1.7 KiB
Python
Raw Normal View History

2022-07-21 12:40:09 +00:00
from twisted.internet.protocol import Factory, Protocol
2022-07-21 12:40:09 +00:00
import main
from core.parser import parseCommand
2022-07-21 12:40:09 +00:00
from utils.logging.log import log, warn
2022-07-21 12:39:41 +00:00
class Server(Protocol):
def __init__(self, addr):
self.addr = addr
self.authed = False
2022-07-21 12:40:05 +00:00
if main.config["UsePassword"] is False:
self.authed = True
def send(self, data):
data += "\r\n"
data = data.encode("utf-8", "replace")
self.transport.write(data)
def dataReceived(self, data):
data = data.decode("utf-8", "replace")
2022-07-21 12:39:41 +00:00
# log("Data received from %s:%s -- %s" % (self.addr.host, self.addr.port, repr(data)))
if "\n" in data:
splitData = [x for x in data.split("\n") if x]
if "\n" in data:
for i in splitData:
parseCommand(self.addr, self.authed, i)
return
parseCommand(self.addr, self.authed, data)
def connectionMade(self):
log("Connection from %s:%s" % (self.addr.host, self.addr.port))
self.send("Greetings.")
def connectionLost(self, reason):
self.authed = False
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
if self.addr in main.connections.keys():
del main.connections[self.addr]
else:
warn("Tried to remove a non-existant connection.")
2022-07-21 12:39:41 +00:00
class ServerFactory(Factory):
def buildProtocol(self, addr):
entry = Server(addr)
main.connections[addr] = entry
return entry
def send(self, addr, data):
if addr in main.connections.keys():
connection = main.connections[addr]
connection.send(data)
else:
return