]>
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/context_find.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/pidstatus.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/arglist.h"
15 #include "sbr/error.h"
16 #include "h/fmt_scan.h"
17 #include "h/rcvmail.h"
22 #include "sbr/m_mktemp.h"
24 #define RCVDIST_SWITCHES \
25 X("form formfile", 4, FORMSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(RCVDIST
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
37 static char backup
[BUFSIZ
] = "";
38 static char drft
[BUFSIZ
] = "";
39 static char tmpfil
[BUFSIZ
] = "";
44 static void rcvdistout (FILE *, char *, char *);
45 static void unlink_done (int) NORETURN
;
49 main (int argc
, char **argv
)
53 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
54 char **argp
, **arguments
, **vec
;
58 if (nmh_init(argv
[0], true, false)) { return 1; }
60 set_done(unlink_done
);
63 * Configure this now, since any unknown switches to rcvdist get
67 vec
= argsplit(postproc
, &program
, &vecp
);
70 arguments
= getarguments (invo_name
, argc
, argv
, 1);
73 while ((cp
= *argp
++)) {
75 switch (smatch (++cp
, switches
)) {
77 ambigsw (cp
, switches
);
84 snprintf (buf
, sizeof(buf
),
85 "%s [switches] [switches for postproc] address ...",
87 print_help (buf
, switches
, 1);
90 print_version(invo_name
);
94 if (!(form
= *argp
++) || *form
== '-')
95 die("missing argument to %s", argp
[-2]);
99 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
103 die("usage: %s [switches] [switches for postproc] address ...",
106 umask (~m_gmprot ());
108 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
109 die("unable to create temporary file in %s", get_temp_dir());
111 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
113 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
114 fseek (fp
, 0L, SEEK_SET
);
116 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
117 die("unable to create temporary file in %s", get_temp_dir());
119 strncpy (drft
, tfile
, sizeof(tmpfil
));
121 rcvdistout (fp
, form
, addrs
);
124 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
127 vec
[vecp
++] = "-dist";
128 if ((cp
= context_find ("mhlproc"))) {
129 vec
[vecp
++] = "-mhlproc";
138 adios("fork", "failed:");
141 execvp (program
, vec
);
142 fprintf (stderr
, "unable to exec ");
147 done (pidXwait(child_id
, postproc
));
150 return 0; /* dead code to satisfy the compiler */
153 /* very similar to routine in replsbr.c */
155 static int outputlinelen
= OUTPUTLINELEN
;
157 static struct format
*fmt
;
161 static char *addrcomps
[] = {
179 rcvdistout (FILE *inb
, char *form
, char *addrs
)
181 int char_read
= 0, format_len
, i
, state
;
183 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
187 m_getfld_state_t gstate
;
189 if (!(out
= fopen (drft
, "w")))
190 adios (drft
, "unable to create");
192 /* get new format string */
193 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
194 format_len
= strlen (cp
);
195 fmt_compile (cp
, &fmt
, 1);
197 for (ap
= addrcomps
; *ap
; ap
++) {
198 cptr
= fmt_findcomp (*ap
);
200 cptr
->c_type
|= CT_ADDR
;
203 cptr
= fmt_findcomp ("addresses");
205 cptr
->c_text
= addrs
;
207 gstate
= m_getfld_state_init(inb
);
209 int msg_count
= sizeof tmpbuf
;
210 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
213 i
= fmt_addcomptext(name
, tmpbuf
);
215 char_read
+= msg_count
;
216 while (state
== FLDPLUS
) {
217 msg_count
= sizeof tmpbuf
;
218 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
219 fmt_appendcomp(i
, name
, tmpbuf
);
220 char_read
+= msg_count
;
224 while (state
== FLDPLUS
) {
225 msg_count
= sizeof tmpbuf
;
226 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
237 die("m_getfld2() returned %d", state
);
241 m_getfld_state_destroy (&gstate
);
243 i
= format_len
+ char_read
+ 256;
244 scanl
= charstring_create (i
+ 2);
245 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
246 dat
[3] = outputlinelen
;
247 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
248 fputs (charstring_buffer (scanl
), out
);
251 adios (drft
, "error writing");
254 charstring_free (scanl
);
260 unlink_done (int status
)
263 (void) m_unlink (backup
);
265 (void) m_unlink (drft
);
267 (void) m_unlink (tmpfil
);
269 exit (status
? RCV_MBX
: RCV_MOK
);