]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
2de2153ebcf07c9992b6a7be0fd20d7e8e737ef1
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/print_help.h"
10 #include "sbr/arglist.h"
11 #include "sbr/error.h"
12 #include "h/fmt_scan.h"
13 #include "h/rcvmail.h"
18 #include "sbr/m_mktemp.h"
20 #define RCVDIST_SWITCHES \
21 X("form formfile", 4, FORMSW) \
22 X("version", 0, VERSIONSW) \
23 X("help", 0, HELPSW) \
25 #define X(sw, minchars, id) id,
26 DEFINE_SWITCH_ENUM(RCVDIST
);
29 #define X(sw, minchars, id) { sw, minchars, id },
30 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
33 static char backup
[BUFSIZ
] = "";
34 static char drft
[BUFSIZ
] = "";
35 static char tmpfil
[BUFSIZ
] = "";
40 static void rcvdistout (FILE *, char *, char *);
41 static void unlink_done (int) NORETURN
;
45 main (int argc
, char **argv
)
49 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
50 char **argp
, **arguments
, **vec
;
54 if (nmh_init(argv
[0], true, false)) { return 1; }
56 set_done(unlink_done
);
59 * Configure this now, since any unknown switches to rcvdist get
63 vec
= argsplit(postproc
, &program
, &vecp
);
66 arguments
= getarguments (invo_name
, argc
, argv
, 1);
69 while ((cp
= *argp
++)) {
71 switch (smatch (++cp
, switches
)) {
73 ambigsw (cp
, switches
);
80 snprintf (buf
, sizeof(buf
),
81 "%s [switches] [switches for postproc] address ...",
83 print_help (buf
, switches
, 1);
86 print_version(invo_name
);
90 if (!(form
= *argp
++) || *form
== '-')
91 die("missing argument to %s", argp
[-2]);
95 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
99 die("usage: %s [switches] [switches for postproc] address ...",
102 umask (~m_gmprot ());
104 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
105 die("unable to create temporary file in %s", get_temp_dir());
107 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
109 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
110 fseek (fp
, 0L, SEEK_SET
);
112 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
113 die("unable to create temporary file in %s", get_temp_dir());
115 strncpy (drft
, tfile
, sizeof(tmpfil
));
117 rcvdistout (fp
, form
, addrs
);
120 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
123 vec
[vecp
++] = "-dist";
124 if ((cp
= context_find ("mhlproc"))) {
125 vec
[vecp
++] = "-mhlproc";
134 adios("fork", "failed:");
137 execvp (program
, vec
);
138 fprintf (stderr
, "unable to exec ");
143 done (pidXwait(child_id
, postproc
));
146 return 0; /* dead code to satisfy the compiler */
149 /* very similar to routine in replsbr.c */
151 static int outputlinelen
= OUTPUTLINELEN
;
153 static struct format
*fmt
;
157 static char *addrcomps
[] = {
175 rcvdistout (FILE *inb
, char *form
, char *addrs
)
177 int char_read
= 0, format_len
, i
, state
;
179 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
183 m_getfld_state_t gstate
;
185 if (!(out
= fopen (drft
, "w")))
186 adios (drft
, "unable to create");
188 /* get new format string */
189 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
190 format_len
= strlen (cp
);
191 fmt_compile (cp
, &fmt
, 1);
193 for (ap
= addrcomps
; *ap
; ap
++) {
194 cptr
= fmt_findcomp (*ap
);
196 cptr
->c_type
|= CT_ADDR
;
199 cptr
= fmt_findcomp ("addresses");
201 cptr
->c_text
= addrs
;
203 gstate
= m_getfld_state_init(inb
);
205 int msg_count
= sizeof tmpbuf
;
206 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
209 i
= fmt_addcomptext(name
, tmpbuf
);
211 char_read
+= msg_count
;
212 while (state
== FLDPLUS
) {
213 msg_count
= sizeof tmpbuf
;
214 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
215 fmt_appendcomp(i
, name
, tmpbuf
);
216 char_read
+= msg_count
;
220 while (state
== FLDPLUS
) {
221 msg_count
= sizeof tmpbuf
;
222 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
233 die("m_getfld2() returned %d", state
);
237 m_getfld_state_destroy (&gstate
);
239 i
= format_len
+ char_read
+ 256;
240 scanl
= charstring_create (i
+ 2);
241 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
242 dat
[3] = outputlinelen
;
243 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
244 fputs (charstring_buffer (scanl
), out
);
247 adios (drft
, "error writing");
250 charstring_free (scanl
);
256 unlink_done (int status
)
259 (void) m_unlink (backup
);
261 (void) m_unlink (drft
);
263 (void) m_unlink (tmpfil
);
265 exit (status
? RCV_MBX
: RCV_MOK
);