Move keywords into a seperate file
This commit is contained in:
parent
9506d3d1f4
commit
77928dc52c
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"Keywords": [
|
||||||
|
"kek"
|
||||||
|
],
|
||||||
|
"KeywordsExcept": {}
|
||||||
|
}
|
92
threshold
92
threshold
|
@ -231,6 +231,15 @@ class Base(Protocol):
|
||||||
def dataReceived(self, data):
|
def dataReceived(self, data):
|
||||||
data = data.decode("utf-8", "replace")
|
data = data.decode("utf-8", "replace")
|
||||||
#log("Data received from %s:%s -- %s" % (self.addr.host, self.addr.port, repr(data)))
|
#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:
|
||||||
|
#timePlus = 0.0
|
||||||
|
for i in splitData:
|
||||||
|
helper.parseCommand(self.addr, self.authed, i)
|
||||||
|
#reactor.callLater(timePlus, lambda: helper.parseCommand(self.addr, self.authed, i))
|
||||||
|
#timePlus += 0.01
|
||||||
|
return
|
||||||
helper.parseCommand(self.addr, self.authed, data)
|
helper.parseCommand(self.addr, self.authed, data)
|
||||||
|
|
||||||
def connectionMade(self):
|
def connectionMade(self):
|
||||||
|
@ -279,12 +288,24 @@ class Helper(object):
|
||||||
else:
|
else:
|
||||||
error("Mandatory values missing from config")
|
error("Mandatory values missing from config")
|
||||||
|
|
||||||
|
|
||||||
def saveConfig(self):
|
def saveConfig(self):
|
||||||
global config
|
global config
|
||||||
with open("config.json", "w") as f:
|
with open("config.json", "w") as f:
|
||||||
dump(config, f, indent=4)
|
dump(config, f, indent=4)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def getKeywordConfig(self):
|
||||||
|
with open("keyword.json", "r") as f:
|
||||||
|
keyconf = load(f)
|
||||||
|
return keyconf
|
||||||
|
|
||||||
|
def saveKeywordConfig(self):
|
||||||
|
global keyconf
|
||||||
|
with open("keyword.json", "w") as f:
|
||||||
|
dump(keyconf, f, indent=4)
|
||||||
|
return
|
||||||
|
|
||||||
def getPool(self):
|
def getPool(self):
|
||||||
with open("pool.json", "r") as f:
|
with open("pool.json", "r") as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
|
@ -329,14 +350,14 @@ class Helper(object):
|
||||||
toUndo = False
|
toUndo = False
|
||||||
uniqueNum = 0
|
uniqueNum = 0
|
||||||
totalNum = 0
|
totalNum = 0
|
||||||
for i in config["Keywords"]:
|
for i in keyconf["Keywords"]:
|
||||||
if i in message:
|
if i in message:
|
||||||
if i in config["KeywordsExcept"].keys():
|
if i in keyconf["KeywordsExcept"].keys():
|
||||||
for x in config["KeywordsExcept"][i]:
|
for x in keyconf["KeywordsExcept"][i]:
|
||||||
if x in message:
|
if x in message:
|
||||||
toUndo = True
|
toUndo = True
|
||||||
messageDuplicate = messageDuplicate.replace(x, "\0\r\n\n\0")
|
messageDuplicate = messageDuplicate.replace(x, "\0\r\n\n\0")
|
||||||
for y in config["Keywords"]:
|
for y in keyconf["Keywords"]:
|
||||||
if i in messageDuplicate:
|
if i in messageDuplicate:
|
||||||
totalNum += messageDuplicate.count(i)
|
totalNum += messageDuplicate.count(i)
|
||||||
message = messageDuplicate.replace(i, "{"+i+"}")
|
message = messageDuplicate.replace(i, "{"+i+"}")
|
||||||
|
@ -388,28 +409,31 @@ class Helper(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
def addKeyword(self, keyword):
|
def addKeyword(self, keyword):
|
||||||
if keyword in config["Keywords"]:
|
if keyword in keyconf["Keywords"]:
|
||||||
return "EXISTS"
|
return "EXISTS"
|
||||||
else:
|
else:
|
||||||
for i in config["Keywords"]:
|
for i in keyconf["Keywords"]:
|
||||||
if i in keyword or keyword in i:
|
if i in keyword or keyword in i:
|
||||||
return "ISIN"
|
return "ISIN"
|
||||||
config["Keywords"].append(keyword)
|
keyconf["Keywords"].append(keyword)
|
||||||
helper.saveConfig()
|
helper.saveKeywordConfig()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def delKeyword(self, keyword):
|
def delKeyword(self, keyword):
|
||||||
if not keyword in config["Keywords"]:
|
if not keyword in keyconf["Keywords"]:
|
||||||
return "NOKEY"
|
return "NOKEY"
|
||||||
config["Keywords"].remove(keyword)
|
keyconf["Keywords"].remove(keyword)
|
||||||
helper.saveConfig()
|
helper.saveKeywordConfig()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def parseCommand(self, addr, authed, data):
|
def parseCommand(self, addr, authed, data):
|
||||||
global pool
|
global pool
|
||||||
data = data.strip("\n")
|
|
||||||
spl = data.split()
|
spl = data.split()
|
||||||
|
if addr in connections.keys():
|
||||||
obj = connections[addr]
|
obj = connections[addr]
|
||||||
|
else:
|
||||||
|
warning("Got connection object with no instance in the address pool")
|
||||||
|
return
|
||||||
|
|
||||||
success = lambda data: sendSuccess(addr, data)
|
success = lambda data: sendSuccess(addr, data)
|
||||||
failure = lambda data: sendFailure(addr, data)
|
failure = lambda data: sendFailure(addr, data)
|
||||||
|
@ -434,8 +458,15 @@ class Helper(object):
|
||||||
elif cmd == "rehash":
|
elif cmd == "rehash":
|
||||||
global config
|
global config
|
||||||
config = helper.getConfig()
|
config = helper.getConfig()
|
||||||
|
log("Configuration rehashed")
|
||||||
success("Configuration rehashed successfully")
|
success("Configuration rehashed successfully")
|
||||||
|
|
||||||
|
elif cmd == "rekey":
|
||||||
|
global keyconf
|
||||||
|
keyconf = helper.getKeywordConfig()
|
||||||
|
log("Keyword configuration rehashed")
|
||||||
|
success("Keyword configuration rehashed successfully")
|
||||||
|
|
||||||
elif cmd == "pass":
|
elif cmd == "pass":
|
||||||
info("You are already authenticated")
|
info("You are already authenticated")
|
||||||
return
|
return
|
||||||
|
@ -576,35 +607,35 @@ class Helper(object):
|
||||||
return
|
return
|
||||||
if length == 4:
|
if length == 4:
|
||||||
if spl[1] == "except":
|
if spl[1] == "except":
|
||||||
if not spl[2] in config["Keywords"]:
|
if not spl[2] in keyconf["Keywords"]:
|
||||||
failure("No such keyword: %s" % spl[2])
|
failure("No such keyword: %s" % spl[2])
|
||||||
return
|
return
|
||||||
if spl[2] in config["KeywordsExcept"].keys():
|
if spl[2] in keyconf["KeywordsExcept"].keys():
|
||||||
if spl[3] in config["KeywordsExcept"][spl[2]]:
|
if spl[3] in keyconf["KeywordsExcept"][spl[2]]:
|
||||||
failure("Exception exists: %s" % spl[3])
|
failure("Exception exists: %s" % spl[3])
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
if not spl[2] in spl[3]:
|
if not spl[2] in spl[3]:
|
||||||
failure("Keyword %s not in exception %s. This won't work" % (spl[2], spl[3]))
|
failure("Keyword %s not in exception %s. This won't work" % (spl[2], spl[3]))
|
||||||
return
|
return
|
||||||
config["KeywordsExcept"][spl[2]] = []
|
keyconf["KeywordsExcept"][spl[2]] = []
|
||||||
|
|
||||||
config["KeywordsExcept"][spl[2]].append(spl[3])
|
keyconf["KeywordsExcept"][spl[2]].append(spl[3])
|
||||||
helper.saveConfig()
|
helper.saveKeywordConfig()
|
||||||
success("Successfully added exception %s for keyword %s" % (spl[3], spl[2]))
|
success("Successfully added exception %s for keyword %s" % (spl[3], spl[2]))
|
||||||
return
|
return
|
||||||
|
|
||||||
elif spl[1] == "unexcept":
|
elif spl[1] == "unexcept":
|
||||||
if not spl[2] in config["KeywordsExcept"].keys():
|
if not spl[2] in keyconf["KeywordsExcept"].keys():
|
||||||
failure("No such exception: %s" % spl[2])
|
failure("No such exception: %s" % spl[2])
|
||||||
return
|
return
|
||||||
if not spl[3] in config["KeywordsExcept"][spl[2]]:
|
if not spl[3] in keyconf["KeywordsExcept"][spl[2]]:
|
||||||
failure("Exception %s has no attribute %s" % (spl[2], spl[3]))
|
failure("Exception %s has no attribute %s" % (spl[2], spl[3]))
|
||||||
return
|
return
|
||||||
config["KeywordsExcept"][spl[2]].remove(spl[3])
|
keyconf["KeywordsExcept"][spl[2]].remove(spl[3])
|
||||||
if config["KeywordsExcept"][spl[2]] == []:
|
if keyconf["KeywordsExcept"][spl[2]] == []:
|
||||||
del config["KeywordsExcept"][spl[2]]
|
del keyconf["KeywordsExcept"][spl[2]]
|
||||||
helper.saveConfig()
|
helper.saveKeywordConfig()
|
||||||
success("Successfully removed exception %s for keyword %s" % (spl[3], spl[2]))
|
success("Successfully removed exception %s for keyword %s" % (spl[3], spl[2]))
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
@ -612,23 +643,23 @@ class Helper(object):
|
||||||
return
|
return
|
||||||
if length == 3:
|
if length == 3:
|
||||||
if spl[1] == "unexcept":
|
if spl[1] == "unexcept":
|
||||||
if not spl[2] in config["KeywordsExcept"].keys():
|
if not spl[2] in keyconf["KeywordsExcept"].keys():
|
||||||
failure("No such exception: %s" % spl[2])
|
failure("No such exception: %s" % spl[2])
|
||||||
return
|
return
|
||||||
del config["KeywordsExcept"][spl[2]]
|
del keyconf["KeywordsExcept"][spl[2]]
|
||||||
helper.saveConfig()
|
helper.saveKeywordConfig()
|
||||||
success("Successfully removed exception list of %s" % spl[2])
|
success("Successfully removed exception list of %s" % spl[2])
|
||||||
return
|
return
|
||||||
if length == 2:
|
if length == 2:
|
||||||
if spl[1] == "show":
|
if spl[1] == "show":
|
||||||
info(",".join(config["Keywords"]))
|
info(",".join(keyconf["Keywords"]))
|
||||||
return
|
return
|
||||||
|
|
||||||
elif spl[1] == "showexcept":
|
elif spl[1] == "showexcept":
|
||||||
exceptMap = []
|
exceptMap = []
|
||||||
for i in config["KeywordsExcept"].keys():
|
for i in keyconf["KeywordsExcept"].keys():
|
||||||
exceptMap.append("Key: %s" % i)
|
exceptMap.append("Key: %s" % i)
|
||||||
exceptMap.append("%s: %s" % (i, ",".join(config["KeywordsExcept"][i])))
|
exceptMap.append("%s: %s" % (i, ",".join(keyconf["KeywordsExcept"][i])))
|
||||||
exceptMap.append("\n")
|
exceptMap.append("\n")
|
||||||
info("\n".join(exceptMap))
|
info("\n".join(exceptMap))
|
||||||
return
|
return
|
||||||
|
@ -815,6 +846,7 @@ class Helper(object):
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
helper = Helper()
|
helper = Helper()
|
||||||
config = helper.getConfig()
|
config = helper.getConfig()
|
||||||
|
keyconf = helper.getKeywordConfig()
|
||||||
pool = helper.getPool()
|
pool = helper.getPool()
|
||||||
help = helper.getHelp()
|
help = helper.getHelp()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue