Change the user handling calls in wolfgame.py

This commit is contained in:
Vgr E. Barry 2016-10-30 11:49:02 -04:00 committed by Emanuel Barry
parent 795caa83fe
commit 12eab068be
2 changed files with 138 additions and 117 deletions

View File

@ -16,12 +16,19 @@ _users = WeakSet()
_arg_msg = "(nick={0}, ident={1}, host={2}, realname={3}, account={4}, allow_bot={5})" _arg_msg = "(nick={0}, ident={1}, host={2}, realname={3}, account={4}, allow_bot={5})"
class _user:
def __init__(self, nick):
self.nick = nick
for name in ("ident", "host", "account", "inchan", "modes", "moded"):
locals()[name] = property(lambda self, name=name: var.USERS[self.nick][name], lambda self, value, name=name: var.USERS[self.nick].__setitem__(name, value))
# This is used to tell if this is a fake nick or not. If this function # This is used to tell if this is a fake nick or not. If this function
# returns a true value, then it's a fake nick. This is useful for # returns a true value, then it's a fake nick. This is useful for
# testing, where we might want everyone to be fake nicks. # testing, where we might want everyone to be fake nicks.
predicate = re.compile(r"^[0-9]+$").search predicate = re.compile(r"^[0-9]+$").search
def get(nick=None, ident=None, host=None, realname=None, account=None, *, allow_multiple=False, allow_none=False, allow_bot=False): def _get(nick=None, ident=None, host=None, realname=None, account=None, *, allow_multiple=False, allow_none=False, allow_bot=False):
"""Return the matching user(s) from the user list. """Return the matching user(s) from the user list.
This takes up to 5 positional arguments (nick, ident, host, realname, This takes up to 5 positional arguments (nick, ident, host, realname,
@ -80,7 +87,11 @@ def get(nick=None, ident=None, host=None, realname=None, account=None, *, allow_
return potential[0] return potential[0]
def add(cli, *, nick, ident=None, host=None, realname=None, account=None, channels=None): def get(nick, *stuff, **morestuff): # backwards-compatible API - kill this as soon as possible!
var.USERS[nick] # _user(nick) evaluates lazily, so check eagerly if the nick exists
return _user(nick)
def _add(cli, *, nick, ident=None, host=None, realname=None, account=None, channels=None):
"""Create a new user, add it to the user list and return it. """Create a new user, add it to the user list and return it.
This function takes up to 6 keyword-only arguments (and no positional This function takes up to 6 keyword-only arguments (and no positional
@ -109,7 +120,10 @@ def add(cli, *, nick, ident=None, host=None, realname=None, account=None, channe
_users.add(new) _users.add(new)
return new return new
def exists(nick=None, ident=None, host=None, realname=None, account=None, *, allow_multiple=False, allow_bot=False): def add(nick, **blah): # backwards-compatible API
var.USERS[nick] = blah
def _exists(nick=None, ident=None, host=None, realname=None, account=None, *, allow_multiple=False, allow_bot=False):
"""Return True if a matching user exists. """Return True if a matching user exists.
Positional and keyword arguments are the same as get(), with the Positional and keyword arguments are the same as get(), with the
@ -125,10 +139,22 @@ def exists(nick=None, ident=None, host=None, realname=None, account=None, *, all
return True return True
def users(): def exists(nick, *stuff, **morestuff): # backwards-compatible API
return nick in var.USERS
def users_():
"""Iterate over the users in the registry.""" """Iterate over the users in the registry."""
yield from _users yield from _users
def users(): # backwards-compatible API
yield from var.USERS
def _items(): # backwards-compat crap (really, it stinks)
yield from var.USERS.items()
users.items = _items
del _items
_raw_nick_pattern = re.compile(r"^(?P<nick>.+?)(?:!(?P<ident>.+?)@(?P<host>.+))?$") _raw_nick_pattern = re.compile(r"^(?P<nick>.+?)(?:!(?P<ident>.+?)@(?P<host>.+))?$")
def parse_rawnick(rawnick, *, default=None): def parse_rawnick(rawnick, *, default=None):

View File

@ -46,19 +46,14 @@ import botconfig
import src import src
import src.settings as var import src.settings as var
from src.utilities import * from src.utilities import *
from src import db, decorators, events, logger, proxy, debuglog, errlog, plog from src import db, decorators, events, users, logger, proxy, debuglog, errlog, plog
from src.decorators import cmd, hook, handle_error, event_listener, COMMANDS
from src.messages import messages from src.messages import messages
from src.warnings import * from src.warnings import *
# done this way so that events is accessible in !eval (useful for debugging) # done this way so that events is accessible in !eval (useful for debugging)
Event = events.Event Event = events.Event
cmd = decorators.cmd
hook = decorators.hook
handle_error = decorators.handle_error
event_listener = decorators.event_listener
COMMANDS = decorators.COMMANDS
# Game Logic Begins: # Game Logic Begins:
var.LAST_STATS = None var.LAST_STATS = None
@ -176,7 +171,7 @@ def connect_callback(cli):
var.DISABLE_ACCOUNTS = True var.DISABLE_ACCOUNTS = True
var.ACCOUNTS_ONLY = False var.ACCOUNTS_ONLY = False
if nick in var.USERS: if users.exists(nick, user, host):
return return
if nick == botconfig.NICK: if nick == botconfig.NICK:
@ -185,17 +180,18 @@ def connect_callback(cli):
cli.hostmask = host cli.hostmask = host
if "+" in status: if "+" in status:
to_be_devoiced.append(user) to_be_devoiced.append(nick)
newstat = "" newstat = ""
for stat in status: for stat in status:
if not stat in var.MODES_PREFIXES: if not stat in var.MODES_PREFIXES:
continue continue
newstat += var.MODES_PREFIXES[stat] newstat += var.MODES_PREFIXES[stat]
var.USERS[nick] = dict(ident=user,host=host,account="*",inchan=True,modes=set(newstat),moded=set()) users.add(nick, ident=user,host=host,account="*",inchan=True,modes=set(newstat),moded=set())
@hook("whospcrpl", hookid=295) @hook("whospcrpl", hookid=295)
def on_whoreply(cli, server, nick, ident, host, _, user, status, acc): def on_whoreply(cli, server, nick, ident, host, _, user, status, acc):
if user in var.USERS: return # Don't add someone who is already there if users.exists(user, ident, host):
return # Don't add someone who is already there
if user == botconfig.NICK: if user == botconfig.NICK:
cli.nickname = user cli.nickname = user
cli.ident = ident cli.ident = ident
@ -209,7 +205,7 @@ def connect_callback(cli):
if not stat in var.MODES_PREFIXES: if not stat in var.MODES_PREFIXES:
continue continue
newstat += var.MODES_PREFIXES[stat] newstat += var.MODES_PREFIXES[stat]
var.USERS[user] = dict(ident=ident,host=host,account=acc,inchan=True,modes=set(newstat),moded=set()) users.add(user, ident=ident,host=host,account=acc,inchan=True,modes=set(newstat),moded=set())
@hook("endofwho", hookid=295) @hook("endofwho", hookid=295)
def afterwho(*args): def afterwho(*args):
@ -238,8 +234,8 @@ def connect_callback(cli):
return return
if modeaction == "+o" and target == botconfig.NICK: if modeaction == "+o" and target == botconfig.NICK:
var.OPPED = True var.OPPED = True
if botconfig.NICK in var.USERS: if users.exists(botconfig.NICK):
var.USERS[botconfig.NICK]["modes"].add("o") users.get(botconfig.NICK).modes.add("o")
if var.PHASE == "none": if var.PHASE == "none":
@hook("quietlistend", hookid=297) @hook("quietlistend", hookid=297)
@ -263,7 +259,7 @@ def connect_callback(cli):
else: else:
cli.who(botconfig.CHANNEL, "%uhsnfa") cli.who(botconfig.CHANNEL, "%uhsnfa")
@hook("mode") @hook("mode") # XXX Get rid of this when the user/channel refactor is done
def check_for_modes(cli, rnick, chan, modeaction, *target): def check_for_modes(cli, rnick, chan, modeaction, *target):
nick = parse_nick(rnick)[0] nick = parse_nick(rnick)[0]
if chan != botconfig.CHANNEL: if chan != botconfig.CHANNEL:
@ -336,8 +332,8 @@ def reset_modes_timers(cli):
continue continue
for mode in var.USERS[plr]["moded"]: for mode in var.USERS[plr]["moded"]:
cmodes.append(("+"+mode, plr)) cmodes.append(("+"+mode, plr))
var.USERS[plr]["modes"].update(var.USERS[plr]["moded"]) users.get(plr).modes.update(users.get(plr).moded)
var.USERS[plr]["moded"] = set() users.get(plr).moded = set()
if var.QUIET_DEAD_PLAYERS: if var.QUIET_DEAD_PLAYERS:
for deadguy in var.DEAD: for deadguy in var.DEAD:
if not is_fake_nick(deadguy): if not is_fake_nick(deadguy):
@ -384,7 +380,7 @@ def fsync(cli, nick, chan, rest):
def sync_modes(cli): def sync_modes(cli):
voices = [] voices = []
pl = list_players() pl = list_players()
for nick, u in var.USERS.items(): for nick, u in users.users.items(): # that's really *just* for backwards-compat
if var.DEVOICE_DURING_NIGHT and var.PHASE == "night": if var.DEVOICE_DURING_NIGHT and var.PHASE == "night":
if "v" in u.get("modes", set()): if "v" in u.get("modes", set()):
voices.append(("-v", nick)) voices.append(("-v", nick))
@ -543,10 +539,10 @@ def mark_simple_notify(cli, nick, chan, rest):
"""Makes the bot give you simple role instructions, in case you are familiar with the roles.""" """Makes the bot give you simple role instructions, in case you are familiar with the roles."""
nick, _, ident, host = parse_nick(nick) nick, _, ident, host = parse_nick(nick)
if nick in var.USERS: if users.exists(nick):
ident = irc_lower(var.USERS[nick]["ident"]) ident = irc_lower(users.get(nick).ident)
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
else: else:
acc = None acc = None
if not acc or acc == "*": if not acc or acc == "*":
@ -605,10 +601,10 @@ def mark_prefer_notice(cli, nick, chan, rest):
# and not an intentional invocation of this command # and not an intentional invocation of this command
return return
if nick in var.USERS: if users.exists(nick):
ident = irc_lower(var.USERS[nick]["ident"]) ident = irc_lower(users.get(nick).ident)
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
else: else:
acc = None acc = None
if not acc or acc == "*": if not acc or acc == "*":
@ -658,7 +654,7 @@ def mark_prefer_notice(cli, nick, chan, rest):
@cmd("swap", "replace", pm=True, phases=("join", "day", "night")) @cmd("swap", "replace", pm=True, phases=("join", "day", "night"))
def replace(cli, nick, chan, rest): def replace(cli, nick, chan, rest):
"""Swap out a player logged in to your account.""" """Swap out a player logged in to your account."""
if nick not in var.USERS or not var.USERS[nick]["inchan"]: if not users.exists(nick) or not users.get(nick).inchan:
pm(cli, nick, messages["invalid_channel"].format(botconfig.CHANNEL)) pm(cli, nick, messages["invalid_channel"].format(botconfig.CHANNEL))
return return
@ -666,7 +662,7 @@ def replace(cli, nick, chan, rest):
reply(cli, nick, chan, messages["already_playing"].format("You"), private=True) reply(cli, nick, chan, messages["already_playing"].format("You"), private=True)
return return
account = irc_lower(var.USERS[nick]["account"]) account = irc_lower(users.get(nick).account)
if not account or account == "*": if not account or account == "*":
reply(cli, nick, chan, messages["not_logged_in"], private=True) reply(cli, nick, chan, messages["not_logged_in"], private=True)
@ -677,8 +673,8 @@ def replace(cli, nick, chan, rest):
if not rest: # bare call if not rest: # bare call
target = None target = None
for user in var.USERS: for user in users.users():
if irc_lower(var.USERS[user]["account"]) == account: if irc_lower(users.get(user).account) == account:
if user == nick or user not in list_participants(): if user == nick or user not in list_participants():
pass pass
elif target is None: elif target is None:
@ -705,11 +701,11 @@ def replace(cli, nick, chan, rest):
reply(cli, nick, chan, msg, private=True) reply(cli, nick, chan, msg, private=True)
return return
if var.USERS[target]["account"] == "*": if users.get(target).account in ("*", None):
reply(cli, nick, chan, messages["target_not_logged_in"], private=True) reply(cli, nick, chan, messages["target_not_logged_in"], private=True)
return return
if irc_lower(var.USERS[target]["account"]) == account and nick != target: if irc_lower(users.get(target).account) == account and nick != target:
rename_player(cli, target, nick) rename_player(cli, target, nick)
# Make sure to remove player from var.DISCONNECTED if they were in there # Make sure to remove player from var.DISCONNECTED if they were in there
if var.PHASE in var.GAME_PHASES: if var.PHASE in var.GAME_PHASES:
@ -726,8 +722,8 @@ def altpinger(cli, nick, chan, rest):
"""Pings you when the number of players reaches your preference. Usage: "pingif <players>". https://werewolf.chat/Pingif""" """Pings you when the number of players reaches your preference. Usage: "pingif <players>". https://werewolf.chat/Pingif"""
players = is_user_altpinged(nick) players = is_user_altpinged(nick)
rest = rest.split() rest = rest.split()
if nick in var.USERS: if users.exists(nick):
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
else: else:
reply(cli, nick, chan, messages["invalid_channel"].format(botconfig.CHANNEL), private=True) reply(cli, nick, chan, messages["invalid_channel"].format(botconfig.CHANNEL), private=True)
return return
@ -775,10 +771,10 @@ def altpinger(cli, nick, chan, rest):
reply(cli, nick, chan, "\n".join(msg), private=True) reply(cli, nick, chan, "\n".join(msg), private=True)
def is_user_altpinged(nick): def is_user_altpinged(nick):
if nick in var.USERS.keys(): if users.exists(nick):
ident = irc_lower(var.USERS[nick]["ident"]) ident = irc_lower(users.get(nick).ident)
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
else: else:
return 0 return 0
if not var.DISABLE_ACCOUNTS and acc and acc != "*": if not var.DISABLE_ACCOUNTS and acc and acc != "*":
@ -792,9 +788,9 @@ def is_user_altpinged(nick):
def toggle_altpinged_status(nick, value, old=None): def toggle_altpinged_status(nick, value, old=None):
# nick should be in var.USERS if not fake; if not, let the error propagate # nick should be in var.USERS if not fake; if not, let the error propagate
ident = irc_lower(var.USERS[nick]["ident"]) ident = irc_lower(users.get(nick).ident)
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
if value == 0: if value == 0:
if not var.DISABLE_ACCOUNTS and acc and acc != "*": if not var.DISABLE_ACCOUNTS and acc and acc != "*":
if acc in var.PING_IF_PREFS_ACCS: if acc in var.PING_IF_PREFS_ACCS:
@ -861,7 +857,7 @@ def join_timer_handler(cli):
# Don't ping alt connections of users that have already joined # Don't ping alt connections of users that have already joined
if not var.DISABLE_ACCOUNTS: if not var.DISABLE_ACCOUNTS:
for acc in (var.USERS[player]["account"] for player in pl if player in var.USERS): for acc in (users.get(player).account for player in pl if users.exists(player)):
var.PINGED_ALREADY_ACCS.add(irc_lower(acc)) var.PINGED_ALREADY_ACCS.add(irc_lower(acc))
# Remove players who have already been pinged from the list of possible players to ping # Remove players who have already been pinged from the list of possible players to ping
@ -936,9 +932,9 @@ def join_timer_handler(cli):
cli.who(botconfig.CHANNEL) cli.who(botconfig.CHANNEL)
def get_deadchat_pref(nick): def get_deadchat_pref(nick):
if nick in var.USERS: if users.exists(nick):
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
else: else:
return False return False
@ -1008,9 +1004,9 @@ def deadchat_pref(cli, nick, chan, rest):
if not var.ENABLE_DEADCHAT: if not var.ENABLE_DEADCHAT:
return return
if nick in var.USERS: if users.exists(nick):
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
else: else:
reply(cli, nick, chan, messages["invalid_channel"].format(botconfig.CHANNEL), private=True) reply(cli, nick, chan, messages["invalid_channel"].format(botconfig.CHANNEL), private=True)
return return
@ -1053,7 +1049,7 @@ def join(cli, nick, chan, rest):
if chan == nick: if chan == nick:
return return
if var.ACCOUNTS_ONLY: if var.ACCOUNTS_ONLY:
if nick in var.USERS and (not var.USERS[nick]["account"] or var.USERS[nick]["account"] == "*"): if users.exists(nick) and (not users.get(nick).account or users.get(nick).account == "*"):
cli.notice(nick, messages["not_logged_in"]) cli.notice(nick, messages["not_logged_in"])
return return
if evt.data["join_player"](cli, nick, chan) and rest: if evt.data["join_player"](cli, nick, chan) and rest:
@ -1077,10 +1073,10 @@ def join_player(cli, player, chan, who=None, forced=False, *, sanity=True):
cli.msg(var.CHANSERV, var.CHANSERV_OP_COMMAND.format(channel=botconfig.CHANNEL)) cli.msg(var.CHANSERV, var.CHANSERV_OP_COMMAND.format(channel=botconfig.CHANNEL))
return False return False
if player in var.USERS: if users.exists(player):
ident = irc_lower(var.USERS[player]["ident"]) ident = irc_lower(users.get(player).ident)
host = var.USERS[player]["host"].lower() host = users.get(player).host.lower()
acc = irc_lower(var.USERS[player]["account"]) acc = irc_lower(users.get(player).account)
hostmask = player + "!" + ident + "@" + host hostmask = player + "!" + ident + "@" + host
elif is_fake_nick(player) and botconfig.DEBUG_MODE: elif is_fake_nick(player) and botconfig.DEBUG_MODE:
# fakenick # fakenick
@ -1111,11 +1107,11 @@ def join_player(cli, player, chan, who=None, forced=False, *, sanity=True):
cmodes = [("+v", player)] cmodes = [("+v", player)]
if var.PHASE == "none": if var.PHASE == "none":
if var.AUTO_TOGGLE_MODES and player in var.USERS and var.USERS[player]["modes"]: if var.AUTO_TOGGLE_MODES and users.exists(player) and users.get(player).modes:
for mode in var.USERS[player]["modes"]: for mode in users.get(player).modes:
cmodes.append(("-"+mode, player)) cmodes.append(("-"+mode, player))
var.USERS[player]["moded"].update(var.USERS[player]["modes"]) users.get(player).moded.update(users.get(player).modes)
var.USERS[player]["modes"] = set() users.get(player).modes = set()
mass_mode(cli, cmodes, []) mass_mode(cli, cmodes, [])
var.ROLES["person"].add(player) var.ROLES["person"].add(player)
var.ALL_PLAYERS.append(player) var.ALL_PLAYERS.append(player)
@ -1155,7 +1151,7 @@ def join_player(cli, player, chan, who=None, forced=False, *, sanity=True):
else: else:
if acc is not None and not botconfig.DEBUG_MODE: if acc is not None and not botconfig.DEBUG_MODE:
for user in pl: for user in pl:
if irc_lower(var.USERS[user]["account"]) == acc: if irc_lower(users.get(user).account) == acc:
msg = messages["account_already_joined"] msg = messages["account_already_joined"]
if who == player: if who == player:
cli.notice(who, msg.format(user, "your", messages["join_swap_instead"].format(botconfig.CMD_CHAR))) cli.notice(who, msg.format(user, "your", messages["join_swap_instead"].format(botconfig.CMD_CHAR)))
@ -1165,11 +1161,11 @@ def join_player(cli, player, chan, who=None, forced=False, *, sanity=True):
var.ALL_PLAYERS.append(player) var.ALL_PLAYERS.append(player)
if not is_fake_nick(player) or not botconfig.DEBUG_MODE: if not is_fake_nick(player) or not botconfig.DEBUG_MODE:
if var.AUTO_TOGGLE_MODES and var.USERS[player]["modes"]: if var.AUTO_TOGGLE_MODES and users.get(player).modes:
for mode in var.USERS[player]["modes"]: for mode in var.USERS[player]["modes"]:
cmodes.append(("-"+mode, player)) cmodes.append(("-"+mode, player))
var.USERS[player]["moded"].update(var.USERS[player]["modes"]) users.get(player).moded.update(users.get(player).modes)
var.USERS[player]["modes"] = set() users.get(player).modes = set()
mass_mode(cli, cmodes, []) mass_mode(cli, cmodes, [])
cli.msg(chan, messages["player_joined"].format(player, len(pl) + 1)) cli.msg(chan, messages["player_joined"].format(player, len(pl) + 1))
if not sanity: if not sanity:
@ -1278,7 +1274,7 @@ def fjoin(cli, nick, chan, rest):
if not is_fake_nick(tojoin): if not is_fake_nick(tojoin):
tojoin = ul[ull.index(tojoin.lower())].strip() tojoin = ul[ull.index(tojoin.lower())].strip()
if not botconfig.DEBUG_MODE and var.ACCOUNTS_ONLY: if not botconfig.DEBUG_MODE and var.ACCOUNTS_ONLY:
if not var.USERS[tojoin]["account"] or var.USERS[tojoin]["account"] == "*": if not users.get(tojoin).account or users.get(tojoin).account == "*":
cli.notice(nick, messages["account_not_logged_in"].format(tojoin)) cli.notice(nick, messages["account_not_logged_in"].format(tojoin))
return return
elif botconfig.DEBUG_MODE: elif botconfig.DEBUG_MODE:
@ -1351,9 +1347,9 @@ def on_kicked(cli, nick, chan, victim, reason):
cli.join(chan) cli.join(chan)
if chan == botconfig.CHANNEL and var.CHANSERV_OP_COMMAND: if chan == botconfig.CHANNEL and var.CHANSERV_OP_COMMAND:
cli.msg(var.CHANSERV, var.CHANSERV_OP_COMMAND.format(channel=botconfig.CHANNEL)) cli.msg(var.CHANSERV, var.CHANSERV_OP_COMMAND.format(channel=botconfig.CHANNEL))
if var.AUTO_TOGGLE_MODES and victim in var.USERS: if var.AUTO_TOGGLE_MODES and users.exists(victim):
var.USERS[victim]["modes"] = set() users.get(victim).modes = set()
var.USERS[victim]["moded"] = set() users.get(victim).moded = set()
@hook("account") @hook("account")
def on_account(cli, rnick, acc): def on_account(cli, rnick, acc):
@ -1365,16 +1361,16 @@ def on_account(cli, rnick, acc):
leave(cli, "account", nick) leave(cli, "account", nick)
if var.PHASE not in "join": if var.PHASE not in "join":
cli.mode(chan, "-v", nick) cli.mode(chan, "-v", nick)
cli.notice(nick, messages["account_reidentify"].format(var.USERS[nick]["account"])) cli.notice(nick, messages["account_reidentify"].format(users.get(nick).account))
else: else:
cli.notice(nick, messages["account_midgame_change"]) cli.notice(nick, messages["account_midgame_change"])
if nick in var.USERS.keys(): if users.exists(nick).keys():
var.USERS[nick]["ident"] = ident users.get(nick).ident = ident
var.USERS[nick]["host"] = host users.get(nick).host = host
var.USERS[nick]["account"] = acc users.get(nick).account = acc
if nick in var.DISCONNECTED.keys(): if nick in var.DISCONNECTED.keys():
if lacc == var.DISCONNECTED[nick][0]: if lacc == var.DISCONNECTED[nick][0]:
if nick in var.USERS and var.USERS[nick]["inchan"]: if users.exists(nick) and users.get(nick).inchan:
with var.GRAVEYARD_LOCK: with var.GRAVEYARD_LOCK:
hm = var.DISCONNECTED[nick][1] hm = var.DISCONNECTED[nick][1]
act = var.DISCONNECTED[nick][0] act = var.DISCONNECTED[nick][0]
@ -2419,18 +2415,18 @@ def stop_game(cli, winner="", abort=False, additional_winners=None, log=True):
if plr.startswith("(dced)"): if plr.startswith("(dced)"):
pentry["dced"] = True pentry["dced"] = True
splr = plr[6:] splr = plr[6:]
if splr in var.USERS: if users.exists(splr):
if not var.DISABLE_ACCOUNTS: if not var.DISABLE_ACCOUNTS:
pentry["account"] = var.USERS[splr]["account"] pentry["account"] = users.get(splr).account
pentry["nick"] = splr pentry["nick"] = splr
pentry["ident"] = var.USERS[splr]["ident"] pentry["ident"] = users.get(splr).ident
pentry["host"] = var.USERS[splr]["host"] pentry["host"] = users.get(splr).host
elif plr in var.USERS: elif plr in var.USERS:
if not var.DISABLE_ACCOUNTS: if not var.DISABLE_ACCOUNTS:
pentry["account"] = var.USERS[plr]["account"] pentry["account"] = users.get(plr).account
pentry["nick"] = plr pentry["nick"] = plr
pentry["ident"] = var.USERS[plr]["ident"] pentry["ident"] = users.get(plr).ident
pentry["host"] = var.USERS[plr]["host"] pentry["host"] = users.get(plr).host
pentry["role"] = rol pentry["role"] = rol
pentry["templates"] = pltp[plr] pentry["templates"] = pltp[plr]
@ -3048,9 +3044,9 @@ def del_player(cli, nick, forced_death=False, devoice=True, end_game=True, death
if devoice and (var.PHASE != "night" or not var.DEVOICE_DURING_NIGHT): if devoice and (var.PHASE != "night" or not var.DEVOICE_DURING_NIGHT):
cmode.append(("-v", nick)) cmode.append(("-v", nick))
if nick in var.USERS: if users.exists(nick):
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
if acc not in var.DEADCHAT_PREFS_ACCS and host not in var.DEADCHAT_PREFS: if acc not in var.DEADCHAT_PREFS_ACCS and host not in var.DEADCHAT_PREFS:
deadchat.append(nick) deadchat.append(nick)
# devoice all players that died as a result, if we are in the original del_player # devoice all players that died as a result, if we are in the original del_player
@ -3070,11 +3066,11 @@ def del_player(cli, nick, forced_death=False, devoice=True, end_game=True, death
del var.TIMERS["start_votes"] del var.TIMERS["start_votes"]
# Died during the joining process as a person # Died during the joining process as a person
if var.AUTO_TOGGLE_MODES and nick in var.USERS and var.USERS[nick]["moded"]: if var.AUTO_TOGGLE_MODES and users.exists(nick) and users.get(nick).moded:
for newmode in var.USERS[nick]["moded"]: for newmode in var.USERS[nick]["moded"]:
cmode.append(("+"+newmode, nick)) cmode.append(("+"+newmode, nick))
var.USERS[nick]["modes"].update(var.USERS[nick]["moded"]) users.get(nick).modes.update(users.get(nick).moded)
var.USERS[nick]["moded"] = set() users.get(nick).moded = set()
var.ALL_PLAYERS.remove(nick) var.ALL_PLAYERS.remove(nick)
ret = not chk_win(cli) ret = not chk_win(cli)
else: else:
@ -3256,15 +3252,15 @@ def on_join(cli, raw_nick, chan, acc="*", rname=""):
nick, _, ident, host = parse_nick(raw_nick) nick, _, ident, host = parse_nick(raw_nick)
if nick == botconfig.NICK: if nick == botconfig.NICK:
plog("Joined {0}".format(chan)) plog("Joined {0}".format(chan))
elif nick not in var.USERS.keys(): elif not users.exists(nick):
var.USERS[nick] = dict(ident=ident,host=host,account=acc,inchan=chan == botconfig.CHANNEL,modes=set(),moded=set()) users.add(nick, ident=ident,host=host,account=acc,inchan=(chan == botconfig.CHANNEL),modes=set(),moded=set())
else: else:
var.USERS[nick]["ident"] = ident users.get(nick).ident = ident
var.USERS[nick]["host"] = host users.get(nick).host = host
var.USERS[nick]["account"] = acc users.get(nick).account = acc
if not var.USERS[nick]["inchan"]: if not users.get(nick).inchan:
# Will be True if the user joined the main channel, else False # Will be True if the user joined the main channel, else False
var.USERS[nick]["inchan"] = (chan == botconfig.CHANNEL) users.get(nick).inchan = (chan == botconfig.CHANNEL)
if chan != botconfig.CHANNEL: if chan != botconfig.CHANNEL:
return return
with var.GRAVEYARD_LOCK: with var.GRAVEYARD_LOCK:
@ -3329,8 +3325,7 @@ def goat(cli, nick, chan, rest):
def fgoat(cli, nick, chan, rest): def fgoat(cli, nick, chan, rest):
"""Forces a goat to interact with anyone or anything, without limitations.""" """Forces a goat to interact with anyone or anything, without limitations."""
nick_ = rest.split(' ')[0].strip() nick_ = rest.split(' ')[0].strip()
ul = list(var.USERS.keys()) if nick_.lower() in (x.lower() for x in users.users()):
if nick_.lower() in (x.lower() for x in ul):
togoat = nick_ togoat = nick_
else: else:
togoat = rest togoat = rest
@ -3344,10 +3339,10 @@ def return_to_village(cli, chan, nick, show_message):
if nick in var.DISCONNECTED.keys(): if nick in var.DISCONNECTED.keys():
hm = var.DISCONNECTED[nick][1] hm = var.DISCONNECTED[nick][1]
act = var.DISCONNECTED[nick][0] act = var.DISCONNECTED[nick][0]
if nick in var.USERS: if users.exists(nick):
ident = irc_lower(var.USERS[nick]["ident"]) ident = irc_lower(users.get(nick).ident)
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
else: else:
acc = None acc = None
if not acc or acc == "*": if not acc or acc == "*":
@ -3514,7 +3509,7 @@ def rename_player(cli, prefix, nick):
var.NO_LYNCH.remove(prefix) var.NO_LYNCH.remove(prefix)
var.NO_LYNCH.add(nick) var.NO_LYNCH.add(nick)
@hook("nick") @hook("nick") # XXX Update once the user/channel refactor is done
def on_nick(cli, oldnick, nick): def on_nick(cli, oldnick, nick):
prefix, _, ident, host = parse_nick(oldnick) prefix, _, ident, host = parse_nick(oldnick)
chan = botconfig.CHANNEL chan = botconfig.CHANNEL
@ -3542,12 +3537,12 @@ def on_nick(cli, oldnick, nick):
def leave(cli, what, nick, why=""): def leave(cli, what, nick, why=""):
nick, _, ident, host = parse_nick(nick) nick, _, ident, host = parse_nick(nick)
if nick in var.USERS: if users.exists(nick):
acc = irc_lower(var.USERS[nick]["account"]) acc = irc_lower(users.get(nick).account)
ident = irc_lower(var.USERS[nick]["ident"]) ident = irc_lower(users.get(nick).ident)
host = var.USERS[nick]["host"].lower() host = users.get(nick).host.lower()
if what == "quit" or (not what in ("account",) and why == botconfig.CHANNEL): if what == "quit" or (not what in ("account",) and why == botconfig.CHANNEL):
var.USERS[nick]["inchan"] = False users.get(nick).inchan = False
else: else:
acc = None acc = None
if not acc or acc == "*": if not acc or acc == "*":
@ -3617,9 +3612,9 @@ def leave(cli, what, nick, why=""):
var.SPECTATING_WOLFCHAT.discard(nick) var.SPECTATING_WOLFCHAT.discard(nick)
var.SPECTATING_DEADCHAT.discard(nick) var.SPECTATING_DEADCHAT.discard(nick)
leave_deadchat(cli, nick) leave_deadchat(cli, nick)
if what not in ("badnick", "account") and nick in var.USERS: if what not in ("badnick", "account") and users.exists(nick):
var.USERS[nick]["modes"] = set() users.get(nick).modes = set()
var.USERS[nick]["moded"] = set() users.get(nick).moded = set()
if killplayer: if killplayer:
del_player(cli, nick, death_triggers = False) del_player(cli, nick, death_triggers = False)
else: else:
@ -7332,7 +7327,7 @@ def _say(cli, raw_nick, rest, command, action=False):
(target, message) = rest (target, message) = rest
if not is_admin(nick, ident, host): if not is_admin(nick, ident, host):
if nick not in var.USERS: if not users.exists(nick):
pm(cli, nick, messages["wrong_channel"].format( pm(cli, nick, messages["wrong_channel"].format(
botconfig.CHANNEL)) botconfig.CHANNEL))
@ -7372,11 +7367,11 @@ def can_run_restricted_cmd(nick):
if nick in pl: if nick in pl:
return False return False
if nick in var.USERS and var.USERS[nick]["account"] in [var.USERS[player]["account"] for player in pl if player in var.USERS]: if users.exists(nick) and users.get(nick).account in [users.get(player).account for player in pl if users.exists(player)]:
return False return False
hostmask = var.USERS[nick]["ident"] + "@" + var.USERS[nick]["host"] hostmask = users.get(nick).ident + "@" + users.get(nick).host
if nick in var.USERS and hostmask in [var.USERS[player]["ident"] + "@" + var.USERS[player]["host"] for player in pl if player in var.USERS]: if users.exists(nick) and hostmask in [users.get(player).ident + "@" + users.get(player).host for player in pl if users.exists(player)]:
return False return False
return True return True
@ -7594,7 +7589,7 @@ if botconfig.DEBUG_MODE or botconfig.ALLOWED_NORMAL_MODE_COMMANDS:
who = list_players() who = list_players()
else: else:
if not is_fake_nick(who): if not is_fake_nick(who):
ul = list(var.USERS.keys()) ul = list(var.USERS.keys()) # ark
ull = [u.lower() for u in ul] ull = [u.lower() for u in ul]
if who.lower() not in ull: if who.lower() not in ull:
cli.msg(chan, messages["invalid_target"]) cli.msg(chan, messages["invalid_target"])
@ -7608,7 +7603,7 @@ if botconfig.DEBUG_MODE or botconfig.ALLOWED_NORMAL_MODE_COMMANDS:
for fn in COMMANDS[comm]: for fn in COMMANDS[comm]:
if fn.owner_only: if fn.owner_only:
continue continue
if fn.flag and nick in var.USERS and not is_admin(nick): if fn.flag and users.exists(nick) and not is_admin(nick):
# Not a full admin # Not a full admin
cli.notice(nick, messages["admin_only_force"]) cli.notice(nick, messages["admin_only_force"])
continue continue
@ -7648,7 +7643,7 @@ if botconfig.DEBUG_MODE or botconfig.ALLOWED_NORMAL_MODE_COMMANDS:
for fn in COMMANDS[comm]: for fn in COMMANDS[comm]:
if fn.owner_only: if fn.owner_only:
continue continue
if fn.flag and nick in var.USERS and not is_admin(nick): if fn.flag and users.exists(nick) and not is_admin(nick):
# Not a full admin # Not a full admin
cli.notice(nick, messages["admin_only_force"]) cli.notice(nick, messages["admin_only_force"])
continue continue