From: Andreas Scherbaum Date: Fri, 20 Jan 2012 14:44:27 +0000 (+0100) Subject: - track channels for each session X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=2f5478137d8cbf0a99487aac0163aa69b6f89fa5;p=docbot.git - track channels for each session - remove unnesessary channel join code, directly use configured channel list - add list of channels for session 1 --- diff --git a/docbot.conf b/docbot.conf index 722632f..57855ee 100644 --- a/docbot.conf +++ b/docbot.conf @@ -16,6 +16,14 @@ channels: password: '' session: 3 language: 'en' + '#pg_docbot_test4': + password: '' + session: 1 + language: 'en' + '#pg_docbot_test5': + password: '' + session: 1 + language: 'en' database: host: 127.0.0.1 name: docbot diff --git a/docbot.pl b/docbot.pl index e36734d..9c5600c 100755 --- a/docbot.pl +++ b/docbot.pl @@ -163,6 +163,9 @@ foreach my $session (keys(%main::sessions)) { 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, @@ -174,9 +177,7 @@ foreach my $session (keys(%main::sessions)) { # 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, @@ -630,6 +631,7 @@ sub init_terminal { 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 @@ -639,27 +641,11 @@ sub init_sessions { 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); - } } @@ -725,7 +711,12 @@ sub watchdog { 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 @@ -1222,9 +1213,17 @@ sub on_connect { $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"; @@ -1232,7 +1231,8 @@ sub on_connect { 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 ); } @@ -1252,7 +1252,7 @@ sub on_connect { # 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; @@ -1436,7 +1436,7 @@ sub on_message { # 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(); @@ -1455,7 +1455,7 @@ sub on_whois_identified { # 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(); @@ -1473,7 +1473,7 @@ sub on_whois_end { # 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); @@ -1630,7 +1630,7 @@ sub on_nickused { 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 @@ -1667,7 +1667,7 @@ sub on_end_motd { 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; @@ -1684,7 +1684,7 @@ sub on_error { 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); @@ -1697,6 +1697,80 @@ sub on_error { } +# 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); +} + +