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