xref: /original-bsd/bin/rmail/rmail.c (revision 2d15e5d1)
1 /*
2 ** rmail: front end for mail to stack up those stupid >From ... remote from ...
3 ** lines and make a correct return address.  This works with the -f option
4 ** to /usr/lib/sendmail so it won't work on systems without sendmail.
5 ** However, it ought to be easy to modify a standard /bin/mail to do the
6 ** same thing.
7 */
8 
9 static char	SccsId[] =	"@(#)rmail.c	3.5	10/31/81";
10 
11 # include <stdio.h>
12 # include <sysexits.h>
13 # include "useful.h"
14 
15 extern FILE *popen();
16 extern char *index();
17 
18 bool	Debug;
19 
20 # define MAILER	"/usr/lib/sendmail"
21 
22 main(argc, argv)
23 	char **argv;
24 {
25 	FILE *out;	/* output to sendmail */
26 	char lbuf[512];	/* one line of the message */
27 	char from[512];	/* accumulated path of sender */
28 	char ufrom[64];	/* user on remote system */
29 	char sys[64];	/* a system in path */
30 	char junk[512];	/* scratchpad */
31 	char cmd[2000];
32 	register char *cp;
33 	register char *uf;	/* ptr into ufrom */
34 
35 # ifdef DEBUG
36 	if (argc > 1 && strcmp(argv[1], "-T") == 0)
37 	{
38 		Debug = TRUE;
39 		argc--;
40 		argv++;
41 	}
42 # endif DEBUG
43 
44 	if (argc < 2)
45 	{
46 		fprintf(stderr, "Usage: rmail user ...\n");
47 		exit(EX_USAGE);
48 	}
49 
50 	for (;;)
51 	{
52 		(void) fgets(lbuf, sizeof lbuf, stdin);
53 		if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
54 			break;
55 		(void) sscanf(lbuf, "%s %s", junk, ufrom);
56 		cp = lbuf;
57 		uf = ufrom;
58 		for (;;)
59 		{
60 			cp = index(cp+1, 'r');
61 			if (cp == NULL)
62 			{
63 				register char *p = rindex(uf, '!');
64 
65 				if (p != NULL)
66 				{
67 					*p = '\0';
68 					strcpy(sys, uf);
69 					uf = p + 1;
70 					break;
71 				}
72 				cp = "remote from somewhere";
73 			}
74 #ifdef DEBUG
75 			if (Debug)
76 				printf("cp='%s'\n", cp);
77 #endif
78 			if (strncmp(cp, "remote from ", 12)==0)
79 				break;
80 		}
81 		if (cp != NULL)
82 			(void) sscanf(cp, "remote from %s", sys);
83 		strcat(from, sys);
84 		strcat(from, "!");
85 #ifdef DEBUG
86 		if (Debug)
87 			printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
88 #endif
89 	}
90 	strcat(from, ufrom);
91 
92 	sprintf(cmd, "%s -em -f%s", MAILER, from);
93 	while (*++argv != NULL)
94 	{
95 		strcat(cmd, " '");
96 		strcat(cmd, *argv);
97 		strcat(cmd, "'");
98 	}
99 #ifdef DEBUG
100 	if (Debug)
101 		printf("cmd='%s'\n", cmd);
102 #endif
103 	out = popen(cmd, "w");
104 	fputs(lbuf, out);
105 	while (fgets(lbuf, sizeof lbuf, stdin))
106 		fputs(lbuf, out);
107 	pclose(out);
108 
109 	exit(EX_OK);
110 }
111