]>
diplodocus.org Git - nmh/blob - uip/whom.c
cb49e7f29f6059e8f16d9a2bea305beb78412a78
1 /* whom.c -- report to whom a message would be sent
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/closefds.h"
10 #include "sbr/m_draft.h"
11 #include "sbr/context_find.h"
12 #include "sbr/brkstring.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
20 #include "h/signals.h"
21 #include "sbr/m_maildir.h"
22 #include "sbr/m_mktemp.h"
25 # define SASLminc(a) (a)
26 #else /* CYRUS_SASL */
27 # define SASLminc(a) 0
28 #endif /* CYRUS_SASL */
31 # define TLSminc(a) (a)
32 #else /* TLS_SUPPORT */
34 #endif /* TLS_SUPPORT */
36 #define WHOM_SWITCHES \
37 X("alias aliasfile", 0, ALIASW) \
38 X("check", 0, CHKSW) \
39 X("nocheck", 0, NOCHKSW) \
40 X("draft", 0, DRAFTSW) \
41 X("draftfolder +folder", 6, DFOLDSW) \
42 X("draftmessage msg", 6, DMSGSW) \
43 X("nodraftfolder", 0, NDFLDSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("client host", -6, CLIESW) \
47 X("server host", 0, SERVSW) \
48 X("snoop", 0, SNOOPSW) \
49 X("sasl", SASLminc(4), SASLSW) \
50 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
51 X("user username", SASLminc(-4), USERSW) \
52 X("port server port name/number", 4, PORTSW) \
53 X("tls", TLSminc(-3), TLSSW) \
54 X("initialtls", TLSminc(-10), INITTLSSW) \
55 X("notls", TLSminc(-5), NTLSSW) \
56 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(WHOM
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(WHOM
, switches
);
68 main (int argc
, char **argv
)
74 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
75 char *msg
= NULL
, **ap
, **argp
, backup
[BUFSIZ
];
76 char buf
[BUFSIZ
], **arguments
, *vec
[MAXARGS
];
78 if (nmh_init(argv
[0], true, false)) { return 1; }
80 arguments
= getarguments (invo_name
, argc
, argv
, 1);
83 vec
[vecp
++] = invo_name
;
84 vec
[vecp
++] = "-whom";
85 vec
[vecp
++] = "-library";
86 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
88 if ((cp
= context_find ("credentials"))) {
89 /* post doesn't read context so need to pass credentials. */
90 vec
[vecp
++] = "-credentials";
94 /* Don't need to feed fileproc or mhlproc to post because
95 it doesn't use them when used for whom. */
97 while ((cp
= *argp
++)) {
99 switch (smatch (++cp
, switches
)) {
101 ambigsw (cp
, switches
);
104 die("-%s unknown", cp
);
107 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
130 die("only one draft folder at a time!");
131 if (!(cp
= *argp
++) || *cp
== '-')
132 die("missing argument to %s", argp
[-2]);
133 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
134 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
138 die("only one draft message at a time!");
139 if (!(dmsg
= *argp
++) || *dmsg
== '-')
140 die("missing argument to %s", argp
[-2]);
155 if (!(cp
= *argp
++) || *cp
== '-')
156 die("missing argument to %s", argp
[-2]);
162 die("only one draft at a time!");
163 vec
[vecp
++] = msg
= cp
;
166 /* allow Aliasfile: profile entry */
167 if ((cp
= context_find ("Aliasfile"))) {
170 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
171 vec
[vecp
++] = "-alias";
177 cp
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
178 msg
= vec
[vecp
++] = cp
;
181 distsw
= (cp
= getenv("mhdist")) && *cp
&& atoi(cp
) &&
182 (cp
= getenv("mhaltmsg")) && *cp
;
184 if (distout (msg
, cp
, backup
) == NOTOK
)
186 vec
[vecp
++] = "-dist";
193 if ((child_id
= fork()) == -1)
194 adios("fork", "failed:");
197 SIGNAL (SIGHUP
, SIG_IGN
);
198 SIGNAL (SIGINT
, SIG_IGN
);
199 SIGNAL (SIGQUIT
, SIG_IGN
);
200 SIGNAL (SIGTERM
, SIG_IGN
);
202 status
= pidwait(child_id
, OK
);
204 (void) m_unlink (msg
);
205 if (rename (backup
, msg
) == NOTOK
)
206 adios (msg
, "unable to rename %s to", backup
);
211 /* Either the child, or no fork occurred. */
212 execvp (postproc
, vec
);
213 fprintf (stderr
, "unable to exec ");