]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
d45a56d7cc2e55001e64a91928bf1834960274a3
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/ambigsw.h"
10 #include "sbr/pidstatus.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/arglist.h"
14 #include "sbr/error.h"
15 #include "h/fmt_scan.h"
16 #include "h/rcvmail.h"
21 #include "sbr/m_mktemp.h"
23 #define RCVDIST_SWITCHES \
24 X("form formfile", 4, FORMSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(RCVDIST
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
36 static char backup
[BUFSIZ
] = "";
37 static char drft
[BUFSIZ
] = "";
38 static char tmpfil
[BUFSIZ
] = "";
43 static void rcvdistout (FILE *, char *, char *);
44 static void unlink_done (int) NORETURN
;
48 main (int argc
, char **argv
)
52 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
53 char **argp
, **arguments
, **vec
;
57 if (nmh_init(argv
[0], true, false)) { return 1; }
59 set_done(unlink_done
);
62 * Configure this now, since any unknown switches to rcvdist get
66 vec
= argsplit(postproc
, &program
, &vecp
);
69 arguments
= getarguments (invo_name
, argc
, argv
, 1);
72 while ((cp
= *argp
++)) {
74 switch (smatch (++cp
, switches
)) {
76 ambigsw (cp
, switches
);
83 snprintf (buf
, sizeof(buf
),
84 "%s [switches] [switches for postproc] address ...",
86 print_help (buf
, switches
, 1);
89 print_version(invo_name
);
93 if (!(form
= *argp
++) || *form
== '-')
94 die("missing argument to %s", argp
[-2]);
98 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
102 die("usage: %s [switches] [switches for postproc] address ...",
105 umask (~m_gmprot ());
107 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
108 die("unable to create temporary file in %s", get_temp_dir());
110 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
112 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
113 fseek (fp
, 0L, SEEK_SET
);
115 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
116 die("unable to create temporary file in %s", get_temp_dir());
118 strncpy (drft
, tfile
, sizeof(tmpfil
));
120 rcvdistout (fp
, form
, addrs
);
123 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
126 vec
[vecp
++] = "-dist";
127 if ((cp
= context_find ("mhlproc"))) {
128 vec
[vecp
++] = "-mhlproc";
137 adios("fork", "failed:");
140 execvp (program
, vec
);
141 fprintf (stderr
, "unable to exec ");
146 done (pidXwait(child_id
, postproc
));
149 return 0; /* dead code to satisfy the compiler */
152 /* very similar to routine in replsbr.c */
154 static int outputlinelen
= OUTPUTLINELEN
;
156 static struct format
*fmt
;
160 static char *addrcomps
[] = {
178 rcvdistout (FILE *inb
, char *form
, char *addrs
)
180 int char_read
= 0, format_len
, i
, state
;
182 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
186 m_getfld_state_t gstate
;
188 if (!(out
= fopen (drft
, "w")))
189 adios (drft
, "unable to create");
191 /* get new format string */
192 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
193 format_len
= strlen (cp
);
194 fmt_compile (cp
, &fmt
, 1);
196 for (ap
= addrcomps
; *ap
; ap
++) {
197 cptr
= fmt_findcomp (*ap
);
199 cptr
->c_type
|= CT_ADDR
;
202 cptr
= fmt_findcomp ("addresses");
204 cptr
->c_text
= addrs
;
206 gstate
= m_getfld_state_init(inb
);
208 int msg_count
= sizeof tmpbuf
;
209 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
212 i
= fmt_addcomptext(name
, tmpbuf
);
214 char_read
+= msg_count
;
215 while (state
== FLDPLUS
) {
216 msg_count
= sizeof tmpbuf
;
217 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
218 fmt_appendcomp(i
, name
, tmpbuf
);
219 char_read
+= msg_count
;
223 while (state
== FLDPLUS
) {
224 msg_count
= sizeof tmpbuf
;
225 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
236 die("m_getfld2() returned %d", state
);
240 m_getfld_state_destroy (&gstate
);
242 i
= format_len
+ char_read
+ 256;
243 scanl
= charstring_create (i
+ 2);
244 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
245 dat
[3] = outputlinelen
;
246 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
247 fputs (charstring_buffer (scanl
), out
);
250 adios (drft
, "error writing");
253 charstring_free (scanl
);
259 unlink_done (int status
)
262 (void) m_unlink (backup
);
264 (void) m_unlink (drft
);
266 (void) m_unlink (tmpfil
);
268 exit (status
? RCV_MBX
: RCV_MOK
);