1 # include <stdio.h>
2 # include <ctype.h>
3 # include <errno.h>
4 # include "postbox.h"
5 
6 static char	SccsId[] = "@(#)collect.c	3.4	03/07/81";
7 
8 /*
9 **  COLLECT -- read & parse message header & make temp file.
10 **
11 **	Creates a temporary file name and copies the standard
12 **	input to that file.  While it is doing it, it looks for
13 **	"From:" and "Sender:" fields to use as the from-person
14 **	(but only if the -a flag is specified).  It prefers to
15 **	to use the "Sender:" field.
16 **
17 **	MIT seems to like to produce "Sent-By:" fields instead
18 **	of "Sender:" fields.  We used to catch this, but it turns
19 **	out that the "Sent-By:" field doesn't always correspond
20 **	to someone real ("___057", for instance), as required by
21 **	the protocol.  So we limp by.....
22 **
23 **	Parameters:
24 **		none
25 **
26 **	Returns:
27 **		Name of temp file.
28 **
29 **	Side Effects:
30 **		Temp file is created and filled.
31 **
32 **	Called By:
33 **		main
34 **
35 **	Notes:
36 **		This is broken off from main largely so that the
37 **		temp buffer can be deallocated.
38 */
39 
40 char	*MsgId;			/* message-id, determined or created */
41 long	MsgSize;		/* size of message in bytes */
42 char	*Date;			/* UNIX-style origination date */
43 
44 char *
45 collect()
46 {
47 	register FILE *tf;
48 	char buf[MAXFIELD+1];
49 	register char *p;
50 	char c;
51 	extern int errno;
52 	register HDR *h;
53 	HDR **hp;
54 	extern bool isheader();
55 	extern char *newstr();
56 	extern char *xalloc();
57 	char *fname;
58 	char *fvalue;
59 	extern char *index(), *rindex();
60 	char *xfrom;
61 	extern char *hvalue();
62 	struct hdrinfo *hi;
63 	extern char *strcpy(), *strcat(), *mktemp();
64 
65 	/*
66 	**  Create the temp file name and create the file.
67 	*/
68 
69 	mktemp(InFileName);
70 	close(creat(InFileName, 0600));
71 	if ((tf = fopen(InFileName, "w")) == NULL)
72 	{
73 		syserr("Cannot create %s", InFileName);
74 		return (NULL);
75 	}
76 
77 	/* try to read a UNIX-style From line */
78 	if (fgets(buf, sizeof buf, stdin) == NULL)
79 		return (NULL);
80 	if (strncmp(buf, "From ", 5) == 0)
81 	{
82 		eatfrom(buf);
83 		fgets(buf, sizeof buf, stdin);
84 	}
85 
86 	/*
87 	**  Copy stdin to temp file & do message editting.
88 	**	To keep certain mailers from getting confused,
89 	**	and to keep the output clean, lines that look
90 	**	like UNIX "From" lines are deleted in the header,
91 	**	and prepended with ">" in the body.
92 	*/
93 
94 	for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin))
95 	{
96 		/* see if the header is over */
97 		if (!isheader(buf))
98 			break;
99 
100 		/* get the rest of this field */
101 		while ((c = getc(stdin)) == ' ' || c == '\t')
102 		{
103 			p = &buf[strlen(buf)];
104 			*p++ = c;
105 			if (fgets(p, sizeof buf - (p - buf), stdin) == NULL)
106 				break;
107 		}
108 		if (c != EOF)
109 			ungetc(c, stdin);
110 
111 		MsgSize += strlen(buf);
112 
113 		/*
114 		**  Snarf header away.
115 		*/
116 
117 		/* strip off trailing newline */
118 		p = rindex(buf, '\n');
119 		if (p != NULL)
120 			*p = '\0';
121 
122 		/* find canonical name */
123 		fname = buf;
124 		p = index(buf, ':');
125 		fvalue = &p[1];
126 		while (isspace(*--p))
127 			continue;
128 		*++p = '\0';
129 		makelower(fname);
130 
131 		/* strip field value on front */
132 		if (*fvalue == ' ')
133 			fvalue++;
134 
135 		/* search header list for this header */
136 		for (hp = &Header, h = Header; h != NULL; hp = &h->h_link, h = h->h_link)
137 		{
138 			if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags))
139 				break;
140 		}
141 
142 		/* see if it is a known type */
143 		for (hi = HdrInfo; hi->hi_field != NULL; hi++)
144 		{
145 			if (strcmp(hi->hi_field, fname) == 0)
146 				break;
147 		}
148 
149 		/* if this means "end of header" quit now */
150 		if (bitset(H_EOH, hi->hi_flags))
151 			break;
152 
153 		/* create/fill in a new node */
154 		if (h == NULL)
155 		{
156 			/* create a new node */
157 			*hp = h = (HDR *) xalloc(sizeof *h);
158 			h->h_field = newstr(fname);
159 			h->h_value = NULL;
160 			h->h_link = NULL;
161 			h->h_flags = hi->hi_flags;
162 		}
163 		if (h->h_value != NULL)
164 			free(h->h_value);
165 		h->h_value = newstr(fvalue);
166 
167 		/* save the location of this field */
168 		if (hi->hi_pptr != NULL)
169 			*hi->hi_pptr = h->h_value;
170 	}
171 
172 # ifdef DEBUG
173 	if (Debug)
174 		printf("EOH\n");
175 # endif DEBUG
176 
177 	/* throw away a blank line */
178 	if (buf[0] == '\n')
179 		fgets(buf, sizeof buf, stdin);
180 
181 	/*
182 	**  Collect the body of the message.
183 	*/
184 
185 	for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL)
186 	{
187 		/* check for end-of-message */
188 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
189 			break;
190 
191 		/* Hide UNIX-like From lines */
192 		if (strncmp(buf, "From ", 5) == 0)
193 		{
194 			fputs(">", tf);
195 			MsgSize++;
196 		}
197 		MsgSize += strlen(buf);
198 		fputs(buf, tf);
199 		if (ferror(tf))
200 		{
201 			if (errno == ENOSPC)
202 			{
203 				freopen(InFileName, "w", tf);
204 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
205 				syserr("Out of disk space for temp file");
206 			}
207 			else
208 				syserr("Cannot write %s", InFileName);
209 			freopen("/dev/null", "w", tf);
210 		}
211 	}
212 	fclose(tf);
213 
214 	/*
215 	**  Find out some information from the headers.
216 	**	Examples are who is the from person & the date.  Some
217 	**	fields, e.g., Message-Id, may have been handled by
218 	**	the hi_pptr mechanism.
219 	*/
220 
221 	/* from person */
222 	xfrom = hvalue("sender");
223 	if (xfrom == NULL)
224 		xfrom = hvalue("from");
225 
226 	/* date message originated */
227 	/* we don't seem to have a good way to do canonical conversion ....
228 	p = hvalue("date");
229 	if (p != NULL)
230 		Date = newstr(arpatounix(p));
231 	.... so we will ignore the problem for the time being */
232 	if (Date == NULL)
233 	{
234 		auto long t;
235 		extern char *ctime();
236 
237 		time(&t);
238 		Date = newstr(ctime(&t));
239 	}
240 
241 	if (freopen(InFileName, "r", stdin) == NULL)
242 		syserr("Cannot reopen %s", InFileName);
243 
244 # ifdef DEBUG
245 	if (Debug)
246 	{
247 		printf("----- collected header -----\n");
248 		for (h = Header; h != NULL; h = h->h_link)
249 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
250 		printf("----------------------------\n");
251 	}
252 # endif DEBUG
253 	return (ArpaFmt ? xfrom : NULL);
254 }
255 /*
256 **  EATFROM -- chew up a UNIX style from line and process
257 **
258 **	This does indeed make some assumptions about the format
259 **	of UNIX messages.
260 **
261 **	Parameters:
262 **		fm -- the from line.
263 **
264 **	Returns:
265 **		none.
266 **
267 **	Side Effects:
268 **		extracts what information it can from the header,
269 **		such as the Date.
270 */
271 
272 char	*MonthList[] =
273 {
274 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
275 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
276 	NULL
277 };
278 
279 eatfrom(fm)
280 	char *fm;
281 {
282 	register char *p;
283 	register char **dt;
284 
285 	/* find the date part */
286 	p = fm;
287 	while (*p != '\0')
288 	{
289 		/* skip a word */
290 		while (*p != '\0' && *p != ' ')
291 			*p++;
292 		while (*p == ' ')
293 			*p++;
294 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
295 			continue;
296 
297 		/* we have a possible date */
298 		for (dt = MonthList; *dt != NULL; dt++)
299 			if (strncmp(*dt, p, 3) == 0)
300 				break;
301 
302 		if (*dt != NULL)
303 			break;
304 	}
305 
306 	if (*p != NULL)
307 	{
308 		/* we have found a date */
309 		Date = xalloc(25);
310 		strncpy(Date, p, 25);
311 		Date[24] = '\0';
312 	}
313 }
314 /*
315 **  HVALUE -- return value of a header.
316 **
317 **	Parameters:
318 **		field -- the field name.
319 **
320 **	Returns:
321 **		pointer to the value part.
322 **		NULL if not found.
323 **
324 **	Side Effects:
325 **		sets the H_USED bit in the header if found.
326 */
327 
328 char *
329 hvalue(field)
330 	char *field;
331 {
332 	register HDR *h;
333 
334 	for (h = Header; h != NULL; h = h->h_link)
335 	{
336 		if (strcmp(h->h_field, field) == 0)
337 		{
338 			h->h_flags |= H_USED;
339 			return (h->h_value);
340 		}
341 	}
342 	return (NULL);
343 }
344 /*
345 **  MAKEMSGID -- Compute a message id for this process.
346 **
347 **	This routine creates a message id for a message if
348 **	it did not have one already.  If the MESSAGEID compile
349 **	flag is set, the messageid will be added to any message
350 **	that does not already have one.  Currently it is more
351 **	of an artifact, but I suggest that if you are hacking,
352 **	you leave it in -- I may want to use it someday if
353 **	duplicate messages turn out to be a problem.
354 **
355 **	Parameters:
356 **		none.
357 **
358 **	Returns:
359 **		a message id.
360 **
361 **	Side Effects:
362 **		none.
363 */
364 
365 char *
366 makemsgid()
367 {
368 	auto long t;
369 	extern char *MyLocName;
370 	extern char *ArpaHost;
371 	static char buf[50];
372 	extern long time();
373 	extern char *sprintf();
374 
375 	time(&t);
376 	sprintf(buf, "<%ld.%d.%s@%s>", t, getpid(), MyLocName, ArpaHost);
377 	return (buf);
378 }
379 /*
380 **  ISHEADER -- predicate telling if argument is a header.
381 **
382 **	Parameters:
383 **		s -- string to check for possible headerness.
384 **
385 **	Returns:
386 **		TRUE if s is a header.
387 **		FALSE otherwise.
388 **
389 **	Side Effects:
390 **		none.
391 */
392 
393 bool
394 isheader(s)
395 	register char *s;
396 {
397 	if (!isalnum(*s))
398 		return (FALSE);
399 	while (!isspace(*s) && *s != ':')
400 		s++;
401 	while (isspace(*s))
402 		s++;
403 	return (*s == ':');
404 }
405