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