- add "quit" command
authorAndreas Scherbaum <andreas@scherbaum.biz>
Thu, 2 Aug 2012 21:02:55 +0000 (23:02 +0200)
committerAndreas Scherbaum <andreas@scherbaum.biz>
Thu, 2 Aug 2012 21:02:55 +0000 (23:02 +0200)
docbot.conf
docbot.pl

index ed2575876dc6e97b26c482125753b75122675aef..0d684d3a44465714ee315bb3ec73050bca241583 100644 (file)
@@ -101,6 +101,7 @@ translations:
     help_general_line_lost: 'Nutze: ?lost'
     help_general_line_url: 'Nutze: ?url <URL>'
     help_general_line_key: 'Nutze: ?key <Schlüsselwort>'
+    help_general_line_quit: 'Nutze: ?quit'
     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'
index e63dd57f21449b20f1f959a275a5f89527ff3e88..33f1e7ab01c19fb4f0296bd21dd92743b3e5b6eb 100755 (executable)
--- a/docbot.pl
+++ b/docbot.pl
@@ -206,6 +206,7 @@ foreach my $session (keys(%main::sessions)) {
             irc_plugin_add   => \&on_irc_plugin_add,
             irc_raw          => \&on_irc_raw,
             irc_raw_out      => \&on_irc_raw_out,
+            execute_shutdown => \&execute_shutdown,
         },
         heap => { irc => $irc },
     );
@@ -282,6 +283,7 @@ POE::Session->create(
             # call the real maintenance function
             maintenance();
         },
+        execute_shutdown => \&execute_shutdown,
     },
 );
 
@@ -1172,7 +1174,9 @@ sub execute_shutdown {
 sub set_session_activity {
     my $session = shift;
 
-    $main::sessions{$session}{'last_activity'} = time();
+    if (defined($session)) {
+        $main::sessions{$session}{'last_activity'} = time();
+    }
 }
 
 
@@ -1359,6 +1363,8 @@ sub is_valid_admin_command {
         return 1;
     } elsif ($command eq 'lost') {
         return 1;
+    } elsif ($command eq 'quit') {
+        return 1;
     }
 
     return 0;
@@ -1918,6 +1924,9 @@ sub handle_command {
         when('key') {
             return handle_command_key($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel);
         }
+        when('quit') {
+            return handle_command_quit($command, $string, $mode, $kernel, $heap, $who, $nick, $where, $msg, $sender, $irc, $channel);
+        }
     }
 
 
@@ -1925,6 +1934,71 @@ sub handle_command {
 }
 
 
+# handle_command_quit()
+#
+# command handler for the 'quit' 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_quit {
+    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;
+
+
+    # 'quit' goes to the command channel only
+    if (lc($channel) eq lc($irc->nick_name())) {
+        return 'The "quit" command is only allowed in the command channel';
+    }
+    if (lc($channel) ne lc(config_get_key2('bot', 'commandchannel'))) {
+        return 'The "quit" command is only allowed in the command channel';
+    }
+
+
+    send_to_commandchannel('Received "quit" command from: ' . $nick);
+    print_msg('Received "quit" command from: ' . $nick, INFO);
+
+
+    # loop through all sessions and send a QUIT to the irc server
+    foreach my $tmp_session (keys(%main::sessions)) {
+        my $tmp_irc = $main::sessions{$tmp_session}{'session'};
+        if ($tmp_irc->connected) {
+            # this forces the current session to quit from irc, resulting in an "on_error" event
+            $tmp_irc->yield( quit => "Quit" );
+        }
+    }
+
+    # have to shutdown here, else Component::IRC Component::IRC::Plugin::Connector will reconnect
+    $poe_kernel->delay_add( execute_shutdown => 5 );
+    $shutdown = 1;
+
+
+    return '';
+}
+
+
 # handle_command_status()
 #
 # command handler for the 'status' command
@@ -2901,7 +2975,7 @@ sub handle_command_help {
         # translate message
         $answer = translate_text_for_channel($replyto, 'help_general_line_3', $answer);
         $answer .= ': ';
-        $answer .= 'search, help, info, learn, forget, config, status, say, wallchan, lost, url, key, join, leave';
+        $answer .= 'search, help, info, learn, forget, config, status, say, wallchan, lost, url, key, join, leave, quit';
         $irc->yield( privmsg => $replyto, $answer );
     }
 
@@ -2941,6 +3015,13 @@ sub handle_command_help {
         $irc->yield( privmsg => $replyto, $answer );
     }
 
+    if ($string eq 'quit') {
+        my $answer = "Use ?quit";
+        # translate message
+        $answer = translate_text_for_channel($replyto, 'help_general_line_quit', $answer);
+        $irc->yield( privmsg => $replyto, $answer );
+    }
+
     return '';
 }