xref: /csrg-svn/libexec/bugfiler/bugfiler.c (revision 15686)
114552Ssam #ifndef lint
2*15686Sralph static char sccsid[] = "@(#)bugfiler.c	4.12 (Berkeley) 12/08/83";
314552Ssam #endif
414552Ssam 
512375Sralph /*
612375Sralph  * Bug report processing program.
714809Ssam  * It is designed to be invoked by alias(5)
814809Ssam  * and to be compatible with mh.
912375Sralph  */
1012375Sralph 
1112375Sralph #include <stdio.h>
1212375Sralph #include <ctype.h>
1312375Sralph #include <signal.h>
1414809Ssam #include <pwd.h>
1514809Ssam 
1612375Sralph #include <sys/types.h>
1712375Sralph #include <sys/stat.h>
1812695Sralph #include <sys/dir.h>
1912375Sralph 
2014809Ssam #define	BUGS_NAME	"4bsd-bugs"
2114809Ssam #define	BUGS_HOME	"%ucbarpa@BERKELEY"
2214809Ssam #define	MAILCMD		"/usr/lib/sendmail -i -t"
2314809Ssam 
2414808Ssam char	unixtomh[] = "/usr/new/lib/mh/unixtomh";
2514809Ssam char	*bugperson = "bugs";
2614809Ssam char	*maildir = "mail";
2712375Sralph char	ackfile[] = ".ack";
2812375Sralph char	errfile[] = ".format";
2912375Sralph char	sumfile[] = "summary";
3012375Sralph char	logfile[] = "errors/log";
3114809Ssam char	redistfile[] = ".redist";
3212375Sralph char	tmpname[] = "BfXXXXXX";
3312375Sralph char	draft[] = "RpXXXXXX";
3414809Ssam char	disttmp[] = "RcXXXXXX";
3512375Sralph 
3612695Sralph char	buf[8192];
3712375Sralph char	folder[MAXNAMLEN];
3812375Sralph int	num;
3912375Sralph int	msg_prot = 0664;
4012375Sralph 
4112375Sralph int	debug;
4212375Sralph 
4312375Sralph char	*index();
4412375Sralph char	*rindex();
4512375Sralph char	*fixaddr();
4614809Ssam char	*any();
4712375Sralph 
4812375Sralph main(argc, argv)
4912375Sralph 	char *argv[];
5012375Sralph {
5112375Sralph 	register char *cp;
5212695Sralph 	register int n;
5312695Sralph 	int pfd[2];
5412375Sralph 
55*15686Sralph 	if (argc > 4) {
5612375Sralph 	usage:
5712695Sralph 		fprintf(stderr, "Usage: bugfiler [-d] [-mmsg_mode] [maildir]\n");
5812375Sralph 		exit(1);
5912375Sralph 	}
6012375Sralph 	while (--argc > 0) {
6112375Sralph 		cp = *++argv;
6212695Sralph 		if (*cp == '-')
6312695Sralph 			switch (cp[1]) {
6412375Sralph 			case 'd':
6512375Sralph 				debug++;
6612375Sralph 				break;
6712695Sralph 
6812695Sralph 			case 'm':	/* set message protection */
6912695Sralph 				n = 0;
7012695Sralph 				for (cp += 2; *cp >= '0' && *cp <= '7'; )
7112695Sralph 					n = (n << 3) + (*cp++ - '0');
7212695Sralph 				msg_prot = n & 0777;
7312695Sralph 				break;
7412695Sralph 
7512375Sralph 			default:
7612375Sralph 				goto usage;
7712375Sralph 			}
7812375Sralph 		else
7912375Sralph 			maildir = cp;
8012375Sralph 	}
8112695Sralph 	if (!debug)
8212695Sralph 		freopen(logfile, "a", stderr);
8312695Sralph 
8414809Ssam 	if (bugperson) {
8514809Ssam 		struct passwd *pwd = getpwnam(bugperson);
8614809Ssam 
8714809Ssam 		if (pwd == NULL) {
8814809Ssam 			fprintf(stderr, "%s: bugs person is unknown\n",
8914809Ssam 			    bugperson);
9014809Ssam 			exit(1);
9114809Ssam 		}
9214809Ssam 		if (chdir(pwd->pw_dir) < 0) {
9314809Ssam 			fprintf(stderr, "can't chdir to %s\n", pwd->pw_dir);
9414809Ssam 			exit(1);
9514809Ssam 		}
96*15686Sralph 		setuid(pwd->pw_uid);
9714809Ssam 	}
9812375Sralph 	if (chdir(maildir) < 0) {
9912375Sralph 		fprintf(stderr, "can't chdir to %s\n", maildir);
10012375Sralph 		exit(1);
10112375Sralph 	}
10212695Sralph 	umask(0);
10312695Sralph 
10412695Sralph #ifdef UNIXCOMP
10512695Sralph 	/*
10612695Sralph 	 * Convert UNIX style mail to mh style by filtering stdin through
10712695Sralph 	 * unixtomh.
10812695Sralph 	 */
10912695Sralph 	if (pipe(pfd) >= 0) {
11012695Sralph 		while ((n = fork()) == -1)
11112695Sralph 			sleep(5);
11212695Sralph 		if (n == 0) {
11312695Sralph 			close(pfd[0]);
11412695Sralph 			dup2(pfd[1], 1);
11512695Sralph 			close(pfd[1]);
11612695Sralph 			execl(unixtomh, "unixtomh", 0);
11712695Sralph 			_exit(127);
11812695Sralph 		}
11912695Sralph 		close(pfd[1]);
12012695Sralph 		dup2(pfd[0], 0);
12112695Sralph 		close(pfd[0]);
12212695Sralph 	}
12312695Sralph #endif
12412695Sralph 	while (process())
12512695Sralph 		;
12612695Sralph 	exit(0);
12712375Sralph }
12812375Sralph 
12912695Sralph /* states */
13012695Sralph 
13112695Sralph #define EOM	0	/* End of message seen */
13212695Sralph #define FLD	1	/* Looking for header lines */
13312695Sralph #define BODY	2	/* Looking for message body lines */
13412695Sralph 
13512375Sralph /* defines used for tag attributes */
13612375Sralph 
13712375Sralph #define H_REQ 01
13812695Sralph #define H_SAV 02
13912695Sralph #define H_HDR 04
14012695Sralph #define H_FND 010
14112375Sralph 
14212375Sralph #define FROM_I headers[0].h_info
14312375Sralph #define SUBJECT_I headers[1].h_info
14412375Sralph #define INDEX &headers[2]
14512375Sralph #define INDEX_I headers[2].h_info
14612375Sralph #define DATE_I headers[3].h_info
14712375Sralph #define MSGID_I headers[4].h_info
14812375Sralph #define REPLYTO_I headers[5].h_info
14914981Sralph #define TO_I headers[6].h_info
15014981Sralph #define CC_I headers[7].h_info
15114981Sralph #define FIX headers[10]
15212375Sralph 
15312375Sralph struct header {
15412375Sralph 	char	*h_tag;
15512375Sralph 	int	h_flags;
15612375Sralph 	char	*h_info;
15712375Sralph } headers[] = {
15812695Sralph 	"From",		H_REQ|H_SAV|H_HDR, 0,
15914981Sralph 	"Subject",	H_REQ|H_SAV, 0,
16012375Sralph 	"Index",	H_REQ|H_SAV, 0,
16112695Sralph 	"Date",		H_SAV|H_HDR, 0,
16212695Sralph 	"Message-Id",	H_SAV|H_HDR, 0,
16312695Sralph 	"Reply-To",	H_SAV|H_HDR, 0,
16412695Sralph 	"To",		H_SAV|H_HDR, 0,
16512695Sralph 	"Cc",		H_SAV|H_HDR, 0,
16612375Sralph 	"Description",	H_REQ,       0,
16714955Sralph 	"Repeat-By",	0,	     0,
16812695Sralph 	"Fix",		0,	     0,
16912375Sralph 	0,	0,	0,
17012375Sralph };
17112375Sralph 
17212695Sralph struct header *findheader();
17312695Sralph 
17412375Sralph process()
17512375Sralph {
17612375Sralph 	register struct header *hp;
17712375Sralph 	register char *cp;
17812695Sralph 	register int c;
17912375Sralph 	char *info;
18012695Sralph 	int state, tmp;
18112695Sralph 	FILE *tfp, *fs;
18212375Sralph 
18312375Sralph 	/*
18412375Sralph 	 * Insure all headers are in a consistent
18512375Sralph 	 * state.  Anything left there is free'd.
18612375Sralph 	 */
18712375Sralph 	for (hp = headers; hp->h_tag; hp++) {
18812695Sralph 		hp->h_flags &= ~H_FND;
18912375Sralph 		if (hp->h_info) {
19012695Sralph 			free(hp->h_info);
19112375Sralph 			hp->h_info = 0;
19212375Sralph 		}
19312375Sralph 	}
19412375Sralph 	/*
19512375Sralph 	 * Read the report and make a copy.  Must conform to RFC822 and
19612375Sralph 	 * be of the form... <tag>: <info>
19712695Sralph 	 * Note that the input is expected to be in mh mail format
19812695Sralph 	 * (i.e., messages are separated by lines of ^A's).
19912375Sralph 	 */
20012695Sralph 	while ((c = getchar()) == '\001' && peekc(stdin) == '\001')
20112695Sralph 		while (getchar() != '\n')
20212695Sralph 			;
20312695Sralph 	if (c == EOF)
20412695Sralph 		return(0);	/* all done */
20512695Sralph 
20612375Sralph 	mktemp(tmpname);
20712695Sralph 	if ((tmp = creat(tmpname, msg_prot)) < 0) {
20812695Sralph 		fprintf(stderr, "cannont create %s\n", tmpname);
20912695Sralph 		exit(1);
21012695Sralph 	}
21112695Sralph 	if ((tfp = fdopen(tmp, "w")) == NULL) {
21212695Sralph 		fprintf(stderr, "cannot fdopen temp file\n");
21312695Sralph 		exit(1);
21412695Sralph 	}
21512695Sralph 
21612695Sralph 	for (state = FLD; state != EOF && state != EOM; c = getchar()) {
21712695Sralph 		switch (state) {
21812695Sralph 		case FLD:
21912695Sralph 			if (c == '\n' || c == '-')
22012695Sralph 				goto body;
22112695Sralph 			for (cp = buf; c != ':'; c = getchar()) {
22212695Sralph 				if (cp < buf+sizeof(buf)-1 && c != '\n' && c != EOF) {
22312695Sralph 					*cp++ = c;
22412695Sralph 					continue;
22512695Sralph 				}
22612695Sralph 				*cp = '\0';
22712695Sralph 				fputs(buf, tfp);
22812695Sralph 				state = EOF;
22912695Sralph 				while (c != EOF) {
23012695Sralph 					if (c == '\n')
23112695Sralph 						if ((tmp = peekc(stdin)) == EOF)
23212695Sralph 							break;
23312695Sralph 						else if (tmp == '\001') {
23412695Sralph 							state = EOM;
23512695Sralph 							break;
23612695Sralph 						}
23712695Sralph 					putc(c, tfp);
23812695Sralph 					c = getchar();
23912695Sralph 				}
24012695Sralph 				fclose(tfp);
24112695Sralph 				goto badfmt;
24212695Sralph 			}
24312695Sralph 			*cp = '\0';
24412695Sralph 			fprintf(tfp, "%s:", buf);
24512695Sralph 			hp = findheader(buf, state);
24612695Sralph 
24712695Sralph 			for (cp = buf; ; ) {
24812695Sralph 				if (cp >= buf+sizeof(buf)-1) {
24912695Sralph 					fprintf(stderr, "field truncated\n");
25012695Sralph 					while ((c = getchar()) != EOF && c != '\n')
25112695Sralph 						putc(c, tfp);
25212695Sralph 				}
25312695Sralph 				if ((c = getchar()) == EOF) {
25412695Sralph 					state = EOF;
25512695Sralph 					break;
25612695Sralph 				}
25712695Sralph 				putc(c, tfp);
25812695Sralph 				*cp++ = c;
25912695Sralph 				if (c == '\n')
26012695Sralph 					if ((c = peekc(stdin)) != ' ' && c != '\t') {
26112695Sralph 						if (c == EOF)
26212695Sralph 							state = EOF;
26312695Sralph 						else if (c == '\001')
26412695Sralph 							state = EOM;
26512695Sralph 						break;
26612695Sralph 					}
26712695Sralph 			}
26812695Sralph 			*cp = '\0';
26912695Sralph 			cp = buf;
27012695Sralph 			break;
27112695Sralph 
27212695Sralph 		body:
27312695Sralph 			state = BODY;
27412695Sralph 		case BODY:
27512695Sralph 			for (cp = buf; ; c = getchar()) {
27612695Sralph 				if (c == EOF) {
27712695Sralph 					state = EOF;
27812695Sralph 					break;
27912695Sralph 				}
28012695Sralph 				if (c == '\001' && peekc(stdin) == '\001') {
28112695Sralph 					state = EOM;
28212695Sralph 					break;
28312695Sralph 				}
28412695Sralph 				putc(c, tfp);
28512695Sralph 				*cp++ = c;
28612695Sralph 				if (cp >= buf+sizeof(buf)-1 || c == '\n')
28712695Sralph 					break;
28812695Sralph 			}
28912695Sralph 			*cp = '\0';
29012695Sralph 			if ((cp = index(buf, ':')) == NULL)
29112695Sralph 				continue;
29212695Sralph 			*cp++ = '\0';
29312695Sralph 			hp = findheader(buf, state);
29412695Sralph 		}
29512695Sralph 
29612695Sralph 		/*
29712695Sralph 		 * Don't save the info if the header wasn't found, we don't
29812695Sralph 		 * care about the info, or the header is repeated.
29912695Sralph 		 */
30012695Sralph 		if (hp == NULL || !(hp->h_flags & H_SAV) || hp->h_info)
30112375Sralph 			continue;
30212375Sralph 		while (isspace(*cp))
30312375Sralph 			cp++;
30412375Sralph 		if (*cp) {
30512375Sralph 			info = cp;
30612375Sralph 			while (*cp++);
30712375Sralph 			cp--;
30812375Sralph 			while (isspace(cp[-1]))
30912375Sralph 				*--cp = '\0';
31012375Sralph 			hp->h_info = (char *) malloc(strlen(info) + 1);
31112695Sralph 			if (hp->h_info == NULL) {
31212695Sralph 				fprintf(stderr, "ran out of memory\n");
31312375Sralph 				continue;
31412695Sralph 			}
31512375Sralph 			strcpy(hp->h_info, info);
31612375Sralph 			if (hp == INDEX)
31712375Sralph 				chkindex(hp);
31812375Sralph 		}
31912375Sralph 	}
32012695Sralph 	fclose(tfp);
32112375Sralph 	/*
32212375Sralph 	 * Verify all the required pieces of information
32312375Sralph 	 * are present.
32412375Sralph 	 */
32512695Sralph 	for (hp = headers; hp->h_tag; hp++) {
32612375Sralph 		/*
32712375Sralph 		 * Mail the bug report back to the sender with a note
32812375Sralph 		 * explaining they must conform to the specification.
32912375Sralph 		 */
33012695Sralph 		if ((hp->h_flags & H_REQ) && !(hp->h_flags & H_FND)) {
33112695Sralph 			if (debug)
33212695Sralph 				printf("Missing %s\n", hp->h_tag);
33312695Sralph 		badfmt:
33412695Sralph 			reply(FROM_I, errfile, tmpname);
33512695Sralph 			file(tmpname, "errors");
33612695Sralph 			return(state == EOM);
33712695Sralph 		}
33812375Sralph 	}
33912375Sralph 	/*
34012695Sralph 	 * Acknowledge receipt.
34112695Sralph 	 */
34212695Sralph 	reply(FROM_I, ackfile, (char *)0);
34312695Sralph 	file(tmpname, folder);
34412695Sralph 	/*
34512375Sralph 	 * Append information about the new bug report
34612375Sralph 	 * to the summary file.
34712375Sralph 	 */
34812695Sralph 	if ((fs = fopen(sumfile, "a")) == NULL)
34912375Sralph 		fprintf(stderr, "Can't open %s\n", sumfile);
35012695Sralph 	else {
35112695Sralph 		fprintf(fs, "%14.14s/%-3d  ", folder, num);
35212695Sralph 		fprintf(fs, "%-51.51s Recv\n", INDEX_I);
35312695Sralph 		fprintf(fs, "\t\t    %-51.51s\n", SUBJECT_I);
35412375Sralph 	}
35512375Sralph 	fclose(fs);
35614809Ssam 	/*
35714809Ssam 	 * Check redistribution list and, if members,
35814809Ssam 	 * mail a copy of the bug report to these people.
35914809Ssam 	 */
36014809Ssam 	redistribute(folder, num);
36112695Sralph 	return(state == EOM);
36212375Sralph }
36312375Sralph 
36412375Sralph /*
36512695Sralph  * Lookup the string in the list of headers and return a pointer
36612695Sralph  * to the entry or NULL.
36712695Sralph  */
36812695Sralph 
36912695Sralph struct header *
37012695Sralph findheader(name, state)
37112695Sralph 	char *name;
37212695Sralph 	int state;
37312695Sralph {
37412695Sralph 	register struct header *hp;
37512695Sralph 
37612695Sralph 	if (debug)
37712695Sralph 		printf("findheader(%s, %d)\n", name, state);
37812695Sralph 
37912695Sralph 	for (hp = headers; hp->h_tag; hp++) {
38012695Sralph 		if (!streq(hp->h_tag, buf))
38112695Sralph 			continue;
38212695Sralph 		if ((hp->h_flags & H_HDR) && state != FLD)
38312695Sralph 			continue;
38412695Sralph 		hp->h_flags |= H_FND;
38512695Sralph 		return(hp);
38612695Sralph 	}
38712695Sralph 	return(NULL);
38812695Sralph }
38912695Sralph 
39012695Sralph /*
39112375Sralph  * Check the format of the Index information.
39212375Sralph  * A side effect is to set the name of the folder if all is well.
39312375Sralph  */
39412375Sralph 
39512375Sralph chkindex(hp)
39612375Sralph 	struct header *hp;
39712375Sralph {
39812695Sralph 	register char *cp1, *cp2;
39912375Sralph 	register char c;
40012375Sralph 	struct stat stbuf;
40112375Sralph 
40212375Sralph 	if (debug)
40312695Sralph 		printf("chkindex(%s)\n", hp->h_info);
40412375Sralph 	/*
40514865Ssam 	 * Strip of leading "/", "usr/", or "src/".
40612695Sralph 	 */
40712695Sralph 	cp1 = hp->h_info;
40812695Sralph 	while (*cp1 == '/')
40912695Sralph 		cp1++;
41014865Ssam 	while (substr(cp1, "usr/") || substr(cp1, "src/"))
41112695Sralph 		cp1 += 4;
41212695Sralph 	/*
41312375Sralph 	 * Read the folder name and remove it from the index line.
41412375Sralph 	 */
41512695Sralph 	for (cp2 = folder; ;) {
41612695Sralph 		switch (c = *cp1++) {
41712695Sralph 		case '/':
41812695Sralph 			if (cp2 == folder)
41912695Sralph 				continue;
42012375Sralph 			break;
42112695Sralph 		case '\0':
42212695Sralph 			cp1--;
42312695Sralph 			break;
42412695Sralph 		case ' ':
42512695Sralph 		case '\t':
42612695Sralph 			while (isspace(*cp1))
42712695Sralph 				cp1++;
42812695Sralph 			break;
42912695Sralph 		default:
43012695Sralph 			if (cp2 < folder+sizeof(folder)-1)
43112695Sralph 				*cp2++ = c;
43212695Sralph 			continue;
43312375Sralph 		}
43412695Sralph 		*cp2 = '\0';
43512695Sralph 		for (cp2 = hp->h_info; *cp2++ = *cp1++; )
43612695Sralph 			;
43712695Sralph 		break;
43812375Sralph 	}
43912695Sralph 	if (debug)
44012695Sralph 		printf("folder = %s\n", folder);
44112375Sralph 	/*
44212375Sralph 	 * Check to make sure we have a valid folder name
44312375Sralph 	 */
44412375Sralph 	if (stat(folder, &stbuf) == 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR)
44512375Sralph 		return;
44612375Sralph 	/*
44712375Sralph 	 * The Index line is not in the correct format so clear
44812695Sralph 	 * the H_FND flag to mail back the correct format.
44912375Sralph 	 */
45012695Sralph 	hp->h_flags &= ~H_FND;
45112375Sralph }
45212375Sralph 
45312375Sralph /*
45412375Sralph  * Move or copy the file msg to the folder (directory).
45514809Ssam  * As a side effect, num is set to the number under which
45614809Ssam  * the message is filed in folder.
45712375Sralph  */
45812375Sralph 
45912375Sralph file(fname, folder)
46012375Sralph 	char *fname, *folder;
46112375Sralph {
46212375Sralph 	register char *cp, n;
46312375Sralph 	char msgname[MAXNAMLEN*2+2];
46412375Sralph 	struct stat stbuf;
46512375Sralph 	DIR *dirp;
46612375Sralph 	struct direct *d;
46712375Sralph 
46812375Sralph 	if (debug)
46912695Sralph 		printf("file(%s, %s)\n", fname, folder);
47012375Sralph 	/*
47112375Sralph 	 * Get the next number to use by finding the last message number
47212375Sralph 	 * in folder and adding one.
47312375Sralph 	 */
47412375Sralph 	if ((dirp = opendir(folder)) == NULL) {
47512375Sralph 		fprintf(stderr, "Cannot open %s/%s\n", maildir, folder);
47612375Sralph 		return;
47712375Sralph 	}
47812375Sralph 	num = 0;
47912375Sralph 	while ((d = readdir(dirp)) != NULL) {
48012375Sralph 		cp = d->d_name;
48112375Sralph 		n = 0;
48212375Sralph 		while (isdigit(*cp))
48312375Sralph 			n = n * 10 + (*cp++ - '0');
48412375Sralph 		if (*cp == '\0' && n > num)
48512375Sralph 			num = n;
48612375Sralph 	}
48712375Sralph 	closedir(dirp);
48812375Sralph 	num++;
48912375Sralph 	/*
49012375Sralph 	 * Create the destination file "folder/num" and copy fname to it.
49112375Sralph 	 */
49212375Sralph 	sprintf(msgname, "%s/%d", folder, num);
49312375Sralph 	if (link(fname, msgname) < 0) {
49412375Sralph 		int fin, fout;
49512375Sralph 
49612695Sralph 		if ((fin = open(fname, 0)) < 0) {
49712695Sralph 			fprintf(stderr, "cannot open %s\n", fname);
49812375Sralph 			return;
49912695Sralph 		}
50012695Sralph 		if ((fout = creat(msgname, msg_prot)) < 0) {
50112695Sralph 			fprintf(stderr, "cannot create %s\n", msgname);
50212375Sralph 			return;
50312695Sralph 		}
50412695Sralph 		while ((n = read(fin, buf, sizeof(buf))) > 0)
50512695Sralph 			write(fout, buf, n);
50612375Sralph 		close(fin);
50712375Sralph 		close(fout);
50812375Sralph 	}
50912375Sralph 	unlink(fname);
51012375Sralph }
51112375Sralph 
51212375Sralph /*
51314809Ssam  * Redistribute a bug report to those people indicated
51414809Ssam  * in the redistribution list file.  Perhaps should also
51514809Ssam  * annotate bug report with this information for future
51614809Ssam  * reference?
51714809Ssam  */
51814809Ssam redistribute(folder, num)
51914809Ssam 	char *folder;
52014809Ssam 	int num;
52114809Ssam {
52214809Ssam 	FILE *fredist, *fbug, *ftemp;
52314809Ssam 	char line[BUFSIZ], bug[2 * MAXNAMLEN + 1];
52414809Ssam 	register char *cp;
52514809Ssam 	int redistcnt, continuation, first;
52614809Ssam 
52714809Ssam 	fredist = fopen(redistfile, "r");
52814809Ssam 	if (fredist == NULL) {
52914809Ssam 		if (debug)
53014809Ssam 			printf("redistribute(%s, %d), no distribution list\n",
53114809Ssam 			    folder, num);
53214809Ssam 		return;
53314809Ssam 	}
53414809Ssam 	continuation = 0;
53514809Ssam 	first = 1;
53614809Ssam 	redistcnt = 0;
53714809Ssam 	while (fgets(line, sizeof (line) - 1, fredist) != NULL) {
53814809Ssam 		if (debug)
53914809Ssam 			printf("%s: %s", redistfile, line);
54014809Ssam 		if (continuation && index(line, '\\'))
54114809Ssam 			continue;
54214809Ssam 		continuation = 0;
54314809Ssam 		cp = any(line, " \t");
54414809Ssam 		if (cp == NULL)
54514809Ssam 			continue;
54614809Ssam 		*cp++ = '\0';
54714809Ssam 		if (strcmp(folder, line) == 0)
54814809Ssam 			goto found;
54914809Ssam 		if (index(cp, '\\'))
55014809Ssam 			continuation = 1;
55114809Ssam 	}
55214809Ssam 	if (debug)
55314809Ssam 		printf("no redistribution list found\n");
55414809Ssam 	fclose(fredist);
55514809Ssam 	return;
55614809Ssam found:
55714809Ssam 	mktemp(disttmp);
55814809Ssam 	ftemp = fopen(disttmp, "w+r");
55914809Ssam 	if (ftemp == NULL) {
56014809Ssam 		if (debug)
56114809Ssam 			printf("%s: couldn't create\n", disttmp);
56214809Ssam 		return;
56314809Ssam 	}
56414809Ssam again:
56514809Ssam 	if (debug)
56614809Ssam 		printf("redistribution list %s", cp);
56714809Ssam 	while (cp) {
56814809Ssam 		char *user, terminator;
56914809Ssam 
57014809Ssam 		while (*cp && (*cp == ' ' || *cp == '\t' || *cp == ','))
57114809Ssam 			cp++;
57214809Ssam 		user = cp, cp = any(cp, ", \t\n\\");
57314809Ssam 		if (cp) {
57414809Ssam 			terminator = *cp;
57514809Ssam 			*cp++ = '\0';
57614809Ssam 			if (terminator == '\n')
57714809Ssam 				cp = 0;
57814809Ssam 			if (terminator == '\\')
57914809Ssam 				continuation++;
58014809Ssam 		}
58114809Ssam 		if (*user == '\0')
58214809Ssam 			continue;
58314809Ssam 		if (debug)
58414809Ssam 			printf("copy to %s\n", user);
58514809Ssam 		if (first) {
58614809Ssam 			fprintf(ftemp, "To: %s", user);
58714809Ssam 			first = 0;
58814809Ssam 		} else
58914809Ssam 			fprintf(ftemp, ", %s", user);
59014809Ssam 		redistcnt++;
59114809Ssam 	}
59214809Ssam 	if (!first)
59314809Ssam 		putc('\n', ftemp);
59414809Ssam 	if (continuation) {
59514809Ssam 		first = 1;
59614809Ssam 		continuation = 0;
59714809Ssam 		cp = line;
59814809Ssam 		if (fgets(line, sizeof (line) - 1, fredist))
59914809Ssam 			goto again;
60014809Ssam 	}
60114809Ssam 	fclose(fredist);
60214809Ssam 	if (redistcnt == 0)
60314809Ssam 		goto cleanup;
60414809Ssam 	fprintf(ftemp, "Subject: ");
60514809Ssam 	if (SUBJECT_I)
60614809Ssam 		fprintf(ftemp, "%s\n", SUBJECT_I);
60714809Ssam 	else
60814809Ssam 		fprintf(ftemp, "Untitled bug report\n");
60914809Ssam 	fprintf(ftemp, "\nRedistributed-by: %s%s\n", BUGS_NAME, BUGS_HOME);
61014809Ssam 	/*
61114809Ssam 	 * Create copy of bug report.  Perhaps we should
61214809Ssam 	 * truncate large messages and just give people
61314809Ssam 	 * a pointer to the original?
61414809Ssam 	 */
61514809Ssam 	sprintf(bug, "%s/%d", folder, num);
61614809Ssam 	fbug = fopen(bug, "r");
61714809Ssam 	if (fbug == NULL) {
61814809Ssam 		if (debug)
61914809Ssam 			printf("%s: disappeared?\n", bug);
62014809Ssam 		goto cleanup;
62114809Ssam 	}
62214809Ssam 	first = 1;
62314809Ssam 	while (fgets(line, sizeof (line) - 1, fbug)) {
62414809Ssam 		/* first blank line indicates start of mesg */
62514809Ssam 		if (first && line[0] == '\n') {
62614809Ssam 			first = 0;
62714809Ssam 			continue;
62814809Ssam 		}
62914809Ssam 		fputs(line, ftemp);
63014809Ssam 	}
63114809Ssam 	fclose(fbug);
63214809Ssam 	if (first) {
63314809Ssam 		if (debug)
63414809Ssam 			printf("empty bug report?\n");
63514809Ssam 		goto cleanup;
63614809Ssam 	}
63714809Ssam 	if (dodeliver(ftemp))
63814809Ssam 		unlink(disttmp);
63914809Ssam 	fclose(ftemp);
64014809Ssam 	return;
64114809Ssam cleanup:
64214809Ssam 	fclose(ftemp);
64314809Ssam 	unlink(disttmp);
64414809Ssam }
64514809Ssam 
64614809Ssam dodeliver(fd)
64714809Ssam 	FILE *fd;
64814809Ssam {
64914809Ssam 	char buf[BUFSIZ], cmd[BUFSIZ];
65014809Ssam 	FILE *pf, *popen();
65114809Ssam 
65214809Ssam 	strcpy(cmd, MAILCMD);
65314809Ssam 	if (debug) {
65414809Ssam 		strcat(cmd, " -v");
65514809Ssam 		printf("dodeliver \"%s\"\n", cmd);
65614809Ssam 	}
65714809Ssam 	pf = popen(cmd, "w");
65814809Ssam 	if (pf == NULL) {
65914809Ssam 		if (debug)
66014809Ssam 			printf("dodeliver, \"%s\" failed\n", cmd);
66114809Ssam 		return (0);
66214809Ssam 	}
66314809Ssam 	rewind(fd);
66414809Ssam 	while (fgets(buf, sizeof (buf) - 1, fd)) {
66514809Ssam 		if (debug)
66614809Ssam 			printf("%s", buf);
66714809Ssam 		(void) fputs(buf, pf);
66814809Ssam 	}
66914809Ssam 	if (debug)
67014809Ssam 		printf("EOF\n");
67114809Ssam 	(void) pclose(pf);
67214809Ssam 	return (1);
67314809Ssam }
67414809Ssam 
67514809Ssam /*
67612375Sralph  * Mail file1 and file2 back to the sender.
67712375Sralph  */
67812375Sralph 
67912375Sralph reply(to, file1, file2)
68012375Sralph 	char	*to, *file1, *file2;
68112375Sralph {
68214809Ssam 	int pfd[2], in, w;
68312375Sralph 	FILE *fout;
68412375Sralph 
68512375Sralph 	if (debug)
68612695Sralph 		printf("reply(%s, %s, %s)\n", to, file1, file2);
68712695Sralph 
68812375Sralph 	/*
68912375Sralph 	 * Create a temporary file to put the message in.
69012375Sralph 	 */
69112375Sralph 	mktemp(draft);
69214809Ssam 	if ((fout = fopen(draft, "w+r")) == NULL) {
69312375Sralph 		fprintf(stderr, "Can't create %s\n", draft);
69412375Sralph 		return;
69512375Sralph 	}
69612375Sralph 	/*
69712375Sralph 	 * Output the proper header information.
69812375Sralph 	 */
69914809Ssam 	fprintf(fout, "Reply-To: %s%s\n", BUGS_NAME, BUGS_HOME);
70014819Skarels 	fprintf(fout, "From: %s%s (Bugs Bunny)\n", BUGS_NAME, BUGS_HOME);
70112375Sralph 	if (REPLYTO_I != NULL)
70212375Sralph 		to = REPLYTO_I;
70312375Sralph 	if ((to = fixaddr(to)) == 0) {
70412375Sralph 		fprintf(stderr, "No one to reply to\n");
70512375Sralph 		return;
70612375Sralph 	}
70712375Sralph 	fprintf(fout, "To: %s\n", to);
70812375Sralph 	if (SUBJECT_I) {
70912375Sralph 		fprintf(fout, "Subject: ");
71012375Sralph 		if ((SUBJECT_I[0] != 'R' && SUBJECT_I[0] != 'r') ||
71112375Sralph 		    (SUBJECT_I[1] != 'E' && SUBJECT_I[1] != 'e') ||
71212375Sralph 		    SUBJECT_I[2] != ':')
71312375Sralph 			fprintf(fout, "Re: ");
71412375Sralph 		fprintf(fout, "%s\n", SUBJECT_I);
71512375Sralph 	}
71612375Sralph 	if (DATE_I) {
71712375Sralph 		fprintf(fout, "In-Acknowledgement-Of: Your message of ");
71812375Sralph 		fprintf(fout, "%s.\n", DATE_I);
71912375Sralph 		if (MSGID_I)
72012375Sralph 			fprintf(fout, "             %s\n", MSGID_I);
72112375Sralph 	}
72214809Ssam 	fprintf(fout, "\n");
72312375Sralph 	if ((in = open(file1, 0)) >= 0) {
72412695Sralph 		while ((w = read(in, buf, sizeof(buf))) > 0)
72512695Sralph 			fwrite(buf, 1, w, fout);
72612375Sralph 		close(in);
72712375Sralph 	}
72812375Sralph 	if (file2 && (in = open(file2, 0)) >= 0) {
72912695Sralph 		while ((w = read(in, buf, sizeof(buf))) > 0)
73012695Sralph 			fwrite(buf, 1, w, fout);
73112375Sralph 		close(in);
73212375Sralph 	}
73314809Ssam 	if (dodeliver(fout))
73414809Ssam 		unlink(draft);
73512375Sralph 	fclose(fout);
73612375Sralph }
73712375Sralph 
73812375Sralph /*
73912375Sralph  * fix names like "xxx (something)" to "xxx" and
74012375Sralph  * "xxx <something>" to "something".
74112375Sralph  */
74212375Sralph 
74312375Sralph char *
74412375Sralph fixaddr(text)
74512375Sralph 	char *text;
74612375Sralph {
74712375Sralph 	register char *cp, *lp, c;
74812375Sralph 	char *tp;
74912375Sralph 
75012375Sralph 	if (!text)
75112375Sralph 		return(0);
75212375Sralph 	for (lp = cp = text; ; ) {
75312375Sralph 		switch (c = *cp++) {
75412375Sralph 		case '(':
75512375Sralph 			while (*cp && *cp++ != ')');
75612375Sralph 			continue;
75712375Sralph 		case '<':
75812375Sralph 			lp = text;
75912375Sralph 		case '>':
76012375Sralph 			continue;
76112375Sralph 		case '\0':
76212375Sralph 			while (lp != text && (*lp == ' ' || *lp == '\t'))
76312375Sralph 				lp--;
76412375Sralph 			*lp = c;
76512375Sralph 			return(text);
76612375Sralph 		}
76712375Sralph 		*lp++ = c;
76812375Sralph 	}
76912375Sralph }
77012375Sralph 
77112375Sralph /*
77212375Sralph  * Compare two strings and convert any upper case letters to lower case.
77312375Sralph  */
77412375Sralph 
77512695Sralph streq(s1, s2)
77612695Sralph 	register char *s1, *s2;
77712375Sralph {
77812375Sralph 	register int c;
77912375Sralph 
78012695Sralph 	while (c = *s1++)
78112695Sralph 		if ((c | 040) != (*s2++ | 040))
78212375Sralph 			return(0);
78312695Sralph 	return(*s2 == '\0');
78412375Sralph }
78512695Sralph 
78612695Sralph /*
78712695Sralph  * Return true if string s2 matches the first part of s1.
78812695Sralph  */
78912695Sralph 
79012695Sralph substr(s1, s2)
79112695Sralph 	register char *s1, *s2;
79212695Sralph {
79312695Sralph 	register int c;
79412695Sralph 
79512695Sralph 	while (c = *s2++)
79612695Sralph 		if (c != *s1++)
79712695Sralph 			return(0);
79812695Sralph 	return(1);
79912695Sralph }
80012695Sralph 
80114809Ssam char *
80214809Ssam any(cp, set)
80314809Ssam 	register char *cp;
80414809Ssam 	char *set;
80514809Ssam {
80614809Ssam 	register char *sp;
80714809Ssam 
80814809Ssam 	if (cp == 0 || set == 0)
80914809Ssam 		return (0);
81014809Ssam 	while (*cp) {
81114809Ssam 		for (sp = set; *sp; sp++)
81214809Ssam 			if (*cp == *sp)
81314809Ssam 				return (cp);
81414809Ssam 		cp++;
81514809Ssam 	}
81614809Ssam 	return ((char *)0);
81714809Ssam }
81814809Ssam 
81912695Sralph peekc(fp)
82012695Sralph FILE *fp;
82112695Sralph {
82212695Sralph 	register c;
82312695Sralph 
82412695Sralph 	c = getc(fp);
82512695Sralph 	ungetc(c, fp);
82612695Sralph 	return(c);
82712695Sralph }
828