Commit | Line | Data |
---|---|---|
9adc8f2c | 1 | /* |
09fa495a EA |
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 | */ | |
9adc8f2c | 8 | |
0f5791a6 EA |
9 | # include <stdio.h> |
10 | # include <sysexits.h> | |
11 | # include "useful.h" | |
b5fd168f | 12 | # include "conf.h" |
9adc8f2c | 13 | |
4e5e456f EA |
14 | SCCSID(@(#)rmail.c 3.6 (Berkeley) %G%); |
15 | ||
0f5791a6 EA |
16 | extern FILE *popen(); |
17 | extern char *index(); | |
18 | ||
19 | bool Debug; | |
20 | ||
21 | # define MAILER "/usr/lib/sendmail" | |
9adc8f2c EA |
22 | |
23 | main(argc, argv) | |
0f5791a6 | 24 | char **argv; |
9adc8f2c | 25 | { |
0f5791a6 | 26 | FILE *out; /* output to sendmail */ |
9adc8f2c EA |
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 */ | |
0f5791a6 EA |
32 | char cmd[2000]; |
33 | register char *cp; | |
09fa495a | 34 | register char *uf; /* ptr into ufrom */ |
9adc8f2c | 35 | |
0f5791a6 EA |
36 | # ifdef DEBUG |
37 | if (argc > 1 && strcmp(argv[1], "-T") == 0) | |
38 | { | |
39 | Debug = TRUE; | |
40 | argc--; | |
41 | argv++; | |
9adc8f2c | 42 | } |
0f5791a6 | 43 | # endif DEBUG |
9adc8f2c | 44 | |
0f5791a6 EA |
45 | if (argc < 2) |
46 | { | |
47 | fprintf(stderr, "Usage: rmail user ...\n"); | |
48 | exit(EX_USAGE); | |
49 | } | |
50 | ||
51 | for (;;) | |
52 | { | |
74c5fe7c | 53 | (void) fgets(lbuf, sizeof lbuf, stdin); |
0f5791a6 | 54 | if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0) |
9adc8f2c | 55 | break; |
ed45aae1 | 56 | (void) sscanf(lbuf, "%s %s", junk, ufrom); |
9adc8f2c | 57 | cp = lbuf; |
09fa495a | 58 | uf = ufrom; |
0f5791a6 EA |
59 | for (;;) |
60 | { | |
9adc8f2c EA |
61 | cp = index(cp+1, 'r'); |
62 | if (cp == NULL) | |
09fa495a EA |
63 | { |
64 | register char *p = rindex(uf, '!'); | |
65 | ||
66 | if (p != NULL) | |
67 | { | |
68 | *p = '\0'; | |
69 | strcpy(sys, uf); | |
70 | uf = p + 1; | |
71 | break; | |
72 | } | |
9adc8f2c | 73 | cp = "remote from somewhere"; |
09fa495a | 74 | } |
9adc8f2c | 75 | #ifdef DEBUG |
0f5791a6 EA |
76 | if (Debug) |
77 | printf("cp='%s'\n", cp); | |
9adc8f2c EA |
78 | #endif |
79 | if (strncmp(cp, "remote from ", 12)==0) | |
80 | break; | |
81 | } | |
09fa495a EA |
82 | if (cp != NULL) |
83 | (void) sscanf(cp, "remote from %s", sys); | |
9adc8f2c EA |
84 | strcat(from, sys); |
85 | strcat(from, "!"); | |
86 | #ifdef DEBUG | |
0f5791a6 | 87 | if (Debug) |
09fa495a | 88 | printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from); |
9adc8f2c EA |
89 | #endif |
90 | } | |
91 | strcat(from, ufrom); | |
92 | ||
4e5e456f | 93 | (void) sprintf(cmd, "%s -em -f%s", MAILER, from); |
0f5791a6 EA |
94 | while (*++argv != NULL) |
95 | { | |
96 | strcat(cmd, " '"); | |
97 | strcat(cmd, *argv); | |
98 | strcat(cmd, "'"); | |
99 | } | |
9adc8f2c | 100 | #ifdef DEBUG |
0f5791a6 EA |
101 | if (Debug) |
102 | printf("cmd='%s'\n", cmd); | |
9adc8f2c EA |
103 | #endif |
104 | out = popen(cmd, "w"); | |
105 | fputs(lbuf, out); | |
106 | while (fgets(lbuf, sizeof lbuf, stdin)) | |
107 | fputs(lbuf, out); | |
108 | pclose(out); | |
9adc8f2c | 109 | |
0f5791a6 | 110 | exit(EX_OK); |
9adc8f2c | 111 | } |