]> diplodocus.org Git - nmh/blob - uip/rcvdist.c
Update manpages to use .TP for tagged paragraphs (part I).
[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 m_getfld_state_t gstate = 0;
180
181 if (!(out = fopen (drft, "w")))
182 adios (drft, "unable to create");
183
184 /* get new format string */
185 cp = new_fs (form ? form : rcvdistcomps, NULL, NULL);
186 format_len = strlen (cp);
187 fmt_compile (cp, &fmt, 1);
188
189 for (ap = addrcomps; *ap; ap++) {
190 cptr = fmt_findcomp (*ap);
191 if (cptr)
192 cptr->c_type |= CT_ADDR;
193 }
194
195 cptr = fmt_findcomp ("addresses");
196 if (cptr)
197 cptr->c_text = addrs;
198
199 for (;;) {
200 int msg_count = SBUFSIZ;
201 switch (state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb)) {
202 case FLD:
203 case FLDPLUS:
204 i = fmt_addcomptext(name, tmpbuf);
205 if (i != -1) {
206 char_read += msg_count;
207 while (state == FLDPLUS) {
208 msg_count = SBUFSIZ;
209 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
210 fmt_appendcomp(i, name, tmpbuf);
211 char_read += msg_count;
212 }
213 }
214
215 while (state == FLDPLUS) {
216 msg_count = SBUFSIZ;
217 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
218 }
219 break;
220
221 case LENERR:
222 case FMTERR:
223 case BODY:
224 case FILEEOF:
225 goto finished;
226
227 default:
228 adios (NULL, "m_getfld() returned %d", state);
229 }
230 }
231 finished: ;
232 m_getfld_state_destroy (&gstate);
233
234 i = format_len + char_read + 256;
235 scanl = mh_xmalloc ((size_t) i + 2);
236 dat[0] = dat[1] = dat[2] = dat[4] = 0;
237 dat[3] = outputlinelen;
238 fmt_scan (fmt, scanl, i + 1, i, dat);
239 fputs (scanl, out);
240
241 if (ferror (out))
242 adios (drft, "error writing");
243 fclose (out);
244
245 free (scanl);
246 fmt_free(fmt, 1);
247 }
248
249
250 static void
251 unlink_done (int status)
252 {
253 if (backup[0])
254 unlink (backup);
255 if (drft[0])
256 unlink (drft);
257 if (tmpfil[0])
258 unlink (tmpfil);
259
260 exit (status ? RCV_MBX : RCV_MOK);
261 }