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