This repository has been archived on 2018-07-26. You can view files and clone it, but cannot push or open issues or pull requests.
auditbot/bot.py

50 lines
1.8 KiB
Python

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")):
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):
def on_pubmsg(self,target,nick,text):
nick = nick.split("|")[0]
say_log("[{}] {}: {}".format(target,nick,text))
if text.startswith(trigger(self._bot.bot_nick)):
if nick in opts.get("sudoers"):
say_log("{} is auditing \"{}\"".format(nick,log(self._bot.bot_nick,text)),"!","green")
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))
self.say(target,"{}: audited!".format(nick))
else:
say_log("{} is auditing \"{}\" but is not a sudoer!".format(nick,log(self._bot.bot_nick,text)),"X")
self.say(target,"{}: you aren't a sudoer!".format(nick))
if __name__=="__main__":
channels = "#sudoers".split()
bot = teambot.TeamBot(channels,"auditor","localhost",chandler=AuditBot)
say_log("Starting IRC bot...")
bot.start()