]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
8b672cd7ba849994a551939d0c1eca2b63b3783d
[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 setlocale(LC_ALL, "");
54 invo_name = r1bindex (argv[0], '/');
55
56 /* read user profile/context */
57 context_read();
58
59 /*
60 * Configure this now, since any unknown switches to rcvdist get
61 * sent to postproc
62 */
63
64 vec = argsplit(postproc, &program, &vecp);
65
66 mts_init (invo_name);
67 arguments = getarguments (invo_name, argc, argv, 1);
68 argp = arguments;
69
70 while ((cp = *argp++)) {
71 if (*cp == '-') {
72 switch (smatch (++cp, switches)) {
73 case AMBIGSW:
74 ambigsw (cp, switches);
75 done (1);
76 case UNKWNSW:
77 vec[vecp++] = --cp;
78 continue;
79
80 case HELPSW:
81 snprintf (buf, sizeof(buf),
82 "%s [switches] [switches for postproc] address ...",
83 invo_name);
84 print_help (buf, switches, 1);
85 done (0);
86 case VERSIONSW:
87 print_version(invo_name);
88 done (0);
89
90 case FORMSW:
91 if (!(form = *argp++) || *form == '-')
92 adios (NULL, "missing argument to %s", argp[-2]);
93 continue;
94 }
95 }
96 addrs = addrs ? add (cp, add (", ", addrs)) : getcpy (cp);
97 }
98
99 if (addrs == NULL)
100 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
101 invo_name);
102
103 umask (~m_gmprot ());
104
105 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
106 if (tfile == NULL) adios("rcvdist", "unable to create temporary file");
107 strncpy (tmpfil, tfile, sizeof(tmpfil));
108
109 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
110 fseek (fp, 0L, SEEK_SET);
111
112 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
113 if (tfile == NULL) adios("forw", "unable to create temporary file");
114 strncpy (drft, tfile, sizeof(tmpfil));
115
116 rcvdistout (fp, form, addrs);
117 fclose (fp);
118
119 if (distout (drft, tmpfil, backup) == NOTOK)
120 done (1);
121
122 vec[vecp++] = "-dist";
123 vec[vecp++] = drft;
124 if ((cp = context_find ("mhlproc"))) {
125 vec[vecp++] = "-mhlproc";
126 vec[vecp++] = cp;
127 }
128 vec[vecp] = NULL;
129
130 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
131 sleep (5);
132 switch (child_id) {
133 case NOTOK:
134 admonish (NULL, "unable to fork");/* fall */
135 case OK:
136 execvp (program, vec);
137 fprintf (stderr, "unable to exec ");
138 perror (postproc);
139 _exit (1);
140
141 default:
142 done (pidXwait(child_id, postproc));
143 }
144
145 return 0; /* dead code to satisfy the compiler */
146 }
147
148 /* very similar to routine in replsbr.c */
149
150 #define SBUFSIZ 256
151
152 static int outputlinelen = OUTPUTLINELEN;
153
154 static struct format *fmt;
155
156 static int dat[5];
157
158 static char *addrcomps[] = {
159 "from",
160 "sender",
161 "reply-to",
162 "to",
163 "cc",
164 "bcc",
165 "resent-from",
166 "resent-sender",
167 "resent-reply-to",
168 "resent-to",
169 "resent-cc",
170 "resent-bcc",
171 NULL
172 };
173
174
175 static void
176 rcvdistout (FILE *inb, char *form, char *addrs)
177 {
178 register int char_read = 0, format_len, i, state;
179 register char **ap;
180 char *cp, *scanl, name[NAMESZ], tmpbuf[SBUFSIZ];
181 register 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 = mh_xmalloc ((size_t) i + 2);
240 dat[0] = dat[1] = dat[2] = dat[4] = 0;
241 dat[3] = outputlinelen;
242 fmt_scan (fmt, scanl, i + 1, i, dat, NULL);
243 fputs (scanl, out);
244
245 if (ferror (out))
246 adios (drft, "error writing");
247 fclose (out);
248
249 free (scanl);
250 fmt_free(fmt, 1);
251 }
252
253
254 static void
255 unlink_done (int status)
256 {
257 if (backup[0])
258 unlink (backup);
259 if (drft[0])
260 unlink (drft);
261 if (tmpfil[0])
262 unlink (tmpfil);
263
264 exit (status ? RCV_MBX : RCV_MOK);
265 }