--- trunk/irc-logger.pl 2006/03/13 21:02:16 19
+++ trunk/irc-logger.pl 2006/06/25 00:10:13 35
@@ -18,13 +18,17 @@
## CONFIG
-my $NICK = 'irc-logger-dev';
+my $HOSTNAME = `hostname`;
+
+my $NICK = 'irc-logger';
+$NICK .= '-dev' if ($HOSTNAME =~ m/llin/);
my $CONNECT =
{Server => 'irc.freenode.net',
Nick => $NICK,
Ircname => "try /msg $NICK help",
};
my $CHANNEL = '#razmjenavjestina';
+$CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);
my $IRC_ALIAS = "log";
my %FOLLOWS =
@@ -36,6 +40,7 @@
my $DSN = 'DBI:Pg:dbname=' . $NICK;
my $ENCODING = 'ISO-8859-2';
+my $TIMESTAMP = '%Y-%m-%d %H:%M:%S';
## END CONFIG
@@ -44,10 +49,12 @@
use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);
use HTTP::Status;
use DBI;
-use Encode qw/from_to/;
+use Encode qw/from_to is_utf8/;
use Regexp::Common qw /URI/;
use CGI::Simple;
-
+use HTML::TagCloud;
+use POSIX qw/strftime/;
+use HTML::CalendarMonthSimple;
my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
@@ -82,6 +89,9 @@
values (?,?,?,?)
});
+my $tags;
+my $tag_regex = '\b([\w-_]+)//';
+
=head2 get_from_log
my @messages = get_from_log(
@@ -91,45 +101,76 @@
time => '{%s} ',
time_channel => '{%s %s} ',
nick => '%s: ',
+ me_nick => '***%s ',
message => '%s',
},
- message_filter => sub {
- # modify message content
- return shift;
- }
+ filter => {
+ message => sub {
+ # modify message content
+ return shift;
+ }
+ },
+ context => 5,
);
+Order is important. Fields are first passed through C (if available) and
+then throgh C<< sprintf($fmt->{message}, $message >> if available.
+
+C defines number of messages around each search hit for display.
+
=cut
sub get_from_log {
my $args = {@_};
- $args->{limit} ||= 10;
-
$args->{fmt} ||= {
+ date => '[%s] ',
time => '{%s} ',
time_channel => '{%s %s} ',
nick => '%s: ',
+ me_nick => '***%s ',
message => '%s',
};
- my $sql = qq{
+ my $sql_message = qq{
select
time::date as date,
time::time as time,
channel,
+ me,
nick,
message
from log
};
- $sql .= " where message ilike ? " if ($args->{search});
+
+ my $sql_context = qq{
+ select
+ id
+ from log
+ };
+
+ my $context = $1 if ($args->{search} && $args->{search} =~ s/\s*\+(\d+)\s*/ /);
+
+ 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};
+ $sql .= " limit " . $args->{limit} if ($args->{limit});
my $sth = $dbh->prepare( $sql );
- if ($args->{search}) {
- $sth->execute( '%' . $args->{search} . '%' );
- warn "search for '$args->{search}' returned ", $sth->rows, " results\n";
+ if (my $search = $args->{search}) {
+ $search =~ s/^\s+//;
+ $search =~ s/\s+$//;
+ $sth->execute( ( '%' . $search . '%' ) x 2 );
+ warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";
+ } elsif (my $tag = $args->{tag}) {
+ $sth->execute();
+ warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";
+ } elsif (my $date = $args->{date}) {
+ $sth->execute($date);
+ warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n";
} else {
$sth->execute();
}
@@ -150,37 +191,82 @@
"Showing " . ($#rows + 1) . " messages..."
);
+ if ($context) {
+ my @ids = @rows;
+ @rows = ();
+
+ my $last_to = 0;
+
+ my $sth = $dbh->prepare( $sql_message . qq{ where id >= ? and id < ? } );
+ foreach my $row_id (sort { $a->{id} <=> $b->{id} } @ids) {
+ my $id = $row_id->{id} || die "can't find id in row";
+
+ my ($from, $to) = ($id - $context, $id + $context);
+ $from = $last_to if ($from < $last_to);
+ $last_to = $to;
+ $sth->execute( $from, $to );
+
+ #warn "## id: $id from: $from to: $to returned: ", $sth->rows, "\n";
+
+ while (my $row = $sth->fetchrow_hashref) {
+ push @rows, $row;
+ }
+
+ }
+ }
+
+ # sprintf which can take coderef as first parametar
+ sub cr_sprintf {
+ my $fmt = shift || return;
+ if (ref($fmt) eq 'CODE') {
+ $fmt->(@_);
+ } else {
+ sprintf($fmt, @_);
+ }
+ }
+
foreach my $row (@rows) {
$row->{time} =~ s#\.\d+##;
- my $t;
- $t = $row->{date} . ' ' if ($last_row->{date} ne $row->{date});
- $t .= $row->{time};
-
my $msg = '';
+ $msg = cr_sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date});
+ my $t = $row->{time};
+
if ($last_row->{channel} ne $row->{channel}) {
- $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
+ $msg .= cr_sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
} else {
- $msg .= sprintf($args->{fmt}->{time}, $t);
+ $msg .= cr_sprintf($args->{fmt}->{time}, $t);
}
my $append = 1;
- if ($last_row->{nick} ne $row->{nick}) {
- $msg .= sprintf($args->{fmt}->{nick}, $row->{nick});
+ my $nick = $row->{nick};
+ if ($nick =~ s/^_*(.*?)_*$/$1/) {
+ $row->{nick} = $nick;
+ }
+
+ if ($last_row->{nick} ne $nick) {
+ # obfu way to find format for me_nick if needed or fallback to default
+ my $fmt = $row->{me} ? ( $args->{fmt}->{me_nick} || $args->{fmt}->{nick} ) : $args->{fmt}->{nick};
+ $fmt ||= '%s';
+
+ $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE');
+
+ $msg .= cr_sprintf( $fmt, $nick );
$append = 0;
}
- if (ref($args->{message_filter}) eq 'CODE') {
- $msg .= sprintf($args->{fmt}->{message},
- $args->{message_filter}->(
+ $args->{fmt}->{message} ||= '%s';
+ if (ref($args->{filter}->{message}) eq 'CODE') {
+ $msg .= cr_sprintf($args->{fmt}->{message},
+ $args->{filter}->{message}->(
$row->{message}
)
);
} else {
- $msg .= sprintf($args->{fmt}->{message}, $row->{message});
+ $msg .= cr_sprintf($args->{fmt}->{message}, $row->{message});
}
if ($append && @msgs) {
@@ -201,8 +287,7 @@
POE::Component::IRC->new($IRC_ALIAS);
-POE::Session->create
- (inline_states =>
+POE::Session->create( inline_states =>
{_start => sub {
$_[KERNEL]->post($IRC_ALIAS => register => 'all');
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
@@ -224,6 +309,8 @@
print "$channel: <$nick> $msg\n";
$sth->execute($channel, 0, $nick, $msg);
+ add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
+ message => $msg);
},
irc_ctcp_action => sub {
my $kernel = $_[KERNEL];
@@ -235,6 +322,8 @@
print "$channel ***$nick $msg\n";
$sth->execute($channel, 1, $nick, $msg);
+ add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
+ message => $msg);
},
irc_msg => sub {
my $kernel = $_[KERNEL];
@@ -281,11 +370,14 @@
$res = '';
- } elsif ($msg =~ m/^(search|grep)\s+(.*)$/i) {
+ } elsif ($msg =~ m/^(search|grep)\s+(.*)\s*$/i) {
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 );
@@ -321,30 +413,13 @@
# warn "## indetify $NICK\n";
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
# },
- irc_372 => sub {
- print "MOTD: ", $_[ARG1], "\n";
- },
- irc_snotice => sub {
- print "(server notice): ", $_[ARG0], "\n";
- },
- (map
- {
- ;"irc_$_" => sub { }}
- qw(
- )),
-# join
-# ctcp_version
-# connected snotice ctcp_action ping notice mode part quit
-# 001 002 003 004 005
-# 250 251 252 253 254 265 266
-# 332 333 353 366 372 375 376
-# 477
_child => sub {},
_default => sub {
- printf "%s: session %s caught an unhandled %s event.\n",
- scalar localtime(), $_[SESSION]->ID, $_[ARG0];
- print "The $_[ARG0] event was given these parameters: ",
- join(" ", map({"ARRAY" eq ref $_ ? "[@$_]" : "$_"} @{$_[ARG1]})), "\n";
+ printf "%s #%s %s %s\n",
+ strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0],
+ ref($_[ARG1]) eq "ARRAY" ? join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] }) :
+ $_[ARG1] ? $_[ARG1] :
+ "";
0; # false for signals
},
my_add => sub {
@@ -410,6 +485,53 @@
},
);
+# tags support
+
+my $cloud = HTML::TagCloud->new;
+
+=head2 add_tag
+
+ add_tag( id => 42, message => 'irc message' );
+
+=cut
+
+sub add_tag {
+ my $arg = {@_};
+
+ return unless ($arg->{id} && $arg->{message});
+
+ my $m = $arg->{message};
+ from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));
+
+ while ($m =~ s#$tag_regex##s) {
+ my $tag = $1;
+ next if (! $tag || $tag =~ m/https?:/i);
+ push @{ $tags->{$tag} }, $arg->{id};
+ #warn "+tag $tag: $arg->{id}\n";
+ $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
+ }
+}
+
+=head2 seed_tags
+
+Read all tags from database and create in-memory cache for tags
+
+=cut
+
+sub seed_tags {
+ my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' });
+ $sth->execute;
+ while (my $row = $sth->fetchrow_hashref) {
+ add_tag( %$row );
+ }
+
+ foreach my $tag (keys %$tags) {
+ $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);
+ }
+}
+
+seed_tags;
+
# http server
my $httpd = POE::Component::Server::HTTP->new(
@@ -424,11 +546,24 @@
my $style = <<'_END_OF_STYLE_';
p { margin: 0; padding: 0.1em; }
.time, .channel { color: #808080; font-size: 60%; }
-.nick { color: #0000ff; font-size: 80%; }
+.date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; }
+.nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }
.message { color: #000000; font-size: 100%; }
.search { float: right; }
+.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; }
_END_OF_STYLE_
+my $max_color = 4;
+
+my %nick_enumerator;
+
sub root_handler {
my ($request, $response) = @_;
$response->code(RC_OK);
@@ -446,34 +581,90 @@
my $search = $q->param('search') || $q->param('grep') || '';
- $response->content(
- qq{$NICK
-
-
} .
- join("
",
+ $cloud->html(500) .
+ qq{
};
+ if ($request->url =~ m#/history#) {
+ my $sth = $dbh->prepare(qq{
+ select date(time) as date,count(*) as nr
+ from log
+ group by date(time)
+ order by date(time) desc
+ });
+ $sth->execute();
+ my ($l_yyyy,$l_mm) = (0,0);
+ my $cal;
+ 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);
+ $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);
+ $cal->border(2);
+ ($l_yyyy,$l_mm) = ($yyyy,$mm);
+ }
+ $cal->setcontent($dd, qq{
+ $row->{nr}
+ });
+ }
+ $html .= $cal->as_HTML() if ($cal);
+
+ } else {
+ $html .= join("
",
get_from_log(
- limit => $q->param('limit') || 100,
- search => $q->param('search') || $q->param('grep') || undef,
+ limit => $q->param('last') || $q->param('date') ? undef : 100,
+ search => $search || undef,
+ tag => $q->param('tag') || undef,
+ date => $q->param('date') || undef,
fmt => {
+ date => sub {
+ my $date = shift || return;
+ qq{
'};
+ },
time => '%s ',
time_channel => '%s %s ',
- nick => '%s: ',
+ nick => '%s: ',
+ me_nick => '***%s ',
message => '%s',
},
- message_filter => sub {
- my $m = shift || return;
- $m =~ s/($escape_re)/$escape{$1}/gs;
- $m =~ s#($RE{URI}{HTTP})#$1#gs;
- return $m;
+ filter => {
+ message => sub {
+ my $m = shift || return;
+ $m =~ s/($escape_re)/$escape{$1}/gs;
+ $m =~ s#($RE{URI}{HTTP})#$1#gs;
+ $m =~ s#$tag_regex#$1#g;
+ return $m;
+ },
+ nick => sub {
+ my $n = shift || return;
+ if (! $nick_enumerator{$n}) {
+ my $max = scalar keys %nick_enumerator;
+ $nick_enumerator{$n} = $max + 1;
+ }
+ return '' . $n . '';
+ },
},
)
- ) .
- qq{
}
- );
+ );
+ }
+
+ $html .= qq{
+
+ See history of all messages.
+ };
+
+ $response->content( $html );
return RC_OK;
}