/[irc-logger]/trunk/bin/irc-logger.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/irc-logger.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 118 by dpavlin, Wed Mar 12 18:21:03 2008 UTC revision 119 by dpavlin, Fri Mar 14 00:17:49 2008 UTC
# Line 18  use DateTime::Format::ISO8601; Line 18  use DateTime::Format::ISO8601;
18  use Carp qw/confess/;  use Carp qw/confess/;
19  use XML::Feed;  use XML::Feed;
20  use DateTime::Format::Flexible;  use DateTime::Format::Flexible;
21    use IPC::DirQueue;
22    use File::Slurp;
23    
24  =head1 NAME  =head1 NAME
25    
# Line 47  log all conversation on irc channel Line 49  log all conversation on irc channel
49    
50  ## CONFIG  ## CONFIG
51    
52    my $debug = 0;
53    
54  my $irc_config = {  my $irc_config = {
55          nick => 'irc-logger',          nick => 'irc-logger',
56          server => 'irc.freenode.net',          server => 'irc.freenode.net',
# Line 54  my $irc_config = { Line 58  my $irc_config = {
58          ircname => 'Anna the bot: try /msg irc-logger help',          ircname => 'Anna the bot: try /msg irc-logger help',
59  };  };
60    
61    my $queue_dir = './queue';
62    
63  my $HOSTNAME = `hostname -f`;  my $HOSTNAME = `hostname -f`;
64  chomp($HOSTNAME);  chomp($HOSTNAME);
65    
# Line 106  my $log_path; Line 112  my $log_path;
112  GetOptions(  GetOptions(
113          'import-dircproxy:s' => \$import_dircproxy,          'import-dircproxy:s' => \$import_dircproxy,
114          'log:s' => \$log_path,          'log:s' => \$log_path,
115            'queue:s' => \$queue_dir,
116  );  );
117    
118  #$SIG{__DIE__} = sub {  #$SIG{__DIE__} = sub {
# Line 118  sub _log { Line 125  sub _log {
125    
126  open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n";  open(STDOUT, '>', $log_path) && warn "log to $log_path: $!\n";
127    
128    # queue
129    
130    if ( ! -d $queue_dir ) {
131            warn "## creating queue directory $queue_dir";
132            mkdir $queue_dir or die "can't create queue directory $queue_dir: $!";
133    }
134    
135    my $dq = IPC::DirQueue->new({ dir => $queue_dir });
136    
137  # HTML formatters  # HTML formatters
138    
139  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 654  my $_stat; Line 670  my $_stat;
670  sub rss_fetch {  sub rss_fetch {
671          my ($args) = @_;          my ($args) = @_;
672    
673    
674          # how many messages to send out when feed is seen for the first time?          # how many messages to send out when feed is seen for the first time?
675          my $send_rss_msgs = 1;          my $send_rss_msgs = 1;
676    
# Line 709  sub rss_fetch { Line 726  sub rss_fetch {
726                          $send_rss_msgs--;                          $send_rss_msgs--;
727                          if ( ! $args->{private} ) {                          if ( ! $args->{private} ) {
728                                  # FIXME bug! should be save_message                                  # FIXME bug! should be save_message
729  #                               save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg );                                  save_message( channel => $args->{channel}, me => 1, nick => $NICK, message => $msg );
730                                  $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );  #                               $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );
731                          }                          }
732                          my ( $type, $to ) = ( 'notice', $args->{channel} );                          my ( $type, $to ) = ( 'notice', $args->{channel} );
733                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};                          ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};
734    
735                          _log(">> $type $to", $msg);                          _log(">> $type $to", $msg);
736                          $args->{kernel}->post( $irc => $type => $to, $msg );  #                       $args->{kernel}->post( $irc => $type => $to, $msg );
737                            # XXX enqueue message to send later
738                            sub enqueue_post {
739                                    my $post = dump( @_ );
740                                    warn "## queue_post $post\n" if $debug;
741                                    $dq->enqueue_string( $post );
742                            }
743                            enqueue_post( $type => $to => $msg );
744    
745                          $updates++;                          $updates++;
746                  }                  }
747          }          }
# Line 755  sub rss_check_updates { Line 781  sub rss_check_updates {
781          my $kernel = shift;          my $kernel = shift;
782          $_stat->{rss}->{last_poll} ||= time();          $_stat->{rss}->{last_poll} ||= time();
783          my $dt = time() - $_stat->{rss}->{last_poll};          my $dt = time() - $_stat->{rss}->{last_poll};
         warn "## rss_check_updates $dt > $rss_min_delay\n";  
784          if ( $dt > $rss_min_delay ) {          if ( $dt > $rss_min_delay ) {
785                    warn "## rss_check_updates $dt > $rss_min_delay\n";
786                  $_stat->{rss}->{last_poll} = time();                  $_stat->{rss}->{last_poll} = time();
787                  _log rss_fetch_all( $kernel );                  _log rss_fetch_all( $kernel );
788          }          }
789            # XXX send queue messages
790            while ( my $job = $dq->pickup_queued_job() ) {
791                    my $data = read_file( $job->get_data_path ) || die "can't load ", $job->get_data_path, ": $!";
792    #               $kernel->post( $irc => $type => $to, $msg );
793                    my @data = eval $data;
794                    _log ">> post from queue ", $irc, @data;
795                    $kernel->post( $irc => @data );
796                    $job->finish;
797                    warn "## done queued job: ",dump( @data ) if $debug;
798            }
799  }  }
800    
801  # seed rss seen cache so we won't send out all items on startup  # seed rss seen cache so we won't send out all items on startup
802  _log rss_fetch_all;  _log rss_fetch_all if ! $debug;
803    
804  POE::Session->create( inline_states => {  POE::Session->create( inline_states => {
805          _start => sub {                _start => sub {      
# Line 829  POE::Session->create( inline_states => { Line 865  POE::Session->create( inline_states => {
865                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
866                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
867                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
868                    warn "# ARG = ",dump( @_[ARG0,ARG1,ARG2] ) if $debug;
869    
870                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
871                  my @out;                  my @out;
# Line 1073  POE::Session->create( inline_states => { Line 1110  POE::Session->create( inline_states => {
1110                          _log ">> registreted, so IDENTIFY";                          _log ">> registreted, so IDENTIFY";
1111                          $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );                          $_[KERNEL]->post( $irc => privmsg => 'nickserv', "IDENTIFY $NICK" );
1112                  } else {                  } else {
1113                          warn "## ignore $m\n";                          warn "## ignore $m\n" if $debug;
1114                  }                  }
1115          },          },
1116          irc_snotice => sub {          irc_snotice => sub {

Legend:
Removed from v.118  
changed lines
  Added in v.119

  ViewVC Help
Powered by ViewVC 1.1.26