Removed mts.conf; the maildelivery option went into slocal directly.
[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 = mhbasename(argv[0]);
53
54         /* read user profile/context */
55         context_read();
56
57         arguments = getarguments(invo_name, argc, argv, 1);
58         argp = arguments;
59
60         while ((cp = *argp++)) {
61                 if (*cp == '-') {
62                         switch (smatch(++cp, switches)) {
63                         case AMBIGSW:
64                                 ambigsw(cp, switches);
65                                 done(1);
66                         case UNKWNSW:
67                                 vec[vecp++] = --cp;
68                                 continue;
69
70                         case HELPSW:
71                                 snprintf(buf, sizeof(buf), "%s [switches] [switches for postproc] address ...", invo_name);
72                                 print_help(buf, switches, 1);
73                                 done(1);
74                         case VERSIONSW:
75                                 print_version(invo_name);
76                                 done(1);
77
78                         case FORMSW:
79                                 if (!(form = *argp++) || *form == '-')
80                                         adios(NULL, "missing argument to %s",
81                                                         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] = mhbasename(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 struct format *fmt;
138
139 static int ncomps = 0;
140 static char **compbuffers = 0;
141 static struct comp **used_buf = 0;
142
143 static int dat[5];
144
145 static char *addrcomps[] = {
146         "from",
147         "sender",
148         "reply-to",
149         "to",
150         "cc",
151         "bcc",
152         "resent-from",
153         "resent-sender",
154         "resent-reply-to",
155         "resent-to",
156         "resent-cc",
157         "resent-bcc",
158         NULL
159 };
160
161
162 static void
163 rcvdistout(FILE *inb, char *form, char *addrs)
164 {
165         register int char_read = 0, format_len, i, state;
166         register char *tmpbuf, **nxtbuf, **ap;
167         char *cp, *scanl, name[NAMESZ];
168         register struct comp *cptr, **savecomp;
169         FILE *out;
170
171         if (!(out = fopen(drft, "w")))
172                 adios(drft, "unable to create");
173
174         /* get new format string */
175         cp = new_fs(form ? form : rcvdistcomps, NULL, NULL);
176         format_len = strlen(cp);
177         ncomps = fmt_compile(cp, &fmt) + 1;
178         if (!(nxtbuf = compbuffers =
179                         (char **) calloc((size_t) ncomps, sizeof(char *))))
180                 adios(NULL, "unable to allocate component buffers");
181         if (!(savecomp = used_buf =
182                         (struct comp **) calloc((size_t) (ncomps + 1),
183                         sizeof(struct comp *))))
184                 adios(NULL, "unable to allocate component buffer stack");
185         savecomp += ncomps + 1;
186         *--savecomp = 0;
187
188         for (i = ncomps; i--;)
189                 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
190         nxtbuf = compbuffers;
191         tmpbuf = *nxtbuf++;
192
193         for (ap = addrcomps; *ap; ap++) {
194                 FINDCOMP(cptr, *ap);
195                 if (cptr)
196                         cptr->c_type |= CT_ADDR;
197         }
198
199         FINDCOMP(cptr, "addresses");
200         if (cptr)
201                 cptr->c_text = addrs;
202
203         for (state = FLD;;) {
204                 switch (state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb)) {
205                 case FLD:
206                 case FLDPLUS:
207                         if ((cptr = wantcomp[CHASH(name)]))
208                                 do {
209                                         if (!mh_strcasecmp(name, cptr->c_name)) {
210                                                 char_read += msg_count;
211                                                 if (!cptr->c_text) {
212                                                         cptr->c_text = tmpbuf;
213                                                         *--savecomp = cptr;
214                                                         tmpbuf = *nxtbuf++;
215                                                 } else {
216                                                         i = strlen(cp = cptr->c_text) - 1;
217                                                         if (cp[i] == '\n') {
218                                                                 if (cptr->c_type & CT_ADDR) {
219                                                                         cp[i] = 0;
220                                                                         cp = add(",\n\t", cp);
221                                                                 } else
222                                                                         cp = add("\t", cp);
223                                                         }
224                                                         cptr->c_text = add(tmpbuf, cp);
225                                                 }
226                                                 while (state == FLDPLUS) {
227                                                         state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
228                                                         cptr->c_text = add(tmpbuf, cptr->c_text);
229                                                         char_read += msg_count;
230                                                 }
231                                                 break;
232                                         }
233                                 } while ((cptr = cptr->c_next));
234
235                         while (state == FLDPLUS)
236                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
237                         break;
238
239                 case LENERR:
240                 case FMTERR:
241                 case BODY:
242                 case FILEEOF:
243                         goto finished;
244
245                 default:
246                         adios(NULL, "m_getfld() returned %d", state);
247                 }
248         }
249 finished: ;
250
251         i = format_len + char_read + 256;
252         scanl = mh_xmalloc((size_t) i + 2);
253         dat[0] = dat[1] = dat[2] = dat[4] = 0;
254         dat[3] = OUTPUTLINELEN;
255         fmt_scan(fmt, scanl, i, dat);
256         fputs(scanl, out);
257
258         if (ferror(out))
259                 adios(drft, "error writing");
260         fclose(out);
261
262         free(scanl);
263         for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
264                         nxtbuf++, i--)
265                 free(cptr->c_text);
266         while (i-- > 0)
267                 free(*nxtbuf++);
268         free((char *) compbuffers);
269         free((char *) used_buf);
270 }
271
272
273 static void
274 unlink_done(int status)
275 {
276         if (backup[0])
277                 unlink(backup);
278         if (drft[0])
279                 unlink(drft);
280         if (tmpfil[0])
281                 unlink(tmpfil);
282
283         exit(status ? RCV_MBX : RCV_MOK);
284 }