|
|
|
@ -2,7 +2,7 @@
|
|
|
|
|
from twisted.internet import reactor
|
|
|
|
|
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
|
|
|
|
from twisted.internet.protocol import Protocol, Factory
|
|
|
|
|
from json import load, dump
|
|
|
|
|
from json import load, dump, loads
|
|
|
|
|
from sys import exit
|
|
|
|
|
|
|
|
|
|
listener = None
|
|
|
|
@ -54,6 +54,7 @@ class Base(Protocol):
|
|
|
|
|
|
|
|
|
|
def connectionLost(self, reason):
|
|
|
|
|
global connections
|
|
|
|
|
self.authed = False
|
|
|
|
|
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
|
|
|
|
if not listener == None:
|
|
|
|
|
if self.addr in connections.keys():
|
|
|
|
@ -93,7 +94,36 @@ class Helper(object):
|
|
|
|
|
else:
|
|
|
|
|
error("Mandatory values missing from config")
|
|
|
|
|
|
|
|
|
|
def getPool(self):
|
|
|
|
|
with open("pool.json", "r") as f:
|
|
|
|
|
data = f.read()
|
|
|
|
|
if not data == "":
|
|
|
|
|
pool = loads(data.strip())
|
|
|
|
|
return pool
|
|
|
|
|
else:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
def savePool(self):
|
|
|
|
|
global pool
|
|
|
|
|
with open("pool.json", "w") as f:
|
|
|
|
|
dump(pool, f, indent=4)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def getHelp(self):
|
|
|
|
|
with open("help.json", "r") as f:
|
|
|
|
|
help = load(f)
|
|
|
|
|
return help
|
|
|
|
|
|
|
|
|
|
def incorrectUsage(self, addr, mode):
|
|
|
|
|
if mode == None:
|
|
|
|
|
sendFailure(addr, "Incorrect usage")
|
|
|
|
|
return
|
|
|
|
|
if mode in help.keys():
|
|
|
|
|
sendFailure(addr, "Usage: " + help[mode])
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def parseCommand(self, addr, authed, data):
|
|
|
|
|
global pool
|
|
|
|
|
data = data.strip()
|
|
|
|
|
spl = data.split()
|
|
|
|
|
obj = connections[addr]
|
|
|
|
@ -102,45 +132,85 @@ class Helper(object):
|
|
|
|
|
failure = lambda data: sendFailure(addr, data)
|
|
|
|
|
info = lambda data: sendInfo(addr, data)
|
|
|
|
|
|
|
|
|
|
incUsage = lambda: sendFailure(addr, "Incorrect usage")
|
|
|
|
|
incUsage = lambda mode: self.incorrectUsage(addr, mode)
|
|
|
|
|
length = len(spl)
|
|
|
|
|
if len(spl) > 0:
|
|
|
|
|
cmd = spl[0]
|
|
|
|
|
else:
|
|
|
|
|
failure("No text was sent")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if authed == True:
|
|
|
|
|
if cmd == "pass":
|
|
|
|
|
if cmd == "help":
|
|
|
|
|
helpMap = []
|
|
|
|
|
for i in help.keys():
|
|
|
|
|
helpMap.append("%s: %s" % (i, help[i]))
|
|
|
|
|
info("\n".join(helpMap))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
elif cmd == "pass":
|
|
|
|
|
info("You are already authenticated")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
elif cmd == "logout":
|
|
|
|
|
obj.authed = False
|
|
|
|
|
success("Logged out")
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
incUsage()
|
|
|
|
|
|
|
|
|
|
elif cmd == "list":
|
|
|
|
|
poolMap = []
|
|
|
|
|
for i in pool.keys():
|
|
|
|
|
poolMap.append("%s: %s" % (i, pool[i]))
|
|
|
|
|
info("\n".join(poolMap))
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
if cmd == "pass":
|
|
|
|
|
if length == 2:
|
|
|
|
|
if spl[1] == config["password"]:
|
|
|
|
|
success("Authenticated successfully")
|
|
|
|
|
obj.authed = True
|
|
|
|
|
|
|
|
|
|
elif cmd == "connect":
|
|
|
|
|
if length == 6:
|
|
|
|
|
|
|
|
|
|
if spl[1] in pool.keys():
|
|
|
|
|
failure("Name already exists: %s" % spl[1])
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
failure("Password incorrect")
|
|
|
|
|
obj.transport.loseConnection()
|
|
|
|
|
|
|
|
|
|
protocol = spl[4].lower()
|
|
|
|
|
|
|
|
|
|
if not protocol in ["ssl", "plain"]:
|
|
|
|
|
incUsage("connect")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
pool[spl[1]] = { "address": spl[2],
|
|
|
|
|
"port": spl[3],
|
|
|
|
|
"protocol": protocol,
|
|
|
|
|
"nickname": spl[5],
|
|
|
|
|
}
|
|
|
|
|
success("Successfully created bot")
|
|
|
|
|
self.savePool()
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
incUsage("connect")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
incUsage(None)
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
if cmd == "pass" and length == 2:
|
|
|
|
|
if spl[1] == config["password"]:
|
|
|
|
|
success("Authenticated successfully")
|
|
|
|
|
obj.authed = True
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
incUsage()
|
|
|
|
|
failure("Password incorrect")
|
|
|
|
|
obj.transport.loseConnection()
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
incUsage()
|
|
|
|
|
incUsage(None)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
helper = Helper()
|
|
|
|
|
config = helper.getConfig()
|
|
|
|
|
pool = helper.getPool()
|
|
|
|
|
help = helper.getHelp()
|
|
|
|
|
|
|
|
|
|
listener = BaseFactory()
|
|
|
|
|
if config["usessl"] == True:
|
|
|
|
|