xref: /csrg-svn/libexec/bugfiler/bugfiler.c (revision 14819)
114552Ssam #ifndef lint
2*14819Skarels static char sccsid[] = "@(#)bugfiler.c	4.8 (Berkeley) 08/23/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 
5512375Sralph 	if (argc > 3) {
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 		}
9614809Ssam 	}
9712375Sralph 	if (chdir(maildir) < 0) {
9812375Sralph 		fprintf(stderr, "can't chdir to %s\n", maildir);
9912375Sralph 		exit(1);
10012375Sralph 	}
10112695Sralph 	umask(0);
10212695Sralph 
10312695Sralph #ifdef UNIXCOMP
10412695Sralph 	/*
10512695Sralph 	 * Convert UNIX style mail to mh style by filtering stdin through
10612695Sralph 	 * unixtomh.
10712695Sralph 	 */
10812695Sralph 	if (pipe(pfd) >= 0) {
10912695Sralph 		while ((n = fork()) == -1)
11012695Sralph 			sleep(5);
11112695Sralph 		if (n == 0) {
11212695Sralph 			close(pfd[0]);
11312695Sralph 			dup2(pfd[1], 1);
11412695Sralph 			close(pfd[1]);
11512695Sralph 			execl(unixtomh, "unixtomh", 0);
11612695Sralph 			_exit(127);
11712695Sralph 		}
11812695Sralph 		close(pfd[1]);
11912695Sralph 		dup2(pfd[0], 0);
12012695Sralph 		close(pfd[0]);
12112695Sralph 	}
12212695Sralph #endif
12312695Sralph 	while (process())
12412695Sralph 		;
12512695Sralph 	exit(0);
12612375Sralph }
12712375Sralph 
12812695Sralph /* states */
12912695Sralph 
13012695Sralph #define EOM	0	/* End of message seen */
13112695Sralph #define FLD	1	/* Looking for header lines */
13212695Sralph #define BODY	2	/* Looking for message body lines */
13312695Sralph 
13412375Sralph /* defines used for tag attributes */
13512375Sralph 
13612375Sralph #define H_REQ 01
13712695Sralph #define H_SAV 02
13812695Sralph #define H_HDR 04
13912695Sralph #define H_FND 010
14012375Sralph 
14112375Sralph #define FROM_I headers[0].h_info
14212375Sralph #define SUBJECT_I headers[1].h_info
14312375Sralph #define INDEX &headers[2]
14412375Sralph #define INDEX_I headers[2].h_info
14512375Sralph #define DATE_I headers[3].h_info
14612375Sralph #define MSGID_I headers[4].h_info
14712375Sralph #define REPLYTO_I headers[5].h_info
14812375Sralph #define RETURNPATH_I headers[6].h_info
14912375Sralph #define TO_I headers[7].h_info
15012375Sralph #define CC_I headers[8].h_info
15112375Sralph #define FIX headers[11]
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,
15912695Sralph 	"Subject",	H_REQ|H_SAV|H_HDR, 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 	"Return-Path",	H_SAV|H_HDR, 0,
16512695Sralph 	"To",		H_SAV|H_HDR, 0,
16612695Sralph 	"Cc",		H_SAV|H_HDR, 0,
16712375Sralph 	"Description",	H_REQ,       0,
16812375Sralph 	"Repeat-By",	H_REQ,	     0,
16912695Sralph 	"Fix",		0,	     0,
17012375Sralph 	0,	0,	0,
17112375Sralph };
17212375Sralph 
17312695Sralph struct header *findheader();
17412695Sralph 
17512375Sralph process()
17612375Sralph {
17712375Sralph 	register struct header *hp;
17812375Sralph 	register char *cp;
17912695Sralph 	register int c;
18012375Sralph 	char *info;
18112695Sralph 	int state, tmp;
18212695Sralph 	FILE *tfp, *fs;
18312375Sralph 
18412375Sralph 	/*
18512375Sralph 	 * Insure all headers are in a consistent
18612375Sralph 	 * state.  Anything left there is free'd.
18712375Sralph 	 */
18812375Sralph 	for (hp = headers; hp->h_tag; hp++) {
18912695Sralph 		hp->h_flags &= ~H_FND;
19012375Sralph 		if (hp->h_info) {
19112695Sralph 			free(hp->h_info);
19212375Sralph 			hp->h_info = 0;
19312375Sralph 		}
19412375Sralph 	}
19512375Sralph 	/*
19612375Sralph 	 * Read the report and make a copy.  Must conform to RFC822 and
19712375Sralph 	 * be of the form... <tag>: <info>
19812695Sralph 	 * Note that the input is expected to be in mh mail format
19912695Sralph 	 * (i.e., messages are separated by lines of ^A's).
20012375Sralph 	 */
20112695Sralph 	while ((c = getchar()) == '\001' && peekc(stdin) == '\001')
20212695Sralph 		while (getchar() != '\n')
20312695Sralph 			;
20412695Sralph 	if (c == EOF)
20512695Sralph 		return(0);	/* all done */
20612695Sralph 
20712375Sralph 	mktemp(tmpname);
20812695Sralph 	if ((tmp = creat(tmpname, msg_prot)) < 0) {
20912695Sralph 		fprintf(stderr, "cannont create %s\n", tmpname);
21012695Sralph 		exit(1);
21112695Sralph 	}
21212695Sralph 	if ((tfp = fdopen(tmp, "w")) == NULL) {
21312695Sralph 		fprintf(stderr, "cannot fdopen temp file\n");
21412695Sralph 		exit(1);
21512695Sralph 	}
21612695Sralph 
21712695Sralph 	for (state = FLD; state != EOF && state != EOM; c = getchar()) {
21812695Sralph 		switch (state) {
21912695Sralph 		case FLD:
22012695Sralph 			if (c == '\n' || c == '-')
22112695Sralph 				goto body;
22212695Sralph 			for (cp = buf; c != ':'; c = getchar()) {
22312695Sralph 				if (cp < buf+sizeof(buf)-1 && c != '\n' && c != EOF) {
22412695Sralph 					*cp++ = c;
22512695Sralph 					continue;
22612695Sralph 				}
22712695Sralph 				*cp = '\0';
22812695Sralph 				fputs(buf, tfp);
22912695Sralph 				state = EOF;
23012695Sralph 				while (c != EOF) {
23112695Sralph 					if (c == '\n')
23212695Sralph 						if ((tmp = peekc(stdin)) == EOF)
23312695Sralph 							break;
23412695Sralph 						else if (tmp == '\001') {
23512695Sralph 							state = EOM;
23612695Sralph 							break;
23712695Sralph 						}
23812695Sralph 					putc(c, tfp);
23912695Sralph 					c = getchar();
24012695Sralph 				}
24112695Sralph 				fclose(tfp);
24212695Sralph 				goto badfmt;
24312695Sralph 			}
24412695Sralph 			*cp = '\0';
24512695Sralph 			fprintf(tfp, "%s:", buf);
24612695Sralph 			hp = findheader(buf, state);
24712695Sralph 
24812695Sralph 			for (cp = buf; ; ) {
24912695Sralph 				if (cp >= buf+sizeof(buf)-1) {
25012695Sralph 					fprintf(stderr, "field truncated\n");
25112695Sralph 					while ((c = getchar()) != EOF && c != '\n')
25212695Sralph 						putc(c, tfp);
25312695Sralph 				}
25412695Sralph 				if ((c = getchar()) == EOF) {
25512695Sralph 					state = EOF;
25612695Sralph 					break;
25712695Sralph 				}
25812695Sralph 				putc(c, tfp);
25912695Sralph 				*cp++ = c;
26012695Sralph 				if (c == '\n')
26112695Sralph 					if ((c = peekc(stdin)) != ' ' && c != '\t') {
26212695Sralph 						if (c == EOF)
26312695Sralph 							state = EOF;
26412695Sralph 						else if (c == '\001')
26512695Sralph 							state = EOM;
26612695Sralph 						break;
26712695Sralph 					}
26812695Sralph 			}
26912695Sralph 			*cp = '\0';
27012695Sralph 			cp = buf;
27112695Sralph 			break;
27212695Sralph 
27312695Sralph 		body:
27412695Sralph 			state = BODY;
27512695Sralph 		case BODY:
27612695Sralph 			for (cp = buf; ; c = getchar()) {
27712695Sralph 				if (c == EOF) {
27812695Sralph 					state = EOF;
27912695Sralph 					break;
28012695Sralph 				}
28112695Sralph 				if (c == '\001' && peekc(stdin) == '\001') {
28212695Sralph 					state = EOM;
28312695Sralph 					break;
28412695Sralph 				}
28512695Sralph 				putc(c, tfp);
28612695Sralph 				*cp++ = c;
28712695Sralph 				if (cp >= buf+sizeof(buf)-1 || c == '\n')
28812695Sralph 					break;
28912695Sralph 			}
29012695Sralph 			*cp = '\0';
29112695Sralph 			if ((cp = index(buf, ':')) == NULL)
29212695Sralph 				continue;
29312695Sralph 			*cp++ = '\0';
29412695Sralph 			hp = findheader(buf, state);
29512695Sralph 		}
29612695Sralph 
29712695Sralph 		/*
29812695Sralph 		 * Don't save the info if the header wasn't found, we don't
29912695Sralph 		 * care about the info, or the header is repeated.
30012695Sralph 		 */
30112695Sralph 		if (hp == NULL || !(hp->h_flags & H_SAV) || hp->h_info)
30212375Sralph 			continue;
30312375Sralph 		while (isspace(*cp))
30412375Sralph 			cp++;
30512375Sralph 		if (*cp) {
30612375Sralph 			info = cp;
30712375Sralph 			while (*cp++);
30812375Sralph 			cp--;
30912375Sralph 			while (isspace(cp[-1]))
31012375Sralph 				*--cp = '\0';
31112375Sralph 			hp->h_info = (char *) malloc(strlen(info) + 1);
31212695Sralph 			if (hp->h_info == NULL) {
31312695Sralph 				fprintf(stderr, "ran out of memory\n");
31412375Sralph 				continue;
31512695Sralph 			}
31612375Sralph 			strcpy(hp->h_info, info);
31712375Sralph 			if (hp == INDEX)
31812375Sralph 				chkindex(hp);
31912375Sralph 		}
32012375Sralph 	}
32112695Sralph 	fclose(tfp);
32212375Sralph 	/*
32312375Sralph 	 * Verify all the required pieces of information
32412375Sralph 	 * are present.
32512375Sralph 	 */
32612695Sralph 	for (hp = headers; hp->h_tag; hp++) {
32712375Sralph 		/*
32812375Sralph 		 * Mail the bug report back to the sender with a note
32912375Sralph 		 * explaining they must conform to the specification.
33012375Sralph 		 */
33112695Sralph 		if ((hp->h_flags & H_REQ) && !(hp->h_flags & H_FND)) {
33212695Sralph 			if (debug)
33312695Sralph 				printf("Missing %s\n", hp->h_tag);
33412695Sralph 		badfmt:
33512695Sralph 			reply(FROM_I, errfile, tmpname);
33612695Sralph 			file(tmpname, "errors");
33712695Sralph 			return(state == EOM);
33812695Sralph 		}
33912375Sralph 	}
34012375Sralph 	/*
34112695Sralph 	 * Acknowledge receipt.
34212695Sralph 	 */
34312695Sralph 	reply(FROM_I, ackfile, (char *)0);
34412695Sralph 	file(tmpname, folder);
34512695Sralph 	/*
34612375Sralph 	 * Append information about the new bug report
34712375Sralph 	 * to the summary file.
34812375Sralph 	 */
34912695Sralph 	if ((fs = fopen(sumfile, "a")) == NULL)
35012375Sralph 		fprintf(stderr, "Can't open %s\n", sumfile);
35112695Sralph 	else {
35212695Sralph 		fprintf(fs, "%14.14s/%-3d  ", folder, num);
35312695Sralph 		fprintf(fs, "%-51.51s Recv\n", INDEX_I);
35412695Sralph 		fprintf(fs, "\t\t    %-51.51s\n", SUBJECT_I);
35512375Sralph 	}
35612375Sralph 	fclose(fs);
35714809Ssam 	/*
35814809Ssam 	 * Check redistribution list and, if members,
35914809Ssam 	 * mail a copy of the bug report to these people.
36014809Ssam 	 */
36114809Ssam 	redistribute(folder, num);
36212695Sralph 	return(state == EOM);
36312375Sralph }
36412375Sralph 
36512375Sralph /*
36612695Sralph  * Lookup the string in the list of headers and return a pointer
36712695Sralph  * to the entry or NULL.
36812695Sralph  */
36912695Sralph 
37012695Sralph struct header *
37112695Sralph findheader(name, state)
37212695Sralph 	char *name;
37312695Sralph 	int state;
37412695Sralph {
37512695Sralph 	register struct header *hp;
37612695Sralph 
37712695Sralph 	if (debug)
37812695Sralph 		printf("findheader(%s, %d)\n", name, state);
37912695Sralph 
38012695Sralph 	for (hp = headers; hp->h_tag; hp++) {
38112695Sralph 		if (!streq(hp->h_tag, buf))
38212695Sralph 			continue;
38312695Sralph 		if ((hp->h_flags & H_HDR) && state != FLD)
38412695Sralph 			continue;
38512695Sralph 		hp->h_flags |= H_FND;
38612695Sralph 		return(hp);
38712695Sralph 	}
38812695Sralph 	return(NULL);
38912695Sralph }
39012695Sralph 
39112695Sralph /*
39212375Sralph  * Check the format of the Index information.
39312375Sralph  * A side effect is to set the name of the folder if all is well.
39412375Sralph  */
39512375Sralph 
39612375Sralph chkindex(hp)
39712375Sralph 	struct header *hp;
39812375Sralph {
39912695Sralph 	register char *cp1, *cp2;
40012375Sralph 	register char c;
40112375Sralph 	struct stat stbuf;
40212375Sralph 
40312375Sralph 	if (debug)
40412695Sralph 		printf("chkindex(%s)\n", hp->h_info);
40512375Sralph 	/*
40612695Sralph 	 * Strip of leading "/", "usr/", "src/" or "sys/".
40712695Sralph 	 */
40812695Sralph 	cp1 = hp->h_info;
40912695Sralph 	while (*cp1 == '/')
41012695Sralph 		cp1++;
41112695Sralph 	while (substr(cp1, "usr/") || substr(cp1, "src/") || substr(cp1, "sys/"))
41212695Sralph 		cp1 += 4;
41314809Ssam 	/*
41414809Ssam 	 * Don't toss "sys/" if nothing else is given for
41514809Ssam 	 * a folder name, this is a valid folder as well.
41614809Ssam 	 */
41714809Ssam 	if (index(cp1, '/') == NULL && substr(cp1 - 4, "sys/"))
41814809Ssam 		cp1 -= 4;
41912695Sralph 	/*
42012375Sralph 	 * Read the folder name and remove it from the index line.
42112375Sralph 	 */
42212695Sralph 	for (cp2 = folder; ;) {
42312695Sralph 		switch (c = *cp1++) {
42412695Sralph 		case '/':
42512695Sralph 			if (cp2 == folder)
42612695Sralph 				continue;
42712375Sralph 			break;
42812695Sralph 		case '\0':
42912695Sralph 			cp1--;
43012695Sralph 			break;
43112695Sralph 		case ' ':
43212695Sralph 		case '\t':
43312695Sralph 			while (isspace(*cp1))
43412695Sralph 				cp1++;
43512695Sralph 			break;
43612695Sralph 		default:
43712695Sralph 			if (cp2 < folder+sizeof(folder)-1)
43812695Sralph 				*cp2++ = c;
43912695Sralph 			continue;
44012375Sralph 		}
44112695Sralph 		*cp2 = '\0';
44212695Sralph 		for (cp2 = hp->h_info; *cp2++ = *cp1++; )
44312695Sralph 			;
44412695Sralph 		break;
44512375Sralph 	}
44612695Sralph 	if (debug)
44712695Sralph 		printf("folder = %s\n", folder);
44812375Sralph 	/*
44912375Sralph 	 * Check to make sure we have a valid folder name
45012375Sralph 	 */
45112375Sralph 	if (stat(folder, &stbuf) == 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR)
45212375Sralph 		return;
45312375Sralph 	/*
45412375Sralph 	 * The Index line is not in the correct format so clear
45512695Sralph 	 * the H_FND flag to mail back the correct format.
45612375Sralph 	 */
45712695Sralph 	hp->h_flags &= ~H_FND;
45812375Sralph }
45912375Sralph 
46012375Sralph /*
46112375Sralph  * Move or copy the file msg to the folder (directory).
46214809Ssam  * As a side effect, num is set to the number under which
46314809Ssam  * the message is filed in folder.
46412375Sralph  */
46512375Sralph 
46612375Sralph file(fname, folder)
46712375Sralph 	char *fname, *folder;
46812375Sralph {
46912375Sralph 	register char *cp, n;
47012375Sralph 	char msgname[MAXNAMLEN*2+2];
47112375Sralph 	struct stat stbuf;
47212375Sralph 	DIR *dirp;
47312375Sralph 	struct direct *d;
47412375Sralph 
47512375Sralph 	if (debug)
47612695Sralph 		printf("file(%s, %s)\n", fname, folder);
47712375Sralph 	/*
47812375Sralph 	 * Get the next number to use by finding the last message number
47912375Sralph 	 * in folder and adding one.
48012375Sralph 	 */
48112375Sralph 	if ((dirp = opendir(folder)) == NULL) {
48212375Sralph 		fprintf(stderr, "Cannot open %s/%s\n", maildir, folder);
48312375Sralph 		return;
48412375Sralph 	}
48512375Sralph 	num = 0;
48612375Sralph 	while ((d = readdir(dirp)) != NULL) {
48712375Sralph 		cp = d->d_name;
48812375Sralph 		n = 0;
48912375Sralph 		while (isdigit(*cp))
49012375Sralph 			n = n * 10 + (*cp++ - '0');
49112375Sralph 		if (*cp == '\0' && n > num)
49212375Sralph 			num = n;
49312375Sralph 	}
49412375Sralph 	closedir(dirp);
49512375Sralph 	num++;
49612375Sralph 	/*
49712375Sralph 	 * Create the destination file "folder/num" and copy fname to it.
49812375Sralph 	 */
49912375Sralph 	sprintf(msgname, "%s/%d", folder, num);
50012375Sralph 	if (link(fname, msgname) < 0) {
50112375Sralph 		int fin, fout;
50212375Sralph 
50312695Sralph 		if ((fin = open(fname, 0)) < 0) {
50412695Sralph 			fprintf(stderr, "cannot open %s\n", fname);
50512375Sralph 			return;
50612695Sralph 		}
50712695Sralph 		if ((fout = creat(msgname, msg_prot)) < 0) {
50812695Sralph 			fprintf(stderr, "cannot create %s\n", msgname);
50912375Sralph 			return;
51012695Sralph 		}
51112695Sralph 		while ((n = read(fin, buf, sizeof(buf))) > 0)
51212695Sralph 			write(fout, buf, n);
51312375Sralph 		close(fin);
51412375Sralph 		close(fout);
51512375Sralph 	}
51612375Sralph 	unlink(fname);
51712375Sralph }
51812375Sralph 
51912375Sralph /*
52014809Ssam  * Redistribute a bug report to those people indicated
52114809Ssam  * in the redistribution list file.  Perhaps should also
52214809Ssam  * annotate bug report with this information for future
52314809Ssam  * reference?
52414809Ssam  */
52514809Ssam redistribute(folder, num)
52614809Ssam 	char *folder;
52714809Ssam 	int num;
52814809Ssam {
52914809Ssam 	FILE *fredist, *fbug, *ftemp;
53014809Ssam 	char line[BUFSIZ], bug[2 * MAXNAMLEN + 1];
53114809Ssam 	register char *cp;
53214809Ssam 	int redistcnt, continuation, first;
53314809Ssam 
53414809Ssam 	fredist = fopen(redistfile, "r");
53514809Ssam 	if (fredist == NULL) {
53614809Ssam 		if (debug)
53714809Ssam 			printf("redistribute(%s, %d), no distribution list\n",
53814809Ssam 			    folder, num);
53914809Ssam 		return;
54014809Ssam 	}
54114809Ssam 	continuation = 0;
54214809Ssam 	first = 1;
54314809Ssam 	redistcnt = 0;
54414809Ssam 	while (fgets(line, sizeof (line) - 1, fredist) != NULL) {
54514809Ssam 		if (debug)
54614809Ssam 			printf("%s: %s", redistfile, line);
54714809Ssam 		if (continuation && index(line, '\\'))
54814809Ssam 			continue;
54914809Ssam 		continuation = 0;
55014809Ssam 		cp = any(line, " \t");
55114809Ssam 		if (cp == NULL)
55214809Ssam 			continue;
55314809Ssam 		*cp++ = '\0';
55414809Ssam 		if (strcmp(folder, line) == 0)
55514809Ssam 			goto found;
55614809Ssam 		if (index(cp, '\\'))
55714809Ssam 			continuation = 1;
55814809Ssam 	}
55914809Ssam 	if (debug)
56014809Ssam 		printf("no redistribution list found\n");
56114809Ssam 	fclose(fredist);
56214809Ssam 	return;
56314809Ssam found:
56414809Ssam 	mktemp(disttmp);
56514809Ssam 	ftemp = fopen(disttmp, "w+r");
56614809Ssam 	if (ftemp == NULL) {
56714809Ssam 		if (debug)
56814809Ssam 			printf("%s: couldn't create\n", disttmp);
56914809Ssam 		return;
57014809Ssam 	}
57114809Ssam again:
57214809Ssam 	if (debug)
57314809Ssam 		printf("redistribution list %s", cp);
57414809Ssam 	while (cp) {
57514809Ssam 		char *user, terminator;
57614809Ssam 
57714809Ssam 		while (*cp && (*cp == ' ' || *cp == '\t' || *cp == ','))
57814809Ssam 			cp++;
57914809Ssam 		user = cp, cp = any(cp, ", \t\n\\");
58014809Ssam 		if (cp) {
58114809Ssam 			terminator = *cp;
58214809Ssam 			*cp++ = '\0';
58314809Ssam 			if (terminator == '\n')
58414809Ssam 				cp = 0;
58514809Ssam 			if (terminator == '\\')
58614809Ssam 				continuation++;
58714809Ssam 		}
58814809Ssam 		if (*user == '\0')
58914809Ssam 			continue;
59014809Ssam 		if (debug)
59114809Ssam 			printf("copy to %s\n", user);
59214809Ssam 		if (first) {
59314809Ssam 			fprintf(ftemp, "To: %s", user);
59414809Ssam 			first = 0;
59514809Ssam 		} else
59614809Ssam 			fprintf(ftemp, ", %s", user);
59714809Ssam 		redistcnt++;
59814809Ssam 	}
59914809Ssam 	if (!first)
60014809Ssam 		putc('\n', ftemp);
60114809Ssam 	if (continuation) {
60214809Ssam 		first = 1;
60314809Ssam 		continuation = 0;
60414809Ssam 		cp = line;
60514809Ssam 		if (fgets(line, sizeof (line) - 1, fredist))
60614809Ssam 			goto again;
60714809Ssam 	}
60814809Ssam 	fclose(fredist);
60914809Ssam 	if (redistcnt == 0)
61014809Ssam 		goto cleanup;
61114809Ssam 	fprintf(ftemp, "Subject: ");
61214809Ssam 	if (SUBJECT_I)
61314809Ssam 		fprintf(ftemp, "%s\n", SUBJECT_I);
61414809Ssam 	else
61514809Ssam 		fprintf(ftemp, "Untitled bug report\n");
61614809Ssam 	fprintf(ftemp, "\nRedistributed-by: %s%s\n", BUGS_NAME, BUGS_HOME);
61714809Ssam 	/*
61814809Ssam 	 * Create copy of bug report.  Perhaps we should
61914809Ssam 	 * truncate large messages and just give people
62014809Ssam 	 * a pointer to the original?
62114809Ssam 	 */
62214809Ssam 	sprintf(bug, "%s/%d", folder, num);
62314809Ssam 	fbug = fopen(bug, "r");
62414809Ssam 	if (fbug == NULL) {
62514809Ssam 		if (debug)
62614809Ssam 			printf("%s: disappeared?\n", bug);
62714809Ssam 		goto cleanup;
62814809Ssam 	}
62914809Ssam 	first = 1;
63014809Ssam 	while (fgets(line, sizeof (line) - 1, fbug)) {
63114809Ssam 		/* first blank line indicates start of mesg */
63214809Ssam 		if (first && line[0] == '\n') {
63314809Ssam 			first = 0;
63414809Ssam 			continue;
63514809Ssam 		}
63614809Ssam 		fputs(line, ftemp);
63714809Ssam 	}
63814809Ssam 	fclose(fbug);
63914809Ssam 	if (first) {
64014809Ssam 		if (debug)
64114809Ssam 			printf("empty bug report?\n");
64214809Ssam 		goto cleanup;
64314809Ssam 	}
64414809Ssam 	if (dodeliver(ftemp))
64514809Ssam 		unlink(disttmp);
64614809Ssam 	fclose(ftemp);
64714809Ssam 	return;
64814809Ssam cleanup:
64914809Ssam 	fclose(ftemp);
65014809Ssam 	unlink(disttmp);
65114809Ssam }
65214809Ssam 
65314809Ssam dodeliver(fd)
65414809Ssam 	FILE *fd;
65514809Ssam {
65614809Ssam 	char buf[BUFSIZ], cmd[BUFSIZ];
65714809Ssam 	FILE *pf, *popen();
65814809Ssam 
65914809Ssam 	strcpy(cmd, MAILCMD);
66014809Ssam 	if (debug) {
66114809Ssam 		strcat(cmd, " -v");
66214809Ssam 		printf("dodeliver \"%s\"\n", cmd);
66314809Ssam 	}
66414809Ssam 	pf = popen(cmd, "w");
66514809Ssam 	if (pf == NULL) {
66614809Ssam 		if (debug)
66714809Ssam 			printf("dodeliver, \"%s\" failed\n", cmd);
66814809Ssam 		return (0);
66914809Ssam 	}
67014809Ssam 	rewind(fd);
67114809Ssam 	while (fgets(buf, sizeof (buf) - 1, fd)) {
67214809Ssam 		if (debug)
67314809Ssam 			printf("%s", buf);
67414809Ssam 		(void) fputs(buf, pf);
67514809Ssam 	}
67614809Ssam 	if (debug)
67714809Ssam 		printf("EOF\n");
67814809Ssam 	(void) pclose(pf);
67914809Ssam 	return (1);
68014809Ssam }
68114809Ssam 
68214809Ssam /*
68312375Sralph  * Mail file1 and file2 back to the sender.
68412375Sralph  */
68512375Sralph 
68612375Sralph reply(to, file1, file2)
68712375Sralph 	char	*to, *file1, *file2;
68812375Sralph {
68914809Ssam 	int pfd[2], in, w;
69012375Sralph 	FILE *fout;
69112375Sralph 
69212375Sralph 	if (debug)
69312695Sralph 		printf("reply(%s, %s, %s)\n", to, file1, file2);
69412695Sralph 
69512375Sralph 	/*
69612375Sralph 	 * Create a temporary file to put the message in.
69712375Sralph 	 */
69812375Sralph 	mktemp(draft);
69914809Ssam 	if ((fout = fopen(draft, "w+r")) == NULL) {
70012375Sralph 		fprintf(stderr, "Can't create %s\n", draft);
70112375Sralph 		return;
70212375Sralph 	}
70312375Sralph 	/*
70412375Sralph 	 * Output the proper header information.
70512375Sralph 	 */
70614809Ssam 	fprintf(fout, "Reply-To: %s%s\n", BUGS_NAME, BUGS_HOME);
707*14819Skarels 	fprintf(fout, "From: %s%s (Bugs Bunny)\n", BUGS_NAME, BUGS_HOME);
70812375Sralph 	if (RETURNPATH_I != NULL)
70912375Sralph 		to = RETURNPATH_I;
71012375Sralph 	if (REPLYTO_I != NULL)
71112375Sralph 		to = REPLYTO_I;
71212375Sralph 	if ((to = fixaddr(to)) == 0) {
71312375Sralph 		fprintf(stderr, "No one to reply to\n");
71412375Sralph 		return;
71512375Sralph 	}
71612375Sralph 	fprintf(fout, "To: %s\n", to);
71712375Sralph 	if (SUBJECT_I) {
71812375Sralph 		fprintf(fout, "Subject: ");
71912375Sralph 		if ((SUBJECT_I[0] != 'R' && SUBJECT_I[0] != 'r') ||
72012375Sralph 		    (SUBJECT_I[1] != 'E' && SUBJECT_I[1] != 'e') ||
72112375Sralph 		    SUBJECT_I[2] != ':')
72212375Sralph 			fprintf(fout, "Re: ");
72312375Sralph 		fprintf(fout, "%s\n", SUBJECT_I);
72412375Sralph 	}
72512375Sralph 	if (DATE_I) {
72612375Sralph 		fprintf(fout, "In-Acknowledgement-Of: Your message of ");
72712375Sralph 		fprintf(fout, "%s.\n", DATE_I);
72812375Sralph 		if (MSGID_I)
72912375Sralph 			fprintf(fout, "             %s\n", MSGID_I);
73012375Sralph 	}
73114809Ssam 	fprintf(fout, "\n");
73212375Sralph 	if ((in = open(file1, 0)) >= 0) {
73312695Sralph 		while ((w = read(in, buf, sizeof(buf))) > 0)
73412695Sralph 			fwrite(buf, 1, w, fout);
73512375Sralph 		close(in);
73612375Sralph 	}
73712375Sralph 	if (file2 && (in = open(file2, 0)) >= 0) {
73812695Sralph 		while ((w = read(in, buf, sizeof(buf))) > 0)
73912695Sralph 			fwrite(buf, 1, w, fout);
74012375Sralph 		close(in);
74112375Sralph 	}
74214809Ssam 	if (dodeliver(fout))
74314809Ssam 		unlink(draft);
74412375Sralph 	fclose(fout);
74512375Sralph }
74612375Sralph 
74712375Sralph /*
74812375Sralph  * fix names like "xxx (something)" to "xxx" and
74912375Sralph  * "xxx <something>" to "something".
75012375Sralph  */
75112375Sralph 
75212375Sralph char *
75312375Sralph fixaddr(text)
75412375Sralph 	char *text;
75512375Sralph {
75612375Sralph 	register char *cp, *lp, c;
75712375Sralph 	char *tp;
75812375Sralph 
75912375Sralph 	if (!text)
76012375Sralph 		return(0);
76112375Sralph 	for (lp = cp = text; ; ) {
76212375Sralph 		switch (c = *cp++) {
76312375Sralph 		case '(':
76412375Sralph 			while (*cp && *cp++ != ')');
76512375Sralph 			continue;
76612375Sralph 		case '<':
76712375Sralph 			lp = text;
76812375Sralph 		case '>':
76912375Sralph 			continue;
77012375Sralph 		case '\0':
77112375Sralph 			while (lp != text && (*lp == ' ' || *lp == '\t'))
77212375Sralph 				lp--;
77312375Sralph 			*lp = c;
77412375Sralph 			return(text);
77512375Sralph 		}
77612375Sralph 		*lp++ = c;
77712375Sralph 	}
77812375Sralph }
77912375Sralph 
78012375Sralph /*
78112375Sralph  * Compare two strings and convert any upper case letters to lower case.
78212375Sralph  */
78312375Sralph 
78412695Sralph streq(s1, s2)
78512695Sralph 	register char *s1, *s2;
78612375Sralph {
78712375Sralph 	register int c;
78812375Sralph 
78912695Sralph 	while (c = *s1++)
79012695Sralph 		if ((c | 040) != (*s2++ | 040))
79112375Sralph 			return(0);
79212695Sralph 	return(*s2 == '\0');
79312375Sralph }
79412695Sralph 
79512695Sralph /*
79612695Sralph  * Return true if string s2 matches the first part of s1.
79712695Sralph  */
79812695Sralph 
79912695Sralph substr(s1, s2)
80012695Sralph 	register char *s1, *s2;
80112695Sralph {
80212695Sralph 	register int c;
80312695Sralph 
80412695Sralph 	while (c = *s2++)
80512695Sralph 		if (c != *s1++)
80612695Sralph 			return(0);
80712695Sralph 	return(1);
80812695Sralph }
80912695Sralph 
81014809Ssam char *
81114809Ssam any(cp, set)
81214809Ssam 	register char *cp;
81314809Ssam 	char *set;
81414809Ssam {
81514809Ssam 	register char *sp;
81614809Ssam 
81714809Ssam 	if (cp == 0 || set == 0)
81814809Ssam 		return (0);
81914809Ssam 	while (*cp) {
82014809Ssam 		for (sp = set; *sp; sp++)
82114809Ssam 			if (*cp == *sp)
82214809Ssam 				return (cp);
82314809Ssam 		cp++;
82414809Ssam 	}
82514809Ssam 	return ((char *)0);
82614809Ssam }
82714809Ssam 
82812695Sralph peekc(fp)
82912695Sralph FILE *fp;
83012695Sralph {
83112695Sralph 	register c;
83212695Sralph 
83312695Sralph 	c = getc(fp);
83412695Sralph 	ungetc(c, fp);
83512695Sralph 	return(c);
83612695Sralph }
837