1/***************************************************************************
2 * LPRng - An Extended Print Spooler System
3 *
4 * Copyright 1988-2003, Patrick Powell, San Diego, CA
5 *     papowell@lprng.com
6 * See LICENSE for conditions of use.
7 *
8 ***************************************************************************/
9
10 static char *const _id =
11"$Id: sendmail.c,v 1.1.1.1 2008/10/15 03:28:27 james26_jang Exp $";
12
13#include "lp.h"
14#include "errorcodes.h"
15#include "fileopen.h"
16#include "getqueue.h"
17#include "sendmail.h"
18#include "child.h"
19/**** ENDINCLUDE ****/
20
21/*
22 * sendmail --- tell people about job completion
23 * 1. fork a sendmail process
24 * 2. if successful, send the good news
25 * 3. if unsuccessful, send the bad news
26 */
27
28void Sendmail_to_user( int retval, struct job *job )
29{
30	char buffer[SMALLBUFFER], msg[SMALLBUFFER];
31	int n, tempfd;
32	char *id, *mailname, *opname, *s;
33
34	/*
35	 * check to see if the user really wanted
36	 * "your file was printed ok" message
37	 */
38	id = Find_str_value(&job->info,IDENTIFIER,Value_sep);
39	if(!id) id = Find_str_value(&job->info,TRANSFERNAME,Value_sep);
40	mailname = Find_str_value(&job->info,MAILNAME,Value_sep);
41	opname = Mail_operator_on_error_DYN;
42	DEBUG2("Sendmail_to_user: user '%s', operator '%s', sendmail '%s'",
43		mailname, opname, Sendmail_DYN );
44
45/*
46	We will let the sendmail script or program check for correct formats
47		This allows the most hideous things to be done to mail messages
48		but we know that this is a loophole
49	if( !safestrchr( mailname,'%') && !safestrchr(mailname,'@') ) mailname = 0;
50	if( !safestrchr( opname,'%') && !safestrchr(opname,'@') ) opname = 0;
51*/
52	if( retval == JSUCC ) opname = 0;
53	if( Sendmail_DYN == 0 ) return;
54	if( !Sendmail_to_user_DYN ) mailname = 0;
55	if( mailname == 0 && opname == 0 ) return;
56
57	tempfd = Make_temp_fd( 0 );
58
59	DEBUG2("Sendmail_to_user: user '%s', operator '%s'", mailname, opname );
60
61	msg[0] = 0;
62	if( mailname ){
63		SNPRINTF( msg, sizeof(msg)) "'%s'", mailname );
64		SNPRINTF( buffer, sizeof(buffer)) "To: %s\n", mailname );
65		if( Write_fd_str( tempfd, buffer ) < 0 ) goto wr_error;
66	}
67	if( opname ){
68		n = safestrlen(msg);
69		SNPRINTF( msg+n, sizeof(msg)-n) "%s'%s'",n?" and ":"", opname );
70		SNPRINTF(buffer,sizeof(buffer))
71		"%s: %s\n", mailname?"CC":"To", opname );
72		if( Write_fd_str( tempfd, buffer ) < 0 ) goto wr_error;
73	}
74	setstatus( job, "sending mail to %s", msg );
75	SNPRINTF(buffer,sizeof(buffer))
76		"From: %s@%s\n",
77		Mail_from_DYN ? Mail_from_DYN : Printer_DYN, FQDNHost_FQDN );
78	if( Write_fd_str( tempfd, buffer ) < 0 ) goto wr_error;
79
80	SNPRINTF(buffer,sizeof(buffer))
81		"Subject: %s@%s job %s\n\n",
82		Printer_DYN, FQDNHost_FQDN, id );
83	if( Write_fd_str( tempfd, buffer ) < 0 ) goto wr_error;
84
85	/* now do the message */
86	SNPRINTF(buffer,sizeof(buffer))
87		_("printer %s job %s"), Printer_DYN, id );
88	if( Write_fd_str( tempfd, buffer ) < 0 ) goto wr_error;
89
90	switch( retval) {
91	case JSUCC:
92		SNPRINTF(buffer,sizeof(buffer))
93		_(" was successful.\n"));
94		break;
95
96	case JFAIL:
97		SNPRINTF(buffer,sizeof(buffer))
98		_(" failed, and retry count was exceeded.\n") );
99		break;
100
101	case JABORT:
102		SNPRINTF(buffer,sizeof(buffer))
103		_(" failed and could not be retried.\n") );
104		break;
105
106	default:
107		SNPRINTF(buffer,sizeof(buffer))
108		_(" died a horrible death.\n"));
109		break;
110	}
111	if( Write_fd_str( tempfd, buffer ) < 0 ) goto wr_error;
112
113	/*
114	 * get the last status of the spooler
115	 */
116	if( (s = Get_file_image( Queue_status_file_DYN, Max_status_size_DYN )) ){
117		if( Write_fd_str( tempfd, "\nStatus:\n\n" ) < 0 ||
118			Write_fd_str( tempfd, s ) < 0 ) goto wr_error;
119		if(s) free(s); s = 0;
120	}
121
122	if( (s = Get_file_image( Status_file_DYN, Max_status_size_DYN )) ){
123		if( Write_fd_str( tempfd, "\nFilter Status:\n\n" ) < 0 ||
124			Write_fd_str( tempfd, s ) < 0 ) goto wr_error;
125		if(s) free(s); s = 0;
126	}
127	if( lseek( tempfd, 0, SEEK_SET ) == -1 ){
128		Errorcode = JABORT;
129		LOGERR_DIE(LOG_ERR) "Sendmail_to_user: seek failed");
130	}
131	n = Filter_file( tempfd, -1, "MAIL", Sendmail_DYN, 0, job, 0, 0 );
132	if( n ){
133		Errorcode = JABORT;
134		LOGERR(LOG_ERR) "Sendmail_to_user: '%s' failed '%s'", Sendmail_DYN, Server_status(n) );
135	}
136	return;
137
138 wr_error:
139	Errorcode = JABORT;
140	LOGERR_DIE(LOG_ERR) "Sendmail_to_user: write failed");
141}
142