/[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 63 by dpavlin, Fri Jun 8 12:07:45 2007 UTC revision 69 by dpavlin, Fri Dec 7 12:51:55 2007 UTC
# Line 76  use DateTime; Line 76  use DateTime;
76  use URI::Escape;  use URI::Escape;
77  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
78  use DateTime::Format::ISO8601;  use DateTime::Format::ISO8601;
79    use Carp qw/confess/;
80    
81  my $use_twitter = 1;  my $use_twitter = 1;
82  eval { require Net::Twitter; };  eval { require Net::Twitter; };
# Line 88  GetOptions( Line 89  GetOptions(
89          'log:s' => \$log_path,          'log:s' => \$log_path,
90  );  );
91    
92    $SIG{__DIE__} = sub {
93            confess "fatal error";
94    };
95    
96  open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";  open(STDOUT, '>', $log_path) || warn "can't redirect log to $log_path: $!";
97    
98  sub _log {  sub _log {
# Line 230  C<me>, C<nick> and C<message> keys. Line 235  C<me>, C<nick> and C<message> keys.
235  sub get_from_log {  sub get_from_log {
236          my $args = {@_};          my $args = {@_};
237    
238          $args->{fmt} ||= {          if ( ! $args->{fmt} ) {
239                  date => '[%s] ',                  $args->{fmt} = {
240                  time => '{%s} ',                          date => '[%s] ',
241                  time_channel => '{%s %s} ',                          time => '{%s} ',
242                  nick => '%s: ',                          time_channel => '{%s %s} ',
243                  me_nick => '***%s ',                          nick => '%s: ',
244                  message => '%s',                          me_nick => '***%s ',
245          };                          message => '%s',
246                    };
247            }
248    
249          my $sql_message = qq{          my $sql_message = qq{
250                  select                  select
# Line 260  sub get_from_log { Line 267  sub get_from_log {
267    
268          my $sql = $context ? $sql_context : $sql_message;          my $sql = $context ? $sql_context : $sql_message;
269    
270          $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});          sub check_date {
271          $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });                  my $date = shift || return;
272          if ($args->{date}) {                  my $new_date = eval { DateTime::Format::ISO8601->parse_datetime( $date )->ymd; };
273                  $args->{date} = DateTime::Format::ISO8601->parse_datetime( $args->{date} )->ymd;                  if ( $@ ) {
274                  $sql .= " where date(time) = ? ";                          warn "invalid date $date\n";
275                            $new_date = DateTime->now->ymd;
276                    }
277                    return $new_date;
278          }          }
         $sql .= " order by log.time desc";  
         $sql .= " limit " . $args->{limit} if ($args->{limit});  
279    
280          my $sth = $dbh->prepare( $sql );          my @where;
281            my @args;
282    
283          if (my $search = $args->{search}) {          if (my $search = $args->{search}) {
284                  $search =~ s/^\s+//;                  $search =~ s/^\s+//;
285                  $search =~ s/\s+$//;                  $search =~ s/\s+$//;
286                  $sth->execute( ( '%' . $search . '%' ) x 2 );                  push @where, 'message ilike ? or nick ilike ?';
287                  _log "search for '$search' returned ", $sth->rows, " results ", $context || '';                  push @args, ( ( '%' . $search . '%' ) x 2 );
288          } elsif (my $tag = $args->{tag}) {                  _log "search for '$search'";
289                  $sth->execute();          }
290                  _log "tag '$tag' returned ", $sth->rows, " results ", $context || '';  
291          } elsif (my $date = $args->{date}) {          if ($args->{tag} && $tags->{ $args->{tag} }) {
292                  $sth->execute($date);                  push @where, 'id in (' . join(',', @{ $tags->{ $args->{tag} } }) . ')';
293                  _log "found ", $sth->rows, " messages for date $date ", $context || '';                  _log "search for tags $args->{tag}";
         } else {  
                 $sth->execute();  
294          }          }
295    
296            if (my $date = $args->{date} ) {
297                    $date = check_date( $date );
298                    push @where, 'date(time) = ?';
299                    push @args, $date;
300                    _log "search for date $date";
301            }
302    
303            $sql .= " where " . join(" and ", @where) if @where;
304    
305            $sql .= " order by log.time desc";
306            $sql .= " limit " . $args->{limit} if ($args->{limit});
307    
308            #warn "### sql: $sql ", dump( @args );
309    
310            my $sth = $dbh->prepare( $sql );
311            eval { $sth->execute( @args ) };
312            return if $@;
313    
314          my $last_row = {          my $last_row = {
315                  date => '',                  date => '',
316                  time => '',                  time => '',
# Line 483  sub save_message { Line 510  sub save_message {
510  if ($import_dircproxy) {  if ($import_dircproxy) {
511          open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";          open(my $l, $import_dircproxy) || die "can't open $import_dircproxy: $!";
512          warn "importing $import_dircproxy...\n";          warn "importing $import_dircproxy...\n";
513          my $tz_offset = 2 * 60 * 60;    # TZ GMT+2          my $tz_offset = 1 * 60 * 60;    # TZ GMT+2
514          while(<$l>) {          while(<$l>) {
515                  chomp;                  chomp;
516                  if (/^@(\d+)\s(\S+)\s(.+)$/) {                  if (/^@(\d+)\s(\S+)\s(.+)$/) {
# Line 850  hr { border: 1px dashed #ccc; height: 1p Line 877  hr { border: 1px dashed #ccc; height: 1p
877  .col-3 { background: #ff9999 }  .col-3 { background: #ff9999 }
878  .col-4 { background: #ff66ff }  .col-4 { background: #ff66ff }
879  */  */
880    .calendar { border: 1px solid red; width: 100%; }
881    .month { border: 0px; width: 100%; }
882  _END_OF_STYLE_  _END_OF_STYLE_
883    
884  my $max_color = 4;  my $max_color = 4;
# Line 902  sub root_handler { Line 931  sub root_handler {
931                  qq{<p>};                  qq{<p>};
932          if ($request->url =~ m#/history#) {          if ($request->url =~ m#/history#) {
933                  my $sth = $dbh->prepare(qq{                  my $sth = $dbh->prepare(qq{
934                          select date(time) as date,count(*) as nr                          select date(time) as date,count(*) as nr,sum(length(message)) as len
935                                  from log                                  from log
936                                  group by date(time)                                  group by date(time)
937                                  order by date(time) desc                                  order by date(time) desc
938                  });                  });
939                  $sth->execute();                  $sth->execute();
940                  my ($l_yyyy,$l_mm) = (0,0);                  my ($l_yyyy,$l_mm) = (0,0);
941                    $html .= qq{<table class="calendar"><tr>};
942                  my $cal;                  my $cal;
943                    my $ord = 0;
944                  while (my $row = $sth->fetchrow_hashref) {                  while (my $row = $sth->fetchrow_hashref) {
945                          # this is probably PostgreSQL specific, expects ISO date                          # this is probably PostgreSQL specific, expects ISO date
946                          my ($yyyy,$mm,$dd) = split(/-/, $row->{date});                          my ($yyyy,$mm,$dd) = split(/-/, $row->{date});
947                          if ($yyyy != $l_yyyy || $mm != $l_mm) {                          if ($yyyy != $l_yyyy || $mm != $l_mm) {
948                                  $html .= $cal->as_HTML() if ($cal);                                  if ( $cal ) {
949                                            $html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td>};
950                                            $ord++;
951                                            $html .= qq{</tr><tr>} if ( $ord % 3 == 0 );
952                                    }
953                                  $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);                                  $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy);
954                                  $cal->border(2);                                  $cal->border(1);
955                                    $cal->width('30%');
956                                    $cal->cellheight('5em');
957                                    $cal->tableclass('month');
958                                    #$cal->cellclass('day');
959                                    $cal->sunday('SUN');
960                                    $cal->saturday('SAT');
961                                    $cal->weekdays('MON','TUE','WED','THU','FRI');
962                                  ($l_yyyy,$l_mm) = ($yyyy,$mm);                                  ($l_yyyy,$l_mm) = ($yyyy,$mm);
963                          }                          }
964                          $cal->setcontent($dd, qq{                          $cal->setcontent($dd, qq{
965                                  <a href="/?date=$row->{date}">$row->{nr}</a>                                  <a href="/?date=$row->{date}">$row->{nr}</a><br/>$row->{len}
966                          });                          });
967                            
968                  }                  }
969                  $html .= $cal->as_HTML() if ($cal);                  $html .= qq{<td valign="top">} . $cal->as_HTML() . qq{</td></tr></table>};
970    
971          } else {          } else {
972                  $html .= join("</p><p>",                  $html .= join("</p><p>",
973                          get_from_log(                          get_from_log(
974                                  limit => $q->param('last') || $q->param('date') ? undef : 100,                                  limit => ( $q->param('last') || $q->param('date') ) ? undef : 100,
975                                  search => $search || undef,                                  search => $search || undef,
976                                  tag => $q->param('tag') || undef,                                  tag => $q->param('tag') || undef,
977                                  date => $q->param('date') || undef,                                  date => $q->param('date') || undef,

Legend:
Removed from v.63  
changed lines
  Added in v.69

  ViewVC Help
Powered by ViewVC 1.1.26