--- trunk/irc-logger.pl 2006/03/01 22:42:21 8
+++ trunk/irc-logger.pl 2006/06/25 17:48:33 38
@@ -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,26 +45,47 @@
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
-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/;
-
+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)
@@ -62,33 +95,344 @@
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(
+ limit => 42,
+ search => '%what to stuff in ilike%',
+ fmt => {
+ time => '{%s} ',
+ time_channel => '{%s %s} ',
+ nick => '%s: ',
+ me_nick => '***%s ',
+ message => '%s',
+ },
+ 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->{fmt} ||= {
+ date => '[%s] ',
+ time => '{%s} ',
+ time_channel => '{%s %s} ',
+ nick => '%s: ',
+ me_nick => '***%s ',
+ message => '%s',
+ };
+
+ my $sql_message = qq{
+ select
+ time::date as date,
+ time::time as time,
+ channel,
+ me,
+ nick,
+ message
+ from log
+ };
+
+ 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} if ($args->{limit});
+
+ my $sth = $dbh->prepare( $sql );
+ 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();
+ }
+ my $last_row = {
+ date => '',
+ time => '',
+ channel => '',
+ nick => '',
+ };
+
+ my @rows;
+
+ while (my $row = $sth->fetchrow_hashref) {
+ unshift @rows, $row;
+ }
+
+ my @msgs = (
+ "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 $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 .= cr_sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
+ } else {
+ $msg .= cr_sprintf($args->{fmt}->{time}, $t);
+ }
+
+ my $append = 1;
+
+ 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;
+ }
+
+ $args->{fmt}->{message} ||= '%s';
+ if (ref($args->{filter}->{message}) eq 'CODE') {
+ $msg .= cr_sprintf($args->{fmt}->{message},
+ $args->{filter}->{message}->(
+ $row->{message}
+ )
+ );
+ } else {
+ $msg .= cr_sprintf($args->{fmt}->{message}, $row->{message});
+ }
+
+ if ($append && @msgs) {
+ $msgs[$#msgs] .= " " . $msg;
+ } else {
+ push @msgs, $msg;
+ }
+
+ $last_row = $row;
+ }
+
+ 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
+
+ See history of all messages.
+ };
+
+ $response->content( $html );
+ return RC_OK;
+}
+
POE::Kernel->run;