/[irc-logger]/trunk/bin/irc-logger.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/irc-logger.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/irc-logger.pl revision 41 by dpavlin, Tue Oct 24 12:51:49 2006 UTC trunk/bin/irc-logger.pl revision 98 by dpavlin, Fri Mar 7 16:02:27 2008 UTC
# Line 18  irc-logger.pl Line 18  irc-logger.pl
18    
19  Import log from C<dircproxy> to C<irc-logger> database  Import log from C<dircproxy> to C<irc-logger> database
20    
21    =item --log=irc-logger.log
22    
23    =back
24    
25  =head1 DESCRIPTION  =head1 DESCRIPTION
26    
27  log all conversation on irc channel  log all conversation on irc channel
# Line 26  log all conversation on irc channel Line 30  log all conversation on irc channel
30    
31  ## CONFIG  ## CONFIG
32    
33  my $HOSTNAME = `hostname`;  my $HOSTNAME = `hostname -f`;
34    chomp($HOSTNAME);
35    
36  my $NICK = 'irc-logger';  my $NICK = 'irc-logger';
37  $NICK .= '-dev' if ($HOSTNAME =~ m/llin/);  $NICK .= '-dev' if ($HOSTNAME =~ m/llin/);
# Line 39  my $CHANNEL = '#razmjenavjestina'; Line 44  my $CHANNEL = '#razmjenavjestina';
44  $CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);  $CHANNEL = '#irc-logger' if ($HOSTNAME =~ m/llin/);
45  my $IRC_ALIAS = "log";  my $IRC_ALIAS = "log";
46    
 my %FOLLOWS =  
   (  
    ACCESS => "/var/log/apache/access.log",  
    ERROR => "/var/log/apache/error.log",  
   );  
   
47  my $DSN = 'DBI:Pg:dbname=' . $NICK;  my $DSN = 'DBI:Pg:dbname=' . $NICK;
48    
 my $ENCODING = 'ISO-8859-2';  
49  my $TIMESTAMP = '%Y-%m-%d %H:%M:%S';  my $TIMESTAMP = '%Y-%m-%d %H:%M:%S';
50    
51  my $sleep_on_error = 5;  my $sleep_on_error = 5;
52    
53  ## END CONFIG  # number of last tags to keep in circular buffer
54    my $last_x_tags = 50;
55    
56    # don't pull rss feeds more often than this
57    my $rss_min_delay = 60;
58    
59    my $http_port = $NICK =~ m/-dev/ ? 8001 : 8000;
60    
61    my $url = "http://$HOSTNAME:$http_port";
62    
63    ## END CONFIG
64    
65  use POE qw(Component::IRC Wheel::FollowTail Component::Server::HTTP);  use POE qw(Component::IRC Component::Server::HTTP);
66  use HTTP::Status;  use HTTP::Status;
67  use DBI;  use DBI;
 use Encode qw/from_to is_utf8/;  
