--- trunk/bin/irc-logger.pl 2007/03/18 16:03:24 51 +++ trunk/bin/irc-logger.pl 2007/12/07 12:51:55 69 @@ -73,7 +73,14 @@ use HTML::CalendarMonthSimple; use Getopt::Long; use DateTime; +use URI::Escape; use Data::Dump qw/dump/; +use DateTime::Format::ISO8601; +use Carp qw/confess/; + +my $use_twitter = 1; +eval { require Net::Twitter; }; +$use_twitter = 0 if ($@); my $import_dircproxy; my $log_path; @@ -82,6 +89,10 @@ 'log:s' => \$log_path, ); +$SIG{__DIE__} = sub { + confess "fatal error"; +}; + open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!"; sub _log { @@ -224,14 +235,16 @@ sub get_from_log { my $args = {@_}; - $args->{fmt} ||= { - date => '[%s] ', - time => '{%s} ', - time_channel => '{%s %s} ', - nick => '%s: ', - me_nick => '***%s ', - message => '%s', - }; + if ( ! $args->{fmt} ) { + $args->{fmt} = { + date => '[%s] ', + time => '{%s} ', + time_channel => '{%s %s} ', + nick => '%s: ', + me_nick => '***%s ', + message => '%s', + }; + } my $sql_message = qq{ select @@ -254,27 +267,50 @@ my $sql = $context ? $sql_context : $sql_message; - $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 .= " where date(time) = ? " if ($args->{date}); - $sql .= " order by log.time desc"; - $sql .= " limit " . $args->{limit} if ($args->{limit}); + sub check_date { + my $date = shift || return; + my $new_date = eval { DateTime::Format::ISO8601->parse_datetime( $date )->ymd; }; + if ( $@ ) { + warn "invalid date $date\n"; + $new_date = DateTime->now->ymd; + } + return $new_date; + } + + my @where; + my @args; - my $sth = $dbh->prepare( $sql ); if (my $search = $args->{search}) { $search =~ s/^\s+//; $search =~ s/\s+$//; - $sth->execute( ( '%' . $search . '%' ) x 2 ); - _log "search for '$search' returned ", $sth->rows, " results ", $context || ''; - } elsif (my $tag = $args->{tag}) { - $sth->execute(); - _log "tag '$tag' returned ", $sth->rows, " results ", $context || ''; - } elsif (my $date = $args->{date}) { - $sth->execute($date); - _log "found ", $sth->rows, " messages for date $date ", $context || ''; - } else { - $sth->execute(); + push @where, 'message ilike ? or nick ilike ?'; + push @args, ( ( '%' . $search . '%' ) x 2 ); + _log "search for '$search'"; + } + + if ($args->{tag} && $tags->{ $args->{tag} }) { + push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')'; + _log "search for tags $args->{tag}"; + } + + if (my $date = $args->{date} ) { + $date = check_date( $date ); + push @where, 'date(time) = ?'; + push @args, $date; + _log "search for date $date"; } + + $sql .= " where " . join(" and ", @where) if @where; + + $sql .= " order by log.time desc"; + $sql .= " limit " . $args->{limit} if ($args->{limit}); + + #warn "### sql: $sql ", dump( @args ); + + my $sth = $dbh->prepare( $sql ); + eval { $sth->execute( @args ) }; + return if $@; + my $last_row = { date => '', time => '', @@ -474,7 +510,7 @@ if ($import_dircproxy) { open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!"; warn "importing $import_dircproxy...\n"; - my $tz_offset = 2 * 60 * 60; # TZ GMT+2 + my $tz_offset = 1 * 60 * 60; # TZ GMT+2 while(<$l>) { chomp; if (/^@(\d+)\s(\S+)\s(.+)$/) { @@ -545,8 +581,13 @@ save_message( channel => $channel, me => 1, nick => $nick, msg => $msg); - if ( my $twitter = ( $nick, $channel, 'twitter' ) ) { - _log("FIXME: send twitter for $nick on $channel [$twitter]"); + if ( $use_twitter ) { + if ( my $twitter = meta( $nick, $NICK, 'twitter' ) ) { + my ($login,$passwd) = split(/\s+/,$twitter,2); + _log("sending twitter for $nick/$login on $channel "); + my $bot = Net::Twitter->new( username=>$login, password=>$passwd ); + $bot->update("<${channel}> $msg"); + } } }, @@ -593,11 +634,11 @@ my $sth = $dbh->prepare(qq{ select - nick, + trim(both '_' from nick) as nick, count(*) as count, sum(length(message)) as len from log - group by nick + group by trim(both '_' from nick) order by len desc,count desc limit $nr }); @@ -673,9 +714,9 @@ if ( ! defined( $1 ) ) { my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? }); $sth->execute( $nick, $channel ); - $res = "config for $nick "; + $res = "config for $nick on $channel"; while ( my ($n,$v) = $sth->fetchrow_array ) { - $res .= "| $n = $v"; + $res .= " | $n = $v"; } } elsif ( ! $2 ) { my $val = meta( $nick, $channel, $1 ); @@ -826,18 +867,37 @@ .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } .message { color: #000000; font-size: 100%; } .search { float: right; } +a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } +a:hover.tag { border: 1px solid #eee } +hr { border: 1px dashed #ccc; height: 1px; clear: both; } +/* .col-0 { background: #ffff66 } .col-1 { background: #a0ffff } .col-2 { background: #99ff99 } .col-3 { background: #ff9999 } .col-4 { background: #ff66ff } -a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } -a:hover.tag { border: 1px solid #eee } -hr { border: 1px dashed #ccc; height: 1px; clear: both; } +*/ +.calendar { border: 1px solid red; width: 100%; } +.month { border: 0px; width: 100%; } _END_OF_STYLE_ my $max_color = 4; +my @cols = qw( + #ffcccc #ccffe6 #ccccff #e6ccff #ffccff #ffcce6 #ff9999 #ffcc99 #ffff99 + #ccff99 #99ff99 #99ffcc #99ccff #9999ff #cc99ff #ff6666 #ffb366 #ffff66 + #66ff66 #66ffb3 #66b3ff #6666ff #ff3333 #33ff33 #3399ff #3333ff #ff3399 + #a0a0a0 #ff0000 #ffff00 #80ff00 #0000ff #8000ff #ff00ff #ff0080 #994d00 + #999900 #009900 #cc0066 #c0c0c0 #ccff99 #99ff33 #808080 #660033 #ffffff +); + +$max_color = 0; +foreach my $c (@cols) { + $style .= ".col-${max_color} { background: $c }\n"; + $max_color++; +} +warn "defined $max_color colors for users...\n"; + my %nick_enumerator; sub root_handler { @@ -871,33 +931,47 @@ qq{

}; if ($request->url =~ m#/history#) { my $sth = $dbh->prepare(qq{ - select date(time) as date,count(*) as nr + select date(time) as date,count(*) as nr,sum(length(message)) as len from log group by date(time) order by date(time) desc }); $sth->execute(); my ($l_yyyy,$l_mm) = (0,0); + $html .= qq{}; my $cal; + my $ord = 0; while (my $row = $sth->fetchrow_hashref) { # this is probably PostgreSQL specific, expects ISO date my ($yyyy,$mm,$dd) = split(/-/, $row->{date}); if ($yyyy != $l_yyyy || $mm != $l_mm) { - $html .= $cal->as_HTML() if ($cal); + if ( $cal ) { + $html .= qq{}; + $ord++; + $html .= qq{} if ( $ord % 3 == 0 ); + } $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy); - $cal->border(2); + $cal->border(1); + $cal->width('30%'); + $cal->cellheight('5em'); + $cal->tableclass('month'); + #$cal->cellclass('day'); + $cal->sunday('SUN'); + $cal->saturday('SAT'); + $cal->weekdays('MON','TUE','WED','THU','FRI'); ($l_yyyy,$l_mm) = ($yyyy,$mm); } $cal->setcontent($dd, qq{ - $row->{nr} + $row->{nr}
$row->{len} }); + } - $html .= $cal->as_HTML() if ($cal); + $html .= qq{
} . $cal->as_HTML() . qq{
} . $cal->as_HTML() . qq{
}; } else { $html .= join("

", get_from_log( - limit => $q->param('last') || $q->param('date') ? undef : 100, + limit => ( $q->param('last') || $q->param('date') ) ? undef : 100, search => $search || undef, tag => $q->param('tag') || undef, date => $q->param('date') || undef, @@ -915,9 +989,21 @@ filter => { message => sub { my $m = shift || return; + + # protect HTML from wiki modifications + sub e { + my $t = shift; + return 'uri_unescape{' . uri_escape($t) . '}'; + } + $m =~ s/($escape_re)/$escape{$1}/gs; - $m =~ s#($RE{URI}{HTTP})#$1#gs; - $m =~ s#$tag_regex#$1#g; + $m =~ s#($RE{URI}{HTTP})#e(qq{$1})#egs; + $m =~ s#$tag_regex#e(qq{$1})#egs; + $m =~ s#\*(\w+)\*#$1#gs; + $m =~ s#_(\w+)_#$1#gs; + $m =~ s#\/(\w+)\/#$1#gs; + + $m =~ s#uri_unescape{([^}]+)}#uri_unescape($1)#egs; return $m; }, nick => sub {