/[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 86 by dpavlin, Thu Mar 6 22:57:16 2008 UTC revision 94 by dpavlin, Fri Mar 7 10:50:16 2008 UTC
# Line 20  Import log from C<dircproxy> to C<irc-lo Line 20  Import log from C<dircproxy> to C<irc-lo
20    
21  =item --log=irc-logger.log  =item --log=irc-logger.log
22    
 Name of log file  
   
 =item --follow=file.log  
   
 Follows new messages in file  
   
23  =back  =back
24    
25  =head1 DESCRIPTION  =head1 DESCRIPTION
# Line 50  my $CHANNEL = '#razmjenavjestina'; Line 44  my $CHANNEL = '#razmjenavjestina';
44  $CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);  $CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);
45  my $IRC_ALIAS = "log";  my $IRC_ALIAS = "log";
46    
 # default log to follow and announce messages  
 my $follows_path = 'follows.log';  
   
47  my $DSN = 'DBI:Pg:dbname=' . $NICK;  my $DSN = 'DBI:Pg:dbname=' . $NICK;
48    
 # log output encoding  
 my $ENCODING = 'ISO-8859-2';  
49  my $TIMESTAMP = '%Y-%m-%d %H:%M:%S';  my $TIMESTAMP = '%Y-%m-%d %H:%M:%S';
50    
51  my $sleep_on_error = 5;  my $sleep_on_error = 5;
# Line 74  my $url = "http://$HOSTNAME:$http_port"; Line 63  my $url = "http://$HOSTNAME:$http_port";
63    
64  ## END CONFIG  ## END CONFIG
65    
66  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);  use POE qw(Component::IRC Component::Server::HTTP);
67  use HTTP::Status;  use HTTP::Status;
68  use DBI;  use DBI;
 use Encode qw/from_to is_utf8/;  
69  use Regexp::Common qw /URI/;  use Regexp::Common qw /URI/;
70  use CGI::Simple;  use CGI::Simple;
71  use HTML::TagCloud;  use HTML::TagCloud;
# Line 100  my $import_dircproxy; Line 88  my $import_dircproxy;
88  my $log_path;  my $log_path;
89  GetOptions(  GetOptions(
90          'import-dircproxy:s' => \$import_dircproxy,          'import-dircproxy:s' => \$import_dircproxy,
         'follows:s' => \$follows_path,  
91          'log:s' => \$log_path,          'log:s' => \$log_path,
92  );  );
93    
94  $SIG{__DIE__} = sub {  #$SIG{__DIE__} = sub {
95          confess "fatal error";  #       confess "fatal error";
96  };  #};
97    
98  open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";  open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
99    
100  sub _log {  sub _log {
101          my $out = strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/;          print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/;
         from_to( $out, 'UTF-8', $ENCODING );  
         print $out;  
102  }  }
103    
 # LOG following  
   
 my %FOLLOWS =  
   (  
 #   ACCESS => "/var/log/apache/access.log",  
 #   ERROR => "/var/log/apache/error.log",  
   );  
   
 sub add_follow_path {  
         my $path = shift;  
         my $name = $path;  
         $name =~ s/\..*$//;  
         warn "# using $path to announce messages from $name\n";  
         $FOLLOWS{$name} = $path;  
 }  
   
 add_follow_path( $follows_path ) if ( -e $follows_path );  
   
