xref: /csrg-svn/bin/rmail/rmail.c (revision 6067)
14308Seric /*
24714Seric ** rmail: front end for mail to stack up those stupid >From ... remote from ...
34714Seric ** lines and make a correct return address.  This works with the -f option
44714Seric ** to /usr/lib/sendmail so it won't work on systems without sendmail.
54714Seric ** However, it ought to be easy to modify a standard /bin/mail to do the
64714Seric ** same thing.
74714Seric */
84308Seric 
94309Seric # include <stdio.h>
104309Seric # include <sysexits.h>
114309Seric # include "useful.h"
124308Seric 
13*6067Seric SCCSID(@(#)rmail.c	3.7	(Berkeley)	03/07/82);
145198Seric 
154309Seric extern FILE *popen();
164309Seric extern char *index();
174308Seric 
184309Seric bool	Debug;
194309Seric 
204309Seric # define MAILER	"/usr/lib/sendmail"
214309Seric 
224308Seric main(argc, argv)
234309Seric 	char **argv;
244308Seric {
254309Seric 	FILE *out;	/* output to sendmail */
264308Seric 	char lbuf[512];	/* one line of the message */
274308Seric 	char from[512];	/* accumulated path of sender */
284308Seric 	char ufrom[64];	/* user on remote system */
294308Seric 	char sys[64];	/* a system in path */
304308Seric 	char junk[512];	/* scratchpad */
314309Seric 	char cmd[2000];
324309Seric 	register char *cp;
334714Seric 	register char *uf;	/* ptr into ufrom */
34*6067Seric 	int i;
354308Seric 
364309Seric # ifdef DEBUG
374309Seric 	if (argc > 1 && strcmp(argv[1], "-T") == 0)
384309Seric 	{
394309Seric 		Debug = TRUE;
404309Seric 		argc--;
414309Seric 		argv++;
424308Seric 	}
434309Seric # endif DEBUG
444308Seric 
454309Seric 	if (argc < 2)
464309Seric 	{
474309Seric 		fprintf(stderr, "Usage: rmail user ...\n");
484309Seric 		exit(EX_USAGE);
494309Seric 	}
504309Seric 
514309Seric 	for (;;)
524309Seric 	{
534319Seric 		(void) fgets(lbuf, sizeof lbuf, stdin);
544309Seric 		if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
554308Seric 			break;
564627Seric 		(void) sscanf(lbuf, "%s %s", junk, ufrom);
574308Seric 		cp = lbuf;
584714Seric 		uf = ufrom;
594309Seric 		for (;;)
604309Seric 		{
614308Seric 			cp = index(cp+1, 'r');
624308Seric 			if (cp == NULL)
634714Seric 			{
644714Seric 				register char *p = rindex(uf, '!');
654714Seric 
664714Seric 				if (p != NULL)
674714Seric 				{
684714Seric 					*p = '\0';
694714Seric 					strcpy(sys, uf);
704714Seric 					uf = p + 1;
714714Seric 					break;
724714Seric 				}
734308Seric 				cp = "remote from somewhere";
744714Seric 			}
754308Seric #ifdef DEBUG
764309Seric 			if (Debug)
774309Seric 				printf("cp='%s'\n", cp);
784308Seric #endif
794308Seric 			if (strncmp(cp, "remote from ", 12)==0)
804308Seric 				break;
814308Seric 		}
824714Seric 		if (cp != NULL)
834714Seric 			(void) sscanf(cp, "remote from %s", sys);
844308Seric 		strcat(from, sys);
854308Seric 		strcat(from, "!");
864308Seric #ifdef DEBUG
874309Seric 		if (Debug)
884714Seric 			printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
894308Seric #endif
904308Seric 	}
914308Seric 	strcat(from, ufrom);
924308Seric 
935198Seric 	(void) sprintf(cmd, "%s -em -f%s", MAILER, from);
944309Seric 	while (*++argv != NULL)
954309Seric 	{
964309Seric 		strcat(cmd, " '");
974309Seric 		strcat(cmd, *argv);
984309Seric 		strcat(cmd, "'");
994309Seric 	}
1004308Seric #ifdef DEBUG
1014309Seric 	if (Debug)
1024309Seric 		printf("cmd='%s'\n", cmd);
1034308Seric #endif
1044308Seric 	out = popen(cmd, "w");
1054308Seric 	fputs(lbuf, out);
1064308Seric 	while (fgets(lbuf, sizeof lbuf, stdin))
1074308Seric 		fputs(lbuf, out);
108*6067Seric 	i = pclose(out);
109*6067Seric 	if ((i & 0377) != 0)
110*6067Seric 	{
111*6067Seric 		fprintf(stderr, "pclose: status 0%o\n", i);
112*6067Seric 		exit(EX_OSERR);
113*6067Seric 	}
1144308Seric 
115*6067Seric 	exit((i >> 8) & 0377);
1164308Seric }
117