/[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

revision 34 by dpavlin, Sat Jun 24 22:57:26 2006 UTC revision 35 by dpavlin, Sun Jun 25 00:10:13 2006 UTC
# Line 54  use Regexp::Common qw /URI/; Line 54  use Regexp::Common qw /URI/;
54  use CGI::Simple;  use CGI::Simple;
55  use HTML::TagCloud;  use HTML::TagCloud;
56  use POSIX qw/strftime/;  use POSIX qw/strftime/;
57    use HTML::CalendarMonthSimple;
58    
59  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
60    
# Line 122  C<context> defines number of messages ar Line 123  C<context> defines number of messages ar
123  sub get_from_log {  sub get_from_log {
124          my $args = {@_};          my $args = {@_};
125    
         $args->{limit} ||= 10;  
   
126          $args->{fmt} ||= {          $args->{fmt} ||= {
127                  date => '[%s] ',                  date => '[%s] ',
128                  time => '{%s} ',                  time => '{%s} ',
# Line 156  sub get_from_log { Line 155  sub get_from_log {
155    
156          $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});          $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});
157          $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });          $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });
158            $sql .= " where date(time) = ? " if ($args->{date});
159          $sql .= " order by log.time desc";          $sql .= " order by log.time desc";
160          $sql .= " limit " . $args->{limit};          $sql .= " limit " . $args->{limit} if ($args->{limit});
161    
162          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
163          if (my $search = $args->{search}) {          if (my $search = $args->{search}) {
# Line 168  sub get_from_log { Line 168  sub get_from_log {
168          } elsif (my $tag = $args->{tag}) {          } elsif (my $tag = $args->{tag}) {
169                  $sth->execute();                  $sth->execute();
170                  warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";                  warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n";
171            } elsif (my $date = $args->{date}) {
172                    $sth->execute($date);
173                    warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n";
174          } else {          } else {
175                  $sth->execute();                  $sth->execute();
176          }          }
# Line 212  sub get_from_log { Line 215  sub get_from_log {
215                  }                  }
216          }          }
217    
218            # sprintf which can take coderef as first parametar
219            sub cr_sprintf {
220                    my $fmt = shift || return;
221                    if (ref($fmt) eq 'CODE') {
222                            $fmt->(@_);
223                    } else {
224                            sprintf($fmt, @_);
225                    }
226            }
227    
228          foreach my $row (@rows) {          foreach my $row (@rows) {
229    
230                  $row->{time} =~ s#\.\d+##;                  $row->{time} =~ s#\.\d+##;
231    
232                  my $msg = '';                  my $msg = '';
233    
234                  $msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date});                  $msg = cr_sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date});
235                  my $t = $row->{time};                  my $t = $row->{time};
236    
237                  if ($last_row->{channel} ne $row->{channel}) {                  if ($last_row->{channel} ne $row->{channel}) {
238                          $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});                          $msg .= cr_sprintf($args->{fmt}->{time_channel}, $t, $row->{channel});
239                  } else {                  } else {
240                          $msg .= sprintf($args->{fmt}->{time}, $t);                          $msg .= cr_sprintf($args->{fmt}->{time}, $t);
241                  }                  }
242    
243                  my $append = 1;                  my $append = 1;
# Line 241  sub get_from_log { Line 254  sub get_from_log {
254    
255                          $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE');                          $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE');
256    
257                          $msg .= sprintf( $fmt, $nick );                          $msg .= cr_sprintf( $fmt, $nick );
258                          $append = 0;                          $append = 0;
259                  }                  }
260    
261                  $args->{fmt}->{message} ||= '%s';                  $args->{fmt}->{message} ||= '%s';
262                  if (ref($args->{filter}->{message}) eq 'CODE') {                  if (ref($args->{filter}->{message}) eq 'CODE') {
263                          $msg .= sprintf($args->{fmt}->{message},                          $msg .= cr_sprintf($args->{fmt}->{message},
264                                  $args->{filter}->{message}->(                                  $args->{filter}->{message}->(
265                                          $row->{message}                                          $row->{message}
266                                  )                                  )
267                          );                          );
268                  } else {                  } else {
269                          $msg .= sprintf($args->{fmt}->{message}, $row->{message});                          $msg .= cr_sprintf($args->{fmt}->{message}, $row->{message});
270                  }                  }
271    
272                  if ($append && @msgs) {                  if ($append && @msgs) {
# Line 544  p { margin: 0; padding: 0.1em; } Line 557  p { margin: 0; padding: 0.1em; }
557  .col-4 { background: #ff66ff }  .col-4 { background: #ff66ff }
558  a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none }  a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none }
559  a:hover.tag { border: 1px solid #eee }  a:hover.tag { border: 1px solid #eee }
560    hr { border: 1px dashed #ccc; height: 1px; clear: both; }
561  _END_OF_STYLE_  _END_OF_STYLE_
562    
563  my $max_color = 4;  my $max_color = 4;
# Line 567  sub root_handler { Line 581  sub root_handler {
581    
582          my $search = $q->param('search') || $q->param('grep') || '';          my $search = $q->param('search') || $q->param('grep') || '';
583    
584          $response->content(          my $html =
585                  qq{<html><head><title>$NICK</title><style type="text/css">$style} .                  qq{<html><head><title>$NICK</title><style type="text/css">$style} .
586                  $cloud->css .                  $cloud->css .
587                  qq{</style></head><body>} .                  qq{</style></head><body>} .
# Line 578  sub root_handler { Line 592  sub root_handler {
592                  </form>                  </form>
593                  } .                  } .
594                  $cloud->html(500) .                  $cloud->html(500) .
595                  qq{<p>} .                  qq{<p>};
596                  join("</p><p>",          if ($request->url =~ m#/history#) {
597                    my $sth = $dbh->prepare(qq{
598                            select date(time) as date,count(*) as nr
599                                    from log
600                                    group by date(time)
601                                    order by date(time) desc
602                    });
603                    $sth->execute();
604                    my ($l_yyyy,$l_mm) = (0,0);
605                    my $cal;
606                    while (my $row = $sth->fetchrow_hashref) {
607                            # this is probably PostgreSQL specific, expects ISO date
608                            my ($yyyy,$mm,$dd) = split(/-/, $row->{date});
609                            if ($yyyy != $l_yyyy || $mm != $l_mm) {
610                                    $html .= $cal->as_HTML() if ($cal);
611                                    $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);
612                                    $cal->border(2);
613                                    ($l_yyyy,$l_mm) = ($yyyy,$mm);
614                            }
615                            $cal->setcontent($dd, qq{
616                                    <a href="/?date=$row->{date}">$row->{nr}</a>
617                            });
618                    }
619                    $html .= $cal->as_HTML() if ($cal);
620    
621            } else {
622                    $html .= join("</p><p>",
623                          get_from_log(                          get_from_log(
624                                  limit => $q->param('last') || 100,                                  limit => $q->param('last') || $q->param('date') ? undef : 100,
625                                  search => $search || undef,                                  search => $search || undef,
626                                  tag => $q->param('tag') || undef,                                  tag => $q->param('tag') || undef,
627                                    date => $q->param('date') || undef,
628                                  fmt => {                                  fmt => {
629                                          date => '<hr size="1" style="clear: both;"/><div class="date">%s</div> ',                                          date => sub {
630                                                    my $date = shift || return;
631                                                    qq{<hr/><div class="date"><a href="/?date=$date">$date</a></div> '};
632                                            },
633                                          time => '<span class="time">%s</span> ',                                          time => '<span class="time">%s</span> ',
634                                          time_channel => '<span class="channel">%s %s</span> ',                                          time_channel => '<span class="channel">%s %s</span> ',
635                                          nick => '%s:&nbsp;',                                          nick => '%s:&nbsp;',
# Line 612  sub root_handler { Line 656  sub root_handler {
656                                          },                                          },
657                                  },                                  },
658                          )                          )
659                  ) .                  );
660                  qq{</p></body></html>}          }
661          );  
662            $html .= qq{</p>
663            <hr/>
664            <p>See <a href="/history">history</a> of all messages.</p>
665            </body></html>};
666    
667            $response->content( $html );
668          return RC_OK;          return RC_OK;
669  }  }
670    

Legend:
Removed from v.34  
changed lines
  Added in v.35

  ViewVC Help
Powered by ViewVC 1.1.26