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