irc_433 => \&on_nickused,
irc_330 => \&on_whois_identified,
irc_318 => \&on_whois_end,
+ irc_join => \&on_join,
+ irc_part => \&on_part,
+ irc_quit => \&on_quit,
irc_ping => \&on_ping,
autoping => \&do_autoping,
irc_error => \&on_error,
# inline_states => {
# irc_353 => \&on_names,
-# irc_join => \&on_join,
# irc_part => \&on_part,
-# irc_quit => \&on_quit,
# irc_nick => \&on_nick,
# irc_330 => \&on_whois_identified,
# irc_318 => \&on_whois_end,
sub init_sessions {
my @sessions = config_get_keys1('sessions');
+ my @channels = config_get_keys1('channels');
# validate_config() already made sure, that the session names are integers
my $password = config_get_key3('sessions', $session, 'password');
$main::sessions{$session}{'nickname'} = $nickname;
$main::sessions{$session}{'password'} = $password;
- $main::sessions{$session}{'irc_channels'} = [];
+ $main::sessions{$session}{'joined_channels'} = [];
# for the watchdog
stop_session_activity($session);
$main::sessions{$session}{'last_nick_change_attempt'} = time();
}
-
- # FIXME: use channels from config directly on join
- sort_irc_channels_into_sessions();
-}
-
-
-sub sort_irc_channels_into_sessions {
- my @channels = config_get_keys1('channels');
-
- foreach my $channel (@channels) {
- # FIXME: find out if this channel is already joined
- # FIXME: if session changes, reassign the channel
- my $session = config_get_key3('channels', $channel, 'session');
- push(@{$main::sessions{$session}{'irc_channels'}}, $channel);
- print_msg("assign irc channel '$channel' to session '$session'", DEBUG);
- }
}
foreach my $session (keys(%main::sessions)) {
if (defined(read_session_activity($session))) {
- if (read_session_activity($session) < (time() - 180) and read_session_activity($session) < (time() > 240)) {
+ if (read_session_activity($session) < (time() - 30) and read_session_activity($session) < (time() - 45)) {
+ my $irc = $main::sessions{$session}{'session'};
+ $irc->yield( part => '#pg_docbot' );
+ }
+
+ if (read_session_activity($session) < (time() - 180) and read_session_activity($session) < (time() - 240)) {
print_msg("Session $session timed out", INFO);
# FIXME: what to do?
# automatic reconnects should be done by a plugin
$main::sessions{$session}{'past_motd'} = 0;
$main::sessions{$session}{'last_nick_change_attempt'} = time();
- # get all channels for this session
- my @irc_channels = @{$main::sessions{$session}{'irc_channels'}};
- print_msg("Channel list for session $session: " . join(", ", @irc_channels), DEBUG);
+ # get all channels for this session from config
+ my @channels = config_get_keys1('channels');
+ my @join_channels = ();
+ foreach my $channel (@channels) {
+ my $channel_session = config_get_key3('channels', $channel, 'session');
+ if ($channel_session == $session) {
+ push(@join_channels, $channel);
+ }
+ print_msg("assign irc channel '$channel' to session '$session'", DEBUG);
+ }
+ print_msg("Channel list for session $session: " . join(", ", @join_channels), DEBUG);
print "Session $session connected to " . $irc->server_name() . "\n";
set_session_activity($session);
# join all channels
- foreach my $channel (@irc_channels) {
+ $main::sessions{$session}{'joined_channels'} = [];
+ foreach my $channel (@join_channels) {
# based on the current configuration, each channel can only be joined by one bot session
$irc->yield( join => $channel );
}
# called when some message was sent to channel or to bot
#
sub on_message {
- my ( $kernel, $heap, $who, $where, $msg, $sender ) = @_[ KERNEL, HEAP, ARG0, ARG1, ARG2, SENDER ];
+ my ($kernel, $heap, $who, $where, $msg, $sender) = @_[KERNEL, HEAP, ARG0, ARG1, ARG2, SENDER];
my $nick = ( split /!/, $who )[0];
my $channel = $where->[0];
my $replyto = $channel;
# parse whois lines
#
sub on_whois_identified {
- my ( $kernel, $heap, $detail, $sender ) = @_[KERNEL, HEAP, ARG1, SENDER];
+ my ($kernel, $heap, $detail, $sender) = @_[KERNEL, HEAP, ARG1, SENDER];
my $nick = ( split / /, $detail )[0];
my $irc = $sender->get_heap();
# end of whois output
#
sub on_whois_end {
- my ( $kernel, $heap, $detail, $sender ) = @_[KERNEL, HEAP, ARG1, SENDER];
+ my ($kernel, $heap, $detail, $sender) = @_[KERNEL, HEAP, ARG1, SENDER];
my $nick = ( split / /, $detail )[0];
my $irc = $sender->get_heap();
# catch the ping and update activity
#
sub on_ping {
- my ($kernel, $heap, $sender ) = @_[ KERNEL, HEAP, SENDER ];
+ my ($kernel, $heap, $sender ) = @_[KERNEL, HEAP, SENDER];
my $irc = $sender->get_heap();
my $session = find_irc_session($irc);
my $irc = $heap->{irc};
my $session = find_irc_session($irc);
- print_msg("on_nickused(session: $session)\n", DEBUG);
+ print_msg("on_nickused(session: $session)", DEBUG);
# extract the nickname from the error message
my $irc = $heap->{irc};
my $session = find_irc_session($irc);
- print_msg("on_end_motd(session: $session)\n", DEBUG);
+ print_msg("on_end_motd(session: $session)", DEBUG);
$main::sessions{$session}{'past_motd'} = 1;
my $irc = $sender->get_heap();
my $session = find_irc_session($irc);
- print_msg("on_error(session: $session, error: \"" . $text . "\")\n", DEBUG);
+ print_msg("on_error(session: $session, error: \"" . $text . "\")", DEBUG);
if ($shutdown == 1) {
print_msg("Shutting down in on_error(session: $session)", INFO);
}
+# on_join()
+#
+# handle channel join events
+#
+sub on_join {
+ my ($sender, $kernel, $heap, $who, $channel) = @_[SENDER, KERNEL, HEAP, ARG0, ARG1];
+ my $nick = ( split /!/, $who )[0];
+
+ my $irc = $sender->get_heap();
+ my $session = find_irc_session($irc);
+
+ print_msg("on_join(session: $session, nick: $nick, channel: $channel)", DEBUG);
+
+ if ($irc->nick_name() eq $nick) {
+ print_msg("I just joined channel $channel (session: $session)", DEBUG);
+ # add this list to the list of session channels
+ push(@{$main::sessions{$session}{'joined_channels'}}, $channel);
+ }
+
+ set_session_activity($session);
+}
+
+
+# on_part()
+#
+# handle channel part events
+#
+sub on_part {
+ my ($sender, $kernel, $heap, $who, $channel) = @_[SENDER, KERNEL, HEAP, ARG0, ARG1];
+ my $nick = ( split /!/, $who )[0];
+
+ my $irc = $sender->get_heap();
+ my $session = find_irc_session($irc);
+
+ print_msg("on_part(session: $session, nick: $nick, channel: $channel)", DEBUG);
+
+ if ($irc->nick_name() eq $nick) {
+ print_msg("I just left channel $channel (session: $session)", DEBUG);
+ # remove this channel from the list of session channels
+ my @new_channels = @{$main::sessions{$session}{'joined_channels'}};
+ $main::sessions{$session}{'joined_channels'} = [];
+ foreach my $temp_channel (@new_channels) {
+ if (lc($temp_channel) ne lc($channel)) {
+ push(@{$main::sessions{$session}{'joined_channels'}}, $temp_channel);
+ }
+ }
+ }
+
+ set_session_activity($session);
+}
+
+
+# on_quit()
+#
+# handle quit events
+#
+sub on_quit {
+ my ($sender, $kernel, $heap, $who) = @_[SENDER, KERNEL, HEAP, ARG0];
+ my $nick = ( split /!/, $who )[0];
+
+ my $irc = $sender->get_heap();
+ my $session = find_irc_session($irc);
+ print_msg("on_quit(session: $session, nick: $nick)", DEBUG);
+
+ if ($irc->nick_name() eq $nick) {
+ print_msg("I quit the IRC (session: $session)", INFO);
+ # clean the session channel list
+ $main::sessions{$session}{'joined_channels'} = [];
+ }
+
+ set_session_activity($session);
+}
+
+