1#! /bin/sh
2#	$Id$
3#
4# HylaFAX Facsimile Software
5#
6# Copyright (c) 1990-1996 Sam Leffler
7# Copyright (c) 1991-1996 Silicon Graphics, Inc.
8#
9# Permission to use, copy, modify, distribute, and sell this software and
10# its documentation for any purpose is hereby granted without fee, provided
11# that (i) the above copyright notices and this permission notice appear in
12# all copies of the software and related documentation, and (ii) the names of
13# Sam Leffler and Silicon Graphics may not be used in any advertising or
14# publicity relating to the software without the specific, prior written
15# permission of Sam Leffler and Silicon Graphics.
16#
17# THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
18# EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
19# WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
20#
21# IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
22# ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
23# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
24# WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
25# LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26# OF THIS SOFTWARE.
27#
28
29#
30# Awk support program for notify shell script.  This
31# stuff is broken out into a separate file to avoid
32# overflowing the exec arg list on some systems like SCO.
33#
34
35function printItem(fmt, tag, value)
36{
37    printf "%14s: " fmt "\n", tag, value;
38}
39
40function printBanner(banner)
41{
42    print "";
43    print "    ---- " banner " ----";
44    print "";
45}
46
47function docType(s)
48{
49    if (match(s, "\.cover"))
50	return "PostScript cover page";
51    else if (match(s, "\.ps"))
52	return "PostScript";
53    else if (match(s, "\.tif"))
54	return "TIFF";
55    else if (match(s, "\.pdf"))
56	return "PDF";
57    else if (match(s, "\.pcl"))
58	return "PCL";
59    else
60	return "Unknown document type";
61}
62
63#
64# Construct a return-to-sender message.
65#
66function returnToSender()
67{
68    printBanner("Unsent job status");
69    printItem("%s", "Destination", number);
70    printItem("%s", "JobID", jobid);
71    printItem("%s", "GroupID", groupid);
72    printItem("%s", "Sender", sender);
73    printItem("%s", "Mailaddr", mailaddr);
74    if (commid != "")
75	printItem("%s", "CommID", commid);
76    if (modem != "any")
77	printItem("%s", "Modem", modem);
78    printItem("%s", "Submitted From", client);
79    if (jobType == "facsimile") {
80	printItem("%u (mm)", "Page Width", pagewidth);
81	printItem("%.0f (mm)", "Page Length", pagelength);
82	printItem("%.0f (lpi)", "Resolution", resolution);
83    }
84    printItem("%s", "Status", status == "" ? "  (nothing available)" : status);
85    printItem("%u (exchanges with remote device)", "Dialogs", tottries);
86    printItem("%u (consecutive failed calls to destination)", "Dials", ndials);
87    printItem("%u (total phone calls placed)", "Calls", totdials);
88    if (jobType == "facsimile") {
89	printItem("%u (pages transmitted)", "Pages", npages);
90	printItem("%u (total pages to transmit)", "TotPages", totpages);
91	printItem("%u (attempts to send current page)", "Attempts", ntries);
92	printItem("%u (directory of next page to send)", "Dirnum", dirnum);
93	if (nfiles > 0) {
94	    printBanner("Documents submitted for transmission");
95	    print "The following documents were submitted for transmission and are";
96	    print "available on the server for reuse until they are automatically";
97	    print "purged when this job is " doneop "d.  Documents may also be manually";
98	    print "removed using the faxrm command; consult faxrm(1) for information.";
99	    print ""
100	    printf "%-20s %8s %s\n", "Filename", "Size", "Type";
101	    for (i = 0; i < nfiles; i++) {
102		"wc -c " files[i] | getline;
103		printf "%-20s %8d %s\n", files[i], $1, docType(files[i]);
104		close("wc -c " files[i]);
105	     }
106	}
107    } else if (jobType == "pager") {
108	if (npins != 0) {
109	    printBanner("Unsent pages submitted for transmission");
110	    for (i = 0; i < npins; i++)
111		printf "%15s\n",  "PIN " pins[i];
112	}
113	if (nfiles != 0) {
114	    printBanner("Message text");
115	    while ((getline <files[0]) > 0)
116		print $0;
117	    close(files[0]);
118	}
119    }
120}
121
122function returnTranscript()
123{
124    printBanner("Transcript of session follows");
125    comFile = "log/c" commid;
126    if ((getline <comFile) > 0) {
127	do {
128	    if (index($0, "-- data") == 0)
129		print $0
130	} while ((getline <comFile) > 0);
131	close(comFile);
132    } else {
133	printf "    No transcript available";
134	if (commid != "")
135	    printf "(CommID c" commid ")";
136	print ".";
137    }
138}
139
140function printStatus(s)
141{
142    if (s == "")
143	print "<no reason recorded>";
144    else
145	print s
146}
147
148function putHeaders(subject)
149{
150    print "To: " mailaddr;
151    print "Subject: " subject;
152    print "";
153    printf "Your " jobType " job to " number;
154}
155
156BEGIN		{ nfiles = 0;
157		  npins = 0;
158		  pagewidth = 0;
159		  pagelength = 0;
160		  resolution = 0;
161		  jobType = "facsimile";
162		  signalrate = "unknown";
163		  dataformat = "unknown";
164		  doneop = "default";
165                pagernum = "unknown";
166		  commid = "";
167		}
168/^jobid/	{ jobid = $2; }
169/^groupid/	{ groupid = $2; }
170/^state/	{ state = $2+0; }
171/^doneop/	{ doneop = $2; }
172/^number/	{ number = $2; }
173/^external/	{ number = $2; }		# override unprocessed number
174/^sender/	{ sender = $2; }
175/^mailaddr/	{ mailaddr = $2; }
176/^jobtag/	{ jobtag = $0; sub("jobtag:", "", jobtag); }
177/^jobtype/	{ jobType = $2; }
178/^status/	{ status = $0; sub("status:", "", status);
179		  if (status ~ /\\$/) {
180		      sub("\\\\$", "\n", status);
181		      while (getline > 0) {
182			  status = status $0;
183			  sub("\\\\$", "\n", status);
184			  if ($0 !~ /\\$/)
185			      break;
186		      }
187		  }
188		}
189/^resolution/	{ resolution = $2; }
190/^npages/	{ npages = $2; }
191/^totpages/	{ totpages = $2; }
192/^dirnum/	{ dirnum = $2; }
193/^commid/	{ commid = $2; }
194/^ntries/	{ ntries = $2; }
195/^ndials/	{ ndials = $2; }
196/^pagewidth/	{ pagewidth = $2; }
197/^pagelength/	{ pagelength = $2; }
198/^signalrate/	{ signalrate = $2; }
199/^dataformat/	{ dataformat = $2; }
200/^modem/	{ modem = $2; }
201/^totdials/	{ totdials = $2; }
202/^tottries/	{ tottries = $2; }
203/^client/	{ client = $2; }
204/^[!]*post/	{ files[nfiles++] = $4; }
205/^[!]*tiff/	{ files[nfiles++] = $4; }
206/^[!]*pcl/	{ files[nfiles++] = $4; }
207/^page:/	{ pins[npins++] = $4; }
208/^[!]page:/   { pagernum = $4; }
209/^data:/	{ files[nfiles++] = $4; }
210/^poll/		{ poll = " -p"; }
211END {
212    if (jobtag == "")
213	jobtag = jobType " job " jobid;;
214    if (doneop == "default")
215	doneop = "remove";
216    if (jobType == "pager")
217      number = pagernum;
218    if (why == "done") {
219	putHeaders(jobtag " to " number " completed");
220	print " was completed successfully.";
221	print "";
222	if (jobType == "facsimile") {
223	    printItem("%u", "Pages", npages);
224	    if (resolution == 196)
225		printItem("%s", "Quality", "Fine");
226	    else
227		printItem("%s", "Quality", "Normal");
228	    printItem("%u (mm)", "Page Width", pagewidth);
229	    printItem("%.0f (mm)", "Page Length", pagelength);
230	    printItem("%s", "Signal Rate", signalrate);
231	    printItem("%s", "Data Format", dataformat);
232	}
233	if (tottries != 1)
234	    printItem("%s (exchanges with remote device)", "Dialogs", tottries);
235	if (totdials != 1)
236	    printItem("%s (total phone calls placed)", "Calls", totdials);
237	if (modem != "any")
238	    printItem("%s", "Modem", modem);
239	printItem("%s", "Submitted From", client);
240	printItem("%s", "JobID", jobid);
241	printItem("%s", "GroupID", groupid);
242	printItem("%s", "CommID", "c" commid);
243	printf "\nProcessing time was " jobTime ".\n";
244	if (status != "") {
245	    print "  Additional information:\n    " status;
246	    returnTranscript();
247	}
248    } else if (why == "failed") {
249	putHeaders(jobtag " to " number " failed");
250	printf " failed because:\n    ";
251	printStatus(status);
252	returnTranscript();
253	returnToSender();
254    } else if (why == "rejected") {
255	putHeaders(jobtag " to " number " failed");
256	printf " was rejected because:\n    ";
257	printStatus(status);
258	returnToSender();
259    } else if (why == "blocked") {
260	putHeaders(jobtag " to " number " blocked");
261	printf " is delayed in the scheduling queues because:\n    ";
262	printStatus(status);
263	print "";
264	print "The job will be processed as soon as possible."
265    } else if (why == "requeued") {
266	putHeaders(jobtag " to " number " requeued");
267	printf " was not sent because:\n    ";
268	printStatus(status);
269	print "";
270	print "The job will be retried at " nextTry "."
271	returnTranscript();
272    } else if (why == "removed" || why == "killed") {
273	putHeaders(jobtag " to " number " removed from queue");
274	print " was deleted from the queue.";
275	if (why == "killed")
276	    returnToSender();
277    } else if (why == "timedout") {
278	putHeaders(jobtag " to " number " failed");
279	print " could not be completed before the appointed deadline.";
280	returnToSender();
281    } else if (why == "format_failed") {
282	putHeaders(jobtag " to " number " failed");
283	print " was not sent because document conversion"
284	print "to facsimile failed.  The output from the converter program was:\n";
285	print status "\n";
286	printf "Check any PostScript documents for non-standard fonts %s.\n",
287	    "and invalid constructs";
288	returnToSender();
289    } else if (why == "no_formatter") {
290	putHeaders(jobtag " to " number " failed");
291	print " was not sent because";
292	print "the document conversion script was not found.";
293	returnToSender();
294    } else if (match(why, "poll_*")) {
295	putHeaders("Notice about " jobtag);
296	printf ", a polling request,\ncould not be completed because ";
297	if (why == "poll_rejected")
298	    print "the remote side rejected your request.";
299	else if (why == "poll_no_document")
300	    print "no document was available for retrieval.";
301	else if (why == "poll_failed")
302	    print "an unspecified problem occurred.";
303	print "";
304	printf "Processing time was %s.\n", jobTime;
305	returnTranscript();
306    } else {
307	putHeaders("Notice about " jobtag);
308	print " had something happen to it."
309	print "Unfortunately, the notification script was invoked",
310	    "with an unknown reason"
311	print "so the rest of this message is for debugging:\n";
312	print "why: " why;
313	print "jobTime: " jobTime;
314	print "nextTry: " nextTry;
315	print  "";
316	print "This should not happen, please report it to your administrator.";
317	returnTranscript();
318	returnToSender();
319    }
320}
321