SOCKS: fix unitialized-pointer
[exim.git] / src / src / exipick.src
CommitLineData
059ec3d9 1#!PERL_COMMAND
059ec3d9 2
57397119
HSHR
3# This variables should be set by the building process
4my $spool = 'SPOOL_DIRECTORY'; # may be overridden later
5my $exim = 'BIN_DIRECTORY/exim';
6
0ea2a468
JJ
7# Need to set this dynamically during build, but it's not used right now anyway.
8my $charset = 'ISO-8859-1';
059ec3d9 9
e22ca4ac
JJ
10# use 'exipick --help' to view documentation for this program.
11# Documentation also viewable online at
12# http://www.exim.org/eximwiki/ToolExipickManPage
13
059ec3d9 14use strict;
4d3d955f 15BEGIN { pop @INC if $INC[-1] eq '.' };
059ec3d9
PH
16use Getopt::Long;
17
18my($p_name) = $0 =~ m|/?([^/]+)$|;
1a41defa 19my $p_version = "20100323.0";
059ec3d9
PH
20my $p_usage = "Usage: $p_name [--help|--version] (see --help for details)";
21my $p_cp = <<EOM;
465e92cf 22 Copyright (c) 2003-2010 John Jetmore <jj33\@pobox.com>
059ec3d9
PH
23
24 This program is free software; you can redistribute it and/or modify
25 it under the terms of the GNU General Public License as published by
26 the Free Software Foundation; either version 2 of the License, or
27 (at your option) any later version.
28
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 GNU General Public License for more details.
33
34 You should have received a copy of the GNU General Public License
35 along with this program; if not, write to the Free Software
e22ca4ac 36 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
059ec3d9
PH
37EOM
38ext_usage(); # before we do anything else, check for --help
39
bf759a8b
PH
40$| = 1; # unbuffer STDOUT
41
059ec3d9
PH
42Getopt::Long::Configure("bundling_override");
43GetOptions(
a2405d83 44 'spool=s' => \$G::spool, # exim spool dir
edae0343
JJ
45 'input-dir=s' => \$G::input_dir, # name of the "input" dir
46 'finput' => \$G::finput, # same as "--input-dir Finput"
bf759a8b
PH
47 'bp' => \$G::mailq_bp, # List the queue (noop - default)
48 'bpa' => \$G::mailq_bpa, # ... with generated address as well
49 'bpc' => \$G::mailq_bpc, # ... but just show a count of messages
50 'bpr' => \$G::mailq_bpr, # ... do not sort
51 'bpra' => \$G::mailq_bpra, # ... with generated addresses, unsorted
52 'bpru' => \$G::mailq_bpru, # ... only undelivered addresses, unsorted
53 'bpu' => \$G::mailq_bpu, # ... only undelivered addresses
54 'and' => \$G::and, # 'and' the criteria (default)
55 'or' => \$G::or, # 'or' the criteria
a2405d83
JJ
56 'f=s' => \$G::qgrep_f, # from regexp
57 'r=s' => \$G::qgrep_r, # recipient regexp
58 's=s' => \$G::qgrep_s, # match against size field
59 'y=s' => \$G::qgrep_y, # message younger than (secs)
60 'o=s' => \$G::qgrep_o, # message older than (secs)
bf759a8b
PH
61 'z' => \$G::qgrep_z, # frozen only
62 'x' => \$G::qgrep_x, # non-frozen only
63 'c' => \$G::qgrep_c, # display match count
64 'l' => \$G::qgrep_l, # long format (default)
65 'i' => \$G::qgrep_i, # message ids only
66 'b' => \$G::qgrep_b, # brief format
e22ca4ac
JJ
67 'size' => \$G::size_only, # sum the size of the matching msgs
68 'not' => \$G::negate, # flip every test
69 'R|reverse' => \$G::reverse, # reverse output (-R is qgrep option)
a2405d83
JJ
70 'sort=s' => \@G::sort, # allow you to choose variables to sort by
71 'freeze=s' => \$G::freeze, # freeze data in this file
72 'thaw=s' => \$G::thaw, # thaw data from this file
9cf6b11a 73 'unsorted' => \$G::unsorted, # unsorted, regardless of output format
e22ca4ac 74 'random' => \$G::random, # (poorly) randomize evaluation order
bf759a8b
PH
75 'flatq' => \$G::flatq, # brief format
76 'caseful' => \$G::caseful, # in '=' criteria, respect case
77 'caseless' => \$G::caseless, # ...ignore case (default)
0ea2a468 78 'charset=s' => \$charset, # charset for $bh and $h variables
a2405d83 79 'show-vars=s' => \$G::show_vars, # display the contents of these vars
0ea2a468 80 'just-vars' => \$G::just_vars, # only display vars, no other info
bf759a8b
PH
81 'show-rules' => \$G::show_rules, # display compiled match rules
82 'show-tests' => \$G::show_tests # display tests as applied to each message
059ec3d9
PH
83) || exit(1);
84
4c04137d 85# if both freeze and thaw specified, only thaw as it is less destructive
9cf6b11a
JJ
86$G::freeze = undef if ($G::freeze && $G::thaw);
87freeze_start() if ($G::freeze);
88thaw_start() if ($G::thaw);
89
e22ca4ac
JJ
90# massage sort options (make '$var,Var:' be 'var','var')
91for (my $i = scalar(@G::sort)-1; $i >= 0; $i--) {
92 $G::sort[$i] = lc($G::sort[$i]);
93 $G::sort[$i] =~ s/[\$:\s]//g;
94 if ((my @vars = split(/,/, $G::sort[$i])) > 1) {
95 $G::sort[$i] = $vars[0]; shift(@vars); # replace current slot w/ first var
96 splice(@G::sort, $i+1, 0, @vars); # add other vars after current pos
97 }
98}
99push(@G::sort, "message_exim_id") if (@G::sort);
100die "empty value provided to --sort not allowed, exiting\n"
101 if (grep /^\s*$/, @G::sort);
102
103# massage the qgrep options into standard criteria
5f970846
PH
104push(@ARGV, "\$sender_address =~ /$G::qgrep_f/") if ($G::qgrep_f);
105push(@ARGV, "\$recipients =~ /$G::qgrep_r/") if ($G::qgrep_r);
106push(@ARGV, "\$shown_message_size eq $G::qgrep_s") if ($G::qgrep_s);
107push(@ARGV, "\$message_age < $G::qgrep_y") if ($G::qgrep_y);
108push(@ARGV, "\$message_age > $G::qgrep_o") if ($G::qgrep_o);
109push(@ARGV, "\$deliver_freeze") if ($G::qgrep_z);
110push(@ARGV, "!\$deliver_freeze") if ($G::qgrep_x);
e22ca4ac 111
bf759a8b
PH
112$G::mailq_bp = $G::mailq_bp; # shut up -w
113$G::and = $G::and; # shut up -w
b3f69ca8 114$G::msg_ids = {}; # short circuit when crit is only MID
bf759a8b 115$G::caseless = $G::caseful ? 0 : 1; # nocase by default, case if both
b3f69ca8 116@G::recipients_crit = (); # holds per-recip criteria
57397119
HSHR
117$spool = defined $G::spool ? $G::spool
118 : do { chomp($_ = `$exim -n -bP spool_directory`);
119 $_ // $spool };
edae0343 120my $input_dir = $G::input_dir || ($G::finput ? "Finput" : "input");
9cf6b11a
JJ
121my $count_only = 1 if ($G::mailq_bpc || $G::qgrep_c);
122my $unsorted = 1 if ($G::mailq_bpr || $G::mailq_bpra ||
123 $G::mailq_bpru || $G::unsorted);
124my $msg = $G::thaw ? thaw_message_list()
edae0343 125 : get_all_msgs($spool, $input_dir, $unsorted,
e22ca4ac 126 $G::reverse, $G::random);
9cf6b11a 127die "Problem accessing thaw file\n" if ($G::thaw && !$msg);
bf759a8b
PH
128my $crit = process_criteria(\@ARGV);
129my $e = Exim::SpoolFile->new();
b3f69ca8
JJ
130my $tcount = 0 if ($count_only); # holds count of all messages
131my $mcount = 0 if ($count_only); # holds count of matching messages
e22ca4ac 132my $total_size = 0 if ($G::size_only);
bf759a8b
PH
133$e->set_undelivered_only(1) if ($G::mailq_bpru || $G::mailq_bpu);
134$e->set_show_generated(1) if ($G::mailq_bpra || $G::mailq_bpa);
135$e->output_long() if ($G::qgrep_l);
136$e->output_idonly() if ($G::qgrep_i);
137$e->output_brief() if ($G::qgrep_b);
138$e->output_flatq() if ($G::flatq);
0ea2a468 139$e->output_vars_only() if ($G::just_vars && $G::show_vars);
059ec3d9 140$e->set_show_vars($G::show_vars) if ($G::show_vars);
edae0343 141$e->set_spool($spool, $input_dir);
059ec3d9
PH
142
143MSG:
144foreach my $m (@$msg) {
af66f652
PH
145 next if (scalar(keys(%$G::msg_ids)) && !$G::or
146 && !$G::msg_ids->{$m->{message}});
9cf6b11a
JJ
147 if ($G::thaw) {
148 my $data = thaw_data();
149 if (!$e->restore_state($data)) {
150 warn "Couldn't thaw $data->{_message}: ".$e->error()."\n";
151 next MSG;
152 }
153 } else {
154 if (!$e->parse_message($m->{message}, $m->{path})) {
155 warn "Couldn't parse $m->{message}: ".$e->error()."\n";
156 next MSG;
157 }
059ec3d9
PH
158 }
159 $tcount++;
160 my $match = 0;
bf759a8b
PH
161 my @local_crit = ();
162 foreach my $c (@G::recipients_crit) { # handle each_recip* vars
163 foreach my $addr (split(/, /, $e->get_var($c->{var}))) {
164 my %t = ( 'cmp' => $c->{cmp}, 'var' => $c->{var} );
165 $t{cmp} =~ s/"?\$var"?/'$addr'/;
166 push(@local_crit, \%t);
167 }
168 }
ee744174 169 if ($G::show_tests) { print $e->get_var('message_exim_id'), "\n"; }
059ec3d9 170 CRITERIA:
bf759a8b 171 foreach my $c (@$crit, @local_crit) {
059ec3d9
PH
172 my $var = $e->get_var($c->{var});
173 my $ret = eval($c->{cmp});
bf759a8b
PH
174 if ($G::show_tests) {
175 printf " %25s = '%s'\n %25s => $ret\n",$c->{var},$var,$c->{cmp},$ret;
176 }
059ec3d9
PH
177 if ($@) {
178 print STDERR "Error in eval '$c->{cmp}': $@\n";
11121d3d 179 next MSG;
059ec3d9
PH
180 } elsif ($ret) {
181 $match = 1;
11121d3d
JJ
182 if ($G::or) { last CRITERIA; }
183 else { next CRITERIA; }
059ec3d9 184 } else { # no match
11121d3d
JJ
185 if ($G::or) { next CRITERIA; }
186 else { next MSG; }
059ec3d9
PH
187 }
188 }
b3f69ca8
JJ
189
190 # skip this message if any criteria were supplied and it didn't match
11121d3d 191 next MSG if ((scalar(@$crit) || scalar(@local_crit)) && !$match);
059ec3d9 192
e22ca4ac 193 if ($count_only || $G::size_only) {
059ec3d9 194 $mcount++;
e22ca4ac 195 $total_size += $e->get_var('message_size');
059ec3d9 196 } else {
e22ca4ac
JJ
197 if (@G::sort) {
198 # if we are defining criteria to sort on, save the message here. If
199 # we don't save here and do the sort later, we have a chicken/egg
200 # problem
201 push(@G::to_print, { vars => {}, output => "" });
202 foreach my $var (@G::sort) {
203 # save any values we want to sort on. I don't like doing the internal
204 # struct access here, but calling get_var a bunch can be _slow_ =(
205 $G::sort_type{$var} ||= '<=>';
206 $G::to_print[-1]{vars}{$var} = $e->{_vars}{$var};
207 $G::sort_type{$var} = 'cmp' if ($G::to_print[-1]{vars}{$var} =~ /\D/);
208 }
209 $G::to_print[-1]{output} = $e->format_message();
210 } else {
211 print $e->format_message();
212 }
059ec3d9 213 }
9cf6b11a
JJ
214
215 if ($G::freeze) {
216 freeze_data($e->get_state());
217 push(@G::frozen_msgs, $m);
218 }
059ec3d9
PH
219}
220
e22ca4ac
JJ
221if (@G::to_print) {
222 msg_sort(\@G::to_print, \@G::sort, $G::reverse);
223 foreach my $msg (@G::to_print) {
224 print $msg->{output};
225 }
226}
227
228if ($G::qgrep_c) {
229 print "$mcount matches out of $tcount messages" .
230 ($G::size_only ? " ($total_size)" : "") . "\n";
231} elsif ($G::mailq_bpc) {
232 print "$mcount" . ($G::size_only ? " ($total_size)" : "") . "\n";
233} elsif ($G::size_only) {
234 print "$total_size\n";
059ec3d9
PH
235}
236
9cf6b11a
JJ
237if ($G::freeze) {
238 freeze_message_list(\@G::frozen_msgs);
239 freeze_end();
240} elsif ($G::thaw) {
241 thaw_end();
242}
243
059ec3d9
PH
244exit;
245
e22ca4ac
JJ
246# sender_address_domain,shown_message_size
247sub msg_sort {
248 my $msgs = shift;
249 my $vars = shift;
250 my $reverse = shift;
251
252 my @pieces = ();
253 foreach my $v (@G::sort) {
254 push(@pieces, "\$a->{vars}{\"$v\"} $G::sort_type{$v} \$b->{vars}{\"$v\"}");
255 }
256 my $sort_str = join(" || ", @pieces);
257
258 @$msgs = sort { eval $sort_str } (@$msgs);
259 @$msgs = reverse(@$msgs) if ($reverse);
260}
261
262sub try_load {
263 my $mod = shift;
264
265 eval("use $mod");
266 return $@ ? 0 : 1;
267}
268
9cf6b11a
JJ
269# FREEZE FILE FORMAT:
270# message_data_bytes
271# message_data
272# <...>
273# EOM
274# message_list
275# message_list_bytes <- 10 bytes, zero-packed, plus \n
276
277sub freeze_start {
278 eval("use Storable");
279 die "Storable module not found: $@\n" if ($@);
280 open(O, ">$G::freeze") || die "Can't open freeze file $G::freeze: $!\n";
281 $G::freeze_handle = \*O;
282}
283
284sub freeze_end {
285 close($G::freeze_handle);
286}
287
288sub thaw_start {
289 eval("use Storable");
290 die "Storable module not found: $@\n" if ($@);
291 open(I, "<$G::thaw") || die "Can't open freeze file $G::thaw: $!\n";
292 $G::freeze_handle = \*I;
293}
294
295sub thaw_end {
296 close($G::freeze_handle);
297}
298
299sub freeze_data {
300 my $h = Storable::freeze($_[0]);
301 print $G::freeze_handle length($h)+1, "\n$h\n";
302}
303
304sub freeze_message_list {
305 my $h = Storable::freeze($_[0]);
306 my $l = length($h) + 1;
307 printf $G::freeze_handle "EOM\n$l\n$h\n%010d\n", $l+11+length($l)+1;
308}
309
310sub thaw_message_list {
311 my $orig_pos = tell($G::freeze_handle);
312 seek($G::freeze_handle, -11, 2);
313 chomp(my $bytes = <$G::freeze_handle>);
314 seek($G::freeze_handle, $bytes * -1, 2);
315 my $obj = thaw_data();
316 seek($G::freeze_handle, 0, $orig_pos);
317 return($obj);
318}
319
320sub thaw_data {
321 my $obj;
322 chomp(my $bytes = <$G::freeze_handle>);
323 return(undef) if (!$bytes || $bytes eq 'EOM');
324 my $read = read(I, $obj, $bytes);
325 die "Format error in thaw file (expected $bytes bytes, got $read)\n"
326 if ($bytes != $read);
327 chomp($obj);
328 return(Storable::thaw($obj));
329}
330
059ec3d9
PH
331sub process_criteria {
332 my $a = shift;
333 my @c = ();
334 my $e = 0;
335
336 foreach (@$a) {
e22ca4ac 337 foreach my $t ('@') { s/$t/\\$t/g; }
059ec3d9
PH
338 if (/^(.*?)\s+(<=|>=|==|!=|<|>)\s+(.*)$/) {
339 #print STDERR "found as integer\n";
340 my $v = $1; my $o = $2; my $n = $3;
0ea2a468
JJ
341 if ($n =~ /^(-?[\d\.]+)M$/) { $n = $1 * 1024 * 1024; }
342 elsif ($n =~ /^(-?[\d\.]+)K$/) { $n = $1 * 1024; }
343 elsif ($n =~ /^(-?[\d\.]+)B?$/) { $n = $1; }
344 elsif ($n =~ /^(-?[\d\.]+)d$/) { $n = $1 * 60 * 60 * 24; }
345 elsif ($n =~ /^(-?[\d\.]+)h$/) { $n = $1 * 60 * 60; }
346 elsif ($n =~ /^(-?[\d\.]+)m$/) { $n = $1 * 60; }
347 elsif ($n =~ /^(-?[\d\.]+)s?$/) { $n = $1; }
059ec3d9
PH
348 else {
349 print STDERR "Expression $_ did not parse: numeric comparison with ",
350 "non-number\n";
351 $e = 1;
352 next;
353 }
9cf6b11a 354 push(@c, { var => lc($v), cmp => "(\$var $o $n)" });
059ec3d9
PH
355 } elsif (/^(.*?)\s+(=~|!~)\s+(.*)$/) {
356 #print STDERR "found as string regexp\n";
9cf6b11a 357 push(@c, { var => lc($1), cmp => "(\"\$var\" $2 $3)" });
059ec3d9
PH
358 } elsif (/^(.*?)\s+=\s+(.*)$/) {
359 #print STDERR "found as bare string regexp\n";
af66f652 360 my $case = $G::caseful ? '' : 'i';
9cf6b11a 361 push(@c, { var => lc($1), cmp => "(\"\$var\" =~ /$2/$case)" });
e22ca4ac
JJ
362 # quote special characters in perl text string
363 #foreach my $t ('@') { $c[-1]{cmp} =~ s/$t/\\$t/g; }
059ec3d9
PH
364 } elsif (/^(.*?)\s+(eq|ne)\s+(.*)$/) {
365 #print STDERR "found as string cmp\n";
af66f652 366 my $var = lc($1); my $op = $2; my $val = $3;
5f970846 367 $val =~ s|^(['"])(.*)\1$|$2|;
9cf6b11a 368 push(@c, { var => $var, cmp => "(\"\$var\" $op \"$val\")" });
ee744174 369 if (($var eq 'message_id' || $var eq 'message_exim_id') && $op eq "eq") {
af66f652
PH
370 #print STDERR "short circuit @c[-1]->{cmp} $val\n";
371 $G::msg_ids->{$val} = 1;
372 }
e22ca4ac 373 #foreach my $t ('@') { $c[-1]{cmp} =~ s/$t/\\$t/g; }
9cf6b11a 374 } elsif (/^(\S+)$/) {
059ec3d9 375 #print STDERR "found as boolean\n";
9cf6b11a 376 push(@c, { var => lc($1), cmp => "(\$var)" });
059ec3d9
PH
377 } else {
378 print STDERR "Expression $_ did not parse\n";
379 $e = 1;
0ea2a468 380 next;
059ec3d9 381 }
9cf6b11a 382 # assign the results of the cmp test here (handle "!" negation)
e22ca4ac 383 # also handle global --not negation
9cf6b11a 384 if ($c[-1]{var} =~ s|^!||) {
e22ca4ac 385 $c[-1]{cmp} .= $G::negate ? " ? 1 : 0" : " ? 0 : 1";
9cf6b11a 386 } else {
e22ca4ac 387 $c[-1]{cmp} .= $G::negate ? " ? 0 : 1" : " ? 1 : 0";
9cf6b11a 388 }
bf759a8b
PH
389 # support the each_* psuedo variables. Steal the criteria off of the
390 # queue for special processing later
391 if ($c[-1]{var} =~ /^each_(recipients(_(un)?del)?)$/) {
392 my $var = $1;
393 push(@G::recipients_crit,pop(@c));
394 $G::recipients_crit[-1]{var} = $var; # remove each_ from the variable
395 }
059ec3d9
PH
396 }
397
398 exit(1) if ($e);
399
400 if ($G::show_rules) { foreach (@c) { print "$_->{var}\t$_->{cmp}\n"; } }
401
402 return(\@c);
403}
404
405sub get_all_msgs {
edae0343
JJ
406 my $d = shift();
407 my $i = shift();
e22ca4ac
JJ
408 my $u = shift; # don't sort
409 my $r = shift; # right before returning, reverse order
410 my $o = shift; # if true, randomize list order before returning
059ec3d9
PH
411 my @m = ();
412
edae0343
JJ
413 if ($i =~ m|^/|) { $d = $i; } else { $d = $d . '/' . $i; }
414
059ec3d9
PH
415 opendir(D, "$d") || die "Couldn't opendir $d: $!\n";
416 foreach my $e (grep !/^\./, readdir(D)) {
417 if ($e =~ /^[a-zA-Z0-9]$/) {
418 opendir(DD, "$d/$e") || next;
419 foreach my $f (grep !/^\./, readdir(DD)) {
9cf6b11a 420 push(@m, { message => $1, path => "$d/$e" }) if ($f =~ /^(.{16})-H$/);
059ec3d9
PH
421 }
422 closedir(DD);
423 } elsif ($e =~ /^(.{16})-H$/) {
9cf6b11a 424 push(@m, { message => $1, path => $d });
059ec3d9
PH
425 }
426 }
427 closedir(D);
428
e22ca4ac
JJ
429 if ($o) {
430 my $c = scalar(@m);
431 # loop twice to pretend we're doing a good job of mixing things up
432 for (my $i = 0; $i < 2 * $c; $i++) {
433 my $rand = int(rand($c));
434 ($m[$i % $c],$m[$rand]) = ($m[$rand],$m[$i % $c]);
435 }
436 } elsif (!$u) {
437 @m = sort { $a->{message} cmp $b->{message} } @m;
438 }
439 @m = reverse(@m) if ($r);
440
441 return(\@m);
059ec3d9
PH
442}
443
444BEGIN {
445
446package Exim::SpoolFile;
447
b3f69ca8
JJ
448# versions 4.61 and higher will not need these variables anymore, but they
449# are left for handling legacy installs
450$Exim::SpoolFile::ACL_C_MAX_LEGACY = 10;
451#$Exim::SpoolFile::ACL_M_MAX _LEGACY= 10;
059ec3d9
PH
452
453sub new {
454 my $class = shift;
455 my $self = {};
456 bless($self, $class);
457
458 $self->{_spool_dir} = '';
edae0343 459 $self->{_input_path} = '';
059ec3d9
PH
460 $self->{_undelivered_only} = 0;
461 $self->{_show_generated} = 0;
462 $self->{_output_long} = 1;
463 $self->{_output_idonly} = 0;
464 $self->{_output_brief} = 0;
465 $self->{_output_flatq} = 0;
0ea2a468 466 $self->{_output_vars_only} = 0;
5f970846 467 $self->{_show_vars} = [];
059ec3d9
PH
468
469 $self->_reset();
470 return($self);
471}
472
473sub output_long {
474 my $self = shift;
475
476 $self->{_output_long} = 1;
477 $self->{_output_idonly} = 0;
478 $self->{_output_brief} = 0;
479 $self->{_output_flatq} = 0;
0ea2a468 480 $self->{_output_vars_only} = 0;
059ec3d9
PH
481}
482
483sub output_idonly {
484 my $self = shift;
485
486 $self->{_output_long} = 0;
487 $self->{_output_idonly} = 1;
488 $self->{_output_brief} = 0;
489 $self->{_output_flatq} = 0;
0ea2a468 490 $self->{_output_vars_only} = 0;
059ec3d9
PH
491}
492
493sub output_brief {
494 my $self = shift;
495
496 $self->{_output_long} = 0;
497 $self->{_output_idonly} = 0;
498 $self->{_output_brief} = 1;
499 $self->{_output_flatq} = 0;
0ea2a468 500 $self->{_output_vars_only} = 0;
059ec3d9
PH
501}
502
503sub output_flatq {
504 my $self = shift;
505
506 $self->{_output_long} = 0;
507 $self->{_output_idonly} = 0;
508 $self->{_output_brief} = 0;
509 $self->{_output_flatq} = 1;
0ea2a468
JJ
510 $self->{_output_vars_only} = 0;
511}
512
513sub output_vars_only {
514 my $self = shift;
515
516 $self->{_output_long} = 0;
517 $self->{_output_idonly} = 0;
518 $self->{_output_brief} = 0;
519 $self->{_output_flatq} = 0;
520 $self->{_output_vars_only} = 1;
059ec3d9
PH
521}
522
523sub set_show_vars {
524 my $self = shift;
525 my $s = shift;
526
527 foreach my $v (split(/\s*,\s*/, $s)) {
5f970846 528 push(@{$self->{_show_vars}}, $v);
059ec3d9
PH
529 }
530}
531
532sub set_show_generated {
533 my $self = shift;
534 $self->{_show_generated} = shift;
535}
536
537sub set_undelivered_only {
538 my $self = shift;
539 $self->{_undelivered_only} = shift;
540}
541
542sub error {
543 my $self = shift;
544 return $self->{_error};
545}
546
547sub _error {
548 my $self = shift;
549 $self->{_error} = shift;
550 return(undef);
551}
552
553sub _reset {
554 my $self = shift;
555
556 $self->{_error} = '';
557 $self->{_delivered} = 0;
558 $self->{_message} = '';
559 $self->{_path} = '';
560 $self->{_vars} = {};
0ea2a468 561 $self->{_vars_raw} = {};
059ec3d9
PH
562
563 $self->{_numrecips} = 0;
564 $self->{_udel_tree} = {};
565 $self->{_del_tree} = {};
566 $self->{_recips} = {};
567
568 return($self);
569}
570
571sub parse_message {
572 my $self = shift;
8e669ac1 573
059ec3d9
PH
574 $self->_reset();
575 $self->{_message} = shift || return(0);
9cf6b11a 576 $self->{_path} = shift; # optional path to message
edae0343 577 return(0) if (!$self->{_input_path});
9cf6b11a 578 if (!$self->{_path} && !$self->_find_path()) {
059ec3d9
PH
579 # assume the message was delivered from under us and ignore
580 $self->{_delivered} = 1;
581 return(1);
582 }
583 $self->_parse_header() || return(0);
584
585 return(1);
586}
587
9cf6b11a
JJ
588# take the output of get_state() and set up a message internally like
589# parse_message (except from a saved data struct, not by parsing the
590# files on disk).
591sub restore_state {
592 my $self = shift;
593 my $h = shift;
594
595 return(1) if ($h->{_delivered});
596 $self->_reset();
597 $self->{_message} = $h->{_message} || return(0);
edae0343 598 return(0) if (!$self->{_input_path});
9cf6b11a
JJ
599
600 $self->{_path} = $h->{_path};
601 $self->{_vars} = $h->{_vars};
602 $self->{_numrecips} = $h->{_numrecips};
603 $self->{_udel_tree} = $h->{_udel_tree};
604 $self->{_del_tree} = $h->{_del_tree};
605 $self->{_recips} = $h->{_recips};
606
607 $self->{_vars}{message_age} = time() - $self->{_vars}{received_time};
608 return(1);
609}
610
611# This returns the state data for a specific message in a format that can
612# be later frozen back in to regain state
613#
614# after calling this function, this specific state is not expect to be
615# reused. That's because we're returning direct references to specific
616# internal structures. We're also modifying the structure ourselves
617# by deleting certain internal message variables.
618sub get_state {
619 my $self = shift;
620 my $h = {}; # this is the hash ref we'll be returning.
621
622 $h->{_delivered} = $self->{_delivered};
623 $h->{_message} = $self->{_message};
624 $h->{_path} = $self->{_path};
625 $h->{_vars} = $self->{_vars};
626 $h->{_numrecips} = $self->{_numrecips};
627 $h->{_udel_tree} = $self->{_udel_tree};
628 $h->{_del_tree} = $self->{_del_tree};
629 $h->{_recips} = $self->{_recips};
630
631 # delete some internal variables that we will rebuild later if needed
632 delete($h->{_vars}{message_body});
633 delete($h->{_vars}{message_age});
634
635 return($h);
636}
637
638# keep this sub as a feature if we ever break this module out, but do away
639# with its use in exipick (pass it in from caller instead)
059ec3d9
PH
640sub _find_path {
641 my $self = shift;
642
643 return(0) if (!$self->{_message});
edae0343 644 return(0) if (!$self->{_input_path});
059ec3d9 645
9cf6b11a
JJ
646 # test split spool first on the theory that people concerned about
647 # performance will have split spool set =).
648 foreach my $f (substr($self->{_message}, 5, 1).'/', '') {
edae0343
JJ
649 if (-f "$self->{_input_path}/$f$self->{_message}-H") {
650 $self->{_path} = "$self->{_input_path}}/$f";
059ec3d9
PH
651 return(1);
652 }
653 }
654 return(0);
655}
656
657sub set_spool {
658 my $self = shift;
659 $self->{_spool_dir} = shift;
edae0343
JJ
660 $self->{_input_path} = shift;
661 if ($self->{_input_path} !~ m|^/|) {
662 $self->{_input_path} = $self->{_spool_dir} . '/' . $self->{_input_path};
663 }
059ec3d9
PH
664}
665
a2405d83
JJ
666sub get_matching_vars {
667 my $self = shift;
668 my $e = shift;
669
670 if ($e =~ /^\^/) {
671 my @r = ();
672 foreach my $v (keys %{$self->{_vars}}) { push(@r, $v) if ($v =~ /$e/); }
673 return(@r);
674 } else {
675 return($e);
676 }
677}
678
059ec3d9
PH
679# accepts a variable with or without leading '$' or trailing ':'
680sub get_var {
681 my $self = shift;
0ea2a468
JJ
682 my $var = lc(shift); $var =~ s/^\$//; $var =~ s/:$//;
683
684 if ($var eq 'message_body' && !defined($self->{_vars}{message_body})) {
685 $self->_parse_body()
686 } elsif ($var =~ s|^([rb]?h)(eader)?_|${1}eader_| &&
687 exists($self->{_vars}{$var}) && !defined($self->{_vars}{$var}))
688 {
689 if ((my $type = $1) eq 'rh') {
690 $self->{_vars}{$var} = join('', @{$self->{_vars_raw}{$var}{vals}});
691 } else {
692 # both bh_ and h_ build their strings from rh_. Do common work here
693 my $rh = $var; $rh =~ s|^b?|r|;
694 my $comma = 1 if ($self->{_vars_raw}{$rh}{type} =~ /^[BCFRST]$/);
695 foreach (@{$self->{_vars_raw}{$rh}{vals}}) {
696 my $x = $_; # editing $_ here would change the original, which is bad
697 $x =~ s|^\s+||;
698 $x =~ s|\s+$||;
699 if ($comma) { chomp($x); $self->{_vars}{$var} .= "$x,\n"; }
700 else { $self->{_vars}{$var} .= $x; }
701 }
702 $self->{_vars}{$var} =~ s|[\s\n]*$||;
703 $self->{_vars}{$var} =~ s|,$|| if ($comma);
704 # ok, that's the preprocessing, not do specific processing for h type
705 if ($type eq 'bh') {
706 $self->{_vars}{$var} = $self->_decode_2047($self->{_vars}{$var});
707 } else {
708 $self->{_vars}{$var} =
709 $self->_decode_2047($self->{_vars}{$var}, $charset);
710 }
711 }
712 }
713 elsif ($var eq 'received_count' && !defined($self->{_vars}{received_count}))
714 {
715 $self->{_vars}{received_count} =
716 scalar(@{$self->{_vars_raw}{rheader_received}{vals}});
717 }
718 elsif ($var eq 'message_headers' && !defined($self->{_vars}{message_headers}))
719 {
720 $self->{_vars}{$var} =
721 $self->_decode_2047($self->{_vars}{message_headers_raw}, $charset);
722 chomp($self->{_vars}{$var});
723 }
724 elsif ($var eq 'reply_address' && !defined($self->{_vars}{reply_address}))
725 {
726 $self->{_vars}{reply_address} = exists($self->{_vars}{"header_reply-to"})
727 ? $self->get_var("header_reply-to") : $self->get_var("header_from");
728 }
059ec3d9 729
0ea2a468
JJ
730 #chomp($self->{_vars}{$var}); # I think this was only for headers, obsolete
731 return $self->{_vars}{$var};
732}
733
734sub _decode_2047 {
735 my $self = shift;
736 my $s = shift; # string to decode
737 my $c = shift; # target charset. If empty, just decode, don't convert
738 my $t = ''; # the translated string
739 my $e = 0; # set to true if we get an error in here anywhere
740
741 return($s) if ($s !~ /=\?/); # don't even bother to look if there's no sign
742
743 my @p = ();
744 foreach my $mw (split(/(=\?[^\?]{3,}\?[BQ]\?[^\?]{1,74}\?=)/i, $s)) {
745 next if ($mw eq '');
746 if ($mw =~ /=\?([^\?]{3,})\?([BQ])\?([^\?]{1,74})\?=/i) {
747 push(@p, { data => $3, encoding => uc($2), charset => uc($1),
748 is_mime => 1 });
749 if ($p[-1]{encoding} eq 'Q') {
750 my @ow = split('', $p[-1]{data});
751 my @nw = ();
752 for (my $i = 0; $i < @ow; $i++) {
753 if ($ow[$i] eq '_') { push(@nw, ' '); }
754 elsif ($ow[$i] eq '=') {
755 if (scalar(@ow) - ($i+1) < 2) { # ran out of characters
756 $e = 1; last;
757 } elsif ($ow[$i+1] !~ /[\dA-F]/i || $ow[$i+2] !~ /[\dA-F]/i) {
758 $e = 1; last;
759 } else {
760 #push(@nw, chr('0x'.$ow[$i+1].$ow[$i+2]));
761 push(@nw, pack("C", hex($ow[$i+1].$ow[$i+2])));
762 $i += 2;
763 }
764 }
4c04137d 765 elsif ($ow[$i] =~ /\s/) { # whitespace is illegal
0ea2a468
JJ
766 $e = 1;
767 last;
768 }
769 else { push(@nw, $ow[$i]); }
770 }
771 $p[-1]{data} = join('', @nw);
772 } elsif ($p[-1]{encoding} eq 'B') {
773 my $x = $p[-1]{data};
774 $x =~ tr#A-Za-z0-9+/##cd;
775 $x =~ s|=+$||;
776 $x =~ tr#A-Za-z0-9+/# -_#;
777 my $r = '';
778 while ($x =~ s/(.{1,60})//s) {
779 $r .= unpack("u", chr(32 + int(length($1)*3/4)) . $1);
780 }
781 $p[-1]{data} = $r;
782 }
783 } else {
784 push(@p, { data => $mw, is_mime => 0,
785 is_ws => ($mw =~ m|^[\s\n]+|sm) ? 1 : 0 });
786 }
787 }
059ec3d9 788
0ea2a468
JJ
789 for (my $i = 0; $i < @p; $i++) {
790 # mark entities we want to skip (whitespace between consecutive mimewords)
791 if ($p[$i]{is_mime} && $p[$i+1]{is_ws} && $p[$i+2]{is_mime}) {
792 $p[$i+1]{skip} = 1;
793 }
059ec3d9 794
0ea2a468
JJ
795 # if word is a mimeword and we have access to Encode and charset was
796 # specified, try to convert text
797 # XXX _cannot_ get consistent conversion results in perl, can't get them
798 # to return same conversions that exim performs. Until I can figure this
799 # out, don't attempt any conversions (header_ will return same value as
800 # bheader_).
801 #if ($c && $p[$i]{is_mime} && $self->_try_load('Encode')) {
802 # # XXX not sure how to catch errors here
803 # Encode::from_to($p[$i]{data}, $p[$i]{charset}, $c);
804 #}
805
806 # replace binary zeros w/ '?' in decoded text
807 if ($p[$i]{is_mime}) { $p[$i]{data} =~ s|\x00|?|g; }
808 }
809
810 if ($e) {
811 return($s);
812 } else {
813 return(join('', map { $_->{data} } grep { !$_->{skip} } @p));
814 }
815}
816
817# This isn't a class func but I'm tired
818sub _try_load {
819 my $self = shift;
820 my $mod = shift;
821
822 eval("use $mod");
823 return $@ ? 0 : 1;
059ec3d9
PH
824}
825
826sub _parse_body {
827 my $self = shift;
828 my $f = $self->{_path} . '/' . $self->{_message} . '-D';
0ea2a468 829 $self->{_vars}{message_body} = ""; # define var so we only come here once
059ec3d9
PH
830
831 open(I, "<$f") || return($self->_error("Couldn't open $f: $!"));
832 chomp($_ = <I>);
833 return(0) if ($self->{_message}.'-D' ne $_);
834
835 $self->{_vars}{message_body} = join('', <I>);
836 close(I);
837 $self->{_vars}{message_body} =~ s/\n/ /g;
838 $self->{_vars}{message_body} =~ s/\000/ /g;
839 return(1);
840}
841
842sub _parse_header {
843 my $self = shift;
844 my $f = $self->{_path} . '/' . $self->{_message} . '-H';
465e92cf
JJ
845 $self->{_vars}{header_path} = $f;
846 $self->{_vars}{data_path} = $self->{_path} . '/' . $self->{_message} . '-D';
059ec3d9 847
9cf6b11a
JJ
848 if (!open(I, "<$f")) {
849 # assume message went away and silently ignore
850 $self->{_delivered} = 1;
851 return(1);
852 }
853
0ea2a468
JJ
854 # There are a few numeric variables that should explicitly be set to
855 # zero if they aren't found in the header. Technically an empty value
856 # works just as well, but might as well be pedantic
857 $self->{_vars}{body_zerocount} = 0;
858 $self->{_vars}{host_lookup_deferred} = 0;
859 $self->{_vars}{host_lookup_failed} = 0;
860 $self->{_vars}{tls_certificate_verified} = 0;
861
059ec3d9
PH
862 chomp($_ = <I>);
863 return(0) if ($self->{_message}.'-H' ne $_);
864 $self->{_vars}{message_id} = $self->{_message};
ee744174 865 $self->{_vars}{message_exim_id} = $self->{_message};
059ec3d9
PH
866
867 # line 2
868 chomp($_ = <I>);
5f970846 869 return(0) if (!/^(.+)\s(\-?\d+)\s(\-?\d+)$/);
059ec3d9
PH
870 $self->{_vars}{originator_login} = $1;
871 $self->{_vars}{originator_uid} = $2;
872 $self->{_vars}{originator_gid} = $3;
873
874 # line 3
875 chomp($_ = <I>);
876 return(0) if (!/^<(.*)>$/);
877 $self->{_vars}{sender_address} = $1;
878 $self->{_vars}{sender_address_domain} = $1;
879 $self->{_vars}{sender_address_local_part} = $1;
880 $self->{_vars}{sender_address_domain} =~ s/^.*\@//;
881 $self->{_vars}{sender_address_local_part} =~ s/^(.*)\@.*$/$1/;
882
883 # line 4
884 chomp($_ = <I>);
885 return(0) if (!/^(\d+)\s(\d+)$/);
886 $self->{_vars}{received_time} = $1;
887 $self->{_vars}{warning_count} = $2;
888 $self->{_vars}{message_age} = time() - $self->{_vars}{received_time};
889
890 while (<I>) {
891 chomp();
892 if (/^(-\S+)\s*(.*$)/) {
893 my $tag = $1;
894 my $arg = $2;
895 if ($tag eq '-acl') {
896 my $t;
897 return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
b3f69ca8 898 if ($1 < $Exim::SpoolFile::ACL_C_MAX_LEGACY) {
059ec3d9
PH
899 $t = "acl_c$1";
900 } else {
b3f69ca8 901 $t = "acl_m" . ($1 - $Exim::SpoolFile::ACL_C_MAX_LEGACY);
059ec3d9
PH
902 }
903 read(I, $self->{_vars}{$t}, $2+1) || return(0);
904 chomp($self->{_vars}{$t});
b3f69ca8 905 } elsif ($tag eq '-aclc') {
a2405d83
JJ
906 #return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
907 return(0) if ($arg !~ /^(\S+)\s(\d+)$/);
b3f69ca8
JJ
908 my $t = "acl_c$1";
909 read(I, $self->{_vars}{$t}, $2+1) || return(0);
910 chomp($self->{_vars}{$t});
911 } elsif ($tag eq '-aclm') {
a2405d83
JJ
912 #return(0) if ($arg !~ /^(\d+)\s(\d+)$/);
913 return(0) if ($arg !~ /^(\S+)\s(\d+)$/);
b3f69ca8
JJ
914 my $t = "acl_m$1";
915 read(I, $self->{_vars}{$t}, $2+1) || return(0);
916 chomp($self->{_vars}{$t});
059ec3d9
PH
917 } elsif ($tag eq '-local') {
918 $self->{_vars}{sender_local} = 1;
919 } elsif ($tag eq '-localerror') {
920 $self->{_vars}{local_error_message} = 1;
921 } elsif ($tag eq '-local_scan') {
922 $self->{_vars}{local_scan_data} = $arg;
bf759a8b
PH
923 } elsif ($tag eq '-spam_score_int') {
924 $self->{_vars}{spam_score_int} = $arg;
925 $self->{_vars}{spam_score} = $arg / 10;
926 } elsif ($tag eq '-bmi_verdicts') {
927 $self->{_vars}{bmi_verdicts} = $arg;
928 } elsif ($tag eq '-host_lookup_deferred') {
929 $self->{_vars}{host_lookup_deferred} = 1;
059ec3d9
PH
930 } elsif ($tag eq '-host_lookup_failed') {
931 $self->{_vars}{host_lookup_failed} = 1;
932 } elsif ($tag eq '-body_linecount') {
933 $self->{_vars}{body_linecount} = $arg;
465e92cf
JJ
934 } elsif ($tag eq '-max_received_linelength') {
935 $self->{_vars}{max_received_linelength} = $arg;
bf759a8b
PH
936 } elsif ($tag eq '-body_zerocount') {
937 $self->{_vars}{body_zerocount} = $arg;
059ec3d9
PH
938 } elsif ($tag eq '-frozen') {
939 $self->{_vars}{deliver_freeze} = 1;
940 $self->{_vars}{deliver_frozen_at} = $arg;
bf759a8b
PH
941 } elsif ($tag eq '-allow_unqualified_recipient') {
942 $self->{_vars}{allow_unqualified_recipient} = 1;
943 } elsif ($tag eq '-allow_unqualified_sender') {
944 $self->{_vars}{allow_unqualified_sender} = 1;
059ec3d9
PH
945 } elsif ($tag eq '-deliver_firsttime') {
946 $self->{_vars}{deliver_firsttime} = 1;
947 $self->{_vars}{first_delivery} = 1;
948 } elsif ($tag eq '-manual_thaw') {
949 $self->{_vars}{deliver_manual_thaw} = 1;
950 $self->{_vars}{manually_thawed} = 1;
951 } elsif ($tag eq '-auth_id') {
952 $self->{_vars}{authenticated_id} = $arg;
953 } elsif ($tag eq '-auth_sender') {
954 $self->{_vars}{authenticated_sender} = $arg;
955 } elsif ($tag eq '-sender_set_untrusted') {
956 $self->{_vars}{sender_set_untrusted} = 1;
957 } elsif ($tag eq '-tls_certificate_verified') {
958 $self->{_vars}{tls_certificate_verified} = 1;
959 } elsif ($tag eq '-tls_cipher') {
960 $self->{_vars}{tls_cipher} = $arg;
961 } elsif ($tag eq '-tls_peerdn') {
962 $self->{_vars}{tls_peerdn} = $arg;
3f0945ff
PP
963 } elsif ($tag eq '-tls_sni') {
964 $self->{_vars}{tls_sni} = $arg;
059ec3d9
PH
965 } elsif ($tag eq '-host_address') {
966 $self->{_vars}{sender_host_port} = $self->_get_host_and_port(\$arg);
967 $self->{_vars}{sender_host_address} = $arg;
968 } elsif ($tag eq '-interface_address') {
0ea2a468
JJ
969 $self->{_vars}{received_port} =
970 $self->{_vars}{interface_port} = $self->_get_host_and_port(\$arg);
971 $self->{_vars}{received_ip_address} =
972 $self->{_vars}{interface_address} = $arg;
bf759a8b
PH
973 } elsif ($tag eq '-active_hostname') {
974 $self->{_vars}{smtp_active_hostname} = $arg;
059ec3d9
PH
975 } elsif ($tag eq '-host_auth') {
976 $self->{_vars}{sender_host_authenticated} = $arg;
977 } elsif ($tag eq '-host_name') {
978 $self->{_vars}{sender_host_name} = $arg;
979 } elsif ($tag eq '-helo_name') {
980 $self->{_vars}{sender_helo_name} = $arg;
981 } elsif ($tag eq '-ident') {
982 $self->{_vars}{sender_ident} = $arg;
983 } elsif ($tag eq '-received_protocol') {
984 $self->{_vars}{received_protocol} = $arg;
985 } elsif ($tag eq '-N') {
986 $self->{_vars}{dont_deliver} = 1;
059ec3d9
PH
987 } else {
988 # unrecognized tag, save it for reference
989 $self->{$tag} = $arg;
990 }
991 } else {
992 last;
993 }
994 }
995
8e669ac1 996 # when we drop out of the while loop, we have the first line of the
059ec3d9
PH
997 # delivered tree in $_
998 do {
999 if ($_ eq 'XX') {
1000 ; # noop
1001 } elsif ($_ =~ s/^[YN][YN]\s+//) {
1002 $self->{_del_tree}{$_} = 1;
1003 } else {
1004 return(0);
1005 }
1006 chomp($_ = <I>);
1007 } while ($_ !~ /^\d+$/);
1008
1009 $self->{_numrecips} = $_;
1010 $self->{_vars}{recipients_count} = $self->{_numrecips};
1011 for (my $i = 0; $i < $self->{_numrecips}; $i++) {
1012 chomp($_ = <I>);
1013 return(0) if (/^$/);
1014 my $addr = '';
1015 if (/^(.*)\s\d+,(\d+),\d+$/) {
1016 #print STDERR "exim3 type (untested): $_\n";
1017 $self->{_recips}{$1} = { pno => $2 };
1018 $addr = $1;
1019 } elsif (/^(.*)\s(\d+)$/) {
1020 #print STDERR "exim4 original type (untested): $_\n";
1021 $self->{_recips}{$1} = { pno => $2 };
1022 $addr = $1;
1023 } elsif (/^(.*)\s(.*)\s(\d+),(\d+)#1$/) {
1024 #print STDERR "exim4 new type #1 (untested): $_\n";
1025 return($self->_error("incorrect format: $_")) if (length($2) != $3);
1026 $self->{_recips}{$1} = { pno => $4, errors_to => $2 };
1027 $addr = $1;
bad059db
WB
1028 } elsif (/^(\S*)\s(\S*)\s(\d+),(\d+)\s(\S*)\s(\d+),(-?\d+)#3$/) {
1029 #print STDERR "exim4 new type #3 DSN (untested): $_\n";
1030 return($self->_error("incorrect format: $_"))
1031 if ((length($2) != $3) || (length($5) != $6));
1032 $self->{_recips}{$1} = { pno => $7, errors_to => $5 };
1033 $addr = $1;
059ec3d9 1034 } elsif (/^.*#(\d+)$/) {
bf759a8b 1035 #print STDERR "exim4 #$1 style (unimplemented): $_\n";
059ec3d9
PH
1036 $self->_error("exim4 #$1 style (unimplemented): $_");
1037 } else {
1038 #print STDERR "default type: $_\n";
1039 $self->{_recips}{$_} = {};
1040 $addr = $_;
1041 }
1042 $self->{_udel_tree}{$addr} = 1 if (!$self->{_del_tree}{$addr});
1043 }
af66f652
PH
1044 $self->{_vars}{recipients} = join(', ', keys(%{$self->{_recips}}));
1045 $self->{_vars}{recipients_del} = join(', ', keys(%{$self->{_del_tree}}));
1046 $self->{_vars}{recipients_undel} = join(', ', keys(%{$self->{_udel_tree}}));
1047 $self->{_vars}{recipients_undel_count} = scalar(keys(%{$self->{_udel_tree}}));
1048 $self->{_vars}{recipients_del_count} = 0;
1049 foreach my $r (keys %{$self->{_del_tree}}) {
1050 next if (!$self->{_recips}{$r});
1051 $self->{_vars}{recipients_del_count}++;
1052 }
059ec3d9
PH
1053
1054 # blank line
1055 $_ = <I>;
1056 return(0) if (!/^$/);
1057
1058 # start reading headers
1059 while (read(I, $_, 3) == 3) {
1060 my $t = getc(I);
1061 return(0) if (!length($t));
1062 while ($t =~ /^\d$/) {
1063 $_ .= $t;
1064 $t = getc(I);
1065 }
0ea2a468
JJ
1066 my $hdr_flag = $t;
1067 my $hdr_bytes = $_;
1068 $t = getc(I); # strip the space out of the file
1069 return(0) if (read(I, $_, $hdr_bytes) != $hdr_bytes);
1070 if ($hdr_flag ne '*') {
1071 $self->{_vars}{message_linecount} += (tr/\n//);
1072 $self->{_vars}{message_size} += $hdr_bytes;
1073 }
1074
1075 # mark (rb)?header_ vars as existing and store raw value. They'll be
1076 # processed further in get_var() if needed
9cf6b11a
JJ
1077 my($v,$d) = split(/:/, $_, 2);
1078 $v = "header_" . lc($v);
0ea2a468
JJ
1079 $self->{_vars}{$v} = $self->{_vars}{"b$v"} = $self->{_vars}{"r$v"} = undef;
1080 push(@{$self->{_vars_raw}{"r$v"}{vals}}, $d);
1081 $self->{_vars_raw}{"r$v"}{type} = $hdr_flag;
1082 $self->{_vars}{message_headers_raw} .= $_;
059ec3d9
PH
1083 }
1084 close(I);
059ec3d9
PH
1085
1086 $self->{_vars}{message_body_size} =
1087 (stat($self->{_path}.'/'.$self->{_message}.'-D'))[7] - 19;
1088 if ($self->{_vars}{message_body_size} < 0) {
1089 $self->{_vars}{message_size} = 0;
0ea2a468 1090 $self->{_vars}{message_body_missing} = 1;
059ec3d9
PH
1091 } else {
1092 $self->{_vars}{message_size} += $self->{_vars}{message_body_size} + 1;
1093 }
1094
5f970846
PH
1095 $self->{_vars}{message_linecount} += $self->{_vars}{body_linecount};
1096
1097 my $i = $self->{_vars}{message_size};
9cf6b11a
JJ
1098 if ($i == 0) { $i = ""; }
1099 elsif ($i < 1024) { $i = sprintf("%d", $i); }
1100 elsif ($i < 10240) { $i = sprintf("%.1fK", $i / 1024); }
1101 elsif ($i < 1048576) { $i = sprintf("%dK", ($i+512)/1024); }
1102 elsif ($i < 10485760) { $i = sprintf("%.1fM", $i/1048576); }
1103 else { $i = sprintf("%dM", ($i + 524288)/1048576); }
5f970846
PH
1104 $self->{_vars}{shown_message_size} = $i;
1105
059ec3d9 1106 return(1);
8e669ac1 1107}
059ec3d9
PH
1108
1109# mimic exim's host_extract_port function - receive a ref to a scalar,
1110# strip it of port, return port
1111sub _get_host_and_port {
1112 my $self = shift;
1113 my $host = shift; # scalar ref, be careful
1114
1115 if ($$host =~ /^\[([^\]]+)\](?:\:(\d+))?$/) {
1116 $$host = $1;
1117 return($2 || 0);
1118 } elsif ($$host =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\.(\d+))?$/) {
1119 $$host = $1;
1120 return($2 || 0);
1121 } elsif ($$host =~ /^([\d\:]+)(?:\.(\d+))?$/) {
1122 $$host = $1;
1123 return($2 || 0);
1124 }
1125 # implicit else
1126 return(0);
1127}
1128
e22ca4ac
JJ
1129# honoring all formatting preferences, return a scalar variable of the
1130# information for the single message matching what exim -bp would show.
1131# We can print later if we want.
1132sub format_message {
059ec3d9 1133 my $self = shift;
e22ca4ac 1134 my $o = '';
059ec3d9
PH
1135 return if ($self->{_delivered});
1136
a2405d83
JJ
1137 # define any vars we want to print out for this message. The requests
1138 # can be regexps, and the defined vars can change for each message, so we
1139 # have to build this list for each message
1140 my @vars = ();
1141 if (@{$self->{_show_vars}}) {
1142 my %t = ();
1143 foreach my $e (@{$self->{_show_vars}}) {
1144 foreach my $v ($self->get_matching_vars($e)) {
1145 next if ($t{$v}); $t{$v}++; push(@vars, $v);
1146 }
1147 }
1148 }
1149
059ec3d9 1150 if ($self->{_output_idonly}) {
e22ca4ac 1151 $o .= $self->{_message};
0ea2a468 1152 foreach my $v (@vars) { $o .= " $v='" . $self->get_var($v) . "'"; }
e22ca4ac
JJ
1153 $o .= "\n";
1154 return $o;
0ea2a468
JJ
1155 } elsif ($self->{_output_vars_only}) {
1156 foreach my $v (@vars) { $o .= $self->get_var($v) . "\n"; }
1157 return $o;
059ec3d9 1158 }
8e669ac1 1159
059ec3d9
PH
1160 if ($self->{_output_long} || $self->{_output_flatq}) {
1161 my $i = int($self->{_vars}{message_age} / 60);
1162 if ($i > 90) {
1163 $i = int(($i+30)/60);
e22ca4ac
JJ
1164 if ($i > 72) { $o .= sprintf "%2dd ", int(($i+12)/24); }
1165 else { $o .= sprintf "%2dh ", $i; }
1166 } else { $o .= sprintf "%2dm ", $i; }
059ec3d9 1167
a2405d83
JJ
1168 if ($self->{_output_flatq} && @vars) {
1169 $o .= join(';', map { "$_='".$self->get_var($_)."'" } (@vars)
e22ca4ac 1170 );
5f970846 1171 } else {
e22ca4ac 1172 $o .= sprintf "%5s", $self->{_vars}{shown_message_size};
5f970846 1173 }
e22ca4ac 1174 $o .= " ";
059ec3d9 1175 }
e22ca4ac
JJ
1176 $o .= "$self->{_message} ";
1177 $o .= "From: " if ($self->{_output_brief});
1178 $o .= "<$self->{_vars}{sender_address}>";
059ec3d9
PH
1179
1180 if ($self->{_output_long}) {
e22ca4ac 1181 $o .= " ($self->{_vars}{originator_login})"
059ec3d9 1182 if ($self->{_vars}{sender_set_untrusted});
8e669ac1 1183
059ec3d9 1184 # XXX exim contains code here to print spool format errors
e22ca4ac
JJ
1185 $o .= " *** frozen ***" if ($self->{_vars}{deliver_freeze});
1186 $o .= "\n";
059ec3d9 1187
a2405d83 1188 foreach my $v (@vars) {
e22ca4ac 1189 $o .= sprintf " %25s = '%s'\n", $v, $self->get_var($v);
059ec3d9 1190 }
8e669ac1 1191
059ec3d9
PH
1192 foreach my $r (keys %{$self->{_recips}}) {
1193 next if ($self->{_del_tree}{$r} && $self->{_undelivered_only});
e22ca4ac 1194 $o .= sprintf " %s %s\n", $self->{_del_tree}{$r} ? "D" : " ", $r;
059ec3d9
PH
1195 }
1196 if ($self->{_show_generated}) {
1197 foreach my $r (keys %{$self->{_del_tree}}) {
1198 next if ($self->{_recips}{$r});
e22ca4ac 1199 $o .= sprintf " +D %s\n", $r;
059ec3d9
PH
1200 }
1201 }
1202 } elsif ($self->{_output_brief}) {
1203 my @r = ();
1204 foreach my $r (keys %{$self->{_recips}}) {
1205 next if ($self->{_del_tree}{$r});
1206 push(@r, $r);
1207 }
e22ca4ac 1208 $o .= " To: " . join(';', @r);
a2405d83
JJ
1209 if (scalar(@vars)) {
1210 $o .= " Vars: ".join(';',map { "$_='".$self->get_var($_)."'" } (@vars));
5f970846 1211 }
059ec3d9 1212 } elsif ($self->{_output_flatq}) {
e22ca4ac 1213 $o .= " *** frozen ***" if ($self->{_vars}{deliver_freeze});
059ec3d9
PH
1214 my @r = ();
1215 foreach my $r (keys %{$self->{_recips}}) {
1216 next if ($self->{_del_tree}{$r});
1217 push(@r, $r);
1218 }
e22ca4ac 1219 $o .= " " . join(' ', @r);
059ec3d9
PH
1220 }
1221
e22ca4ac
JJ
1222 $o .= "\n";
1223 return($o);
1224}
1225
1226sub print_message {
1227 my $self = shift;
1228 my $fh = shift || \*STDOUT;
1229 return if ($self->{_delivered});
1230
1231 print $fh $self->format_message();
059ec3d9
PH
1232}
1233
1234sub dump {
1235 my $self = shift;
1236
1237 foreach my $k (sort keys %$self) {
1238 my $r = ref($self->{$k});
1239 if ($r eq 'ARRAY') {
1240 printf "%20s <<EOM\n", $k;
1241 print @{$self->{$k}}, "EOM\n";
1242 } elsif ($r eq 'HASH') {
1243 printf "%20s <<EOM\n", $k;
1244 foreach (sort keys %{$self->{$k}}) {
1245 printf "%20s %s\n", $_, $self->{$k}{$_};
1246 }
1247 print "EOM\n";
1248 } else {
1249 printf "%20s %s\n", $k, $self->{$k};
1250 }
1251 }
1252}
1253
1254} # BEGIN
1255
1256sub ext_usage {
1257 if ($ARGV[0] =~ /^--help$/i) {
1258 require Config;
1259 $ENV{PATH} .= ":" unless $ENV{PATH} eq "";
1260 $ENV{PATH} = "$ENV{PATH}$Config::Config{'installscript'}";
1261 #exec("perldoc", "-F", "-U", $0) || exit 1;
1262 $< = $> = 1 if ($> == 0 || $< == 0);
1263 exec("perldoc", $0) || exit 1;
1264 # make parser happy
1265 %Config::Config = ();
1266 } elsif ($ARGV[0] =~ /^--version$/i) {
1267 print "$p_name version $p_version\n\n$p_cp\n";
1268 } else {
1269 return;
1270 }
1271
1272 exit(0);
1273}
1274
1275__END__
1276
1277=head1 NAME
1278
e22ca4ac 1279exipick - selectively display messages from an Exim queue
059ec3d9 1280
e22ca4ac 1281=head1 SYNOPSIS
059ec3d9 1282
e22ca4ac 1283exipick [<options>] [<criterion> [<criterion> ...]]
059ec3d9
PH
1284
1285=head1 DESCRIPTION
1286
e22ca4ac
JJ
1287exipick is a tool to display messages in an Exim queue. It is very similar to exiqgrep and is, in fact, a drop in replacement for exiqgrep. exipick allows you to select messages to be displayed using any piece of data stored in an Exim spool file. Matching messages can be displayed in a variety of formats.
1288
1289=head1 QUICK START
1290
1291Delete every frozen message from queue:
1292 exipick -zi | xargs exim -Mrm
1293
1294Show only messages which have not yet been virus scanned:
1295 exipick '$received_protocol ne virus-scanned'
1296
1297Run the queue in a semi-random order:
1298 exipick -i --random | xargs exim -M
1299
1300Show the count and total size of all messages which either originated from localhost or have a received protocol of 'local':
1301 exipick --or --size --bpc \
1302 '$sender_host_address eq 127.0.0.1' \
1303 '$received_protocol eq local'
1304
1305Display all messages received on the MSA port, ordered first by the sender's email domain and then by the size of the emails:
1306 exipick --sort sender_address_domain,message_size \
0ea2a468 1307 '$received_port == 587'
e22ca4ac
JJ
1308
1309Display only messages whose every recipient is in the example.com domain, also listing the IP address of the sending host:
1310 exipick --show-vars sender_host_address \
1311 '$each_recipients = example.com'
059ec3d9 1312
a2405d83
JJ
1313Same as above, but show values for all defined variables starting with sender_ and the number of recipients:
1314 exipick --show-vars ^sender_,recipients_count \
1315 '$each_recipients = example.com'
1316
059ec3d9
PH
1317=head1 OPTIONS
1318
1319=over 4
1320
e22ca4ac 1321=item --and
059ec3d9 1322
e22ca4ac 1323Display messages matching all criteria (default)
059ec3d9 1324
e22ca4ac 1325=item -b
059ec3d9 1326
e22ca4ac 1327Display messages in brief format (exiqgrep)
059ec3d9 1328
e22ca4ac 1329=item -bp
059ec3d9 1330
e22ca4ac 1331Display messages in standard mailq format (default)
059ec3d9 1332
e22ca4ac 1333=item -bpa
af66f652 1334
e22ca4ac 1335Same as -bp, show generated addresses also (exim)
af66f652 1336
e22ca4ac 1337=item -bpc
5f970846 1338
e22ca4ac 1339Show a count of matching messages (exim)
5f970846 1340
e22ca4ac 1341=item -bpr
5f970846 1342
e22ca4ac 1343Same as '-bp --unsorted' (exim)
5f970846 1344
e22ca4ac 1345=item -bpra
5f970846 1346
1a41defa 1347Same as '-bpa --unsorted' (exim)
5f970846 1348
e22ca4ac 1349=item -bpru
5f970846 1350
e22ca4ac 1351Same as '-bpu --unsorted' (exim)
5f970846 1352
e22ca4ac 1353=item -bpu
9cf6b11a 1354
e22ca4ac 1355Same as -bp, but only show undelivered messages (exim)
9cf6b11a 1356
e22ca4ac 1357=item -c
059ec3d9 1358
e22ca4ac 1359Show a count of matching messages (exiqgrep)
059ec3d9 1360
e22ca4ac 1361=item --caseful
059ec3d9 1362
e22ca4ac 1363Make operators involving '=' honor case
059ec3d9 1364
0ea2a468
JJ
1365=item --charset
1366
1367Override the default local character set for $header_ decoding
1368
e22ca4ac 1369=item -f <regexp>
059ec3d9 1370
465e92cf 1371Same as '$sender_address =~ /<regexp>/' (exiqgrep). Note that this preserves the default case sensitivity of exiqgrep's interface.
059ec3d9 1372
edae0343
JJ
1373=item --finput
1374
1375Same as '--input-dir Finput'. 'Finput' is where exim copies frozen messages when compiled with SUPPORT_MOVE_FROZEN_MESSAGES.
1376
e22ca4ac 1377=item --flatq
059ec3d9 1378
e22ca4ac 1379Use a single-line output format
059ec3d9 1380
e22ca4ac 1381=item --freeze <cache file>
059ec3d9 1382
e22ca4ac 1383Save queue information in an quickly retrievable format
059ec3d9 1384
e22ca4ac 1385=item --help
059ec3d9 1386
e22ca4ac 1387Display this output
059ec3d9 1388
e22ca4ac 1389=item -i
5f970846 1390
e22ca4ac 1391Display only the message IDs (exiqgrep)
059ec3d9 1392
edae0343
JJ
1393=item --input-dir <inputname>
1394
4c04137d 1395Set the name of the directory under the spool directory. By default this is "input". If this starts with '/', the value of --spool is ignored. See also --finput.
edae0343 1396
e22ca4ac 1397=item -l
059ec3d9 1398
e22ca4ac 1399Same as -bp (exiqgrep)
059ec3d9 1400
e22ca4ac 1401=item --not
059ec3d9 1402
e22ca4ac 1403Negate all tests.
059ec3d9 1404
e22ca4ac 1405=item -o <seconds>
059ec3d9 1406
e22ca4ac 1407Same as '$message_age > <seconds>' (exiqgrep)
059ec3d9 1408
e22ca4ac 1409=item --or
059ec3d9 1410
e22ca4ac 1411Display messages matching any criteria
059ec3d9 1412
e22ca4ac 1413=item -R
059ec3d9 1414
e22ca4ac 1415Same as --reverse (exiqgrep)
059ec3d9 1416
e22ca4ac 1417=item -r <regexp>
059ec3d9 1418
465e92cf 1419Same as '$recipients =~ /<regexp>/' (exiqgrep). Note that this preserves the default case sensitivity of exiqgrep's interface.
059ec3d9 1420
e22ca4ac 1421=item --random
9cf6b11a 1422
e22ca4ac 1423Display messages in random order
9cf6b11a 1424
e22ca4ac 1425=item --reverse
9cf6b11a 1426
e22ca4ac 1427Display messages in reverse order
9cf6b11a 1428
e22ca4ac 1429=item -s <string>
9cf6b11a 1430
e22ca4ac 1431Same as '$shown_message_size eq <string>' (exiqgrep)
9cf6b11a 1432
e22ca4ac 1433=item --spool <path>
059ec3d9 1434
57397119
HSHR
1435Set the path to the exim spool to use. This value will have the argument to --input or 'input' appended, or be ignored if --input is a full path. If not specified, exipick uses the value from C<exim -bP spool_directory>, and if this fails, the F<SPOOL_DIRECTORY>
1436from build time (F<Local/Makefile>) is used.
059ec3d9 1437
e22ca4ac 1438=item --show-rules
059ec3d9 1439
e22ca4ac 1440Show the internal representation of each criterion specified
059ec3d9 1441
e22ca4ac 1442=item --show-tests
059ec3d9 1443
e22ca4ac 1444Show the result of each criterion on each message
059ec3d9 1445
e22ca4ac 1446=item --show-vars <variable>[,<variable>...]
059ec3d9 1447
a2405d83 1448Show the value for <variable> for each displayed message. <variable> will be a regular expression if it begins with a circumflex.
059ec3d9 1449
e22ca4ac 1450=item --size
059ec3d9 1451
e22ca4ac 1452Show the total bytes used by each displayed message
059ec3d9 1453
e22ca4ac 1454=item --thaw <cache file>
059ec3d9 1455
e22ca4ac 1456Read queue information cached from a previous --freeze run
059ec3d9 1457
e22ca4ac 1458=item --sort <variable>[,<variable>...]
059ec3d9 1459
e22ca4ac 1460Display matching messages sorted according to <variable>
059ec3d9 1461
e22ca4ac 1462=item --unsorted
059ec3d9 1463
e22ca4ac 1464Do not apply any sorting to output
059ec3d9 1465
e22ca4ac 1466=item --version
059ec3d9 1467
e22ca4ac 1468Display the version of this command
059ec3d9 1469
e22ca4ac
JJ
1470=item -x
1471
1472Same as '!$deliver_freeze' (exiqgrep)
1473
1474=item -y
9cf6b11a 1475
e22ca4ac
JJ
1476Same as '$message_age < <seconds>' (exiqgrep)
1477
1478=item -z
1479
1480Same as '$deliver_freeze' (exiqgrep)
9cf6b11a 1481
059ec3d9
PH
1482=back
1483
e22ca4ac 1484=head1 CRITERIA
059ec3d9 1485
e22ca4ac 1486Exipick decides which messages to display by applying a test against each message. The rules take the general form of 'VARIABLE OPERATOR VALUE'. For example, '$message_age > 60'. When exipick is deciding which messages to display, it checks the $message_age variable for each message. If a message's age is greater than 60, the message will be displayed. If the message's age is 60 or less seconds, it will not be displayed.
059ec3d9 1487
e22ca4ac 1488Multiple criteria can be used. The order they are specified does not matter. By default all criteria must evaluate to true for a message to be displayed. If the --or option is used, a message is displayed as long as any of the criteria evaluate to true.
059ec3d9 1489
e22ca4ac 1490See the VARIABLES and OPERATORS sections below for more details
059ec3d9 1491
e22ca4ac 1492=head1 OPERATORS
059ec3d9 1493
e22ca4ac 1494=over 4
059ec3d9 1495
e22ca4ac 1496=item BOOLEAN
059ec3d9 1497
e22ca4ac
JJ
1498Boolean variables are checked simply by being true or false. There is no real operator except negation. Examples of valid boolean tests:
1499 '$deliver_freeze'
1500 '!$deliver_freeze'
059ec3d9 1501
e22ca4ac 1502=item NUMERIC
059ec3d9 1503
43236f35 1504Valid comparisons are <, <=, >, >=, ==, and !=. Numbers can be integers or floats. Any number in a test suffixed with d, h, m, s, M, K, or B will be multiplied by 86400, 3600, 60, 1, 1048576, 1024, or 1 respectively. Examples of valid numeric tests:
e22ca4ac
JJ
1505 '$message_age >= 3d'
1506 '$local_interface == 587'
1507 '$message_size < 30K'
059ec3d9 1508
e22ca4ac 1509=item STRING
059ec3d9 1510
e22ca4ac
JJ
1511The string operators are =, eq, ne, =~, and !~. With the exception of '=', the operators all match the functionality of the like-named perl operators. eq and ne match a string exactly. !~, =~, and = apply a perl regular expression to a string. The '=' operator behaves just like =~ but you are not required to place // around the regular expression. Examples of valid string tests:
1512 '$received_protocol eq esmtp'
1513 '$sender_address = example.com'
1514 '$each_recipients =~ /^a[a-z]{2,3}@example.com$/'
059ec3d9 1515
e22ca4ac 1516=item NEGATION
059ec3d9 1517
43236f35 1518There are many ways to negate tests, each having a reason for existing. Many tests can be negated using native operators. For instance, >1 is the opposite of <=1 and eq and ne are opposites. In addition, each individual test can be negated by adding a ! at the beginning of the test. For instance, '!$acl_m1 =~ /^DENY$/' is the same as '$acl_m1 !~ /^DENY$/'. Finally, every test can be specified by using the command line argument --not. This is functionally equivalent to adding a ! to the beginning of every test.
059ec3d9 1519
e22ca4ac 1520=back
059ec3d9 1521
e22ca4ac 1522=head1 VARIABLES
059ec3d9 1523
e22ca4ac 1524With 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 1525
e22ca4ac 1526Internally, all variables are represented as strings, meaning any operator will work on any variable. This means that '$sender_host_name > 4' is a legal criterion, even if it does not produce meaningful results. Variables in the list below are marked with a 'type' to help in choosing which types of operators make sense to use.
bf759a8b 1527
e22ca4ac
JJ
1528 Identifiers
1529 B - Boolean variables
1530 S - String variables
1531 N - Numeric variables
1532 . - Standard variable matching Exim's content definition
1533 # - Standard variable, contents differ from Exim's definition
1534 + - Non-standard variable
bf759a8b 1535
e22ca4ac 1536=over 4
059ec3d9 1537
e22ca4ac 1538=item S . $acl_c0-$acl_c9, $acl_m0-$acl_m9
059ec3d9 1539
e22ca4ac 1540User definable variables.
059ec3d9 1541
e22ca4ac 1542=item B + $allow_unqualified_recipient
059ec3d9 1543
e22ca4ac 1544TRUE if unqualified recipient addresses are permitted in header lines.
059ec3d9 1545
e22ca4ac 1546=item B + $allow_unqualified_sender
059ec3d9 1547
e22ca4ac 1548TRUE if unqualified sender addresses are permitted in header lines.
059ec3d9 1549
e22ca4ac 1550=item S . $authenticated_id
059ec3d9 1551
e22ca4ac 1552Optional saved information from authenticators, or the login name of the calling process for locally submitted messages.
059ec3d9 1553
e22ca4ac 1554=item S . $authenticated_sender
059ec3d9 1555
e22ca4ac 1556The value of AUTH= param for smtp messages, or a generated value from the calling processes login and qualify domain for locally submitted messages.
059ec3d9 1557
0ea2a468
JJ
1558=item S . $bheader_*, $bh_*
1559
1560Value of the header(s) with the same name with any RFC2047 words decoded if present. See section 11.5 of Exim's spec.txt for full details.
1561
e22ca4ac 1562=item S + $bmi_verdicts
059ec3d9 1563
e22ca4ac 1564The verdict string provided by a Brightmail content scan
059ec3d9 1565
e22ca4ac 1566=item N . $body_linecount
059ec3d9
PH
1567
1568The number of lines in the message's body.
1569
e22ca4ac 1570=item N . $body_zerocount
059ec3d9
PH
1571
1572The number of binary zero bytes in the message's body.
1573
465e92cf
JJ
1574=item S + $data_path
1575
1576The path to the body file's location in the filesystem.
1577
e22ca4ac 1578=item B + $deliver_freeze
059ec3d9 1579
e22ca4ac 1580TRUE if the message is currently frozen.
059ec3d9 1581
e22ca4ac 1582=item N + $deliver_frozen_at
059ec3d9 1583
e22ca4ac 1584The epoch time at which message was frozen.
059ec3d9 1585
e22ca4ac 1586=item B + $dont_deliver
059ec3d9 1587
e22ca4ac 1588TRUE if, under normal circumstances, Exim will not try to deliver the message.
059ec3d9 1589
e22ca4ac 1590=item S + $each_recipients
059ec3d9 1591
e22ca4ac 1592This is a psuedo variable which allows you to apply a test against each address in $recipients individually. Whereas '$recipients =~ /@aol.com/' will match if any recipient address contains aol.com, '$each_recipients =~ /@aol.com$/' will only be true if 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.
5f970846 1593
e22ca4ac 1594=item S + $each_recipients_del
5f970846 1595
e22ca4ac 1596Like $each_recipients, but for $recipients_del
059ec3d9 1597
e22ca4ac 1598=item S + $each_recipients_undel
059ec3d9 1599
e22ca4ac 1600Like $each_recipients, but for $recipients_undel
059ec3d9 1601
e22ca4ac 1602=item B . $first_delivery
059ec3d9 1603
e22ca4ac 1604TRUE if the message has never been deferred.
059ec3d9 1605
0ea2a468 1606=item S . $header_*, $h_*
059ec3d9 1607
0ea2a468 1608This will always match the contents of the corresponding $bheader_* variable currently (the same behaviour Exim displays when iconv is not installed).
059ec3d9 1609
465e92cf
JJ
1610=item S + $header_path
1611
1612The path to the header file's location in the filesystem.
1613
e22ca4ac 1614=item B . $host_lookup_deferred
059ec3d9 1615
e22ca4ac 1616TRUE if there was an attempt to look up the host's name from its IP address, but an error occurred that during the attempt.
059ec3d9 1617
e22ca4ac 1618=item B . $host_lookup_failed
059ec3d9 1619
e22ca4ac 1620TRUE if there was an attempt to look up the host's name from its IP address, but the attempt returned a negative result.
059ec3d9 1621
e22ca4ac 1622=item B + $local_error_message
059ec3d9 1623
e22ca4ac 1624TRUE if the message is a locally-generated error message.
059ec3d9 1625
e22ca4ac 1626=item S . $local_scan_data
059ec3d9 1627
e22ca4ac 1628The text returned by the local_scan() function when a message is received.
059ec3d9 1629
e22ca4ac 1630=item B . $manually_thawed
059ec3d9 1631
e22ca4ac 1632TRUE when the message has been manually thawed.
059ec3d9 1633
465e92cf
JJ
1634=item N . $max_received_linelength
1635
1636The number of bytes in the longest line that was received as part of the message, not counting line termination characters.
1637
e22ca4ac 1638=item N . $message_age
059ec3d9 1639
e22ca4ac 1640The number of seconds since the message was received.
059ec3d9 1641
e22ca4ac 1642=item S # $message_body
059ec3d9 1643
e22ca4ac 1644The message's body. Unlike Exim's variable of the same name, this variable contains the entire message body. Newlines and nulls are replaced by spaces.
059ec3d9 1645
0ea2a468
JJ
1646=item B + $message_body_missing
1647
1648TRUE is a message's spool data file (-D file) is missing or unreadable.
1649
e22ca4ac 1650=item N . $message_body_size
059ec3d9 1651
e22ca4ac 1652The size of the body in bytes.
059ec3d9 1653
e22ca4ac 1654=item S . $message_exim_id, $message_id
059ec3d9 1655
e22ca4ac 1656The unique message id that is used by Exim to identify the message. $message_id is deprecated as of Exim 4.53.
059ec3d9 1657
e22ca4ac 1658=item S . $message_headers
bf759a8b 1659
0ea2a468
JJ
1660A concatenation of all the header lines except for lines added by routers or transports. RFC2047 decoding is performed
1661
1662=item S . $message_headers_raw
1663
1664A concatenation of all the header lines except for lines added by routers or transports. No decoding or translation is performed.
bf759a8b 1665
e22ca4ac 1666=item N . $message_linecount
bf759a8b 1667
e22ca4ac 1668The number of lines in the entire message (body and headers).
bf759a8b 1669
e22ca4ac 1670=item N . $message_size
bf759a8b 1671
e22ca4ac 1672The size of the message in bytes.
bf759a8b 1673
e22ca4ac 1674=item N . $originator_gid
bf759a8b 1675
e22ca4ac 1676The group id under which the process that called Exim was running as when the message was received.
bf759a8b 1677
e22ca4ac 1678=item S + $originator_login
059ec3d9 1679
e22ca4ac 1680The login of the process which called Exim.
059ec3d9 1681
e22ca4ac 1682=item N . $originator_uid
059ec3d9 1683
e22ca4ac 1684The user id under which the process that called Exim was running as when the message was received.
059ec3d9 1685
0ea2a468
JJ
1686=item S . $received_ip_address, $interface_address
1687
1688The address of the local IP interface for network-originated messages. $interface_address is deprecated as of Exim 4.64
1689
1690=item N . $received_port, $interface_port
1691
1692The local port number if network-originated messages. $interface_port is deprecated as of Exim 4.64
1693
e22ca4ac 1694=item N . $received_count
059ec3d9 1695
e22ca4ac 1696The number of Received: header lines in the message.
059ec3d9 1697
e22ca4ac 1698=item S . $received_protocol
059ec3d9 1699
e22ca4ac 1700The name of the protocol by which the message was received.
059ec3d9 1701
e22ca4ac 1702=item N . $received_time
059ec3d9 1703
e22ca4ac 1704The epoch time at which the message was received.
059ec3d9 1705
e22ca4ac 1706=item S # $recipients
059ec3d9 1707
465e92cf 1708The list of envelope recipients for a message. Unlike Exim's version, this variable always contains every recipient of the message. The recipients are separated by a comma and a space. See also $each_recipients.
059ec3d9 1709
e22ca4ac 1710=item N . $recipients_count
059ec3d9 1711
e22ca4ac 1712The number of envelope recipients for the message.
059ec3d9 1713
e22ca4ac 1714=item S + $recipients_del
059ec3d9 1715
e22ca4ac 1716The 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. See also $each_recipients_del.
059ec3d9 1717
e22ca4ac 1718=item N + $recipients_del_count
059ec3d9 1719
e22ca4ac 1720The 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.
059ec3d9 1721
e22ca4ac 1722=item S + $recipients_undel
059ec3d9 1723
e22ca4ac 1724The 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. See also $each_recipients_undel.
059ec3d9 1725
e22ca4ac 1726=item N + $recipients_undel_count
059ec3d9 1727
e22ca4ac 1728The number of envelope recipients for the message which have not yet been delivered.
059ec3d9 1729
e22ca4ac 1730=item S . $reply_address
059ec3d9
PH
1731
1732The contents of the Reply-To: header line if one exists and it is not empty, or otherwise the contents of the From: header line.
1733
0ea2a468
JJ
1734=item S . $rheader_*, $rh_*
1735
1736The value of the message's header(s) with the same name. See section 11.5 of Exim's spec.txt for full description.
1737
e22ca4ac 1738=item S . $sender_address
059ec3d9
PH
1739
1740The sender's address that was received in the message's envelope. For bounce messages, the value of this variable is the empty string.
1741
e22ca4ac 1742=item S . $sender_address_domain
059ec3d9 1743
bf759a8b 1744The domain part of $sender_address.
059ec3d9 1745
e22ca4ac 1746=item S . $sender_address_local_part
059ec3d9 1747
bf759a8b 1748The local part of $sender_address.
059ec3d9 1749
e22ca4ac 1750=item S . $sender_helo_name
059ec3d9
PH
1751
1752The HELO or EHLO value supplied for smtp or bsmtp messages.
1753
e22ca4ac 1754=item S . $sender_host_address
059ec3d9
PH
1755
1756The remote host's IP address.
1757
e22ca4ac 1758=item S . $sender_host_authenticated
059ec3d9
PH
1759
1760The name of the authenticator driver which successfully authenticated the client from which the message was received.
1761
e22ca4ac 1762=item S . $sender_host_name
059ec3d9
PH
1763
1764The remote host's name as obtained by looking up its IP address.
1765
e22ca4ac 1766=item N . $sender_host_port
059ec3d9 1767
e22ca4ac 1768The port number that was used on the remote host for network-originated messages.
5f970846 1769
e22ca4ac 1770=item S . $sender_ident
5f970846 1771
e22ca4ac 1772The 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.
bf759a8b 1773
e22ca4ac 1774=item B + $sender_local
bf759a8b 1775
e22ca4ac 1776TRUE if the message was locally generated.
bf759a8b 1777
e22ca4ac 1778=item B + $sender_set_untrusted
bf759a8b 1779
e22ca4ac 1780TRUE if the envelope sender of this message was set by an untrusted local caller.
bf759a8b 1781
e22ca4ac 1782=item S + $shown_message_size
bf759a8b 1783
e22ca4ac 1784This non-standard variable contains the formatted size string. That is, for a message whose $message_size is 66566 bytes, $shown_message_size is 65K.
059ec3d9 1785
e22ca4ac 1786=item S . $smtp_active_hostname
059ec3d9 1787
e22ca4ac 1788The value of the active host name when the message was received, as specified by the "smtp_active_hostname" option.
059ec3d9 1789
e22ca4ac 1790=item S . $spam_score
059ec3d9 1791
e22ca4ac 1792The spam score of the message, for example '3.4' or '30.5'. (Requires exiscan or WITH_CONTENT_SCAN)
059ec3d9 1793
e22ca4ac 1794=item S . $spam_score_int
059ec3d9 1795
e22ca4ac 1796The spam score of the message, multiplied by ten, as an integer value. For instance '34' or '305'. (Requires exiscan or WITH_CONTENT_SCAN)
059ec3d9 1797
e22ca4ac 1798=item B . $tls_certificate_verified
059ec3d9 1799
e22ca4ac 1800TRUE if a TLS certificate was verified when the message was received.
059ec3d9 1801
e22ca4ac 1802=item S . $tls_cipher
059ec3d9 1803
e22ca4ac 1804The cipher suite that was negotiated for encrypted SMTP connections.
059ec3d9 1805
e22ca4ac 1806=item S . $tls_peerdn
059ec3d9 1807
e22ca4ac 1808The value of the Distinguished Name of the certificate if Exim is configured to request one
059ec3d9 1809
3f0945ff
PP
1810=item S . $tls_sni
1811
1812The value of the Server Name Indication TLS extension sent by a client, if one was sent.
1813
e22ca4ac 1814=item N + $warning_count
059ec3d9 1815
e22ca4ac 1816The number of delay warnings which have been sent for this message.
059ec3d9
PH
1817
1818=back
1819
059ec3d9
PH
1820=head1 CONTACT
1821
1822=over 4
1823
1824=item EMAIL: proj-exipick@jetmore.net
1825
1826=item HOME: jetmore.org/john/code/#exipick
1827
1828=back
1829
1830=cut