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