From 8a90145474dc18c45f2c6da300886c0edbc8dcf8 Mon Sep 17 00:00:00 2001 From: nyuszika7h Date: Sun, 6 Sep 2015 11:46:49 +0200 Subject: [PATCH] Case-insensitively compare the bot's nick when checking for PMs Closes #161. --- src/handler.py | 18 +++++++++++++----- src/settings.py | 18 ++++++++++++++++++ src/wolfgame.py | 7 +++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/handler.py b/src/handler.py index beeaae0..3f88b06 100644 --- a/src/handler.py +++ b/src/handler.py @@ -1,9 +1,9 @@ # The bot commands implemented in here are present no matter which module is loaded -import traceback import base64 import socket import sys +import traceback from oyoyo.parse import parse_nick @@ -19,7 +19,6 @@ sys.stderr.target_logger = log hook = decorators.hook def on_privmsg(cli, rawnick, chan, msg, notice = False): - try: prefixes = getattr(var, "STATUSMSG_PREFIXES") except AttributeError: @@ -28,11 +27,20 @@ def on_privmsg(cli, rawnick, chan, msg, notice = False): if botconfig.IGNORE_HIDDEN_COMMANDS and chan[0] in prefixes: return - if (notice and ((chan != botconfig.NICK and not botconfig.ALLOW_NOTICE_COMMANDS) or - (chan == botconfig.NICK and not botconfig.ALLOW_PRIVATE_NOTICE_COMMANDS))): + try: + getattr(var, "CASEMAPPING") + except AttributeError: + var.CASEMAPPING = "rfc1459" + + if not (notice and "!" not in rawnick and chan in ("*", "AUTH")): + # Not an on-connect message before RPL_ISUPPORT. + log("Server did not send a case mapping; falling back to rfc1459.") + + if (notice and ((not var.irc_equals(chan, botconfig.NICK) and not botconfig.ALLOW_NOTICE_COMMANDS) or + (var.irc_equals(chan, botconfig.NICK) and not botconfig.ALLOW_PRIVATE_NOTICE_COMMANDS))): return # not allowed in settings - if chan == botconfig.NICK: + if var.irc_equals(chan, botconfig.NICK): chan = parse_nick(rawnick)[0] for fn in decorators.COMMANDS[""]: diff --git a/src/settings.py b/src/settings.py index fb5e9f9..75b6a16 100644 --- a/src/settings.py +++ b/src/settings.py @@ -378,6 +378,24 @@ def check_priv(priv): is_admin = check_priv("admin") is_owner = check_priv("owner") +def irc_lower(nick): + mapping = { + "[": "{", + "]": "}", + "\\": "|", + "^": "~", + } + + if CASEMAPPING == "strict-rfc1459": + mapping.pop("^") + elif CASEMAPPING == "ascii": + mapping = {} + + return nick.lower().translate(str.maketrans(mapping)) + +def irc_equals(nick1, nick2): + return irc_lower(nick1) == irc_lower(nick2) + def plural(role): bits = role.split() bits[-1] = {"person": "people", "wolf": "wolves"}.get(bits[-1], bits[-1] + "s") diff --git a/src/wolfgame.py b/src/wolfgame.py index 918df20..697112d 100644 --- a/src/wolfgame.py +++ b/src/wolfgame.py @@ -5877,6 +5877,13 @@ def getfeatures(cli, nick, *rest): pass if r.startswith("STATUSMSG="): var.STATUSMSG_PREFIXES = list(r.split("=")[1]) + if r.startswith("CASEMAPPING="): + var.CASEMAPPING = r.split("=")[1] + + if var.CASEMAPPING not in ("rfc1459", "strict-rfc1459", "ascii"): + # This is very unlikely to happen, but just in case. + errlog("Unsupported case mapping: {0!r}; falling back to rfc1459.".format(var.CASEMAPPING)) + var.CASEMAPPING = "rfc1459" def mass_privmsg(cli, targets, msg, notice=False, privmsg=False): if not notice and not privmsg: