]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
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/smatch.h"
10 #include "sbr/cpydata.h"
11 #include "sbr/context_find.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/pidstatus.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/arglist.h"
17 #include "sbr/error.h"
18 #include "h/fmt_scan.h"
19 #include "h/rcvmail.h"
24 #include "sbr/m_mktemp.h"
26 #define RCVDIST_SWITCHES \
27 X("form formfile", 4, FORMSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(RCVDIST
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
39 static char backup
[BUFSIZ
] = "";
40 static char drft
[BUFSIZ
] = "";
41 static char tmpfil
[BUFSIZ
] = "";
46 static void rcvdistout (FILE *, char *, char *);
47 static void unlink_done (int) NORETURN
;
51 main (int argc
, char **argv
)
55 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
56 char **argp
, **arguments
, **vec
;
60 if (nmh_init(argv
[0], true, false)) { return 1; }
62 set_done(unlink_done
);
65 * Configure this now, since any unknown switches to rcvdist get
69 vec
= argsplit(postproc
, &program
, &vecp
);
72 arguments
= getarguments (invo_name
, argc
, argv
, 1);
75 while ((cp
= *argp
++)) {
77 switch (smatch (++cp
, switches
)) {
79 ambigsw (cp
, switches
);
86 snprintf (buf
, sizeof(buf
),
87 "%s [switches] [switches for postproc] address ...",
89 print_help (buf
, switches
, 1);
92 print_version(invo_name
);
96 if (!(form
= *argp
++) || *form
== '-')
97 die("missing argument to %s", argp
[-2]);
101 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
105 die("usage: %s [switches] [switches for postproc] address ...",
108 umask (~m_gmprot ());
110 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
111 die("unable to create temporary file in %s", get_temp_dir());
113 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
115 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
116 fseek (fp
, 0L, SEEK_SET
);
118 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
119 die("unable to create temporary file in %s", get_temp_dir());
121 strncpy (drft
, tfile
, sizeof(tmpfil
));
123 rcvdistout (fp
, form
, addrs
);
126 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
129 vec
[vecp
++] = "-dist";
130 if ((cp
= context_find ("mhlproc"))) {
131 vec
[vecp
++] = "-mhlproc";
140 adios("fork", "failed:");
143 execvp (program
, vec
);
144 fprintf (stderr
, "unable to exec ");
149 done (pidXwait(child_id
, postproc
));
152 return 0; /* dead code to satisfy the compiler */
155 /* very similar to routine in replsbr.c */
157 static int outputlinelen
= OUTPUTLINELEN
;
159 static struct format
*fmt
;
163 static char *addrcomps
[] = {
181 rcvdistout (FILE *inb
, char *form
, char *addrs
)
183 int char_read
= 0, format_len
, i
, state
;
185 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
189 m_getfld_state_t gstate
;
191 if (!(out
= fopen (drft
, "w")))
192 adios (drft
, "unable to create");
194 /* get new format string */
195 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
196 format_len
= strlen (cp
);
197 fmt_compile (cp
, &fmt
, 1);
199 for (ap
= addrcomps
; *ap
; ap
++) {
200 cptr
= fmt_findcomp (*ap
);
202 cptr
->c_type
|= CT_ADDR
;
205 cptr
= fmt_findcomp ("addresses");
207 cptr
->c_text
= addrs
;
209 gstate
= m_getfld_state_init(inb
);
211 int msg_count
= sizeof tmpbuf
;
212 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
215 i
= fmt_addcomptext(name
, tmpbuf
);
217 char_read
+= msg_count
;
218 while (state
== FLDPLUS
) {
219 msg_count
= sizeof tmpbuf
;
220 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
221 fmt_appendcomp(i
, name
, tmpbuf
);
222 char_read
+= msg_count
;
226 while (state
== FLDPLUS
) {
227 msg_count
= sizeof tmpbuf
;
228 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
239 die("m_getfld2() returned %d", state
);
243 m_getfld_state_destroy (&gstate
);
245 i
= format_len
+ char_read
+ 256;
246 scanl
= charstring_create (i
+ 2);
247 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
248 dat
[3] = outputlinelen
;
249 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
250 fputs (charstring_buffer (scanl
), out
);
253 adios (drft
, "error writing");
256 charstring_free (scanl
);
262 unlink_done (int status
)
265 (void) m_unlink (backup
);
267 (void) m_unlink (drft
);
269 (void) m_unlink (tmpfil
);
271 exit (status
? RCV_MBX
: RCV_MOK
);