nonstatic function for atexit, check if atexit fails
[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 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         if (atexit(unlink_done) != 0) {
46                 adios(NULL, "atexit failed");
47         }
48
49         setlocale(LC_ALL, "");
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                                 exit(1);
64                         case UNKWNSW:
65                                 vec[vecp++] = --cp;
66                                 continue;
67
68                         case HELPSW:
69                                 snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
70                                 print_help(buf, switches, 1);
71                                 exit(0);
72                         case VERSIONSW:
73                                 print_version(invo_name);
74                                 exit(0);
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 spost] 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                 /* sysexits.h EX_DATAERR */
109                 exit(1);
110         }
111
112         vec[0] = "spost";
113         vec[vecp++] = "-dist";
114         vec[vecp++] = drft;
115         vec[vecp] = NULL;
116
117         execvp(*vec, vec);
118         fprintf(stderr, "unable to exec ");
119         perror(*vec);
120         _exit(1);
121         return 0;  /* dead code to satisfy the compiler */
122 }
123
124 /* very similar to routine in replsbr.c */
125
126 #define SBUFSIZ 256
127
128 static struct format *fmt;
129
130 static int ncomps = 0;
131 static char **compbuffers = 0;
132 static struct comp **used_buf = 0;
133
134 static int dat[5];
135
136 static char *addrcomps[] = {
137         "from",
138         "sender",
139         "reply-to",
140         "to",
141         "cc",
142         "bcc",
143         "resent-from",
144         "resent-sender",
145         "resent-reply-to",
146         "resent-to",
147         "resent-cc",
148         "resent-bcc",
149         NULL
150 };
151
152
153 static void
154 rcvdistout(FILE *inb, char *form, char *addrs)
155 {
156         register int char_read = 0, format_len, i, state;
157         register char *tmpbuf, **nxtbuf, **ap;
158         char *cp, *scanl, name[NAMESZ];
159         register struct comp *cptr, **savecomp;
160         FILE *out;
161
162         if (!(out = fopen(drft, "w"))) {
163                 adios(drft, "unable to create");
164         }
165
166         /* get new format string */
167         cp = new_fs(form ? form : rcvdistcomps, NULL);
168         format_len = strlen(cp);
169         ncomps = fmt_compile(cp, &fmt) + 1;
170         if (!(nxtbuf = compbuffers =
171                         (char **) calloc((size_t) ncomps, sizeof(char *)))) {
172                 adios(NULL, "unable to allocate component buffers");
173         }
174         if (!(savecomp = used_buf =
175                         (struct comp **) calloc((size_t) (ncomps + 1),
176                         sizeof(struct comp *)))) {
177                 adios(NULL, "unable to allocate component buffer stack");
178         }
179         savecomp += ncomps + 1;
180         *--savecomp = 0;
181
182         for (i = ncomps; i--;) {
183                 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
184         }
185         nxtbuf = compbuffers;
186         tmpbuf = *nxtbuf++;
187
188         for (ap = addrcomps; *ap; ap++) {
189                 FINDCOMP(cptr, *ap);
190                 if (cptr) {
191                         cptr->c_type |= CT_ADDR;
192                 }
193         }
194
195         FINDCOMP(cptr, "addresses");
196         if (cptr) {
197                 cptr->c_text = addrs;
198         }
199         state = FLD;
200         while (1) {
201                 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
202                 switch (state) {
203                 case FLD:
204                 case FLDPLUS:
205                         if ((cptr = wantcomp[CHASH(name)])) {
206                                 do {
207                                         if (mh_strcasecmp(name, cptr->c_name)!=0) {
208                                                 continue;
209                                         }
210                                         char_read += msg_count;
211                                         if (!cptr->c_text) {
212                                                 cptr->c_text = tmpbuf;
213                                                 *--savecomp = cptr;
214                                                 tmpbuf = *nxtbuf++;
215                                         } else {
216                                                 cp = cptr->c_text;
217                                                 i = strlen(cp) - 1;
218                                                 if (cp[i] == '\n') {
219                                                         if (cptr->c_type & CT_ADDR) {
220                                                                 cp[i] = 0;
221                                                                 cp = add(",\n\t", cp);
222                                                         } else {
223                                                                 cp = add("\t", cp);
224                                                         }
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                                 } while ((cptr = cptr->c_next));
235                         }
236
237                         while (state == FLDPLUS) {
238                                 state = m_getfld(state, name, tmpbuf,
239                                                 SBUFSIZ, inb);
240                         }
241                         break;
242
243                 case LENERR:
244                 case FMTERR:
245                 case BODY:
246                 case FILEEOF:
247                         goto finished;
248
249                 default:
250                         adios(NULL, "m_getfld() returned %d", state);
251                 }
252         }
253 finished: ;
254
255         i = format_len + char_read + 256;
256         scanl = mh_xmalloc((size_t) i + 2);
257         dat[0] = dat[1] = dat[2] = dat[4] = 0;
258         dat[3] = OUTPUTLINELEN;
259         fmt_scan(fmt, scanl, i, dat);
260         fputs(scanl, out);
261
262         if (ferror(out)) {
263                 adios(drft, "error writing");
264         }
265         fclose(out);
266
267         free(scanl);
268         for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
269                         nxtbuf++, i--) {
270                 free(cptr->c_text);
271         }
272         while (i-- > 0) {
273                 free(*nxtbuf++);
274         }
275         free(compbuffers);
276         free(used_buf);
277 }
278
279
280 void
281 unlink_done()
282 {
283         if (*backup) {
284                 unlink(backup);
285         }
286         if (*drft) {
287                 unlink(drft);
288         }
289         if (*tmpfil) {
290                 unlink(tmpfil);
291         }
292 }