xref: /original-bsd/bin/rmail/rmail.c (revision a910c8b7)
1 #ifndef lint
2 static char sccsid[] =	"@(#)rmail.c	4.5 (Berkeley) 08/18/84";
3 #endif
4 
5 /*
6 **  RMAIL -- UUCP mail server.
7 **
8 **	This program reads the >From ... remote from ... lines that
9 **	UUCP is so fond of and turns them into something reasonable.
10 **	It calls sendmail giving it a -f option built from these
11 **	lines.
12 */
13 
14 # include <stdio.h>
15 # include <sysexits.h>
16 
17 typedef char	bool;
18 #define TRUE	1
19 #define FALSE	0
20 
21 extern FILE	*popen();
22 extern char	*index();
23 extern char	*rindex();
24 
25 bool	Debug;
26 
27 # define MAILER	"/usr/lib/sendmail"
28 
29 main(argc, argv)
30 	char **argv;
31 {
32 	FILE *out;	/* output to sendmail */
33 	char lbuf[512];	/* one line of the message */
34 	char from[512];	/* accumulated path of sender */
35 	char ufrom[64];	/* user on remote system */
36 	char sys[64];	/* a system in path */
37 	char junk[512];	/* scratchpad */
38 	char cmd[2000];
39 	register char *cp;
40 	register char *uf;	/* ptr into ufrom */
41 	int i;
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 	(void) strcpy(from, "");
59 	(void) strcpy(ufrom, "/dev/null");
60 
61 	for (;;)
62 	{
63 		(void) fgets(lbuf, sizeof lbuf, stdin);
64 		if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
65 			break;
66 		(void) sscanf(lbuf, "%s %s", junk, ufrom);
67 		cp = lbuf;
68 		uf = ufrom;
69 		for (;;)
70 		{
71 			cp = index(cp+1, 'r');
72 			if (cp == NULL)
73 			{
74 				register char *p = rindex(uf, '!');
75 
76 				if (p != NULL)
77 				{
78 					*p = '\0';
79 					(void) strcpy(sys, uf);
80 					uf = p + 1;
81 					break;
82 				}
83 				cp = "remote from somewhere";
84 			}
85 #ifdef DEBUG
86 			if (Debug)
87 				printf("cp='%s'\n", cp);
88 #endif
89 			if (strncmp(cp, "remote from ", 12)==0)
90 				break;
91 		}
92 		if (cp != NULL)
93 			(void) sscanf(cp, "remote from %s", sys);
94 		(void) strcat(from, sys);
95 		(void) strcat(from, "!");
96 #ifdef DEBUG
97 		if (Debug)
98 			printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
99 #endif
100 	}
101 	(void) strcat(from, uf);
102 
103 	(void) sprintf(cmd, "%s -ee -f%s", MAILER, from);
104 	while (*++argv != NULL)
105 	{
106 		(void) strcat(cmd, " '");
107 		if (**argv == '(')
108 			(void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
109 		else
110 			(void) strcat(cmd, *argv);
111 		(void) strcat(cmd, "'");
112 	}
113 #ifdef DEBUG
114 	if (Debug)
115 		printf("cmd='%s'\n", cmd);
116 #endif
117 	out = popen(cmd, "w");
118 	fputs(lbuf, out);
119 	while (fgets(lbuf, sizeof lbuf, stdin))
120 		fputs(lbuf, out);
121 	i = pclose(out);
122 	if ((i & 0377) != 0)
123 	{
124 		fprintf(stderr, "pclose: status 0%o\n", i);
125 		exit(EX_OSERR);
126 	}
127 
128 	exit((i >> 8) & 0377);
129 }
130