]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
4f7a83f7b30635993d17e9eadc97ea900364891e
[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 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
108 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
109 }
110 strncpy (tmpfil, tfile, sizeof(tmpfil));
111
112 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
113 fseek (fp, 0L, SEEK_SET);
114
115 if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
116 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
117 }
118 strncpy (drft, tfile, sizeof(tmpfil));
119
120 rcvdistout (fp, form, addrs);
121 fclose (fp);
122
123 if (distout (drft, tmpfil, backup) == NOTOK)
124 done (1);
125
126 vec[vecp++] = "-dist";
127 vec[vecp++] = drft;
128 if ((cp = context_find ("mhlproc"))) {
129 vec[vecp++] = "-mhlproc";
130 vec[vecp++] = cp;
131 }
132 vec[vecp] = NULL;
133
134 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
135 sleep (5);
136 switch (child_id) {
137 case NOTOK:
138 admonish (NULL, "unable to fork");/* fall */
139 case OK:
140 execvp (program, vec);
141 fprintf (stderr, "unable to exec ");
142 perror (postproc);
143 _exit (1);
144
145 default:
146 done (pidXwait(child_id, postproc));
147 }
148
149 return 0; /* dead code to satisfy the compiler */
150 }
151
152 /* very similar to routine in replsbr.c */
153
154 #define SBUFSIZ 256
155
156 static int outputlinelen = OUTPUTLINELEN;
157
158 static struct format *fmt;
159
160 static int dat[5];
161
162 static char *addrcomps[] = {
163 "from",
164 "sender",
165 "reply-to",
166 "to",
167 "cc",
168 "bcc",
169 "resent-from",
170 "resent-sender",
171 "resent-reply-to",
172 "resent-to",
173 "resent-cc",
174 "resent-bcc",
175 NULL
176 };
177
178
179 static void
180 rcvdistout (FILE *inb, char *form, char *addrs)
181 {
182 register int char_read = 0, format_len, i, state;
183 register char **ap;
184 char *cp, *scanl, name[NAMESZ], tmpbuf[SBUFSIZ];
185 register struct comp *cptr;
186 FILE *out;
187 m_getfld_state_t gstate = 0;
188
189 if (!(out = fopen (drft, "w")))
190 adios (drft, "unable to create");
191
192 /* get new format string */
193 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
194 format_len = strlen (cp);
195 fmt_compile (cp, &fmt, 1);
196
197 for (ap = addrcomps; *ap; ap++) {
198 cptr = fmt_findcomp (*ap);
199 if (cptr)
200 cptr->c_type |= CT_ADDR;
201 }
202
203 cptr = fmt_findcomp ("addresses");
204 if (cptr)
205 cptr->c_text = addrs;
206
207 for (;;) {
208 int msg_count = SBUFSIZ;
209 switch (state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb)) {
210 case FLD:
211 case FLDPLUS:
212 i = fmt_addcomptext(name, tmpbuf);
213 if (i != -1) {
214 char_read += msg_count;
215 while (state == FLDPLUS) {
216 msg_count = SBUFSIZ;
217 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
218 fmt_appendcomp(i, name, tmpbuf);
219 char_read += msg_count;
220 }
221 }
222
223 while (state == FLDPLUS) {
224 msg_count = SBUFSIZ;
225 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
226 }
227 break;
228
229 case LENERR:
230 case FMTERR:
231 case BODY:
232 case FILEEOF:
233 goto finished;
234
235 default:
236 adios (NULL, "m_getfld() returned %d", state);
237 }
238 }
239 finished: ;
240 m_getfld_state_destroy (&gstate);
241
242 i = format_len + char_read + 256;
243 scanl = mh_xmalloc ((size_t) i + 2);
244 dat[0] = dat[1] = dat[2] = dat[4] = 0;
245 dat[3] = outputlinelen;
246 fmt_scan (fmt, scanl, i + 1, i, dat, NULL);
247 fputs (scanl, out);
248
249 if (ferror (out))
250 adios (drft, "error writing");
251 fclose (out);
252
253 free (scanl);
254 fmt_free(fmt, 1);
255 }
256
257
258 static void
259 unlink_done (int status)
260 {
261 if (backup[0])
262 unlink (backup);
263 if (drft[0])
264 unlink (drft);
265 if (tmpfil[0])
266 unlink (tmpfil);
267
268 exit (status ? RCV_MBX : RCV_MOK);
269 }