- command handling
authorAndreas Scherbaum <andreas@scherbaum.biz>
Sat, 21 Jan 2012 15:47:19 +0000 (16:47 +0100)
committerAndreas Scherbaum <andreas@scherbaum.biz>
Sat, 21 Jan 2012 15:47:19 +0000 (16:47 +0100)
- implement basic authentication for operator and admin commands

docbot.conf
docbot.pl

index 57855ee04abea6f040853bfa2eeef4c5145bec87..b33ea208c1f82b5b9ffeef69956e99306733952f 100644 (file)
@@ -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'
index b3ef50fa2c0165b414b0e5880c1d412305737c25..8664a8668f85e9aa36f2b527e8006607ffc4c967 100755 (executable)
--- 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;
         }
+
     }