Skip to content

Commit

Permalink
Add an .is_nick() function to the Nick class
Browse files Browse the repository at this point in the history
Enables easier checking for whether an identifier is a nick or a
channel. Closes #281.
  • Loading branch information
embolalia committed Mar 29, 2014
1 parent 6f164ba commit f1182ff
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 23 deletions.
7 changes: 2 additions & 5 deletions willie/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,7 @@ def __new__(cls, text, origin, bytes, match, event, args, self):
s = unicode.__new__(cls, text)

"""Is trigger from a channel or in PM"""
s.is_privmsg = False
if (origin.sender is not None
and not origin.sender.startswith(('#', '&', '+', '!'))):
s.is_privmsg = True
s.is_privmsg = origin.sender.is_nick()

s.sender = origin.sender
"""
Expand Down Expand Up @@ -753,7 +750,7 @@ def call(self, func, origin, willie, trigger):
self.times[nick][func] = time.time()

def limit(self, origin, func):
if origin.sender and origin.sender.startswith('#'):
if origin.sender and not origin.sender.is_nick():
if self.config.has_section('limit'):
limits = self.config.limit.get(origin.sender)
if limits and (func.__module__ not in limits):
Expand Down
2 changes: 1 addition & 1 deletion willie/coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def track_modes(bot, trigger):

# If the first character of where the mode is being set isn't a #
# then it's a user mode, not a channel mode, so we'll ignore it.
if channel[0] != '#':
if channel.is_nick():
return

def handle_old_modes(nick, mode):
Expand Down
25 changes: 13 additions & 12 deletions willie/modules/adminchannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import re
from willie.module import commands, priority, OP
from willie.tools import Nick


def setup(bot):
Expand Down Expand Up @@ -87,11 +88,11 @@ def kick(bot, trigger):
argc = len(text)
if argc < 2:
return
opt = text[1]
opt = Nick(text[1])
nick = opt
channel = trigger.sender
reasonidx = 2
if opt.startswith('#'):
if not opt.is_nick():
if argc < 3:
return
nick = text[2]
Expand Down Expand Up @@ -137,10 +138,10 @@ def ban(bot, trigger):
argc = len(text)
if argc < 2:
return
opt = text[1]
opt = Nick(text[1])
banmask = opt
channel = trigger.sender
if opt.startswith('#'):
if not opt.is_nick():
if argc < 3:
return
channel = opt
Expand All @@ -163,10 +164,10 @@ def unban(bot, trigger):
argc = len(text)
if argc < 2:
return
opt = text[1]
opt = Nick(text[1])
banmask = opt
channel = trigger.sender
if opt.startswith('#'):
if not opt.is_nick():
if argc < 3:
return
channel = opt
Expand All @@ -189,10 +190,10 @@ def quiet(bot, trigger):
argc = len(text)
if argc < 2:
return
opt = text[1]
opt = Nick(text[1])
quietmask = opt
channel = trigger.sender
if opt.startswith('#'):
if not opt.is_nick():
if argc < 3:
return
quietmask = text[2]
Expand All @@ -215,10 +216,10 @@ def unquiet(bot, trigger):
argc = len(text)
if argc < 2:
return
opt = text[1]
opt = Nick(text[1])
quietmask = opt
channel = trigger.sender
if opt.startswith('#'):
if not opt.is_nick():
if argc < 3:
return
quietmask = text[2]
Expand All @@ -243,11 +244,11 @@ def kickban(bot, trigger):
argc = len(text)
if argc < 4:
return
opt = text[1]
opt = Nick(text[1])
nick = opt
mask = text[2]
reasonidx = 3
if opt.startswith('#'):
if not opt.is_nick():
if argc < 5:
return
channel = opt
Expand Down
2 changes: 1 addition & 1 deletion willie/modules/chanlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def setup(bot):
def log_message(bot, message):
"Log every message in a channel"
# if this is a private message and we're not logging those, return early
if not bot.origin.sender.startswith("#") and not bot.config.chanlogs.privmsg:
if message.sender.is_nick() and not bot.config.chanlogs.privmsg:
return

# determine which template we want, message or action
Expand Down
2 changes: 1 addition & 1 deletion willie/modules/meetbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def take_comment(bot, trigger):
"""
target, message = trigger.group(2).split(None, 1)
target = Nick(target)
if trigger.sender[0] in '#&+!':
if not trigger.sender.is_nick():
return
if not ismeetingrunning(target):
bot.say("There's not currently a meeting in that channel.")
Expand Down
2 changes: 1 addition & 1 deletion willie/modules/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def wikipedia(bot, trigger):
#change lang if channel has custom language set
if (
trigger.sender and
trigger.sender.startswith('#') and
not trigger.sender.is_nick() and
bot.config.has_option('wikipedia', 'lang_per_channel')
):
customlang = re.search(
Expand Down
9 changes: 7 additions & 2 deletions willie/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
itervalues = dict.itervalues
iterkeys = dict.iterkeys

_channel_prefixes = ('#', '&', '+', '!')

class ExpressionEvaluator:

Expand Down Expand Up @@ -228,8 +229,7 @@ def __getitem__(self, key):


class Nick(unicode):

"""A `unicode` subclass which acts appropriately for an IRC nickname.
"""A `unicode` subclass which acts appropriately for IRC identifiers.
When used as normal `unicode` objects, case will be preserved.
However, when comparing two Nick objects, or comparing a Nick object with a
Expand Down Expand Up @@ -299,6 +299,11 @@ def __eq__(self, other):
def __ne__(self, other):
return not (self == other)

def is_nick(self):
"""Returns True if the Identifier is a nickname (as opposed to channel)
"""
return self and not self.startswith(_channel_prefixes)


class OutputRedirect:

Expand Down

0 comments on commit f1182ff

Please sign in to comment.