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