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