- add function to write to a channel
authorAndreas Scherbaum <[email protected]>
Fri, 20 Jan 2012 21:23:03 +0000 (22:23 +0100)
committerAndreas Scherbaum <[email protected]>
Fri, 20 Jan 2012 21:23:03 +0000 (22:23 +0100)
- add function to write to the command channel
- add function to identify the session for a joined channel

docbot.pl

index 973ca24a362a5691693da1bb22b86f3230ba74b6..423ecfeb86a59c52873d7104f0dce3610d267f6b 100755 (executable)
--- a/docbot.pl
+++ b/docbot.pl
@@ -649,8 +649,25 @@ sub init_sessions {
 }
 
 
-sub write_to_channel {
+# send_to_channel()
+#
+# send a message to a channel
+#
+# parameter:
+#  - message
+# return:
+#  none
+sub send_to_channel {
+    my $channel = shift;
+    my $msg = shift;
+    $msg =~ s/\n//g;
 
+    my $session = session_for_channel($channel);
+
+    if (defined($session)) {
+        my $irc = $main::sessions{$session}{'session'};
+        $irc->yield( privmsg => $channel, $msg );
+    }
 }
 
 
@@ -747,12 +764,36 @@ sub watchdog {
 # return:
 #  none
 sub send_to_commandchannel {
+    my $msg = shift;
+    $msg =~ s/\n//g;
 
-# FIXME: check if channel is joined
+    my $commandchannel = config_get_key2('bot', 'commandchannel');
 
+    print_msg("commandchannel: $msg", DEBUG);
+    send_to_channel(config_get_key2('bot', 'commandchannel'), $msg)
+}
+
+
+# session_for_channel()
+#
+# find a session for a specific channel
+#
+# parameter:
+#  - channel name
+# return:
+#  - session number, or undef
+sub session_for_channel {
+    my $channel = shift;
 
-# http://search.cpan.org/dist/POE-Component-IRC/lib/POE/Component/IRC.pm#connected
+    foreach my $session (keys(%main::sessions)) {
+        if (grep {lc($_) eq lc($channel)} @{$main::sessions{$session}{'joined_channels'}}) {
+            # so far the first session is used:
+            # based on the current configuration method, each channel can only be assigned to one session
+            return $session;
+        }
+    }
 
+    return undef;
 }