remove unnecessary casts
[mmh] / uip / rcvdist.c
1 /*
2 ** rcvdist.c -- asynchronously redistribute messages
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/fmt_scan.h>
11 #include <h/rcvmail.h>
12 #include <h/tws.h>
13 #include <h/utils.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <locale.h>
17 #include <sysexits.h>
18
19 static struct swit switches[] = {
20 #define FORMSW  0
21         { "form formfile",  0 },
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 void unlink_done();
38
39
40 int
41 main(int argc, char **argv)
42 {
43         int vecp = 1;
44         char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
45         char **argp, **arguments, *vec[MAXARGS];
46         FILE *fp;
47         char *tfile = NULL;
48
49         if (atexit(unlink_done) != 0) {
50                 adios(EX_OSERR, NULL, "atexit failed");
51         }
52
53         setlocale(LC_ALL, "");
54         invo_name = mhbasename(argv[0]);
55
56         /* read user profile/context */
57         context_read();
58
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                                 exit(EX_USAGE);
68                         case UNKWNSW:
69                                 vec[vecp++] = --cp;
70                                 continue;
71
72                         case HELPSW:
73                                 snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
74                                 print_help(buf, switches, 1);
75                                 exit(argc == 2 ? EX_OK : EX_USAGE);
76                         case VERSIONSW:
77                                 print_version(invo_name);
78                                 exit(argc == 2 ? EX_OK : EX_USAGE);
79
80                         case FORMSW:
81                                 if (!(form = *argp++) || *form == '-') {
82                                         adios(EX_USAGE, NULL, "missing argument to %s",
83                                                         argp[-2]);
84                                 }
85                                 continue;
86                         }
87                 }
88                 addrs = addrs ? add(cp, add(", ", addrs)) : getcpy(cp);
89         }
90
91         if (!addrs) {
92                 adios(EX_USAGE, NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
93         }
94
95         umask(~m_gmprot());
96
97         tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
98         if (tfile == NULL) adios(EX_CANTCREAT, "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(EX_CANTCREAT, "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                 exit(EX_IOERR);
113         }
114
115         vec[0] = "spost";
116         vec[vecp++] = "-dist";
117         vec[vecp++] = drft;
118         vec[vecp] = NULL;
119
120         execvp(*vec, vec);
121         fprintf(stderr, "unable to exec ");
122         perror(*vec);
123         _exit(EX_OSERR);
124         return 0;  /* dead code to satisfy the compiler */
125 }
126
127 /* very similar to routine in replsbr.c */
128
129 #define SBUFSIZ 256
130
131 static struct format *fmt;
132
133 static int ncomps = 0;
134 static char **compbuffers = 0;
135 static struct comp **used_buf = 0;
136
137 static int dat[5];
138
139 static char *addrcomps[] = {
140         "from",
141         "sender",
142         "reply-to",
143         "to",
144         "cc",
145         "bcc",
146         "resent-from",
147         "resent-sender",
148         "resent-reply-to",
149         "resent-to",
150         "resent-cc",
151         "resent-bcc",
152         NULL
153 };
154
155
156 static void
157 rcvdistout(FILE *inb, char *form, char *addrs)
158 {
159         int char_read = 0, format_len, i, state;
160         char *tmpbuf, **nxtbuf, **ap;
161         char *cp, *scanl, name[NAMESZ];
162         struct comp *cptr, **savecomp;
163         FILE *out;
164
165         if (!(out = fopen(drft, "w"))) {
166                 adios(EX_CANTCREAT, drft, "unable to create");
167         }
168
169         /* get new format string */
170         cp = new_fs(form ? form : rcvdistcomps, NULL);
171         format_len = strlen(cp);
172         ncomps = fmt_compile(cp, &fmt) + 1;
173         nxtbuf = compbuffers = mh_xcalloc(ncomps, sizeof(char *));
174         savecomp = used_buf = mh_xcalloc(ncomps + 1, sizeof(struct comp *));
175         savecomp += ncomps + 1;
176         *--savecomp = 0;
177
178         for (i = ncomps; i--;) {
179                 *nxtbuf++ = mh_xcalloc(SBUFSIZ, sizeof(char));
180         }
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
191         FINDCOMP(cptr, "addresses");
192         if (cptr) {
193                 cptr->c_text = addrs;
194         }
195         state = FLD;
196         while (1) {
197                 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
198                 switch (state) {
199                 case FLD:
200                 case FLDPLUS:
201                         if ((cptr = wantcomp[CHASH(name)])) {
202                                 do {
203                                         if (mh_strcasecmp(name, cptr->c_name)!=0) {
204                                                 continue;
205                                         }
206                                         char_read += msg_count;
207                                         if (!cptr->c_text) {
208                                                 cptr->c_text = tmpbuf;
209                                                 *--savecomp = cptr;
210                                                 tmpbuf = *nxtbuf++;
211                                         } else {
212                                                 cp = cptr->c_text;
213                                                 i = strlen(cp) - 1;
214                                                 if (cp[i] == '\n') {
215                                                         if (cptr->c_type & CT_ADDR) {
216                                                                 cp[i] = 0;
217                                                                 cp = add(",\n\t", cp);
218                                                         } else {
219                                                                 cp = add("\t", cp);
220                                                         }
221                                                 }
222                                                 cptr->c_text = add(tmpbuf, cp);
223                                         }
224                                         while (state == FLDPLUS) {
225                                                 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
226                                                 cptr->c_text = add(tmpbuf, cptr->c_text);
227                                                 char_read += msg_count;
228                                         }
229                                         break;
230                                 } while ((cptr = cptr->c_next));
231                         }
232
233                         while (state == FLDPLUS) {
234                                 state = m_getfld(state, name, tmpbuf,
235                                                 SBUFSIZ, inb);
236                         }
237                         break;
238
239                 case LENERR:
240                 case FMTERR:
241                 case BODY:
242                 case FILEEOF:
243                         goto finished;
244
245                 default:
246                         adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
247                 }
248         }
249 finished: ;
250
251         i = format_len + char_read + 256;
252         scanl = mh_xcalloc(i + 2, sizeof(char));
253         dat[0] = dat[1] = dat[2] = dat[4] = 0;
254         dat[3] = OUTPUTLINELEN;
255         fmt_scan(fmt, scanl, i, dat);
256         fputs(scanl, out);
257
258         if (ferror(out)) {
259                 adios(EX_IOERR, drft, "error writing");
260         }
261         fclose(out);
262
263         free(scanl);
264         for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
265                         nxtbuf++, i--) {
266                 free(cptr->c_text);
267         }
268         while (i-- > 0) {
269                 free(*nxtbuf++);
270         }
271         free(compbuffers);
272         free(used_buf);
273 }
274
275
276 void
277 unlink_done()
278 {
279         if (*backup) {
280                 unlink(backup);
281         }
282         if (*drft) {
283                 unlink(drft);
284         }
285         if (*tmpfil) {
286                 unlink(tmpfil);
287         }
288 }