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