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