From ce11af291b034c228de4ab3df816f2560c67ce6e Mon Sep 17 00:00:00 2001 From: jcao219 Date: Sat, 9 Jul 2011 13:25:54 -0500 Subject: [PATCH] improved many things added stuff like !restart, !fping etc for admins only --- decorators.py | 15 +++++-- oyoyo/client.py | 14 +++++- wolfgame.py | 112 +++++++++++++++++++++++++++++++++++++----------- 3 files changed, 111 insertions(+), 30 deletions(-) diff --git a/decorators.py b/decorators.py index 0fb000b..2f82bb8 100644 --- a/decorators.py +++ b/decorators.py @@ -3,22 +3,29 @@ import botconfig def generate(fdict, **kwargs): """Generates a decorator generator. Always use this""" - def cmd(*s, raw_nick=False, admin_only=False): + def cmd(*s, raw_nick=False, admin_only=False, owner_only=False): def dec(f): def innerf(*args): largs = list(args) if not raw_nick and largs[1]: + cloak = parse_nick(largs[1])[3] largs[1] = parse_nick(largs[1])[0] + if owner_only: + if cloak and cloak == botconfig.OWNER: + return f(*largs) + elif cloak: + largs[0].notice(largs[1], "You are not the owner.") + return if admin_only: - if largs[1] and largs[1] in botconfig.ADMINS: + if cloak and cloak in botconfig.ADMINS: return f(*largs) - else: + elif cloak: largs[0].notice(largs[1], "You are not an admin.") return return f(*largs) for x in s: fdict[x] = innerf - return f + return innerf return dec diff --git a/oyoyo/client.py b/oyoyo/client.py index ebc5968..a6863b4 100644 --- a/oyoyo/client.py +++ b/oyoyo/client.py @@ -36,7 +36,8 @@ def add_commands(d): @add_commands(("join", "mode", "nick", - "part")) + "part", + "kick")) class IRCClient(object): """ IRC Client class. This handles one connection to a server. This can be used either with or without IRCApp ( see connect() docs ) @@ -120,7 +121,16 @@ class IRCClient(object): """ try: logging.info('connecting to {0}:{1}'.format(self.host, self.port)) - self.socket.connect(("{0}".format(self.host), self.port)) + retries = 0 + while True: + try: + self.socket.connect(("{0}".format(self.host), self.port)) + break + except socket.error as e: + retries += 1 + logging.warning('Error: {0}'.format(e)) + if retries > 3: + break if not self.blocking: self.socket.setblocking(0) diff --git a/wolfgame.py b/wolfgame.py index a6a5735..e1d812e 100644 --- a/wolfgame.py +++ b/wolfgame.py @@ -9,6 +9,8 @@ import copy from time import sleep import re import logging +import sys +import os COMMANDS = {} PM_COMMANDS = {} @@ -43,7 +45,7 @@ def connect_callback(cli): var.GRAVEYARD_LOCK = threading.Lock() -@cmd("!say") +@pmcmd("!say") def say(cli, nick, rest): # To be removed later cli.msg(botconfig.CHANNEL, "{0} says: {1}".format(nick, rest)) @@ -111,22 +113,67 @@ def forced_exit(cli, nick, *rest): # Admin Only dict.clear(COMMANDS) dict.clear(PM_COMMANDS) dict.clear(PM_COMMANDS) - sleep(5) cli.quit("Forced quit from admin") raise SystemExit -@cmd("!exec", admin_only=True) +@cmd("!exec", owner_only = True) def py(cli, nick, chan, rest): try: exec(rest) except Exception as e: cli.msg(chan, str(type(e))+":"+str(e)) - - - - + + + +@cmd("!eval", owner_only = True) +def pyeval(cli, nick, chan, rest): + try: + a = str(eval(rest)) + if len(a) < 500: + cli.msg(chan, a) + else: + cli.msg(chan, a[0:500]) + except Exception as e: + cli.msg(chan, str(type(e))+":"+str(e)) + + +@cmd("!restart", admin_only=True) +def restart_program(cli, nick, chan, rest): + try: + forced_exit(cli, nick, chan, rest) + finally: + print("RESTARTING!") + python = sys.executable + os.execl(python, python, *sys.argv) + + +@cmd("!op", admin_only=True) +def give_op(cli, nick, chan, rest): + if not rest.strip(): + rest = nick + cli.msg("ChanServ", " ".join(("op",chan,rest.strip()))) + + + +@cmd("!deop", admin_only=True) +def take_op(cli, nick, chan, rest): + if not rest.strip(): + rest = nick + cli.msg("ChanServ", " ".join(("deop",chan,rest.strip()))) + + +@cmd("!sudo revoke", owner_only=True) +def revoke(cli, nick, chan, rest): + r = rest.strip() + if r in botconfig.ADMINS: + ladmins = list(botconfig.ADMINS) + ladmins.remove(r) + botconfig.ADMINS = tuple(ladmins) + cli.msg(chan, "Revoked admin rights for \u0002{0}\u0002.".format(r)) + + @cmd("!ping") def pinger(cli, nick, chan, rest): @@ -172,7 +219,7 @@ def pinger(cli, nick, chan, rest): -@cmd("!sudo ping", admin_only=True) +@cmd("!fping", admin_only=True) def fpinger(cli, nick, chan, rest): var.LAST_PING = None pinger(cli, nick, chan, rest) @@ -200,27 +247,41 @@ def join(cli, nick, chan, rest): cli.msg(chan, '\u0002{0}\u0002 has joined the game.'.format(nick)) -@cmd("!fjoin") +@cmd("!fjoin", admin_only=True) def fjoin(cli, nick, chan, rest): - if nick in ("nyuszika7h", "jcao219"): - for a in rest.split(" "): - a = a.strip() - if a != botconfig.NICK: - join(cli, a.strip(), chan, "") + for a in rest.split(" "): + a = a.strip() + if a != botconfig.NICK: + join(cli, a.strip(), chan, "") -@cmd("!fleave") +@cmd("!fleave", admin_only=True) def fleave(cli, nick, chan, rest): - if nick in ("nyuszika7h", "jcao219"): - for a in rest.split(" "): - a = a.strip() - if a.lower() != botconfig.NICK.lower(): - del_player(cli, a.strip()) - -@cmd("!fstart") + for a in rest.split(" "): + a = a.strip() + if a.lower() != botconfig.NICK.lower(): + del_player(cli, a.strip()) + cli.msg(chan, ("\u0002{0}\u0002 has used fleave"+ + " on \u0002{1}\u0002.").format(nick, a.strip())) + +@cmd("!fstart", admin_only=True) def fstart(cli, nick, chan, rest): - if nick not in ("nyuszika7h", "jcao219"): return var.CAN_START_TIME = datetime.now() start(cli, nick, chan, rest) + +@cmd("!chankick", admin_only=True) +def chankick(cli, nick, chan, rest): + rest = rest.split(" ", 1) + if rest[0] != botconfig.NICK: + cli.kick(chan, *rest) + else: + cli.kick(chan, nick, "No.") + +@hook("kick") +def on_kicked(cli, nick, chan, victim, reason): + if victim == botconfig.NICK: + cli.join(botconfig.CHANNEL) + cli.kick(chan, nick, "No.") + @cmd("!stats") def stats(cli, nick, chan, rest): @@ -725,6 +786,8 @@ def chk_nightdone(cli): (not var.ROLES["wolf"] + var.ROLES["werecrow"])) and var.PHASE == "night"): # no wolves if var.TIMERS[0]: + if var.TIMERS[0].is_alive(): + return var.TIMERS[0].cancel() # cancel timer var.TIMERS[0] = None if var.PHASE == "night": # Double check @@ -1283,6 +1346,7 @@ def start(cli, nick, chan, rest): # DEATH TO IDLERS! reapertimer = threading.Thread(None, reaper, args=(cli,)) + reapertimer.daemon = True reapertimer.start() @@ -1334,7 +1398,7 @@ def nay(cli, nick, chan, rest): cli.notice(nick, "You are already in the opposition.") return var.SETTINGS_CHANGE_OPPOSITION.append(nick) - needed = len(pl)//2 + 1 + needed = max(len(pl)//2 + 1, 2) if len(var.SETTINGS_CHANGE_OPPOSITION) >= needed: cli.msg(chan, "The settings change request has been downvoted "+ "to oblivion. The default settings are restored.")