]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
9f52a4b25e79e38e26bfe9b73f2839bc7f493a8b
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.
9 #include "sbr/arglist.h"
10 #include "sbr/error.h"
11 #include "h/fmt_scan.h"
12 #include "h/rcvmail.h"
17 #include "sbr/m_mktemp.h"
19 #define RCVDIST_SWITCHES \
20 X("form formfile", 4, FORMSW) \
21 X("version", 0, VERSIONSW) \
22 X("help", 0, HELPSW) \
24 #define X(sw, minchars, id) id,
25 DEFINE_SWITCH_ENUM(RCVDIST
);
28 #define X(sw, minchars, id) { sw, minchars, id },
29 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
32 static char backup
[BUFSIZ
] = "";
33 static char drft
[BUFSIZ
] = "";
34 static char tmpfil
[BUFSIZ
] = "";
39 static void rcvdistout (FILE *, char *, char *);
40 static void unlink_done (int) NORETURN
;
44 main (int argc
, char **argv
)
48 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
49 char **argp
, **arguments
, **vec
;
53 if (nmh_init(argv
[0], true, false)) { return 1; }
55 set_done(unlink_done
);
58 * Configure this now, since any unknown switches to rcvdist get
62 vec
= argsplit(postproc
, &program
, &vecp
);
65 arguments
= getarguments (invo_name
, argc
, argv
, 1);
68 while ((cp
= *argp
++)) {
70 switch (smatch (++cp
, switches
)) {
72 ambigsw (cp
, switches
);
79 snprintf (buf
, sizeof(buf
),
80 "%s [switches] [switches for postproc] address ...",
82 print_help (buf
, switches
, 1);
85 print_version(invo_name
);
89 if (!(form
= *argp
++) || *form
== '-')
90 die("missing argument to %s", argp
[-2]);
94 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
98 die("usage: %s [switches] [switches for postproc] address ...",
101 umask (~m_gmprot ());
103 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
104 die("unable to create temporary file in %s", get_temp_dir());
106 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
108 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
109 fseek (fp
, 0L, SEEK_SET
);
111 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
112 die("unable to create temporary file in %s", get_temp_dir());
114 strncpy (drft
, tfile
, sizeof(tmpfil
));
116 rcvdistout (fp
, form
, addrs
);
119 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
122 vec
[vecp
++] = "-dist";
123 if ((cp
= context_find ("mhlproc"))) {
124 vec
[vecp
++] = "-mhlproc";
133 adios("fork", "failed:");
136 execvp (program
, vec
);
137 fprintf (stderr
, "unable to exec ");
142 done (pidXwait(child_id
, postproc
));
145 return 0; /* dead code to satisfy the compiler */
148 /* very similar to routine in replsbr.c */
150 static int outputlinelen
= OUTPUTLINELEN
;
152 static struct format
*fmt
;
156 static char *addrcomps
[] = {
174 rcvdistout (FILE *inb
, char *form
, char *addrs
)
176 int char_read
= 0, format_len
, i
, state
;
178 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
182 m_getfld_state_t gstate
;
184 if (!(out
= fopen (drft
, "w")))
185 adios (drft
, "unable to create");
187 /* get new format string */
188 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
189 format_len
= strlen (cp
);
190 fmt_compile (cp
, &fmt
, 1);
192 for (ap
= addrcomps
; *ap
; ap
++) {
193 cptr
= fmt_findcomp (*ap
);
195 cptr
->c_type
|= CT_ADDR
;
198 cptr
= fmt_findcomp ("addresses");
200 cptr
->c_text
= addrs
;
202 gstate
= m_getfld_state_init(inb
);
204 int msg_count
= sizeof tmpbuf
;
205 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
208 i
= fmt_addcomptext(name
, tmpbuf
);
210 char_read
+= msg_count
;
211 while (state
== FLDPLUS
) {
212 msg_count
= sizeof tmpbuf
;
213 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
214 fmt_appendcomp(i
, name
, tmpbuf
);
215 char_read
+= msg_count
;
219 while (state
== FLDPLUS
) {
220 msg_count
= sizeof tmpbuf
;
221 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
232 die("m_getfld2() returned %d", state
);
236 m_getfld_state_destroy (&gstate
);
238 i
= format_len
+ char_read
+ 256;
239 scanl
= charstring_create (i
+ 2);
240 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
241 dat
[3] = outputlinelen
;
242 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
243 fputs (charstring_buffer (scanl
), out
);
246 adios (drft
, "error writing");
249 charstring_free (scanl
);
255 unlink_done (int status
)
258 (void) m_unlink (backup
);
260 (void) m_unlink (drft
);
262 (void) m_unlink (tmpfil
);
264 exit (status
? RCV_MBX
: RCV_MOK
);