From aaa8c75746410b5951b11e93479cdc8b6c702727 Mon Sep 17 00:00:00 2001 From: Andreas Scherbaum Date: Sat, 21 Jan 2012 16:47:19 +0100 Subject: [PATCH] - command handling - implement basic authentication for operator and admin commands --- docbot.conf | 1 + docbot.pl | 134 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 108 insertions(+), 27 deletions(-) diff --git a/docbot.conf b/docbot.conf index 57855ee..b33ea20 100644 --- a/docbot.conf +++ b/docbot.conf @@ -75,6 +75,7 @@ translations: nothing_found: 'Nichts gefunden' additional_information_at: 'Weitere Informationen unter' you_are_not_authorized: 'Sie sind nicht authorisiert' + access_denied: 'Zugriff verweigert' fr: learn: 'apprendre' forget: 'oublier' diff --git a/docbot.pl b/docbot.pl index b3ef50f..8664a86 100755 --- a/docbot.pl +++ b/docbot.pl @@ -957,9 +957,9 @@ sub find_command { my ($command, $string); - if ($msg =~ /^\s*\?([a-z]+)\s+(.+)/) { + if ($msg =~ /^\s*\?([a-z]+)\s*(.*)/) { $command = lc($1); - $string = $2; + $string = defined($2) ? $2 : ''; # looks like a command, at least started with a question mark # find out if it really is one @@ -980,13 +980,18 @@ sub find_command { } } + if ($msg =~ /^\s*\?\?(.+)/) { + # a valid search + return ('search', $1); + } + return undef; } # is_valid_command() # -# find out if this is a valid command (includes valid admin commands) +# find out if this is a valid command (includes valid admin and operator commands) # # parameter: # - command @@ -995,7 +1000,13 @@ sub find_command { sub is_valid_command { my $command = shift; - my $status = is_valid_admin_command($command); + my $status; + + $status = is_valid_operator_command($command); + if ($status == 1) { + return 1; + } + $status = is_valid_admin_command($command); if ($status == 1) { return 1; } @@ -1004,28 +1015,49 @@ sub is_valid_command { return 1; } elsif ($command eq 'info') { return 1; + } elsif ($command eq 'search') { + return 1; } return 0; } -# is_valid_admin_command() +# is_valid_operator_command() # -# find out if this is a valid admin command +# find out if this is a valid operator command # # parameter: # - command # return: # - 0/1 -sub is_valid_admin_command { +sub is_valid_operator_command { my $command = shift; if ($command eq 'learn') { return 1; } elsif ($command eq 'forget') { return 1; - } elsif ($command eq 'config') { + } + + return 0; +} + + +# is_valid_admin_command() +# +# find out if this is a valid admin command +# +# parameter: +# - command +# return: +# - 0/1 +sub is_valid_admin_command { + my $command = shift; + + if ($command eq 'config') { + return 1; + } elsif ($command eq 'status') { return 1; } @@ -1191,6 +1223,29 @@ sub translate_text_for_channel { } +# FIXME: implement this function +sub is_nick_allowed_admin_command { + my $nick = shift; + + if ($nick eq 'ads2') { + return 1; + } + + return 0; +} + + +# FIXME: implement this function +sub is_nick_allowed_operator_command { + my $nick = shift; + + if ($nick eq 'ads') { + return 1; + } + + return 0; +} + @@ -1304,10 +1359,10 @@ sub on_message { if (defined($command)) { - my $answer; + my $answer = ''; - # handle all admin commands - if (is_valid_admin_command($command)) { + # handle authentication commands + if (is_valid_admin_command($command) or is_valid_operator_command($command)) { # no authentication information available, create callback if (!defined($heap->{whois_callback}->{$nick}->{authed})) { @@ -1328,30 +1383,55 @@ sub on_message { # translate error message $answer = translate_text_for_channel($channel, 'you_are_not_authorized', $answer); } - else { - # execute desired command - #$answer = $admin_commands->{$command}($kernel, $nick, $channel, $string); - $answer = "Execute command: $command"; - print_msg("Execute command: $command", INFO); - } - # drop the callback for this nick - undef ($heap->{whois_callback}->{$nick}); - - if (length($answer)) { - # if command was called in channel print answer to channel, if it was PM print it as PM - if (lc($channel) eq lc($irc->nick_name())) { - $irc->yield( privmsg => $nick, $answer); + elsif (is_valid_admin_command($command)) { + if (is_nick_allowed_admin_command($nick)) { + # execute desired command + #$answer = $admin_commands->{$command}($kernel, $nick, $channel, $string); + $answer = "Execute command: $command"; + print_msg("Execute command: $command", INFO); + } else { + # user is not allowed to execute admin commands + $answer = "Access denied"; + # translate error message + $answer = translate_text_for_channel($channel, 'access_denied', $answer); } - else { - $irc->yield( privmsg => $channel, $answer); + } + elsif (is_valid_operator_command($command)) { + if (is_nick_allowed_operator_command($nick)) { + # execute desired command + #$answer = $admin_commands->{$command}($kernel, $nick, $channel, $string); + $answer = "Execute command: $command"; + print_msg("Execute command: $command", INFO); + } else { + # user is not allowed to execute admin commands + $answer = "Access denied"; + # translate error message + $answer = translate_text_for_channel($channel, 'access_denied', $answer); } - return; } + # drop the callback for this nick + undef ($heap->{whois_callback}->{$nick}); + } else { + # execute desired command + #$answer = $admin_commands->{$command}($kernel, $nick, $channel, $string); + $answer = "Execute command: $command"; + print_msg("Execute command: $command", INFO); + } + if (length($answer)) { + # if command was called in channel print answer to channel, if it was PM print it as PM + if (lc($channel) eq lc($irc->nick_name())) { + $irc->yield( privmsg => $nick, $answer); + } + else { + $irc->yield( privmsg => $channel, $answer); + } + return; } + } -- 2.39.5