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