remove any leading ::ffff: on ipv4 addresses
[spider.git] / perl / DXLogPrint.pm
1 #
2 # Log Printing routines
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 #
7 #
8
9 package DXLog;
10
11 use IO::File;
12 use DXVars;
13 use DXDebug qw(dbg isdbg);
14 use DXUtil;
15 use DXLog;
16 use Julian;
17 use RingBuf;
18
19 use strict;
20
21 use vars qw($maxmonths);
22 $maxmonths = 36;
23
24 #
25 # print some items from the log backwards in time
26 #
27 # This command outputs a list of n lines starting from time t with $pattern tags
28 #
29 sub print
30 {
31         my $fcb = $DXLog::log;
32         my $from = shift || 0;
33         my $to = shift || 10;
34         my $jdate = $fcb->unixtoj(shift);
35         my $pattern = shift;
36         my $who = shift;
37         my $search;
38         my @in;
39         my @out = ();
40         my $eval;
41         my $tot = $from + $to;
42         my $hint = "";
43             
44         $who = uc $who if defined $who;
45         
46         if ($pattern) {
47                 $hint = "m{\\Q$pattern\\E}i";
48         } else {
49                 $hint = "!m{\\^(?:ann|rcmd|talk|chat)\\^}";
50         }
51         if ($who) {
52                 $hint .= ' && ' if $hint;
53                 $hint .= 'm{\\Q$who\\E}i';
54         } 
55         $hint = "next unless $hint" if $hint;
56         $hint .= ";next unless /^\\d+\\^$pattern\\^/" if $pattern;
57         $hint ||= "";
58         
59         $eval = qq(while (<\$fh>) {
60                                    $hint;
61                                    chomp;
62                                    \$ring->write(\$_);
63                            } );
64         
65         if (isdbg('search')) {
66                 dbg("sh/log hint: $hint");
67                 dbg("sh/log eval: $eval");
68         }
69         
70         $fcb->close;                                      # close any open files
71
72         my $months;
73         my $fh = $fcb->open($jdate); 
74  L1: for ($months = 0; $months < $maxmonths && @in < $tot; $months++) {
75                 my $ref;
76                 my $ring = RingBuf->new($tot);
77
78                 if ($fh) {
79                         my @tmp;
80                         eval $eval;               # do the search on this file
81                         return ("Log search error", $@) if $@;
82                         
83                         @in = ($ring->readall, @in);
84                         last L1 if @in >= $tot;
85                 }
86
87                 $fh = $fcb->openprev();      # get the next file
88                 last if !$fh;
89         }
90         
91         @in = splice @in, -$tot, $tot if @in > $tot;
92     
93         for (@in) {
94                 my @line = split /\^/ ;
95                 push @out, print_item(\@line);
96         
97         }
98         return @out;
99 }
100
101
102 #
103 # the standard log printing interpreting routine.
104 #
105 # every line that is printed should call this routine to be actually visualised
106 #
107 # Don't really know whether this is the correct place to put this stuff, but where
108 # else is correct?
109 #
110 # I get a reference to an array of items
111 #
112 sub print_item
113 {
114         my $r = shift;
115         my $d = atime($r->[0]);
116         my $s = 'undef';
117         
118         if ($r->[1] eq 'rcmd') {
119                 $r->[6] ||= 'Unknown';
120                 if ($r->[2] eq 'in') {
121                         $r->[5] ||= "";
122                         $s = "in: $r->[4] ($r->[6] priv: $r->[3]) rcmd: $r->[5]";
123                 } else {
124                         $r->[4] ||= "";
125                         $s = "$r->[3] $r->[6] reply: $r->[4]";
126                 }
127         } elsif ($r->[1] eq 'talk') {
128                 $r->[5] ||= "";
129                 $s = "$r->[3] -> $r->[2] ($r->[4]) $r->[5]";
130         } elsif ($r->[1] eq 'ann' || $r->[1] eq 'chat') {
131                 $r->[4] ||= "";
132                 $r->[4] =~ s/^\#\d+ //;
133                 $s = "$r->[3] -> $r->[2] $r->[4]";
134         } else {
135                 $r->[2] ||= "";
136                 $s = "$r->[2]";
137         }
138         return "$d $s";
139 }
140
141 1;