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