]>
diplodocus.org Git - nmh/blob - uip/whom.c
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/smatch.h"
10 #include "sbr/closefds.h"
11 #include "sbr/m_draft.h"
12 #include "sbr/context_find.h"
13 #include "sbr/brkstring.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
21 #include "h/signals.h"
22 #include "sbr/m_maildir.h"
23 #include "sbr/m_mktemp.h"
26 # define SASLminc(a) (a)
27 #else /* CYRUS_SASL */
28 # define SASLminc(a) 0
29 #endif /* CYRUS_SASL */
32 # define TLSminc(a) (a)
33 #else /* TLS_SUPPORT */
35 #endif /* TLS_SUPPORT */
37 #define WHOM_SWITCHES \
38 X("alias aliasfile", 0, ALIASW) \
39 X("check", 0, CHKSW) \
40 X("nocheck", 0, NOCHKSW) \
41 X("draft", 0, DRAFTSW) \
42 X("draftfolder +folder", 6, DFOLDSW) \
43 X("draftmessage msg", 6, DMSGSW) \
44 X("nodraftfolder", 0, NDFLDSW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
47 X("client host", -6, CLIESW) \
48 X("server host", 0, SERVSW) \
49 X("snoop", 0, SNOOPSW) \
50 X("sasl", SASLminc(4), SASLSW) \
51 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
52 X("user username", SASLminc(-4), USERSW) \
53 X("port server port name/number", 4, PORTSW) \
54 X("tls", TLSminc(-3), TLSSW) \
55 X("initialtls", TLSminc(-10), INITTLSSW) \
56 X("notls", TLSminc(-5), NTLSSW) \
57 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(WHOM
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(WHOM
, switches
);
69 main (int argc
, char **argv
)
75 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
76 char *msg
= NULL
, **ap
, **argp
, backup
[BUFSIZ
];
77 char buf
[BUFSIZ
], **arguments
, *vec
[MAXARGS
];
79 if (nmh_init(argv
[0], true, false)) { return 1; }
81 arguments
= getarguments (invo_name
, argc
, argv
, 1);
84 vec
[vecp
++] = invo_name
;
85 vec
[vecp
++] = "-whom";
86 vec
[vecp
++] = "-library";
87 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
89 if ((cp
= context_find ("credentials"))) {
90 /* post doesn't read context so need to pass credentials. */
91 vec
[vecp
++] = "-credentials";
95 /* Don't need to feed fileproc or mhlproc to post because
96 it doesn't use them when used for whom. */
98 while ((cp
= *argp
++)) {
100 switch (smatch (++cp
, switches
)) {
102 ambigsw (cp
, switches
);
105 die("-%s unknown", cp
);
108 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
109 print_help (buf
, switches
, 1);
112 print_version(invo_name
);
131 die("only one draft folder at a time!");
132 if (!(cp
= *argp
++) || *cp
== '-')
133 die("missing argument to %s", argp
[-2]);
134 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
135 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
139 die("only one draft message at a time!");
140 if (!(dmsg
= *argp
++) || *dmsg
== '-')
141 die("missing argument to %s", argp
[-2]);
156 if (!(cp
= *argp
++) || *cp
== '-')
157 die("missing argument to %s", argp
[-2]);
163 die("only one draft at a time!");
164 vec
[vecp
++] = msg
= cp
;
167 /* allow Aliasfile: profile entry */
168 if ((cp
= context_find ("Aliasfile"))) {
171 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
172 vec
[vecp
++] = "-alias";
178 cp
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
179 msg
= vec
[vecp
++] = cp
;
182 distsw
= (cp
= getenv("mhdist")) && *cp
&& atoi(cp
) &&
183 (cp
= getenv("mhaltmsg")) && *cp
;
185 if (distout (msg
, cp
, backup
) == NOTOK
)
187 vec
[vecp
++] = "-dist";
194 if ((child_id
= fork()) == -1)
195 adios("fork", "failed:");
198 SIGNAL (SIGHUP
, SIG_IGN
);
199 SIGNAL (SIGINT
, SIG_IGN
);
200 SIGNAL (SIGQUIT
, SIG_IGN
);
201 SIGNAL (SIGTERM
, SIG_IGN
);
203 status
= pidwait(child_id
, OK
);
205 (void) m_unlink (msg
);
206 if (rename (backup
, msg
) == NOTOK
)
207 adios (msg
, "unable to rename %s to", backup
);
212 /* Either the child, or no fork occurred. */
213 execvp (postproc
, vec
);
214 fprintf (stderr
, "unable to exec ");