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