68  use Regexp::Common qw /URI/;  use Regexp::Common qw /URI/;
69  use CGI::Simple;  use CGI::Simple;
70  use HTML::TagCloud;  use HTML::TagCloud;
# Line 67  use POSIX qw/strftime/; Line 72  use POSIX qw/strftime/;
72  use HTML::CalendarMonthSimple;  use HTML::CalendarMonthSimple;
73  use Getopt::Long;  use Getopt::Long;
74  use DateTime;  use DateTime;
75    use URI::Escape;
76    use Data::Dump qw/dump/;
77    use DateTime::Format::ISO8601;
78    use Carp qw/confess/;
79    use XML::Feed;
80    use DateTime::Format::Flexible;
81    
82    my $use_twitter = 1;
83    eval { require Net::Twitter; };
84    $use_twitter = 0 if ($@);
85    
86  my $import_dircproxy;  my $import_dircproxy;
87    my $log_path;
88  GetOptions(  GetOptions(
89          'import-dircproxy:s' => \$import_dircproxy,          'import-dircproxy:s' => \$import_dircproxy,
90            'log:s' => \$log_path,
91  );  );
92    
93  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  #$SIG{__DIE__} = sub {
94    #       confess "fatal error";
95    #};
96    
97    open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
98    
99    sub _log {
100            print strftime($TIMESTAMP,localtime()) . ' ' . join(" ",@_) . $/;
101    }
102    
103    # HTML formatters
104    
105    my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
106    my $escape_re  = join '|' => keys %escape;
107    
108    my $tag_regex = '\b([\w-_]+)//';
109    
110    my %nick_enumerator;
111    my $max_color = 0;
112    
113    my $filter = {
114            message => sub {
115                    my $m = shift || return;
116    
117                    # protect HTML from wiki modifications
118                    sub e {
119                            my $t = shift;
120                            return 'uri_unescape{' . uri_escape($t, '^a-zA-Z0-9') . '}';
121                    }
122    
123                    $m =~ s/($escape_re)/$escape{$1}/gs;
124                    $m =~ s#($RE{URI}{HTTP})#e(qq{<a href="$1">$1</a>})#egs;
125                    $m =~ s#\/(\w+)\/#<i>$1</i>#gs;
126                    $m =~ s#$tag_regex#e(qq{<a href="$url?tag=$1" class="tag">$1</a>})#egs;
127                    $m =~ s#\*(\w+)\*#<b>$1</b>#gs;
128                    $m =~ s#_(\w+)_#<u>$1</u>#gs;
129    
130  eval {                  $m =~ s#uri_unescape{([^}]+)}#uri_unescape($1)#egs;
131          $dbh->do(qq{ select count(*) from log });                  return $m;
132            },
133            nick => sub {
134                    my $n = shift || return;
135                    if (! $nick_enumerator{$n})  {
136                            my $max = scalar keys %nick_enumerator;
137                            $nick_enumerator{$n} = $max + 1;
138                    }
139                    return '<span class="nick col-' .
140                            ( $nick_enumerator{$n} % $max_color ) .
141                            '">' . $n . '</span>';
142            },
143  };  };
144    
145  if ($@) {  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
146          warn "creating database table in $DSN\n";  $dbh->do( qq{ set client_encoding = 'UTF-8' } );
         $dbh->do(<<'_SQL_SCHEMA_');  
147    
148    my $sql_schema = {
149            log => qq{
150  create table log (  create table log (
151          id serial,          id serial,
152          time timestamp default now(),          time timestamp default now(),
# Line 96  create table log ( Line 160  create table log (
160  create index log_time on log(time);  create index log_time on log(time);
161  create index log_channel on log(channel);  create index log_channel on log(channel);
162  create index log_nick on log(nick);  create index log_nick on log(nick);
163            },
164            meta => q{
165    create table meta (
166            nick text not null,
167            channel text not null,
168            name text not null,
169            value text,
170            changed timestamp default 'now()',
171            primary key(nick,channel,name)
172    );
173            },
174            feeds => qq{
175    create table feeds (
176            id serial,
177            url text not null,
178            name text,
179            delay interval not null default '5 min',
180            active boolean default true,
181            channel text not null,
182            nick text not null,
183            private boolean default false,
184            last_update timestamp default 'now()',
185            polls int default 0,
186            updates int default 0
187    );
188    create unique index feeds_url on feeds(url);
189    insert into feeds (url,name,channel,nick) values ('http://wiki.razmjenavjestina.org/feed/workspace/razmjenavjestina?category=Recent%20Changes','wiki','$CHANNEL','dpavlin');
190            },
191    };
192    
193    foreach my $table ( keys %$sql_schema ) {
194    
195  _SQL_SCHEMA_          eval {
196                    $dbh->do(qq{ select count(*) from $table });
197            };
198    
199            if ($@) {
200                    warn "creating database table $table in $DSN\n";
201                    $dbh->do( $sql_schema->{ $table } );
202            }
203  }  }
204    
205  my $sth = $dbh->prepare(qq{  
206    =head2 meta
207    
208    Set or get some meta data into database
209    
210            meta('nick','channel','var_name', $var_value );
211    
212            $var_value = meta('nick','channel','var_name');
213            ( $var_value, $changed ) = meta('nick','channel','var_name');
214    
215    =cut
216    
217    sub meta {
218            my ($nick,$channel,$name,$value) = @_;
219    
220            # normalize channel name
221            $channel =~ s/^#//;
222    
223            if (defined($value)) {
224    
225                    my $sth = $dbh->prepare(qq{ update meta set value = ?, changed = now() where nick = ? and channel = ? and name = ? });
226    
227                    eval { $sth->execute( $value, $nick, $channel, $name ) };
228    
229                    # error or no result
230                    if ( $@ || ! $sth->rows ) {
231                            $sth = $dbh->prepare(qq{ insert into meta (value,nick,channel,name,changed) values (?,?,?,?,now()) });
232                            $sth->execute( $value, $nick, $channel, $name );
233                            warn "## created $nick/$channel/$name = $value\n";
234                    } else {
235                            warn "## updated $nick/$channel/$name = $value\n";
236                    }
237    
238                    return $value;
239    
240            } else {
241    
242                    my $sth = $dbh->prepare(qq{ select value,changed from meta where nick = ? and channel = ? and name = ? });
243                    $sth->execute( $nick, $channel, $name );
244                    my ($v,$c) = $sth->fetchrow_array;
245                    warn "## fetched $nick/$channel/$name = $v [$c]\n";
246                    return ($v,$c) if wantarray;
247                    return $v;
248    
249            }
250    }
251    
252    
253    
254    my $sth_insert_log = $dbh->prepare(qq{
255  insert into log  insert into log
256          (channel, me, nick, message, time)          (channel, me, nick, message, time)
257  values (?,?,?,?,?)  values (?,?,?,?,?)
258  });  });
259    
260    
261  my $tags;  my $tags;
 my $tag_regex = '\b([\w-_]+)//';  
262    
263  =head2 get_from_log  =head2 get_from_log
264    
# Line 128  my $tag_regex = '\b([\w-_]+)//'; Line 279  my $tag_regex = '\b([\w-_]+)//';
279                  }                  }
280          },          },
281          context => 5,          context => 5,
282            full_rows => 1,
283   );   );
284    
285  Order is important. Fields are first passed through C<filter> (if available) and  Order is important. Fields are first passed through C<filter> (if available) and
# Line 135  then throgh C<< sprintf($fmt->{message}, Line 287  then throgh C<< sprintf($fmt->{message},
287    
288  C<context> defines number of messages around each search hit for display.  C<context> defines number of messages around each search hit for display.
289    
290    C<full_rows> will return database rows for each result with C<date>, C<time>, C<channel>,
291    C<me>, C<nick> and C<message> keys.
292    
293  =cut  =cut
294    
295  sub get_from_log {  sub get_from_log {
296          my $args = {@_};          my $args = {@_};
297    
298          $args->{fmt} ||= {          if ( ! $args->{fmt} ) {
299                  date => '[%s] ',                  $args->{fmt} = {
300                  time => '{%s} ',                          date => '[%s] ',
301                  time_channel => '{%s %s} ',                          time => '{%s} ',
302                  nick => '%s: ',                          time_channel => '{%s %s} ',
303                  me_nick => '***%s ',                          nick => '%s: ',
304                  message => '%s',                          me_nick => '***%s ',
305          };                          message => '%s',
306                    };
307            }
308    
309          my $sql_message = qq{          my $sql_message = qq{
310                  select                  select
# Line 170  sub get_from_log { Line 327  sub get_from_log {
327    
328          my $sql = $context ? $sql_context : $sql_message;          my $sql = $context ? $sql_context : $sql_message;
329    
330          $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});          sub check_date {
331          $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });                  my $date = shift || return;
332          $sql .= " where date(time) = ? " if ($args->{date});                  my $new_date = eval { DateTime::Format::ISO8601->parse_datetime( $date )->ymd; };
333          $sql .= " order by log.time desc";                  if ( $@ ) {
334          $sql .= " limit " . $args->{limit} if ($args->{limit});                          warn "invalid date $date\n";
335                            $new_date = DateTime->now->ymd;
336                    }
337                    return $new_date;
338            }
339    
340            my @where;
341            my @args;
342    
         my $sth = $dbh->prepare( $sql );  
