]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
sbr/dtime.c: Remove struct-assigning twscopy().
[nmh] / uip / rcvdist.c
1 /* rcvdist.c -- asynchronously redistribute messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/fmt_scan.h>
10 #include <h/rcvmail.h>
11 #include <h/tws.h>
12 #include <h/mts.h>
13 #include <h/utils.h>
14
15 #define RCVDIST_SWITCHES \
16 X("form formfile", 4, FORMSW) \
17 X("version", 0, VERSIONSW) \
18 X("help", 0, HELPSW) \
19
20 #define X(sw, minchars, id) id,
21 DEFINE_SWITCH_ENUM(RCVDIST);
22 #undef X
23
24 #define X(sw, minchars, id) { sw, minchars, id },
25 DEFINE_SWITCH_ARRAY(RCVDIST, switches);
26 #undef X
27
28 static char backup[BUFSIZ] = "";
29 static char drft[BUFSIZ] = "";
30 static char tmpfil[BUFSIZ] = "";
31
32 /*
33 * prototypes
34 */
35 static void rcvdistout (FILE *, char *, char *);
36 static void unlink_done (int) NORETURN;
37
38
39 int
40 main (int argc, char **argv)
41 {
42 pid_t child_id;
43 int i, vecp;
44 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ], *program;
45 char **argp, **arguments, **vec;
46 FILE *fp;
47 char *tfile = NULL;
48
49 if (nmh_init(argv[0], 2)) { return 1; }
50
51 done=unlink_done;
52
53 /*
54 * Configure this now, since any unknown switches to rcvdist get
55 * sent to postproc
56 */
57
58 vec = argsplit(postproc, &program, &vecp);
59
60 mts_init ();
61 arguments = getarguments (invo_name, argc, argv, 1);
62 argp = arguments;
63
64 while ((cp = *argp++)) {
65 if (*cp == '-') {
66 switch (smatch (++cp, switches)) {
67 case AMBIGSW:
68 ambigsw (cp, switches);
69 done (1);
70 case UNKWNSW:
71 vec[vecp++] = --cp;
72 continue;
73
74 case HELPSW:
75 snprintf (buf, sizeof(buf),
76 "%s [switches] [switches for postproc] address ...",
77 invo_name);
78 print_help (buf, switches, 1);
79 done (0);
80 case VERSIONSW:
81 print_version(invo_name);
82 done (0);
83
84 case FORMSW:
85 if (!(form = *argp++) || *form == '-')
86 adios (NULL, "missing argument to %s", argp[-2]);
87 continue;
88 }
89 }
90 addrs = addrs ? add (cp, add (", ", addrs)) : mh_xstrdup(cp);
91 }
92
93 if (addrs == NULL)
94 adios (NULL, "usage: %s [switches] [switches for postproc] address ...",
95 invo_name);
96
97 umask (~m_gmprot ());
98
99 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
100 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
101 }
102 strncpy (tmpfil, tfile, sizeof(tmpfil));
103
104 cpydata (fileno (stdin), fileno (fp), "message", tmpfil);
105 fseek (fp, 0L, SEEK_SET);
106
107 if ((tfile = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
108 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
109 }
110 strncpy (drft, tfile, sizeof(tmpfil));
111
112 rcvdistout (fp, form, addrs);
113 fclose (fp);
114
115 if (distout (drft, tmpfil, backup) == NOTOK)
116 done (1);
117
118 vec[vecp++] = "-dist";
119 if ((cp = context_find ("mhlproc"))) {
120 vec[vecp++] = "-mhlproc";
121 vec[vecp++] = cp;
122 }
123 vec[vecp++] = drft;
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 inform("unable to fork, continuing...");
131 /* FALLTHRU */
132 case OK:
133 execvp (program, vec);
134 fprintf (stderr, "unable to exec ");
135 perror (postproc);
136 _exit (1);
137
138 default:
139 done (pidXwait(child_id, postproc));
140 }
141
142 return 0; /* dead code to satisfy the compiler */
143 }
144
145 /* very similar to routine in replsbr.c */
146
147 #define SBUFSIZ 256
148
149 static int outputlinelen = OUTPUTLINELEN;
150
151 static struct format *fmt;
152
153 static int dat[5];
154
155 static char *addrcomps[] = {
156 "from",
157 "sender",
158 "reply-to",
159 "to",
160 "cc",
161 "bcc",
162 "resent-from",
163 "resent-sender",
164 "resent-reply-to",
165 "resent-to",
166 "resent-cc",
167 "resent-bcc",
168 NULL
169 };
170
171
172 static void
173 rcvdistout (FILE *inb, char *form, char *addrs)
174 {
175 int char_read = 0, format_len, i, state;
176 char **ap;
177 char *cp, name[NAMESZ], tmpbuf[SBUFSIZ];
178 charstring_t scanl;
179 struct comp *cptr;
180 FILE *out;
181 m_getfld_state_t gstate = 0;
182
183 if (!(out = fopen (drft, "w")))
184 adios (drft, "unable to create");
185
186 /* get new format string */
187 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
188 format_len = strlen (cp);
189 fmt_compile (cp, &fmt, 1);
190
191 for (ap = addrcomps; *ap; ap++) {
192 cptr = fmt_findcomp (*ap);
193 if (cptr)
194 cptr->c_type |= CT_ADDR;
195 }
196
197 cptr = fmt_findcomp ("addresses");
198 if (cptr)
199 cptr->c_text = addrs;
200
201 for (;;) {
202 int msg_count = SBUFSIZ;
203 switch (state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb)) {
204 case FLD:
205 case FLDPLUS:
206 i = fmt_addcomptext(name, tmpbuf);
207 if (i != -1) {
208 char_read += msg_count;
209 while (state == FLDPLUS) {
210 msg_count = SBUFSIZ;
211 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
212 fmt_appendcomp(i, name, tmpbuf);
213 char_read += msg_count;
214 }
215 }
216
217 while (state == FLDPLUS) {
218 msg_count = SBUFSIZ;
219 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
220 }
221 break;
222
223 case LENERR:
224 case FMTERR:
225 case BODY:
226 case FILEEOF:
227 goto finished;
228
229 default:
230 adios (NULL, "m_getfld() returned %d", state);
231 }
232 }
233 finished: ;
234 m_getfld_state_destroy (&gstate);
235
236 i = format_len + char_read + 256;
237 scanl = charstring_create (i + 2);
238 dat[0] = dat[1] = dat[2] = dat[4] = 0;
239 dat[3] = outputlinelen;
240 fmt_scan (fmt, scanl, i, dat, NULL);
241 fputs (charstring_buffer (scanl), out);
242
243 if (ferror (out))
244 adios (drft, "error writing");
245 fclose (out);
246
247 charstring_free (scanl);
248 fmt_free(fmt, 1);
249 }
250
251
252 static void
253 unlink_done (int status)
254 {
255 if (backup[0])
256 (void) m_unlink (backup);
257 if (drft[0])
258 (void) m_unlink (drft);
259 if (tmpfil[0])
260 (void) m_unlink (tmpfil);
261
262 exit (status ? RCV_MBX : RCV_MOK);
263 }