--- trunk/irc-logger.pl 2006/03/12 14:19:00 13 +++ trunk/irc-logger.pl 2006/03/13 16:50:07 17 @@ -35,6 +35,8 @@ my $DSN = 'DBI:Pg:dbname=irc-logger'; +my $ENCODING = 'ISO-8859-2'; + ## END CONFIG @@ -43,6 +45,8 @@ use HTTP::Status; use DBI; use Encode qw/from_to/; +use Regexp::Common qw /URI/; +use CGI::Simple; my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; @@ -84,6 +88,10 @@ nick => '%s: ', message => '%s', }, + message_filter => sub { + # modify message content + return shift; + } ); =cut @@ -115,7 +123,8 @@ my $sth = $dbh->prepare( $sql ); if ($args->{search}) { - $sth->execute( $args->{search} ); + $sth->execute( '%' . $args->{search} . '%' ); + warn "search for '$args->{search}' returned ", $sth->rows, " results\n"; } else { $sth->execute(); } @@ -132,7 +141,9 @@ unshift @rows, $row; } - my @msgs; + my @msgs = ( + "Showing " . ($#rows + 1) . " messages..." + ); foreach my $row (@rows) { @@ -157,7 +168,15 @@ $append = 0; } - $msg .= sprintf($args->{fmt}->{message}, $row->{message}); + if (ref($args->{message_filter}) eq 'CODE') { + $msg .= sprintf($args->{fmt}->{message}, + $args->{message_filter}->( + $row->{message} + ) + ); + } else { + $msg .= sprintf($args->{fmt}->{message}, $row->{message}); + } if ($append && @msgs) { $msgs[$#msgs] .= " " . $msg; @@ -196,7 +215,7 @@ my $channel = $_[ARG1]->[0]; my $msg = $_[ARG2]; - from_to($msg, 'UTF-8', 'ISO-8859-2'); + from_to($msg, 'UTF-8', $ENCODING); print "$channel: <$nick> $msg\n"; $sth->execute($channel, $nick, $msg); @@ -205,7 +224,7 @@ my $kernel = $_[KERNEL]; my $nick = (split /!/, $_[ARG0])[0]; my $msg = $_[ARG2]; - from_to($msg, 'UTF-8', 'ISO-8859-2'); + from_to($msg, 'UTF-8', $ENCODING); my $res = "unknown command '$msg', try /msg $NICK help!"; my @out; @@ -240,7 +259,7 @@ foreach my $res (get_from_log( limit => $1 )) { print "last: $res\n"; - from_to($res, 'ISO-8859-2', 'UTF-8'); + from_to($res, $ENCODING, 'UTF-8'); $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); } @@ -250,9 +269,9 @@ my $what = $2; - foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) { + foreach my $res (get_from_log( limit => 20, search => $what )) { print "search [$what]: $res\n"; - from_to($res, 'ISO-8859-2', 'UTF-8'); + from_to($res, $ENCODING, 'UTF-8'); $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); } @@ -262,7 +281,7 @@ if ($res) { print ">> [$nick] $res\n"; - from_to($res, 'ISO-8859-2', 'UTF-8'); + from_to($res, $ENCODING, 'UTF-8'); $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); } @@ -378,36 +397,62 @@ # http server my $httpd = POE::Component::Server::HTTP->new( - Port => 8000, + Port => $NICK =~ m/-dev/ ? 8001 : 8000, ContentHandler => { '/' => \&root_handler }, Headers => { Server => 'irc-logger' }, ); my $style = <<'_END_OF_STYLE_'; +p { margin: 0; padding: 0.1em; } .time, .channel { color: #808080; font-size: 60%; } .nick { color: #0000ff; font-size: 80%; } .message { color: #000000; font-size: 100%; } +.search { float: right; } _END_OF_STYLE_ sub root_handler { my ($request, $response) = @_; $response->code(RC_OK); - $response->content_type('text/html'); + $response->content_type("text/html; charset=$ENCODING"); + + my $q; + + if ( $request->method eq 'POST' ) { + $q = new CGI::Simple( $request->content ); + } elsif ( $request->uri =~ /\?(.+)$/ ) { + $q = new CGI::Simple( $1 ); + } else { + $q = new CGI::Simple; + } + + my $search = $q->param('search') || $q->param('grep') || ''; + $response->content( - qq{$NICK} . - "irc-logger url: " . $request->uri . '
' . - join("
", + qq{$NICK + +

+ } . + join("

", get_from_log( - limit => 100, + limit => $q->param('limit') || 100, + search => $q->param('search') || $q->param('grep') || undef, fmt => { time => '%s ', time_channel => '%s %s ', nick => '%s: ', message => '%s', }, + message_filter => sub { + my $m = shift || return; + $m =~ s#($RE{URI}{HTTP})#$1#gs; + return $m; + }, ) ) . - qq{} + qq{

} ); return RC_OK; }