343          if (my $search = $args->{search}) {          if (my $search = $args->{search}) {
344                  $search =~ s/^\s+//;                  $search =~ s/^\s+//;
345                  $search =~ s/\s+$//;                  $search =~ s/\s+$//;
346                  $sth->execute( ( '%' . $search . '%' ) x 2 );                  push @where, 'message ilike ? or nick ilike ?';
347                  warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n";                  push @args, ( ( '%' . $search . '%' ) x 2 );
348          } elsif (my $tag = $args->{tag}) {                  _log "search for '$search'";
349                  $sth->execute();          }
350                  warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";  
351          } elsif (my $date = $args->{date}) {          if ($args->{tag} && $tags->{ $args->{tag} }) {
352                  $sth->execute($date);                  push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')';
353                  warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n";                  _log "search for tags $args->{tag}";
354          } else {          }
355                  $sth->execute();  
356            if (my $date = $args->{date} ) {
357                    $date = check_date( $date );
358                    push @where, 'date(time) = ?';
359                    push @args, $date;
360                    _log "search for date $date";
361          }          }
362    
363            $sql .= " where " . join(" and ", @where) if @where;
364    
365            $sql .= " order by log.time desc";
366            $sql .= " limit " . $args->{limit} if ($args->{limit});
367    
368            #warn "### sql: $sql ", dump( @args );
369    
370            my $sth = $dbh->prepare( $sql );
371            eval { $sth->execute( @args ) };
372            return if $@;
373    
374          my $last_row = {          my $last_row = {
375                  date => '',                  date => '',
376                  time => '',                  time => '',
# Line 204  sub get_from_log { Line 384  sub get_from_log {
384                  unshift @rows, $row;                  unshift @rows, $row;
385          }          }
386    
387            # normalize nick names
388            map {
389                    $_->{nick} =~ s/^_*(.*?)_*$/$1/
390            } @rows;
391    
392            return @rows if ($args->{full_rows});
393    
394          my @msgs = (          my @msgs = (
395                  "Showing " . ($#rows + 1) . " messages..."                  "Showing " . ($#rows + 1) . " messages..."
396          );          );
# Line 260  sub get_from_log { Line 447  sub get_from_log {
447                  my $append = 1;                  my $append = 1;
448    
449                  my $nick = $row->{nick};                  my $nick = $row->{nick};
450                  if ($nick =~ s/^_*(.*?)_*$/$1/) {  #               if ($nick =~ s/^_*(.*?)_*$/$1/) {
451                          $row->{nick} = $nick;  #                       $row->{nick} = $nick;
452                  }  #               }
453    
454                  if ($last_row->{nick} ne $nick) {                  if ($last_row->{nick} ne $nick) {
455                          # obfu way to find format for me_nick if needed or fallback to default                          # obfu way to find format for me_nick if needed or fallback to default
# Line 304  my $cloud = HTML::TagCloud->new; Line 491  my $cloud = HTML::TagCloud->new;
491    
492  =head2 add_tag  =head2 add_tag
493    
494   add_tag( id => 42, message => 'irc message' );   add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] );
495    
496  =cut  =cut
497    
498    my @last_tags;
499    
500  sub add_tag {  sub add_tag {
501          my $arg = {@_};          my $arg = {@_};
502    
503          return unless ($arg->{id} && $arg->{message});          return unless ($arg->{id} && $arg->{message});
504    
505          my $m = $arg->{message};          my $m = $arg->{message};
506          from_to('UTF-8', 'iso-8859-2', $m) if (is_utf8($m));  
507            my @tags;
508    
509          while ($m =~ s#$tag_regex##s) {          while ($m =~ s#$tag_regex##s) {
510                  my $tag = $1;                  my $tag = $1;
511                  next if (! $tag || $tag =~ m/https?:/i);                  next if (! $tag || $tag =~ m/https?:/i);
512                  push @{ $tags->{$tag} }, $arg->{id};                  push @{ $tags->{$tag} }, $arg->{id};
513                  #warn "+tag $tag: $arg->{id}\n";                  #warn "+tag $tag: $arg->{id}\n";
514                  $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);                  $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1);
515                    push @tags, $tag;
516    
517          }          }
518    
519            if ( @tags ) {
520                    pop @last_tags if $#last_tags == $last_x_tags;
521                    unshift @last_tags, { tags => [ @tags ], %$arg };
522            }
523    
524  }  }
525    
526  =head2 seed_tags  =head2 seed_tags
# Line 332  Read all tags from database and create i Line 530  Read all tags from database and create i
530  =cut  =cut
531    
532  sub seed_tags {  sub seed_tags {
533          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 });
534          $sth->execute;          $sth->execute;
535          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
536                  add_tag( %$row );                  add_tag( %$row );
537          }          }
538    
539          foreach my $tag (keys %$tags) {          foreach my $tag (keys %$tags) {
540                  $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1);                  $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1);
541          }          }
542  }  }
543    
# Line 352  seed_tags; Line 550  seed_tags;
550          channel => '#foobar',          channel => '#foobar',
551          me => 0,          me => 0,
552          nick => 'dpavlin',          nick => 'dpavlin',
553          msg => 'test message',          message => 'test message',
554          time => '2006-06-25 18:57:18',          time => '2006-06-25 18:57:18',
555    );    );
556    
# Line 364  C<me> if not specified will be C<0> (not Line 562  C<me> if not specified will be C<0> (not
562    
563  sub save_message {  sub save_message {
564          my $a = {@_};          my $a = {@_};
565            confess "have msg" if $a->{msg};
566          $a->{me} ||= 0;          $a->{me} ||= 0;
567          $a->{time} ||= strftime($TIMESTAMP,localtime());          $a->{time} ||= strftime($TIMESTAMP,localtime());
568    
569          print          _log
                 $a->{time}, " ",  
570                  $a->{channel}, " ",                  $a->{channel}, " ",
571                  $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",                  $a->{me} ? "***" . $a->{nick} : "<" . $a->{nick} . ">",
572                  " " . $a->{msg} . "\n";                  " " . $a->{message};
573    
574          from_to($a->{msg}, 'UTF-8', $ENCODING);          $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time});
575            add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a );
         $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});  
