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