]>
diplodocus.org Git - nmh/blob - uip/rcvdist.c
531518d8dd54efe8466a410534b28806f93938f5
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
);
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
);
135 if (smatch(cp
+ 1, post_switches
) != UNKWNSW
) {
136 /* It's a post switch that does take an argument. */
137 if (!(cp
= *argp
) || *cp
== '-') {
138 die("missing argument to %s", argp
[-1]);
140 vec
[vecp
++] = *argp
++;
145 snprintf (buf
, sizeof(buf
),
146 "%s [switches] [switches for postproc] address ...",
148 print_help (buf
, switches
, 1);
151 print_version(invo_name
);
155 if (!(form
= *argp
++) || *form
== '-')
156 die("missing argument to %s", argp
[-2]);
160 addrs
= addrs
? add (cp
, add (", ", addrs
)) : mh_xstrdup(cp
);
164 die("usage: %s [switches] [switches for postproc] address ...",
167 umask (~m_gmprot ());
169 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
170 die("unable to create temporary file in %s", get_temp_dir());
172 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
174 cpydata (fileno (stdin
), fileno (fp
), "message", tmpfil
);
175 fseek (fp
, 0L, SEEK_SET
);
177 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
178 die("unable to create temporary file in %s", get_temp_dir());
180 strncpy (drft
, tfile
, sizeof(tmpfil
));
182 rcvdistout (fp
, form
, addrs
);
185 if (distout (drft
, tmpfil
, backup
) == NOTOK
)
188 vec
[vecp
++] = "-dist";
189 if ((cp
= context_find ("mhlproc"))) {
190 vec
[vecp
++] = "-mhlproc";
199 adios("fork", "failed:");
202 execvp (program
, vec
);
203 fprintf (stderr
, "unable to exec ");
208 done (pidXwait(child_id
, postproc
));
211 return 0; /* dead code to satisfy the compiler */
214 /* very similar to routine in replsbr.c */
216 static int outputlinelen
= OUTPUTLINELEN
;
218 static struct format
*fmt
;
222 static char *addrcomps
[] = {
240 rcvdistout (FILE *inb
, char *form
, char *addrs
)
242 int char_read
= 0, format_len
, i
, state
;
244 char *cp
, name
[NAMESZ
], tmpbuf
[NMH_BUFSIZ
];
248 m_getfld_state_t gstate
;
250 if (!(out
= fopen (drft
, "w")))
251 adios (drft
, "unable to create");
253 /* get new format string */
254 cp
= new_fs (form
? form
: rcvdistcomps
, NULL
, NULL
);
255 format_len
= strlen (cp
);
256 fmt_compile (cp
, &fmt
, 1);
258 for (ap
= addrcomps
; *ap
; ap
++) {
259 cptr
= fmt_findcomp (*ap
);
261 cptr
->c_type
|= CT_ADDR
;
264 cptr
= fmt_findcomp ("addresses");
266 cptr
->c_text
= addrs
;
268 gstate
= m_getfld_state_init(inb
);
270 int msg_count
= sizeof tmpbuf
;
271 switch (state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
)) {
274 i
= fmt_addcomptext(name
, tmpbuf
);
276 char_read
+= msg_count
;
277 while (state
== FLDPLUS
) {
278 msg_count
= sizeof tmpbuf
;
279 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
280 fmt_appendcomp(i
, name
, tmpbuf
);
281 char_read
+= msg_count
;
285 while (state
== FLDPLUS
) {
286 msg_count
= sizeof tmpbuf
;
287 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
298 die("m_getfld2() returned %d", state
);
302 m_getfld_state_destroy (&gstate
);
304 i
= format_len
+ char_read
+ 256;
305 scanl
= charstring_create (i
+ 2);
306 dat
[0] = dat
[1] = dat
[2] = dat
[4] = 0;
307 dat
[3] = outputlinelen
;
308 fmt_scan (fmt
, scanl
, i
, dat
, NULL
);
309 fputs (charstring_buffer (scanl
), out
);
312 adios (drft
, "error writing");
315 charstring_free (scanl
);
321 unlink_done (int status
)
324 (void) m_unlink (backup
);
326 (void) m_unlink (drft
);
328 (void) m_unlink (tmpfil
);
330 exit(status
? 1 : 0);