]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
3a5a34811a37e819f55d0acaded1269a7d71126f
[nmh] / uip / rcvdist.c
1 /* rcvdist.c -- asynchronously redistribute messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "distsbr.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/smatch.h"
14 #include "sbr/cpydata.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
17 #include "sbr/pidstatus.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/arglist.h"
21 #include "sbr/error.h"
22 #include "h/fmt_scan.h"
23 #include "h/rcvmail.h"
24 #include "h/tws.h"
25 #include "h/mts.h"
26 #include "h/done.h"
27 #include "h/utils.h"
28 #include "sbr/m_mktemp.h"
29
30 #define RCVDIST_SWITCHES \
31 X("form formfile", 4, FORMSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
34
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(RCVDIST);
37 #undef X
38
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(RCVDIST, switches);
41 #undef X
42
43 static char backup[BUFSIZ] = "";
44 static char drft[BUFSIZ] = "";
45 static char tmpfil[BUFSIZ] = "";
46
47 /*
48 * prototypes
49 */
50 static void rcvdistout (FILE *, char *, char *);
51 static void unlink_done (int) NORETURN;
52
53
54 int
55 main (int argc, char **argv)
56 {
57 pid_t child_id;
58 int vecp;
59 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ], *program;
60 char **argp, **arguments, **vec;
61 FILE *fp;
62 char *tfile = NULL;
63
64 if (nmh_init(argv[0], true, false)) { return 1; }
65
66 set_done(unlink_done);
67
68 /*
69 * Configure this now, since any unknown switches to rcvdist get
70 * sent to postproc
71 */
72
73 vec = argsplit(postproc, &program, &vecp);
74
75 mts_init ();
76 arguments = getarguments (invo_name, argc, argv, 1);
77 argp = arguments;
78
79 while ((cp = *argp++)) {
80 if (*cp == '-') {
81 switch (smatch (++cp, switches)) {
82 case AMBIGSW:
83 ambigsw (cp, switches);
84 done (1);
85 case UNKWNSW:
86 vec[vecp++] = --cp;
87 continue;
88
89 case HELPSW:
90 snprintf (buf, sizeof(buf),
91 "%s [switches] [switches for postproc] address ...",
92 invo_name);
93 print_help (buf, switches, 1);
94 done (0);
95 case VERSIONSW:
96 print_version(invo_name);
97 done (0);
98
99 case FORMSW:
100 if (!(form = *argp++) || *form == '-')
101 die("missing argument to %s", argp[-2]);
102 continue;
103 }
104 }
105 addrs = addrs ? add (cp, add (", ", addrs)) : mh_xstrdup(cp);
106 }
107
108 if (addrs == NULL)
109 die("usage: %s [switches] [switches for postproc] address ...",
110 invo_name);
111
112 umask (~m_gmprot ());
113
114 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
115 die("unable to create temporary file in %s", get_temp_dir());
116 }
117 strncpy (tmpfil, tfile, sizeof(tmpfil));
118
119 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
120 fseek (fp, 0L, SEEK_SET);
121
122 if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
123 die("unable to create temporary file in %s", get_temp_dir());
124 }
125 strncpy (drft, tfile, sizeof(tmpfil));
126
127 rcvdistout (fp, form, addrs);
128 fclose (fp);
129
130 if (distout (drft, tmpfil, backup) == NOTOK)
131 done (1);
132
133 vec[vecp++] = "-dist";
134 if ((cp = context_find ("mhlproc"))) {
135 vec[vecp++] = "-mhlproc";
136 vec[vecp++] = cp;
137 }
138 vec[vecp++] = drft;
139 vec[vecp] = NULL;
140
141 child_id = fork();
142 switch (child_id) {
143 case NOTOK:
144 adios("fork", "failed:");
145
146 case OK:
147 execvp (program, vec);
148 fprintf (stderr, "unable to exec ");
149 perror (postproc);
150 _exit (1);
151
152 default:
153 done (pidXwait(child_id, postproc));
154 }
155
156 return 0; /* dead code to satisfy the compiler */
157 }
158
159 /* very similar to routine in replsbr.c */
160
161 static int outputlinelen = OUTPUTLINELEN;
162
163 static struct format *fmt;
164
165 static int dat[5];
166
167 static char *addrcomps[] = {
168 "from",
169 "sender",
170 "reply-to",
171 "to",
172 "cc",
173 "bcc",
174 "resent-from",
175 "resent-sender",
176 "resent-reply-to",
177 "resent-to",
178 "resent-cc",
179 "resent-bcc",
180 NULL
181 };
182
183
184 static void
185 rcvdistout (FILE *inb, char *form, char *addrs)
186 {
187 int char_read = 0, format_len, i, state;
188 char **ap;
189 char *cp, name[NAMESZ], tmpbuf[NMH_BUFSIZ];
190 charstring_t scanl;
191 struct comp *cptr;
192 FILE *out;
193 m_getfld_state_t gstate;
194
195 if (!(out = fopen (drft, "w")))
196 adios (drft, "unable to create");
197
198 /* get new format string */
199 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
200 format_len = strlen (cp);
201 fmt_compile (cp, &fmt, 1);
202
203 for (ap = addrcomps; *ap; ap++) {
204 cptr = fmt_findcomp (*ap);
205 if (cptr)
206 cptr->c_type |= CT_ADDR;
207 }
208
209 cptr = fmt_findcomp ("addresses");
210 if (cptr)
211 cptr->c_text = addrs;
212
213 gstate = m_getfld_state_init(inb);
214 for (;;) {
215 int msg_count = sizeof tmpbuf;
216 switch (state = m_getfld2(&gstate, name, tmpbuf, &msg_count)) {
217 case FLD:
218 case FLDPLUS:
219 i = fmt_addcomptext(name, tmpbuf);
220 if (i != -1) {
221 char_read += msg_count;
222 while (state == FLDPLUS) {
223 msg_count = sizeof tmpbuf;
224 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
225 fmt_appendcomp(i, name, tmpbuf);
226 char_read += msg_count;
227 }
228 }
229
230 while (state == FLDPLUS) {
231 msg_count = sizeof tmpbuf;
232 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
233 }
234 break;
235
236 case LENERR:
237 case FMTERR:
238 case BODY:
239 case FILEEOF:
240 goto finished;
241
242 default:
243 die("m_getfld2() returned %d", state);
244 }
245 }
246 finished: ;
247 m_getfld_state_destroy (&gstate);
248
249 i = format_len + char_read + 256;
250 scanl = charstring_create (i + 2);
251 dat[0] = dat[1] = dat[2] = dat[4] = 0;
252 dat[3] = outputlinelen;
253 fmt_scan (fmt, scanl, i, dat, NULL);
254 fputs (charstring_buffer (scanl), out);
255
256 if (ferror (out))
257 adios (drft, "error writing");
258 fclose (out);
259
260 charstring_free (scanl);
261 fmt_free(fmt, 1);
262 }
263
264
265 static void NORETURN
266 unlink_done (int status)
267 {
268 if (backup[0])
269 (void) m_unlink (backup);
270 if (drft[0])
271 (void) m_unlink (drft);
272 if (tmpfil[0])
273 (void) m_unlink (tmpfil);
274
275 exit (status ? RCV_MBX : RCV_MOK);
276 }