104  # HTML formatters  # HTML formatters
105    
106  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
# Line 282  sub meta { Line 249  sub meta {
249    
250    
251    
252  my $sth = $dbh->prepare(qq{  my $sth_insert_log = $dbh->prepare(qq{
253  insert into log  insert into log
254          (channel, me, nick, message, time)          (channel, me, nick, message, time)
255  values (?,?,?,?,?)  values (?,?,?,?,?)
# Line 534  sub add_tag { Line 501  sub add_tag {
501          return unless ($arg->{id} && $arg->{message});          return unless ($arg->{id} && $arg->{message});
502    
503          my $m = $arg->{message};          my $m = $arg->{message};
         from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));  
504    
505          my @tags;          my @tags;
506    
# Line 603  sub save_message { Line 569  sub save_message {
569                  $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",                  $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",
570                  " " . $a->{message};                  " " . $a->{message};
571    
572          $sth->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time});          $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time});
573          add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );          add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );
574  }  }
575    
# Line 655  sub rss_fetch { Line 621  sub rss_fetch {
621          # 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?
622          my $send_rss_msgs = 1;          my $send_rss_msgs = 1;
623    
624            _log "RSS fetch", $args->{url};
625    
626          my $feed = XML::Feed->parse(URI->new( $args->{url} ));          my $feed = XML::Feed->parse(URI->new( $args->{url} ));
627          if ( ! $feed ) {          if ( ! $feed ) {
628                  _log("can't fetch RSS ", $args->{url});                  _log("can't fetch RSS ", $args->{url});
629                  return;                  return;
630          }          }
631          my $updates = 0;  
632            my ( $total, $updates ) = ( 0, 0 );
633          for my $entry ($feed->entries) {          for my $entry ($feed->entries) {
634                    $total++;
635    
636                  # seen allready?                  # seen allready?
637                  return if $_rss->{$feed->link}->{seen}->{$entry->id}++ > 0;                  next if $_rss->{$feed->link}->{seen}->{$entry->id}++ > 0;
638    
639                  sub prefix {                  sub prefix {
640                          my ($txt,$var) = @_;                          my ($txt,$var) = @_;
641                            $var =~ s/\s+/ /gs;
642                          $var =~ s/^\s+//g;                          $var =~ s/^\s+//g;
643                            $var =~ s/\s+$//g;
644                          return $txt . $var if $var;                          return $txt . $var if $var;
645                  }                  }
646    
647                    # fix absolute and relative links to feed entries
648                    my $link = $entry->link;
649                    if ( $link =~ m!^/! ) {
650                            my $host = $args->{url};
651                            $host =~ s!^(http://[^/]+).*$!$1!;      #!vim
652                            $link = "$host/$link";
653                    } elsif ( $link !~ m!^http! ) {
654                            $link = $args->{url} . $link;
655                    }
656                    $link =~ s!//+!/!g;
657    
658                  my $msg;                  my $msg;
659                  $msg .= prefix( 'From: ' , $feed->title );                  $msg .= prefix( 'From: ' , $args->{name} || $feed->title );
660                  $msg .= prefix( ' by ' , $entry->author );                  $msg .= prefix( ' by ' , $entry->author );
661                  $msg .= prefix( ' -- ' , $entry->link );                  $msg .= prefix( ' | ' , $entry->title );
662                    $msg .= prefix( ' | ' , $link );
663  #               $msg .= prefix( ' id ' , $entry->id );  #               $msg .= prefix( ' id ' , $entry->id );
664    
665                  if ( $args->{kernel} && $send_rss_msgs ) {                  if ( $args->{kernel} && $send_rss_msgs ) {
                         warn "# sending to $CHANNEL\n";  
666                          $send_rss_msgs--;                          $send_rss_msgs--;
667                            _log('>>', $msg);
668                            $sth_insert_log->execute( $CHANNEL, 1, $NICK, $msg, 'now()' );
669                          $args->{kernel}->post( $IRC_ALIAS => notice => $CHANNEL, $msg );                          $args->{kernel}->post( $IRC_ALIAS => notice => $CHANNEL, $msg );
670                          $updates++;                          $updates++;
                         save_message( channel => $CHANNEL, me => 1, nick => $NICK, message => $msg );  
                         _log('RSS', $msg);  
671                  }                  }
672          }          }
673    
# Line 693  sub rss_fetch { Line 676  sub rss_fetch {
676          $sql .= qq{where id = } . $args->{id};          $sql .= qq{where id = } . $args->{id};
677          eval { $dbh->do( $sql ) };          eval { $dbh->do( $sql ) };
678    
679            _log "RSS got $total items of which $updates new";
680    
681          return $updates;          return $updates;
682  }  }
683    
# Line 710  sub rss_fetch_all { Line 695  sub rss_fetch_all {
695          warn "# ",$sth->rows," active RSS feeds\n";          warn "# ",$sth->rows," active RSS feeds\n";
696          my $count = 0;          my $count = 0;
697          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
                 warn "+++ fetch RSS feed: ",dump( $row );  
