From 93007ca23b3c8e4d1c0cee5f50c0e904f6d5960e Mon Sep 17 00:00:00 2001 From: Fulgen301 Date: Sun, 11 Dec 2016 13:06:29 +0100 Subject: Using base64-encoding for the regexps of the config file. --- pycrctrl.pyp | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/pycrctrl.pyp b/pycrctrl.pyp index 8bd7357..6219e06 100644 --- a/pycrctrl.pyp +++ b/pycrctrl.pyp @@ -33,6 +33,7 @@ import os import re import signal import json +import base64 import supybot.ircmsgs as ircmsgs from pickle import Pickler, Unpickler @@ -137,7 +138,7 @@ class PyCRCtrl(object): self.path = path self.loadConfigFile() self.loadScenarioList() - self.codec = codec = QTextCodec.codecForName(self.getConfigEntry("encoding")) + self.codec = codec = QTextCodec.codecForName(self.config.get("encoding")) self.queue = queue.Queue(5) def __ostream__(self, ostream): @@ -172,14 +173,8 @@ class PyCRCtrl(object): self.config = json.load(open(os.path.join(self.path, "pycrctrl.conf"),"r")) return True - def getConfigEntry(self, entry=""): - if type(self.config) != dict: - return - - try: - return self.config[entry] - except KeyError: - return + def decodeRegExp(self, regexp): + return self.codec.toUnicode(base64.b64decode(regexp)) def setConfigEntry(self, entry="", value=None): if type(self.config) != dict: @@ -188,7 +183,7 @@ class PyCRCtrl(object): try: self.config[entry] = value finally: - json.dump(self.config, open(os.path.join(self.path, "pycrctrl.conf"), "w")) + json.dump(self.config, open(os.path.join(self.path, "pycrctrl.conf"), "w"),indent=4) return True @@ -229,7 +224,7 @@ class PyCRCtrl(object): else: self.scenario = self.scenlist[randint(0,len(self.scenlist))-1] self.clonk = subprocess.Popen( - './{} {} "{}"'.format(self.getConfigEntry("engine"), self.getConfigEntry("commandline") + (" --" if type(self).__name__ == "PyOCCtrl" else " /") + ("league" if self.scenario in self.league_scenlist() else "noleague"),self.scenario), + './{} {} "{}"'.format(self.config.get("engine"), self.config.get("commandline") + (" --" if type(self).__name__ == "PyOCCtrl" else " /") + ("league" if self.scenario in self.league_scenlist() else "noleague"),self.scenario), 0, None, subprocess.PIPE, @@ -240,7 +235,7 @@ class PyCRCtrl(object): ) self.state = "Lobby" self.readServerOutput() - if self.getConfigEntry("autohost") == False: + if self.config.get("autohost") == False: self.thread_started = False self.setTopic("Kein laufendes Spiel.") break @@ -256,7 +251,7 @@ class PyCRCtrl(object): try: output = self.clonk.stdout.readline() - if bool(re.match(self.getConfigEntry("RegExps")["shutdownExp"], self.codec.toUnicode(output))): + if bool(re.match(self.decodeRegExp(self.config.get("RegExps")["shutdownExp"]), self.codec.toUnicode(output))): self.clonk.stdin.close() elif output == b"" and self.clonk.poll() is not None: if self.clonk: @@ -271,8 +266,8 @@ class PyCRCtrl(object): if output[0] == ">": output = output[1:] - if output.find(self.getConfigEntry("prefix")) != -1 and output.find("<{}>".format(self.irc.nick)) == -1: - part = output[output.find(self.getConfigEntry("prefix"))+len(self.getConfigEntry("prefix")):].split(" ",1) + if output.find(self.config.get("prefix")) != -1 and output.find("<{}>".format(self.irc.nick)) == -1: + part = output[output.find(self.config.get("prefix"))+len(self.config.get("prefix")):].split(" ",1) found = False x = None if len(part) > 0: @@ -291,10 +286,10 @@ class PyCRCtrl(object): if type(x) == tuple and x[1] != "": self.writeToServer(x[1]) del x - if bool(re.match(self.getConfigEntry("RegExps")["lobbyStartExp"], output)): + if bool(re.match(self.decodeRegExp(self.config.get("RegExps")["lobbyStartExp"]), output)): self.state = "Lädt" - elif bool(re.match(self.getConfigEntry("RegExps")["startExp"], output)): + elif bool(re.match(self.decodeRegExp(self.config.get("RegExps")["startExp"]), output)): self.state = "Läuft" try: cout << output << endl @@ -304,12 +299,12 @@ class PyCRCtrl(object): if self.irc: if output.find("<" + self.irc.nick + ">") == -1: if bool(re.match(r"^<.*>", output) and output.find("[IRC]") == -1) and \ - bool(output.find(self.getConfigEntry("prefix")) == -1): - self.irc.reply("[Clonk]{}".format(output), to=self.getConfigEntry("channels")["ingame"]) + bool(output.find(self.config.get("prefix")) == -1): + self.irc.reply("[Clonk]{}".format(output), to=self.config.get("channels")["ingame"]) - elif bool(re.match(self.getConfigEntry("RegExps")["joinExp"], output)) or \ - bool(re.match(self.getConfigEntry("RegExps")["leaveExp"], output)): - self.irc.reply(output, to=self.getConfigEntry("channels")["ingame"]) + elif bool(re.match(self.decodeRegExp(self.config.get("RegExps")["joinExp"]), output)) or \ + bool(re.match(self.decodeRegExp(self.config.get("RegExps")["leaveExp"]), output)): + self.irc.reply(output, to=self.config.get("channels")["ingame"]) except KeyboardInterrupt: @@ -338,7 +333,7 @@ class PyCRCtrl(object): if not self.irc: return for channel in msg.args[0].split(","): - if channel == self.getConfigEntry("channels")["ingame"] and msg.nick != self.irc.nick: + if channel == self.config.get("channels")["ingame"] and msg.nick != self.irc.nick: self.writeToServer("[IRC]<{}> {}".format(msg.nick, msg.args[1])) @@ -371,7 +366,7 @@ class PyCRCtrl(object): else: self.topic = text - return bool(self.irc.sendMsg(ircmsgs.topic(self.getConfigEntry("channels")["ingame"], text))) + return bool(self.irc.sendMsg(ircmsgs.topic(self.config.get("channels")["ingame"], text))) def start(self, time=None): self.stopped = False @@ -445,8 +440,7 @@ class PyCRCtrl(object): def addScenario(self, link): name = "" - fname = urllib.parse.urlsplit(link).split("/") - for item in fname: + for item in link.split("/"): if re.match(r"(.*)\.[oc][c4]s",item): name = item break @@ -455,8 +449,11 @@ class PyCRCtrl(object): with open(os.path.join(self.path, name),"wb") as fobj: fobj.write(site) - self.scenlist().append(name) - Pickler(open(os.path.join(self.path, "scenlist"))).dump(self.scenlist()) + try: + self.scenlist().index(name) + except Exception: + self.scenlist().append(name) + Pickler(open(os.path.join(self.path, "scenlist"), "wb")).dump(self.scenlist()) return self -- cgit v1.2.3-54-g00ecf