]>
diplodocus.org Git - nmh/blob - uip/whom.c
c2d1a3043ada0ef0508124ec3106134966160620
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/m_draft.h"
10 #include "sbr/context_find.h"
11 #include "sbr/brkstring.h"
12 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
19 #include "h/signals.h"
20 #include "sbr/m_maildir.h"
21 #include "sbr/m_mktemp.h"
24 # define SASLminc(a) (a)
25 #else /* CYRUS_SASL */
26 # define SASLminc(a) 0
27 #endif /* CYRUS_SASL */
30 # define TLSminc(a) (a)
31 #else /* TLS_SUPPORT */
33 #endif /* TLS_SUPPORT */
35 #define WHOM_SWITCHES \
36 X("alias aliasfile", 0, ALIASW) \
37 X("check", 0, CHKSW) \
38 X("nocheck", 0, NOCHKSW) \
39 X("draft", 0, DRAFTSW) \
40 X("draftfolder +folder", 6, DFOLDSW) \
41 X("draftmessage msg", 6, DMSGSW) \
42 X("nodraftfolder", 0, NDFLDSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
45 X("client host", -6, CLIESW) \
46 X("server host", 0, SERVSW) \
47 X("snoop", 0, SNOOPSW) \
48 X("sasl", SASLminc(4), SASLSW) \
49 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
50 X("user username", SASLminc(-4), USERSW) \
51 X("port server port name/number", 4, PORTSW) \
52 X("tls", TLSminc(-3), TLSSW) \
53 X("initialtls", TLSminc(-10), INITTLSSW) \
54 X("notls", TLSminc(-5), NTLSSW) \
55 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(WHOM
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(WHOM
, switches
);
67 main (int argc
, char **argv
)
73 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
74 char *msg
= NULL
, **ap
, **argp
, backup
[BUFSIZ
];
75 char buf
[BUFSIZ
], **arguments
, *vec
[MAXARGS
];
77 if (nmh_init(argv
[0], true, false)) { return 1; }
79 arguments
= getarguments (invo_name
, argc
, argv
, 1);
82 vec
[vecp
++] = invo_name
;
83 vec
[vecp
++] = "-whom";
84 vec
[vecp
++] = "-library";
85 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
87 if ((cp
= context_find ("credentials"))) {
88 /* post doesn't read context so need to pass credentials. */
89 vec
[vecp
++] = "-credentials";
93 /* Don't need to feed fileproc or mhlproc to post because
94 it doesn't use them when used for whom. */
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 die("-%s unknown", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
107 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
129 die("only one draft folder at a time!");
130 if (!(cp
= *argp
++) || *cp
== '-')
131 die("missing argument to %s", argp
[-2]);
132 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
133 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
137 die("only one draft message at a time!");
138 if (!(dmsg
= *argp
++) || *dmsg
== '-')
139 die("missing argument to %s", argp
[-2]);
154 if (!(cp
= *argp
++) || *cp
== '-')
155 die("missing argument to %s", argp
[-2]);
161 die("only one draft at a time!");
162 vec
[vecp
++] = msg
= cp
;
165 /* allow Aliasfile: profile entry */
166 if ((cp
= context_find ("Aliasfile"))) {
169 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
170 vec
[vecp
++] = "-alias";
176 cp
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
177 msg
= vec
[vecp
++] = cp
;
180 distsw
= (cp
= getenv("mhdist")) && *cp
&& atoi(cp
) &&
181 (cp
= getenv("mhaltmsg")) && *cp
;
183 if (distout (msg
, cp
, backup
) == NOTOK
)
185 vec
[vecp
++] = "-dist";
192 if ((child_id
= fork()) == -1)
193 adios("fork", "failed:");
196 SIGNAL (SIGHUP
, SIG_IGN
);
197 SIGNAL (SIGINT
, SIG_IGN
);
198 SIGNAL (SIGQUIT
, SIG_IGN
);
199 SIGNAL (SIGTERM
, SIG_IGN
);
201 status
= pidwait(child_id
, OK
);
203 (void) m_unlink (msg
);
204 if (rename (backup
, msg
) == NOTOK
)
205 adios (msg
, "unable to rename %s to", backup
);
210 /* Either the child, or no fork occurred. */
211 execvp (postproc
, vec
);
212 fprintf (stderr
, "unable to exec ");