/[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 143 by dpavlin, Sat Dec 19 21:21:47 2009 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 128  open(STDOUT, '>', $log_path) && warn "lo Line 127  open(STDOUT, '>', $log_path) && warn "lo
127  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
128  my $escape_re  = join '|' => keys %escape;  my $escape_re  = join '|' => keys %escape;
129    
130  my $tag_regex = '\b([\w-_]+)//';  my $tag_regex = '\b([\w\-_]+)//';
131    
132  my %nick_enumerator;  my %nick_enumerator;
133  my $max_color = 0;  my $max_color = 0;
# Line 140  my $filter = { Line 139  my $filter = {
139                  # protect HTML from wiki modifications                  # protect HTML from wiki modifications
140                  sub e {                  sub e {
141                          my $t = shift;                          my $t = shift;
142                          return 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}';                          eval { $t = 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}'; };
143                            return $t;
144                  }                  }
145    
146                  $m =~ s/($escape_re)/$escape{$1}/gs;                  $m =~ s/($escape_re)/$escape{$1}/gs;
147                  $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;
148                  $m =~ s#\/(\w+)\/#<i>$1</i>#gs;  #               $m =~ s#\/(\w+)\/#<i>$1</i>#gs;
149                  $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;
150                  $m =~ s#\*(\w+)\*#<b>$1</b>#gs;                  $m =~ s#\*(\w+)\*#<b>$1</b>#gs;
151                  $m =~ s#_(\w+)_#<u>$1</u>#gs;                  $m =~ s#_(\w+)_#<u>$1</u>#gs;
# Line 256  sub meta { Line 256  sub meta {
256    
257                  eval { $sth->execute( $value, $nick, $channel, $name ) };                  eval { $sth->execute( $value, $nick, $channel, $name ) };
258    
259                  # error or no result                  if ( $@ ) {
260                  if ( $@ || ! $sth->rows ) {                          # error
261                            _log("META ERROR: $@");
262                    } elsif ( ! $sth->rows ) {
263                            # no result -> add new
264                          $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()) });
265                          $sth->execute( $value, $nick, $channel, $name );                          eval { $sth->execute( $value, $nick, $channel, $name ); };
266                          warn "## created $nick/$channel/$name = $value\n";                          if ( $@ ) {
267                                    _log "META ERROR: $@";
268                            } else {
269                                    _log "META: created $nick/$channel/$name = $value\n";
270                            }
271                  } else {                  } else {
272                          warn "## updated $nick/$channel/$name = $value\n";                          _log "META: updated $nick/$channel/$name = $value\n";
273                  }                  }
274    
275                  return $value;                  return $value;
# Line 526  sub get_from_log { Line 533  sub get_from_log {
533    
534  # tags support  # tags support
535    
536  my $cloud = HTML::TagCloud->new;  my $cloud = TagCloud->new;
537    $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;  
   
