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