xref: /csrg-svn/bin/rmail/rmail.c (revision 4436)
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  * NOTE: Rmail is SPECIFICALLY INTENDED for ERNIE COVAX because of its
9  * physical position as a gateway between the uucp net and the arpanet.
10  * By default, other sites will probably want /bin/rmail to be a link
11  * to /bin/mail, as it was intended by BTL.  However, other than the
12  * (somewhat annoying) loss of information about when the mail was
13  * originally sent, rmail should work OK on other systems running uucp.
14  * If you don't run uucp you don't even need any rmail.
15  */
16 
17 static char	SccsId[] =	"@(#)rmail.c	3.3.1.1	09/23/81";
18 
19 # include <stdio.h>
20 # include <sysexits.h>
21 # include "useful.h"
22 # include "conf.h"
23 
24 extern FILE *popen();
25 extern char *index();
26 
27 bool	Debug;
28 
29 # define MAILER	"/usr/lib/sendmail"
30 
31 main(argc, argv)
32 	char **argv;
33 {
34 	FILE *out;	/* output to sendmail */
35 	char lbuf[512];	/* one line of the message */
36 	char from[512];	/* accumulated path of sender */
37 	char ufrom[64];	/* user on remote system */
38 	char sys[64];	/* a system in path */
39 	char junk[512];	/* scratchpad */
40 	char cmd[2000];
41 	register char *cp;
42 
43 # ifdef DEBUG
44 	if (argc > 1 && strcmp(argv[1], "-T") == 0)
45 	{
46 		Debug = TRUE;
47 		argc--;
48 		argv++;
49 	}
50 # endif DEBUG
51 
52 	if (argc < 2)
53 	{
54 		fprintf(stderr, "Usage: rmail user ...\n");
55 		exit(EX_USAGE);
56 	}
57 
58 	for (;;)
59 	{
60 		(void) fgets(lbuf, sizeof lbuf, stdin);
61 		if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
62 			break;
63 		sscanf(lbuf, "%s %s", junk, ufrom);
64 		cp = lbuf;
65 		for (;;)
66 		{
67 			cp = index(cp+1, 'r');
68 			if (cp == NULL)
69 				cp = "remote from somewhere";
70 #ifdef DEBUG
71 			if (Debug)
72 				printf("cp='%s'\n", cp);
73 #endif
74 			if (strncmp(cp, "remote from ", 12)==0)
75 				break;
76 		}
77 		sscanf(cp, "remote from %s", sys);
78 		strcat(from, sys);
79 		strcat(from, "!");
80 #ifdef DEBUG
81 		if (Debug)
82 			printf("ufrom='%s', sys='%s', from now '%s'\n", ufrom, sys, from);
83 #endif
84 	}
85 	strcat(from, ufrom);
86 
87 	sprintf(cmd, "%s -em -f%s", MAILER, from);
88 	while (*++argv != NULL)
89 	{
90 		strcat(cmd, " '");
91 		strcat(cmd, *argv);
92 		strcat(cmd, "'");
93 	}
94 #ifdef DEBUG
95 	if (Debug)
96 		printf("cmd='%s'\n", cmd);
97 #endif
98 	out = popen(cmd, "w");
99 	fputs(lbuf, out);
100 	while (fgets(lbuf, sizeof lbuf, stdin))
101 		fputs(lbuf, out);
102 	pclose(out);
103 
104 	exit(EX_OK);
105 }
106