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