remove unused defines in uip/pick.c
[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 char *version=VERSION;
30
31 static char backup[BUFSIZ] = "";
32 static char drft[BUFSIZ] = "";
33 static char tmpfil[BUFSIZ] = "";
34
35 /*
36 ** prototypes
37 */
38 static void rcvdistout(FILE *, char *, char *);
39 void unlink_done();
40
41
42 int
43 main(int argc, char **argv)
44 {
45         int vecp = 1;
46         char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
47         char **argp, **arguments, *vec[MAXARGS];
48         FILE *fp;
49         char *tfile = NULL;
50
51         if (atexit(unlink_done) != 0) {
52                 adios(EX_OSERR, NULL, "atexit failed");
53         }
54
55         setlocale(LC_ALL, "");
56         invo_name = mhbasename(argv[0]);
57
58         /* read user profile/context */
59         context_read();
60
61         arguments = getarguments(invo_name, argc, argv, 1);
62         argp = arguments;
63
64         while ((cp = *argp++)) {
65                 if (*cp == '-') {
66                         switch (smatch(++cp, switches)) {
67                         case AMBIGSW:
68                                 ambigsw(cp, switches);
69                                 exit(EX_USAGE);
70                         case UNKWNSW:
71                                 vec[vecp++] = --cp;
72                                 continue;
73
74                         case HELPSW:
75                                 snprintf(buf, sizeof(buf), "%s [switches] [switches for spost] address ...", invo_name);
76                                 print_help(buf, switches, 1);
77                                 exit(argc == 2 ? EX_OK : EX_USAGE);
78                         case VERSIONSW:
79                                 print_version(invo_name);
80                                 exit(argc == 2 ? EX_OK : EX_USAGE);
81
82                         case FORMSW:
83                                 if (!(form = *argp++) || *form == '-') {
84                                         adios(EX_USAGE, NULL, "missing argument to %s",
85                                                         argp[-2]);
86                                 }
87                                 continue;
88                         }
89                 }
90                 addrs = addrs ? add(cp, add(", ", addrs)) : mh_xstrdup(cp);
91         }
92
93         if (!addrs) {
94                 adios(EX_USAGE, NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
95         }
96
97         umask(~m_gmprot());
98
99         tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
100         if (tfile == NULL) adios(EX_CANTCREAT, "rcvdist", "unable to create temporary file");
101         strncpy(tmpfil, tfile, sizeof(tmpfil));
102
103         cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
104         fseek(fp, 0L, SEEK_SET);
105
106         tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
107         if (tfile == NULL) adios(EX_CANTCREAT, "forw", "unable to create temporary file");
108         strncpy(drft, tfile, sizeof(tmpfil));
109
110         rcvdistout(fp, form, addrs);
111         fclose(fp);
112
113         if (distout(drft, tmpfil, backup) == NOTOK) {
114                 exit(EX_IOERR);
115         }
116
117         vec[0] = "spost";
118         vec[vecp++] = "-dist";
119         vec[vecp++] = drft;
120         vec[vecp] = NULL;
121
122         execvp(*vec, vec);
123         fprintf(stderr, "unable to exec ");
124         perror(*vec);
125         _exit(EX_OSERR);
126         return 0;  /* dead code to satisfy the compiler */
127 }
128
129 /* very similar to routine in replsbr.c */
130
131 static struct format *fmt;
132
133 static int ncomps = 0;
134
135 static int dat[5];
136
137 static char *addrcomps[] = {
138         "from",
139         "sender",
140         "reply-to",
141         "to",
142         "cc",
143         "bcc",
144         "resent-from",
145         "resent-sender",
146         "resent-reply-to",
147         "resent-to",
148         "resent-cc",
149         "resent-bcc",
150         NULL
151 };
152
153
154 static void
155 rcvdistout(FILE *inb, char *form, char *addrs)
156 {
157         int char_read = 0, format_len, i;
158         enum state state;
159         struct field f = {{0}};
160         char **ap;
161         char *cp, *scanl;
162         struct comp *cptr;
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
174         for (ap = addrcomps; *ap; ap++) {
175                 FINDCOMP(cptr, *ap);
176                 if (cptr) {
177                         cptr->c_type |= CT_ADDR;
178                 }
179         }
180
181         FINDCOMP(cptr, "addresses");
182         if (cptr) {
183                 cptr->c_text = addrs;
184         }
185         state = FLD2;
186         while (1) {
187                 state = m_getfld2(state, &f, inb);
188                 switch (state) {
189                 case FLD2:
190                         if ((cptr = wantcomp[CHASH(f.name)])) {
191                                 do {
192                                         if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
193                                                 continue;
194                                         }
195                                         char_read += strlen(f.value);
196                                         if (!cptr->c_text) {
197                                                 cptr->c_text = mh_xstrdup(f.value);
198                                         } else {
199                                                 cp = cptr->c_text;
200                                                 i = strlen(cp) - 1;
201                                                 if (cp[i] == '\n') {
202                                                         if (cptr->c_type & CT_ADDR) {
203                                                                 cp[i] = 0;
204                                                                 cp = add(",\n\t", cp);
205                                                         } else {
206                                                                 cp = add("\t", cp);
207                                                         }
208                                                 }
209                                                 cptr->c_text = add(f.value, cp);
210                                         }
211                                         break;
212                                 } while ((cptr = cptr->c_next));
213                         }
214                         break;
215
216                 case LENERR2:
217                 case FMTERR2:
218                 case IOERR2:
219                 case BODY2:
220                 case FILEEOF2:
221                         goto finished;
222
223                 default:
224                         adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
225                 }
226         }
227 finished: ;
228
229         i = format_len + char_read + 256;
230         scanl = mh_xcalloc(i + 2, sizeof(char));
231         dat[0] = dat[1] = dat[2] = dat[4] = 0;
232         dat[3] = OUTPUTLINELEN;
233         fmt_scan(fmt, scanl, i, dat);
234         fputs(scanl, out);
235
236         if (ferror(out)) {
237                 adios(EX_IOERR, drft, "error writing");
238         }
239         fclose(out);
240
241         mh_free0(&scanl);
242 }
243
244
245 void
246 unlink_done()
247 {
248         if (*backup) {
249                 unlink(backup);
250         }
251         if (*drft) {
252                 unlink(drft);
253         }
254         if (*tmpfil) {
255                 unlink(tmpfil);
256         }
257 }