Move keywords into a seperate file

This commit is contained in:
Mark Veidemanis 2017-12-03 13:10:51 +00:00
parent 9506d3d1f4
commit 77928dc52c
2 changed files with 69 additions and 31 deletions

6
keyword.json Normal file
View File

@ -0,0 +1,6 @@
{
"Keywords": [
"kek"
],
"KeywordsExcept": {}
}

View File

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