]>
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
15 #include "h/signals.h"
16 #include "sbr/m_maildir.h"
17 #include "sbr/m_mktemp.h"
20 # define SASLminc(a) (a)
21 #else /* CYRUS_SASL */
22 # define SASLminc(a) 0
23 #endif /* CYRUS_SASL */
26 # define TLSminc(a) (a)
27 #else /* TLS_SUPPORT */
29 #endif /* TLS_SUPPORT */
31 #define WHOM_SWITCHES \
32 X("alias aliasfile", 0, ALIASW) \
33 X("check", 0, CHKSW) \
34 X("nocheck", 0, NOCHKSW) \
35 X("draft", 0, DRAFTSW) \
36 X("draftfolder +folder", 6, DFOLDSW) \
37 X("draftmessage msg", 6, DMSGSW) \
38 X("nodraftfolder", 0, NDFLDSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
41 X("client host", -6, CLIESW) \
42 X("server host", 0, SERVSW) \
43 X("snoop", 0, SNOOPSW) \
44 X("sasl", SASLminc(4), SASLSW) \
45 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
46 X("user username", SASLminc(-4), USERSW) \
47 X("port server port name/number", 4, PORTSW) \
48 X("tls", TLSminc(-3), TLSSW) \
49 X("initialtls", TLSminc(-10), INITTLSSW) \
50 X("notls", TLSminc(-5), NTLSSW) \
51 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(WHOM
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(WHOM
, switches
);
63 main (int argc
, char **argv
)
69 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
70 char *msg
= NULL
, **ap
, **argp
, backup
[BUFSIZ
];
71 char buf
[BUFSIZ
], **arguments
, *vec
[MAXARGS
];
73 if (nmh_init(argv
[0], true, false)) { return 1; }
75 arguments
= getarguments (invo_name
, argc
, argv
, 1);
78 vec
[vecp
++] = invo_name
;
79 vec
[vecp
++] = "-whom";
80 vec
[vecp
++] = "-library";
81 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
83 if ((cp
= context_find ("credentials"))) {
84 /* post doesn't read context so need to pass credentials. */
85 vec
[vecp
++] = "-credentials";
89 /* Don't need to feed fileproc or mhlproc to post because
90 it doesn't use them when used for whom. */
92 while ((cp
= *argp
++)) {
94 switch (smatch (++cp
, switches
)) {
96 ambigsw (cp
, switches
);
99 die("-%s unknown", cp
);
102 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
103 print_help (buf
, switches
, 1);
106 print_version(invo_name
);
125 die("only one draft folder at a time!");
126 if (!(cp
= *argp
++) || *cp
== '-')
127 die("missing argument to %s", argp
[-2]);
128 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
129 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
133 die("only one draft message at a time!");
134 if (!(dmsg
= *argp
++) || *dmsg
== '-')
135 die("missing argument to %s", argp
[-2]);
150 if (!(cp
= *argp
++) || *cp
== '-')
151 die("missing argument to %s", argp
[-2]);
157 die("only one draft at a time!");
158 vec
[vecp
++] = msg
= cp
;
161 /* allow Aliasfile: profile entry */
162 if ((cp
= context_find ("Aliasfile"))) {
165 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
166 vec
[vecp
++] = "-alias";
172 cp
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
173 msg
= vec
[vecp
++] = cp
;
176 distsw
= (cp
= getenv("mhdist")) && *cp
&& atoi(cp
) &&
177 (cp
= getenv("mhaltmsg")) && *cp
;
179 if (distout (msg
, cp
, backup
) == NOTOK
)
181 vec
[vecp
++] = "-dist";
188 if ((child_id
= fork()) == -1)
189 adios("fork", "failed:");
192 SIGNAL (SIGHUP
, SIG_IGN
);
193 SIGNAL (SIGINT
, SIG_IGN
);
194 SIGNAL (SIGQUIT
, SIG_IGN
);
195 SIGNAL (SIGTERM
, SIG_IGN
);
197 status
= pidwait(child_id
, OK
);
199 (void) m_unlink (msg
);
200 if (rename (backup
, msg
) == NOTOK
)
201 adios (msg
, "unable to rename %s to", backup
);
206 /* Either the child, or no fork occurred. */
207 execvp (postproc
, vec
);
208 fprintf (stderr
, "unable to exec ");