/[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 67 by dpavlin, Sat Sep 29 13:13:41 2007 UTC revision 68 by dpavlin, Sat Sep 29 14:11: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 261  sub get_from_log { Line 268  sub get_from_log {
268          my $sql = $context ? $sql_context : $sql_message;          my $sql = $context ? $sql_context : $sql_message;
269    
270          sub check_date {          sub check_date {
271                  my $date = shift;                  my $date = shift || return;
272                  $date = eval { DateTime::Format::ISO8601->parse_datetime( $args->{date} )->ymd; };                  my $new_date = eval { DateTime::Format::ISO8601->parse_datetime( $date )->ymd; };
273                  if ( $@ ) {                  if ( $@ ) {
274                          warn "invalid date ", $args->{date}, $/;                          warn "invalid date $date\n";
275                          $date = DateTime->now->ymd;                          $new_date = DateTime->now->ymd;
276                  }                  }
277                  return $date;                  return $new_date;
278          }          }
279    
280          $sql .= " where message ilike ? or nick ilike ? " if ($args->{search});          my @where;
281          $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} });          my @args;
         if ($args->{date}) {  
                 $sql .= " where date(time) = ? ";  
                 $args->{date} = check_date( $args->{date} );  
         }  
         $sql .= " order by log.time desc";  
         $sql .= " limit " . $args->{limit} if ($args->{limit});  
282    
         my $sth = $dbh->prepare( $sql );  
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( check_date($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            $sth->execute( @args );
312    
313          my $last_row = {          my $last_row = {
314                  date => '',                  date => '',
315                  time => '',                  time => '',
# Line 954  sub root_handler { Line 970  sub root_handler {
970          } else {          } else {
971                  $html .= join("</p><p>",                  $html .= join("</p><p>",
972                          get_from_log(                          get_from_log(
973                                  limit => $q->param('last') || $q->param('date') ? undef : 100,                                  limit => ( $q->param('last') || $q->param('date') ) ? undef : 100,
974                                  search => $search || undef,                                  search => $search || undef,
975                                  tag => $q->param('tag') || undef,                                  tag => $q->param('tag') || undef,
976                                  date => check_date( $q->param('date') ),                                  date => $q->param('date') || undef,
977                                  fmt => {                                  fmt => {
978                                          date => sub {                                          date => sub {
979                                                  my $date = shift || return;                                                  my $date = shift || return;

Legend:
Removed from v.67  
changed lines
  Added in v.68

  ViewVC Help
Powered by ViewVC 1.1.26