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