Add PQsetClientEncoding(conn, "SQL_ASCII") to the pgsql module.
[exim.git] / src / src / exipick.src
CommitLineData
059ec3d9 1#!PERL_COMMAND
9322b322 2# $Cambridge: exim/src/src/exipick.src,v 1.11 2006/03/07 21:05:30 jetmore Exp $
059ec3d9
PH
3
4# This variable should be set by the building process to Exim's spool directory.
5my $spool = 'SPOOL_DIRECTORY';
6
7use strict;
8use Getopt::Long;
9
10my($p_name) = $0 =~ m|/?([^/]+)$|;
9322b322 11my $p_version = "20060307.1";
059ec3d9
PH
12my $p_usage = "Usage: $p_name [--help|--version] (see --help for details)";
13my $p_cp = <<EOM;
11121d3d 14 Copyright (c) 2003-2006 John Jetmore <jj33\@pobox.com>
059ec3d9
PH
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
29EOM
30ext_usage(); # before we do anything else, check for --help
31
bf759a8b
PH
32$| = 1; # unbuffer STDOUT
33
059ec3d9
PH
34Getopt::Long::Configure("bundling_override");
35GetOptions(
bf759a8b
PH
36 'spool:s' => \$G::spool, # exim spool dir
37 'bp' => \$G::mailq_bp, # List the queue (noop - default)
38 'bpa' => \$G::mailq_bpa, # ... with generated address as well
39 'bpc' => \$G::mailq_bpc, # ... but just show a count of messages
40 'bpr' => \$G::mailq_bpr, # ... do not sort
41 'bpra' => \$G::mailq_bpra, # ... with generated addresses, unsorted
42 'bpru' => \$G::mailq_bpru, # ... only undelivered addresses, unsorted
43 'bpu' => \$G::mailq_bpu, # ... only undelivered addresses
44 'and' => \$G::and, # 'and' the criteria (default)
45 'or' => \$G::or, # 'or' the criteria
46 'f:s' => \$G::qgrep_f, # from regexp
47 'r:s' => \$G::qgrep_r, # recipient regexp
5f970846 48 's:s' => \$G::qgrep_s, # match against size field
bf759a8b
PH
49 'y:s' => \$G::qgrep_y, # message younger than (secs)
50 'o:s' => \$G::qgrep_o, # message older than (secs)
51 'z' => \$G::qgrep_z, # frozen only
52 'x' => \$G::qgrep_x, # non-frozen only
53 'c' => \$G::qgrep_c, # display match count
54 'l' => \$G::qgrep_l, # long format (default)
55 'i' => \$G::qgrep_i, # message ids only
56 'b' => \$G::qgrep_b, # brief format
9cf6b11a
JJ
57 'freeze:s' => \$G::freeze, # freeze data in this file
58 'thaw:s' => \$G::thaw, # thaw data from this file
59 'unsorted' => \$G::unsorted, # unsorted, regardless of output format
bf759a8b
PH
60 'flatq' => \$G::flatq, # brief format
61 'caseful' => \$G::caseful, # in '=' criteria, respect case
62 'caseless' => \$G::caseless, # ...ignore case (default)
63 'show-vars:s' => \$G::show_vars, # display the contents of these vars
64 'show-rules' => \$G::show_rules, # display compiled match rules
65 'show-tests' => \$G::show_tests # display tests as applied to each message
059ec3d9
PH
66) || exit(1);
67
9cf6b11a
JJ
68# if both freeze and thaw specified, only thaw as it is less desctructive
69$G::freeze = undef if ($G::freeze && $G::thaw);
70freeze_start() if ($G::freeze);
71thaw_start() if ($G::thaw);
72
5f970846
PH
73push(@ARGV, "\$sender_address =~ /$G::qgrep_f/") if ($G::qgrep_f);
74push(@ARGV, "\$recipients =~ /$G::qgrep_r/") if ($G::qgrep_r);
75push(@ARGV, "\$shown_message_size eq $G::qgrep_s") if ($G::qgrep_s);
76push(@ARGV, "\$message_age < $G::qgrep_y") if ($G::qgrep_y);
77push(@ARGV, "\$message_age > $G::qgrep_o") if ($G::qgrep_o);
78push(@ARGV, "\$deliver_freeze") if ($G::qgrep_z);
79push(@ARGV, "!\$deliver_freeze") if ($G::qgrep_x);
bf759a8b
PH
80$G::mailq_bp = $G::mailq_bp; # shut up -w
81$G::and = $G::and; # shut up -w
b3f69ca8 82$G::msg_ids = {}; # short circuit when crit is only MID
bf759a8b 83$G::caseless = $G::caseful ? 0 : 1; # nocase by default, case if both
b3f69ca8 84@G::recipients_crit = (); # holds per-recip criteria
bf759a8b 85$spool = $G::spool if ($G::spool);
9cf6b11a
JJ
86my $count_only = 1 if ($G::mailq_bpc || $G::qgrep_c);
87my $unsorted = 1 if ($G::mailq_bpr || $G::mailq_bpra ||
88 $G::mailq_bpru || $G::unsorted);
89my $msg = $G::thaw ? thaw_message_list()
90 : get_all_msgs($spool,$unsorted);
91die "Problem accessing thaw file\n" if ($G::thaw && !$msg);
bf759a8b
PH
92my $crit = process_criteria(\@ARGV);
93my $e = Exim::SpoolFile->new();
b3f69ca8
JJ
94my $tcount = 0 if ($count_only); # holds count of all messages
95my $mcount = 0 if ($count_only); # holds count of matching messages
bf759a8b
PH
96$e->set_undelivered_only(1) if ($G::mailq_bpru || $G::mailq_bpu);
97$e->set_show_generated(1) if ($G::mailq_bpra || $G::mailq_bpa);
98$e->output_long() if ($G::qgrep_l);
99$e->output_idonly() if ($G::qgrep_i);
100$e->output_brief() if ($G::qgrep_b);
101$e->output_flatq() if ($G::flatq);
059ec3d9 102$e->set_show_vars($G::show_vars) if ($G::show_vars);
bf759a8b 103$e->set_spool($spool);
059ec3d9 104
9cf6b11a 105
059ec3d9
PH
106MSG:
107foreach my $m (@$msg) {
af66f652
PH
108 next if (scalar(keys(%$G::msg_ids)) && !$G::or
109 && !$G::msg_ids->{$m->{message}});
9cf6b11a
JJ
110 if ($G::thaw) {
111 my $data = thaw_data();
112 if (!$e->restore_state($data)) {
113 warn "Couldn't thaw $data->{_message}: ".$e->error()."\n";
114 next MSG;
115 }
116 } else {
117 if (!$e->parse_message($m->{message}, $m->{path})) {
118 warn "Couldn't parse $m->{message}: ".$e->error()."\n";
119 next MSG;
120 }
059ec3d9
PH
121 }
122 $tcount++;
123 my $match = 0;
bf759a8b
PH
124 my @local_crit = ();
125 foreach my $c (@G::recipients_crit) { # handle each_recip* vars
126 foreach my $addr (split(/, /, $e->get_var($c->{var}))) {
127 my %t = ( 'cmp' => $c->{cmp}, 'var' => $c->{var} );
128 $t{cmp} =~ s/"?\$var"?/'$addr'/;
129 push(@local_crit, \%t);
130 }
131 }
ee744174 132 if ($G::show_tests) { print $e->get_var('message_exim_id'), "\n"; }
059ec3d9 133 CRITERIA:
bf759a8b 134 foreach my $c (@$crit, @local_crit) {
059ec3d9
PH
135 my $var = $e->get_var($c->{var});
136 my $ret = eval($c->{cmp});
bf759a8b
PH
137 if ($G::show_tests) {
138 printf " %25s = '%s'\n %25s => $ret\n",$c->{var},$var,$c->{cmp},$ret;
139 }
059ec3d9
PH
140 if ($@) {
141 print STDERR "Error in eval '$c->{cmp}': $@\n";
11121d3d 142 next MSG;
059ec3d9
PH
143 } elsif ($ret) {
144 $match = 1;
11121d3d
JJ
145 if ($G::or) { last CRITERIA; }
146 else { next CRITERIA; }
059ec3d9 147 } else { # no match
11121d3d
JJ
148 if ($G::or) { next CRITERIA; }
149 else { next MSG; }
059ec3d9
PH
150 }
151 }
b3f69ca8
JJ
152
153 # skip this message if any criteria were supplied and it didn't match
11121d3d 154 next MSG if ((scalar(@$crit) || scalar(@local_crit)) && !$match);
059ec3d9
PH
155
156 if ($count_only) {
157 $mcount++;
158 } else {
159 $e->print_message(\*STDOUT);
160 }
9cf6b11a
JJ
161
162 if ($G::freeze) {
163 freeze_data($e->get_state());
164 push(@G::frozen_msgs, $m);
165 }
059ec3d9
PH
166}
167
168if ($G::mailq_bpc) {
11121d3d 169 print "$mcount\n";
059ec3d9
PH
170} elsif ($G::qgrep_c) {
171 print "$mcount matches out of $tcount messages\n";
172}
173
9cf6b11a
JJ
174if ($G::freeze) {
175 freeze_message_list(\@G::frozen_msgs);
176 freeze_end();
177} elsif ($G::thaw) {
178 thaw_end();
179}
180
059ec3d9
PH
181exit;
182
9cf6b11a
JJ
183# FREEZE FILE FORMAT:
184# message_data_bytes
185# message_data
186# <...>
187# EOM
188# message_list
189# message_list_bytes <- 10 bytes, zero-packed, plus \n
190
191sub freeze_start {
192 eval("use Storable");
193 die "Storable module not found: $@\n" if ($@);
194 open(O, ">$G::freeze") || die "Can't open freeze file $G::freeze: $!\n";
195 $G::freeze_handle = \*O;
196}
197
198sub freeze_end {
199 close($G::freeze_handle);
200}
201
202sub thaw_start {
203 eval("use Storable");
204 die "Storable module not found: $@\n" if ($@);
205 open(I, "<$G::thaw") || die "Can't open freeze file $G::thaw: $!\n";
206 $G::freeze_handle = \*I;
207}
208
209sub thaw_end {
210 close($G::freeze_handle);
211}
212
213sub freeze_data {
214 my $h = Storable::freeze($_[0]);
215 print $G::freeze_handle length($h)+1, "\n$h\n";
216}
217
218sub freeze_message_list {
219 my $h = Storable::freeze($_[0]);
220 my $l = length($h) + 1;
221 printf $G::freeze_handle "EOM\n$l\n$h\n%010d\n", $l+11+length($l)+1;
222}
223
224sub thaw_message_list {
225 my $orig_pos = tell($G::freeze_handle);
226 seek($G::freeze_handle, -11, 2);
227 chomp(my $bytes = <$G::freeze_handle>);
228 seek($G::freeze_handle, $bytes * -1, 2);
229 my $obj = thaw_data();
230 seek($G::freeze_handle, 0, $orig_pos);
231 return($obj);
232}
233
234sub thaw_data {
235 my $obj;
236 chomp(my $bytes = <$G::freeze_handle>);
237 return(undef) if (!$bytes || $bytes eq 'EOM');
238 my $read = read(I, $obj, $bytes);
239 die "Format error in thaw file (expected $bytes bytes, got $read)\n"
240 if ($bytes != $read);
241 chomp($obj);
242 return(Storable::thaw($obj));
243}
244
059ec3d9
PH
245sub process_criteria {
246 my $a = shift;
247 my @c = ();
248 my $e = 0;
249
250 foreach (@$a) {
251 foreach my $t ('@') { s/$t/\\$t/g; } # '$'
252 if (/^(.*?)\s+(<=|>=|==|!=|<|>)\s+(.*)$/) {
253 #print STDERR "found as integer\n";
254 my $v = $1; my $o = $2; my $n = $3;
255 if ($n =~ /^([\d\.]+)M$/) { $n = $1 * 1024 * 1024; }
256 elsif ($n =~ /^([\d\.]+)K$/) { $n = $1 * 1024; }
257 elsif ($n =~ /^([\d\.]+)B?$/) { $n = $1; }
258 elsif ($n =~ /^([\d\.]+)d$/) { $n = $1 * 60 * 60 * 24; }
259 elsif ($n =~ /^([\d\.]+)h$/) { $n = $1 * 60 * 60; }
260 elsif ($n =~ /^([\d\.]+)m$/) { $n = $1 * 60; }
261 elsif ($n =~ /^([\d\.]+)s?$/) { $n = $1; }
262 else {
263 print STDERR "Expression $_ did not parse: numeric comparison with ",
264 "non-number\n";
265 $e = 1;
266 next;
267 }
9cf6b11a
JJ
268 #push(@c, { var => lc($v), cmp => "(\$var $o $n) ? 1 : 0" });
269 push(@c, { var => lc($v), cmp => "(\$var $o $n)" });
059ec3d9
PH
270 } elsif (/^(.*?)\s+(=~|!~)\s+(.*)$/) {
271 #print STDERR "found as string regexp\n";
9cf6b11a 272 push(@c, { var => lc($1), cmp => "(\"\$var\" $2 $3)" });
059ec3d9
PH
273 } elsif (/^(.*?)\s+=\s+(.*)$/) {
274 #print STDERR "found as bare string regexp\n";
af66f652 275 my $case = $G::caseful ? '' : 'i';
9cf6b11a 276 push(@c, { var => lc($1), cmp => "(\"\$var\" =~ /$2/$case)" });
059ec3d9
PH
277 } elsif (/^(.*?)\s+(eq|ne)\s+(.*)$/) {
278 #print STDERR "found as string cmp\n";
af66f652 279 my $var = lc($1); my $op = $2; my $val = $3;
5f970846 280 $val =~ s|^(['"])(.*)\1$|$2|;
9cf6b11a 281 push(@c, { var => $var, cmp => "(\"\$var\" $op \"$val\")" });
ee744174 282 if (($var eq 'message_id' || $var eq 'message_exim_id') && $op eq "eq") {
af66f652
PH
283 #print STDERR "short circuit @c[-1]->{cmp} $val\n";
284 $G::msg_ids->{$val} = 1;
285 }
9cf6b11a 286 } elsif (/^(\S+)$/) {
059ec3d9 287 #print STDERR "found as boolean\n";
9cf6b11a 288 push(@c, { var => lc($1), cmp => "(\$var)" });
059ec3d9
PH
289 } else {
290 print STDERR "Expression $_ did not parse\n";
291 $e = 1;
292 }
9cf6b11a
JJ
293 # assign the results of the cmp test here (handle "!" negation)
294 if ($c[-1]{var} =~ s|^!||) {
295 $c[-1]{cmp} .= " ? 0 : 1";
296 } else {
297 $c[-1]{cmp} .= " ? 1 : 0";
298 }
bf759a8b
PH
299 # support the each_* psuedo variables. Steal the criteria off of the
300 # queue for special processing later
301 if ($c[-1]{var} =~ /^each_(recipients(_(un)?del)?)$/) {
302 my $var = $1;
303 push(@G::recipients_crit,pop(@c));
304 $G::recipients_crit[-1]{var} = $var; # remove each_ from the variable
305 }
059ec3d9
PH
306 }
307
308 exit(1) if ($e);
309
310 if ($G::show_rules) { foreach (@c) { print "$_->{var}\t$_->{cmp}\n"; } }
311
312 return(\@c);
313}
314
315sub get_all_msgs {
316 my $d = shift() . '/input';
317 my $u = shift;
318 my @m = ();
319
320 opendir(D, "$d") || die "Couldn't opendir $d: $!\n";
321 foreach my $e (grep !/^\./, readdir(D)) {
322 if ($e =~ /^[a-zA-Z0-9]$/) {
323 opendir(DD, "$d/$e") || next;
324 foreach my $f (grep !/^\./, readdir(DD)) {
9cf6b11a 325 push(@m, { message => $1, path => "$d/$e" }) if ($f =~ /^(.{16})-H$/);
059ec3d9
PH
326 }
327 closedir(DD);
328 } elsif ($e =~ /^(.{16})-H$/) {
9cf6b11a 329 push(@m, { message => $1, path => $d });
059ec3d9
PH
330 }
331 }
332 closedir(D);
333
334 return($u ? \@m : [ sort { $a->{message} cmp $b->{message} } @m ]);
335}
336
337BEGIN {
338
339package Exim::SpoolFile;
340
b3f69ca8
JJ
341# versions 4.61 and higher will not need these variables anymore, but they
342# are left for handling legacy installs
343$Exim::SpoolFile::ACL_C_MAX_LEGACY = 10;
344#$Exim::SpoolFile::ACL_M_MAX _LEGACY= 10;
059ec3d9
PH
345
346sub new {
347 my $class = shift;
348 my $self = {};
349 bless($self, $class);
350
351 $self->{_spool_dir} = '';
352 $self->{_undelivered_only} = 0;
353 $self->{_show_generated} = 0;
354 $self->{_output_long} = 1;
355 $self->{_output_idonly} = 0;
356 $self->{_output_brief} = 0;
357 $self->{_output_flatq} = 0;
5f970846 358 $self->{_show_vars} = [];
059ec3d9
PH
359
360 $self->_reset();
361 return($self);
362}
363
364sub output_long {
365 my $self = shift;
366
367 $self->{_output_long} = 1;
368 $self->{_output_idonly} = 0;
369 $self->{_output_brief} = 0;
370 $self->{_output_flatq} = 0;
371}
372
373sub output_idonly {
374 my $self = shift;
375
376 $self->{_output_long} = 0;
377 $self->{_output_idonly} = 1;
378 $self->{_output_brief} = 0;
379 $self->{_output_flatq} = 0;
380}
381
382sub output_brief {
383 my $self = shift;
384
385 $self->{_output_long} = 0;
386 $self->{_output_idonly} = 0;
387 $self->{_output_brief} = 1;
388 $self->{_output_flatq} = 0;
389}
390
391sub output_flatq {
392 my $self = shift;
393
394 $self->{_output_long} = 0;
395 $self->{_output_idonly} = 0;
396 $self->{_output_brief} = 0;
397 $self->{_output_flatq} = 1;
398}
399
400sub set_show_vars {
401 my $self = shift;
402 my $s = shift;
403
404 foreach my $v (split(/\s*,\s*/, $s)) {
5f970846 405 push(@{$self->{_show_vars}}, $v);
059ec3d9
PH
406 }
407}
408
409sub set_show_generated {
410 my $self = shift;
411 $self->{_show_generated} = shift;
412}
413
414sub set_undelivered_only {
415 my $self = shift;
416 $self->{_undelivered_only} = shift;
417}
418
419sub error {
420 my $self = shift;
421 return $self->{_error};
422}
423
424sub _error {
425 my $self = shift;
426 $self->{_error} = shift;
427 return(undef);
428}
429
430sub _reset {
431 my $self = shift;
432
433 $self->{_error} = '';
434 $self->{_delivered} = 0;
435 $self->{_message} = '';
436 $self->{_path} = '';
437 $self->{_vars} = {};
438
439 $self->{_numrecips} = 0;
440 $self->{_udel_tree} = {};
441 $self->{_del_tree} = {};
442 $self->{_recips} = {};
443
444 return($self);
445}
446
447sub parse_message {
448 my $self = shift;
8e669ac1 449
059ec3d9
PH
450 $self->_reset();
451 $self->{_message} = shift || return(0);
9cf6b11a 452 $self->{_path} = shift; # optional path to message
059ec3d9 453 return(0) if (!$self->{_spool_dir});
9cf6b11a 454 if (!$self->{_path} && !$self->_find_path()) {
059ec3d9
PH
455 # assume the message was delivered from under us and ignore
456 $self->{_delivered} = 1;
457 return(1);
458 }
459 $self->_parse_header() || return(0);
460
461 return(1);
462}
463
9cf6b11a
JJ
464# take the output of get_state() and set up a message internally like
465# parse_message (except from a saved data struct, not by parsing the
466# files on disk).
467sub restore_state {
468 my $self = shift;
469 my $h = shift;
470
471 return(1) if ($h->{_delivered});
472 $self->_reset();
473 $self->{_message} = $h->{_message} || return(0);
474 return(0) if (!$self->{_spool_dir});
475
476 $self->{_path} = $h->{_path};
477 $self->{_vars} = $h->{_vars};
478 $self->{_numrecips} = $h->{_numrecips};
479 $self->{_udel_tree} = $h->{_udel_tree};
480 $self->{_del_tree} = $h->{_del_tree};
481 $self->{_recips} = $h->{_recips};
482
483 $self->{_vars}{message_age} = time() - $self->{_vars}{received_time};
484 return(1);
485}
486
487# This returns the state data for a specific message in a format that can
488# be later frozen back in to regain state
489#
490# after calling this function, this specific state is not expect to be
491# reused. That's because we're returning direct references to specific
492# internal structures. We're also modifying the structure ourselves
493# by deleting certain internal message variables.
494sub get_state {
495 my $self = shift;
496 my $h = {}; # this is the hash ref we'll be returning.
497
498 $h->{_delivered} = $self->{_delivered};
499 $h->{_message} = $self->{_message};
500 $h->{_path} = $self->{_path};
501 $h->{_vars} = $self->{_vars};
502 $h->{_numrecips} = $self->{_numrecips};
503 $h->{_udel_tree} = $self->{_udel_tree};
504 $h->{_del_tree} = $self->{_del_tree};
505 $h->{_recips} = $self->{_recips};
506
507 # delete some internal variables that we will rebuild later if needed
508 delete($h->{_vars}{message_body});
509 delete($h->{_vars}{message_age});
510
511 return($h);
512}
513
514# keep this sub as a feature if we ever break this module out, but do away
515# with its use in exipick (pass it in from caller instead)
059ec3d9
PH
516sub _find_path {
517 my $self = shift;
518
519 return(0) if (!$self->{_message});
520 return(0) if (!$self->{_spool_dir});
521
9cf6b11a
JJ
522 # test split spool first on the theory that people concerned about
523 # performance will have split spool set =).
524 foreach my $f (substr($self->{_message}, 5, 1).'/', '') {
525 if (-f "$self->{_spool_dir}/input/$f$self->{_message}-H") {
059ec3d9
PH
526 $self->{_path} = $self->{_spool_dir} . "/input/$f";
527 return(1);
528 }
529 }
530 return(0);
531}
532
533sub set_spool {
534 my $self = shift;
535 $self->{_spool_dir} = shift;
536}
537
538# accepts a variable with or without leading '$' or trailing ':'
539sub get_var {
540 my $self = shift;
b3f69ca8 541 my $var = lc(shift);
059ec3d9
PH
542
543 $var =~ s/^\$//;
544 $var =~ s/:$//;
545
546 $self->_parse_body()
547 if ($var eq 'message_body' && !$self->{_vars}{message_body});
548
9cf6b11a 549 chomp($self->{_vars}{$var});
059ec3d9
PH
550 return $self->{_vars}{$var};
551}
552
553sub _parse_body {
554 my $self = shift;
555 my $f = $self->{_path} . '/' . $self->{_message} . '-D';
556
557 open(I, "<$f") || return($self->_error("Couldn't open $f: $!"));
558 chomp($_ = <I>);
559 return(0) if ($self->{_message}.'-D' ne $_);
560
561 $self->{_vars}{message_body} = join('', <I>);
562 close(I);
563 $self->{_vars}{message_body} =~ s/\n/ /g;
564 $self->{_vars}{message_body} =~ s/\000/ /g;
565 return(1);
566}
567
568sub _parse_header {
569 my $self = shift;
570 my $f = $self->{_path} . '/' . $self->{_message} . '-H';
571
9cf6b11a
JJ
572 if (!open(I, "<$f")) {
573 # assume message went away and silently ignore
574 $self->{_delivered} = 1;
575 return(1);
576 }
577
059ec3d9
PH
578 chomp($_ = <I>);
579 return(0) if ($self->{_message}.'-H' ne $_);
580 $self->{_vars}{message_id} = $self->{_message};
ee744174 581 $self->{_vars}{message_exim_id} = $self->{_message};
059ec3d9
PH
582
583 # line 2
584 chomp($_ = <I>);
5f970846 585 return(0) if (!/^(.+)\s(\-?\d+)\s(\-?\d+)$/);
059ec3d9
PH
586 $self->{_vars}{originator_login} = $1;
587 $self->{_vars}{originator_uid} = $2;
588 $self->{_vars}{originator_gid} = $3;
589
590 # line 3
591 chomp($_ = <I>);
592 return(0) if (!/^<(.*)>$/);
593 $self->{_vars}{sender_address} = $1;
594 $self->{_vars}{sender_address_domain} = $1;
595 $self->{_vars}{sender_address_local_part} = $1;
596 $self->{_vars}{sender_address_domain} =~ s/^.*\@//;
597 $self->{_vars}{sender_address_local_part} =~ s/^(.*)\@.*$/$1/;
598
599 # line 4
600 chomp($_ = <I>);
601 return(0) if (!/^(\d+)\s(\d+)$/);
602 $self->{_vars}{received_time} = $1;
603 $self->{_vars}{warning_count} = $2;
604 $self->{_vars}{message_age} = time() - $self->{_vars}{received_time};
605
606 while (<I>) {
607 chomp();
608 if (/^(-\S+)\s*(.*$)/) {
609 my $tag = $1;
610 my $arg = $2;
611 if ($tag eq '-acl') {
612 my $t;
613 return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
b3f69ca8 614 if ($1 < $Exim::SpoolFile::ACL_C_MAX_LEGACY) {
059ec3d9
PH
615 $t = "acl_c$1";
616 } else {
b3f69ca8 617 $t = "acl_m" . ($1 - $Exim::SpoolFile::ACL_C_MAX_LEGACY);
059ec3d9
PH
618 }
619 read(I, $self->{_vars}{$t}, $2+1) || return(0);
620 chomp($self->{_vars}{$t});
b3f69ca8
JJ
621 } elsif ($tag eq '-aclc') {
622 return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
623 my $t = "acl_c$1";
624 read(I, $self->{_vars}{$t}, $2+1) || return(0);
625 chomp($self->{_vars}{$t});
626 } elsif ($tag eq '-aclm') {
627 return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
628 my $t = "acl_m$1";
629 read(I, $self->{_vars}{$t}, $2+1) || return(0);
630 chomp($self->{_vars}{$t});
059ec3d9
PH
631 } elsif ($tag eq '-local') {
632 $self->{_vars}{sender_local} = 1;
633 } elsif ($tag eq '-localerror') {
634 $self->{_vars}{local_error_message} = 1;
635 } elsif ($tag eq '-local_scan') {
636 $self->{_vars}{local_scan_data} = $arg;
bf759a8b
PH
637 } elsif ($tag eq '-spam_score_int') {
638 $self->{_vars}{spam_score_int} = $arg;
639 $self->{_vars}{spam_score} = $arg / 10;
640 } elsif ($tag eq '-bmi_verdicts') {
641 $self->{_vars}{bmi_verdicts} = $arg;
642 } elsif ($tag eq '-host_lookup_deferred') {
643 $self->{_vars}{host_lookup_deferred} = 1;
059ec3d9
PH
644 } elsif ($tag eq '-host_lookup_failed') {
645 $self->{_vars}{host_lookup_failed} = 1;
646 } elsif ($tag eq '-body_linecount') {
647 $self->{_vars}{body_linecount} = $arg;
bf759a8b
PH
648 } elsif ($tag eq '-body_zerocount') {
649 $self->{_vars}{body_zerocount} = $arg;
059ec3d9
PH
650 } elsif ($tag eq '-frozen') {
651 $self->{_vars}{deliver_freeze} = 1;
652 $self->{_vars}{deliver_frozen_at} = $arg;
bf759a8b
PH
653 } elsif ($tag eq '-allow_unqualified_recipient') {
654 $self->{_vars}{allow_unqualified_recipient} = 1;
655 } elsif ($tag eq '-allow_unqualified_sender') {
656 $self->{_vars}{allow_unqualified_sender} = 1;
059ec3d9
PH
657 } elsif ($tag eq '-deliver_firsttime') {
658 $self->{_vars}{deliver_firsttime} = 1;
659 $self->{_vars}{first_delivery} = 1;
660 } elsif ($tag eq '-manual_thaw') {
661 $self->{_vars}{deliver_manual_thaw} = 1;
662 $self->{_vars}{manually_thawed} = 1;
663 } elsif ($tag eq '-auth_id') {
664 $self->{_vars}{authenticated_id} = $arg;
665 } elsif ($tag eq '-auth_sender') {
666 $self->{_vars}{authenticated_sender} = $arg;
667 } elsif ($tag eq '-sender_set_untrusted') {
668 $self->{_vars}{sender_set_untrusted} = 1;
669 } elsif ($tag eq '-tls_certificate_verified') {
670 $self->{_vars}{tls_certificate_verified} = 1;
671 } elsif ($tag eq '-tls_cipher') {
672 $self->{_vars}{tls_cipher} = $arg;
673 } elsif ($tag eq '-tls_peerdn') {
674 $self->{_vars}{tls_peerdn} = $arg;
675 } elsif ($tag eq '-host_address') {
676 $self->{_vars}{sender_host_port} = $self->_get_host_and_port(\$arg);
677 $self->{_vars}{sender_host_address} = $arg;
678 } elsif ($tag eq '-interface_address') {
679 $self->{_vars}{interface_port} = $self->_get_host_and_port(\$arg);
680 $self->{_vars}{interface_address} = $arg;
bf759a8b
PH
681 } elsif ($tag eq '-active_hostname') {
682 $self->{_vars}{smtp_active_hostname} = $arg;
059ec3d9
PH
683 } elsif ($tag eq '-host_auth') {
684 $self->{_vars}{sender_host_authenticated} = $arg;
685 } elsif ($tag eq '-host_name') {
686 $self->{_vars}{sender_host_name} = $arg;
687 } elsif ($tag eq '-helo_name') {
688 $self->{_vars}{sender_helo_name} = $arg;
689 } elsif ($tag eq '-ident') {
690 $self->{_vars}{sender_ident} = $arg;
691 } elsif ($tag eq '-received_protocol') {
692 $self->{_vars}{received_protocol} = $arg;
693 } elsif ($tag eq '-N') {
694 $self->{_vars}{dont_deliver} = 1;
059ec3d9
PH
695 } else {
696 # unrecognized tag, save it for reference
697 $self->{$tag} = $arg;
698 }
699 } else {
700 last;
701 }
702 }
703
8e669ac1 704 # when we drop out of the while loop, we have the first line of the
059ec3d9
PH
705 # delivered tree in $_
706 do {
707 if ($_ eq 'XX') {
708 ; # noop
709 } elsif ($_ =~ s/^[YN][YN]\s+//) {
710 $self->{_del_tree}{$_} = 1;
711 } else {
712 return(0);
713 }
714 chomp($_ = <I>);
715 } while ($_ !~ /^\d+$/);
716
717 $self->{_numrecips} = $_;
718 $self->{_vars}{recipients_count} = $self->{_numrecips};
719 for (my $i = 0; $i < $self->{_numrecips}; $i++) {
720 chomp($_ = <I>);
721 return(0) if (/^$/);
722 my $addr = '';
723 if (/^(.*)\s\d+,(\d+),\d+$/) {
724 #print STDERR "exim3 type (untested): $_\n";
725 $self->{_recips}{$1} = { pno => $2 };
726 $addr = $1;
727 } elsif (/^(.*)\s(\d+)$/) {
728 #print STDERR "exim4 original type (untested): $_\n";
729 $self->{_recips}{$1} = { pno => $2 };
730 $addr = $1;
731 } elsif (/^(.*)\s(.*)\s(\d+),(\d+)#1$/) {
732 #print STDERR "exim4 new type #1 (untested): $_\n";
733 return($self->_error("incorrect format: $_")) if (length($2) != $3);
734 $self->{_recips}{$1} = { pno => $4, errors_to => $2 };
735 $addr = $1;
736 } elsif (/^.*#(\d+)$/) {
bf759a8b 737 #print STDERR "exim4 #$1 style (unimplemented): $_\n";
059ec3d9
PH
738 $self->_error("exim4 #$1 style (unimplemented): $_");
739 } else {
740 #print STDERR "default type: $_\n";
741 $self->{_recips}{$_} = {};
742 $addr = $_;
743 }
744 $self->{_udel_tree}{$addr} = 1 if (!$self->{_del_tree}{$addr});
745 }
af66f652
PH
746 $self->{_vars}{recipients} = join(', ', keys(%{$self->{_recips}}));
747 $self->{_vars}{recipients_del} = join(', ', keys(%{$self->{_del_tree}}));
748 $self->{_vars}{recipients_undel} = join(', ', keys(%{$self->{_udel_tree}}));
749 $self->{_vars}{recipients_undel_count} = scalar(keys(%{$self->{_udel_tree}}));
750 $self->{_vars}{recipients_del_count} = 0;
751 foreach my $r (keys %{$self->{_del_tree}}) {
752 next if (!$self->{_recips}{$r});
753 $self->{_vars}{recipients_del_count}++;
754 }
059ec3d9
PH
755
756 # blank line
757 $_ = <I>;
758 return(0) if (!/^$/);
759
760 # start reading headers
761 while (read(I, $_, 3) == 3) {
762 my $t = getc(I);
763 return(0) if (!length($t));
764 while ($t =~ /^\d$/) {
765 $_ .= $t;
766 $t = getc(I);
767 }
768 # ok, right here $t contains the header flag and $_ contains the number of
769 # bytes to read. If we ever use the header flag, grab it here.
770 $self->{_vars}{message_size} += $_ if ($t ne '*');
771 $t = getc(I); # strip the space out of the file
772 my $bytes = $_;
773 return(0) if (read(I, $_, $bytes) != $bytes);
9cf6b11a
JJ
774 $self->{_vars}{message_linecount} += (tr/\n//) if ($t ne '*');
775
059ec3d9 776 # build the $header_ variable, following exim's rules (sort of)
9cf6b11a
JJ
777 my($v,$d) = split(/:/, $_, 2);
778 $v = "header_" . lc($v);
779 $d =~ s/^\s+//;
780 $d =~ s/\s+$//;
781 $self->{_vars}{$v} .= "$d\n";
782 $self->{_vars}{received_count}++ if ($v eq 'header_received');
059ec3d9 783 # push header onto $message_headers var, following exim's rules
9cf6b11a 784 $self->{_vars}{message_headers} .= $_;
059ec3d9
PH
785 }
786 close(I);
9cf6b11a
JJ
787 # remove trailing newline from $message_headers
788 chomp($self->{_vars}{message_headers});
059ec3d9
PH
789
790 if (length($self->{_vars}{"header_reply-to"}) > 0) {
791 $self->{_vars}{reply_address} = $self->{_vars}{"header_reply-to"};
792 } else {
793 $self->{_vars}{reply_address} = $self->{_vars}{header_from};
794 }
795
796 $self->{_vars}{message_body_size} =
797 (stat($self->{_path}.'/'.$self->{_message}.'-D'))[7] - 19;
798 if ($self->{_vars}{message_body_size} < 0) {
799 $self->{_vars}{message_size} = 0;
800 } else {
801 $self->{_vars}{message_size} += $self->{_vars}{message_body_size} + 1;
802 }
803
5f970846
PH
804 $self->{_vars}{message_linecount} += $self->{_vars}{body_linecount};
805
806 my $i = $self->{_vars}{message_size};
9cf6b11a
JJ
807 if ($i == 0) { $i = ""; }
808 elsif ($i < 1024) { $i = sprintf("%d", $i); }
809 elsif ($i < 10240) { $i = sprintf("%.1fK", $i / 1024); }
810 elsif ($i < 1048576) { $i = sprintf("%dK", ($i+512)/1024); }
811 elsif ($i < 10485760) { $i = sprintf("%.1fM", $i/1048576); }
812 else { $i = sprintf("%dM", ($i + 524288)/1048576); }
5f970846
PH
813 $self->{_vars}{shown_message_size} = $i;
814
059ec3d9 815 return(1);
8e669ac1 816}
059ec3d9
PH
817
818# mimic exim's host_extract_port function - receive a ref to a scalar,
819# strip it of port, return port
820sub _get_host_and_port {
821 my $self = shift;
822 my $host = shift; # scalar ref, be careful
823
824 if ($$host =~ /^\[([^\]]+)\](?:\:(\d+))?$/) {
825 $$host = $1;
826 return($2 || 0);
827 } elsif ($$host =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\.(\d+))?$/) {
828 $$host = $1;
829 return($2 || 0);
830 } elsif ($$host =~ /^([\d\:]+)(?:\.(\d+))?$/) {
831 $$host = $1;
832 return($2 || 0);
833 }
834 # implicit else
835 return(0);
836}
837
838sub print_message {
839 my $self = shift;
840 my $fh = shift || \*STDOUT;
841 return if ($self->{_delivered});
842
843 if ($self->{_output_idonly}) {
5f970846
PH
844 print $fh $self->{_message};
845 foreach my $v (@{$self->{_show_vars}}) {
846 print $fh " $v='", $self->get_var($v), "'";
847 }
848 print $fh "\n";
059ec3d9
PH
849 return;
850 }
8e669ac1 851
059ec3d9
PH
852 if ($self->{_output_long} || $self->{_output_flatq}) {
853 my $i = int($self->{_vars}{message_age} / 60);
854 if ($i > 90) {
855 $i = int(($i+30)/60);
856 if ($i > 72) { printf $fh "%2dd ", int(($i+12)/24); }
857 else { printf $fh "%2dh ", $i; }
858 } else { printf $fh "%2dm ", $i; }
859
5f970846
PH
860 if ($self->{_output_flatq} && $self->{_show_vars}) {
861 print $fh join(';',
862 map { "$_='".$self->get_var($_)."'" }
863 (@{$self->{_show_vars}})
864 );
865 } else {
866 printf $fh "%5s", $self->{_vars}{shown_message_size};
867 }
868 print $fh " ";
059ec3d9
PH
869 }
870 print $fh "$self->{_message} ";
871 print $fh "From: " if ($self->{_output_brief});
872 print $fh "<$self->{_vars}{sender_address}>";
873
874 if ($self->{_output_long}) {
875 print $fh " ($self->{_vars}{originator_login})"
876 if ($self->{_vars}{sender_set_untrusted});
8e669ac1 877
059ec3d9
PH
878 # XXX exim contains code here to print spool format errors
879 print $fh " *** frozen ***" if ($self->{_vars}{deliver_freeze});
880 print $fh "\n";
881
5f970846 882 foreach my $v (@{$self->{_show_vars}}) {
059ec3d9
PH
883 printf $fh " %25s = '%s'\n", $v, $self->get_var($v);
884 }
8e669ac1 885
059ec3d9
PH
886 foreach my $r (keys %{$self->{_recips}}) {
887 next if ($self->{_del_tree}{$r} && $self->{_undelivered_only});
888 printf $fh " %s %s\n", $self->{_del_tree}{$r} ? "D" : " ", $r;
889 }
890 if ($self->{_show_generated}) {
891 foreach my $r (keys %{$self->{_del_tree}}) {
892 next if ($self->{_recips}{$r});
893 printf $fh " +D %s\n", $r;
894 }
895 }
896 } elsif ($self->{_output_brief}) {
897 my @r = ();
898 foreach my $r (keys %{$self->{_recips}}) {
899 next if ($self->{_del_tree}{$r});
900 push(@r, $r);
901 }
902 print $fh " To: ", join(';', @r);
b3f69ca8 903 if ($self->{_show_vars} && scalar(@{$self->{_show_vars}})) {
5f970846
PH
904 print $fh " Vars: ", join(';',
905 map { "$_='".$self->get_var($_)."'" }
906 (@{$self->{_show_vars}})
907 );
908 }
059ec3d9
PH
909 } elsif ($self->{_output_flatq}) {
910 print $fh " *** frozen ***" if ($self->{_vars}{deliver_freeze});
911 my @r = ();
912 foreach my $r (keys %{$self->{_recips}}) {
913 next if ($self->{_del_tree}{$r});
914 push(@r, $r);
915 }
916 print $fh " ", join(' ', @r);
917 }
918
919 print $fh "\n";
920}
921
922sub dump {
923 my $self = shift;
924
925 foreach my $k (sort keys %$self) {
926 my $r = ref($self->{$k});
927 if ($r eq 'ARRAY') {
928 printf "%20s <<EOM\n", $k;
929 print @{$self->{$k}}, "EOM\n";
930 } elsif ($r eq 'HASH') {
931 printf "%20s <<EOM\n", $k;
932 foreach (sort keys %{$self->{$k}}) {
933 printf "%20s %s\n", $_, $self->{$k}{$_};
934 }
935 print "EOM\n";
936 } else {
937 printf "%20s %s\n", $k, $self->{$k};
938 }
939 }
940}
941
942} # BEGIN
943
944sub ext_usage {
945 if ($ARGV[0] =~ /^--help$/i) {
946 require Config;
947 $ENV{PATH} .= ":" unless $ENV{PATH} eq "";
948 $ENV{PATH} = "$ENV{PATH}$Config::Config{'installscript'}";
949 #exec("perldoc", "-F", "-U", $0) || exit 1;
950 $< = $> = 1 if ($> == 0 || $< == 0);
951 exec("perldoc", $0) || exit 1;
952 # make parser happy
953 %Config::Config = ();
954 } elsif ($ARGV[0] =~ /^--version$/i) {
955 print "$p_name version $p_version\n\n$p_cp\n";
956 } else {
957 return;
958 }
959
960 exit(0);
961}
962
963__END__
964
965=head1 NAME
966
967exipick - display messages from Exim queue based on a variety of criteria
968
969=head1 USAGE
970
971exipick [--help|--version] | [-spool <spool>] [-and|-or] [-bp|-bpa|-bpc|-bpr|-bpra|-bpru|-bpu] [<criterion> [<criterion> ...]]
972
973=head1 DESCRIPTION
974
bf759a8b 975exipick 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.
059ec3d9
PH
976
977=head1 OPTIONS
978
979=over 4
980
bf759a8b 981=item --spool
059ec3d9 982
bf759a8b 983The 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 (and if exipick was installed from the Exim distribution, this is done by default), but this option is useful for alternate installs, or installs on NFS servers, etc.
059ec3d9 984
bf759a8b 985=item --and
059ec3d9
PH
986
987A message will be displayed only if it matches all of the specified criteria. This is the default.
988
bf759a8b 989=item --or
059ec3d9
PH
990
991A message will be displayed if it matches any of the specified criteria.
992
af66f652
PH
993=item --caseful
994
995By default criteria using the '=' operator are caseless. Specifying this option make them respect case.
996
5f970846
PH
997=item --show-vars <variable>[,<variable>...]
998
999Cause the value of each specified variable to be displayed for every message dispayed. For instance, the command "exipick --show-vars '$sender_ident' 'sender_host_address eq 127.0.01'" will show the ident string for every message submitted via localhost. How exactly the variable value is diplayed changes according to what output format you specify.
1000
1001=item --show-rules
1002
1003If specified the internal representation of each message criteria is shown. This is primarily used for debugging purposes.
1004
1005==item --show-tests
1006
1007If specified, for every message (regardless of matching criteria) the criteria's actual value is shown and the compiled internal eval is shown. This is used primarily for debugging purposes.
1008
1009=item --flatq
1010
1011Change format of output so that every message is on a single line. Useful for parsing with tools such as sed, awk, cut, etc.
1012
9cf6b11a
JJ
1013=item --unsorted
1014
1015This prevents sorting the messages according to their age when they are displayed. While there were exim-clone options that enabled this functionality (-bpr, -bpra, etc) they only worked in the standard output format. --unsorted works in all output formats, including the exiqgrep clone output and --flatq.
1016
059ec3d9
PH
1017=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:
1018
1019=item -bp display the matching messages in 'mailq' format.
1020
1021=item -bpa ... with generated addresses as well.
1022
1023=item -bpc ... just show a count of messages.
1024
1025=item -bpr ... do not sort.
1026
1027=item -bpra ... with generated addresses, unsorted.
1028
1029=item -bpru ... only undelivered addresses, unsorted.
1030
1031=item -bpu ... only undelivered addresses.
1032
1033Please see Exim's spec.txt for details on the format and information displayed with each option.
1034
1035=item The following options are included for compatibility with the 'exiqgrep' utility:
1036
1037=item -f <regexp> Same as '$sender_address = <regexp>'
1038
1039=item -r <regexp> Same as '$recipients = <regexp>'
1040
5f970846
PH
1041=item -s <string> Same as '$shown_message_size eq <string>'
1042
059ec3d9
PH
1043=item -y <seconds> Same as '$message_age < <seconds>'
1044
1045=item -o <seconds> Same as '$message_age > <seconds>'
1046
1047=item -z Same as '$deliver_freeze'
1048
1049=item -x Same as '!$deliver_freeze'
1050
1051=item -c Display count of matches only
1052
1053=item -l Display in long format (default)
1054
1055=item -i Display message IDs only
1056
1057=item -b Display brief format only
1058
1059Please see the 'exiqgrep' documentation for more details on the behaviour and output format produced by these options
1060
1061=item <criterion>
1062
bf759a8b 1063The 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.
059ec3d9
PH
1064
1065The 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.
1066
1067If 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')
1068
9cf6b11a
JJ
1069=item --freeze <cache file>, --thaw <cache file>
1070
1071Every time exipick runs, it has to rescan the input directory, open every file, and correctly parse the contents of every file. While this isn't very time consuming on with a small queue or a lightly loaded server, it can take a great deal of time on heavily loaded machines or large queues. Unfortunately, one of the best times to use exipick is diagnosing large mail queues.
1072
1073To speed run times in these situations, you can use --freeze to save a cache of the message information. --thaw can then be used to read from the cache rather than directly from the spool. Over time, of course, the information in the cache will drift further and further out of date, but this is not a significant problem over short runs, but do keep in mind that any deliveries made or messages removed from the queue after the cache file is made will not be reflected in the output when using --thaw.
1074
1075All message variables are saved to the cache except $message_body and $message_age. $message_age is skipped because it is recalculated dynamically at every running of exipick. $message_body is skipped because of the potentially large storage requirements. If $message_body is referenced in any criteria when using --thaw, the data will be looked up from the spool file if the message is still in the spool.
1076
1077If criteria are specified when using --freeze, only matching messages will be written to the cache file. Subsequent runs of exipick --thaw using that cache file will not need the original criteria specified.
1078
1079There are tradeoffs when using this system, time and space. The cache file will take disk space to write. The size of the file depends on the type of mail the server handles, but it ranges between 2KB and 5KB per message. The run of exipick which creates the cache file will take longer to run than a standard run, perhaps as much as 50% longer, but the subsequent runs readng from the cache file will take as little as 10-20% of the time it would take for a run of exipick without --freeze/--thaw. In other words, if a system is in a state where it takes 30 seconds to run exipick, making a cache file will take around 45 second, but subsequent reads of the cache will take around 5 seconds. The size needed for the cache file decrease and the performance gains on the --thaw runs increase if criteria which limits the number of messages written to the cache file are used on the --freeze run.
1080
059ec3d9
PH
1081=item --help
1082
1083This screen.
1084
1085=item --version
1086
1087Version info.
1088
1089=back
1090
1091=head1 VARIABLE TYPES
1092
1093Although 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.
1094
1095=over 4
1096
1097=item NUMERIC
1098
1099Variable of the numeric type can be of integer or float. Valid comparisons are <, <=, >, >=, ==, and !=.
1100
bf759a8b 1101The 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. That is, though it's odd it's legal to create a criterion of a message being around for 3 kiloseconds: '$message_age >= 3K'.
059ec3d9
PH
1102
1103=item BOOLEAN
1104
bf759a8b 1105Variables 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.
059ec3d9
PH
1106
1107=item STRING
1108
bf759a8b 1109String variables are basically defined as those that are neither numeric nor boolean and can contain any data. The string operators are =, eq, ne, =~, and !~. With the exception of '=', the operators all match the functionality of the like-named perl operators.
059ec3d9 1110
bf759a8b 1111The 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. This comparison is caseless by default, but see the --caseful option to change this.
059ec3d9 1112
bf759a8b 1113Slightly 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 with a helo string other than "hotmail.com".
059ec3d9 1114
bf759a8b 1115The 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.
059ec3d9 1116
9cf6b11a
JJ
1117=item NEGATION
1118
1119In addition to standard logical negation available with the operators above (== vs !=, < vs >=, etc) any criteria can be whole negated by prepending an exclamation mark ("!") to the variable name. This is required for negating boolean variables, and very convenient for negating the simple '=' operator (previously, the opposite of '$var = foo' was '$var !~ /foo/'. This can now be written '!$var = foo').
1120
059ec3d9
PH
1121=back
1122
1123=head1 VARIABLES
1124
bf759a8b 1125With 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.
059ec3d9
PH
1126
1127In 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.
1128
1129=head2 Boolean variables
1130
1131=over 4
1132
bf759a8b 1133=item + $allow_unqualified_recipient
059ec3d9
PH
1134
1135TRUE if unqualified recipient addresses are permitted in header lines.
1136
bf759a8b 1137=item + $allow_unqualified_sender
059ec3d9
PH
1138
1139TRUE if unqualified sender addresses are permitted in header lines.
1140
bf759a8b 1141=item + $deliver_freeze
059ec3d9 1142
bf759a8b 1143TRUE if the message is currently frozen.
059ec3d9 1144
bf759a8b 1145=item . $first_delivery
059ec3d9 1146
bf759a8b 1147TRUE if the message has never been deferred.
059ec3d9 1148
bf759a8b 1149=item . $manually_thawed
059ec3d9
PH
1150
1151TRUE when the message has been manually thawed.
1152
bf759a8b 1153=item + $dont_deliver
059ec3d9
PH
1154
1155TRUE if, under normal circumstances, Exim will not try to deliver the message.
1156
bf759a8b
PH
1157=item . $host_lookup_deferred
1158
1159TRUE if there was an attempt to look up the host's name from its IP address, but an error occurred that during the attempt.
1160
1161=item . $host_lookup_failed
059ec3d9 1162
bf759a8b 1163TRUE if there was an attempt to look up the host's name from its IP address, but the attempt returned a negative result.
059ec3d9 1164
bf759a8b 1165=item + $local_error_message
059ec3d9
PH
1166
1167TRUE if the message is a locally-generated error message.
1168
bf759a8b 1169=item + $sender_local
059ec3d9
PH
1170
1171TRUE if the message was locally generated.
1172
bf759a8b 1173=item + $sender_set_untrusted
059ec3d9
PH
1174
1175TRUE if the envelope sender of this message was set by an untrusted local caller.
1176
bf759a8b 1177=item . $tls_certificate_verified
059ec3d9
PH
1178
1179TRUE if a TLS certificate was verified when the message was received.
1180
1181=back
1182
1183=head2 Numeric variables
1184
1185=over 4
1186
bf759a8b 1187=item . $body_linecount
059ec3d9
PH
1188
1189The number of lines in the message's body.
1190
bf759a8b 1191=item . $body_zerocount
059ec3d9
PH
1192
1193The number of binary zero bytes in the message's body.
1194
bf759a8b 1195=item + $deliver_frozen_at
059ec3d9
PH
1196
1197The epoch time at which message was frozen.
1198
bf759a8b 1199=item . $interface_port
059ec3d9
PH
1200
1201The local port number if network-originated messages.
1202
bf759a8b 1203=item . $message_age
059ec3d9
PH
1204
1205The number of seconds since the message was received.
1206
bf759a8b 1207=item . $message_body_size
059ec3d9
PH
1208
1209The size of the body in bytes.
1210
5f970846
PH
1211=item . $message_linecount
1212
1213The number of lines in the entire message (body and headers).
1214
bf759a8b 1215=item . $message_size
059ec3d9
PH
1216
1217The size of the message in bytes.
1218
bf759a8b 1219=item . $originator_gid
059ec3d9
PH
1220
1221The group id under which the process that called Exim was running as when the message was received.
1222
bf759a8b 1223=item . $originator_uid
059ec3d9
PH
1224
1225The user id under which the process that called Exim was running as when the message was received.
1226
bf759a8b 1227=item . $received_count
059ec3d9
PH
1228
1229The number of Received: header lines in the message.
1230
5f970846 1231=item . $received_time
059ec3d9
PH
1232
1233The epoch time at which the message was received.
1234
bf759a8b 1235=item . $recipients_count
059ec3d9 1236
af66f652
PH
1237The number of envelope recipients for the message.
1238
bf759a8b 1239=item + $recipients_del_count
af66f652
PH
1240
1241The number of envelope recipients for the message which have already been delivered. Note that this is the count of original recipients to which the message has been delivered. It does not include generated addresses so it is possible that this number will be less than the number of addresses in the recipients_del string.
1242
bf759a8b 1243=item + $recipients_undel_count
af66f652
PH
1244
1245The number of envelope recipients for the message which have not yet been delivered.
059ec3d9 1246
bf759a8b 1247=item . $sender_host_port
059ec3d9
PH
1248
1249The port number that was used on the remote host for network-originated messages.
1250
bf759a8b 1251=item + $warning_count
059ec3d9
PH
1252
1253The number of delay warnings which have been sent for this message.
1254
1255=back
1256
1257=head2 String variables
1258
1259=over 4
1260
bf759a8b 1261=item . $acl_c0-$acl_c9, $acl_m0-$acl_m9
059ec3d9
PH
1262
1263User definable variables.
1264
bf759a8b 1265=item . $authenticated_id
059ec3d9
PH
1266
1267Optional saved information from authenticators, or the login name of the calling process for locally submitted messages.
1268
bf759a8b 1269=item . $authenticated_sender
059ec3d9
PH
1270
1271The value of AUTH= param for smtp messages, or a generated value from the calling processes login and qualify domain for locally submitted messages.
1272
bf759a8b
PH
1273=item + $bmi_verdicts
1274
1275I honestly don't know what the format of this variable is. It only exists if you have Exim compiled with WITH_CONTENT_SCAN and EXPERIMENTAL_BRIGHTMAIL (and, you know, pay Symantec/Brightmail a bunch of money for the client libs and a server to use them with).
1276
1277=item + $each_recipients
1278
1279This is a psuedo variable which allows you to apply a criterion against each address in $recipients individually. This allows you to create criteria against which every individual recipient is tested. For instance, '$recipients =~ /aol.com/' will match if any of the recipient addresses contain the string "aol.com". However, with the criterion '$each_recipients =~ /@aol.com$/', a message will only match if B<every> recipient matches that pattern. Note that this obeys --and or --or being set. Using it with --or is very similar to just matching against $recipients, but with the added benefit of being able to use anchors at the beginning and end of each recipient address.
1280
1281=item + $each_recipients_del
1282
1283Like $each_recipients, but for the $recipients_del variable.
1284
1285=item + $each_recipients_undel
1286
1287Like $each_recipients, but for the $recipients_undel variable.
1288
1289=item # $header_*
059ec3d9
PH
1290
1291The 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.
1292
bf759a8b 1293=item . $interface_address
059ec3d9
PH
1294
1295The address of the local IP interface for network-originated messages.
1296
bf759a8b 1297=item . $local_scan_data
059ec3d9
PH
1298
1299The text returned by the local_scan() function when a message is received.
1300
bf759a8b 1301=item # $message_body
059ec3d9
PH
1302
1303The 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.
1304
bf759a8b 1305=item . $message_headers
059ec3d9
PH
1306
1307A concatenation of all the header lines except for lines added by routers or transports.
1308
ee744174 1309=item . $message_exim_id, $message_id
059ec3d9 1310
ee744174 1311The unique message id that is used by Exim to identify the message. $message_id is deprecated as of Exim 4.53.
059ec3d9 1312
bf759a8b 1313=item + $originator_login
059ec3d9
PH
1314
1315The login of the process which called Exim.
1316
bf759a8b 1317=item . $received_protocol
059ec3d9
PH
1318
1319The name of the protocol by which the message was received.
1320
bf759a8b 1321=item # $recipients
059ec3d9
PH
1322
1323The 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.
1324
bf759a8b 1325=item + $recipients_del
059ec3d9 1326
af66f652 1327The 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 including any generated addresses.
059ec3d9 1328
bf759a8b 1329=item + $recipients_undel
059ec3d9
PH
1330
1331The 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.
1332
bf759a8b 1333=item . $reply_address
059ec3d9
PH
1334
1335The contents of the Reply-To: header line if one exists and it is not empty, or otherwise the contents of the From: header line.
1336
bf759a8b 1337=item . $sender_address
059ec3d9
PH
1338
1339The sender's address that was received in the message's envelope. For bounce messages, the value of this variable is the empty string.
1340
bf759a8b 1341=item . $sender_address_domain
059ec3d9 1342
bf759a8b 1343The domain part of $sender_address.
059ec3d9 1344
bf759a8b 1345=item . $sender_address_local_part
059ec3d9 1346
bf759a8b 1347The local part of $sender_address.
059ec3d9 1348
bf759a8b 1349=item . $sender_helo_name
059ec3d9
PH
1350
1351The HELO or EHLO value supplied for smtp or bsmtp messages.
1352
bf759a8b 1353=item . $sender_host_address
059ec3d9
PH
1354
1355The remote host's IP address.
1356
bf759a8b 1357=item . $sender_host_authenticated
059ec3d9
PH
1358
1359The name of the authenticator driver which successfully authenticated the client from which the message was received.
1360
bf759a8b 1361=item . $sender_host_name
059ec3d9
PH
1362
1363The remote host's name as obtained by looking up its IP address.
1364
bf759a8b 1365=item . $sender_ident
059ec3d9
PH
1366
1367The 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.
1368
5f970846
PH
1369=item + $shown_message_size
1370
1371This non-standard variable contains the formatted size string. That is, for a message whose $message_size is 66566 bytes, $shown_message_size is 65K.
1372
bf759a8b
PH
1373=item . $smtp_active_hostname
1374
1375The value of the active host name when the message was received, as specified by the "smtp_active_hostname" option.
1376
1377=item . $spam_score
1378
1379The spam score of the message, for example '3.4' or '30.5'. (Requires exiscan or WITH_CONTENT_SCAN)
1380
1381=item . $spam_score_int
1382
1383The spam score of the message, multiplied by ten, as an integer value. For instance '34' or '305'. (Requires exiscan or WITH_CONTENT_SCAN)
1384
1385=item . $tls_cipher
059ec3d9
PH
1386
1387The cipher suite that was negotiated for encrypted SMTP connections.
1388
bf759a8b 1389=item . $tls_peerdn
059ec3d9
PH
1390
1391The value of the Distinguished Name of the certificate if Exim is configured to request one.
1392
1393=back
1394
1395=head1 EXAMPLES
1396
1397=over 4
1398
bf759a8b 1399=item exipick '$deliver_freeze'
059ec3d9
PH
1400
1401Display only frozen messages.
1402
bf759a8b 1403=item exipick '$received_protocol eq asmtp' '$message_age < 20m'
059ec3d9 1404
bf759a8b 1405Display only messages which were delivered over an authenticated smtp session in the last 20 minutes.
059ec3d9 1406
bf759a8b 1407=item exipick -bpc '$message_size > 200K'
059ec3d9
PH
1408
1409Display a count of messages in the queue which are over 200 kilobytes in size.
1410
bf759a8b 1411=item exipick -or '$sender_helo_name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/' '$sender_helo_name = _'
059ec3d9
PH
1412
1413Display message which have a HELO string which either is an IP address or contains an underscore.
1414
1415=back
1416
1417=head1 REQUIREMENTS
1418
bf759a8b 1419None 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 (assuming this was not done automatically by the Exim install process).
059ec3d9
PH
1420
1421=head1 ACKNOWLEDGEMENTS
1422
1423Although I conceived of the concept for this program independently, the name 'exipick' was taken from the Exim WishList and was suggested by Jeffrey Goldberg.
1424
1425Thank 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.
1426
1427=head1 CONTACT
1428
1429=over 4
1430
1431=item EMAIL: proj-exipick@jetmore.net
1432
1433=item HOME: jetmore.org/john/code/#exipick
1434
1435=back
1436
1437=cut