--- trunk/irc-logger.pl 2006/03/13 17:07:40 18 +++ trunk/irc-logger.pl 2006/06/25 17:40:59 37 @@ -10,6 +10,14 @@ ./irc-logger.pl +=head2 Options + +=over 4 + +=item --import-dircproxy=filename + +Import log from C to C database + =head1 DESCRIPTION log all conversation on irc channel @@ -18,13 +26,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 = @@ -33,9 +45,10 @@ ERROR => "/var/log/apache/error.log", ); -my $DSN = 'DBI:Pg:dbname=irc-logger'; +my $DSN = 'DBI:Pg:dbname=' . $NICK; my $ENCODING = 'ISO-8859-2'; +my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; ## END CONFIG @@ -44,20 +57,35 @@ 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; +use Getopt::Long; +use DateTime; + +my $import_dircproxy; +GetOptions( + 'import-dircproxy:s' => \$import_dircproxy, +); 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) @@ -67,16 +95,18 @@ 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, time) +values (?,?,?,?,?) }); +my $tags; +my $tag_regex = '\b([\w-_]+)//'; + =head2 get_from_log my @messages = get_from_log( @@ -86,45 +116,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(); } @@ -145,37 +206,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) { @@ -190,14 +296,132 @@ return @msgs; } +# 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; + + +=head2 save_message + + save_message( + channel => '#foobar', + me => 0, + nick => 'dpavlin', + msg => 'test message', + time => '2006-06-25 18:57:18', + ); + +C