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