drop old NCP stuff for ARPANET handling; fix some bugs in error
[unix-history] / usr / src / bin / rmail / rmail.c
CommitLineData
9adc8f2c
EA
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
0f5791a6 4 * to /usr/lib/sendmail so it won't work on systems without sendmail.
9adc8f2c
EA
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
ed45aae1 17static char SccsId[] = "@(#)rmail.c 3.4 %G%";
9adc8f2c 18
0f5791a6
EA
19# include <stdio.h>
20# include <sysexits.h>
21# include "useful.h"
b5fd168f 22# include "conf.h"
9adc8f2c 23
0f5791a6
EA
24extern FILE *popen();
25extern char *index();
26
27bool Debug;
28
29# define MAILER "/usr/lib/sendmail"
9adc8f2c
EA
30
31main(argc, argv)
0f5791a6 32 char **argv;
9adc8f2c 33{
0f5791a6 34 FILE *out; /* output to sendmail */
9adc8f2c
EA
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 */
0f5791a6
EA
40 char cmd[2000];
41 register char *cp;
9adc8f2c 42
0f5791a6
EA
43# ifdef DEBUG
44 if (argc > 1 && strcmp(argv[1], "-T") == 0)
45 {
46 Debug = TRUE;
47 argc--;
48 argv++;
9adc8f2c 49 }
0f5791a6 50# endif DEBUG
9adc8f2c 51
0f5791a6
EA
52 if (argc < 2)
53 {
54 fprintf(stderr, "Usage: rmail user ...\n");
55 exit(EX_USAGE);
56 }
57
58 for (;;)
59 {
74c5fe7c 60 (void) fgets(lbuf, sizeof lbuf, stdin);
0f5791a6 61 if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0)
9adc8f2c 62 break;
ed45aae1 63 (void) sscanf(lbuf, "%s %s", junk, ufrom);
9adc8f2c 64 cp = lbuf;
0f5791a6
EA
65 for (;;)
66 {
9adc8f2c
EA
67 cp = index(cp+1, 'r');
68 if (cp == NULL)
69 cp = "remote from somewhere";
70#ifdef DEBUG
0f5791a6
EA
71 if (Debug)
72 printf("cp='%s'\n", cp);
9adc8f2c
EA
73#endif
74 if (strncmp(cp, "remote from ", 12)==0)
75 break;
76 }
ed45aae1 77 (void) sscanf(cp, "remote from %s", sys);
9adc8f2c
EA
78 strcat(from, sys);
79 strcat(from, "!");
80#ifdef DEBUG
0f5791a6
EA
81 if (Debug)
82 printf("ufrom='%s', sys='%s', from now '%s'\n", ufrom, sys, from);
9adc8f2c
EA
83#endif
84 }
85 strcat(from, ufrom);
86
b55816b0 87 sprintf(cmd, "%s -em -f%s", MAILER, from);
0f5791a6
EA
88 while (*++argv != NULL)
89 {
90 strcat(cmd, " '");
91 strcat(cmd, *argv);
92 strcat(cmd, "'");
93 }
9adc8f2c 94#ifdef DEBUG
0f5791a6
EA
95 if (Debug)
96 printf("cmd='%s'\n", cmd);
9adc8f2c
EA
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);
9adc8f2c 103
0f5791a6 104 exit(EX_OK);
9adc8f2c 105}