/[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 132 by dpavlin, Tue Apr 1 19:04:32 2008 UTC revision 147 by dpavlin, Sat Aug 28 20:24:57 2010 UTC
# Line 7  use HTTP::Status; Line 7  use HTTP::Status;
7  use DBI;  use DBI;
8  use Regexp::Common qw /URI/;  use Regexp::Common qw /URI/;
9  use CGI::Simple;  use CGI::Simple;
 use HTML::TagCloud;  
10  use POSIX qw/strftime/;  use POSIX qw/strftime/;
11  use HTML::CalendarMonthSimple;  use HTML::CalendarMonthSimple;
12  use Getopt::Long;  use Getopt::Long;
# Line 19  use Carp qw/confess/; Line 18  use Carp qw/confess/;
18  use XML::Feed;  use XML::Feed;
19  use DateTime::Format::Flexible;  use DateTime::Format::Flexible;
20  use Encode;  use Encode;
21    use Redis 2.0;
22    
23  =head1 NAME  =head1 NAME
24    
# Line 128  open(STDOUT, '>', $log_path) && warn "lo Line 128  open(STDOUT, '>', $log_path) && warn "lo
128  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
129  my $escape_re  = join '|' => keys %escape;  my $escape_re  = join '|' => keys %escape;
130    
131  my $tag_regex = '\b([\w-_]+)//';  my $tag_regex = '\b([\w\-_]+)//';
132    
133  my %nick_enumerator;  my %nick_enumerator;
134  my $max_color = 0;  my $max_color = 0;
# Line 140  my $filter = { Line 140  my $filter = {
140                  # protect HTML from wiki modifications                  # protect HTML from wiki modifications
141                  sub e {                  sub e {
142                          my $t = shift;                          my $t = shift;
143                          return 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}';                          eval { $t = 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}'; };
144                            return $t;
145                  }                  }
146    
147                  $m =~ s/($escape_re)/$escape{$1}/gs;                  $m =~ s/($escape_re)/$escape{$1}/gs;
148                  $m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs;                  $m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs;
149                  $m =~ s#\/(\w+)\/#<i>$1</i>#gs;  #               $m =~ s#\/(\w+)\/#<i>$1</i>#gs;
150                  $m =~ s#$tag_regex#e(qq{<a href="$url?tag=$1" class="tag">$1</a>})#egs;                  $m =~ s#$tag_regex#e(qq{<a href="$url?tag=$1" class="tag">$1</a>})#egs;
151                  $m =~ s#\*(\w+)\*#<b>$1</b>#gs;                  $m =~ s#\*(\w+)\*#<b>$1</b>#gs;
152                  $m =~ s#_(\w+)_#<u>$1</u>#gs;                  $m =~ s#_(\w+)_#<u>$1</u>#gs;
# Line 256  sub meta { Line 257  sub meta {
257    
258                  eval { $sth->execute( $value, $nick, $channel, $name ) };                  eval { $sth->execute( $value, $nick, $channel, $name ) };
259    
260                  # error or no result                  if ( $@ ) {
261                  if ( $@ || ! $sth->rows ) {                          # error
262                            _log("META ERROR: $@");
263                    } elsif ( ! $sth->rows ) {
264                            # no result -> add new
265                          $sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) });                          $sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) });
266                          $sth->execute( $value, $nick, $channel, $name );                          eval { $sth->execute( $value, $nick, $channel, $name ); };
267                          warn "## created $nick/$channel/$name = $value\n";                          if ( $@ ) {
268                                    _log "META ERROR: $@";
269                            } else {
270                                    _log "META: created $nick/$channel/$name = $value\n";
271                            }
272                  } else {                  } else {
273                          warn "## updated $nick/$channel/$name = $value\n";                          _log "META: updated $nick/$channel/$name = $value\n";
274                  }                  }
275    
276                  return $value;                  return $value;
# Line 526  sub get_from_log { Line 534  sub get_from_log {
534    
535  # tags support  # tags support
536    
537  my $cloud = HTML::TagCloud->new;  my $cloud = TagCloud->new;
538    $cloud->seed_tags;
 =head2 add_tag  
   
  add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] );  
   
 =cut  
   
 my @last_tags;  
   
 sub add_tag {  
         my $arg = {@_};  
   
         return unless ($arg->{id} && $arg->{message});  
   
         my $m = $arg->{message};  
   
         my @tags;  
   
         while ($m =~ s#$tag_regex##s) {  
                 my $tag = $1;  
                 next if (! $tag || $tag =~ m/https?:/i);  
                 push @{ $tags->{$tag} }, $arg->{id};  
                 #warn "+tag $tag: $arg->{id}\n";  
                 $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1);  
                 push @tags, $tag;  
   
         }  
   
         if ( @tags ) {  
                 pop @last_tags if $#last_tags == $last_x_tags;  
                 unshift @last_tags, { tags => [ @tags ], %$arg };  
         }  
   
 }  
   
 =head2 seed_tags  
   
 Read all tags from database and create in-memory cache for tags  
   
 =cut  
   
 sub seed_tags {  
         my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc });  
         $sth->execute;  
         while (my $row = $sth->fetchrow_hashref) {  
                 add_tag( %$row );  
         }  
   
         foreach my $tag (keys %$tags) {  
                 $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1);  
         }  
 }  
   
 seed_tags;  
   
