xref: /csrg-svn/bin/rmail/rmail.c (revision 4309)
14308Seric /*
24308Seric  * rmail: front end for mail to stack up those stupid >From ... remote from ...
34308Seric  * lines and make a correct return address.  This works with the -f option
4*4309Seric  * to /usr/lib/sendmail so it won't work on systems without sendmail.
54308Seric  * However, it ought to be easy to modify a standard /bin/mail to do the
64308Seric  * same thing.
74308Seric  *
84308Seric  * NOTE: Rmail is SPECIFICALLY INTENDED for ERNIE COVAX because of its
94308Seric  * physical position as a gateway between the uucp net and the arpanet.
104308Seric  * By default, other sites will probably want /bin/rmail to be a link
114308Seric  * to /bin/mail, as it was intended by BTL.  However, other than the
124308Seric  * (somewhat annoying) loss of information about when the mail was
134308Seric  * originally sent, rmail should work OK on other systems running uucp.
144308Seric  * If you don't run uucp you don't even need any rmail.
154308Seric  */
164308Seric 
17*4309Seric static char	SccsId[] =	"@(#)rmail.c	3.1	09/06/81";
184308Seric 
19*4309Seric # include <stdio.h>
20*4309Seric # include <sysexits.h>
21*4309Seric # include "useful.h"
224308Seric 
23*4309Seric extern FILE *popen();
24*4309Seric extern char *index();
254308Seric 
26*4309Seric bool	Debug;
27*4309Seric 
28*4309Seric # define MAILER	"/usr/lib/sendmail"
29*4309Seric 
304308Seric main(argc, argv)
31*4309Seric 	char **argv;
324308Seric {
33*4309Seric 	FILE *out;	/* output to sendmail */
344308Seric 	char lbuf[512];	/* one line of the message */
354308Seric 	char from[512];	/* accumulated path of sender */
364308Seric 	char ufrom[64];	/* user on remote system */
374308Seric 	char sys[64];	/* a system in path */
384308Seric 	char junk[512];	/* scratchpad */
39*4309Seric 	char cmd[2000];
40*4309Seric 	register char *cp;
414308Seric 
42*4309Seric # ifdef DEBUG
43*4309Seric 	if (argc > 1 && strcmp(argv[1], "-T") == 0)
44*4309Seric 	{
45*4309Seric 		Debug = TRUE;
46*4309Seric 		argc--;
47*4309Seric 		argv++;
484308Seric 	}
49*4309Seric # endif DEBUG
504308Seric 
51*4309Seric 	if (argc < 2)
52*4309Seric 	{
53*4309Seric 		fprintf(stderr, "Usage: rmail user ...\n");
54*4309Seric 		exit(EX_USAGE);
55*4309Seric 	}
56*4309Seric 
57*4309Seric 	for (;;)
58*4309Seric 	{
594308Seric 		fgets(lbuf, sizeof lbuf, stdin);
60*4309Seric 		if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
614308Seric 			break;
624308Seric 		sscanf(lbuf, "%s %s", junk, ufrom);
634308Seric 		cp = lbuf;
64*4309Seric 		for (;;)
65*4309Seric 		{
664308Seric 			cp = index(cp+1, 'r');
674308Seric 			if (cp == NULL)
684308Seric 				cp = "remote from somewhere";
694308Seric #ifdef DEBUG
70*4309Seric 			if (Debug)
71*4309Seric 				printf("cp='%s'\n", cp);
724308Seric #endif
734308Seric 			if (strncmp(cp, "remote from ", 12)==0)
744308Seric 				break;
754308Seric 		}
764308Seric 		sscanf(cp, "remote from %s", sys);
774308Seric 		strcat(from, sys);
784308Seric 		strcat(from, "!");
794308Seric #ifdef DEBUG
80*4309Seric 		if (Debug)
81*4309Seric 			printf("ufrom='%s', sys='%s', from now '%s'\n", ufrom, sys, from);
824308Seric #endif
834308Seric 	}
844308Seric 	strcat(from, ufrom);
854308Seric 
86*4309Seric 	sprintf(cmd, "%s -f%s", MAILER, from);
87*4309Seric 	while (*++argv != NULL)
88*4309Seric 	{
89*4309Seric 		strcat(cmd, " '");
90*4309Seric 		strcat(cmd, *argv);
91*4309Seric 		strcat(cmd, "'");
92*4309Seric 	}
934308Seric #ifdef DEBUG
94*4309Seric 	if (Debug)
95*4309Seric 		printf("cmd='%s'\n", cmd);
964308Seric #endif
974308Seric 	out = popen(cmd, "w");
984308Seric 	fputs(lbuf, out);
994308Seric 	while (fgets(lbuf, sizeof lbuf, stdin))
1004308Seric 		fputs(lbuf, out);
1014308Seric 	pclose(out);
1024308Seric 
103*4309Seric 	exit(EX_OK);
1044308Seric }
105