698                  $row->{kernel} = $kernel if $kernel;                  $row->{kernel} = $kernel if $kernel;
699                  $count += rss_fetch( $row );                  $count += rss_fetch( $row );
700          }          }
701          return "OK, fetched $count posts from " . $sth->rows . " feeds";          return "OK, fetched $count posts from " . $sth->rows . " feeds";
702  }  }
703    
 my $rss_last_poll = time();  
704    
705  sub rss_check_updates {  sub rss_check_updates {
706          my $kernel = shift;          my $kernel = shift;
707            my $last_t = $_rss->{last_poll} || time();
708          my $t = time();          my $t = time();
709          if ( $rss_last_poll - $t > $rss_min_delay ) {          if ( $t - $last_t > $rss_min_delay ) {
710                  $rss_last_poll = $t;                  $_rss->{last_poll} = $t;
711                  _log rss_fetch_all( $kernel );                  _log rss_fetch_all( $kernel );
712          }          }
713  }  }
# Line 735  _log rss_fetch_all; Line 719  _log rss_fetch_all;
719  # POE handing part  # POE handing part
720  #  #
721    
 my $SKIPPING = 0;               # if skipping, how many we've done  
 my $SEND_QUEUE;                 # cache  
722  my $ping;                                               # ping stats  my $ping;                                               # ping stats
723    
724  POE::Component::IRC->new($IRC_ALIAS);  POE::Component::IRC->new($IRC_ALIAS);
# Line 748  POE::Session->create( inline_states => { Line 730  POE::Session->create( inline_states => {
730      },      },
731      irc_255 => sub {    # server is done blabbing      irc_255 => sub {    # server is done blabbing
732                  $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);                  $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
                 $_[KERNEL]->post($IRC_ALIAS => join => '#logger');  
                 $_[KERNEL]->yield("heartbeat"); # start heartbeat  
                 $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;  
