]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
a6742a1a3897fc2325cc4af4aadcd15f6ae5c5d3
1 /* rcvdist.c -- asynchronously redistribute messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/fmt_scan.h>
11 #include <h/rcvmail.h>
16 #include "sbr/m_mktemp.h"
18 #define RCVDIST_SWITCHES \
19 X("form formfile", 4, FORMSW) \
20 X("version", 0, VERSIONSW) \
21 X("help", 0, HELPSW) \
23 #define X(sw, minchars, id) id,
24 DEFINE_SWITCH_ENUM(RCVDIST
);
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
31 static char backup
[BUFSIZ
] = "";
32 static char drft
[BUFSIZ
] = "";
33 static char tmpfil
[BUFSIZ
] = "";
38 static void rcvdistout (FILE *, char *, char *);
39 static void unlink_done (int) NORETURN
;
43 main (int argc
, char **argv
)
47 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
48 char **argp
, **arguments
, **vec
;
52 if (nmh_init(argv
[0], true, false)) { return 1; }
54 set_done(unlink_done
);
57 * Configure this now, since any unknown switches to rcvdist get
61 vec
= argsplit(postproc
, &program
, &vecp
);
64 arguments
= getarguments (invo_name
, argc
, argv
, 1);
67 while ((cp
= *argp
++)) {
69 switch (smatch (++cp
, switches
)) {
71 ambigsw (cp
, switches
);
78 snprintf (buf
, sizeof(buf
),
79 "%s [switches] [switches for postproc] address ...",
81 print_help (buf
, switches
, 1);
84 print_version(invo_name
);
88 if (!(form
= *argp
++) || *form
== '-')
89 die("missing argument to %s", argp
[-2]);
93 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
97 die("usage: %s [switches] [switches for postproc] address ...",
100 umask (~m_gmprot ());
102 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
103 die("unable to create temporary file in %s", get_temp_dir());
105 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
107 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
108 fseek (fp
, 0L, SEEK_SET
);
110 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
111 die("unable to create temporary file in %s", get_temp_dir());
113 strncpy (drft
, tfile
, sizeof(tmpfil
));
115 rcvdistout (fp
, form
, addrs
);
118 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
121 vec
[vecp
++] = "-dist";
122 if ((cp
= context_find ("mhlproc"))) {
123 vec
[vecp
++] = "-mhlproc";
132 adios("fork", "failed:");
135 execvp (program
, vec
);
136 fprintf (stderr
, "unable to exec ");
141 done (pidXwait(child_id
, postproc
));
144 return 0; /* dead code to satisfy the compiler */
147 /* very similar to routine in replsbr.c */
149 static int outputlinelen
= OUTPUTLINELEN
;
151 static struct format
*fmt
;
155 static char *addrcomps
[] = {
173 rcvdistout (FILE *inb
, char *form
, char *addrs
)
175 int char_read
= 0, format_len
, i
, state
;
177 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
181 m_getfld_state_t gstate
;
183 if (!(out
= fopen (drft
, "w")))
184 adios (drft
, "unable to create");
186 /* get new format string */
187 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
188 format_len
= strlen (cp
);
189 fmt_compile (cp
, &fmt
, 1);
191 for (ap
= addrcomps
; *ap
; ap
++) {
192 cptr
= fmt_findcomp (*ap
);
194 cptr
->c_type
|= CT_ADDR
;
197 cptr
= fmt_findcomp ("addresses");
199 cptr
->c_text
= addrs
;
201 gstate
= m_getfld_state_init(inb
);
203 int msg_count
= sizeof tmpbuf
;
204 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
207 i
= fmt_addcomptext(name
, tmpbuf
);
209 char_read
+= msg_count
;
210 while (state
== FLDPLUS
) {
211 msg_count
= sizeof tmpbuf
;
212 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
213 fmt_appendcomp(i
, name
, tmpbuf
);
214 char_read
+= msg_count
;
218 while (state
== FLDPLUS
) {
219 msg_count
= sizeof tmpbuf
;
220 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
231 die("m_getfld2() returned %d", state
);
235 m_getfld_state_destroy (&gstate
);
237 i
= format_len
+ char_read
+ 256;
238 scanl
= charstring_create (i
+ 2);
239 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
240 dat
[3] = outputlinelen
;
241 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
242 fputs (charstring_buffer (scanl
), out
);
245 adios (drft
, "error writing");
248 charstring_free (scanl
);
254 unlink_done (int status
)
257 (void) m_unlink (backup
);
259 (void) m_unlink (drft
);
261 (void) m_unlink (tmpfil
);
263 exit (status
? RCV_MBX
: RCV_MOK
);