--- trunk/irc-logger.pl 2006/03/13 12:56:26 15 +++ trunk/irc-logger.pl 2006/03/13 21:02:16 19 @@ -18,7 +18,7 @@ ## CONFIG -my $NICK = 'irc-logger'; +my $NICK = 'irc-logger-dev'; my $CONNECT = {Server => 'irc.freenode.net', Nick => $NICK, @@ -33,7 +33,7 @@ ERROR => "/var/log/apache/error.log", ); -my $DSN = 'DBI:Pg:dbname=irc-logger'; +my $DSN = 'DBI:Pg:dbname=' . $NICK; my $ENCODING = 'ISO-8859-2'; @@ -46,17 +46,24 @@ 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; -=for SQL schema +eval { + $dbh->do(qq{ select count(*) from log }); +}; + +if ($@) { + warn "creating database table in $DSN\n"; + $dbh->do(<<'_SQL_SCHEMA_'); -$dbh->do(qq{ create table log ( id serial, time timestamp default now(), channel text not null, + me boolean default false, nick text not null, message text not null, primary key(id) @@ -66,14 +73,13 @@ create index log_channel on log(channel); create index log_nick on log(nick); -}); - -=cut +_SQL_SCHEMA_ +} my $sth = $dbh->prepare(qq{ insert into log - (channel, nick, message) -values (?,?,?) + (channel, me, nick, message) +values (?,?,?,?) }); =head2 get_from_log @@ -122,7 +128,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(); } @@ -139,7 +146,9 @@ unshift @rows, $row; } - my @msgs; + my @msgs = ( + "Showing " . ($#rows + 1) . " messages..." + ); foreach my $row (@rows) { @@ -214,7 +223,18 @@ from_to($msg, 'UTF-8', $ENCODING); print "$channel: <$nick> $msg\n"; - $sth->execute($channel, $nick, $msg); + $sth->execute($channel, 0, $nick, $msg); + }, + irc_ctcp_action => sub { + my $kernel = $_[KERNEL]; + my $nick = (split /!/, $_[ARG0])[0]; + my $channel = $_[ARG1]->[0]; + my $msg = $_[ARG2]; + + from_to($msg, 'UTF-8', $ENCODING); + + print "$channel ***$nick $msg\n"; + $sth->execute($channel, 1, $nick, $msg); }, irc_msg => sub { my $kernel = $_[KERNEL]; @@ -265,7 +285,7 @@ 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, $ENCODING, 'UTF-8'); $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); @@ -398,22 +418,46 @@ Headers => { Server => 'irc-logger' }, ); +my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); +my $escape_re = join '|' => keys %escape; + 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; 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 ', @@ -422,12 +466,13 @@ }, message_filter => sub { my $m = shift || return; + $m =~ s/($escape_re)/$escape{$1}/gs; $m =~ s#($RE{URI}{HTTP})#$1#gs; return $m; }, ) ) . - qq{} + qq{

} ); return RC_OK; }