733                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
734      },      },
735      irc_public => sub {      irc_public => sub {
# Line 931  POE::Session->create( inline_states => { Line 910  POE::Session->create( inline_states => {
910                          $res = rss_fetch_all( $_[KERNEL] );                          $res = rss_fetch_all( $_[KERNEL] );
911                  } elsif ($msg =~ m/^rss-clean/) {                  } elsif ($msg =~ m/^rss-clean/) {
912                          $_rss = undef;                          $_rss = undef;
913                            $dbh->do( qq{ update feeds set last_update = now() - delay } );
914                          $res = "OK, cleaned RSS cache";                          $res = "OK, cleaned RSS cache";
915                    } elsif ($msg =~ m/^rss-list/) {
916                            my $sth = $dbh->prepare(qq{ select url,name,last_update,active from feeds });
917                            $sth->execute;
918                            while (my @row = $sth->fetchrow_array) {
919                                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, join(' | ',@row) );
920                            }
921                            $res = '';
922                  } elsif ($msg =~ m!^rss-(add|remove|stop|start)\s+(http://\S+)\s*(.*)!) {                  } elsif ($msg =~ m!^rss-(add|remove|stop|start)\s+(http://\S+)\s*(.*)!) {
923                          my $sql = {                          my $sql = {
924                                  add             => qq{ insert into feeds (url,name) values (?,?) },                                  add             => qq{ insert into feeds (url,name) values (?,?) },
925  #                               remove  => qq{ delete from feeds                                where url = ? and name = ? },  #                               remove  => qq{ delete from feeds                                where url = ? and name = ? },
926                                  start   => qq{ update feeds set active = true   where url = ? -- ? },                                  start   => qq{ update feeds set active = true   where url = ? },
927                                  stop    => qq{ update feeds set active = false  where url = ? -- ? },                                  stop    => qq{ update feeds set active = false  where url = ? },
                                   
928                          };                          };
929                          if (my $q = $sql->{$1} ) {                          if (my $q = $sql->{$1} ) {
930                                  my $sth = $dbh->prepare( $q );                                  my $sth = $dbh->prepare( $q );
931                                  warn "## SQL $q ( $2 | $3 )\n";                                  my @data = ( $2 );
932                                  eval { $sth->execute( $2, $3 ) };                                  push @data, $3 if ( $q =~ s/\?//g == 2 );
933                                    warn "## $1 SQL $q with ",dump( @data ),"\n";
934                                    eval { $sth->execute( @data ) };
935                          }                          }
936    
937                          $res ||= "OK, RSS $1 : $2 - $3";                          $res = "OK, RSS $1 : $2 - $3";
938                  }                  }
939    
940                  if ($res) {                  if ($res) {
# Line 993  POE::Session->create( inline_states => { Line 981  POE::Session->create( inline_states => {
981                          "";                          "";
982        0;                        # false for signals        0;                        # false for signals
983      },      },
     my_add => sub {  
       my $trailing = $_[ARG0];  
       my $session = $_[SESSION];  
       POE::Session->create  
           (inline_states =>  
            {_start => sub {  
               $_[HEAP]->{wheel} =  
                 POE::Wheel::FollowTail->new  
                     (  
                      Filename => $FOLLOWS{$trailing},  
                      InputEvent => 'got_line',  
                     );  
                                 warn "+++ following $trailing at $FOLLOWS{$trailing}\n";  
             },  
             got_line => sub {  
                                 warn "+++ $trailing : $_[ARG0]\n";  
                                 $_[KERNEL]->post($session => my_tailed => time, $trailing, $_[ARG0]);  
             },  
            },  
           );  
       
     },  
     my_tailed => sub {  
       my ($time, $file, $line) = @_[ARG0..ARG2];  
       ## $time will be undef on a probe, or a time value if a real line  
   
       ## PoCo::IRC has throttling built in, but no external visibility  
       ## so this is reaching "under the hood"  
       $SEND_QUEUE ||=  
         $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};  
   
       ## handle "no need to keep skipping" transition  
       if ($SKIPPING and @$SEND_QUEUE < 1) {  
         $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>  
                          "[discarded $SKIPPING messages]");  
         $SKIPPING = 0;  
       }  
   
       ## handle potential message display  
       if ($time) {  
         if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds  
           $SKIPPING++;  
         } else {  
           my @time = localtime $time;  
           $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>  
                            sprintf "%02d:%02d:%02d: %s: %s",  
                            ($time[2] + 11) % 12 + 1, $time[1], $time[0],  
                            $file, $line);  
         }  
       }  
   
       ## handle re-probe/flush if skipping  
       if ($SKIPPING) {  
         $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef  
       }  
   
     },  
     my_heartbeat => sub {  
       $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");  
       $_[KERNEL]->delay($_[STATE] => 10);  
     }  
984     },     },
985    );    );
986    
# Line 1275  sub root_handler { Line 1202  sub root_handler {
1202                          }                          }
1203                          $cal->setcontent($dd, qq[                          $cal->setcontent($dd, qq[
1204                                  <a href="$url?date=$row->{date}">$row->{nr}</a><br/>$row->{len}                                  <a href="$url?date=$row->{date}">$row->{nr}</a><br/>$row->{len}
1205                          ]);                          ]) if $cal;
1206                                                    
1207                  }                  }
1208                  $html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>};                  $html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>};

Legend:
Removed from v.86  
changed lines
  Added in v.94

  ViewVC Help
Powered by ViewVC 1.1.26