From 2b047862eb2caf8438dc5289f72e9eae5e6bda0c Mon Sep 17 00:00:00 2001 From: Andreas Scherbaum Date: Mon, 25 Jun 2012 23:28:09 +0200 Subject: [PATCH] - implement "key" and "url" commands --- docbot.conf | 7 ++ docbot.pl | 268 ++++++++++++++++++++++++++++++++++++++++++++++++++++ todo.txt | 4 +- 3 files changed, 277 insertions(+), 2 deletions(-) diff --git a/docbot.conf b/docbot.conf index 4472a07..ed25758 100644 --- a/docbot.conf +++ b/docbot.conf @@ -62,6 +62,7 @@ translations: help: 'hilfe' search: 'suche' say: 'sage' + key: 'Schlüsselwort' successfully_added_1_keyword: 'Ein Schlüsselwort hinzugefügt' successfully_added_1_url: 'Eine URL hinzugefügt' successfully_added_x_keyword: '% Schlüsselwörter hinzugefügt' @@ -84,6 +85,8 @@ translations: more_result: 'weiteres Ergebnis' more_results: 'weitere Ergebnisse' error_wallchan_command_parameter: 'Der "wallchan" Befehl erfordert einen Parameter' + error_url_command_parameter: 'Der "url" Befehl erfordert einen Parameter' + error_key_command_parameter: 'Der "key" Befehl erfordert einen Parameter' wallchan_command_message: 'Nachricht vom Operator' error_say_command_parameter: 'Der "say" Befehl erfordert zwei Parameter' error_say_not_joined: 'Der Bot ist nicht in diesem Channel' @@ -96,7 +99,11 @@ translations: 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' + help_general_line_url: 'Nutze: ?url ' + help_general_line_key: 'Nutze: ?key ' lost_only_in_commandchannel: 'Der "lost" Befehl ist nur im Adminchannel erlaubt' + url_only_in_commandchannel: 'Der "url" Befehl ist nur im Adminchannel erlaubt' + key_only_in_commandchannel: 'Der "key" Befehl ist nur im Adminchannel erlaubt' 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' diff --git a/docbot.pl b/docbot.pl index 1f35bfb..13b9a10 100755 --- a/docbot.pl +++ b/docbot.pl @@ -328,6 +328,8 @@ sub init_statistics { $main::statistics{'command_counter_leave'} = 0; $main::statistics{'command_access_denied'} = 0; $main::statistics{'command_access_lost'} = 0; + $main::statistics{'command_access_url'} = 0; + $main::statistics{'command_access_key'} = 0; $main::statistics{'database_connects'} = 0; $main::statistics{'database_queries'} = 0; @@ -1323,6 +1325,10 @@ sub is_valid_operator_command { return 1; } elsif ($command eq 'forget') { return 1; + } elsif ($command eq 'url') { + return 1; + } elsif ($command eq 'key') { + return 1; } return 0; @@ -1907,6 +1913,12 @@ sub handle_command { $main::statistics{'command_counter_lost'}++; return handle_command_lost($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel); } + case('url') { + return handle_command_url($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel); + } + case('key') { + return handle_command_key($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel); + } } @@ -1981,6 +1993,8 @@ sub handle_command_status { push(@commands, 'leave: ' . $main::statistics{'command_counter_leave'}); push(@commands, 'status: ' . $main::statistics{'command_counter_status'}); push(@commands, 'lost: ' . $main::statistics{'command_counter_lost'}); + push(@commands, 'url: ' . $main::statistics{'command_counter_url'}); + push(@commands, 'key: ' . $main::statistics{'command_counter_key'}); $irc->yield( privmsg => $channel, 'Number of executed IRC commands: ' . join(", ", @commands) ); $irc->yield( privmsg => $channel, 'Number of denied IRC requests: ' . $main::statistics{'command_access_denied'} ); @@ -2097,6 +2111,246 @@ sub handle_command_lost { } +# handle_command_url() +# +# command handler for the 'url' 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_url { + 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; + + + # 'url' goes to the command channel only + if (lc($channel) eq lc($irc->nick_name())) { + my $answer = 'The "url" command is only allowed in the command channel'; + # translate error message + $answer = translate_text_for_channel($channel, 'url_only_in_commandchannel', $answer); + return $answer; + } + if (lc($channel) ne lc(config_get_key2('bot', 'commandchannel'))) { + my $answer = 'The "url" command is only allowed in the command channel'; + # translate error message + $answer = translate_text_for_channel($channel, 'url_only_in_commandchannel', $answer); + return $answer; + } + + + if (length($string) < 1) { + my $answer = 'The "url" command requires a parameter'; + $answer = translate_text_for_channel($channel, 'error_url_command_parameter', $answer); + return $answer; + } + + print_msg("url: '$string', by $nick", DEBUG); + send_to_commandchannel("url: '$string', by $nick"); + $main::statistics{'command_counter_url'}++; + + + my $query = "SELECT key FROM docbot_key WHERE kurl IN (SELECT id FROM docbot_url WHERE url = ?::TEXT) ORDER BY key"; + my $st = $main::db->query($query, $string); + 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 = "URL not in database or no keys connected"; + 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'); + if ($maxwrap > 1 and $maxwrap < 5) { + # set a more reasonable default for number of keys + $maxwrap = 5; + } + 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 keys in database for url '$string':" ); + foreach my $line (@lines) { + $irc->yield( privmsg => $channel, $line ); + } + } + + + return ''; +} + + +# handle_command_key() +# +# command handler for the 'key' 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_key { + 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; + + + # 'key' goes to the command channel only + if (lc($channel) eq lc($irc->nick_name())) { + my $answer = 'The "key" command is only allowed in the command channel'; + # translate error message + $answer = translate_text_for_channel($channel, 'key_only_in_commandchannel', $answer); + return $answer; + } + if (lc($channel) ne lc(config_get_key2('bot', 'commandchannel'))) { + my $answer = 'The "key" command is only allowed in the command channel'; + # translate error message + $answer = translate_text_for_channel($channel, 'key_only_in_commandchannel', $answer); + return $answer; + } + + + if (length($string) < 1) { + my $answer = 'The "key" command requires a parameter'; + $answer = translate_text_for_channel($channel, 'error_key_command_parameter', $answer); + return $answer; + } + + print_msg("key: '$string', by $nick", DEBUG); + send_to_commandchannel("key: '$string', by $nick"); + $main::statistics{'command_counter_key'}++; + + + my $query = "SELECT url FROM docbot_url WHERE id IN (SELECT kurl FROM docbot_key WHERE key = ?::TEXT) ORDER BY url"; + my $st = $main::db->query($query, $string); + 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 = "Key not in database or no urls connected"; + 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'); + if ($maxwrap > 1 and $maxwrap < 5) { + # set a more reasonable default for number of keys + $maxwrap = 5; + } + 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 urls in database for key '$string':" ); + foreach my $line (@lines) { + $irc->yield( privmsg => $channel, $line ); + } + } + + + return ''; +} + + # handle_command_wallchan() # # command handler for the 'wallchan' command @@ -2674,6 +2928,20 @@ sub handle_command_help { $irc->yield( privmsg => $replyto, $answer ); } + if ($string eq 'url') { + my $answer = "Use ?url "; + # translate message + $answer = translate_text_for_channel($replyto, 'help_general_line_url', $answer); + $irc->yield( privmsg => $replyto, $answer ); + } + + if ($string eq 'url') { + my $answer = "Use ?key "; + # translate message + $answer = translate_text_for_channel($replyto, 'help_general_line_key', $answer); + $irc->yield( privmsg => $replyto, $answer ); + } + return ''; } diff --git a/todo.txt b/todo.txt index 802aa3d..52b2b79 100644 --- a/todo.txt +++ b/todo.txt @@ -12,8 +12,8 @@ - track each channel message and see, if the bot was mentioned - Log::Log4perl? - catch 'irc_registered' OK -- add "key" command -- add "url" command +- add "key" command OK +- add "url" command OK - add "say" command OK - add "join" and "leave" command OK - add "lost" command OK -- 2.39.5