4e052ccd09cfceb9a5fb6e9aa36415ec43828957
[mmh] / uip / rcvdist.c
1 /*
2 ** rcvdist.c -- asynchronously redistribute messages
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/fmt_scan.h>
11 #include <h/rcvmail.h>
12 #include <h/tws.h>
13 #include <h/utils.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <locale.h>
17
18 static struct swit switches[] = {
19 #define FORMSW  0
20         { "form formfile",  0 },
21 #define VERSIONSW  1
22         { "Version", 0 },
23 #define HELPSW  2
24         { "help", 0 },
25         { NULL, 0 }
26 };
27
28 static char backup[BUFSIZ] = "";
29 static char drft[BUFSIZ] = "";
30 static char tmpfil[BUFSIZ] = "";
31
32 /*
33 ** prototypes
34 */
35 static void rcvdistout(FILE *, char *, char *);
36 void unlink_done();
37
38
39 int
40 main(int argc, char **argv)
41 {
42         int vecp = 1;
43         char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
44         char **argp, **arguments, *vec[MAXARGS];
45         FILE *fp;
46         char *tfile = NULL;
47
48         if (atexit(unlink_done) != 0) {
49                 adios(NULL, "atexit failed");
50         }
51
52         setlocale(LC_ALL, "");
53         invo_name = mhbasename(argv[0]);
54
55         /* read user profile/context */
56         context_read();
57
58         arguments = getarguments(invo_name, argc, argv, 1);
59         argp = arguments;
60
61         while ((cp = *argp++)) {
62                 if (*cp == '-') {
63                         switch (smatch(++cp, switches)) {
64                         case AMBIGSW:
65                                 ambigsw(cp, switches);
66                                 exit(1);
67                         case UNKWNSW:
68                                 vec[vecp++] = --cp;
69                                 continue;
70
71                         case HELPSW:
72                                 snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
73                                 print_help(buf, switches, 1);
74                                 exit(0);
75                         case VERSIONSW:
76                                 print_version(invo_name);
77                                 exit(0);
78
79                         case FORMSW:
80                                 if (!(form = *argp++) || *form == '-') {
81                                         adios(NULL, "missing argument to %s",
82                                                         argp[-2]);
83                                 }
84                                 continue;
85                         }
86                 }
87                 addrs = addrs ? add(cp, add(", ", addrs)) : getcpy(cp);
88         }
89
90         if (!addrs) {
91                 adios(NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
92         }
93
94         umask(~m_gmprot());
95
96         tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
97         if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
98         strncpy(tmpfil, tfile, sizeof(tmpfil));
99
100         cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
101         fseek(fp, 0L, SEEK_SET);
102
103         tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
104         if (tfile == NULL) adios("forw", "unable to create temporary file");
105         strncpy(drft, tfile, sizeof(tmpfil));
106
107         rcvdistout(fp, form, addrs);
108         fclose(fp);
109
110         if (distout(drft, tmpfil, backup) == NOTOK) {
111                 /* sysexits.h EX_DATAERR */
112                 exit(1);
113         }
114
115         vec[0] = "spost";
116         vec[vecp++] = "-dist";
117         vec[vecp++] = drft;
118         vec[vecp] = NULL;
119
120         execvp(*vec, vec);
121         fprintf(stderr, "unable to exec ");
122         perror(*vec);
123         _exit(1);
124         return 0;  /* dead code to satisfy the compiler */
125 }
126
127 /* very similar to routine in replsbr.c */
128
129 #define SBUFSIZ 256
130
131 static struct format *fmt;
132
133 static int ncomps = 0;
134 static char **compbuffers = 0;
135 static struct comp **used_buf = 0;
136
137 static int dat[5];
138
139 static char *addrcomps[] = {
140         "from",
141         "sender",
142         "reply-to",
143         "to",
144         "cc",
145         "bcc",
146         "resent-from",
147         "resent-sender",
148         "resent-reply-to",
149         "resent-to",
150         "resent-cc",
151         "resent-bcc",
152         NULL
153 };
154
155
156 static void
157 rcvdistout(FILE *inb, char *form, char *addrs)
158 {
159         register int char_read = 0, format_len, i, state;
160         register char *tmpbuf, **nxtbuf, **ap;
161         char *cp, *scanl, name[NAMESZ];
162         register struct comp *cptr, **savecomp;
163         FILE *out;
164
165         if (!(out = fopen(drft, "w"))) {
166                 adios(drft, "unable to create");
167         }
168
169         /* get new format string */
170         cp = new_fs(form ? form : rcvdistcomps, NULL);
171         format_len = strlen(cp);
172         ncomps = fmt_compile(cp, &fmt) + 1;
173         if (!(nxtbuf = compbuffers =
174                         (char **) calloc((size_t) ncomps, sizeof(char *)))) {
175                 adios(NULL, "unable to allocate component buffers");
176         }
177         if (!(savecomp = used_buf =
178                         (struct comp **) calloc((size_t) (ncomps + 1),
179                         sizeof(struct comp *)))) {
180                 adios(NULL, "unable to allocate component buffer stack");
181         }
182         savecomp += ncomps + 1;
183         *--savecomp = 0;
184
185         for (i = ncomps; i--;) {
186                 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
187         }
188         nxtbuf = compbuffers;
189         tmpbuf = *nxtbuf++;
190
191         for (ap = addrcomps; *ap; ap++) {
192                 FINDCOMP(cptr, *ap);
193                 if (cptr) {
194                         cptr->c_type |= CT_ADDR;
195                 }
196         }
197
198         FINDCOMP(cptr, "addresses");
199         if (cptr) {
200                 cptr->c_text = addrs;
201         }
202         state = FLD;
203         while (1) {
204                 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
205                 switch (state) {
206                 case FLD:
207                 case FLDPLUS:
208                         if ((cptr = wantcomp[CHASH(name)])) {
209                                 do {
210                                         if (mh_strcasecmp(name, cptr->c_name)!=0) {
211                                                 continue;
212                                         }
213                                         char_read += msg_count;
214                                         if (!cptr->c_text) {
215                                                 cptr->c_text = tmpbuf;
216                                                 *--savecomp = cptr;
217                                                 tmpbuf = *nxtbuf++;
218                                         } else {
219                                                 cp = cptr->c_text;
220                                                 i = strlen(cp) - 1;
221                                                 if (cp[i] == '\n') {
222                                                         if (cptr->c_type & CT_ADDR) {
223                                                                 cp[i] = 0;
224                                                                 cp = add(",\n\t", cp);
225                                                         } else {
226                                                                 cp = add("\t", cp);
227                                                         }
228                                                 }
229                                                 cptr->c_text = add(tmpbuf, cp);
230                                         }
231                                         while (state == FLDPLUS) {
232                                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
233                                                 cptr->c_text = add(tmpbuf, cptr->c_text);
234                                                 char_read += msg_count;
235                                         }
236                                         break;
237                                 } while ((cptr = cptr->c_next));
238                         }
239
240                         while (state == FLDPLUS) {
241                                 state = m_getfld(state, name, tmpbuf,
242                                                 SBUFSIZ, inb);
243                         }
244                         break;
245
246                 case LENERR:
247                 case FMTERR:
248                 case BODY:
249                 case FILEEOF:
250                         goto finished;
251
252                 default:
253                         adios(NULL, "m_getfld() returned %d", state);
254                 }
255         }
256 finished: ;
257
258         i = format_len + char_read + 256;
259         scanl = mh_xmalloc((size_t) i + 2);
260         dat[0] = dat[1] = dat[2] = dat[4] = 0;
261         dat[3] = OUTPUTLINELEN;
262         fmt_scan(fmt, scanl, i, dat);
263         fputs(scanl, out);
264
265         if (ferror(out)) {
266                 adios(drft, "error writing");
267         }
268         fclose(out);
269
270         free(scanl);
271         for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
272                         nxtbuf++, i--) {
273                 free(cptr->c_text);
274         }
275         while (i-- > 0) {
276                 free(*nxtbuf++);
277         }
278         free(compbuffers);
279         free(used_buf);
280 }
281
282
283 void
284 unlink_done()
285 {
286         if (*backup) {
287                 unlink(backup);
288         }
289         if (*drft) {
290                 unlink(drft);
291         }
292         if (*tmpfil) {
293                 unlink(tmpfil);
294         }
295 }