Replace done with exit at uip
[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",  0 },
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();
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         atexit(unlink_done);
46
47         setlocale(LC_ALL, "");
48         invo_name = mhbasename(argv[0]);
49
50         /* read user profile/context */
51         context_read();
52
53         arguments = getarguments(invo_name, argc, argv, 1);
54         argp = arguments;
55
56         while ((cp = *argp++)) {
57                 if (*cp == '-') {
58                         switch (smatch(++cp, switches)) {
59                         case AMBIGSW:
60                                 ambigsw(cp, switches);
61                                 exit(1);
62                         case UNKWNSW:
63                                 vec[vecp++] = --cp;
64                                 continue;
65
66                         case HELPSW:
67                                 snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
68                                 print_help(buf, switches, 1);
69                                 exit(0);
70                         case VERSIONSW:
71                                 print_version(invo_name);
72                                 exit(0);
73
74                         case FORMSW:
75                                 if (!(form = *argp++) || *form == '-') {
76                                         adios(NULL, "missing argument to %s",
77                                                         argp[-2]);
78                                 }
79                                 continue;
80                         }
81                 }
82                 addrs = addrs ? add(cp, add(", ", addrs)) : getcpy(cp);
83         }
84
85         if (!addrs) {
86                 adios(NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
87         }
88
89         umask(~m_gmprot());
90
91         tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
92         if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
93         strncpy(tmpfil, tfile, sizeof(tmpfil));
94
95         cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
96         fseek(fp, 0L, SEEK_SET);
97
98         tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
99         if (tfile == NULL) adios("forw", "unable to create temporary file");
100         strncpy(drft, tfile, sizeof(tmpfil));
101
102         rcvdistout(fp, form, addrs);
103         fclose(fp);
104
105         if (distout(drft, tmpfil, backup) == NOTOK) {
106                 /* sysexits.h EX_DATAERR */
107                 exit(1);
108         }
109
110         vec[0] = "spost";
111         vec[vecp++] = "-dist";
112         vec[vecp++] = drft;
113         vec[vecp] = NULL;
114
115         execvp(*vec, vec);
116         fprintf(stderr, "unable to exec ");
117         perror(*vec);
118         _exit(1);
119         return 0;  /* dead code to satisfy the compiler */
120 }
121
122 /* very similar to routine in replsbr.c */
123
124 #define SBUFSIZ 256
125
126 static struct format *fmt;
127
128 static int ncomps = 0;
129 static char **compbuffers = 0;
130 static struct comp **used_buf = 0;
131
132 static int dat[5];
133
134 static char *addrcomps[] = {
135         "from",
136         "sender",
137         "reply-to",
138         "to",
139         "cc",
140         "bcc",
141         "resent-from",
142         "resent-sender",
143         "resent-reply-to",
144         "resent-to",
145         "resent-cc",
146         "resent-bcc",
147         NULL
148 };
149
150
151 static void
152 rcvdistout(FILE *inb, char *form, char *addrs)
153 {
154         register int char_read = 0, format_len, i, state;
155         register char *tmpbuf, **nxtbuf, **ap;
156         char *cp, *scanl, name[NAMESZ];
157         register struct comp *cptr, **savecomp;
158         FILE *out;
159
160         if (!(out = fopen(drft, "w"))) {
161                 adios(drft, "unable to create");
162         }
163
164         /* get new format string */
165         cp = new_fs(form ? form : rcvdistcomps, NULL);
166         format_len = strlen(cp);
167         ncomps = fmt_compile(cp, &fmt) + 1;
168         if (!(nxtbuf = compbuffers =
169                         (char **) calloc((size_t) ncomps, sizeof(char *)))) {
170                 adios(NULL, "unable to allocate component buffers");
171         }
172         if (!(savecomp = used_buf =
173                         (struct comp **) calloc((size_t) (ncomps + 1),
174                         sizeof(struct comp *)))) {
175                 adios(NULL, "unable to allocate component buffer stack");
176         }
177         savecomp += ncomps + 1;
178         *--savecomp = 0;
179
180         for (i = ncomps; i--;) {
181                 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
182         }
183         nxtbuf = compbuffers;
184         tmpbuf = *nxtbuf++;
185
186         for (ap = addrcomps; *ap; ap++) {
187                 FINDCOMP(cptr, *ap);
188                 if (cptr) {
189                         cptr->c_type |= CT_ADDR;
190                 }
191         }
192
193         FINDCOMP(cptr, "addresses");
194         if (cptr) {
195                 cptr->c_text = addrs;
196         }
197         state = FLD;
198         while (1) {
199                 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
200                 switch (state) {
201                 case FLD:
202                 case FLDPLUS:
203                         if ((cptr = wantcomp[CHASH(name)])) {
204                                 do {
205                                         if (mh_strcasecmp(name, cptr->c_name)!=0) {
206                                                 continue;
207                                         }
208                                         char_read += msg_count;
209                                         if (!cptr->c_text) {
210                                                 cptr->c_text = tmpbuf;
211                                                 *--savecomp = cptr;
212                                                 tmpbuf = *nxtbuf++;
213                                         } else {
214                                                 cp = cptr->c_text;
215                                                 i = strlen(cp) - 1;
216                                                 if (cp[i] == '\n') {
217                                                         if (cptr->c_type & CT_ADDR) {
218                                                                 cp[i] = 0;
219                                                                 cp = add(",\n\t", cp);
220                                                         } else {
221                                                                 cp = add("\t", cp);
222                                                         }
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                                 } while ((cptr = cptr->c_next));
233                         }
234
235                         while (state == FLDPLUS) {
236                                 state = m_getfld(state, name, tmpbuf,
237                                                 SBUFSIZ, inb);
238                         }
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         }
263         fclose(out);
264
265         free(scanl);
266         for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
267                         nxtbuf++, i--) {
268                 free(cptr->c_text);
269         }
270         while (i-- > 0) {
271                 free(*nxtbuf++);
272         }
273         free(compbuffers);
274         free(used_buf);
275 }
276
277
278 static void
279 unlink_done()
280 {
281         if (*backup) {
282                 unlink(backup);
283         }
284         if (*drft) {
285                 unlink(drft);
286         }
287         if (*tmpfil) {
288                 unlink(tmpfil);
289         }
290 }