xref: /csrg-svn/bin/rmail/rmail.c (revision 4714)
14308Seric /*
2*4714Seric ** rmail: front end for mail to stack up those stupid >From ... remote from ...
3*4714Seric ** lines and make a correct return address.  This works with the -f option
4*4714Seric ** to /usr/lib/sendmail so it won't work on systems without sendmail.
5*4714Seric ** However, it ought to be easy to modify a standard /bin/mail to do the
6*4714Seric ** same thing.
7*4714Seric */
84308Seric 
9*4714Seric static char	SccsId[] =	"@(#)rmail.c	3.5	10/31/81";
104308Seric 
114309Seric # include <stdio.h>
124309Seric # include <sysexits.h>
134309Seric # include "useful.h"
144308Seric 
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;
33*4714Seric 	register char *uf;	/* ptr into ufrom */
344308Seric 
354309Seric # ifdef DEBUG
364309Seric 	if (argc > 1 && strcmp(argv[1], "-T") == 0)
374309Seric 	{
384309Seric 		Debug = TRUE;
394309Seric 		argc--;
404309Seric 		argv++;
414308Seric 	}
424309Seric # endif DEBUG
434308Seric 
444309Seric 	if (argc < 2)
454309Seric 	{
464309Seric 		fprintf(stderr, "Usage: rmail user ...\n");
474309Seric 		exit(EX_USAGE);
484309Seric 	}
494309Seric 
504309Seric 	for (;;)
514309Seric 	{
524319Seric 		(void) fgets(lbuf, sizeof lbuf, stdin);
534309Seric 		if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
544308Seric 			break;
554627Seric 		(void) sscanf(lbuf, "%s %s", junk, ufrom);
564308Seric 		cp = lbuf;
57*4714Seric 		uf = ufrom;
584309Seric 		for (;;)
594309Seric 		{
604308Seric 			cp = index(cp+1, 'r');
614308Seric 			if (cp == NULL)
62*4714Seric 			{
63*4714Seric 				register char *p = rindex(uf, '!');
64*4714Seric 
65*4714Seric 				if (p != NULL)
66*4714Seric 				{
67*4714Seric 					*p = '\0';
68*4714Seric 					strcpy(sys, uf);
69*4714Seric 					uf = p + 1;
70*4714Seric 					break;
71*4714Seric 				}
724308Seric 				cp = "remote from somewhere";
73*4714Seric 			}
744308Seric #ifdef DEBUG
754309Seric 			if (Debug)
764309Seric 				printf("cp='%s'\n", cp);
774308Seric #endif
784308Seric 			if (strncmp(cp, "remote from ", 12)==0)
794308Seric 				break;
804308Seric 		}
81*4714Seric 		if (cp != NULL)
82*4714Seric 			(void) sscanf(cp, "remote from %s", sys);
834308Seric 		strcat(from, sys);
844308Seric 		strcat(from, "!");
854308Seric #ifdef DEBUG
864309Seric 		if (Debug)
87*4714Seric 			printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
884308Seric #endif
894308Seric 	}
904308Seric 	strcat(from, ufrom);
914308Seric 
924310Seric 	sprintf(cmd, "%s -em -f%s", MAILER, from);
934309Seric 	while (*++argv != NULL)
944309Seric 	{
954309Seric 		strcat(cmd, " '");
964309Seric 		strcat(cmd, *argv);
974309Seric 		strcat(cmd, "'");
984309Seric 	}
994308Seric #ifdef DEBUG
1004309Seric 	if (Debug)
1014309Seric 		printf("cmd='%s'\n", cmd);
1024308Seric #endif
1034308Seric 	out = popen(cmd, "w");
1044308Seric 	fputs(lbuf, out);
1054308Seric 	while (fgets(lbuf, sizeof lbuf, stdin))
1064308Seric 		fputs(lbuf, out);
1074308Seric 	pclose(out);
1084308Seric 
1094309Seric 	exit(EX_OK);
1104308Seric }
111