]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
Alter mh-chart(7)'s NAME to be lowercase.
[nmh] / uip / rcvdist.c
1
2 /*
3 * rcvdist.c -- asynchronously redistribute messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/fmt_scan.h>
12 #include <h/rcvmail.h>
13 #include <h/tws.h>
14 #include <h/mts.h>
15 #include <h/utils.h>
16
17 #define RCVDIST_SWITCHES \
18 X("form formfile", 4, FORMSW) \
19 X("version", 0, VERSIONSW) \
20 X("help", 0, HELPSW) \
21
22 #define X(sw, minchars, id) id,
23 DEFINE_SWITCH_ENUM(RCVDIST);
24 #undef X
25
26 #define X(sw, minchars, id) { sw, minchars, id },
27 DEFINE_SWITCH_ARRAY(RCVDIST, switches);
28 #undef X
29
30 static char backup[BUFSIZ] = "";
31 static char drft[BUFSIZ] = "";
32 static char tmpfil[BUFSIZ] = "";
33
34 /*
35 * prototypes
36 */
37 static void rcvdistout (FILE *, char *, char *);
38 static void unlink_done (int) NORETURN;
39
40
41 int
42 main (int argc, char **argv)
43 {
44 pid_t child_id;
45 int i, vecp;
46 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ], *program;
47 char **argp, **arguments, **vec;
48 FILE *fp;
49 char *tfile = NULL;
50
51 if (nmh_init(argv[0], 2)) { return 1; }
52
53 done=unlink_done;
54
55 /*
56 * Configure this now, since any unknown switches to rcvdist get
57 * sent to postproc
58 */
59
60 vec = argsplit(postproc, &program, &vecp);
61
62 mts_init ();
63 arguments = getarguments (invo_name, argc, argv, 1);
64 argp = arguments;
65
66 while ((cp = *argp++)) {
67 if (*cp == '-') {
68 switch (smatch (++cp, switches)) {
69 case AMBIGSW:
70 ambigsw (cp, switches);
71 done (1);
72 case UNKWNSW:
73 vec[vecp++] = --cp;
74 continue;
75
76 case HELPSW:
77 snprintf (buf, sizeof(buf),
78 "%s [switches] [switches for postproc] address ...",
79 invo_name);
80 print_help (buf, switches, 1);
81 done (0);
82 case VERSIONSW:
83 print_version(invo_name);
84 done (0);
85
86 case FORMSW:
87 if (!(form = *argp++) || *form == '-')
88 adios (NULL, "missing argument to %s", argp[-2]);
89 continue;
90 }
91 }
92 addrs = addrs ? add (cp, add (", ", addrs)) : mh_xstrdup(cp);
93 }
94
95 if (addrs == NULL)
96 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
97 invo_name);
98
99 umask (~m_gmprot ());
100
101 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
102 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
103 }
104 strncpy (tmpfil, tfile, sizeof(tmpfil));
105
106 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
107 fseek (fp, 0L, SEEK_SET);
108
109 if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
110 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
111 }
112 strncpy (drft, tfile, sizeof(tmpfil));
113
114 rcvdistout (fp, form, addrs);
115 fclose (fp);
116
117 if (distout (drft, tmpfil, backup) == NOTOK)
118 done (1);
119
120 vec[vecp++] = "-dist";
121 if ((cp = context_find ("mhlproc"))) {
122 vec[vecp++] = "-mhlproc";
123 vec[vecp++] = cp;
124 }
125 vec[vecp++] = drft;
126 vec[vecp] = NULL;
127
128 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
129 sleep (5);
130 switch (child_id) {
131 case NOTOK:
132 admonish (NULL, "unable to fork");
133 /* FALLTHRU */
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 #define SBUFSIZ 256
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[SBUFSIZ];
180 charstring_t scanl;
181 struct comp *cptr;
182 FILE *out;
183 m_getfld_state_t gstate = 0;
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 for (;;) {
204 int msg_count = SBUFSIZ;
205 switch (state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb)) {
206 case FLD:
207 case FLDPLUS:
208 i = fmt_addcomptext(name, tmpbuf);
209 if (i != -1) {
210 char_read += msg_count;
211 while (state == FLDPLUS) {
212 msg_count = SBUFSIZ;
213 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
214 fmt_appendcomp(i, name, tmpbuf);
215 char_read += msg_count;
216 }
217 }
218
219 while (state == FLDPLUS) {
220 msg_count = SBUFSIZ;
221 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
222 }
223 break;
224
225 case LENERR:
226 case FMTERR:
227 case BODY:
228 case FILEEOF:
229 goto finished;
230
231 default:
232 adios (NULL, "m_getfld() returned %d", state);
233 }
234 }
235 finished: ;
236 m_getfld_state_destroy (&gstate);
237
238 i = format_len + char_read + 256;
239 scanl = charstring_create (i + 2);
240 dat[0] = dat[1] = dat[2] = dat[4] = 0;
241 dat[3] = outputlinelen;
242 fmt_scan (fmt, scanl, i, dat, NULL);
243 fputs (charstring_buffer (scanl), out);
244
245 if (ferror (out))
246 adios (drft, "error writing");
247 fclose (out);
248
249 charstring_free (scanl);
250 fmt_free(fmt, 1);
251 }
252
253
254 static void
255 unlink_done (int status)
256 {
257 if (backup[0])
258 (void) m_unlink (backup);
259 if (drft[0])
260 (void) m_unlink (drft);
261 if (tmpfil[0])
262 (void) m_unlink (tmpfil);
263
264 exit (status ? RCV_MBX : RCV_MOK);
265 }