576  }  }
577    
578    
579  if ($import_dircproxy) {  if ($import_dircproxy) {
580          open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";          open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";
581          warn "importing $import_dircproxy...\n";          warn "importing $import_dircproxy...\n";
582          my $tz_offset = 2 * 60 * 60;    # TZ GMT+2          my $tz_offset = 1 * 60 * 60;    # TZ GMT+2
583          while(<$l>) {          while(<$l>) {
584                  chomp;                  chomp;
585                  if (/^@(\d+)\s(\S+)\s(.+)$/) {                  if (/^@(\d+)\s(\S+)\s(.+)$/) {
# Line 401  if ($import_dircproxy) { Line 597  if ($import_dircproxy) {
597                                  channel => $CHANNEL,                                  channel => $CHANNEL,
598                                  me => $me,                                  me => $me,
599                                  nick => $nick,                                  nick => $nick,
600                                  msg => $msg,                                  message => $msg,
601                                  time => $dt->ymd . " " . $dt->hms,                                  time => $dt->ymd . " " . $dt->hms,
602                          ) if ($nick !~ m/^-/);                          ) if ($nick !~ m/^-/);
603    
604                  } else {                  } else {
605                          warn "can't parse: $_\n";                          _log "can't parse: $_";
606                  }                  }
607          }          }
608          close($l);          close($l);
# Line 414  if ($import_dircproxy) { Line 610  if ($import_dircproxy) {
610          exit;          exit;
611  }  }
612    
613    #
614    # RSS follow
615    #
616    
617    my $_rss;
618    
619    
620    sub rss_fetch {
621            my ($args) = @_;
622    
623            # how many messages to send out when feed is seen for the first time?
624            my $send_rss_msgs = 1;
625    
626            _log "RSS fetch", $args->{url};
627    
628            my $feed = XML::Feed->parse(URI->new( $args->{url} ));
629            if ( ! $feed ) {
630                    _log("can't fetch RSS ", $args->{url});
631                    return;
632            }
633    
634            my ( $total, $updates ) = ( 0, 0 );
635            for my $entry ($feed->entries) {
636                    $total++;
637    
638                    # seen allready?
639                    next if $_rss->{$args->{channel}}->{$feed->link}->{$entry->id}++ > 0;
640    
641                    sub prefix {
642                            my ($txt,$var) = @_;
643                            $var =~ s/\s+/ /gs;
644                            $var =~ s/^\s+//g;
645                            $var =~ s/\s+$//g;
646                            return $txt . $var if $var;
647                    }
648    
649                    # fix absolute and relative links to feed entries
650                    my $link = $entry->link;
651                    if ( $link =~ m!^/! ) {
652                            my $host = $args->{url};
653                            $host =~ s!^(http://[^/]+).*$!$1!;      #!vim
654                            $link = "$host/$link";
655                    } elsif ( $link !~ m!^http! ) {
656                            $link = $args->{url} . $link;
657                    }
658                    $link =~ s!//+!/!g;
659    
660                    my $msg;
661                    $msg .= prefix( 'From: ' , $args->{name} || $feed->title );
662                    $msg .= prefix( ' by ' , $entry->author );
663                    $msg .= prefix( ' | ' , $entry->title );
664                    $msg .= prefix( ' | ' , $link );
665    #               $msg .= prefix( ' id ' , $entry->id );
666    
667                    if ( $args->{kernel} && $send_rss_msgs ) {
668                            $send_rss_msgs--;
669                            $sth_insert_log->execute( $args->{channel}, 1, $NICK, $msg, 'now()' );
670                            my ( $type, $to ) = ( 'notice', $args->{channel} );
671                            ( $type, $to ) = ( 'privmsg', $args->{nick} ) if $args->{private};
672                            _log(">> $type $to |", $msg);
673                            $args->{kernel}->post( $IRC_ALIAS => $type => $to, $msg );
674                            $updates++;
675                    }
676            }
677    
678            my $sql = qq{ update feeds set last_update = now(), polls = polls + 1 };
679            $sql .= qq{, updates = updates + $updates } if $updates;
680            $sql .= qq{where id = } . $args->{id};
681            eval { $dbh->do( $sql ) };
682    
683            _log "RSS got $total items of which $updates new";
684    
685            return $updates;
686    }
687    
688    sub rss_fetch_all {
689            my $kernel = shift;
690            my $sql = qq{
691                    select id, url, name, channel, nick, private
692                    from feeds
693                    where active is true
694            };
695            # limit to newer feeds only if we are not sending messages out
696            $sql .= qq{     and last_update + delay < now() } if $kernel;
697            my $sth = $dbh->prepare( $sql );
698            $sth->execute();
699            warn "# ",$sth->rows," active RSS feeds\n";
700            my $count = 0;
701            while (my $row = $sth->fetchrow_hashref) {
702                    $row->{kernel} = $kernel if $kernel;
703                    $count += rss_fetch( $row );
704            }
705            return "OK, fetched $count posts from " . $sth->rows . " feeds";
706    }
707    
708    
709    sub rss_check_updates {
710            my $kernel = shift;
711            $_rss->{last_poll} ||= time();
712            my $dt = time() - $_rss->{last_poll};
713            warn "## rss_check_updates $dt > $rss_min_delay\n";
714            if ( $dt > $rss_min_delay ) {
715                    $_rss->{last_poll} = time();
716                    _log rss_fetch_all( $kernel );
717            }
718    }
719    
720    # seed rss seen cache so we won't send out all items on startup
721    _log rss_fetch_all;
722    
723  #  #
724  # POE handing part  # POE handing part
725  #  #
726    
727  my $SKIPPING = 0;               # if skipping, how many we've done  my $ping;                                               # ping stats
 my $SEND_QUEUE;                 # cache  
728    
729  POE::Component::IRC->new($IRC_ALIAS);  POE::Component::IRC->new($IRC_ALIAS);
730    
731  POE::Session->create( inline_states =>  POE::Session->create( inline_states => {
732     {_start => sub {                _start => sub {      
733                  $_[KERNEL]->post($IRC_ALIAS => register => 'all');                  $_[KERNEL]->post($IRC_ALIAS => register => 'all');
734                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
735      },      },
736      irc_255 => sub {    # server is done blabbing      irc_255 => sub {    # server is done blabbing
737                  $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);                  $_[KERNEL]->post($IRC_ALIAS => join => $CHANNEL);
                 $_[KERNEL]->post($IRC_ALIAS => join => '#logger');  
                 $_[KERNEL]->yield("heartbeat"); # start heartbeat  
 #               $_[KERNEL]->yield("my_add", $_) for keys %FOLLOWS;  
738                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
739      },      },
740      irc_public => sub {      irc_public => sub {
# Line 442  POE::Session->create( inline_states => Line 743  POE::Session->create( inline_states =>
743                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
744                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
745    
746                  save_message( channel => $channel, me => 0, nick => $nick, msg => $msg);                  save_message( channel => $channel, me => 0, nick => $nick, message => $msg);
747                    meta( $nick, $channel, 'last-msg', $msg );
748                    rss_check_updates( $kernel );
749      },      },
750      irc_ctcp_action => sub {      irc_ctcp_action => sub {
751                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
# Line 450  POE::Session->create( inline_states => Line 753  POE::Session->create( inline_states =>
753                  my $channel = $_[ARG1]->[0];                  my $channel = $_[ARG1]->[0];
754                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
755    
756                  save_message( channel => $channel, me => 1, nick => $nick, msg => $msg);                  save_message( channel => $channel, me => 1, nick => $nick, message => $msg);
757    
758                    if ( $use_twitter ) {
759                            if ( my $twitter = meta( $nick, $NICK, 'twitter' ) ) {
760                                    my ($login,$passwd) = split(/\s+/,$twitter,2);
761                                    _log("sending twitter for $nick/$login on $channel ");
762                                    my $bot = Net::Twitter->new( username=>$login, password=>$passwd );
763                                    $bot->update("<${channel}> $msg");
764                            }
765                    }
766    
767      },      },
768            irc_ping => sub {
769                    _log( "pong ", $_[ARG0] );
770                    $ping->{ $_[ARG0] }++;
771                    rss_check_updates( $_[KERNEL] );
772            },
773            irc_invite => sub {
774                    my $kernel = $_[KERNEL];
775                    my $nick = (split /!/, $_[ARG0])[0];
776                    my $channel = $_[ARG1];
777    
778                    _log "invited to $channel by $nick";
779    
780                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, "how nice of you to invite me to $channel, I'll be right there..." );
781                    $_[KERNEL]->post($IRC_ALIAS => join => $channel);
782    
783            },
784          irc_msg => sub {          irc_msg => sub {
785                  my $kernel = $_[KERNEL];                  my $kernel = $_[KERNEL];
786                  my $nick = (split /!/, $_[ARG0])[0];                  my $nick = (split /!/, $_[ARG0])[0];
787                  my $msg = $_[ARG2];                  my $msg = $_[ARG2];
788                  from_to($msg, 'UTF-8', $ENCODING);                  my $channel = $_[ARG1]->[0];
789    
790                  my $res = "unknown command '$msg', try /msg $NICK help!";                  my $res = "unknown command '$msg', try /msg $NICK help!";
791                  my @out;                  my @out;
792    
793                  print "<< $msg\n";                  _log "<< $msg";
794    
795                  if ($msg =~ m/^help/i) {                  if ($msg =~ m/^help/i) {
796    
# Line 469  POE::Session->create( inline_states => Line 798  POE::Session->create( inline_states =>
798    
799                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {                  } elsif ($msg =~ m/^msg\s+(\S+)\s+(.*)$/i) {
800    
801                          print ">> /msg $1 $2\n";                          _log ">> /msg $1 $2";
802                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $1, $2 );
803                          $res = '';                          $res = '';
804    
# Line 479  POE::Session->create( inline_states => Line 808  POE::Session->create( inline_states =>
808    
809                          my $sth = $dbh->prepare(qq{                          my $sth = $dbh->prepare(qq{
810                                  select                                  select
811                                          nick,                                          trim(both '_' from nick) as nick,
812                                          count(*) as count,                                          count(*) as count,
813                                          sum(length(message)) as len                                          sum(length(message)) as len
814                                  from log                                  from log
815                                  group by nick                                  group by trim(both '_' from nick)
816                                  order by len desc,count desc                                  order by len desc,count desc
817                                  limit $nr                                  limit $nr
818                          });                          });
# Line 496  POE::Session->create( inline_states => Line 825  POE::Session->create( inline_states =>
825                          $res .= join(" | ", @users);                          $res .= join(" | ", @users);
826                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {                  } elsif ($msg =~ m/^last.*?\s*(\d*)/i) {
827    
828                          foreach my $res (get_from_log( limit => ($1 || 100) )) {                          my $limit = $1 || meta( $nick, $channel, 'last-size' ) || 10;
829                                  print "last: $res\n";  
830                                  from_to($res, $ENCODING, 'UTF-8');                          foreach my $res (get_from_log( limit => $limit )) {
831                                    _log "last: $res";
832                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
833                          }                          }
834    
# Line 512  POE::Session->create( inline_states => Line 842  POE::Session->create( inline_states =>
842                                          limit => 20,                                          limit => 20,
843                                          search => $what,                                          search => $what,
844                                  )) {                                  )) {
845                                  print "search [$what]: $res\n";                                  _log "search [$what]: $res";
                                 from_to($res, $ENCODING, 'UTF-8');  
846                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
847                          }                          }
848    
849                          $res = '';                          $res = '';
850    
851                    } elsif ($msg =~ m/^(?:count|poll)\s+(.*)(?:\s+(\d+))?\s*$/i) {
852    
853                            my ($what,$limit) = ($1,$2);
854                            $limit ||= 100;
855    
856                            my $stat;
857    
858                            foreach my $res (get_from_log(
859                                            limit => $limit,
860                                            search => $what,
861                                            full_rows => 1,
862                                    )) {
863                                    while ($res->{message} =~ s/\Q$what\E(\+|\-)//) {
864                                            $stat->{vote}->{$1}++;
865                                            $stat->{from}->{ $res->{nick} }++;
866                                    }
867                            }
868    
869                            my @nicks;
870                            foreach my $nick (sort { $stat->{from}->{$a} <=> $stat->{from}->{$b} } keys %{ $stat->{from} }) {
871                                    push @nicks, $nick . ( $stat->{from}->{$nick} == 1 ? '' :
872                                            "(" . $stat->{from}->{$nick} . ")"
873                                    );
874                            }
875    
876                            $res =
877                                    "$what ++ " . ( $stat->{vote}->{'+'} || 0 ) .
878                                    " : " . ( $stat->{vote}->{'-'} || 0 ) . " --" .
879                                    " from " . ( join(", ", @nicks) || 'nobody' );
880    
881                            $_[KERNEL]->post( $IRC_ALIAS => notice => $nick, $res );
882    
883                    } elsif ($msg =~ m/^ping/) {
884                            $res = "ping = " . dump( $ping );
885                    } elsif ($msg =~ m/^conf(?:ig)*\s*(last-size|twitter)*\s*(.*)/) {
886                            if ( ! defined( $1 ) ) {
887                                    my $sth = $dbh->prepare(qq{ select name,value,changed from meta where nick = ? and channel = ? });
888                                    $sth->execute( $nick, $channel );
889                                    $res = "config for $nick on $channel";
890                                    while ( my ($n,$v) = $sth->fetchrow_array ) {
891                                            $res .= " | $n = $v";
892                                    }
893                            } elsif ( ! $2 ) {
894                                    my $val = meta( $nick, $channel, $1 );
895                                    $res = "current $1 = " . ( $val ? $val : 'undefined' );
896                            } else {
897                                    my $validate = {
898                                            'last-size' => qr/^\d+/,
899                                            'twitter' => qr/^\w+\s+\w+/,
900                                    };
901    
902                                    my ( $op, $val ) = ( $1, $2 );
903    
904                                    if ( my $regex = $validate->{$op} ) {
905                                            if ( $val =~ $regex ) {
906                                                    meta( $nick, $channel, $op, $val );
907                                                    $res = "saved $op = $val";
908                                            } else {
909                                                    $res = "config option $op = $val doesn't validate against $regex";
910                                            }
911                                    } else {
912                                            $res = "config option $op doesn't exist";
913                                    }
914                            }
915                    } elsif ($msg =~ m/^rss-update/) {
916                            $res = rss_fetch_all( $_[KERNEL] );
917                    } elsif ($msg =~ m/^rss-clean/) {
918                            $_rss = undef;
919                            $dbh->do( qq{ update feeds set last_update = now() - delay } );
920                            $res = "OK, cleaned RSS cache";
921                    } elsif ($msg =~ m/^rss-list/) {
922                            my $sth = $dbh->prepare(qq{ select url,name,last_update,active,channel,nick,private from feeds });
923                            $sth->execute;
924                            while (my @row = $sth->fetchrow_array) {
925                                    $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, join(' | ',@row) );
926                            }
927                            $res = '';
928                    } elsif ($msg =~ m!^rss-(add|remove|stop|start)(?:-(private))?\s+(http://\S+)\s*(.*)!) {
929                            my ( $command, $sub, $url, $arg ) = ( $1,$2,$3,$4 );
930    
931                            my $channel = $1 if ( $arg =~ s/\s*(#\S+)\s*// );
932                            $channel = $nick if $sub eq 'private';
933    
934                            my $sql = {
935                                    add             => qq{ insert into feeds (url,name,channel,nick,private) values (?,?,?,?,?) },
936    #                               remove  => qq{ delete from feeds                                where url = ? and name = ? },
937                                    start   => qq{ update feeds set active = true   where url = ? },
938                                    stop    => qq{ update feeds set active = false  where url = ? },
939                            };
940    
941                            if (my $q = $sql->{$command} ) {
942                                    my $sth = $dbh->prepare( $q );
943                                    my @data = ( $url );
944                                    if ( $command eq 'add' ) {
945                                            push @data, ( $arg, $channel, $nick, $sub eq 'private' ? 1 : 0 );
946                                    }
947                                    warn "## $command SQL $q with ",dump( @data ),"\n";
948                                    eval { $sth->execute( @data ) };
949                                    if ($@) {
950                                            $res = "ERROR: $@";
951                                    } else {
952                                            $res = "OK, RSS [$command|$sub|$url|$arg]";
953                                    }
954                            } else {
955                                    $res = "ERROR: don't know what to do with: $msg";
956                            }
957    
958                  }                  }
959    
960                  if ($res) {                  if ($res) {
961                          print ">> [$nick] $res\n";                          _log ">> [$nick] $res";
                         from_to($res, $ENCODING, 'UTF-8');  
962                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );                          $_[KERNEL]->post( $IRC_ALIAS => privmsg => $nick, $res );
963                  }                  }
964    
965                    rss_check_updates( $_[KERNEL] );
966          },          },
967          irc_477 => sub {          irc_477 => sub {
968                  print "# irc_477: ",$_[ARG1], "\n";                  _log "# irc_477: ",$_[ARG1];
969                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
970          },          },
971          irc_505 => sub {          irc_505 => sub {
972                  print "# irc_505: ",$_[ARG1], "\n";                  _log "# irc_505: ",$_[ARG1];
973                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "register $NICK" );
974  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set hide email on" );
975  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );  #               $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "set email dpavlin\@rot13.org" );
976          },          },
977          irc_registered => sub {          irc_registered => sub {
978                  warn "## indetify $NICK\n";                  _log "## registrated $NICK";
979                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );                  $_[KERNEL]->post( $IRC_ALIAS => privmsg => 'nickserv', "IDENTIFY $NICK" );
980          },          },
981          irc_disconnected => sub {          irc_disconnected => sub {
982                  warn "## disconnected, reconnecting again\n";                  _log "## disconnected, reconnecting again";
983                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
984          },          },
985          irc_socketerr => sub {          irc_socketerr => sub {
986                  warn "## socket error... sleeping for $sleep_on_error seconds and retry";                  _log "## socket error... sleeping for $sleep_on_error seconds and retry";
987                  sleep($sleep_on_error);                  sleep($sleep_on_error);
988                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);                  $_[KERNEL]->post($IRC_ALIAS => connect => $CONNECT);
989          },          },
# Line 558  POE::Session->create( inline_states => Line 994  POE::Session->create( inline_states =>
994  #       },  #       },
995      _child => sub {},      _child => sub {},
996      _default => sub {      _default => sub {
997                  printf "%s #%s %s %s\n",                  _log sprintf "sID:%s %s %s",
998                          strftime($TIMESTAMP,localtime()), $_[SESSION]->ID, $_[ARG0],                          $_[SESSION]->ID, $_[ARG0],
999                          ref($_[ARG1]) eq "ARRAY"        ?       join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] })     :                          ref($_[ARG1]) eq "ARRAY"        ?       join(",", map { ref($_) eq "ARRAY" ? join(";", @{$_}) : $_ } @{ $_[ARG1] })     :
1000                          $_[ARG1]                                        ?       $_[ARG1]                                        :                          $_[ARG1]                                        ?       $_[ARG1]                                        :
1001                          "";                          "";
1002        0;                        # false for signals        0;                        # false for signals
1003      },      },
     my_add => sub {  
       my $trailing = $_[ARG0];  
       my $session = $_[SESSION];  
       POE::Session->create  
           (inline_states =>  
            {_start => sub {  
               $_[HEAP]->{wheel} =  
                 POE::Wheel::FollowTail->new  
                     (  
                      Filename => $FOLLOWS{$trailing},  
                      InputEvent => 'got_line',  
                     );  
             },  
             got_line => sub {  
               $_[KERNEL]->post($session => my_tailed =>  
                                time, $trailing, $_[ARG0]);  
             },  
            },  
           );  
       
     },  
     my_tailed => sub {  
       my ($time, $file, $line) = @_[ARG0..ARG2];  
       ## $time will be undef on a probe, or a time value if a real line  
   
       ## PoCo::IRC has throttling built in, but no external visibility  
       ## so this is reaching "under the hood"  
       $SEND_QUEUE ||=  
         $_[KERNEL]->alias_resolve($IRC_ALIAS)->get_heap->{send_queue};  
   
       ## handle "no need to keep skipping" transition  
       if ($SKIPPING and @$SEND_QUEUE < 1) {  
         $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>  
                          "[discarded $SKIPPING messages]");  
         $SKIPPING = 0;  
       }  
   
       ## handle potential message display  
       if ($time) {  
         if ($SKIPPING or @$SEND_QUEUE > 3) { # 3 msgs per 10 seconds  
           $SKIPPING++;  
         } else {  
           my @time = localtime $time;  
           $_[KERNEL]->post($IRC_ALIAS => privmsg => $CHANNEL =>  
                            sprintf "%02d:%02d:%02d: %s: %s",  
                            ($time[2] + 11) % 12 + 1, $time[1], $time[0],  
                            $file, $line);  
         }  
       }  
   
       ## handle re-probe/flush if skipping  
       if ($SKIPPING) {  
         $_[KERNEL]->delay($_[STATE] => 0.5); # $time will be undef  
       }  
   
     },  
     my_heartbeat => sub {  
       $_[KERNEL]->yield(my_tailed => time, "heartbeat", "beep");  
       $_[KERNEL]->delay($_[STATE] => 10);  
     }  
