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