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