--- trunk/irc-logger.pl 2007/02/02 21:37:52 42
+++ trunk/bin/irc-logger.pl 2008/02/29 22:11:07 83
@@ -18,6 +18,12 @@
Import log from C to C database
+=item --log=irc-logger.log
+
+Name of log file
+
+=back
+
=head1 DESCRIPTION
log all conversation on irc channel
@@ -26,7 +32,8 @@
## CONFIG
-my $HOSTNAME = `hostname`;
+my $HOSTNAME = `hostname -f`;
+chomp($HOSTNAME);
my $NICK = 'irc-logger';
$NICK .= '-dev' if ($HOSTNAME =~ m/llin/);
@@ -52,6 +59,13 @@
my $sleep_on_error = 5;
+# number of last tags to keep in circular buffer
+my $last_x_tags = 50;
+
+my $http_port = $NICK =~ m/-dev/ ? 8001 : 8000;
+
+my $url = "http://$HOSTNAME:$http_port";
+
## END CONFIG
@@ -67,22 +81,80 @@
use HTML::CalendarMonthSimple;
use Getopt::Long;
use DateTime;
+use URI::Escape;
+use Data::Dump qw/dump/;
+use DateTime::Format::ISO8601;
+use Carp qw/confess/;
+use XML::Feed;
+use DateTime::Format::Flexible;
+
+my $use_twitter = 1;
+eval { require Net::Twitter; };
+$use_twitter = 0 if ($@);
my $import_dircproxy;
+my $log_path;
GetOptions(
'import-dircproxy:s' => \$import_dircproxy,
+ 'log:s' => \$log_path,
);
-my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
+$SIG{__DIE__} = sub {
+ confess "fatal error";
+};
+
+open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
+
+sub _log {
+ print strftime($TIMESTAMP,localtime()), ' ', join(" ",@_), $/;
+}
+
+# HTML formatters
+
+my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
+my $escape_re = join '|' => keys %escape;
-eval {
- $dbh->do(qq{ select count(*) from log });
+my $tag_regex = '\b([\w-_]+)//';
+
+my %nick_enumerator;
+my $max_color = 0;
+
+my $filter = {
+ message => sub {
+ my $m = shift || return;
+
+ # protect HTML from wiki modifications
+ sub e {
+ my $t = shift;
+ return 'uri_unescape{' . uri_escape($t) . '}';
+ }
+
+ $m =~ s/($escape_re)/$escape{$1}/gs;
+ $m =~ s#($RE{URI}{HTTP})#e(qq{$1})#egs ||
+ $m =~ s#\/(\w+)\/#$1#gs;
+ $m =~ s#$tag_regex#e(qq{$1})#egs;
+ $m =~ s#\*(\w+)\*#$1#gs;
+ $m =~ s#_(\w+)_#$1#gs;
+
+ $m =~ s#uri_unescape{([^}]+)}#uri_unescape($1)#egs;
+ 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 . '';
+ },
};
-if ($@) {
- warn "creating database table in $DSN\n";
- $dbh->do(<<'_SQL_SCHEMA_');
+my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
+my $sql_schema = {
+ log => '
create table log (
id serial,
time timestamp default now(),
@@ -96,18 +168,88 @@
create index log_time on log(time);
create index log_channel on log(channel);
create index log_nick on log(nick);
+ ',
+ meta => '
+create table meta (
+ nick text not null,
+ channel text not null,
+ name text not null,
+ value text,
+ changed timestamp default now(),
+ primary key(nick,channel,name)
+);
+ ',
+};
-_SQL_SCHEMA_
+foreach my $table ( keys %$sql_schema ) {
+
+ eval {
+ $dbh->do(qq{ select count(*) from $table });
+ };
+
+ if ($@) {
+ warn "creating database table $table in $DSN\n";
+ $dbh->do( $sql_schema->{ $table } );
+ }
}
+
+=head2 meta
+
+Set or get some meta data into database
+
+ meta('nick','channel','var_name', $var_value );
+
+ $var_value = meta('nick','channel','var_name');
+ ( $var_value, $changed ) = meta('nick','channel','var_name');
+
+=cut
+
+sub meta {
+ my ($nick,$channel,$name,$value) = @_;
+
+ # normalize channel name
+ $channel =~ s/^#//;
+
+ if (defined($value)) {
+
+ my $sth = $dbh->prepare(qq{ update meta set value = ?, changed = now() where nick = ? and channel = ? and name = ? });
+
+ eval { $sth->execute( $value, $nick, $channel, $name ) };
+
+ # error or no result
+ if ( $@ || ! $sth->rows ) {
+ $sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) });
+ $sth->execute( $value, $nick, $channel, $name );
+ _log "created $nick/$channel/$name = $value";
+ } else {
+ _log "updated $nick/$channel/$name = $value ";
+ }
+
+ return $value;
+
+ } else {
+
+ my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? });
+ $sth->execute( $nick, $channel, $name );
+ my ($v,$c) = $sth->fetchrow_array;
+ _log "fetched $nick/$channel/$name = $v [$c]";
+ return ($v,$c) if wantarray;
+ return $v;
+
+ }
+}
+
+
+
my $sth = $dbh->prepare(qq{
insert into log
(channel, me, nick, message, time)
values (?,?,?,?,?)
});
+
my $tags;
-my $tag_regex = '\b([\w-_]+)//';
=head2 get_from_log
@@ -144,14 +286,16 @@
sub get_from_log {
my $args = {@_};
- $args->{fmt} ||= {
- date => '[%s] ',
- time => '{%s} ',
- time_channel => '{%s %s} ',
- nick => '%s: ',
- me_nick => '***%s ',
- message => '%s',
- };
+ if ( ! $args->{fmt} ) {
+ $args->{fmt} = {
+ date => '[%s] ',
+ time => '{%s} ',
+ time_channel => '{%s %s} ',
+ nick => '%s: ',
+ me_nick => '***%s ',
+ message => '%s',
+ };
+ }
my $sql_message = qq{
select
@@ -174,27 +318,50 @@
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});
+ sub check_date {
+ my $date = shift || return;
+ my $new_date = eval { DateTime::Format::ISO8601->parse_datetime( $date )->ymd; };
+ if ( $@ ) {
+ warn "invalid date $date\n";
+ $new_date = DateTime->now->ymd;
+ }
+ return $new_date;
+ }
+
+ my @where;
+ my @args;
- 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();
+ push @where, 'message ilike ? or nick ilike ?';
+ push @args, ( ( '%' . $search . '%' ) x 2 );
+ _log "search for '$search'";
+ }
+
+ if ($args->{tag} && $tags->{ $args->{tag} }) {
+ push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')';
+ _log "search for tags $args->{tag}";
+ }
+
+ if (my $date = $args->{date} ) {
+ $date = check_date( $date );
+ push @where, 'date(time) = ?';
+ push @args, $date;
+ _log "search for date $date";
}
+
+ $sql .= " where " . join(" and ", @where) if @where;
+
+ $sql .= " order by log.time desc";
+ $sql .= " limit " . $args->{limit} if ($args->{limit});
+
+ #warn "### sql: $sql ", dump( @args );
+
+ my $sth = $dbh->prepare( $sql );
+ eval { $sth->execute( @args ) };
+ return if $@;
+
my $last_row = {
date => '',
time => '',
@@ -315,10 +482,12 @@
=head2 add_tag
- add_tag( id => 42, message => 'irc message' );
+ add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] );
=cut
+my @last_tags;
+
sub add_tag {
my $arg = {@_};
@@ -327,13 +496,23 @@
my $m = $arg->{message};
from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));
+ my @tags;
+
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);
+ $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1);
+ push @tags, $tag;
+
+ }
+
+ if ( @tags ) {
+ pop @last_tags if $#last_tags == $last_x_tags;
+ unshift @last_tags, { tags => [ @tags ], %$arg };
}
+
}
=head2 seed_tags
@@ -343,14 +522,14 @@
=cut
sub seed_tags {
- my $sth = $dbh->prepare(qq{ select id,message from log where message like '%//%' });
+ my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc });
$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);
+ $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1);
}
}
@@ -363,7 +542,7 @@
channel => '#foobar',
me => 0,
nick => 'dpavlin',
- msg => 'test message',
+ message => 'test message',
time => '2006-06-25 18:57:18',
);
@@ -375,26 +554,26 @@
sub save_message {
my $a = {@_};
+ confess "have msg" if $a->{msg};
$a->{me} ||= 0;
$a->{time} ||= strftime($TIMESTAMP,localtime());
- print
- $a->{time}, " ",
+ _log
$a->{channel}, " ",
$a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",
- " " . $a->{msg} . "\n";
+ " " . $a->{message};
- from_to($a->{msg}, 'UTF-8', $ENCODING);
+ from_to($a->{message}, 'UTF-8', $ENCODING);
- $sth->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{msg}, $a->{time});
- add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef),
- message => $a->{msg});
+ $sth->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time});
+ add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );
}
+
if ($import_dircproxy) {
open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";
warn "importing $import_dircproxy...\n";
- my $tz_offset = 2 * 60 * 60; # TZ GMT+2
+ my $tz_offset = 1 * 60 * 60; # TZ GMT+2
while(<$l>) {
chomp;
if (/^@(\d+)\s(\S+)\s(.+)$/) {
@@ -412,12 +591,12 @@
channel => $CHANNEL,
me => $me,
nick => $nick,
- msg => $msg,
+ message => $msg,
time => $dt->ymd . " " . $dt->hms,
) if ($nick !~ m/^-/);
} else {
- warn "can't parse: $_\n";
+ _log "can't parse: $_";
}
}
close($l);
@@ -432,6 +611,7 @@
my $SKIPPING = 0; # if skipping, how many we've done
my $SEND_QUEUE; # cache
+my $ping; # ping stats
POE::Component::IRC->new($IRC_ALIAS);
@@ -453,7 +633,8 @@
my $channel = $_[ARG1]->[0];
my $msg = $_[ARG2];
- save_message( channel => $channel, me => 0, nick => $nick, msg => $msg);
+ save_message( channel => $channel, me => 0, nick => $nick, message => $msg);
+ meta( $nick, $channel, 'last-msg', $msg );
},
irc_ctcp_action => sub {
my $kernel = $_[KERNEL];
@@ -461,18 +642,44 @@
my $channel = $_[ARG1]->[0];
my $msg = $_[ARG2];
- save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);
+ save_message( channel => $channel, me => 1, nick => $nick, message => $msg);
+
+ if ( $use_twitter ) {
+ if ( my $twitter = meta( $nick, $NICK, 'twitter' ) ) {
+ my ($login,$passwd) = split(/\s+/,$twitter,2);
+ _log("sending twitter for $nick/$login on $channel ");
+ my $bot = Net::Twitter->new( username=>$login, password=>$passwd );
+ $bot->update("<${channel}> $msg");
+ }
+ }
+
},
+ irc_ping => sub {
+ warn "pong ", $_[ARG0], $/;
+ $ping->{ $_[ARG0] }++;
+ },
+ irc_invite => sub {
+ my $kernel = $_[KERNEL];
+ my $nick = (split /!/, $_[ARG0])[0];
+ my $channel = $_[ARG1];
+
+ warn "invited to $channel by $nick";
+
+ $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, "how nice of you to invite me to $channel, I'll be right there..." );
+ $_[KERNEL]->post($IRC_ALIAS => join => $channel);
+
+ },
irc_msg => sub {
my $kernel = $_[KERNEL];
my $nick = (split /!/, $_[ARG0])[0];
my $msg = $_[ARG2];
+ my $channel = $_[ARG1]->[0];
from_to($msg, 'UTF-8', $ENCODING);
my $res = "unknown command '$msg', try /msg $NICK help!";
my @out;
- print "<< $msg\n";
+ _log "<< $msg";
if ($msg =~ m/^help/i) {
@@ -480,7 +687,7 @@
} elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
- print ">> /msg $1 $2\n";
+ _log ">> /msg $1 $2";
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
$res = '';
@@ -490,11 +697,11 @@
my $sth = $dbh->prepare(qq{
select
- nick,
+ trim(both '_' from nick) as nick,
count(*) as count,
sum(length(message)) as len
from log
- group by nick
+ group by trim(both '_' from nick)
order by len desc,count desc
limit $nr
});
@@ -507,8 +714,10 @@
$res .= join(" | ", @users);
} elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
- foreach my $res (get_from_log( limit => ($1 || 100) )) {
- print "last: $res\n";
+ my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10;
+
+ foreach my $res (get_from_log( limit => $limit )) {
+ _log "last: $res";
from_to($res, $ENCODING, 'UTF-8');
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
}
@@ -523,7 +732,7 @@
limit => 20,
search => $what,
)) {
- print "search [$what]: $res\n";
+ _log "search [$what]: $res";
from_to($res, $ENCODING, 'UTF-8');
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
}
@@ -549,45 +758,80 @@
}
my @nicks;
- foreach my $nick (sort { $stat->{from}->{$a} cmp $stat->{from}->{$b} } keys %{ $stat->{from} }) {
- push @nicks, $nick . $stat->{from}->{$nick} == 1 ? '' :
- "(" . $stat->{from}->{$nick} . ")";
+ foreach my $nick (sort { $stat->{from}->{$a} <=> $stat->{from}->{$b} } keys %{ $stat->{from} }) {
+ push @nicks, $nick . ( $stat->{from}->{$nick} == 1 ? '' :
+ "(" . $stat->{from}->{$nick} . ")"
+ );
}
$res =
- "+ " . ( $stat->{vote}->{'+'} || 0 ) . " : " .
- "- " . ( $stat->{vote}->{'-'} || 0 ) .
+ "$what ++ " . ( $stat->{vote}->{'+'} || 0 ) .
+ " : " . ( $stat->{vote}->{'-'} || 0 ) . " --" .
" from " . ( join(", ", @nicks) || 'nobody' );
+ $_[KERNEL]->post( $IRC_ALIAS => notice => $nick, $res );
+
+ } elsif ($msg =~ m/^ping/) {
+ $res = "ping = " . dump( $ping );
+ } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) {
+ if ( ! defined( $1 ) ) {
+ my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? });
+ $sth->execute( $nick, $channel );
+ $res = "config for $nick on $channel";
+ while ( my ($n,$v) = $sth->fetchrow_array ) {
+ $res .= " | $n = $v";
+ }
+ } elsif ( ! $2 ) {
+ my $val = meta( $nick, $channel, $1 );
+ $res = "current $1 = " . ( $val ? $val : 'undefined' );
+ } else {
+ my $validate = {
+ 'last-size' => qr/^\d+/,
+ 'twitter' => qr/^\w+\s+\w+/,
+ };
+
+ my ( $op, $val ) = ( $1, $2 );
+
+ if ( my $regex = $validate->{$op} ) {
+ if ( $val =~ $regex ) {
+ meta( $nick, $channel, $op, $val );
+ $res = "saved $op = $val";
+ } else {
+ $res = "config option $op = $val doesn't validate against $regex";
+ }
+ } else {
+ $res = "config option $op doesn't exist";
+ }
+ }
}
if ($res) {
- print ">> [$nick] $res\n";
+ _log ">> [$nick] $res";
from_to($res, $ENCODING, 'UTF-8');
$_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
}
},
irc_477 => sub {
- print "# irc_477: ",$_[ARG1], "\n";
+ _log "# irc_477: ",$_[ARG1];
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
},
irc_505 => sub {
- print "# irc_505: ",$_[ARG1], "\n";
+ _log "# irc_505: ",$_[ARG1];
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
# $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
},
irc_registered => sub {
- warn "## indetify $NICK\n";
+ _log "## registrated $NICK";
$_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
},
irc_disconnected => sub {
- warn "## disconnected, reconnecting again\n";
+ _log "## disconnected, reconnecting again";
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
},
irc_socketerr => sub {
- warn "## socket error... sleeping for $sleep_on_error seconds and retry";
+ _log "## socket error... sleeping for $sleep_on_error seconds and retry";
sleep($sleep_on_error);
$_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
},
@@ -598,8 +842,8 @@
# },
_child => sub {},
_default => sub {
- printf "%s #%s %s %s\n",
- strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0],
+ _log sprintf "sID:%s %s %s",
+ $_[SESSION]->ID, $_[ARG0],
ref($_[ARG1]) eq "ARRAY" ? join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] }) :
$_[ARG1] ? $_[ARG1] :
"";
@@ -671,14 +915,16 @@
# http server
my $httpd = POE::Component::Server::HTTP->new(
- Port => $NICK =~ m/-dev/ ? 8001 : 8000,
+ Port => $http_port,
+ PreHandler => {
+ '/' => sub {
+ $_[0]->header(Connection => 'close')
+ }
+ },
ContentHandler => { '/' => \&root_handler },
Headers => { Server => 'irc-logger' },
);
-my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
-my $escape_re = join '|' => keys %escape;
-
my $style = <<'_END_OF_STYLE_';
p { margin: 0; padding: 0.1em; }
.time, .channel { color: #808080; font-size: 60%; }
@@ -686,24 +932,44 @@
.nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }
.message { color: #000000; font-size: 100%; }
.search { float: right; }
+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; }
+/*
.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; }
+*/
+.calendar { border: 1px solid red; width: 100%; }
+.month { border: 0px; width: 100%; }
_END_OF_STYLE_
-my $max_color = 4;
+$max_color = 0;
-my %nick_enumerator;
+my @cols = qw(
+ #ffcccc #ccffe6 #ccccff #e6ccff #ffccff #ffcce6 #ff9999 #ffcc99 #ffff99
+ #ccff99 #99ff99 #99ffcc #99ccff #9999ff #cc99ff #ff6666 #ffb366 #ffff66
+ #66ff66 #66ffb3 #66b3ff #6666ff #ff3333 #33ff33 #3399ff #3333ff #ff3399
+ #a0a0a0 #ff0000 #ffff00 #80ff00 #0000ff #8000ff #ff00ff #ff0080 #994d00
+ #999900 #009900 #cc0066 #c0c0c0 #ccff99 #99ff33 #808080 #660033 #ffffff
+);
+
+foreach my $c (@cols) {
+ $style .= ".col-${max_color} { background: $c }\n";
+ $max_color++;
+}
+warn "defined $max_color colors for users...\n";
sub root_handler {
my ($request, $response) = @_;
$response->code(RC_OK);
- $response->content_type("text/html; charset=$ENCODING");
+
+ # this doesn't seem to work, so moved to PreHandler
+ #$response->header(Connection => 'close');
+
+ return RC_OK if $request->uri =~ m/favicon.ico$/;
my $q;
@@ -717,54 +983,153 @@
my $search = $q->param('search') || $q->param('grep') || '';
+ if ($request->url =~ m#/rss(?:/(tags|last-tag)\w*(?:=(\d+))?)?#i) {
+ my $show = lc($1);
+ my $nr = $2;
+
+ my $type = 'RSS'; # Atom
+
+ $response->content_type( 'application/' . lc($type) . '+xml' );
+
+ my $html = '';
+ #warn "create $type feed from ",dump( @last_tags );
+
+ my $feed = XML::Feed->new( $type );
+
+ if ( $show eq 'tags' ) {
+ $nr ||= 50;
+ $feed->title( "tags from $CHANNEL" );
+ $feed->link( "$url/tags" );
+ $feed->description( "tag cloud created from messages on channel $CHANNEL which have tags// in them" );
+ my $feed_entry = XML::Feed::Entry->new($type);
+ $feed_entry->title( "$nr tags from $CHANNEL" );
+ $feed_entry->author( $NICK );
+ $feed_entry->link( '/#tags' );
+
+ $feed_entry->content(
+ qq{}
+ . $cloud->css
+ . qq{}
+ . $cloud->html( $nr )
+ . qq{]]>}
+ );
+ $feed->add_entry( $feed_entry );
+
+ } elsif ( $show eq 'last-tag' ) {
+
+ $nr ||= $last_x_tags;
+ $nr = $last_x_tags if $nr > $last_x_tags;
+
+ $feed->title( "last $nr tagged messages from $CHANNEL" );
+ $feed->link( $url );
+ $feed->description( "collects messages which have tags// in them" );
+
+ foreach my $m ( @last_tags ) {
+# warn dump( $m );
+ #my $tags = join(' ', @{$m->{tags}} );
+ my $feed_entry = XML::Feed::Entry->new($type);
+ $feed_entry->title( $m->{nick} . '@' . $m->{time} );
+ $feed_entry->author( $m->{nick} );
+ $feed_entry->link( '/#' . $m->{id} );
+ $feed_entry->issued( DateTime::Format::Flexible->build( $m->{time} ) );
+
+ my $message = $filter->{message}->( $m->{message} );
+ $message .= "
\n" unless $message =~ m!<(/p|br/?)>!;
+# warn "## message = $message\n";
+ from_to( $message, $ENCODING, 'UTF-8' );
+
+ #$feed_entry->summary(
+ $feed_entry->content(
+ ""
+ );
+ $feed_entry->category( join(', ', @{$m->{tags}}) );
+ $feed->add_entry( $feed_entry );
+
+ $nr--;
+ last if $nr <= 0;
+
+ }
+
+ } else {
+ warn "!! unknown rss request for $show\n";
+ return RC_DENY;
+ }
+
+ $response->content( $feed->as_xml );
+ return RC_OK;
+ }
+
+ if ( $@ ) {
+ warn "$@";
+ }
+
+ $response->content_type("text/html; charset=$ENCODING");
+
my $html =
- qq{$NICK} .
- qq{
+ qq{$NICK}
+ . qq{
- } .
- $cloud->html(500) .
- qq{};
- if ($request->url =~ m#/history#) {
+ }
+ . $cloud->html(500)
+ . qq{
};
+
+ if ($request->url =~ m#/tags?#) {
+ # nop
+ } elsif ($request->url =~ m#/history#) {
my $sth = $dbh->prepare(qq{
- select date(time) as date,count(*) as nr
+ select date(time) as date,count(*) as nr,sum(length(message)) as len
from log
group by date(time)
order by date(time) desc
});
$sth->execute();
my ($l_yyyy,$l_mm) = (0,0);
+ $html .= qq{
};
my $cal;
+ my $ord = 0;
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);
+ if ( $cal ) {
+ $html .= qq{} . $cal->as_HTML() . qq{ | };
+ $ord++;
+ $html .= qq{
} if ( $ord % 3 == 0 );
+ }
$cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);
- $cal->border(2);
+ $cal->border(1);
+ $cal->width('30%');
+ $cal->cellheight('5em');
+ $cal->tableclass('month');
+ #$cal->cellclass('day');
+ $cal->sunday('SUN');
+ $cal->saturday('SAT');
+ $cal->weekdays('MON','TUE','WED','THU','FRI');
($l_yyyy,$l_mm) = ($yyyy,$mm);
}
- $cal->setcontent($dd, qq{
- $row->{nr}
- });
+ $cal->setcontent($dd, qq[
+ $row->{nr}
$row->{len}
+ ]);
+
}
- $html .= $cal->as_HTML() if ($cal);
+ $html .= qq{} . $cal->as_HTML() . qq{ |
};
} else {
$html .= join("
",
get_from_log(
- limit => $q->param('last') || $q->param('date') ? undef : 100,
+ 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{
};
+ qq{
};
},
time => '%s ',
time_channel => '%s %s ',
@@ -772,25 +1137,7 @@
me_nick => '***%s ',
message => '%s',
},
- 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 . '';
- },
- },
+ filter => $filter,
)
);
}
@@ -801,6 +1148,7 @@
};
$response->content( $html );
+ warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n";
return RC_OK;
}