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 static char backup[BUFSIZ] = "";
30 static char drft[BUFSIZ] = "";
31 static char tmpfil[BUFSIZ] = "";
36 static void rcvdistout(FILE *, char *, char *);
41 main(int argc, char **argv)
44 char *addrs = NULL, *cp, *form = NULL, buf[BUFSIZ];
45 char **argp, **arguments, *vec[MAXARGS];
49 if (atexit(unlink_done) != 0) {
50 adios(EX_OSERR, NULL, "atexit failed");
53 setlocale(LC_ALL, "");
54 invo_name = mhbasename(argv[0]);
56 /* read user profile/context */
59 arguments = getarguments(invo_name, argc, argv, 1);
62 while ((cp = *argp++)) {
64 switch (smatch(++cp, switches)) {
66 ambigsw(cp, switches);
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);
77 print_version(invo_name);
78 exit(argc == 2 ? EX_OK : EX_USAGE);
81 if (!(form = *argp++) || *form == '-') {
82 adios(EX_USAGE, NULL, "missing argument to %s",
88 addrs = addrs ? add(cp, add(", ", addrs)) : mh_xstrdup(cp);
92 adios(EX_USAGE, NULL, "usage: %s [switches] [switches for spost] address ...", invo_name);
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));
101 cpydata(fileno(stdin), fileno(fp), "message", tmpfil);
102 fseek(fp, 0L, SEEK_SET);
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));
108 rcvdistout(fp, form, addrs);
111 if (distout(drft, tmpfil, backup) == NOTOK) {
116 vec[vecp++] = "-dist";
121 fprintf(stderr, "unable to exec ");
124 return 0; /* dead code to satisfy the compiler */
127 /* very similar to routine in replsbr.c */
129 static struct format *fmt;
131 static int ncomps = 0;
135 static char *addrcomps[] = {
153 rcvdistout(FILE *inb, char *form, char *addrs)
155 int char_read = 0, format_len, i;
157 struct field f = {{0}};
163 if (!(out = fopen(drft, "w"))) {
164 adios(EX_CANTCREAT, drft, "unable to create");
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;
172 for (ap = addrcomps; *ap; ap++) {
175 cptr->c_type |= CT_ADDR;
179 FINDCOMP(cptr, "addresses");
181 cptr->c_text = addrs;
185 state = m_getfld2(state, &f, inb);
188 if ((cptr = wantcomp[CHASH(f.name)])) {
190 if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
193 char_read += strlen(f.value);
195 cptr->c_text = mh_xstrdup(f.value);
200 if (cptr->c_type & CT_ADDR) {
202 cp = add(",\n\t", cp);
207 cptr->c_text = add(f.value, cp);
210 } while ((cptr = cptr->c_next));
222 adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
227 i = format_len + char_read + 256;
228 scanl = mh_xcalloc(i + 2, sizeof(char));
229 dat[0] = dat[1] = dat[2] = dat[4] = 0;
230 dat[3] = OUTPUTLINELEN;
231 fmt_scan(fmt, scanl, i, dat);
235 adios(EX_IOERR, drft, "error writing");