perl paranoia about @INC
[exim.git] / src / src / exiqgrep.src
1 #!PERL_COMMAND
2
3 # Utility for searching and displaying queue information.
4 # Written by Matt Hubbard 15 August 2002
5
6 # Except when they appear in comments, the following placeholders in this
7 # source are replaced when it is turned into a runnable script:
8 #
9 # BIN_DIRECTORY
10 # PERL_COMMAND
11
12 # PROCESSED_FLAG
13
14
15 # Routine for extracting the UTC timestamp from message ID
16 # lifted from eximstat utility
17
18 # Version 1.2
19
20 use strict;
21 BEGIN { pop @INC if $INC[-1] eq '.' };
22 use Getopt::Std;
23
24 # Have this variable point to your exim binary.
25 my $exim = 'BIN_DIRECTORY/exim';
26 my $eargs = '-bpu';
27 my %id;
28 my %opt;
29 my $count = 0;
30 my $mcount = 0;
31 my @tab62 =
32   (0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,     # 0-9
33    0,10,11,12,13,14,15,16,17,18,19,20,  # A-K
34   21,22,23,24,25,26,27,28,29,30,31,32,  # L-W
35   33,34,35, 0, 0, 0, 0, 0,              # X-Z
36    0,36,37,38,39,40,41,42,43,44,45,46,  # a-k
37   47,48,49,50,51,52,53,54,55,56,57,58,  # l-w
38   59,60,61);                            # x-z
39
40 my $base;
41 if ($^O eq 'darwin') { # aka MacOS X
42   $base = 36;
43  } else {
44   $base = 62;
45 };
46
47 getopts('hf:r:y:o:s:C:zxlibRca',\%opt);
48 if ($ARGV[0]) { &help; exit;}
49 if ($opt{h}) { &help; exit;}
50 if ($opt{a}) { $eargs = '-bp'; }
51 if ($opt{C} && -e $opt{C} && -f $opt{C} && -R $opt{C}) { $eargs .= ' -C '.$opt{C}; }
52
53 # Read message queue output into hash
54 &collect();
55 # Identify which messages match selection criteria
56 &selection();
57 # Print matching data according to display option.
58 &display();
59 exit;
60
61
62 sub help() {
63         print <<'EOF'
64 Exim message queue display utility.
65
66         -h              This help message.
67         -C              Specify which exim.conf to use.
68
69 Selection criteria:
70         -f <regexp>     Match sender address sender (field is "< >" wrapped)
71         -r <regexp>     Match recipient address
72         -s <regexp>     Match against the size field from long output
73         -y <seconds>    Message younger than
74         -o <seconds>    Message older than
75         -z              Frozen messages only (exclude non-frozen)
76         -x              Non-frozen messages only (exclude frozen)
77
78 [ NB: for regexps, provided string sits in /<string>/ ]
79
80 Display options:
81         -c              Display match count
82         -l              Long Format [Default]
83         -i              Message IDs only
84         -b              Brief Format
85         -R              Reverse order
86         -a              All recipients (including delivered)
87 EOF
88 }
89
90 sub collect() {
91         open(QUEUE,"$exim $eargs |") or die("Error opening pipe: $!\n");
92         while(<QUEUE>) {
93                 chomp();
94                 my $line = $_;
95                 #Should be 1st line of record, if not error.
96                 if ($line =~ /^\s*(\w+)\s+((?:\d+(?:\.\d+)?[A-Z]?)?)\s*(\w{6}-\w{6}-\w{2})\s+(<.*?>)/) {
97                         my $msg = $3;
98                         $id{$msg}{age} = $1;
99                         $id{$msg}{size} = $2;
100                         $id{$msg}{from} = $4;
101                         $id{$msg}{birth} = &msg_utc($msg);
102                         $id{$msg}{ages} = time - $id{$msg}{birth};
103                         if ($line =~ /\*\*\* frozen \*\*\*$/) {
104                                 $id{$msg}{frozen} = 1;
105                         } else {
106                                 $id{$msg}{frozen} = 0;
107                         }
108                         while(<QUEUE> =~ /\s+(.*?\@.*)$/) {
109                                 push(@{$id{$msg}{rcpt}},$1);
110                         }
111                         # Increment message counter.
112                         $count++;
113                 } else {
114                         print STDERR "Line mismatch: $line\n"; exit 1;
115                 }
116         }
117         close(QUEUE) or die("Error closing pipe: $!\n");
118 }
119
120 sub selection() {
121         foreach my $msg (keys(%id)) {
122                 if ($opt{f}) {
123                         # Match sender address
124                         next unless ($id{$msg}{from} =~ /$opt{f}/i);
125                 }
126                 if ($opt{r}) {
127                         # Match any recipient address
128                         my $match = 0;
129                         foreach my $rcpt (@{$id{$msg}{rcpt}}) {
130                                 $match++ if ($rcpt =~ /$opt{r}/i);
131                         }
132                         next unless ($match);
133                 }
134                 if ($opt{s}) {
135                         # Match against the size string.
136                         next unless ($id{$msg}{size} =~ /$opt{s}/);
137                 }
138                 if ($opt{y}) {
139                         # Match younger than
140                         next unless ($id{$msg}{ages} < $opt{y});
141                 }
142                 if ($opt{o}) {
143                         # Match older than
144                         next unless ($id{$msg}{ages} > $opt{o});
145                 }
146                 if ($opt{z}) {
147                         # Exclude non frozen
148                         next unless ($id{$msg}{frozen});
149                 }
150                 if ($opt{x}) {
151                         # Exclude frozen
152                         next if ($id{$msg}{frozen});
153                 }
154                 # Here's what we do to select the record.
155                 # Should only get this far if the message passed all of
156                 # the active tests.
157                 $id{$msg}{d} = 1;
158                 # Increment match counter.
159                 $mcount++;
160         }
161 }
162
163 sub display() {
164         if ($opt{c}) {
165                 printf("%d matches out of %d messages\n",$mcount,$count);
166                 exit;
167         }
168         foreach my $msg (sort { $opt{R} ? $id{$b}{birth} <=> $id{$a}{birth} : $id{$a}{birth} <=> $id{$b}{birth} } keys(%id) ) {
169                 if (exists($id{$msg}{d})) {
170                         if ($opt{i}) {
171                                 # Just the msg ID
172                                 print $msg, "\n";
173                         } elsif ($opt{b}) {
174                                 # Brief format
175                                 printf("%s From: %s To: %s\n",$msg,$id{$msg}{from},join(';',@{$id{$msg}{rcpt}}))
176                         } else {
177                                 # Otherwise Long format attempted duplication of original format.
178                                 printf("%3s %5s %s %s%s\n",$id{$msg}{age},$id{$msg}{size},$msg,$id{$msg}{from},$id{$msg}{frozen} ? " *** frozen ***" : "");
179                                 foreach my $rcpt (@{$id{$msg}{rcpt}}) {
180                                         printf("          %s\n",$rcpt);
181                                 }
182                                 print "\n";
183                         }
184                 }
185         }
186 }
187
188 sub report() {
189         foreach my $msg (keys(%id)) {
190                 print "$id{$msg}{birth} $msg\tAge: $id{$msg}{age}\tSize: $id{$msg}{size}\tFrom: $id{$msg}{from}\tTo: " . join(" ",@{$id{$msg}{rcpt}}). "\n";
191         }
192 }
193
194 sub msg_utc() {
195         my $id = substr((pop @_), 0, 6);
196         my $s = 0;
197         my @c = split(//, $id);
198         while($#c >= 0) { $s = $s * $base + $tab62[ord(shift @c) - ord('0')] }
199         return $s;
200 }