Skip to content

Commit

Permalink
willie.module: Add decorators for privilege checks
Browse files Browse the repository at this point in the history
Also switch to them in adminchannel and admin
  • Loading branch information
embolalia committed Feb 14, 2015
1 parent 0606ee7 commit 3689916
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 73 deletions.
68 changes: 22 additions & 46 deletions admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,51 @@ def configure(config):
config.add_option('admin', 'auto_accept_invites', "Auto Accept All Invites")


@willie.module.require_privmsg
@willie.module.require_admin
@willie.module.commands('join')
@willie.module.priority('low')
@willie.module.example('.join #example or .join #example key')
def join(bot, trigger):
"""Join the specified channel. This is an admin-only command."""
# Can only be done in privmsg by an admin
if not trigger.is_privmsg:
channel, key = trigger.group(3), trigger.group(4)
if not channel:
return

if trigger.admin:
channel, key = trigger.group(3), trigger.group(4)
if not channel:
return
elif not key:
bot.join(channel)
else:
bot.join(channel, key)
elif not key:
bot.join(channel)
else:
bot.join(channel, key)


@willie.module.require_privmsg
@willie.module.require_admin
@willie.module.commands('part')
@willie.module.priority('low')
@willie.module.example('.part #example')
def part(bot, trigger):
"""Part the specified channel. This is an admin-only command."""
# Can only be done in privmsg by an admin
if not trigger.is_privmsg:
return
if not trigger.admin:
return

channel, _sep, part_msg = trigger.group(2).partition(' ')
if part_msg:
bot.part(channel, part_msg)
else:
bot.part(channel)


@willie.module.require_privmsg
@willie.module.require_owner
@willie.module.commands('quit')
@willie.module.priority('low')
def quit(bot, trigger):
"""Quit from the server. This is an owner-only command."""
# Can only be done in privmsg by the owner
if not trigger.is_privmsg:
return
if not trigger.owner:
return

quit_message = trigger.group(2)
if not quit_message:
quit_message = 'Quitting on command from %s' % trigger.nick

bot.quit(quit_message)


@willie.module.require_privmsg
@willie.module.require_admin
@willie.module.commands('msg')
@willie.module.priority('low')
@willie.module.example('.msg #YourPants Does anyone else smell neurotoxin?')
Expand All @@ -87,10 +78,6 @@ def msg(bot, trigger):
Send a message to a given channel or nick. Can only be done in privmsg by an
admin.
"""
if not trigger.is_privmsg:
return
if not trigger.admin:
return
if trigger.group(2) is None:
return

Expand All @@ -102,17 +89,15 @@ def msg(bot, trigger):
bot.msg(channel, message)


@willie.module.require_privmsg
@willie.module.require_admin
@willie.module.commands('me')
@willie.module.priority('low')
def me(bot, trigger):
"""
Send an ACTION (/me) to a given channel or nick. Can only be done in privmsg
by an admin.
"""
if not trigger.is_privmsg:
return
if not trigger.admin:
return
if trigger.group(2) is None:
return

Expand Down Expand Up @@ -154,18 +139,18 @@ def hold_ground(bot, trigger):
bot.join(channel)


@willie.module.require_privmsg
@willie.module.require_admin
@willie.module.commands('mode')
@willie.module.priority('low')
def mode(bot, trigger):
"""Set a user mode on Willie. Can only be done in privmsg by an admin."""
if not trigger.is_privmsg:
return
if not trigger.admin:
return
mode = trigger.group(3)
bot.write(('MODE ', bot.nick + ' ' + mode))


@willie.module.require_privmsg("This command only works as a private message.")
@willie.module.require_admin("This command requires admin privileges.")
@willie.module.commands('set')
@willie.module.example('.set core.owner Me')
def set_config(bot, trigger):
Expand All @@ -178,13 +163,6 @@ def set_config(bot, trigger):
If there is no section, section will default to "core".
If value is None, the option will be deleted.
"""
if not trigger.is_privmsg:
bot.reply("This command only works as a private message.")
return
if not trigger.admin:
bot.reply("This command requires admin priviledges.")
return

# Get section and option from first argument.
arg1 = trigger.group(3).split('.')
if len(arg1) == 1:
Expand Down Expand Up @@ -214,12 +192,10 @@ def set_config(bot, trigger):
setattr(getattr(bot.config, section), option, value)


@willie.module.require_privmsg
@willie.module.require_admin
@willie.module.commands('save')
@willie.module.example('.save')
def save_config(bot, trigger):
"""Save state of willies config object to the configuration file."""
if not trigger.is_privmsg:
return
if not trigger.admin:
return
bot.config.save()
41 changes: 14 additions & 27 deletions adminchannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import re
from willie import formatting
from willie.module import commands, priority, OP, HALFOP
from willie.module import commands, priority, OP, HALFOP, require_privilege
from willie.tools import Identifier


Expand All @@ -25,14 +25,13 @@ def default_mask(trigger):
return '{} {} {} {}'.format(welcome, chan, topic_, arg)


