summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mail/copylet.c
blob: 63e02034b4fa31064b20acd1476fbae8bac2a0b3 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "mail.h"

/*
    NAME
	copylet - copy a given letter to a file pointer

    SYNOPSIS
	int copylet(int letnum, FILE *f, int type)

    DESCRIPTION
	Copylet() will copy the letter "letnum" to the
	given file pointer.

		letnum	-> index into: letter table
		f	-> file pointer to copy file to
		type	-> copy type

	Returns TRUE on a completely successful copy.
*/

int
copylet(int letnum, FILE *f, int type) 
{
	int		pos = ftell(f);
	int		rc  = xxxcopylet(letnum, f, type);

	if (fflush(f) != 0)
		rc = FALSE;
	
	/*
	 * On error, truncate the file to its original position so that a
	 * partial message is not left in the mailbox.
	 */
	if (rc == FALSE)
		ftruncate(fileno(f), pos);

	return(rc);
}

int
xxxcopylet(int letnum, FILE *f, int type) 
{
	static char	pn[] = "copylet";
	char	buf[LSIZE], lastc;
	char	wbuf[LSIZE];
	int	n;
	long	i, k;
	int	num;
	int	rtrncont = 1;	/* True: nondelivery&content included, or regular mail */
	int	suppress = FALSE;
	int	sav_suppress = FALSE; /* Did we suppress previous hdr line? */
	int	print_from_struct = FALSE; /* print from hdrlines struct */
					   /* rather than fgets() buffer */
	int	pushrest = FALSE;
	int	ctf = FALSE;
	int	didafflines = FALSE;	/* Did we already put out any */
					/* H_AFWDFROM lines? */
	int	didrcvlines = FALSE;	/* Did we already put out any */
					/* H_RECEIVED lines? */
	long	clen = -1L;
	int	htype;			/* header type */
	int	sav_htype;	/* Header type of last non-H_CONT header line */
	struct hdrs *hptr;

	if (!sending) {
		/* Clear out any saved header info from previous message */
		clr_hinfo();
	}

	fseek(tmpf, let[letnum].adr, 0);
	/* Get size of message as stored into tempfile by copymt() */
	k = let[letnum+1].adr - let[letnum].adr;
	Dout(pn, 1, "(letnum = %d, type = %d), k = %ld\n", letnum, type, k);
	while (k>0) {	/* process header */
		num = ((k < sizeof(buf)) ? k+1 : sizeof(buf));
		if (fgets (buf, num, tmpf) == NULL) {
			return (FALSE);
		}
		if ((n = strlen (buf)) == 0) {
			k = 0;
			break;
		}
		k -= n;
		lastc = buf[n-1];
		if (pushrest) {
			pushrest = (lastc != '\n');
			continue;
		}
		htype = isheader (buf, &ctf);
		Dout(pn, 5, "loop 1: buf = %s, htype= %d/%s\n", buf, htype, header[htype].tag);
		if (htype == H_CLEN) {
			if (!sending) {
				savehdrs(buf,htype);
			}
			if ((hptr = hdrlines[H_CLEN].head) !=
			    (struct hdrs *)NULL) {
				clen = atol (hptr->value);
			}
		}
		if (type == ZAP) {
			if (htype != FALSE) {
				pushrest = (lastc != '\n');
				continue;
			}
			/* end of header.  Print non-blank line and bail. */
			Dout(pn, 5, "ZAP end header; n=%d, buf[0] = %d\n", n, buf[0]);
			if (buf[0] != '\n') {
				if (fwrite(buf,1,n,f) != n) {
					sav_errno = errno;
					return(FALSE);
				}
			} else {
				n = 0;
			}
			break;
		}
		/* Copy From line appropriately */
		if (fwrite(buf,1,n-1,f) != n-1)  {
			sav_errno = errno;
			return(FALSE);
		}
		if (lastc != '\n') {
			if (fwrite(&lastc,1,1,f) != 1) {
				sav_errno = errno;
				return(FALSE);
			}
			continue;
		}
		switch(type) {
			case REMOTE:
				if (fprintf(f, rmtmsg, thissys) < 0)
				{
					sav_errno = errno;
					return(FALSE);
				}
				
				break;

			case TTY:
			case ORDINARY:
			default:
				if (fprintf(f, "\n") < 0)
				{
					sav_errno = errno;
					return(FALSE);
				}
				break;
		}
		if ((error > 0) && (dflag == 1)) {
			Dout(pn, 3, "before gendeliv(), uval = '%s'\n", uval);
			gendeliv(f, dflag, uval);
			if (!(ckdlivopts(H_TCOPY, (int*)0) & RETURN)) {
				rtrncont = 0;
			} else {
				/* Account for content-type info */
				/* of returned msg */
				if (fprintf(f, "%s %s\n", header[H_CTYPE].tag,
				    (let[letnum].text == TRUE ? "text/plain" : "application/octet-stream")) < 0)
				{
					sav_errno = errno;
					return(FALSE);
				}

				/* Compute Content-Length of what's being */
				/* returned... */
				i = k;
				/* Account for H_AFWDFROM, H_AFWDCNT, */
				/* H_TCOPY, or H_RECEIVED lines which may */
				/* be added later */
				if (affcnt > 0) {
					sprintf(wbuf, "%d", affcnt);
					i += (affbytecnt
						+ strlen(header[H_AFWDCNT].tag)
						+ strlen(wbuf) + 2);
				}
				if (orig_tcopy) {
				    if ((hptr = hdrlines[H_TCOPY].head) !=
							(struct hdrs *)NULL) {
				        i +=
					  strlen(hdrlines[H_TCOPY].head->value);
				    }
				}
				if ((hptr = hdrlines[H_RECEIVED].head) !=
							(struct hdrs *)NULL) {
				    i += rcvbytecnt;
				}
				/* Add in strlen of MIME-Version:, */
				/* Content-Length: and Content-Type: */
				/* values for msg being returned... */
				if ((hptr = hdrlines[H_MIMEVERS].head) !=
							(struct hdrs *)NULL) {
				    i += strlen(hdrlines[H_MIMEVERS].head->value);
				}
				if ((hptr = hdrlines[H_CTYPE].head) !=
							(struct hdrs *)NULL) {
				    i += strlen(hdrlines[H_CTYPE].head->value);
				}
				if ((hptr = hdrlines[H_CLEN].head) !=
							(struct hdrs *)NULL) {
				    i += strlen(hdrlines[H_CLEN].head->value);
				}
				if (fprintf(f, "%s %ld\n", header[H_CLEN].tag, i) < 0)
				{
					sav_errno = errno;
					return(FALSE);
				}
			}
			if (fprintf(f, "\n") < 0)
			{
				sav_errno = errno;
				return(FALSE);
			}
		}
		if (fflush(f))
		{
			sav_errno = errno;
			return(FALSE);
		}
		
		break;
	}
	/* if not ZAP, copy balance of header */
	n = 0;
	if ((type != ZAP) && rtrncont)
		while (k>0 || n>0) {
			if ((n > 0) && !suppress) {
				if (print_from_struct == TRUE) {
					if (printhdr (type, htype, hptr, f) < 0) {
						return (FALSE);
					}
				} else {
				    if (sel_disp(type, htype, buf) >= 0) {
					if (fwrite(buf,1,n,f) != n)  {
						sav_errno = errno;
						return(FALSE);
					}
				    }
				}
				if (htype == H_DATE) {
					dumprcv(type, htype,&didrcvlines,&suppress,f);
					dumpaff(type, htype,&didafflines,&suppress,f);
				}
			}
			if (k <= 0) {
				/* Can only get here if k=0 && n>0, which occurs */
				/* in a message with header lines but no content. */
				/* If we haven't already done it, force out any */
				/* H_AFWDFROM or H_RECEIVED lines */
				dumprcv(type, -1,&didrcvlines,&suppress,f);
				dumpaff(type, -1,&didafflines,&suppress,f);
				break;
			}
			num = ((k < sizeof(buf)) ? k+1 : sizeof(buf));
			if (fgets (buf, num, tmpf) == NULL) {
				return (FALSE);
			}
			n = strlen (buf);
			k -= n;
			lastc = buf[n-1];

			if (pushrest) {
				pushrest = (lastc != '\n');
				continue;
			}
			sav_suppress = suppress;
			suppress = FALSE;
			print_from_struct = FALSE;
			sav_htype = htype;
			htype = isheader (buf, &ctf);
			Dout(pn, 5, "loop 2: buf = %s, htype= %d/%s\n", buf, htype, header[htype].tag);
			/* The following order is defined in the MTA documents. */
			switch (htype) {
			case H_CONT:
			    if (sending) {
				suppress = sav_suppress;
			    }
			    continue;
			case H_TCOPY:
			case H_MIMEVERS:
			case H_CTYPE:
			case H_CLEN:
				if (!sending) {
					savehdrs(buf,htype);
				}
				hptr = hdrlines[htype].head;
				if (htype == H_CLEN) {
					clen = atol (hptr->value);
				}
				/*
				 * Use values saved in hdrlines[] structure
				 * rather than what was read from tmp file.
				 */
				print_from_struct = TRUE;
				/* FALLTHROUGH */
			case H_EOH:
			case H_AFWDFROM:
			case H_AFWDCNT:
			case H_RECEIVED:
				dumprcv(type, htype,&didrcvlines,&suppress,f);
				dumpaff(type, htype,&didafflines,&suppress,f);
				continue;	/* next header line */
			default:
				pushrest = (lastc != '\n');
				continue;	/* next header line */
			case FALSE:	/* end of header */
				break;
			}

			/* Found the blank line after the headers. */
			if (n > 0) {
				if (fwrite(buf,1,n,f) != n)  {
					sav_errno = errno;
					return(FALSE);
				}
			}

			Dout(pn, 3,", let[%d].text = %s\n",
				letnum, (let[letnum].text ? "TRUE" : "FALSE"));

			if ((type == TTY) && (let[letnum].text == FALSE) && !pflg) {
				if (fprintf (f, "\n%s\n", binmsg) < 0)
				{
					sav_errno = errno;
					return(FALSE);
				}
				return (TRUE);
			}

			if (n == 1 && buf[0] == '\n') {
				n = 0;
			}
			break;
		}

	Dout(pn, 1, "header processed, clen/k/n = %ld/%ld/%d\n", clen, k, n);

	if (clen >= 0) {
		if (((clen - n) == k) || ((clen - n) == (k - 1))) {
			k = clen - n;
		} else {
			/* probable content-length mismatch. show it ALL! */
			Dout(pn, 1, "clen conflict. using k = %ld\n", k);
		}
	}

	/* copy balance of message */
	if (rtrncont)
		while (k > 0) {
			num = ((k < sizeof(buf)) ? k : sizeof(buf));
			if ((n = fread (buf, 1, num, tmpf)) <= 0) {
				Dout(pn, 1, "content-length mismatch. return(FALSE)\n");
				return(FALSE);
			}
			k -= n;
			if (fwrite(buf,1,n,f) != n)  {
				sav_errno = errno;
				return(FALSE);
			}
		}

	Dout(pn, 3, "body processed, k=%ld\n", k);

	if (rtrncont && type != ZAP && type != REMOTE) {
		if (fwrite("\n",1,1,f) != 1)  {
			sav_errno = errno;
			return(FALSE);
		}
	}

	return(TRUE);
}