File: housekeeper

package info (click to toggle)
zmailer 2.99.55-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,516 kB
  • ctags: 9,694
  • sloc: ansic: 120,953; sh: 3,862; makefile: 3,166; perl: 2,695; python: 115; awk: 22
file content (132 lines) | stat: -rwxr-xr-x 4,128 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/sh -
#
# housekeeper
#
# zmailer clean-up script
# Jean-Francois Lamy <lamy@ai.utoronto.ca> 88-06-03
#
# Removes stale messages from router,deferred,queue,transport.  Mail message
# back to error return address, except if that address is the mail daemon.
# The scheduler is stopped while removing from queue, so the transport and
# message files can be removed without confusing it.
maildaemon=mailer-daemon

if [ -f /etc/zmailer.conf ]; then
	. /etc/zmailer.conf
else
	MAILBIN="/usr/lib/zmail"
	POSTOFFICE="/spool/postoffice"
fi
case $POSTOFFICE in
/*)	;;
*)	echo "$0: panic!! can't initialize from /etc/zmailer.conf"
	exit 1
	;;
esac

# set this to one less than the number of full days a message must have
# spent in the queue before being considered stale.
days=2	# message will leave after 3 full days in queue.
zmailer=$MAILSHARE/zmailer
sendmail="/usr/lib/sendmail -t -fmailer-daemon"
rm="/bin/rm -f"
mv="/bin/mv"

# debug
#days=0
#sendmail="head -24"
#mv="echo"

cd $POSTOFFICE
dayselapsed=`expr $days + 1`
for old in `find router deferred -type f -mtime +$days -print` ; do

	# extract "from " header (error return address)
	return=`sed -n					 		\
	-e '/^[ 	]*$/q'						\
	-e '/^[Ff][Rr][Oo][Mm][ 	]</s/.*\(<.*>\).*/Error Return Address \1/p'	\
	-e '/^[Ff][Rr][Oo][Mm][ 	]/s/.* \(.*\)$/\1/p'		\
	    	<$old`

	# no "from " header; local mail then. use file ownership.
	if [ -z "$return" ] ; then
		return=`ls -l $old | awk '{print $3}'`
	fi

	if [ "$return" != "$maildaemon" ] ; then
	# mail it.
	( cat <<EOF ; 							\
	  echo; echo "---- Unsent message ----"; cat $old )  |  $sendmail
To: $return
From: The Post Office <postmaster>
Subject: Unable to process message for $dayselapsed days.

For the last $dayselapsed days, it has been impossible to process your message.

This generally happens when name server failures or network connectivity
problems prevent the mailer from looking up one or more of the addresses
mentioned in the message headers (i.e. your message is likely correct and
would have been processed had it not been for these problems over which
we have no control).  Note that the mailer currently examines *all* addresses
(including From:, To:, Cc:, etc).

Because a delay of $dayselapsed days is usually long enough for minor problems
with name servers and network connectivity to get resolved, we are returning
your message unprocessed so you can try an alternate route if one is available
to you.

EOF
	fi
	if [ "$?" -eq 0 ] ; then
#		$rm $old
		$mv $old $POSTOFFICE/housekeeper/deferred
	fi
	return=""
done

cd $POSTOFFICE/queue
$zmailer kill scheduler 2>&1 >/dev/null
for old in `find . -mtime +$days -print` ; do

	# extract "from " header (error return address)
	return=`sed -n					 		\
	-e '/^[ 	]*$/q'						\
	-e '/^[Ff][Rr][Oo][Mm][ 	]</s/.*\(<.*>\).*/Error Return Address \1/p'	\
	-e '/^[Ff][Rr][Oo][Mm][ 	]/s/.* \(.*\)$/\1/p'		\
	    	<$old`

	# no "from " header; local mail then. use file ownership.
	if [ -z "$return" ] ; then
		return=`ls -l $old | awk '{print $3}'`
	fi
	if [ "$return" != "$maildaemon" ] ; then

	# extract list of unreachable recipients from control file.
	# "r " starts lines for undelivered recipients.
	# "r~" means delivery being attempted.  If it failed for the last
	#      n days, it likely would have this time too.
	( cat <<EOF ; 							\
	 (egrep '^r ' ../transport/$old | awk '{print $4}') ;		\
	 (egrep '^r~' ../transport/$old | awk '{print $3}') ;		\
	  echo; echo "---- Unsent message ----"; cat $old )  |  $sendmail
To: $return
From: The Post Office <postmaster>
Subject: Unable to send mail message for $dayselapsed days.

Delivery attempts for the recipient(s) listed below have been failing for the
last $dayselapsed days, and we are now giving up.  Network failures or
problems at the remote end are the usual cause for such problems.  Delivery
was normal for the other recipients.

Delivery did not take place for:

EOF
	fi
	if [ "$?" -eq 0 ] ; then
#		$rm $old ../transport/$old
		$mv $old $POSTOFFICE/housekeeper/queue
		$mv ../transport/$old $POSTOFFICE/housekeeper/transport
	fi
	return=""
done
$zmailer scheduler 2>&1 >/dev/null