--- trunk/irc-logger.pl 2006/03/02 00:52:22 11 +++ trunk/irc-logger.pl 2006/03/12 14:36:12 14 @@ -35,11 +35,14 @@ my $DSN = 'DBI:Pg:dbname=irc-logger'; +my $ENCODING = 'ISO-8859-2'; + ## END CONFIG -use POE qw(Component::IRC Wheel::FollowTail); +use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP); +use HTTP::Status; use DBI; use Encode qw/from_to/; @@ -77,6 +80,12 @@ my @messages = get_from_log( limit => 42, search => '%what to stuff in ilike%', + fmt => { + time => '{%s} ', + time_channel => '{%s %s} ', + nick => '%s: ', + message => '%s', + }, ); =cut @@ -86,6 +95,13 @@ $args->{limit} ||= 10; + $args->{fmt} ||= { + time => '{%s} ', + time_channel => '{%s %s} ', + nick => '%s: ', + message => '%s', + }; + my $sql = qq{ select time::date as date, @@ -130,15 +146,26 @@ my $msg = ''; - $msg .= "($t"; - $msg .= ' ' . $row->{channel} if ($last_row->{channel} ne $row->{channel}); - $msg .= ") "; + if ($last_row->{channel} ne $row->{channel}) { + $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); + } else { + $msg .= sprintf($args->{fmt}->{time}, $t); + } - $msg .= $row->{nick} . ': ' if ($last_row->{nick} ne $row->{nick}); + my $append = 1; - $msg .= $row->{message}; + if ($last_row->{nick} ne $row->{nick}) { + $msg .= sprintf($args->{fmt}->{nick}, $row->{nick}); + $append = 0; + } - push @msgs, $msg; + $msg .= sprintf($args->{fmt}->{message}, $row->{message}); + + if ($append && @msgs) { + $msgs[$#msgs] .= " " . $msg; + } else { + push @msgs, $msg; + } $last_row = $row; } @@ -171,7 +198,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); @@ -180,7 +207,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; @@ -215,7 +242,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 ); } @@ -227,7 +254,7 @@ 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 ); } @@ -237,7 +264,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 ); } @@ -350,4 +377,41 @@ }, ); +# http server + +my $httpd = POE::Component::Server::HTTP->new( + Port => $NICK =~ m/-dev/ ? 8001 : 8000, + ContentHandler => { '/' => \&root_handler }, + Headers => { Server => 'irc-logger' }, +); + +my $style = <<'_END_OF_STYLE_'; +.time, .channel { color: #808080; font-size: 60%; } +.nick { color: #0000ff; font-size: 80%; } +.message { color: #000000; font-size: 100%; } +_END_OF_STYLE_ + +sub root_handler { + my ($request, $response) = @_; + $response->code(RC_OK); + $response->content_type("text/html; charset=$ENCODING"); + $response->content( + qq{$NICK} . + "irc-logger url: " . $request->uri . '
' . + join("
", + get_from_log( + limit => 100, + fmt => { + time => '%s ', + time_channel => '%s %s ', + nick => '%s: ', + message => '%s', + }, + ) + ) . + qq{} + ); + return RC_OK; +} + POE::Kernel->run;