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