1 /*:ts=8*/
2 /*****************************************************************************
3  * FIDOGATE --- Gateway UNIX Mail/News <-> FIDO NetMail/EchoMail
4  *
5  * $Id: bounce.c,v 4.14 2004/08/22 20:19:11 n0ll Exp $
6  *
7  * Bounce mails for various reasons, using LIBDIR/bounce.XXX messages
8  *
9  *****************************************************************************
10  * Copyright (C) 1990-2004
11  *  _____ _____
12  * |     |___  |   Martin Junius             <mj.at.n0ll.dot.net>
13  * | | | |   | |   Radiumstr. 18
14  * |_|_|_|@home|   D-51069 Koeln, Germany
15  *
16  * This file is part of FIDOGATE.
17  *
18  * FIDOGATE is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU General Public License as published by the
20  * Free Software Foundation; either version 2, or (at your option) any
21  * later version.
22  *
23  * FIDOGATE is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with FIDOGATE; see the file COPYING.  If not, write to the Free
30  * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31  *****************************************************************************/
32 
33 #include "fidogate.h"
34 
35 
36 
37 /*
38  * Set CC address for bounced messages
39  */
40 static char *bounce_ccmail = NULL;
41 
bounce_set_cc(char * cc)42 void bounce_set_cc(char *cc)
43 {
44     bounce_ccmail = cc;
45 }
46 
47 
48 
49 /*
50  * Print text file with substitutions for %x
51  */
print_file_subst(FILE * in,FILE * out,Message * msg,char * rfc_to,Textlist * body)52 int print_file_subst(FILE *in, FILE *out, Message *msg, char *rfc_to, Textlist *body)
53 {
54     int c;
55 #ifdef AI_8
56     char *hg;
57 #endif
58 
59     while((c = getc(in)) != EOF)
60     {
61 	if(c == '%')
62 	{
63 	    c = getc(in);
64 	    switch(c)
65 	    {
66 	    case 'F':			/* From node */
67 		fputs( znfp1(&msg->node_from), out);	break;
68 	    case 'T':			/* To node */
69 		fputs( znfp1(&msg->node_to), out);		break;
70 	    case 'O':			/* Orig node */
71 		fputs( znfp1(&msg->node_orig), out);	break;
72 	    case 'd':			/* Date */
73 		fputs( date(NULL, &msg->date), out);			break;
74 	    case 't':			/* To name */
75 		fputs( msg->name_to, out);				break;
76 	    case 'f':			/* From name */
77 		fputs( msg->name_from, out);				break;
78 	    case 's':			/* Subject */
79 		fputs( msg->subject, out);				break;
80 	    case 'R':			/* RFC To: */
81 		fputs( rfc_to, out);					break;
82 	    case 'M':			/* Message */
83 		tl_print(body, out);				break;
84 #ifdef AI_8
85 	    case 'A':			/* RFC From: */
86 		if((hg = s_header_getcomplete("From")))
87 		    fputs( hg, out);					break;
88 	    case 'D':			/* RFC Date: */
89 	    	if((hg = header_get("Date")))
90 		    fputs( hg, out);					break;
91 	    case 'N':			/* RFC Newsgroups: */
92 		if((hg = header_get("Newsgroups")))
93 		    fputs( hg, out);					break;
94 	    case 'S':			/* RFC Subject: */
95 		if((hg = header_get("Subject")))
96 		    fputs( hg, out);					break;
97 #endif
98 	    }
99 	}
100 	else
101 	    putc(c, out);
102     }
103 
104     return ferror(in);
105 }
106 
107 
108 
109 /*
110  * Create header for bounced mail
111  */
bounce_header(char * to)112 int bounce_header(char *to)
113              				/* To: */
114 {
115     /*
116      * Open new mail
117      */
118     if(mail_open('m') == ERROR)
119 	return ERROR;
120 
121     /*
122      * Create RFC header
123      */
124     fprintf(mail_file('m'),
125 	    "From Mailer-Daemon %s\n", date(DATE_FROM, NULL) );
126     fprintf(mail_file('m'),
127 	    "Date: %s\n", date(NULL, NULL) );
128     fprintf(mail_file('m'),
129 	    "From: Mailer-Daemon@%s (Mail Delivery Subsystem)\n", cf_fqdn() );
130     fprintf(mail_file('m'), "To: %s\n", to);
131     if(bounce_ccmail)
132 	fprintf(mail_file('m'), "Cc: %s\n", bounce_ccmail);
133     /* Additional header may follow in message file */
134 
135     return OK;
136 }
137 
138 
139 
140 /*
141  * Bounce mail
142  */
bounce_mail(char * reason,RFCAddr * addr_from,Message * msg,char * rfc_to,Textlist * body)143 int bounce_mail(char *reason, RFCAddr *addr_from, Message *msg, char *rfc_to, Textlist *body)
144 {
145     char *to;
146     FILE *in;
147 
148     to = s_rfcaddr_to_asc(addr_from, TRUE);
149 
150     if(bounce_header(to) == ERROR)
151 	return ERROR;
152 
153     BUF_COPY3(buffer, cf_p_configdir(), "/bounce.", reason);
154 
155     in = xfopen(buffer, R_MODE);
156     print_file_subst(in, mail_file('m'), msg, rfc_to, body);
157     fclose(in);
158 
159     return mail_close('m');
160 }
161 
162 
163