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