]>
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/fmt_new.h"
11 #include "sbr/m_gmprot.h"
12 #include "sbr/m_getfld.h"
13 #include "sbr/getarguments.h"
14 #include "sbr/smatch.h"
15 #include "sbr/cpydata.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
18 #include "sbr/pidstatus.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/arglist.h"
22 #include "sbr/error.h"
23 #include "h/fmt_scan.h"
28 #include "sbr/m_mktemp.h"
30 #define RCVDIST_SWITCHES \
31 X("form formfile", 4, FORMSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(RCVDIST
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(RCVDIST
, switches
);
44 # define SASLminc(a) (a)
45 #else /* CYRUS_SASL */
46 # define SASLminc(a) 0
47 #endif /* CYRUS_SASL */
50 # define OAUTHminc(a) (a)
51 #else /* OAUTH_SUPPORT */
52 # define OAUTHminc(a) 0
53 #endif /* OAUTH_SUPPORT */
55 /* These are just the post(1) switches that take an argument. */
56 #define POST_SWITCHES \
57 X("alias aliasfile", 0, ALIASW) \
58 X("filter filterfile", 0, FILTSW) \
59 X("library directory", -7, LIBSW) /* interface from send, whom */ \
60 X("width columns", 0, WIDTHSW) \
61 X("idanno number", -6, ANNOSW) /* interface from send */ \
62 X("client host", -6, CLIESW) \
63 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
64 X("partno", -6, PARTSW) \
65 X("saslmech", SASLminc(5), SASLMECHSW) \
66 X("user", SASLminc(-4), USERSW) \
67 X("port server submission port name/number", 4, PORTSW) \
68 X("fileproc", -4, FILEPROCSW) \
69 X("mhlproc", -3, MHLPROCSW) \
70 X("sendmail program", 0, MTSSM) \
71 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
72 X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
73 X("messageid localname|random", 2, MESSAGEIDSW) \
74 X("authservice auth-service-name", OAUTHminc(-11), AUTHSERVICESW) \
75 X("oauthcredfile credential-file", OAUTHminc(-7), OAUTHCREDFILESW) \
76 X("oauthclientid client-id", OAUTHminc(-12), OAUTHCLIDSW) \
77 X("oauthclientsecret client-secret", OAUTHminc(-12), OAUTHCLSECSW) \
78 X("oauthauthendpoint authentication-endpoint", OAUTHminc(-6), OAUTHAUTHENDSW) \
79 X("oauthredirect redirect-uri", OAUTHminc(-6), OAUTHREDIRSW) \
80 X("oauthtokenendpoint token-endpoint", OAUTHminc(-6), OAUTHTOKENDSW) \
81 X("oauthscope scope", OAUTHminc(-6), OAUTHSCOPESW) \
83 #define X(sw, minchars, id) id,
84 DEFINE_SWITCH_ENUM(POST
);
87 #define X(sw, minchars, id) { sw, minchars, id },
88 DEFINE_SWITCH_ARRAY(POST
, post_switches_with_args
);
91 static char backup
[BUFSIZ
] = "";
92 static char drft
[BUFSIZ
] = "";
93 static char tmpfil
[BUFSIZ
] = "";
98 static void rcvdistout (FILE *, char *, char *);
99 static void unlink_done (int) NORETURN
;
103 main (int argc
, char **argv
)
107 char *addrs
= NULL
, *cp
, *form
= NULL
, buf
[BUFSIZ
], *program
;
108 char **argp
, **arguments
, **vec
;
112 if (nmh_init(argv
[0], true, false)) { return 1; }
114 set_done(unlink_done
);
117 * Configure this now, since any unknown switches to rcvdist get
121 vec
= argsplit(postproc
, &program
, &vecp
);
124 arguments
= getarguments (invo_name
, argc
, argv
, 1);
127 while ((cp
= *argp
++)) {
129 switch (smatch (++cp
, switches
)) {
131 ambigsw (cp
, switches
);
134 const int argno
= smatch(cp
, post_switches_with_args
);
136 if (argno
!= AMBIGSW
&& argno
!= UNKWNSW
) {
137 /* It's a post switch that does take an argument. */
138 if (!(cp
= *argp
) || *cp
== '-') {
139 die("missing argument to %s", argp
[-1]);
147 snprintf (buf
, sizeof(buf
),
148 "%s [switches] [switches for postproc] address ...",
150 print_help (buf
, switches
, 1);
153 print_version(invo_name
);
157 if (!(form
= *argp
++) || *form
== '-')
158 die("missing argument to %s", argp
[-2]);
162 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
166 die("usage: %s [switches] [switches for postproc] address ...",
169 umask (~m_gmprot ());
171 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
172 die("unable to create temporary file in %s", get_temp_dir());
174 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
176 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
177 fseek (fp
, 0L, SEEK_SET
);
179 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
180 die("unable to create temporary file in %s", get_temp_dir());
182 strncpy (drft
, tfile
, sizeof(tmpfil
));
184 rcvdistout (fp
, form
, addrs
);
187 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
190 vec
[vecp
++] = "-dist";
191 if ((cp
= context_find ("mhlproc"))) {
192 vec
[vecp
++] = "-mhlproc";
201 adios("fork", "failed:");
204 execvp (program
, vec
);
205 fprintf (stderr
, "unable to exec ");
210 done (pidXwait(child_id
, postproc
));
213 return 0; /* dead code to satisfy the compiler */
216 /* very similar to routine in replsbr.c */
218 static int outputlinelen
= OUTPUTLINELEN
;
220 static struct format
*fmt
;
224 static char *addrcomps
[] = {
242 rcvdistout (FILE *inb
, char *form
, char *addrs
)
244 int char_read
= 0, format_len
, i
, state
;
246 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
250 m_getfld_state_t gstate
;
252 if (!(out
= fopen (drft
, "w")))
253 adios (drft
, "unable to create");
255 /* get new format string */
256 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
257 format_len
= strlen (cp
);
258 fmt_compile (cp
, &fmt
, 1);
260 for (ap
= addrcomps
; *ap
; ap
++) {
261 cptr
= fmt_findcomp (*ap
);
263 cptr
->c_type
|= CT_ADDR
;
266 cptr
= fmt_findcomp ("addresses");
268 cptr
->c_text
= addrs
;
270 gstate
= m_getfld_state_init(inb
);
272 int msg_count
= sizeof tmpbuf
;
273 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
276 i
= fmt_addcomptext(name
, tmpbuf
);
278 char_read
+= msg_count
;
279 while (state
== FLDPLUS
) {
280 msg_count
= sizeof tmpbuf
;
281 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
282 fmt_appendcomp(i
, name
, tmpbuf
);
283 char_read
+= msg_count
;
287 while (state
== FLDPLUS
) {
288 msg_count
= sizeof tmpbuf
;
289 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
300 die("m_getfld2() returned %d", state
);
304 m_getfld_state_destroy (&gstate
);
306 i
= format_len
+ char_read
+ 256;
307 scanl
= charstring_create (i
+ 2);
308 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
309 dat
[3] = outputlinelen
;
310 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
311 fputs (charstring_buffer (scanl
), out
);
314 adios (drft
, "error writing");
317 charstring_free (scanl
);
323 unlink_done (int status
)
326 (void) m_unlink (backup
);
328 (void) m_unlink (drft
);
330 (void) m_unlink (tmpfil
);
332 exit(status
? 1 : 0);