- add "lost" command
authorAndreas Scherbaum <andreas@scherbaum.biz>
Wed, 13 Jun 2012 16:19:23 +0000 (18:19 +0200)
committerAndreas Scherbaum <andreas@scherbaum.biz>
Wed, 13 Jun 2012 16:19:23 +0000 (18:19 +0200)
docbot.conf
docbot.pl

index 0a5c940b8a423910bb032c72669ed712d1b52568..ed2adb34a698dbf5209ed8a81e8604739f65b093 100644 (file)
@@ -95,6 +95,7 @@ translations:
     help_general_line_2: 'Starte eine Suche mit zwei Fragezeichen, danach der Suchbegriff'
     help_general_line_3: 'Die folgenden Befehle stehen außerdem zur Verfügung'
     help_general_line_say: 'Nutze: ?sage #channel Nachricht'
+    help_general_line_lost: 'Nutze: ?lost'
     search_bad_parameters: 'Falsche Parameter'
     search_no_new_keywords: 'Alle Schlüsselwörter existieren bereits in der Datenbank'
     search_add_1_keyword: '1 Schlüsselwort erfolgreich hinzugefügt'
index 91f268dea1371b859d3f7e90f75d1538d6049e4d..2c3a3b1b524fe710a5841c7f65eae63c2c8fc76e 100755 (executable)
--- a/docbot.pl
+++ b/docbot.pl
@@ -314,6 +314,7 @@ sub init_statistics {
     $main::statistics{'command_counter_join'} = 0;
     $main::statistics{'command_counter_leave'} = 0;
     $main::statistics{'command_access_denied'} = 0;
+    $main::statistics{'command_access_lost'} = 0;
 
     $main::statistics{'database_connects'} = 0;
     $main::statistics{'database_queries'} = 0;
@@ -1271,6 +1272,8 @@ sub is_valid_admin_command {
         return 1;
     } elsif ($command eq 'leave') {
         return 1;
+    } elsif ($command eq 'lost') {
+        return 1;
     }
 
     return 0;
@@ -1820,6 +1823,10 @@ sub handle_command {
         case('leave') {
             return handle_command_leave($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel);
         }
+        case('lost') {
+            $main::statistics{'command_counter_lost'}++;
+            return handle_command_lost($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel);
+        }
     }
 
 
@@ -1904,6 +1911,105 @@ sub handle_command_status {
 }
 
 
+# handle_command_lost()
+#
+# command handler for the 'lost' command
+#
+# parameter:
+#  - the command (lower case)
+#  - the parameter string (may be empty)
+#  - the command mode (admin/operator/user)
+#  - POE kernel
+#  - POE heap
+#  - the full who of the message sender, including the nick name
+#  - the nick name of the message sender
+#  - the full origin of the message
+#  - the message itself
+#  - POE sender
+#  - session irc handle
+#  - the channel name
+# return:
+#  - text to send back to the sender
+sub handle_command_lost {
+    my $command = shift;
+    my $string = shift;
+    my $mode = shift;
+    my $kernel = shift;
+    my $heap = shift;
+    my $who = shift;
+    my $nick = shift;
+    my $where = shift;
+    my $msg = shift;
+    my $sender = shift;
+    my $irc = shift;
+    my $channel = shift;
+
+
+    # 'lost' goes to the command channel only
+    if (lc($channel) eq lc($irc->nick_name())) {
+        return 'The "lost" command is only allowed in the command channel';
+    }
+    if (lc($channel) ne lc(config_get_key2('bot', 'commandchannel'))) {
+        return 'The "lost" command is only allowed in the command channel';
+    }
+
+
+    my $query = "SELECT url FROM docbot_url WHERE id NOT IN (SELECT kurl FROM docbot_key) ORDER BY id";
+    my $st = $main::db->query($query);
+    if (!defined($st)) {
+        my $answer = "Database error";
+        # translate error message
+        $answer = translate_text_for_channel($channel, 'database_error', $answer);
+        return $answer;
+    }
+    my $rows = $st->rows;
+    if ($rows == 0) {
+        my $answer = "No unconnected urls in database";
+        return $answer;
+    }
+
+
+    # fetch the result
+    my @rows = ();
+    while (my @row = $st->fetchrow_array) {
+        push(@rows, $row[0]);
+    }
+
+
+    my @lines = ();
+    my $maxresults = config_get_key2('search', 'maxresults');
+    if ($maxresults == 0) {
+        # set a reasonable high default
+        $maxresults = 50;
+    }
+    if ($maxresults < 20) {
+        $maxresults = 20;
+    }
+    my $maxwrap = config_get_key2('search', 'maxwrap');
+    for (my $a = 1; $a <= int($maxresults / $maxwrap); $a++) {
+        my @line = ();
+        for (my $b = 1; $b <= $maxwrap; $b++) {
+            if (defined($rows[0])) {
+                push(@line, shift(@rows));
+            }
+        }
+        if (scalar(@line) > 0) {
+            push(@lines, join(" :: ", @line));
+        }
+    }
+
+    if (scalar(@lines) > 0) {
+        $irc->yield( privmsg => $channel, "$rows unconnected urls in database:" );
+        foreach my $line (@lines) {
+            $irc->yield( privmsg => $channel, $line );
+        }
+    }
+
+
+    return '';
+}
+
+
 # handle_command_wallchan()
 #
 # command handler for the 'wallchan' command
@@ -2463,6 +2569,13 @@ sub handle_command_help {
         $irc->yield( privmsg => $replyto, $answer );
     }
 
+    if ($string eq 'lost') {
+        my $answer = "Use ?lost";
+        # translate message
+        $answer = translate_text_for_channel($replyto, 'help_general_line_lost', $answer);
+        $irc->yield( privmsg => $replyto, $answer );
+    }
+
     return '';
 }