From 25eb076c50976b0afa8243b7d524fd1fb32ad310 Mon Sep 17 00:00:00 2001 From: khuxkm Date: Fri, 20 Jul 2018 15:48:06 -0400 Subject: [PATCH] Add verbose option and logging system, as well as making preferences prettier --- bot.py | 27 +++++++++++++++++++++++---- prefer.py | 2 +- prefs-skel.json | 11 ++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index abe0e1e..873211e 100644 --- a/bot.py +++ b/bot.py @@ -1,24 +1,43 @@ -import teambot,network,prefer,time +import teambot,network,prefer,time,sys +from colorama import init, Fore, Style +ENDF = Style.RESET_ALL ROOT = "/home/khuxkm/code/auditbot/" opts = prefer.Preferences(ROOT+"prefs.json") + +VERBOSE = opts.get("verbose") +COLORS = {"*":"cyan","!":"yellow","X":"red"} +def say_log(s,p="*",c=None): + if c is None: + c = COLORS.get(p,None) + c = getattr(Fore,c.upper()) + print(c+"[{}] {}".format(p,s)+ENDF) + bbj = network.BBJ(port=opts.get("port",7099)) +say_log("Logging in with username {} and password {}...".format(opts.get("username"),opts.get("password"))) if not bbj.set_credentials(opts.get("username"),opts.get("password")): - raise Exception("Incorrect details provided") + say_log("Incorrect details provided","X") + sys.exit(1) if not opts.get("thread_id"): + say_log("Creating thread...") thread = bbj.thread_create(opts.get("title"),opts.get("text")) opts.set("thread_id",thread["thread_id"]) +say_log("...and BBJ is all set up!") trigger = lambda x: "{}: ".format(x) log = lambda x,e: e[len(trigger(x)):] class AuditBot(teambot.Handler,teambot.CommandHandlerMixin): def handle_command(self,target,nick,text): - if nick.split("|")[0] in opts.get("sudoers") and text.startswith(trigger(self.bot.bot_nick)): - bbj.thread_reply(opts.get("thread_id"),opts.get("format").format(time.strftime("%Y-%m-%d %H:%M:%S"),nick,log(self.bot.bot_nick,text))) + nick = nick.split("|")[0] + if nick in opts.get("sudoers") and text.startswith(trigger(self.bot.bot_nick)): + say_log("{} is auditing \"{}\"".format(nick,log(self.bot.bot_nick,text))) + r = bbj.thread_reply(opts.get("thread_id"),opts.get("format").format(time.strftime("%Y-%m-%d %H:%M:%S"),nick,log(self.bot.bot_nick,text))) + say_log(repr(r)) if __name__=="__main__": channels = "#sudoers".split() bot = teambot.TeamBot(channels,"auditor","localhost",chandler=AuditBot) + say_log("Starting IRC bot...") bot.start() diff --git a/prefer.py b/prefer.py index 36d3b4b..dbad146 100644 --- a/prefer.py +++ b/prefer.py @@ -2,7 +2,7 @@ import json def toJSONFile(obj,filename): with open(filename,"w") as f: - json.dump(obj,f) + json.dump(obj,f,indent="\t") def fromJSONFile(filename): with open(filename) as f: diff --git a/prefs-skel.json b/prefs-skel.json index aac4fcb..83db92f 100644 --- a/prefs-skel.json +++ b/prefs-skel.json @@ -1 +1,10 @@ -{"username": "", "sudoers": [], "password": "","trigger":"{}: ","title":"audit log","text":"Accountability and whatever are our main principles. We'll try to post here if we do something important.","format":"log date {}:\n{} - \"{}\""} +{ + "username": "", + "sudoers": [], + "password": "", + "trigger": "{}: ", + "title": "audit log", + "text": "Accountability and whatever are our main principles. We'll try to post here if we do something important.", + "format": "log date {}:\n{} - \"{}\"", + "verbose": false +} \ No newline at end of file