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