Testsuite: fix munge for mailq
[exim.git] / src / src / exinext.src
1 #! /bin/sh
2
3 # Copyright (c) The Exim Maintainers 2023
4 # Copyright (c) University of Cambridge, 1995 - 2007
5 # See the file NOTICE for conditions of use and distribution.
6
7 # Except when they appear in comments, the following placeholders in this
8 # source are replaced when it is turned into a runnable script:
9 #
10 # CONFIGURE_FILE_USE_NODE
11 # CONFIGURE_FILE
12 # BIN_DIRECTORY
13
14 # PROCESSED_FLAG
15
16 # A shell+perl script to fish out the next retry time for a given domain;
17 # it first calls exim to find out which hosts are set up for that domain and
18 # then fishes out the retry data for each one.
19
20 # For testing the selection and formatting logic, and perhaps for use in
21 # special cases, the script can have an argument -C <filename> to specify
22 # the use of an alternate Exim configuration file. It may also have any number
23 # of -D options to set macros that are passed to exim.
24
25 config=
26 eximmacdef=
27 exim_path=
28
29 if [ "x$1" = x--version -o "x$1" = x-v ]
30 then
31     echo "`basename $0`: $0"
32     echo "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION"
33     exit 0
34 fi
35
36 if expr -- $1 : '\-' >/dev/null ; then
37   while expr -- $1 : '\-' >/dev/null ; do
38     if [ "$1" = "-C" ]; then
39       config=$2
40       shift
41       shift
42     elif expr -- $1 : '\-D' >/dev/null ; then
43       eximmacdef="$eximmacdef $1"
44       if expr -- $1 : '\-DEXIM_PATH=' >/dev/null ; then
45         exim_path=`expr -- $1 : '\-DEXIM_PATH=\(.*\)'`
46       fi
47       shift
48     else
49       break
50     fi
51   done
52 fi
53
54 # We need to save the script's argument because in the absence of -C we need to
55 # use shell arguments for sorting out the configuration file name.
56
57 argone=$1
58
59 # This is the normal case when no config file or macros are specified
60
61 if [ "$config" = "" ]; then
62   # See if this installation is using the esoteric "USE_NODE" feature of Exim,
63   # in which it uses the host's name as a suffix for the configuration file name.
64
65   if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then
66     hostsuffix=.`uname -n`
67   fi
68
69   # Now find the configuration file name. This has got complicated because
70   # CONFIGURE_FILE may now be a list of files. The one that is used is the first
71   # one that exists. Mimic the code in readconf.c by testing first for the
72   # suffixed file in each case.
73
74   set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <<End
75 CONFIGURE_FILE
76 End
77 `
78   while [ "$config" = "" -a $# -gt 0 ] ; do
79     if [ -f "$1$hostsuffix" ] ; then
80       config="$1$hostsuffix"
81     elif [ -f "$1" ] ; then
82       config="$1"
83     fi
84     shift
85   done
86 fi
87
88 # Determine where the spool directory is. Search for an exim_path setting
89 # in the configure file; otherwise use the bin directory. Call that version of
90 # Exim to find the spool directory and the qualify domain. BEWARE: a tab
91 # character is needed in the command below. It has had a nasty tendency to get
92 # lost in the past. Use a variable to hold a space and a tab to keep the tab in
93 # one place.
94
95 st='     '
96
97 if [ "$exim_path" = "" ]; then
98   exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
99 fi
100
101 if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/exim; fi
102 spool_directory=`$exim_path $eximmacdef -C $config -bP spool_directory | sed 's/.*=[  ]*//'`
103 qualify_domain=`$exim_path $eximmacdef -C $config -bP qualify_domain | sed 's/.*=[  ]*//'`
104
105 # Now do the job. Perl uses $ so frequently that we don't want to have to
106 # escape them all from the shell, so pass in shell variable values as
107 # arguments.
108
109 # 16-May-1996  Fixed it to do better if routing fails to complete.
110 #              Improved the format of the output.
111 # 10-Jun-1996  Complain if no argument given.
112 # 02-Aug-1996  Lower case the domain.
113 # 14-Jan-1999  Add subject to want list even if remote host found, so as to
114 #              pick up routing delays after temporary recipient errors.
115 #              Also add unqualified subject if it looks like a message id.
116 # 01-Apr-2004  Add the -C feature for testing
117 # 22-Dec-2005  Complete the -C feature (!)
118
119 if [ "$argone" = "" ]; then
120   echo "Usage: exinext <address>|<domain>|<local-part>"
121   exit 1
122 fi
123
124 perl - $exim_path "$eximmacdef" $argone $spool_directory $qualify_domain $config <<'End'
125
126   # We don't import anything, but guard against future changes which do
127   BEGIN { pop @INC if $INC[-1] eq '.' };
128
129   # Name the arguments
130
131   $exim = $ARGV[0];
132   $eximmacdef = $ARGV[1];
133   $subject = $ARGV[2];
134   $spool = $ARGV[3];
135   $qualify = $ARGV[4];
136   $config = $ARGV[5];
137
138   # If the subject doesn't contain an @ then construct an address
139   # for the domain, and ensure that in both cases the domain is
140   # lower cased.
141
142   $address = ($subject =~ /^([^\@]*)\@([^\@]*)$/)?
143     "$1\@\L$2\E" : "User\@\L$subject\E";
144
145   # Run Exim to get a list of hosts for the given domain; for
146   # each one construct the appropriate retry key.
147
148   open(LIST, "$exim -C $config -v -bt $address |") ||
149     die "can't run exim to route $address";
150
151   while (<LIST>)
152     {
153     chop;
154     push(@list, $_) if s/\s*host (\S+)\s+\[(.+)\].*/$1:$2/;
155     print "$_\n" if /cannot be resolved/;
156     }
157   close(LIST);
158
159   # If there were no hosts, assume that what was given was a local
160   # username, unless it contains an @, and construct a suitable retry
161   # key for that. Also, if it looks like a message id, search for that
162   # as well, so as to pick up message-specific retry data.
163
164   if (scalar(@list) == 0)
165     {
166     push(@list, $subject) if $subject =~ /^\w{6}-\w{11}-\w{4}$/;
167     push(@list, $subject) if $subject =~ /^\w{6}-\w{6}-\w{2}$/;
168
169     if ($subject !~ /\@/ && $subject !~ /\./)
170       {
171       push(@list, "$subject\@$qualify");
172       }
173     else
174       {
175       print "No remote hosts found for $subject\n";
176       }
177     }
178
179   # Always search for the full address, even if hosts are found, in case
180   # there is a routing delay caused by a temporary recipient error.
181
182   push(@list, $subject);
183
184   # Run exim_dumpdb to get out the retry data and pick off what we want
185
186   open(DATA, "${exim}_dumpdb $spool retry |") ||
187     die "can't run exim_dumpdb";
188
189   while (<DATA>)
190     {
191     for ($i = 0; $i <= $#list; $i++)
192       {
193       if (/$list[$i]/)
194         {
195         $printed = 1;
196         if (/^\s*T:[^:\s]*:/)
197           {
198           # We rely on non-space-containing strings, for parsing
199
200           ($key,$error,$error2,$text) = /^\s*T:(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/;
201
202           ($host,$ip,$port,$msgid) = $key =~
203             /^([^:[]*|\[[^]]*\])        # host (could be an ip)
204               :([^:[]*|\[[^]]*\])       # ip
205               (?::(\d{1,5}))?           # maybe port
206               (?::(\S{23}))?            # maybe msgid
207               $/x;
208
209           printf("Transport: %s %s", $host, $ip);
210           print ":$port" if defined $port;
211           print " $msgid" if defined $msgid;
212           print " error $error: $text\n";
213           }
214
215         else
216           {
217           ($type,$domain,$error,$error2,$text) =
218             /^\s*(\S):(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/;
219           $type = ($type eq 'R')? "Route: " :
220                   ($type eq 'T')? "Transport: " : "";
221           print "$type$domain error $error: $text\n";
222           }
223         $_ = <DATA>;
224         ($first,$last,$next,$expired) =
225           /^(\S+\s+\S+)\s+(\S+\s+\S+)\s+(\S+\s+\S+)\s*(\*?)/;
226         print "  first failed: $first\n";
227         print "  last tried:   $last\n";
228         print "  next try at:  $next\n";
229         print "  past final cutoff time\n" if $expired eq "*";
230         }
231       }
232     }
233
234   close(DATA);
235   print "No retry data found for $subject\n" if !$printed;
236 End
237