--- trunk/irc-logger.pl 2006/06/24 22:15:47 33 +++ trunk/irc-logger.pl 2006/10/24 12:51:49 41 @@ -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 @@ -40,6 +48,9 @@ my $DSN = 'DBI:Pg:dbname=' . $NICK; my $ENCODING = 'ISO-8859-2'; +my $TIMESTAMP = '%Y-%m-%d %H:%M:%S'; + +my $sleep_on_error = 5; ## END CONFIG @@ -52,6 +63,15 @@ 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; @@ -82,8 +102,8 @@ my $sth = $dbh->prepare(qq{ insert into log - (channel, me, nick, message) -values (?,?,?,?) + (channel, me, nick, message, time) +values (?,?,?,?,?) }); my $tags; @@ -120,8 +140,6 @@ sub get_from_log { my $args = {@_}; - $args->{limit} ||= 10; - $args->{fmt} ||= { date => '[%s] ', time => '{%s} ', @@ -154,8 +172,9 @@ $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 (my $search = $args->{search}) { @@ -166,6 +185,9 @@ } 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(); } @@ -210,19 +232,29 @@ } } + # 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 $msg = ''; - $msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date}); + $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; @@ -239,19 +271,19 @@ $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); - $msg .= sprintf( $fmt, $nick ); + $msg .= cr_sprintf( $fmt, $nick ); $append = 0; } $args->{fmt}->{message} ||= '%s'; if (ref($args->{filter}->{message}) eq 'CODE') { - $msg .= sprintf($args->{fmt}->{message}, + $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) { @@ -266,14 +298,133 @@ 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