18 |
|
|
19 |
## CONFIG |
## CONFIG |
20 |
|
|
21 |
my $NICK = 'irc-logger'; |
my $NICK = 'irc-logger-dev'; |
22 |
my $CONNECT = |
my $CONNECT = |
23 |
{Server => 'irc.freenode.net', |
{Server => 'irc.freenode.net', |
24 |
Nick => $NICK, |
Nick => $NICK, |
45 |
use HTTP::Status; |
use HTTP::Status; |
46 |
use DBI; |
use DBI; |
47 |
use Encode qw/from_to/; |
use Encode qw/from_to/; |
48 |
|
use Regexp::Common qw /URI/; |
49 |
|
use CGI::Simple; |
50 |
|
|
51 |
|
|
52 |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; |
88 |
nick => '%s: ', |
nick => '%s: ', |
89 |
message => '%s', |
message => '%s', |
90 |
}, |
}, |
91 |
|
message_filter => sub { |
92 |
|
# modify message content |
93 |
|
return shift; |
94 |
|
} |
95 |
); |
); |
96 |
|
|
97 |
=cut |
=cut |
123 |
|
|
124 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
125 |
if ($args->{search}) { |
if ($args->{search}) { |
126 |
$sth->execute( $args->{search} ); |
$sth->execute( '%' . $args->{search} . '%' ); |
127 |
|
warn "search for '$args->{search}' returned ", $sth->rows, " results\n"; |
128 |
} else { |
} else { |
129 |
$sth->execute(); |
$sth->execute(); |
130 |
} |
} |
141 |
unshift @rows, $row; |
unshift @rows, $row; |
142 |
} |
} |
143 |
|
|
144 |
my @msgs; |
my @msgs = ( |
145 |
|
"Showing " . ($#rows + 1) . " messages..." |
146 |
|
); |
147 |
|
|
148 |
foreach my $row (@rows) { |
foreach my $row (@rows) { |
149 |
|
|
168 |
$append = 0; |
$append = 0; |
169 |
} |
} |
170 |
|
|
171 |
$msg .= sprintf($args->{fmt}->{message}, $row->{message}); |
if (ref($args->{message_filter}) eq 'CODE') { |
172 |
|
$msg .= sprintf($args->{fmt}->{message}, |
173 |
|
$args->{message_filter}->( |
174 |
|
$row->{message} |
175 |
|
) |
176 |
|
); |
177 |
|
} else { |
178 |
|
$msg .= sprintf($args->{fmt}->{message}, $row->{message}); |
179 |
|
} |
180 |
|
|
181 |
if ($append && @msgs) { |
if ($append && @msgs) { |
182 |
$msgs[$#msgs] .= " " . $msg; |
$msgs[$#msgs] .= " " . $msg; |
269 |
|
|
270 |
my $what = $2; |
my $what = $2; |
271 |
|
|
272 |
foreach my $res (get_from_log( limit => 20, search => "%${what}%" )) { |
foreach my $res (get_from_log( limit => 20, search => $what )) { |
273 |
print "search [$what]: $res\n"; |
print "search [$what]: $res\n"; |
274 |
from_to($res, $ENCODING, 'UTF-8'); |
from_to($res, $ENCODING, 'UTF-8'); |
275 |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res ); |
402 |
Headers => { Server => 'irc-logger' }, |
Headers => { Server => 'irc-logger' }, |
403 |
); |
); |
404 |
|
|
405 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
406 |
|
my $escape_re = join '|' => keys %escape; |
407 |
|
|
408 |
my $style = <<'_END_OF_STYLE_'; |
my $style = <<'_END_OF_STYLE_'; |
409 |
|
p { margin: 0; padding: 0.1em; } |
410 |
.time, .channel { color: #808080; font-size: 60%; } |
.time, .channel { color: #808080; font-size: 60%; } |
411 |
.nick { color: #0000ff; font-size: 80%; } |
.nick { color: #0000ff; font-size: 80%; } |
412 |
.message { color: #000000; font-size: 100%; } |
.message { color: #000000; font-size: 100%; } |
413 |
|
.search { float: right; } |
414 |
_END_OF_STYLE_ |
_END_OF_STYLE_ |
415 |
|
|
416 |
sub root_handler { |
sub root_handler { |
417 |
my ($request, $response) = @_; |
my ($request, $response) = @_; |
418 |
$response->code(RC_OK); |
$response->code(RC_OK); |
419 |
$response->content_type("text/html; charset=$ENCODING"); |
$response->content_type("text/html; charset=$ENCODING"); |
420 |
|
|
421 |
|
my $q; |
422 |
|
|
423 |
|
if ( $request->method eq 'POST' ) { |
424 |
|
$q = new CGI::Simple( $request->content ); |
425 |
|
} elsif ( $request->uri =~ /\?(.+)$/ ) { |
426 |
|
$q = new CGI::Simple( $1 ); |
427 |
|
} else { |
428 |
|
$q = new CGI::Simple; |
429 |
|
} |
430 |
|
|
431 |
|
my $search = $q->param('search') || $q->param('grep') || ''; |
432 |
|
|
433 |
$response->content( |
$response->content( |
434 |
qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body>} . |
qq{<html><head><title>$NICK</title><style type="text/css">$style</style></head><body> |
435 |
"irc-logger url: " . $request->uri . '<br/>' . |
<form method="post" class="search"> |
436 |
join("<br/>", |
<input type="text" name="search" value="$search" size="10"> |
437 |
|
<input type="submit" value="search"> |
438 |
|
</form> |
439 |
|
<p> |
440 |
|
} . |
441 |
|
join("</p><p>", |
442 |
get_from_log( |
get_from_log( |
443 |
limit => 100, |
limit => $q->param('limit') || 100, |
444 |
|
search => $q->param('search') || $q->param('grep') || undef, |
445 |
fmt => { |
fmt => { |
446 |
time => '<span class="time">%s</span> ', |
time => '<span class="time">%s</span> ', |
447 |
time_channel => '<span class="channel">%s %s</span> ', |
time_channel => '<span class="channel">%s %s</span> ', |
448 |
nick => '<span class="nick">%s:</span> ', |
nick => '<span class="nick">%s:</span> ', |
449 |
message => '<span class="message">%s</span>', |
message => '<span class="message">%s</span>', |
450 |
}, |
}, |
451 |
|
message_filter => sub { |
452 |
|
my $m = shift || return; |
453 |
|
$m =~ s/($escape_re)/$escape{$1}/gs; |
454 |
|
$m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs; |
455 |
|
return $m; |
456 |
|
}, |
457 |
) |
) |
458 |
) . |
) . |
459 |
qq{</body></html>} |
qq{</p></body></html>} |
460 |
); |
); |
461 |
return RC_OK; |
return RC_OK; |
462 |
} |
} |