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