Skip to content

Instantly share code, notes, and snippets.

@hinrik
Created May 20, 2010 15:27
Show Gist options
  • Save hinrik/407694 to your computer and use it in GitHub Desktop.
Save hinrik/407694 to your computer and use it in GitHub Desktop.
From 28e984dbb1ad6f47e419414d07b298b97c022d07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hinrik=20=C3=96rn=20Sigur=C3=B0sson?= <[email protected]>
Date: Thu, 20 May 2010 15:25:18 +0000
Subject: [PATCH] Support a user-supplied authorization sub for commands
---
Changes | 1 +
lib/POE/Component/IRC/Plugin/BotCommand.pm | 31 ++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 0 deletions(-)
diff --git a/Changes b/Changes
index 3d8285a..88e7a80 100644
--- a/Changes
+++ b/Changes
@@ -4,6 +4,7 @@ Revision history for Perl extension POE::Component::IRC.
- BotCommand.pm: Allow user to choose how help messages are delivered
- BotCommand.pm: Require the command prefix in private messages
- BotCommand.pm: Make the help messages more context-sensitive
+ - BotCommand.pm: Add support for custom auth checks
6.32 Tue May 11 13:43:50 GMT 2010
- IRC.pm: Filter out \r in arguments to non-PRIVMSG commands too
diff --git a/lib/POE/Component/IRC/Plugin/BotCommand.pm b/lib/POE/Component/IRC/Plugin/BotCommand.pm
index cc63533..bcf6030 100644
--- a/lib/POE/Component/IRC/Plugin/BotCommand.pm
+++ b/lib/POE/Component/IRC/Plugin/BotCommand.pm
@@ -104,6 +104,20 @@ sub _handle_cmd {
my $public = $where =~ /^[$chantypes]/ ? 1 : 0;
$cmd = lc $cmd;
+ if (ref $self->{Auth_sub} eq 'CODE') {
+ my ($authed, @errors) = $self->{Auth_sub}->($self->{irc}, $who, $where, $cmd, $args);
+
+ if (!$authed) {
+ if (!$self->{Ignore_unauthorized}) {
+ @errors = 'You are not authorized to use this command.' if !@errors;
+ for my $error (@errors) {
+ $irc->yield($self->{Method}, $where, $error);
+ }
+ }
+ return;
+ }
+ }
+
if (defined $self->{Commands}->{$cmd}) {
$irc->send_event("irc_botcmd_$cmd" => $who, $where, $args);
}
@@ -297,6 +311,23 @@ B<'Prefix'>, a string which all commands must be prefixed with (except in
channels when B<'Addressed'> is true). Default is '!'. You can set it to ''
to allow bare commands.
+=head3 Authorization
+
+B<'Auth_sub'>, a subroutine reference which, if provided, will be called
+for every command. The subroutine will be called in list context. If the
+first value returned is true the command will be processed as normal. If the
+value is false, then no events will be generated, and an error message will
+possibly be sent back to the user. You can override the default error message
+by returning more values. Each string will be sent as a message to the user.
+The sub will get the following arguments: The IRC component object, the
+nick!user@host of the user, the place where the command was issued (the
+nickname of the user if it was in private), the name of the command, and the
+arguments to it.
+
+B<'Ignore_unauthorized'>, if true, the plugin will ignore unauthorized
+commands, rather than printing an error message upon receiving them. This is
+only relevant if B<'Auth_sub'> is also supplied. Default is false.
+
=head3 Miscellaneous
B<'Ignore_unknown'>, if true, the plugin will ignore undefined commands,
--
1.7.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment