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