1004     },     },
1005    );    );
1006    
1007  # http server  # http server
1008    
1009  my $httpd = POE::Component::Server::HTTP->new(  my $httpd = POE::Component::Server::HTTP->new(
1010          Port => $NICK =~ m/-dev/ ? 8001 : 8000,          Port => $http_port,
1011            PreHandler => {
1012                    '/' => sub {
1013                            $_[0]->header(Connection => 'close')
1014                    }
1015            },
1016          ContentHandler => { '/' => \&root_handler },          ContentHandler => { '/' => \&root_handler },
1017          Headers        => { Server => 'irc-logger' },          Headers        => { Server => 'irc-logger' },
1018  );  );
1019    
 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');  
 my $escape_re  = join '|' => keys %escape;  
   
1020  my $style = <<'_END_OF_STYLE_';  my $style = <<'_END_OF_STYLE_';
1021  p { margin: 0; padding: 0.1em; }  p { margin: 0; padding: 0.1em; }
1022  .time, .channel { color: #808080; font-size: 60%; }  .time, .channel { color: #808080; font-size: 60%; }
# Line 646  p { margin: 0; padding: 0.1em; } Line 1024  p { margin: 0; padding: 0.1em; }
1024  .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }  .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; }
1025  .message { color: #000000; font-size: 100%; }  .message { color: #000000; font-size: 100%; }
1026  .search { float: right; }  .search { float: right; }
1027    a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none }
1028    a:hover.tag { border: 1px solid #eee }
1029    hr { border: 1px dashed #ccc; height: 1px; clear: both; }
1030    /*
1031  .col-0 { background: #ffff66 }  .col-0 { background: #ffff66 }
1032  .col-1 { background: #a0ffff }  .col-1 { background: #a0ffff }
1033  .col-2 { background: #99ff99 }  .col-2 { background: #99ff99 }
1034  .col-3 { background: #ff9999 }  .col-3 { background: #ff9999 }
1035  .col-4 { background: #ff66ff }  .col-4 { background: #ff66ff }
1036  a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none }  */
1037  a:hover.tag { border: 1px solid #eee }  .calendar { border: 1px solid red; width: 100%; }
1038  hr { border: 1px dashed #ccc; height: 1px; clear: both; }  .month { border: 0px; width: 100%; }
1039  _END_OF_STYLE_  _END_OF_STYLE_
1040    
1041  my $max_color = 4;  $max_color = 0;
1042    
1043  my %nick_enumerator;  my @cols = qw(
1044            #ffcccc #ccffe6 #ccccff #e6ccff #ffccff #ffcce6 #ff9999 #ffcc99 #ffff99
1045            #ccff99 #99ff99 #99ffcc #99ccff #9999ff #cc99ff #ff6666 #ffb366 #ffff66
1046            #66ff66 #66ffb3 #66b3ff #6666ff #ff3333 #33ff33 #3399ff #3333ff #ff3399
1047            #a0a0a0 #ff0000 #ffff00 #80ff00 #0000ff #8000ff #ff00ff #ff0080 #994d00
1048            #999900 #009900 #cc0066 #c0c0c0 #ccff99 #99ff33 #808080 #660033 #ffffff
1049    );
1050    
1051    foreach my $c (@cols) {
1052            $style .= ".col-${max_color} { background: $c }\n";
1053            $max_color++;
1054    }
1055    warn "defined $max_color colors for users...\n";
1056    
1057  sub root_handler {  sub root_handler {
1058          my ($request, $response) = @_;          my ($request, $response) = @_;
1059          $response->code(RC_OK);          $response->code(RC_OK);
1060          $response->content_type("text/html; charset=$ENCODING");  
1061            # this doesn't seem to work, so moved to PreHandler
1062            #$response->header(Connection => 'close');
1063    
1064            return RC_OK if $request->uri =~ m/favicon.ico$/;
1065    
1066          my $q;          my $q;
1067    
# Line 677  sub root_handler { Line 1075  sub root_handler {
1075    
1076          my $search = $q->param('search') || $q->param('grep') || '';          my $search = $q->param('search') || $q->param('grep') || '';
1077    
1078            if ($request->url =~ m#/rss(?:/(tags|last-tag|follow.*)\w*(?:=(\d+))?)?#i) {
1079                    my $show = lc($1);
1080                    my $nr = $2;
1081    
1082                    my $type = 'RSS';       # Atom
1083    
1084                    $response->content_type( 'application/' . lc($type) . '+xml' );
1085    
1086                    my $html = '<!-- error -->';
1087                    #warn "create $type feed from ",dump( @last_tags );
1088    
1089                    my $feed = XML::Feed->new( $type );
1090                    $feed->link( $url );
1091    
1092                    if ( $show eq 'tags' ) {
1093                            $nr ||= 50;
1094                            $feed->title( "tags from $CHANNEL" );
1095                            $feed->link( "$url/tags" );
1096                            $feed->description( "tag cloud created from messages on channel $CHANNEL which have tags// in them" );
1097                            my $feed_entry = XML::Feed::Entry->new($type);
1098                            $feed_entry->title( "$nr tags from $CHANNEL" );
1099                            $feed_entry->author( $NICK );
1100                            $feed_entry->link( '/#tags'  );
1101    
1102                            $feed_entry->content(
1103                                    qq{<![CDATA[<style type="text/css">}
1104                                    . $cloud->css
1105                                    . qq{</style>}
1106                                    . $cloud->html( $nr )
1107                                    . qq{]]>}
1108                            );
1109                            $feed->add_entry( $feed_entry );
1110    
1111                    } elsif ( $show eq 'last-tag' ) {
1112    
1113                            $nr ||= $last_x_tags;
1114                            $nr = $last_x_tags if $nr > $last_x_tags;
1115    
1116                            $feed->title( "last $nr tagged messages from $CHANNEL" );
1117                            $feed->description( "collects messages which have tags// in them" );
1118    
1119                            foreach my $m ( @last_tags ) {
1120    #                               warn dump( $m );
1121                                    #my $tags = join(' ', @{$m->{tags}} );
1122                                    my $feed_entry = XML::Feed::Entry->new($type);
1123                                    $feed_entry->title( $m->{nick} . '@' . $m->{time} );
1124                                    $feed_entry->author( $m->{nick} );
1125                                    $feed_entry->link( '/#' . $m->{id}  );
1126                                    $feed_entry->issued( DateTime::Format::Flexible->build( $m->{time} ) );
1127    
1128                                    my $message = $filter->{message}->( $m->{message} );
1129                                    $message .= "<br/>\n" unless $message =~ m!<(/p|br/?)>!;
1130    #                               warn "## message = $message\n";
1131    
1132                                    #$feed_entry->summary(
1133                                    $feed_entry->content(
1134                                            "<![CDATA[$message]]>"
1135                                    );
1136                                    $feed_entry->category( join(', ', @{$m->{tags}}) );
1137                                    $feed->add_entry( $feed_entry );
1138    
1139                                    $nr--;
1140                                    last if $nr <= 0;
1141    
1142                            }
1143    
1144                    } elsif ( $show =~ m/^follow/ ) {
1145    
1146                            $feed->title( "Feeds which this bot follows" );
1147    
1148                            my $sth = $dbh->prepare( qq{ select * from feeds order by last_update desc } );
1149                            $sth->execute;
1150                            while (my $row = $sth->fetchrow_hashref) {
1151                                    my $feed_entry = XML::Feed::Entry->new($type);
1152                                    $feed_entry->title( $row->{name} );
1153                                    $feed_entry->link( $row->{url}  );
1154                                    $feed_entry->issued( DateTime::Format::Flexible->build( $row->{last_update} ) );
1155                                    $feed_entry->content(
1156                                            '<![CDATA[<pre>' . dump( $row ) . '</pre>]]>'
1157                                    );
1158                                    $feed->add_entry( $feed_entry );
1159                            }
1160    
1161                            my $feed_entry = XML::Feed::Entry->new($type);
1162                            $feed_entry->title( "Internal stats" );
1163                            $feed_entry->content(
1164                                    '<![CDATA[<pre>' . dump( $_rss ) . '</pre>]]>'
1165                            );
1166                            $feed->add_entry( $feed_entry );
1167    
1168                    } else {
1169                            _log "unknown rss request ",$request->url;
1170                            return RC_DENY;
1171                    }
1172    
1173                    $response->content( $feed->as_xml );
1174                    return RC_OK;
1175            }
1176    
1177            if ( $@ ) {
1178                    warn "$@";
1179            }
1180    
1181            $response->content_type("text/html; charset=UTF-8");
1182    
1183          my $html =          my $html =
1184                  qq{<html><head><title>$NICK</title><style type="text/css">$style} .                  qq{<html><head><title>$NICK</title><style type="text/css">$style}
1185                  $cloud->css .                  . $cloud->css
1186                  qq{</style></head><body>} .                  . qq{</style></head><body>}
1187                  qq{                  . qq{
1188                  <form method="post" class="search" action="/">                  <form method="post" class="search" action="/">
1189                  <input type="text" name="search" value="$search" size="10">                  <input type="text" name="search" value="$search" size="10">
1190                  <input type="submit" value="search">                  <input type="submit" value="search">
1191                  </form>                  </form>
1192                  } .                  }
1193                  $cloud->html(500) .                  . $cloud->html(500)
1194                  qq{<p>};                  . qq{<p>};
1195          if ($request->url =~ m#/history#) {  
1196            if ($request->url =~ m#/tags?#) {
1197                    # nop
1198            } elsif ($request->url =~ m#/history#) {
1199                  my $sth = $dbh->prepare(qq{                  my $sth = $dbh->prepare(qq{
1200                          select date(time) as date,count(*) as nr                          select date(time) as date,count(*) as nr,sum(length(message)) as len
1201                                  from log                                  from log
1202                                  group by date(time)                                  group by date(time)
1203                                  order by date(time) desc                                  order by date(time) desc
1204                  });                  });
1205                  $sth->execute();                  $sth->execute();
1206                  my ($l_yyyy,$l_mm) = (0,0);                  my ($l_yyyy,$l_mm) = (0,0);
1207                    $html .= qq{<table class="calendar"><tr>};
1208                  my $cal;                  my $cal;
1209                    my $ord = 0;
1210                  while (my $row = $sth->fetchrow_hashref) {                  while (my $row = $sth->fetchrow_hashref) {
1211                          # this is probably PostgreSQL specific, expects ISO date                          # this is probably PostgreSQL specific, expects ISO date
1212                          my ($yyyy,$mm,$dd) = split(/-/, $row->{date});                          my ($yyyy,$mm,$dd) = split(/-/, $row->{date});
1213                          if ($yyyy != $l_yyyy || $mm != $l_mm) {                          if ($yyyy != $l_yyyy || $mm != $l_mm) {
1214                                  $html .= $cal->as_HTML() if ($cal);                                  if ( $cal ) {
1215                                            $html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td>};
1216                                            $ord++;
1217                                            $html .= qq{</tr><tr>} if ( $ord % 3 == 0 );
1218                                    }
1219                                  $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);                                  $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);
1220                                  $cal->border(2);                                  $cal->border(1);
1221                                    $cal->width('30%');
1222                                    $cal->cellheight('5em');
1223                                    $cal->tableclass('month');
1224                                    #$cal->cellclass('day');
1225                                    $cal->sunday('SUN');
1226                                    $cal->saturday('SAT');
1227                                    $cal->weekdays('MON','TUE','WED','THU','FRI');
1228                                  ($l_yyyy,$l_mm) = ($yyyy,$mm);                                  ($l_yyyy,$l_mm) = ($yyyy,$mm);
1229                          }                          }
1230                          $cal->setcontent($dd, qq{                          $cal->setcontent($dd, qq[
1231                                  <a href="/?date=$row->{date}">$row->{nr}</a>                                  <a href="$url?date=$row->{date}">$row->{nr}</a><br/>$row->{len}
1232                          });                          ]) if $cal;
1233                            
1234                  }                  }
1235                  $html .= $cal->as_HTML() if ($cal);                  $html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>};
1236    
1237          } else {          } else {
1238                  $html .= join("</p><p>",                  $html .= join("</p><p>",
1239                          get_from_log(                          get_from_log(
1240                                  limit => $q->param('last') || $q->param('date') ? undef : 100,                                  limit => ( $q->param('last') || $q->param('date') ) ? undef : 100,
1241                                  search => $search || undef,                                  search => $search || undef,
1242                                  tag => $q->param('tag') || undef,                                  tag => $q->param('tag') || undef,
1243                                  date => $q->param('date') || undef,                                  date => $q->param('date') || undef,
1244                                  fmt => {                                  fmt => {
1245                                          date => sub {                                          date => sub {
1246                                                  my $date = shift || return;                                                  my $date = shift || return;
1247                                                  qq{<hr/><div class="date"><a href="/?date=$date">$date</a></div>};                                                  qq{<hr/><div class="date"><a href="$url?date=$date">$date</a></div>};
1248                                          },                                          },
1249                                          time => '<span class="time">%s</span> ',                                          time => '<span class="time">%s</span> ',
1250                                          time_channel => '<span class="channel">%s %s</span> ',                                          time_channel => '<span class="channel">%s %s</span> ',
# Line 732  sub root_handler { Line 1252  sub root_handler {
1252                                          me_nick => '***%s&nbsp;',                                          me_nick => '***%s&nbsp;',
1253                                          message => '<span class="message">%s</span>',                                          message => '<span class="message">%s</span>',
1254                                  },                                  },
1255                                  filter => {                                  filter => $filter,
                                         message => sub {  
                                                 my $m = shift || return;  
                                                 $m =~ s/($escape_re)/$escape{$1}/gs;  
                                                 $m =~ s#($RE{URI}{HTTP})#<a href="$1">$1</a>#gs;  
                                                 $m =~ s#$tag_regex#<a href="?tag=$1" class="tag">$1</a>#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 '<span class="nick col-' .  
                                                         ( $nick_enumerator{$n} % $max_color ) .  
                                                         '">' . $n . '</span>';  
                                         },  
                                 },  
1256                          )                          )
1257                  );                  );
1258          }          }
# Line 761  sub root_handler { Line 1263  sub root_handler {
1263          </body></html>};          </body></html>};
1264    
1265          $response->content( $html );          $response->content( $html );
1266            warn "<< ", $request->method, " ", $request->uri, " created ", length($html), " bytes\n";
1267          return RC_OK;          return RC_OK;
1268  }  }
1269    

Legend:
Removed from v.41  
changed lines
  Added in v.98

  ViewVC Help
Powered by ViewVC 1.1.26