]>
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/getarguments.h"
11 #include "sbr/smatch.h"
12 #include "sbr/closefds.h"
13 #include "sbr/m_draft.h"
14 #include "sbr/context_find.h"
15 #include "sbr/brkstring.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/error.h"
23 #include "h/signals.h"
24 #include "sbr/m_maildir.h"
25 #include "sbr/m_mktemp.h"
28 # define SASLminc(a) (a)
29 #else /* CYRUS_SASL */
30 # define SASLminc(a) 0
31 #endif /* CYRUS_SASL */
34 # define TLSminc(a) (a)
35 #else /* TLS_SUPPORT */
37 #endif /* TLS_SUPPORT */
39 #define WHOM_SWITCHES \
40 X("alias aliasfile", 0, ALIASW) \
41 X("check", 0, CHKSW) \
42 X("nocheck", 0, NOCHKSW) \
43 X("draft", 0, DRAFTSW) \
44 X("draftfolder +folder", 6, DFOLDSW) \
45 X("draftmessage msg", 6, DMSGSW) \
46 X("nodraftfolder", 0, NDFLDSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
49 X("client host", -6, CLIESW) \
50 X("server host", 0, SERVSW) \
51 X("snoop", 0, SNOOPSW) \
52 X("sasl", SASLminc(4), SASLSW) \
53 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
54 X("user username", SASLminc(-4), USERSW) \
55 X("port server port name/number", 4, PORTSW) \
56 X("tls", TLSminc(-3), TLSSW) \
57 X("initialtls", TLSminc(-10), INITTLSSW) \
58 X("notls", TLSminc(-5), NTLSSW) \
59 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(WHOM
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(WHOM
, switches
);
71 main (int argc
, char **argv
)
77 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
78 char *msg
= NULL
, **ap
, **argp
, backup
[BUFSIZ
];
79 char buf
[BUFSIZ
], **arguments
, *vec
[MAXARGS
];
81 if (nmh_init(argv
[0], true, false)) { return 1; }
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 vec
[vecp
++] = invo_name
;
87 vec
[vecp
++] = "-whom";
88 vec
[vecp
++] = "-library";
89 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
91 if ((cp
= context_find ("credentials"))) {
92 /* post doesn't read context so need to pass credentials. */
93 vec
[vecp
++] = "-credentials";
97 /* Don't need to feed fileproc or mhlproc to post because
98 it doesn't use them when used for whom. */
100 while ((cp
= *argp
++)) {
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 die("-%s unknown", cp
);
110 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
111 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
133 die("only one draft folder at a time!");
134 if (!(cp
= *argp
++) || *cp
== '-')
135 die("missing argument to %s", argp
[-2]);
136 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
137 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
141 die("only one draft message at a time!");
142 if (!(dmsg
= *argp
++) || *dmsg
== '-')
143 die("missing argument to %s", argp
[-2]);
158 if (!(cp
= *argp
++) || *cp
== '-')
159 die("missing argument to %s", argp
[-2]);
165 die("only one draft at a time!");
166 vec
[vecp
++] = msg
= cp
;
169 /* allow Aliasfile: profile entry */
170 if ((cp
= context_find ("Aliasfile"))) {
173 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
174 vec
[vecp
++] = "-alias";
180 cp
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
181 msg
= vec
[vecp
++] = cp
;
184 distsw
= (cp
= getenv("mhdist")) && *cp
&& atoi(cp
) &&
185 (cp
= getenv("mhaltmsg")) && *cp
;
187 if (distout (msg
, cp
, backup
) == NOTOK
)
189 vec
[vecp
++] = "-dist";
196 if ((child_id
= fork()) == -1)
197 adios("fork", "failed:");
200 SIGNAL (SIGHUP
, SIG_IGN
);
201 SIGNAL (SIGINT
, SIG_IGN
);
202 SIGNAL (SIGQUIT
, SIG_IGN
);
203 SIGNAL (SIGTERM
, SIG_IGN
);
205 status
= pidwait(child_id
, OK
);
207 (void) m_unlink (msg
);
208 if (rename (backup
, msg
) == NOTOK
)
209 adios (msg
, "unable to rename %s to", backup
);
214 /* Either the child, or no fork occurred. */
215 execvp (postproc
, vec
);
216 fprintf (stderr
, "unable to exec ");