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