Convert rcvdist and repl from m_getfld() to m_getfld2()
[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 static struct format *fmt;
130
131 static int ncomps = 0;
132
133 static int dat[5];
134
135 static char *addrcomps[] = {
136         "from",
137         "sender",
138         "reply-to",
139         "to",
140         "cc",
141         "bcc",
142         "resent-from",
143         "resent-sender",
144         "resent-reply-to",
145         "resent-to",
146         "resent-cc",
147         "resent-bcc",
148         NULL
149 };
150
151
152 static void
153 rcvdistout(FILE *inb, char *form, char *addrs)
154 {
155         int char_read = 0, format_len, i;
156         enum state state;
157         struct field f = {{0}};
158         char **ap;
159         char *cp, *scanl;
160         struct comp *cptr;
161         FILE *out;
162
163         if (!(out = fopen(drft, "w"))) {
164                 adios(EX_CANTCREAT, drft, "unable to create");
165         }
166
167         /* get new format string */
168         cp = new_fs(form ? form : rcvdistcomps, NULL);
169         format_len = strlen(cp);
170         ncomps = fmt_compile(cp, &fmt) + 1;
171
172         for (ap = addrcomps; *ap; ap++) {
173                 FINDCOMP(cptr, *ap);
174                 if (cptr) {
175                         cptr->c_type |= CT_ADDR;
176                 }
177         }
178
179         FINDCOMP(cptr, "addresses");
180         if (cptr) {
181                 cptr->c_text = addrs;
182         }
183         state = FLD2;
184         while (1) {
185                 state = m_getfld2(state, &f, inb);
186                 switch (state) {
187                 case FLD2:
188                         if ((cptr = wantcomp[CHASH(f.name)])) {
189                                 do {
190                                         if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
191                                                 continue;
192                                         }
193                                         char_read += strlen(f.value);
194                                         if (!cptr->c_text) {
195                                                 cptr->c_text = getcpy(f.value);
196                                         } else {
197                                                 cp = cptr->c_text;
198                                                 i = strlen(cp) - 1;
199                                                 if (cp[i] == '\n') {
200                                                         if (cptr->c_type & CT_ADDR) {
201                                                                 cp[i] = 0;
202                                                                 cp = add(",\n\t", cp);
203                                                         } else {
204                                                                 cp = add("\t", cp);
205                                                         }
206                                                 }
207                                                 cptr->c_text = add(f.value, cp);
208                                         }
209                                         break;
210                                 } while ((cptr = cptr->c_next));
211                         }
212                         break;
213
214                 case LENERR2:
215                 case FMTERR2:
216                 case IOERR2:
217                 case BODY2:
218                 case FILEEOF2:
219                         goto finished;
220
221                 default:
222                         adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
223                 }
224         }
225 finished: ;
226
227         i = format_len + char_read + 256;
228         scanl = mh_xmalloc((size_t) i + 2);
229         dat[0] = dat[1] = dat[2] = dat[4] = 0;
230         dat[3] = OUTPUTLINELEN;
231         fmt_scan(fmt, scanl, i, dat);
232         fputs(scanl, out);
233
234         if (ferror(out)) {
235                 adios(EX_IOERR, drft, "error writing");
236         }
237         fclose(out);
238
239         free(scanl);
240 }
241
242
243 void
244 unlink_done()
245 {
246         if (*backup) {
247                 unlink(backup);
248         }
249         if (*drft) {
250                 unlink(drft);
251         }
252         if (*tmpfil) {
253                 unlink(tmpfil);
254         }
255 }