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