Update pingif timer handler

This commit is contained in:
Vgr E. Barry 2017-01-13 12:00:09 -05:00
parent 96c36a13b6
commit 4f36b189ff

View File

@ -638,7 +638,7 @@ def altpinger(var, wrapper, message):
wrapper.pm(*msg, sep="\n") wrapper.pm(*msg, sep="\n")
@handle_error @handle_error
def join_timer_handler(): def join_timer_handler(var):
with var.WARNING_LOCK: with var.WARNING_LOCK:
var.PINGING_IFS = True var.PINGING_IFS = True
to_ping = [] to_ping = []
@ -651,83 +651,60 @@ def join_timer_handler():
if not var.DISABLE_ACCOUNTS: if not var.DISABLE_ACCOUNTS:
for num in var.PING_IF_NUMS_ACCS: for num in var.PING_IF_NUMS_ACCS:
if num <= len(pl): if num <= len(pl):
chk_acc.update(var.PING_IF_NUMS_ACCS[num]) for acc in var.PING_IF_NUMS_ACCS[num]:
chk_acc.add(users.lower(acc))
if not var.ACCOUNTS_ONLY: if not var.ACCOUNTS_ONLY:
for num in var.PING_IF_NUMS: for num in var.PING_IF_NUMS:
if num <= len(pl): if num <= len(pl):
checker.update(var.PING_IF_NUMS[num]) for hostmask in var.PING_IF_NUMS[num]:
checker.add(users.lower(hostmask))
# 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 (users.get(player).account for player in pl if users.exists(player)): for player in pl:
var.PINGED_ALREADY_ACCS.add(irc_lower(acc)) user = users._get(player) # FIXME
var.PINGED_ALREADY_ACCS.add(users.lower(user.account))
# 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
for acc in frozenset(chk_acc): chk_acc -= var.PINGED_ALREADY_ACCS
if acc in var.PINGED_ALREADY_ACCS: checker -= var.PINGED_ALREADY
chk_acc.remove(acc)
for hostmask in frozenset(checker):
if hostmask in var.PINGED_ALREADY:
checker.remove(hostmask)
# If there is nobody to ping, do nothing # If there is nobody to ping, do nothing
if not chk_acc and not checker: if not chk_acc and not checker:
var.PINGING_IFS = False var.PINGING_IFS = False
return return
@hook("whoreply", hookid=387) # FIXME: Use events def get_altpingers(event, var, chan, user):
def ping_altpingers_noacc(cli, bot_server, bot_nick, chan, ident, host, server, nick, status, hopcount_gecos): if event.params.away or user.stasis_count() or not var.PINGING_IFS or user is users.Bot or user.nick in pl: # FIXME: Fix this when list_players() returns User instances
if ("G" in status or is_user_stasised(nick) or not var.PINGING_IFS or
nick == bot_nick or nick in pl):
return return
ident = irc_lower(ident) temp = user.lower()
host = host.lower() if temp.account is not None:
hostmask = ident + "@" + host if temp.account in chk_acc:
if hostmask in checker: to_ping.append(temp)
to_ping.append(nick) var.PINGED_ALREADY_ACCS.add(temp.account)
var.PINGED_ALREADY.add(hostmask)
@hook("whospcrpl", hookid=387)
def ping_altpingers(cli, bot_server, bot_nick, data, chan, ident, ip_address, host, server, nick, status, hop, idle, account, realname):
if ("G" in status or is_user_stasised(nick) or not var.PINGING_IFS or
nick == botconfig.NICK or nick in pl):
return
# Create list of players to ping
account = irc_lower(account)
ident = irc_lower(ident)
host = host.lower()
if account and account != "*":
if account in chk_acc:
to_ping.append(nick)
var.PINGED_ALREADY_ACCS.add(account)
elif not var.ACCOUNTS_ONLY: elif not var.ACCOUNTS_ONLY:
hostmask = ident + "@" + host if temp.userhost in checker:
to_ping.append(nick) to_ping.append(temp)
var.PINGED_ALREADY.add(hostmask) var.PINGED_ALREADY.add(temp.userhost)
@hook("endofwho", hookid=387) def ping_altpingers(event, var, request):
def fetch_altpingers(cli, *stuff): if request is channels.Main:
# fun fact: if someone joined 10 seconds after someone else, the bot would break. var.PINGING_IFS = False
# effectively, the join would delete join_pinger from var.TIMERS and this function if to_ping:
# here would be reached before it was created again, thus erroring and crashing. to_ping.sort(key=lambda x: x.nick)
# this is one of the multiple reasons we need unit testing user_list = [(user.ref or user).nick for user in to_ping]
# I was lucky to catch this in testing, as it requires precise timing
# it only failed if a join happened while this outer func had started
var.PINGING_IFS = False
hook.unhook(387)
if to_ping:
to_ping.sort(key=lambda x: x.lower())
msg_prefix = messages["ping_player"].format(len(pl), "" if len(pl) == 1 else "s") msg_prefix = messages["ping_player"].format(len(pl), "" if len(pl) == 1 else "s")
msg = msg_prefix + break_long_message(to_ping).replace("\n", "\n" + msg_prefix) channels.Main.send(*user_list, first=msg_prefix)
cli.msg(botconfig.CHANNEL, msg) events.remove_listener("who_result", get_altpingers)
events.remove_listener("who_end", ping_altpingers)
events.add_listener("who_result", get_altpingers)
events.add_listener("who_end", ping_altpingers)
channels.Main.who() channels.Main.who()
@ -980,7 +957,7 @@ def join_player(var, wrapper, who=None, forced=False, *, sanity=True):
if "join_pinger" in var.TIMERS: if "join_pinger" in var.TIMERS:
var.TIMERS["join_pinger"][0].cancel() var.TIMERS["join_pinger"][0].cancel()
t = threading.Timer(10, join_timer_handler) t = threading.Timer(10, join_timer_handler, (var,))
var.TIMERS["join_pinger"] = (t, time.time(), 10) var.TIMERS["join_pinger"] = (t, time.time(), 10)
t.daemon = True t.daemon = True
t.start() t.start()