- implement "join" and "leave" commands
authorAndreas Scherbaum <andreas@scherbaum.biz>
Wed, 13 Jun 2012 12:09:57 +0000 (14:09 +0200)
committerAndreas Scherbaum <andreas@scherbaum.biz>
Wed, 13 Jun 2012 12:09:57 +0000 (14:09 +0200)
docbot.conf
docbot.pl

index 304c532deb3c1d22b7f34891bc857c6db1ca9bb2..c0caf702d4f5fc240ba13a7e03d889042ae3c1b0 100644 (file)
@@ -86,6 +86,10 @@ translations:
     error_wallchan_command_parameter: 'Der "wallchan" Befehl erfordert einen Parameter'
     wallchan_command_message: 'Nachricht vom Operator'
     error_say_command_parameter: 'Der "say" Befehl erfordert zwei Parameter'
+    error_join_command_parameter: 'Der "join" Befehl erfordert zwei Parameter'
+    error_join_already_joined: 'Der Bot ist bereits in diesem Channel'
+    error_leave_command_parameter: 'Der "leave" Befehl erfordert einen Parameter'
+    error_leave_not_joined: 'Der Bot ist nicht in diesem Channel'
     help_general_line_1: 'Allgemeine Hilfe'
     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'
index 41ea9f3201862452d59b146057d10bd50221da3c..5ec31586d21acb3e4b8535c59015577658af219b 100755 (executable)
--- a/docbot.pl
+++ b/docbot.pl
@@ -311,6 +311,8 @@ sub init_statistics {
     $main::statistics{'command_counter_status'} = 0;
     $main::statistics{'command_counter_wallchan'} = 0;
     $main::statistics{'command_counter_say'} = 0;
+    $main::statistics{'command_counter_join'} = 0;
+    $main::statistics{'command_counter_leave'} = 0;
     $main::statistics{'command_access_denied'} = 0;
 
     $main::statistics{'database_connects'} = 0;
@@ -1265,6 +1267,10 @@ sub is_valid_admin_command {
         return 1;
     } elsif ($command eq 'say') {
         return 1;
+    } elsif ($command eq 'join') {
+        return 1;
+    } elsif ($command eq 'leave') {
+        return 1;
     }
 
     return 0;
@@ -1809,6 +1815,12 @@ sub handle_command {
             $main::statistics{'command_counter_say'}++;
             return handle_command_say($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel);
         }
+        case('join') {
+            return handle_command_join($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel);
+        }
+        case('leave') {
+            return handle_command_leave($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel);
+        }
     }
 
 
@@ -1879,6 +1891,8 @@ sub handle_command_status {
     push(@commands, 'config: ' . $main::statistics{'command_counter_config'});
     push(@commands, 'wallchan: ' . $main::statistics{'command_counter_wallchan'});
     push(@commands, 'say: ' . $main::statistics{'command_counter_say'});
+    push(@commands, 'join: ' . $main::statistics{'command_counter_join'});
+    push(@commands, 'leave: ' . $main::statistics{'command_counter_leave'});
     push(@commands, 'status: ' . $main::statistics{'command_counter_status'});
     $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'} );
@@ -2023,6 +2037,175 @@ sub handle_command_say {
 }
 
 
+# handle_command_join()
+#
+# command handler for the 'join' 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_join {
+    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;
+
+
+    if (length($string) < 1) {
+        my $answer = 'The "join" command requires two parameters';
+        $answer = translate_text_for_channel($channel, 'error_join_command_parameter', $answer);
+        return $answer;
+    }
+
+
+    # remove spaces at beginning and end
+    $string =~ s/^[\s\t]+//gs;
+    $string =~ s/[\s\t]+$//gs;
+
+
+    my ($join_channel, $join_session);
+    if ($string =~ /^([^\s]+)\s+(\d+)$/) {
+        $join_channel = $1;
+        $join_session = $2;
+    } else {
+        my $answer = 'The "join" command requires two parameters';
+        $answer = translate_text_for_channel($channel, 'error_join_command_parameter', $answer);
+        return $answer;
+    }
+
+    if (!is_a_channel($join_channel)) {
+        my $answer = 'The "join" command requires two parameters';
+        $answer = translate_text_for_channel($channel, 'error_join_command_parameter', $answer);
+        return $answer;
+    }
+
+    if (!defined($main::sessions{$join_session})) {
+        my $answer = 'The "join" command requires two parameters';
+        $answer = translate_text_for_channel($channel, 'error_join_command_parameter', $answer);
+        return $answer;
+    }
+    my $join_irc = $main::sessions{$join_session}{'session'};
+
+
+    if (session_for_channel($join_channel)) {
+        my $answer = 'The bot already joined this channel';
+        $answer = translate_text_for_channel($channel, 'error_join_already_joined', $answer);
+        return $answer;
+    }
+
+
+    print_msg("join: '$join_channel' in session '$join_session', by $nick", DEBUG);
+    send_to_commandchannel("join: '$join_channel' in session '$join_session', by $nick");
+
+    $join_irc->yield( join => $join_channel );
+    $main::statistics{'command_counter_join'}++;
+
+
+    return '';
+}
+
+
+# handle_command_leave()
+#
+# command handler for the 'leave' 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_leave {
+    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;
+
+
+    if (length($string) < 1) {
+        my $answer = 'The "leave" command requires one parameter';
+        $answer = translate_text_for_channel($channel, 'error_leave_command_parameter', $answer);
+        return $answer;
+    }
+
+
+    # remove spaces at beginning and end
+    $string =~ s/^[\s\t]+//gs;
+    $string =~ s/[\s\t]+$//gs;
+
+
+    my ($leave_channel);
+    if ($string =~ /^([^\s]+)/) {
+        $leave_channel = $1;
+    } else {
+        my $answer = 'The "leave" command requires one parameter';
+        $answer = translate_text_for_channel($channel, 'error_leave_command_parameter', $answer);
+        return $answer;
+    }
+
+    if (!is_a_channel($leave_channel)) {
+        my $answer = 'The "leave" command requires one parameter';
+        $answer = translate_text_for_channel($channel, 'error_leave_command_parameter', $answer);
+        return $answer;
+    }
+    my $leave_session = session_for_channel($leave_channel);
+
+    if (!$leave_session) {
+        my $answer = 'The bot is not in this channel';
+        $answer = translate_text_for_channel($channel, 'error_leave_not_joined', $answer);
+        return $answer;
+    }
+    my $leave_irc = $main::sessions{$leave_session}{'session'};
+
+
+    print_msg("leave: '$leave_channel' in session '$leave_session', by $nick", DEBUG);
+    send_to_commandchannel("leave: '$leave_channel' in session '$leave_session', by $nick");
+
+    $leave_irc->yield( part => $leave_channel );
+    $main::statistics{'command_counter_leave'}++;
+
+
+    return '';
+}
+
+
 # handle_command_search()
 #
 # command handler for the 'search' command