538    
539  =head2 save_message  =head2 save_message
540    
# Line 611  sub save_message { Line 564  sub save_message {
564                  " " . $a->{message};                  " " . $a->{message};
565    
566          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}); };
567          _log "ERROR: can't archive ", $a->{message} if $@;          if ( $@ ) {
568          add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );                  _log "ERROR: can't archive ", $a->{message};
569            } else {
570                    $cloud->add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );
571            }
572  }  }
573    
574    
# Line 681  sub rss_parse_xml { Line 637  sub rss_parse_xml {
637    
638          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;
639    
640          my $feed = XML::Feed->parse( \$args->{xml} );          my $feed;
641            eval { $feed = XML::Feed->parse( \$args->{xml} ) };
642          if ( ! $feed ) {          if ( ! $feed ) {
643                  _log "can't fetch RSS ", $args->{url}, XML::Feed->errstr;                  _log "can't fetch RSS ", $args->{url}, XML::Feed->errstr;
644                  return;                  return;
# Line 1216  sub root_handler { Line 1173  sub root_handler {
1173    
1174          return RC_OK if $request->uri =~ m/favicon.ico$/;          return RC_OK if $request->uri =~ m/favicon.ico$/;
1175    
1176            if ( $request->uri =~ m/robots.txt$/ ) {
1177                    $response->content_type( 'text/plain' );
1178                    $response->content( qq{
1179    
1180    User-Agent: *
1181    Disallow: *
1182    
1183                    });
1184                    return RC_OK;
1185            }
1186    
1187          my $q;          my $q;
1188    
1189          if ( $request->method eq 'POST' ) {          if ( $request->method eq 'POST' ) {
# Line 1241  sub root_handler { Line 1209  sub root_handler {
1209                  $response->content_type( 'application/' . lc($type) . '+xml' );                  $response->content_type( 'application/' . lc($type) . '+xml' );
1210    
1211                  my $html = '<!-- error -->';                  my $html = '<!-- error -->';
1212                  #warn "create $type feed from ",dump( @last_tags );                  #warn "create $type feed from ",dump( $cloud->last_tags );
1213    
1214                  my $feed = XML::Feed->new( $type );                  my $feed = XML::Feed->new( $type );
1215                  $feed->link( $url );                  $feed->link( $url );
# Line 1275  sub root_handler { Line 1243  sub root_handler {
1243                          $feed->title( "last $nr tagged messages from $CHANNEL" );                          $feed->title( "last $nr tagged messages from $CHANNEL" );
1244                          $feed->description( "collects messages which have tags// in them" );                          $feed->description( "collects messages which have tags// in them" );
1245    
1246                          foreach my $m ( @last_tags ) {                          foreach my $m ( $cloud->last_tags ) {
1247  #                               warn dump( $m );  #                               warn dump( $m );
1248                                  #my $tags = join(' ', @{$m->{tags}} );                                  #my $tags = join(' ', @{$m->{tags}} );
1249                                  my $feed_entry = XML::Feed::Entry->new($type);                                  my $feed_entry = XML::Feed::Entry->new($type);
# Line 1338  sub root_handler { Line 1306  sub root_handler {
1306                          $rc = RC_DENY;                          $rc = RC_DENY;
1307                  }                  }
1308    
1309                  $response->content( $feed->as_xml );                  eval { $response->content( $feed->as_xml ); };
1310                    $rc = RC_INTERNAL_SERVER_ERROR if $@;
1311                  return $rc;                  return $rc;
1312          }          }
1313    
# Line 1405  sub root_handler { Line 1374  sub root_handler {
1374          } else {          } else {
1375                  $html .= join("</p><p>",                  $html .= join("</p><p>",
1376                          get_from_log(                          get_from_log(
1377                                  limit => ( $q->param('last') || $q->param('date') ) ? undef : 100,                                  limit => ( $q->param('date') ? undef : $q->param('last') || 100 ),
1378                                  search => $search || undef,                                  search => $search || undef,
1379                                  tag => $q->param('tag') || undef,                                  tag => $q->param('tag') || undef,
1380                                  date => $q->param('date') || undef,                                  date => $q->param('date') || undef,
# Line 1430  sub root_handler { Line 1399  sub root_handler {
1399          <p>See <a href="/history">history</a> of all messages.</p>          <p>See <a href="/history">history</a> of all messages.</p>
1400          </body></html>};          </body></html>};
1401    
1402          $response->content( decode('utf-8',$html) );          $response->content( $html );
1403          warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n";          warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n";
1404          return RC_OK;          return RC_OK;
1405  }  }
1406    
1407  POE::Kernel->run;  POE::Kernel->run;
1408    
1409    =head1 TagCloud
1410    
1411    Extended L<HTML::TagCloud>
1412    
1413    =cut
1414    
1415    package TagCloud;
1416    use warnings;
1417    use strict;
1418    use HTML::TagCloud;
1419    use base 'HTML::TagCloud';
1420    use Data::Dump qw/dump/;
1421    
1422    =head2 html
1423    
1424    Generate html with number of tags in title of link
1425    
1426    =cut
1427    
1428    sub html {
1429            my($self, $limit) = @_;
1430            my @tags=$self->tags($limit);
1431    
1432            my $ntags = scalar(@tags);
1433            if ($ntags == 0) {
1434                    return "";
1435    #       } elsif ($ntags == 1) {
1436    #               my $tag = $tags[0];
1437    #               return qq{<div id="htmltagcloud"><span class="tagcloud1"><a href="}.
1438    #               $tag->{url}.qq{">}.$tag->{name}.qq{</a></span></div>\n};
1439            }
1440    
1441      my $html = qq{<div id="htmltagcloud">};
1442      foreach my $tag ( sort { lc($a->{name}) cmp lc($b->{name}) } @tags) {
1443        $html .=  sprintf(qq{<span class="tag tagcloud%d"><a href="%s" title="%s">%s</a></span>\n},
1444                    $tag->{level}, $tag->{url}, $tag->{count}, $tag->{name}
1445            );
1446      }
1447      $html .= qq{</div>};
1448      return $html;
1449    }
1450    
1451    =head2 last_tags
1452    
1453      my @tags = $cloud->last_tags;
1454    
1455    =cut
1456    
1457    my @last_tags;
1458    sub last_tags {
1459            return @last_tags;
1460    }
1461    
1462    =head2 add_tag
1463    
1464     $cloud->add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] );
1465    
1466    =cut
1467    
1468    
1469    sub add_tag {
1470            my $self = shift;
1471            my $arg = {@_};
1472    
1473            return unless ($arg->{id} && $arg->{message});
1474    
1475            my $m = $arg->{message};
1476    
1477            my @tags;
1478    
1479            while ($m =~ s#$tag_regex##s) {
1480                    my $tag = $1;
1481                    next if (! $tag || $tag =~ m/https?:/i);
1482                    push @{ $tags->{$tag} }, $arg->{id};
1483                    #warn "+tag $tag: $arg->{id}\n";
1484                    $self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}});
1485                    push @tags, $tag;
1486    
1487            }
1488    
1489            if ( @tags ) {
1490                    pop @last_tags if $#last_tags == $last_x_tags;
1491                    unshift @last_tags, { tags => [ @tags ], %$arg };
1492            }
1493    
1494    }
1495    
1496    =head2 seed_tags
1497    
1498    Read all tags from database and create in-memory cache for tags
1499    
1500    =cut
1501    
1502    sub seed_tags {
1503            my $self = shift;
1504            my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc });
1505            $sth->execute;
1506            while (my $row = $sth->fetchrow_hashref) {
1507                    $self->add_tag( %$row );
1508            }
1509    
1510            foreach my $tag (keys %$tags) {
1511                    $self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}});
1512            }
1513    }
1514    

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

  ViewVC Help
Powered by ViewVC 1.1.26