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