]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
73c86b39d631b57e3be0cc9b09546e512947b573
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/cpydata.h"
10 #include "sbr/context_find.h"
11 #include "sbr/ambigsw.h"
12 #include "sbr/pidstatus.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/arglist.h"
16 #include "sbr/error.h"
17 #include "h/fmt_scan.h"
18 #include "h/rcvmail.h"
23 #include "sbr/m_mktemp.h"
25 #define RCVDIST_SWITCHES \
26 X("form formfile", 4, FORMSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(RCVDIST
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
38 static char backup
[BUFSIZ
] = "";
39 static char drft
[BUFSIZ
] = "";
40 static char tmpfil
[BUFSIZ
] = "";
45 static void rcvdistout (FILE *, char *, char *);
46 static void unlink_done (int) NORETURN
;
50 main (int argc
, char **argv
)
54 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
55 char **argp
, **arguments
, **vec
;
59 if (nmh_init(argv
[0], true, false)) { return 1; }
61 set_done(unlink_done
);
64 * Configure this now, since any unknown switches to rcvdist get
68 vec
= argsplit(postproc
, &program
, &vecp
);
71 arguments
= getarguments (invo_name
, argc
, argv
, 1);
74 while ((cp
= *argp
++)) {
76 switch (smatch (++cp
, switches
)) {
78 ambigsw (cp
, switches
);
85 snprintf (buf
, sizeof(buf
),
86 "%s [switches] [switches for postproc] address ...",
88 print_help (buf
, switches
, 1);
91 print_version(invo_name
);
95 if (!(form
= *argp
++) || *form
== '-')
96 die("missing argument to %s", argp
[-2]);
100 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
104 die("usage: %s [switches] [switches for postproc] address ...",
107 umask (~m_gmprot ());
109 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
110 die("unable to create temporary file in %s", get_temp_dir());
112 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
114 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
115 fseek (fp
, 0L, SEEK_SET
);
117 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
118 die("unable to create temporary file in %s", get_temp_dir());
120 strncpy (drft
, tfile
, sizeof(tmpfil
));
122 rcvdistout (fp
, form
, addrs
);
125 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
128 vec
[vecp
++] = "-dist";
129 if ((cp
= context_find ("mhlproc"))) {
130 vec
[vecp
++] = "-mhlproc";
139 adios("fork", "failed:");
142 execvp (program
, vec
);
143 fprintf (stderr
, "unable to exec ");
148 done (pidXwait(child_id
, postproc
));
151 return 0; /* dead code to satisfy the compiler */
154 /* very similar to routine in replsbr.c */
156 static int outputlinelen
= OUTPUTLINELEN
;
158 static struct format
*fmt
;
162 static char *addrcomps
[] = {
180 rcvdistout (FILE *inb
, char *form
, char *addrs
)
182 int char_read
= 0, format_len
, i
, state
;
184 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
188 m_getfld_state_t gstate
;
190 if (!(out
= fopen (drft
, "w")))
191 adios (drft
, "unable to create");
193 /* get new format string */
194 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
195 format_len
= strlen (cp
);
196 fmt_compile (cp
, &fmt
, 1);
198 for (ap
= addrcomps
; *ap
; ap
++) {
199 cptr
= fmt_findcomp (*ap
);
201 cptr
->c_type
|= CT_ADDR
;
204 cptr
= fmt_findcomp ("addresses");
206 cptr
->c_text
= addrs
;
208 gstate
= m_getfld_state_init(inb
);
210 int msg_count
= sizeof tmpbuf
;
211 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
214 i
= fmt_addcomptext(name
, tmpbuf
);
216 char_read
+= msg_count
;
217 while (state
== FLDPLUS
) {
218 msg_count
= sizeof tmpbuf
;
219 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
220 fmt_appendcomp(i
, name
, tmpbuf
);
221 char_read
+= msg_count
;
225 while (state
== FLDPLUS
) {
226 msg_count
= sizeof tmpbuf
;
227 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
238 die("m_getfld2() returned %d", state
);
242 m_getfld_state_destroy (&gstate
);
244 i
= format_len
+ char_read
+ 256;
245 scanl
= charstring_create (i
+ 2);
246 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
247 dat
[3] = outputlinelen
;
248 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
249 fputs (charstring_buffer (scanl
), out
);
252 adios (drft
, "error writing");
255 charstring_free (scanl
);
261 unlink_done (int status
)
264 (void) m_unlink (backup
);
266 (void) m_unlink (drft
);
268 (void) m_unlink (tmpfil
);
270 exit (status
? RCV_MBX
: RCV_MOK
);