--- trunk/irc-logger.pl 2006/03/24 23:19:57 22 +++ trunk/irc-logger.pl 2006/06/16 20:55:19 29 @@ -18,7 +18,7 @@ ## CONFIG -my $NICK = 'irc-logger-dev'; +my $NICK = 'irc-logger'; my $CONNECT = {Server => 'irc.freenode.net', Nick => $NICK, @@ -47,7 +47,7 @@ use Encode qw/from_to/; use Regexp::Common qw /URI/; use CGI::Simple; - +use HTML::TagCloud; my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; @@ -82,6 +82,8 @@ values (?,?,?,?) }); +my $tags; + =head2 get_from_log my @messages = get_from_log( @@ -116,6 +118,7 @@ $args->{limit} ||= 10; $args->{fmt} ||= { + date => '[%s] ', time => '{%s} ', time_channel => '{%s %s} ', nick => '%s: ', @@ -144,7 +147,8 @@ my $sql = $context ? $sql_context : $sql_message; - $sql .= " where message ilike ? " if ($args->{search}); + $sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); + $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); $sql .= " order by log.time desc"; $sql .= " limit " . $args->{limit}; @@ -152,8 +156,11 @@ if (my $search = $args->{search}) { $search =~ s/^\s+//; $search =~ s/\s+$//; - $sth->execute( '%' . $search . '%' ); + $sth->execute( ( '%' . $search . '%' ) x 2 ); warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; + } elsif (my $tag = $args->{tag}) { + $sth->execute(); + warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; } else { $sth->execute(); } @@ -202,12 +209,11 @@ $row->{time} =~ s#\.\d+##; - my $t; - $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date}); - $t .= $row->{time}; - my $msg = ''; + $msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date}); + my $t = $row->{time}; + if ($last_row->{channel} ne $row->{channel}) { $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); } else { @@ -217,8 +223,10 @@ my $append = 1; my $nick = $row->{nick}; - $nick =~ s/^_*(.*?)_*$/$1/; - + if ($nick =~ s/^_*(.*?)_*$/$1/) { + $row->{nick} = $nick; + } + if ($last_row->{nick} ne $nick) { # obfu way to find format for me_nick if needed or fallback to default my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick}; @@ -471,6 +479,48 @@ }, ); +# tags support + +my $cloud = HTML::TagCloud->new; + +=head2 add_tag + + add_tag( id => 42, message => 'irc message' ); + +=cut + +sub add_tag { + my $arg = {@_}; + + return unless ($arg->{id} && $arg->{message}); + + while ($arg->{message} =~ s#\b(\S+)//##s) { + my $tag = $1; + next if (! $tag || $tag =~ m/https?:/i); + push @{ $tags->{$tag} }, $arg->{id}; + } +} + +=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 from log where message like '%//%' }); + $sth->execute; + while (my $row = $sth->fetchrow_hashref) { + add_tag( %$row ); + } + + foreach my $tag (keys %$tags) { + $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); + } +} + +seed_tags; + # http server my $httpd = POE::Component::Server::HTTP->new( @@ -485,6 +535,7 @@ my $style = <<'_END_OF_STYLE_'; p { margin: 0; padding: 0.1em; } .time, .channel { color: #808080; font-size: 60%; } +.date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; } .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } .message { color: #000000; font-size: 100%; } .search { float: right; } @@ -517,18 +568,24 @@ my $search = $q->param('search') || $q->param('grep') || ''; $response->content( - qq{$NICK + qq{$NICK} . + qq{ -

} . + qq{

} . $cloud->html(500) . qq{
} . + qq{

} . join("

", get_from_log( - limit => $q->param('limit') || 100, - search => $q->param('search') || $q->param('grep') || undef, + limit => $q->param('last') || 100, + search => $search || undef, + tag => $q->param('tag') || undef, fmt => { + date => '


%s
', time => '%s ', time_channel => '%s %s ', nick => '%s: ',