typo in nntp name
[unix-history] / usr / src / bin / rmail / rmail.c
CommitLineData
205a2d85 1#ifndef lint
f6aa3630 2static char sccsid[] = "@(#)rmail.c 4.8 (Berkeley) %G%";
205a2d85
SL
3#endif
4
9adc8f2c 5/*
b8b8df54
EA
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.
09fa495a 12*/
9adc8f2c 13
0f5791a6
EA
14# include <stdio.h>
15# include <sysexits.h>
b5fd168f 16# include "conf.h"
9adc8f2c 17
44ae4c24
EA
18typedef char bool;
19#define TRUE 1
20#define FALSE 0
21
22extern FILE *popen();
23extern char *index();
24extern char *rindex();
0f5791a6
EA
25
26bool Debug;
27
28# define MAILER "/usr/lib/sendmail"
9adc8f2c
EA
29
30main(argc, argv)
0f5791a6 31 char **argv;
9adc8f2c 32{
0f5791a6 33 FILE *out; /* output to sendmail */
ecbd0932
JL
34 char lbuf[1024]; /* one line of the message */
35 char from[512]; /* accumulated path of sender */
36 char ufrom[512]; /* user on remote system */
37 char sys[512]; /* a system in path */
38 char junk[1024]; /* scratchpad */
0f5791a6
EA
39 char cmd[2000];
40 register char *cp;
f6aa3630 41 register char *uf = ufrom; /* ptr into ufrom */
9777ef27 42 int i;
9adc8f2c 43
0f5791a6
EA
44# ifdef DEBUG
45 if (argc > 1 && strcmp(argv[1], "-T") == 0)
46 {
47 Debug = TRUE;
48 argc--;
49 argv++;
9adc8f2c 50 }
0f5791a6 51# endif DEBUG
9adc8f2c 52
0f5791a6
EA
53 if (argc < 2)
54 {
55 fprintf(stderr, "Usage: rmail user ...\n");
56 exit(EX_USAGE);
57 }
58
f9566d23
EA
59 (void) strcpy(from, "");
60 (void) strcpy(ufrom, "/dev/null");
b8b8df54 61
0f5791a6
EA
62 for (;;)
63 {
74c5fe7c 64 (void) fgets(lbuf, sizeof lbuf, stdin);
0f5791a6 65 if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
9adc8f2c 66 break;
ed45aae1 67 (void) sscanf(lbuf, "%s %s", junk, ufrom);
9adc8f2c 68 cp = lbuf;
0f5791a6
EA
69 for (;;)
70 {
9adc8f2c
EA
71 cp = index(cp+1, 'r');
72 if (cp == NULL)
09fa495a
EA
73 {
74 register char *p = rindex(uf, '!');
75
76 if (p != NULL)
77 {
78 *p = '\0';
f9566d23 79 (void) strcpy(sys, uf);
09fa495a
EA
80 uf = p + 1;
81 break;
82 }
9adc8f2c 83 cp = "remote from somewhere";
09fa495a 84 }
9adc8f2c 85#ifdef DEBUG
0f5791a6
EA
86 if (Debug)
87 printf("cp='%s'\n", cp);
9adc8f2c
EA
88#endif
89 if (strncmp(cp, "remote from ", 12)==0)
90 break;
91 }
09fa495a
EA
92 if (cp != NULL)
93 (void) sscanf(cp, "remote from %s", sys);
f9566d23
EA
94 (void) strcat(from, sys);
95 (void) strcat(from, "!");
9adc8f2c 96#ifdef DEBUG
0f5791a6 97 if (Debug)
09fa495a 98 printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from);
9adc8f2c
EA
99#endif
100 }
f9566d23 101 (void) strcat(from, uf);
9adc8f2c 102
37c2e4d4 103 (void) sprintf(cmd, "%s -ee -f%s -i", MAILER, from);
0f5791a6
EA
104 while (*++argv != NULL)
105 {
f9566d23 106 (void) strcat(cmd, " '");
674f6e70 107 if (**argv == '(')
f9566d23 108 (void) strncat(cmd, *argv + 1, strlen(*argv) - 2);
674f6e70 109 else
f9566d23
EA
110 (void) strcat(cmd, *argv);
111 (void) strcat(cmd, "'");
0f5791a6 112 }
9adc8f2c 113#ifdef DEBUG
0f5791a6
EA
114 if (Debug)
115 printf("cmd='%s'\n", cmd);
9adc8f2c
EA
116#endif
117 out = popen(cmd, "w");
118 fputs(lbuf, out);
119 while (fgets(lbuf, sizeof lbuf, stdin))
120 fputs(lbuf, out);
9777ef27
EA
121 i = pclose(out);
122 if ((i & 0377) != 0)
123 {
124 fprintf(stderr, "pclose: status 0%o\n", i);
125 exit(EX_OSERR);
126 }
9adc8f2c 127
9777ef27 128 exit((i >> 8) & 0377);
9adc8f2c 129}