2 ** rcvdist.c -- asynchronously redistribute messages
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.
10 #include <h/fmt_scan.h>
11 #include <h/rcvmail.h>
19 static struct swit switches[] = {
21 { "form formfile", 0 },
29 char *version=VERSION;
31 static char backup[BUFSIZ] = "";
32 static char drft[BUFSIZ] = "";
33 static char tmpfil[BUFSIZ] = "";
38 static void rcvdistout(FILE *, char *, char *);
43 main(int argc, char **argv)
46 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
47 char **argp, **arguments, *vec[MAXARGS];
51 if (atexit(unlink_done) != 0) {
52 adios(EX_OSERR, NULL, "atexit failed");
55 setlocale(LC_ALL, "");
56 invo_name = mhbasename(argv[0]);
58 /* read user profile/context */
61 arguments = getarguments(invo_name, argc, argv, 1);
64 while ((cp = *argp++)) {
66 switch (smatch(++cp, switches)) {
68 ambigsw(cp, switches);
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);
79 print_version(invo_name);
80 exit(argc == 2 ? EX_OK : EX_USAGE);
83 if (!(form = *argp++) || *form == '-') {
84 adios(EX_USAGE, NULL, "missing argument to %s",
90 addrs = addrs ? add(cp, add(", ", addrs)) : mh_xstrdup(cp);
94 adios(EX_USAGE, NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
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));
103 cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
104 fseek(fp, 0L, SEEK_SET);
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));
110 rcvdistout(fp, form, addrs);
113 if (distout(drft, tmpfil, backup) == NOTOK) {
118 vec[vecp++] = "-dist";
123 fprintf(stderr, "unable to exec ");
126 return 0; /* dead code to satisfy the compiler */
129 /* very similar to routine in replsbr.c */
131 static struct format *fmt;
133 static int ncomps = 0;
137 static char *addrcomps[] = {
155 rcvdistout(FILE *inb, char *form, char *addrs)
157 int char_read = 0, format_len, i;
159 struct field f = {{0}};
165 if (!(out = fopen(drft, "w"))) {
166 adios(EX_CANTCREAT, drft, "unable to create");
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;
174 for (ap = addrcomps; *ap; ap++) {
177 cptr->c_type |= CT_ADDR;
181 FINDCOMP(cptr, "addresses");
183 cptr->c_text = addrs;
187 state = m_getfld2(state, &f, inb);
190 if ((cptr = wantcomp[CHASH(f.name)])) {
192 if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
195 char_read += strlen(f.value);
197 cptr->c_text = mh_xstrdup(f.value);
202 if (cptr->c_type & CT_ADDR) {
204 cp = add(",\n\t", cp);
209 cptr->c_text = add(f.value, cp);
212 } while ((cptr = cptr->c_next));
224 adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
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);
237 adios(EX_IOERR, drft, "error writing");