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