539    
540  =head2 save_message  =head2 save_message
541    
# Line 611  sub save_message { Line 565  sub save_message {
565                  " " . $a->{message};                  " " . $a->{message};
566    
567          eval { $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); };          eval { $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); };
568          _log "ERROR: can't archive ", $a->{message} if $@;  
569          add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );          eval {
570            my $redis = Redis->new( server => '192.168.1.61:6379' );
571            my @channel = ( 'channel' , $a->{channel}, $a->{nick} );
572            push @channel, 'me' if $a->{me};
573            $redis->publish( join(' ',@channel), $a->{message} );
574            };
575    
576            if ( $@ ) {
577                    _log "ERROR: can't archive ", $a->{message};
578            } else {
579                    $cloud->add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );
580            }
581  }  }
582    
583    
# Line 681  sub rss_parse_xml { Line 646  sub rss_parse_xml {
646    
647          warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug;          warn "## RSS fetch first $send_rss_msgs items from", $args->{url} if $debug;
648    
649          my $feed = XML::Feed->parse( \$args->{xml} );          my $feed;
650            eval { $feed = XML::Feed->parse( \$args->{xml} ) };
651          if ( ! $feed ) {          if ( ! $feed ) {
652                  _log "can't fetch RSS ", $args->{url}, XML::Feed->errstr;                  _log "can't fetch RSS ", $args->{url}, XML::Feed->errstr;
653                  return;                  return;
# Line 1216  sub root_handler { Line 1182  sub root_handler {
1182    
1183          return RC_OK if $request->uri =~ m/favicon.ico$/;          return RC_OK if $request->uri =~ m/favicon.ico$/;
1184    
1185            if ( $request->uri =~ m/robots.txt$/ ) {
1186                    $response->content_type( 'text/plain' );
1187                    $response->content( qq{
1188    
1189    User-Agent: *
1190    Disallow: /
1191    
1192                    });
1193                    return RC_OK;
1194            }
1195    
1196          my $q;          my $q;
1197    
1198          if ( $request->method eq 'POST' ) {          if ( $request->method eq 'POST' ) {
# Line 1241  sub root_handler { Line 1218  sub root_handler {
1218                  $response->content_type( 'application/' . lc($type) . '+xml' );                  $response->content_type( 'application/' . lc($type) . '+xml' );
1219    
1220                  my $html = '<!-- error -->';                  my $html = '<!-- error -->';
1221                  #warn "create $type feed from ",dump( @last_tags );                  #warn "create $type feed from ",dump( $cloud->last_tags );
1222    
1223                  my $feed = XML::Feed->new( $type );                  my $feed = XML::Feed->new( $type );
1224                  $feed->link( $url );                  $feed->link( $url );
# Line 1275  sub root_handler { Line 1252  sub root_handler {
1252                          $feed->title( "last $nr tagged messages from $CHANNEL" );                          $feed->title( "last $nr tagged messages from $CHANNEL" );
1253                          $feed->description( "collects messages which have tags// in them" );                          $feed->description( "collects messages which have tags// in them" );
1254    
1255                          foreach my $m ( @last_tags ) {                          foreach my $m ( $cloud->last_tags ) {
1256  #                               warn dump( $m );  #                               warn dump( $m );
1257                                  #my $tags = join(' ', @{$m->{tags}} );                                  #my $tags = join(' ', @{$m->{tags}} );
1258                                  my $feed_entry = XML::Feed::Entry->new($type);                                  my $feed_entry = XML::Feed::Entry->new($type);
# Line 1338  sub root_handler { Line 1315  sub root_handler {
1315                          $rc = RC_DENY;                          $rc = RC_DENY;
1316                  }                  }
1317    
1318                  $response->content( $feed->as_xml );                  eval { $response->content( $feed->as_xml ); };
1319                    $rc = RC_INTERNAL_SERVER_ERROR if $@;
1320                  return $rc;                  return $rc;
1321          }          }
1322    
# Line 1405  sub root_handler { Line 1383  sub root_handler {
1383          } else {          } else {
1384                  $html .= join("</p><p>",                  $html .= join("</p><p>",
1385                          get_from_log(                          get_from_log(
1386                                  limit => ( $q->param('last') || $q->param('date') ) ? undef : 100,                                  limit => ( $q->param('date') ? undef : $q->param('last') || 100 ),
1387                                  search => $search || undef,                                  search => $search || undef,
1388                                  tag => $q->param('tag') || undef,                                  tag => $q->param('tag') || undef,
1389                                  date => $q->param('date') || undef,                                  date => $q->param('date') || undef,
# Line 1430  sub root_handler { Line 1408  sub root_handler {
1408          <p>See <a href="/history">history</a> of all messages.</p>          <p>See <a href="/history">history</a> of all messages.</p>
1409          </body></html>};          </body></html>};
1410    
1411          $response->content( decode('utf-8',$html) );          $response->content( $html );
1412          warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n";          warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n";
1413          return RC_OK;          return RC_OK;
1414  }  }
1415    
1416  POE::Kernel->run;  POE::Kernel->run;
1417    
1418    =head1 TagCloud
1419    
1420    Extended L<HTML::TagCloud>
1421    
1422    =cut
1423    
1424    package TagCloud;
1425    use warnings;
1426    use strict;
1427    use HTML::TagCloud;
1428    use base 'HTML::TagCloud';
1429    use Data::Dump qw/dump/;
1430    
1431    =head2 html
1432    
1433    Generate html with number of tags in title of link
1434    
1435    =cut
1436    
1437    sub html {
1438            my($self, $limit) = @_;
1439            my @tags=$self->tags($limit);
1440    
1441            my $ntags = scalar(@tags);
1442            if ($ntags == 0) {
1443                    return "";
1444    #       } elsif ($ntags == 1) {
1445    #               my $tag = $tags[0];
1446    #               return qq{<div id="htmltagcloud"><span class="tagcloud1"><a href="}.
1447    #               $tag->{url}.qq{">}.$tag->{name}.qq{</a></span></div>\n};
1448            }
1449    
1450      my $html = qq{<div id="htmltagcloud">};
1451      foreach my $tag ( sort { lc($a->{name}) cmp lc($b->{name}) } @tags) {
1452        $html .=  sprintf(qq{<span class="tag tagcloud%d"><a href="%s" title="%s">%s</a></span>\n},
1453                    $tag->{level}, $tag->{url}, $tag->{count}, $tag->{name}
1454            );
1455      }
1456      $html .= qq{</div>};
1457      return $html;
1458    }
1459    
1460    =head2 last_tags
1461    
1462      my @tags = $cloud->last_tags;
1463    
1464    =cut
1465    
1466    my @last_tags;
1467    sub last_tags {
1468            return @last_tags;
1469    }
1470    
1471    =head2 add_tag
1472    
1473     $cloud->add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] );
1474    
1475    =cut
1476    
1477    
1478    sub add_tag {
1479            my $self = shift;
1480            my $arg = {@_};
1481    
1482            return unless ($arg->{id} && $arg->{message});
1483    
1484            my $m = $arg->{message};
1485    
1486            my @tags;
1487    
1488            while ($m =~ s#$tag_regex##s) {
1489                    my $tag = $1;
1490                    next if (! $tag || $tag =~ m/https?:/i);
1491                    push @{ $tags->{$tag} }, $arg->{id};
1492                    #warn "+tag $tag: $arg->{id}\n";
1493                    $self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}});
1494                    push @tags, $tag;
1495    
1496            }
1497    
1498            if ( @tags ) {
1499                    pop @last_tags if $#last_tags == $last_x_tags;
1500                    unshift @last_tags, { tags => [ @tags ], %$arg };
1501            }
1502    
1503    }
1504    
1505    =head2 seed_tags
1506    
1507    Read all tags from database and create in-memory cache for tags
1508    
1509    =cut
1510    
1511    sub seed_tags {
1512            my $self = shift;
1513            my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc });
1514            $sth->execute;
1515            while (my $row = $sth->fetchrow_hashref) {
1516                    $self->add_tag( %$row );
1517            }
1518    
1519            foreach my $tag (keys %$tags) {
1520                    $self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}});
1521            }
1522    }
1523    

Legend:
Removed from v.132  
changed lines
  Added in v.147

  ViewVC Help
Powered by ViewVC 1.1.26