Start
[exim.git] / src / src / exipick.src
1 #!PERL_COMMAND
2 # $Cambridge: exim/src/src/exipick.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $
3
4 # This variable should be set by the building process to Exim's spool directory.
5 my $spool = 'SPOOL_DIRECTORY';
6
7 use strict;
8 use Getopt::Long;
9
10 my($p_name) = $0 =~ m|/?([^/]+)$|;
11 my $p_version = "20040725.0";
12 my $p_usage = "Usage: $p_name [--help|--version] (see --help for details)";
13 my $p_cp = <<EOM;
14 Copyright (c) 2003-2004 John Jetmore <jj33\@pobox.com>
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 EOM
30 ext_usage(); # before we do anything else, check for --help
31
32 Getopt::Long::Configure("bundling_override");
33 GetOptions(
34 'spool:s' => \$G::spool, # exim spool dir
35 'bp' => \$G::mailq_bp, # List the queue (noop - default)
36 'bpa' => \$G::mailq_bpa, # ... with generated address as well
37 'bpc' => \$G::mailq_bpc, # ... but just show a count of messages
38 'bpr' => \$G::mailq_bpr, # ... do not sort
39 'bpra' => \$G::mailq_bpra, # ... with generated addresses, unsorted
40 'bpru' => \$G::mailq_bpru, # ... only undelivered addresses, unsorted
41 'bpu' => \$G::mailq_bpu, # ... only undelivered addresses
42 'and' => \$G::and, # 'and' the criteria (default)
43 'or' => \$G::or, # 'or' the criteria
44 'f:s' => \$G::qgrep_f, # from regexp
45 'r:s' => \$G::qgrep_r, # recipient regexp
46 #'s:s' => \$G::qgrep_s, # match against size field
47 'y:s' => \$G::qgrep_y, # message younger than (secs)
48 'o:s' => \$G::qgrep_o, # message older than (secs)
49 'z' => \$G::qgrep_z, # frozen only
50 'x' => \$G::qgrep_x, # non-frozen only
51 'c' => \$G::qgrep_c, # display match count
52 'l' => \$G::qgrep_l, # long format (default)
53 'i' => \$G::qgrep_i, # message ids only
54 'b' => \$G::qgrep_b, # brief format
55 'flatq' => \$G::flatq, # brief format
56 'show-vars:s' => \$G::show_vars, # display the contents of these vars
57 'show-rules' => \$G::show_rules # display compiled match rules
58 ) || exit(1);
59
60 push(@ARGV, "\$sender_address =~ /$G::qgrep_f/") if ($G::qgrep_f);
61 push(@ARGV, "\$recipients =~ /$G::qgrep_r/") if ($G::qgrep_r);
62 push(@ARGV, "\$message_age < $G::qgrep_y") if ($G::qgrep_y);
63 push(@ARGV, "\$message_age > $G::qgrep_o") if ($G::qgrep_o);
64 push(@ARGV, "\$deliver_freeze") if ($G::qgrep_z);
65 push(@ARGV, "!\$deliver_freeze") if ($G::qgrep_x);
66 $G::mailq_bp = $G::mailq_bp; # shut up -w
67 $G::and = $G::and; # shut up -w
68 $spool = $G::spool if ($G::spool);
69 my $count_only = 1 if ($G::mailq_bpc || $G::qgrep_c);
70 my $unsorted = 1 if ($G::mailq_bpr || $G::mailq_bpra || $G::mailq_bpru);
71 my $msg = get_all_msgs($spool, $unsorted);
72 my $crit = process_criteria(\@ARGV);
73 my $e = Exim::SpoolFile->new();
74 my $tcount = 0 if ($count_only);
75 my $mcount = 0 if ($count_only);
76 $e->set_spool($spool);
77 $e->set_undelivered_only(1) if ($G::mailq_bpru || $G::mailq_bpu);
78 $e->set_show_generated(1) if ($G::mailq_bpa || $G::mailq_bpra);
79 $e->output_long() if ($G::qgrep_l);
80 $e->output_idonly() if ($G::qgrep_i);
81 $e->output_brief() if ($G::qgrep_b);
82 $e->output_flatq() if ($G::flatq);
83 $e->set_show_vars($G::show_vars) if ($G::show_vars);
84
85 MSG:
86 foreach my $m (@$msg) {
87 if (!$e->parse_message($m->{message})) {
88 warn "Couldn't parse $m->{message}: ".$e->error()."\n";
89 next(MSG);
90 }
91 $tcount++;
92 my $match = 0;
93 CRITERIA:
94 foreach my $c (@$crit) {
95 my $var = $e->get_var($c->{var});
96 my $ret = eval($c->{cmp});
97 if ($@) {
98 print STDERR "Error in eval '$c->{cmp}': $@\n";
99 next(MSG);
100 } elsif ($ret) {
101 $match = 1;
102 if ($G::or) { last(CRITERIA); }
103 else { next(CRITERIA); }
104 } else { # no match
105 if ($G::or) { next(CRITERIA); }
106 else { next(MSG); }
107 }
108 }
109 next(MSG) if (scalar(@$crit) > 0 && !$match);
110
111 if ($count_only) {
112 $mcount++;
113 } else {
114 $e->print_message(\*STDOUT);
115 }
116 }
117
118 if ($G::mailq_bpc) {
119 print "$tcount\n";
120 } elsif ($G::qgrep_c) {
121 print "$mcount matches out of $tcount messages\n";
122 }
123
124 exit;
125
126 sub process_criteria {
127 my $a = shift;
128 my @c = ();
129 my $e = 0;
130
131 foreach (@$a) {
132 foreach my $t ('@') { s/$t/\\$t/g; } # '$'
133 if (/^(.*?)\s+(<=|>=|==|!=|<|>)\s+(.*)$/) {
134 #print STDERR "found as integer\n";
135 my $v = $1; my $o = $2; my $n = $3;
136 if ($n =~ /^([\d\.]+)M$/) { $n = $1 * 1024 * 1024; }
137 elsif ($n =~ /^([\d\.]+)K$/) { $n = $1 * 1024; }
138 elsif ($n =~ /^([\d\.]+)B?$/) { $n = $1; }
139 elsif ($n =~ /^([\d\.]+)d$/) { $n = $1 * 60 * 60 * 24; }
140 elsif ($n =~ /^([\d\.]+)h$/) { $n = $1 * 60 * 60; }
141 elsif ($n =~ /^([\d\.]+)m$/) { $n = $1 * 60; }
142 elsif ($n =~ /^([\d\.]+)s?$/) { $n = $1; }
143 else {
144 print STDERR "Expression $_ did not parse: numeric comparison with ",
145 "non-number\n";
146 $e = 1;
147 next;
148 }
149 push(@c, { var => lc($v), cmp => "(\$var $o $n) ? 1 : 0" });
150 } elsif (/^(.*?)\s+(=~|!~)\s+(.*)$/) {
151 #print STDERR "found as string regexp\n";
152 push(@c, { var => lc($1), cmp => "(\"\$var\" $2 $3) ? 1 : 0" });
153 } elsif (/^(.*?)\s+=\s+(.*)$/) {
154 #print STDERR "found as bare string regexp\n";
155 push(@c, { var => lc($1), cmp => "(\"\$var\" =~ /$2/) ? 1 : 0" });
156 } elsif (/^(.*?)\s+(eq|ne)\s+(.*)$/) {
157 #print STDERR "found as string cmp\n";
158 push(@c, { var => lc($1), cmp => "(\"\$var\" $2 \"$3\") ? 1 : 0" });
159 } elsif (/^(!)?(\S+)$/) {
160 #print STDERR "found as boolean\n";
161 push(@c, { var => lc($2), cmp => "($1\$var) ? 1 : 0" });
162 } else {
163 print STDERR "Expression $_ did not parse\n";
164 $e = 1;
165 }
166 }
167
168 exit(1) if ($e);
169
170 if ($G::show_rules) { foreach (@c) { print "$_->{var}\t$_->{cmp}\n"; } }
171
172 return(\@c);
173 }
174
175 sub get_all_msgs {
176 my $d = shift() . '/input';
177 my $u = shift;
178 my @m = ();
179
180 opendir(D, "$d") || die "Couldn't opendir $d: $!\n";
181 foreach my $e (grep !/^\./, readdir(D)) {
182 if ($e =~ /^[a-zA-Z0-9]$/) {
183 opendir(DD, "$d/$e") || next;
184 foreach my $f (grep !/^\./, readdir(DD)) {
185 push(@m, { message => $1, path => "$e/$1" }) if ($f =~ /^(.{16})-H$/);
186 }
187 closedir(DD);
188 } elsif ($e =~ /^(.{16})-H$/) {
189 push(@m, { message => $1, path => $1 });
190 }
191 }
192 closedir(D);
193
194 return($u ? \@m : [ sort { $a->{message} cmp $b->{message} } @m ]);
195 }
196
197 BEGIN {
198
199 package Exim::SpoolFile;
200
201 $Exim::SpoolFile::ACL_C_MAX = 10;
202 #$Exim::SpoolFile::ACL_M_MAX = 10;
203
204 sub new {
205 my $class = shift;
206 my $self = {};
207 bless($self, $class);
208
209 $self->{_spool_dir} = '';
210 $self->{_undelivered_only} = 0;
211 $self->{_show_generated} = 0;
212 $self->{_output_long} = 1;
213 $self->{_output_idonly} = 0;
214 $self->{_output_brief} = 0;
215 $self->{_output_flatq} = 0;
216 $self->{_show_vars} = {};
217
218 $self->_reset();
219 return($self);
220 }
221
222 sub output_long {
223 my $self = shift;
224
225 $self->{_output_long} = 1;
226 $self->{_output_idonly} = 0;
227 $self->{_output_brief} = 0;
228 $self->{_output_flatq} = 0;
229 }
230
231 sub output_idonly {
232 my $self = shift;
233
234 $self->{_output_long} = 0;
235 $self->{_output_idonly} = 1;
236 $self->{_output_brief} = 0;
237 $self->{_output_flatq} = 0;
238 }
239
240 sub output_brief {
241 my $self = shift;
242
243 $self->{_output_long} = 0;
244 $self->{_output_idonly} = 0;
245 $self->{_output_brief} = 1;
246 $self->{_output_flatq} = 0;
247 }
248
249 sub output_flatq {
250 my $self = shift;
251
252 $self->{_output_long} = 0;
253 $self->{_output_idonly} = 0;
254 $self->{_output_brief} = 0;
255 $self->{_output_flatq} = 1;
256 }
257
258 sub set_show_vars {
259 my $self = shift;
260 my $s = shift;
261
262 foreach my $v (split(/\s*,\s*/, $s)) {
263 $self->{_show_vars}{$v}++;
264 }
265 }
266
267 sub set_show_generated {
268 my $self = shift;
269 $self->{_show_generated} = shift;
270 }
271
272 sub set_undelivered_only {
273 my $self = shift;
274 $self->{_undelivered_only} = shift;
275 }
276
277 sub error {
278 my $self = shift;
279 return $self->{_error};
280 }
281
282 sub _error {
283 my $self = shift;
284 $self->{_error} = shift;
285 return(undef);
286 }
287
288 sub _reset {
289 my $self = shift;
290
291 $self->{_error} = '';
292 $self->{_delivered} = 0;
293 $self->{_message} = '';
294 $self->{_path} = '';
295 $self->{_vars} = {};
296
297 $self->{_numrecips} = 0;
298 $self->{_udel_tree} = {};
299 $self->{_del_tree} = {};
300 $self->{_recips} = {};
301
302 return($self);
303 }
304
305 sub parse_message {
306 my $self = shift;
307
308 $self->_reset();
309 $self->{_message} = shift || return(0);
310 return(0) if (!$self->{_spool_dir});
311 if (!$self->_find_path()) {
312 # assume the message was delivered from under us and ignore
313 $self->{_delivered} = 1;
314 return(1);
315 }
316 $self->_parse_header() || return(0);
317
318 return(1);
319 }
320
321 sub _find_path {
322 my $self = shift;
323
324 return(0) if (!$self->{_message});
325 return(0) if (!$self->{_spool_dir});
326
327 foreach my $f ('', substr($self->{_message}, 5, 1).'/') {
328 if (-f $self->{_spool_dir} . "/input/$f" . $self->{_message} . '-H') {
329 $self->{_path} = $self->{_spool_dir} . "/input/$f";
330 return(1);
331 }
332 }
333 return(0);
334 }
335
336 sub set_spool {
337 my $self = shift;
338 $self->{_spool_dir} = shift;
339 }
340
341 # accepts a variable with or without leading '$' or trailing ':'
342 sub get_var {
343 my $self = shift;
344 my $var = shift;
345
346 $var =~ s/^\$//;
347 $var =~ s/:$//;
348
349 $self->_parse_body()
350 if ($var eq 'message_body' && !$self->{_vars}{message_body});
351
352 return $self->{_vars}{$var};
353 }
354
355 sub _parse_body {
356 my $self = shift;
357 my $f = $self->{_path} . '/' . $self->{_message} . '-D';
358
359 open(I, "<$f") || return($self->_error("Couldn't open $f: $!"));
360 chomp($_ = <I>);
361 return(0) if ($self->{_message}.'-D' ne $_);
362
363 $self->{_vars}{message_body} = join('', <I>);
364 close(I);
365 $self->{_vars}{message_body} =~ s/\n/ /g;
366 $self->{_vars}{message_body} =~ s/\000/ /g;
367 return(1);
368 }
369
370 sub _parse_header {
371 my $self = shift;
372 my $f = $self->{_path} . '/' . $self->{_message} . '-H';
373
374 open(I, "<$f") || return($self->_error("Couldn't open $f: $!"));
375 chomp($_ = <I>);
376 return(0) if ($self->{_message}.'-H' ne $_);
377 $self->{_vars}{message_id} = $self->{_message};
378
379 # line 2
380 chomp($_ = <I>);
381 return(0) if (!/^(\S+)\s(\d+)\s(\d+)$/);
382 $self->{_vars}{originator_login} = $1;
383 $self->{_vars}{originator_uid} = $2;
384 $self->{_vars}{originator_gid} = $3;
385
386 # line 3
387 chomp($_ = <I>);
388 return(0) if (!/^<(.*)>$/);
389 $self->{_vars}{sender_address} = $1;
390 $self->{_vars}{sender_address_domain} = $1;
391 $self->{_vars}{sender_address_local_part} = $1;
392 $self->{_vars}{sender_address_domain} =~ s/^.*\@//;
393 $self->{_vars}{sender_address_local_part} =~ s/^(.*)\@.*$/$1/;
394
395 # line 4
396 chomp($_ = <I>);
397 return(0) if (!/^(\d+)\s(\d+)$/);
398 $self->{_vars}{received_time} = $1;
399 $self->{_vars}{warning_count} = $2;
400 $self->{_vars}{message_age} = time() - $self->{_vars}{received_time};
401
402 while (<I>) {
403 chomp();
404 if (/^(-\S+)\s*(.*$)/) {
405 my $tag = $1;
406 my $arg = $2;
407 if ($tag eq '-acl') {
408 my $t;
409 return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
410 if ($1 < $Exim::SpoolFile::ACL_C_MAX) {
411 $t = "acl_c$1";
412 } else {
413 $t = "acl_m" . ($1 - $Exim::SpoolFile::ACL_C_MAX);
414 }
415 read(I, $self->{_vars}{$t}, $2+1) || return(0);
416 chomp($self->{_vars}{$t});
417 } elsif ($tag eq '-local') {
418 $self->{_vars}{sender_local} = 1;
419 } elsif ($tag eq '-localerror') {
420 $self->{_vars}{local_error_message} = 1;
421 } elsif ($tag eq '-local_scan') {
422 $self->{_vars}{local_scan_data} = $arg;
423 } elsif ($tag eq '-host_lookup_failed') {
424 $self->{_vars}{host_lookup_failed} = 1;
425 } elsif ($tag eq '-body_linecount') {
426 $self->{_vars}{body_linecount} = $arg;
427 } elsif ($tag eq '-frozen') {
428 $self->{_vars}{deliver_freeze} = 1;
429 $self->{_vars}{deliver_frozen_at} = $arg;
430 } elsif ($tag eq '-deliver_firsttime') {
431 $self->{_vars}{deliver_firsttime} = 1;
432 $self->{_vars}{first_delivery} = 1;
433 } elsif ($tag eq '-manual_thaw') {
434 $self->{_vars}{deliver_manual_thaw} = 1;
435 $self->{_vars}{manually_thawed} = 1;
436 } elsif ($tag eq '-auth_id') {
437 $self->{_vars}{authenticated_id} = $arg;
438 } elsif ($tag eq '-auth_sender') {
439 $self->{_vars}{authenticated_sender} = $arg;
440 } elsif ($tag eq '-sender_set_untrusted') {
441 $self->{_vars}{sender_set_untrusted} = 1;
442 } elsif ($tag eq '-tls_certificate_verified') {
443 $self->{_vars}{tls_certificate_verified} = 1;
444 } elsif ($tag eq '-tls_cipher') {
445 $self->{_vars}{tls_cipher} = $arg;
446 } elsif ($tag eq '-tls_peerdn') {
447 $self->{_vars}{tls_peerdn} = $arg;
448 } elsif ($tag eq '-host_address') {
449 $self->{_vars}{sender_host_port} = $self->_get_host_and_port(\$arg);
450 $self->{_vars}{sender_host_address} = $arg;
451 } elsif ($tag eq '-interface_address') {
452 $self->{_vars}{interface_port} = $self->_get_host_and_port(\$arg);
453 $self->{_vars}{interface_address} = $arg;
454 } elsif ($tag eq '-host_auth') {
455 $self->{_vars}{sender_host_authenticated} = $arg;
456 } elsif ($tag eq '-host_name') {
457 $self->{_vars}{sender_host_name} = $arg;
458 } elsif ($tag eq '-helo_name') {
459 $self->{_vars}{sender_helo_name} = $arg;
460 } elsif ($tag eq '-ident') {
461 $self->{_vars}{sender_ident} = $arg;
462 } elsif ($tag eq '-received_protocol') {
463 $self->{_vars}{received_protocol} = $arg;
464 } elsif ($tag eq '-N') {
465 $self->{_vars}{dont_deliver} = 1;
466 } elsif ($tag eq '-body_zerocount') {
467 $self->{_vars}{body_zerocount} = $arg;
468 } elsif ($tag eq '-allow_unqualified_recipient') {
469 $self->{_vars}{allow_unqualified_recipient} = 1;
470 } elsif ($tag eq '-allow_unqualified_sender') {
471 $self->{_vars}{allow_unqualified_sender} = 1;
472 } else {
473 # unrecognized tag, save it for reference
474 $self->{$tag} = $arg;
475 }
476 } else {
477 last;
478 }
479 }
480
481 # when we drop out of the while loop, we have the first line of the
482 # delivered tree in $_
483 do {
484 if ($_ eq 'XX') {
485 ; # noop
486 } elsif ($_ =~ s/^[YN][YN]\s+//) {
487 $self->{_del_tree}{$_} = 1;
488 } else {
489 return(0);
490 }
491 chomp($_ = <I>);
492 } while ($_ !~ /^\d+$/);
493
494 $self->{_numrecips} = $_;
495 $self->{_vars}{recipients_count} = $self->{_numrecips};
496 for (my $i = 0; $i < $self->{_numrecips}; $i++) {
497 chomp($_ = <I>);
498 return(0) if (/^$/);
499 my $addr = '';
500 if (/^(.*)\s\d+,(\d+),\d+$/) {
501 #print STDERR "exim3 type (untested): $_\n";
502 $self->{_recips}{$1} = { pno => $2 };
503 $addr = $1;
504 } elsif (/^(.*)\s(\d+)$/) {
505 #print STDERR "exim4 original type (untested): $_\n";
506 $self->{_recips}{$1} = { pno => $2 };
507 $addr = $1;
508 } elsif (/^(.*)\s(.*)\s(\d+),(\d+)#1$/) {
509 #print STDERR "exim4 new type #1 (untested): $_\n";
510 return($self->_error("incorrect format: $_")) if (length($2) != $3);
511 $self->{_recips}{$1} = { pno => $4, errors_to => $2 };
512 $addr = $1;
513 } elsif (/^.*#(\d+)$/) {
514 print STDERR "exim4 #$1 style (unimplemented): $_\n";
515 $self->_error("exim4 #$1 style (unimplemented): $_");
516 } else {
517 #print STDERR "default type: $_\n";
518 $self->{_recips}{$_} = {};
519 $addr = $_;
520 }
521 $self->{_udel_tree}{$addr} = 1 if (!$self->{_del_tree}{$addr});
522 }
523 $self->{_vars}{recipients} = join(', ', keys(%{$self->{_recips}}));
524 $self->{_vars}{recipients_del} = join(', ', keys(%{$self->{_del_tree}}));
525 $self->{_vars}{recipients_undel} = join(', ', keys(%{$self->{_udel_tree}}));
526
527 # blank line
528 $_ = <I>;
529 return(0) if (!/^$/);
530
531 # start reading headers
532 while (read(I, $_, 3) == 3) {
533 my $t = getc(I);
534 return(0) if (!length($t));
535 while ($t =~ /^\d$/) {
536 $_ .= $t;
537 $t = getc(I);
538 }
539 # ok, right here $t contains the header flag and $_ contains the number of
540 # bytes to read. If we ever use the header flag, grab it here.
541 $self->{_vars}{message_size} += $_ if ($t ne '*');
542 $t = getc(I); # strip the space out of the file
543 my $bytes = $_;
544 return(0) if (read(I, $_, $bytes) != $bytes);
545 chomp(); # may regret this later
546 # build the $header_ variable, following exim's rules (sort of)
547 if (/^([^ :]+):(.*)$/s) {
548 my $v = "header_" . lc($1);
549 my $d = $2;
550 $d =~ s/^\s*//;
551 $d =~ s/\s*$//;
552 $self->{_vars}{$v} .= (defined($self->{_vars}{$v}) ? "\n" : '') . $d;
553 $self->{_vars}{received_count}++ if ($v eq 'header_received');
554 }
555 # push header onto $message_headers var, following exim's rules
556 $self->{_vars}{message_headers} .=
557 (defined($self->{_vars}{message_headers}) ? "\n" : '') . $_;
558 }
559 close(I);
560
561 if (length($self->{_vars}{"header_reply-to"}) > 0) {
562 $self->{_vars}{reply_address} = $self->{_vars}{"header_reply-to"};
563 } else {
564 $self->{_vars}{reply_address} = $self->{_vars}{header_from};
565 }
566
567 $self->{_vars}{message_body_size} =
568 (stat($self->{_path}.'/'.$self->{_message}.'-D'))[7] - 19;
569 if ($self->{_vars}{message_body_size} < 0) {
570 $self->{_vars}{message_size} = 0;
571 } else {
572 $self->{_vars}{message_size} += $self->{_vars}{message_body_size} + 1;
573 }
574
575 return(1);
576 }
577
578 # mimic exim's host_extract_port function - receive a ref to a scalar,
579 # strip it of port, return port
580 sub _get_host_and_port {
581 my $self = shift;
582 my $host = shift; # scalar ref, be careful
583
584 if ($$host =~ /^\[([^\]]+)\](?:\:(\d+))?$/) {
585 $$host = $1;
586 return($2 || 0);
587 } elsif ($$host =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\.(\d+))?$/) {
588 $$host = $1;
589 return($2 || 0);
590 } elsif ($$host =~ /^([\d\:]+)(?:\.(\d+))?$/) {
591 $$host = $1;
592 return($2 || 0);
593 }
594 # implicit else
595 return(0);
596 }
597
598 sub print_message {
599 my $self = shift;
600 my $fh = shift || \*STDOUT;
601 return if ($self->{_delivered});
602
603 if ($self->{_output_idonly}) {
604 print $fh $self->{_message}, "\n";
605 return;
606 }
607
608 if ($self->{_output_long} || $self->{_output_flatq}) {
609 my $i = int($self->{_vars}{message_age} / 60);
610 if ($i > 90) {
611 $i = int(($i+30)/60);
612 if ($i > 72) { printf $fh "%2dd ", int(($i+12)/24); }
613 else { printf $fh "%2dh ", $i; }
614 } else { printf $fh "%2dm ", $i; }
615
616 $i = $self->{_vars}{message_size};
617 if ($i == 0) { $i = " "; }
618 elsif ($i < 1024) { $i = sprintf("%5d", $i); }
619 elsif ($i < 10*1024) { $i = sprintf("%4.1fK", $i / 1024); }
620 elsif ($i < 1024*1024) { $i = sprintf("%4dK", ($i+512)/1024); }
621 elsif ($i < 10*1024*1024) { $i = sprintf("%4.1fM", $i/(1024*1024)); }
622 else { $i = sprintf("%4dM", ($i + 512 * 1024)/(1024*1024)); }
623 print $fh "$i ";
624 }
625 print $fh "$self->{_message} ";
626 print $fh "From: " if ($self->{_output_brief});
627 print $fh "<$self->{_vars}{sender_address}>";
628
629 if ($self->{_output_long}) {
630 print $fh " ($self->{_vars}{originator_login})"
631 if ($self->{_vars}{sender_set_untrusted});
632
633 # XXX exim contains code here to print spool format errors
634 print $fh " *** frozen ***" if ($self->{_vars}{deliver_freeze});
635 print $fh "\n";
636
637 foreach my $v (keys(%{$self->{_show_vars}})) {
638 printf $fh " %25s = '%s'\n", $v, $self->get_var($v);
639 }
640
641 foreach my $r (keys %{$self->{_recips}}) {
642 next if ($self->{_del_tree}{$r} && $self->{_undelivered_only});
643 printf $fh " %s %s\n", $self->{_del_tree}{$r} ? "D" : " ", $r;
644 }
645 if ($self->{_show_generated}) {
646 foreach my $r (keys %{$self->{_del_tree}}) {
647 next if ($self->{_recips}{$r});
648 printf $fh " +D %s\n", $r;
649 }
650 }
651 } elsif ($self->{_output_brief}) {
652 my @r = ();
653 foreach my $r (keys %{$self->{_recips}}) {
654 next if ($self->{_del_tree}{$r});
655 push(@r, $r);
656 }
657 print $fh " To: ", join(';', @r);
658 } elsif ($self->{_output_flatq}) {
659 print $fh " *** frozen ***" if ($self->{_vars}{deliver_freeze});
660 my @r = ();
661 foreach my $r (keys %{$self->{_recips}}) {
662 next if ($self->{_del_tree}{$r});
663 push(@r, $r);
664 }
665 print $fh " ", join(' ', @r);
666 }
667
668 print $fh "\n";
669 }
670
671 sub dump {
672 my $self = shift;
673
674 foreach my $k (sort keys %$self) {
675 my $r = ref($self->{$k});
676 if ($r eq 'ARRAY') {
677 printf "%20s <<EOM\n", $k;
678 print @{$self->{$k}}, "EOM\n";
679 } elsif ($r eq 'HASH') {
680 printf "%20s <<EOM\n", $k;
681 foreach (sort keys %{$self->{$k}}) {
682 printf "%20s %s\n", $_, $self->{$k}{$_};
683 }
684 print "EOM\n";
685 } else {
686 printf "%20s %s\n", $k, $self->{$k};
687 }
688 }
689 }
690
691 } # BEGIN
692
693 sub ext_usage {
694 if ($ARGV[0] =~ /^--help$/i) {
695 require Config;
696 $ENV{PATH} .= ":" unless $ENV{PATH} eq "";
697 $ENV{PATH} = "$ENV{PATH}$Config::Config{'installscript'}";
698 #exec("perldoc", "-F", "-U", $0) || exit 1;
699 $< = $> = 1 if ($> == 0 || $< == 0);
700 exec("perldoc", $0) || exit 1;
701 # make parser happy
702 %Config::Config = ();
703 } elsif ($ARGV[0] =~ /^--version$/i) {
704 print "$p_name version $p_version\n\n$p_cp\n";
705 } else {
706 return;
707 }
708
709 exit(0);
710 }
711
712 __END__
713
714 =head1 NAME
715
716 exipick - display messages from Exim queue based on a variety of criteria
717
718 =head1 USAGE
719
720 exipick [--help|--version] | [-spool <spool>] [-and|-or] [-bp|-bpa|-bpc|-bpr|-bpra|-bpru|-bpu] [<criterion> [<criterion> ...]]
721
722 =head1 DESCRIPTION
723
724 exipick is designed to display the contents of a Exim mail spool based on user-specified criteria. It is designed to mimic the output of 'exim -bp' (or any of the other -bp* options) and Exim's spec.txt should be used to learn more about the exact format of the output. The criteria are formed by creating comparisons against characteristics of the messages, for instance message_size, sender_helo_name, or message_headers.
725
726 =head1 OPTIONS
727
728 =over 4
729
730 =item -spool
731
732 The path to exim's spool directory. In general usage you should set the $spool variable in the script to your site's main spool directory, but this option is useful for alternate installs, or installs on NFS servers, etc.
733
734 =item -and
735
736 A message will be displayed only if it matches all of the specified criteria. This is the default.
737
738 =item -or
739
740 A message will be displayed if it matches any of the specified criteria.
741
742 =item The -bp* options all control how much information is displayed and in what manner. They all match the functionality of the options of the same name in Exim. Briefly:
743
744 =item -bp display the matching messages in 'mailq' format.
745
746 =item -bpa ... with generated addresses as well.
747
748 =item -bpc ... just show a count of messages.
749
750 =item -bpr ... do not sort.
751
752 =item -bpra ... with generated addresses, unsorted.
753
754 =item -bpru ... only undelivered addresses, unsorted.
755
756 =item -bpu ... only undelivered addresses.
757
758 Please see Exim's spec.txt for details on the format and information displayed with each option.
759
760 =item The following options are included for compatibility with the 'exiqgrep' utility:
761
762 =item -f <regexp> Same as '$sender_address = <regexp>'
763
764 =item -r <regexp> Same as '$recipients = <regexp>'
765
766 =item -y <seconds> Same as '$message_age < <seconds>'
767
768 =item -o <seconds> Same as '$message_age > <seconds>'
769
770 =item -z Same as '$deliver_freeze'
771
772 =item -x Same as '!$deliver_freeze'
773
774 =item -c Display count of matches only
775
776 =item -l Display in long format (default)
777
778 =item -i Display message IDs only
779
780 =item -b Display brief format only
781
782 Please see the 'exiqgrep' documentation for more details on the behaviour and output format produced by these options
783
784 =item <criterion>
785
786 The criteria are used to determine whether or not a given message should be displayed. The criteria are built using variables containing information about the individual messages (see VARIABLES section for list and descriptions of available variables). Each criterion is evaluated for each message in the spool and if all (by default) criteria match or (if -or option is specified) any criterion matches, the message is displayed. See VARIABLE TYPES for explanation of types of variables and the evaluations that can be performed on them and EXAMPLES section for complete examples.
787
788 The format of a criterion is explained in detail below, but a key point to make is that the variable being compared must always be on the left side of the comparison.
789
790 If no criteria are provided all messages in the queue are displayed (in this case the output of exipick should be identical to the output of 'exim -bp')
791
792 =item --help
793
794 This screen.
795
796 =item --version
797
798 Version info.
799
800 =back
801
802 =head1 VARIABLE TYPES
803
804 Although there are variable types defined, they are defined only by the type of data that gets put into them. They are internally typeless. Because of this it is perfectly legal to perform a numeric comparison against a string variable, although the results will probably be meaningless.
805
806 =over 4
807
808 =item NUMERIC
809
810 Variable of the numeric type can be of integer or float. Valid comparisons are <, <=, >, >=, ==, and !=.
811
812 The numbers specified in the criteria can have a suffix of d, h, m, s, M, K, or B, in which case the number will be mulitplied by 86400, 3600, 60, 1, 1048576, 1024, or 1 respectively. These suffixes are case sensitive. While these are obviously designed to aid in date and size calculations, they are not restricted to variables of their respective types.
813
814 =item BOOLEAN
815
816 Variables of the boolean type are very easy to use in criteria. The format is either the variable by itself or the variable negated with a ! sign. For instance, 'deliver_freeze' matches if the message in question is frozen, '!deliver_freeze' matches if message is not frozen.
817
818 =item STRING
819
820 String variables are basically defined as those that are neither numeric nor boolean and can contain any data. There are several types of comparisons that can be made against string variables. With the exception of '=', the operators all match the functionality of the like-named perl operators.
821
822 The simplest form is a bare string regular expression, represented by the operator '='. The value used for the comparison will be evaluated as a regular expression and can be as simple or as complex as desired. For instance 'sender_helo_name = example' on the simple end or 'sender_helo_name = ^aol\.com$' on the more complex end.
823
824 Slightly more complex is the string comparison with the operators 'eq' and 'ne' for equal and not equal, respectively. 'sender_helo_name eq hotmail.com' is true for messages with the exact helo string "hotmail.com", while 'sender_helo_name ne hotmail.com' is true for any message any helo string other than hotmail.com.
825
826 The most complex and the most flexible format are straight regular expressions with the operators =~ and !~. The value in the criteria is expected to be a correctly formatted perl regular expression B<including the regexp delimiters (usually //)>. The criterion 'sender_helo_name !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/' matches for any message which does not have an IP address for its helo string.
827
828 =back
829
830 =head1 VARIABLES
831
832 With a few exceptions the available variables match Exim's internal expansion variables in both name and exact contents. There are a few notable additions and format deviations which are noted below. Although a brief explanation is offered below, Exim's spec.txt should be consulted for full details. It is important to remember that not every variable will be defined for every message. For example, sender_host_port is not defined for messages not received from a remote host.
833
834 In the list below, '.' denotes standard messages with contents matching Exim's variable, '#' denotes standard variables with non-standard contents, and '+' denotes a non-standard variable.
835
836 =head2 Boolean variables
837
838 =over 4
839
840 =item + allow_unqualified_recipient
841
842 TRUE if unqualified recipient addresses are permitted in header lines.
843
844 =item + allow_unqualified_sender
845
846 TRUE if unqualified sender addresses are permitted in header lines.
847
848 =item + deliver_freeze
849
850 TRUE if the message is frozen.
851
852 =item . first_delivery
853
854 TRUE if the message has not been deferred.
855
856 =item . manually_thawed
857
858 TRUE when the message has been manually thawed.
859
860 =item + dont_deliver
861
862 TRUE if, under normal circumstances, Exim will not try to deliver the message.
863
864 =item . host_lookup_failed
865
866 TRUE if there was an attempt to look up the host's name from its IP address, but the attempt failed.
867
868 =item + local_error_message
869
870 TRUE if the message is a locally-generated error message.
871
872 =item + sender_local
873
874 TRUE if the message was locally generated.
875
876 =item + sender_set_untrusted
877
878 TRUE if the envelope sender of this message was set by an untrusted local caller.
879
880 =item . tls_certificate_verified
881
882 TRUE if a TLS certificate was verified when the message was received.
883
884 =back
885
886 =head2 Numeric variables
887
888 =over 4
889
890 =item . body_linecount
891
892 The number of lines in the message's body.
893
894 =item . body_zerocount
895
896 The number of binary zero bytes in the message's body.
897
898 =item + deliver_frozen_at
899
900 The epoch time at which message was frozen.
901
902 =item . interface_port
903
904 The local port number if network-originated messages.
905
906 =item . message_age
907
908 The number of seconds since the message was received.
909
910 =item . message_body_size
911
912 The size of the body in bytes.
913
914 =item . message_size
915
916 The size of the message in bytes.
917
918 =item . originator_gid
919
920 The group id under which the process that called Exim was running as when the message was received.
921
922 =item . originator_uid
923
924 The user id under which the process that called Exim was running as when the message was received.
925
926 =item . received_count
927
928 The number of Received: header lines in the message.
929
930 =item + received_time
931
932 The epoch time at which the message was received.
933
934 =item . recipients_count
935
936 The number of envelope recipients that came with the message.
937
938 =item . sender_host_port
939
940 The port number that was used on the remote host for network-originated messages.
941
942 =item + warning_count
943
944 The number of delay warnings which have been sent for this message.
945
946 =back
947
948 =head2 String variables
949
950 =over 4
951
952 =item . acl_c0-acl_c9, acl_m0-acl_m9
953
954 User definable variables.
955
956 =item . authenticated_id
957
958 Optional saved information from authenticators, or the login name of the calling process for locally submitted messages.
959
960 =item . authenticated_sender
961
962 The value of AUTH= param for smtp messages, or a generated value from the calling processes login and qualify domain for locally submitted messages.
963
964 =item # header_*
965
966 The value of the same named message header, for example header_to or header_reply-to. These variables are really closer to Exim's rheader_* variables, with the exception that leading and trailing space is removed.
967
968 =item . interface_address
969
970 The address of the local IP interface for network-originated messages.
971
972 =item . local_scan_data
973
974 The text returned by the local_scan() function when a message is received.
975
976 =item # message_body
977
978 The message's body. Unlike Exim's variable of the same name, this variable contains the entire message body. The logic behind this is that the message body is not read unless it is specifically referenced, so under normal circumstances it is not a penalty, but when you need the entire body you need the entire body. Like Exim's copy, newlines and nulls are replaced by spaces.
979
980 =item . message_headers
981
982 A concatenation of all the header lines except for lines added by routers or transports.
983
984 =item . message_id
985
986 The unique message id that is used by Exim to identify the message.
987
988 =item + originator_login
989
990 The login of the process which called Exim.
991
992 =item . received_protocol
993
994 The name of the protocol by which the message was received.
995
996 =item # recipients
997
998 The list of envelope recipients for a message. Unlike Exim's version, this variable always contains every envelope recipient of the message. The recipients are separated by a comma and a space.
999
1000 =item + recipients_del
1001
1002 The list of delivered envelope recipients for a message. This non-standard variable is in the same format as recipients and contains the list of already-delivered recipients.
1003
1004 =item + recipients_undel
1005
1006 The list of undelivered envelope recipients for a message. This non-standard variable is in the same format as recipients and contains the list of undelivered recipients.
1007
1008 =item . reply_address
1009
1010 The contents of the Reply-To: header line if one exists and it is not empty, or otherwise the contents of the From: header line.
1011
1012 =item . sender_address
1013
1014 The sender's address that was received in the message's envelope. For bounce messages, the value of this variable is the empty string.
1015
1016 =item . sender_address_domain
1017
1018 The domain part of sender_address.
1019
1020 =item . sender_address_local_part
1021
1022 The local part of sender_address.
1023
1024 =item . sender_helo_name
1025
1026 The HELO or EHLO value supplied for smtp or bsmtp messages.
1027
1028 =item . sender_host_address
1029
1030 The remote host's IP address.
1031
1032 =item . sender_host_authenticated
1033
1034 The name of the authenticator driver which successfully authenticated the client from which the message was received.
1035
1036 =item . sender_host_name
1037
1038 The remote host's name as obtained by looking up its IP address.
1039
1040 =item . sender_ident
1041
1042 The identification received in response to an RFC 1413 request for remote messages, the login name of the user that called Exim for locally generated messages.
1043
1044 =item . tls_cipher
1045
1046 The cipher suite that was negotiated for encrypted SMTP connections.
1047
1048 =item . tls_peerdn
1049
1050 The value of the Distinguished Name of the certificate if Exim is configured to request one.
1051
1052 =back
1053
1054 =head1 EXAMPLES
1055
1056 =over 4
1057
1058 =item exipick 'deliver_freeze'
1059
1060 Display only frozen messages.
1061
1062 =item exipick 'received_protocol eq asmtp' 'message_age < 20m'
1063
1064 Display only messages wich were delivered over an authenticated smtp session in the last 20 minutes.
1065
1066 =item exipick -bpc 'message_size > 200K'
1067
1068 Display a count of messages in the queue which are over 200 kilobytes in size.
1069
1070 =item exipick -or 'sender_helo_name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/' 'sender_helo_name = _'
1071
1072 Display message which have a HELO string which either is an IP address or contains an underscore.
1073
1074 =back
1075
1076 =head1 REQUIREMENTS
1077
1078 None that I know of, except an Exim installation. Your life will also be a lot easier if you set $spool at the top of the script to your install's spool directory.
1079
1080 =head1 ACKNOWLEDGEMENTS
1081
1082 Although I conceived of the concept for this program independently, the name 'exipick' was taken from the Exim WishList and was suggested by Jeffrey Goldberg.
1083
1084 Thank you to Philip Hazel for writing Exim. Of course this program exists because of Exim, but more specifically the message parsing code is based on Exim's and some of this documentation was copy/pasted from Exim's.
1085
1086 =head1 CONTACT
1087
1088 =over 4
1089
1090 =item EMAIL: proj-exipick@jetmore.net
1091
1092 =item HOME: jetmore.org/john/code/#exipick
1093
1094 =back
1095
1096 =cut