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