@require_privilege(OP)
@commands('op')
def op(bot, trigger):
"""
Command to op users in a room. If no nick is given,
willie will op the nick who sent the command
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < OP:
return bot.reply("I'm not a channel operator!")
nick = trigger.group(2)
Expand All @@ -42,14 +41,13 @@ def op(bot, trigger):
bot.write(['MODE', channel, "+o", nick])


@require_privilege(OP)
@commands('deop')
def deop(bot, trigger):
"""
Command to deop users in a room. If no nick is given,
willie will deop the nick who sent the command
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < OP:
return bot.reply("I'm not a channel operator!")
nick = trigger.group(2)
Expand All @@ -59,14 +57,13 @@ def deop(bot, trigger):
bot.write(['MODE', channel, "-o", nick])


@require_privilege(OP)
@commands('voice')
def voice(bot, trigger):
"""
Command to voice users in a room. If no nick is given,
willie will voice the nick who sent the command
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < HALFOP:
return bot.reply("I'm not a channel operator!")
nick = trigger.group(2)
Expand All @@ -76,14 +73,13 @@ def voice(bot, trigger):
bot.write(['MODE', channel, "+v", nick])


@require_privilege(OP)
@commands('devoice')
def devoice(bot, trigger):
"""
Command to devoice users in a room. If no nick is given,
willie will devoice the nick who sent the command
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < HALFOP:
return bot.reply("I'm not a channel operator!")
nick = trigger.group(2)
Expand All @@ -93,14 +89,13 @@ def devoice(bot, trigger):
bot.write(['MODE', channel, "-v", nick])


@require_privilege(OP)
@commands('kick')
@priority('high')
def kick(bot, trigger):
"""
Kick a user from the channel.
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < HALFOP:
return bot.reply("I'm not a channel operator!")
text = trigger.group().split()
Expand Down Expand Up @@ -144,15 +139,14 @@ def configureHostMask(mask):
return ''


@require_privilege(OP)
@commands('ban')
@priority('high')
def ban(bot, trigger):
"""
This give admins the ability to ban a user.
The bot must be a Channel Operator for this command to work.
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < HALFOP:
return bot.reply("I'm not a channel operator!")
text = trigger.group().split()
Expand All @@ -173,14 +167,13 @@ def ban(bot, trigger):
bot.write(['MODE', channel, '+b', banmask])


@require_privilege(OP)
@commands('unban')
def unban(bot, trigger):
"""
This give admins the ability to unban a user.
The bot must be a Channel Operator for this command to work.
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < HALFOP:
return bot.reply("I'm not a channel operator!")
text = trigger.group().split()
Expand All @@ -201,14 +194,13 @@ def unban(bot, trigger):
bot.write(['MODE', channel, '-b', banmask])


@require_privilege(OP)
@commands('quiet')
def quiet(bot, trigger):
"""
This gives admins the ability to quiet a user.
The bot must be a Channel Operator for this command to work.
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < OP:
return bot.reply("I'm not a channel operator!")
text = trigger.group().split()
Expand All @@ -229,14 +221,13 @@ def quiet(bot, trigger):
bot.write(['MODE', channel, '+q', quietmask])


@require_privilege(OP)
@commands('unquiet')
def unquiet(bot, trigger):
"""
This gives admins the ability to unquiet a user.
The bot must be a Channel Operator for this command to work.
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < OP:
return bot.reply("I'm not a channel operator!")
text = trigger.group().split()
Expand All @@ -257,6 +248,7 @@ def unquiet(bot, trigger):
bot.write(['MODE', opt, '-q', quietmask])


@require_privilege(OP)
@commands('kickban', 'kb')
@priority('high')
def kickban(bot, trigger):
Expand All @@ -265,8 +257,6 @@ def kickban(bot, trigger):
The bot must be a Channel Operator for this command to work.
.kickban [#chan] user1 user!*@* get out of here
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < HALFOP:
return bot.reply("I'm not a channel operator!")
text = trigger.group().split()
Expand All @@ -292,15 +282,14 @@ def kickban(bot, trigger):
bot.write(['KICK', channel, nick, ' :', reason])


@require_privilege(OP)
@commands('topic')
def topic(bot, trigger):
"""
This gives ops the ability to change the topic.
The bot must be a Channel Operator for this command to work.
"""
purple, green, bold = '\x0306', '\x0310', '\x02'
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
if bot.privileges[trigger.sender][bot.nick] < HALFOP:
return bot.reply("I'm not a channel operator!")
if not trigger.group(2):
Expand Down Expand Up @@ -328,23 +317,21 @@ def topic(bot, trigger):
bot.write(('TOPIC', channel + ' :' + topic))


@require_privilege(OP)
@commands('tmask')
def set_mask(bot, trigger):
"""
Set the mask to use for .topic in the current channel. {} is used to allow
substituting in chunks of text.
"""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
bot.db.set_channel_value(trigger.sender, 'topic_mask', trigger.group(2))
bot.say("Gotcha, " + trigger.nick)


@require_privilege(OP)
@commands('showmask')
def show_mask(bot, trigger):
"""Show the topic mask for the current channel."""
if bot.privileges[trigger.sender][trigger.nick] < OP:
return
mask = bot.db.get_channel_value(trigger.sender, 'topic_mask')
mask = mask or default_mask(trigger)
bot.say(mask)

0 comments on commit 3689916

Please sign in to comment.