added chat stuff for fun
[spider.git] / perl / DXLogPrint.pm
index 54cfc516b2173a2ea850ff3b585d4976df25eaaf..752e72acd64aaa5470d0d3cae4cb619041d0ed4a 100644 (file)
@@ -8,16 +8,21 @@
 
 package DXLog;
 
-use FileHandle;
+use IO::File;
 use DXVars;
-use DXDebug ();
+#use DXDebug ();
 use DXUtil;
 use DXLog;
 use Julian;
-use Carp;
 
 use strict;
 
+use vars qw($VERSION $BRANCH);
+$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
+$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
+$main::build += $VERSION;
+$main::branch += $BRANCH;
+
 #
 # print some items from the log backwards in time
 #
@@ -25,50 +30,61 @@ use strict;
 #
 sub print
 {
-       my $self = $DXLog::log;
-       my $from = shift;
-       my $to = shift;
-       my @date = $self->unixtoj(shift);
+       my $fcb = $DXLog::log;
+       my $from = shift || 0;
+       my $to = shift || 20;
+       my $jdate = $fcb->unixtoj(shift);
        my $pattern = shift;
+       my $who = uc shift;
        my $search;
        my @in;
-       my @out;
+       my @out = ();
        my $eval;
-       my $count;
+       my $tot = $from + $to;
+       my $hint = "";
            
-       $search = $pattern ? "\$ref->[1] =~ /$pattern/" : '1' ;
-       $eval = qq(
-                          my \$c;
-                          my \$ref;
-                          for (\$c = \$#in; \$c >= 0; \$c--) {
-                                       \$ref = \$in[\$c];
-                                       if ($search) {
-                                               \$count++;
-                                               next if \$count < $from;
-                                               push \@out, print_item(\$ref);
-                                               last LOOP if \$count >= \$to;                  # stop after n
-                                       }
-                               }
-                         );
+       if ($pattern) {
+               $hint = "m{\\Q$pattern\\E}i";
+       } else {
+               $hint = "!m{ann|rcmd|talk|chat}";
+       }
+       if ($who) {
+               $hint .= ' && ' if $hint;
+               $hint .= 'm{\\Q$who\\E}i';
+       } 
+       $hint = "next unless $hint" if $hint;
+       $hint .= ";next unless /^\\d+\\^$pattern\\^/" if $pattern;
+       $hint ||= "";
+       
+       $eval = qq(while (<\$fh>) {
+                                  $hint;
+                                  chomp;
+                                  push \@tmp, \$_;
+                          } );
        
-       $self->close;                                      # close any open files
+       $fcb->close;                                      # close any open files
 
-       my $fh = $self->open(@date); 
-LOOP:
-       while ($count < $to) {
-               my @spots = ();
+       my $fh = $fcb->open($jdate); 
+ L1: for (;@in < $to;) {
+               my $ref;
                if ($fh) {
-                       while (<$fh>) {
-                               chomp;
-                               push @in, [ split '\^' ];
-                       }
+                       my @tmp;
                        eval $eval;               # do the search on this file
-                       return ("Spot search error", $@) if $@;
+                       return ("Log search error", $@) if $@;
+                       @in = (@tmp, @in);
+                       if (@in > $to) {
+                               @in = splice @in, -$to, $to;
+                               last L1;
+                       } 
                }
-               $fh = $self->openprev();      # get the next file
+               $fh = $fcb->openprev();      # get the next file
                last if !$fh;
        }
-
+       for (@in) {
+               my @line = split /\^/ ;
+               push @out, print_item(\@line);
+       
+       }
        return @out;
 }
 
@@ -85,22 +101,27 @@ LOOP:
 sub print_item
 {
        my $r = shift;
-       my @ref = @$r;
-       my $d = atime($ref[0]);
+       my $d = atime($r->[0]);
        my $s = 'undef';
        
-       if ($ref[1] eq 'rcmd') {
-               if ($ref[2] eq 'in') {
-                       $s = "$ref[4] (priv: $ref[3]) rcmd: $ref[5]";
+       if ($r->[1] eq 'rcmd') {
+               if ($r->[2] eq 'in') {
+                       $r->[5] ||= "";
+                       $s = "$r->[4] (priv: $r->[3]) rcmd: $r->[5]";
                } else {
-                       $s = "$ref[3] reply: $ref[4]";
+                       $r->[4] ||= "";
+                       $s = "$r->[3] reply: $r->[4]";
                }
-       } elsif ($ref[1] eq 'talk') {
-               $s = "$ref[3] -> $ref[2] ($ref[4]) $ref[5]";
-       } elsif ($ref[1] eq 'ann') {
-               $s = "$ref[3] -> $ref[2] $ref[4]";
+       } elsif ($r->[1] eq 'talk') {
+               $r->[5] ||= "";
+               $s = "$r->[3] -> $r->[2] ($r->[4]) $r->[5]";
+       } elsif ($r->[1] eq 'ann' || $r->[1] eq 'chat') {
+               $r->[4] ||= "";
+               $r->[4] =~ s/^\#\d+ //;
+               $s = "$r->[3] -> $r->[2] $r->[4]";
        } else {
-               $s = "$ref[2]";
+               $r->[2] ||= "";
+               $s = "$r->[2]";
        }
        return "$d $s";
 }