]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
Add back missing include of <sys/wait.h>
[nmh] / uip / rcvdist.c
1
2 /*
3 * rcvdist.c -- asynchronously redistribute messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/fmt_scan.h>
12 #include <h/rcvmail.h>
13 #include <h/tws.h>
14 #include <h/mts.h>
15 #include <h/utils.h>
16
17 static struct swit switches[] = {
18 #define FORMSW 0
19 { "form formfile", 4 },
20 #define VERSIONSW 1
21 { "version", 0 },
22 #define HELPSW 2
23 { "help", 0 },
24 { NULL, 0 }
25 };
26
27 static char backup[BUFSIZ] = "";
28 static char drft[BUFSIZ] = "";
29 static char tmpfil[BUFSIZ] = "";
30
31 /*
32 * prototypes
33 */
34 static void rcvdistout (FILE *, char *, char *);
35 static void unlink_done (int) NORETURN;
36
37
38 int
39 main (int argc, char **argv)
40 {
41 pid_t child_id;
42 int i, vecp = 1;
43 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
44 char **argp, **arguments, *vec[MAXARGS];
45 FILE *fp;
46 char *tfile = NULL;
47
48 done=unlink_done;
49
50 #ifdef LOCALE
51 setlocale(LC_ALL, "");
52 #endif
53 invo_name = r1bindex (argv[0], '/');
54
55 /* read user profile/context */
56 context_read();
57
58 mts_init (invo_name);
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 done (1);
68 case UNKWNSW:
69 vec[vecp++] = --cp;
70 continue;
71
72 case HELPSW:
73 snprintf (buf, sizeof(buf),
74 "%s [switches] [switches for postproc] address ...",
75 invo_name);
76 print_help (buf, switches, 1);
77 done (1);
78 case VERSIONSW:
79 print_version(invo_name);
80 done (1);
81
82 case FORMSW:
83 if (!(form = *argp++) || *form == '-')
84 adios (NULL, "missing argument to %s", argp[-2]);
85 continue;
86 }
87 }
88 addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
89 }
90
91 if (addrs == NULL)
92 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
93 invo_name);
94
95 umask (~m_gmprot ());
96
97 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
98 if (tfile == NULL) adios("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("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 done (1);
113
114 vec[0] = r1bindex (postproc, '/');
115 vec[vecp++] = "-dist";
116 vec[vecp++] = drft;
117 vec[vecp] = NULL;
118
119 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
120 sleep (5);
121 switch (child_id) {
122 case NOTOK:
123 admonish (NULL, "unable to fork");/* fall */
124 case OK:
125 execvp (postproc, vec);
126 fprintf (stderr, "unable to exec ");
127 perror (postproc);
128 _exit (1);
129
130 default:
131 done (pidXwait(child_id, postproc));
132 }
133
134 return 0; /* dead code to satisfy the compiler */
135 }
136
137 /* very similar to routine in replsbr.c */
138
139 #define SBUFSIZ 256
140
141 static int outputlinelen = OUTPUTLINELEN;
142
143 static struct format *fmt;
144
145 static int ncomps = 0;
146 static char **compbuffers = 0;
147 static struct comp **used_buf = 0;
148
149 static int dat[5];
150
151 static char *addrcomps[] = {
152 "from",
153 "sender",
154 "reply-to",
155 "to",
156 "cc",
157 "bcc",
158 "resent-from",
159 "resent-sender",
160 "resent-reply-to",
161 "resent-to",
162 "resent-cc",
163 "resent-bcc",
164 NULL
165 };
166
167
168 static void
169 rcvdistout (FILE *inb, char *form, char *addrs)
170 {
171 register int char_read = 0, format_len, i, state;
172 register char *tmpbuf, **nxtbuf, **ap;
173 char *cp, *scanl, name[NAMESZ];
174 register struct comp *cptr, **savecomp;
175 FILE *out;
176
177 if (!(out = fopen (drft, "w")))
178 adios (drft, "unable to create");
179
180 /* get new format string */
181 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
182 format_len = strlen (cp);
183 ncomps = fmt_compile (cp, &fmt) + 1;
184 if (!(nxtbuf = compbuffers = (char **) calloc ((size_t) ncomps, sizeof(char *))))
185 adios (NULL, "unable to allocate component buffers");
186 if (!(savecomp = used_buf = (struct comp **) calloc ((size_t) (ncomps + 1), sizeof(struct comp *))))
187 adios (NULL, "unable to allocate component buffer stack");
188 savecomp += ncomps + 1;
189 *--savecomp = 0;
190
191 for (i = ncomps; i--;)
192 *nxtbuf++ = mh_xmalloc (SBUFSIZ);
193 nxtbuf = compbuffers;
194 tmpbuf = *nxtbuf++;
195
196 for (ap = addrcomps; *ap; ap++) {
197 FINDCOMP (cptr, *ap);
198 if (cptr)
199 cptr->c_type |= CT_ADDR;
200 }
201
202 FINDCOMP (cptr, "addresses");
203 if (cptr)
204 cptr->c_text = addrs;
205
206 for (state = FLD;;) {
207 switch (state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb)) {
208 case FLD:
209 case FLDPLUS:
210 if ((cptr = wantcomp[CHASH (name)]))
211 do {
212 if (!mh_strcasecmp (name, cptr->c_name)) {
213 char_read += msg_count;
214 if (!cptr->c_text) {
215 cptr->c_text = tmpbuf;
216 *--savecomp = cptr;
217 tmpbuf = *nxtbuf++;
218 }
219 else {
220 i = strlen (cp = cptr->c_text) - 1;
221 if (cp[i] == '\n') {
222 if (cptr->c_type & CT_ADDR) {
223 cp[i] = 0;
224 cp = add (",\n\t", cp);
225 }
226 else
227 cp = add ("\t", cp);
228 }
229 cptr->c_text = add (tmpbuf, cp);
230 }
231 while (state == FLDPLUS) {
232 state = m_getfld (state, name, tmpbuf,
233 SBUFSIZ, inb);
234 cptr->c_text = add (tmpbuf, cptr->c_text);
235 char_read += msg_count;
236 }
237 break;
238 }
239 } while ((cptr = cptr->c_next));
240
241 while (state == FLDPLUS)
242 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
243 break;
244
245 case LENERR:
246 case FMTERR:
247 case BODY:
248 case FILEEOF:
249 goto finished;
250
251 default:
252 adios (NULL, "m_getfld() returned %d", state);
253 }
254 }
255 finished: ;
256
257 i = format_len + char_read + 256;
258 scanl = mh_xmalloc ((size_t) i + 2);
259 dat[0] = dat[1] = dat[2] = dat[4] = 0;
260 dat[3] = outputlinelen;
261 fmt_scan (fmt, scanl, i, dat);
262 fputs (scanl, out);
263
264 if (ferror (out))
265 adios (drft, "error writing");
266 fclose (out);
267
268 free (scanl);
269 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
270 free (cptr->c_text);
271 while (i-- > 0)
272 free (*nxtbuf++);
273 free ((char *) compbuffers);
274 free ((char *) used_buf);
275 }
276
277
278 static void
279 unlink_done (int status)
280 {
281 if (backup[0])
282 unlink (backup);
283 if (drft[0])
284 unlink (drft);
285 if (tmpfil[0])
286 unlink (tmpfil);
287
288 exit (status ? RCV_MBX : RCV_MOK);
289 }