]>
diplodocus.org Git - nmh/blob - uip/whom.c
d3e5c5fc0b397eb31560e2b8c4b03c1e22ac0cbd
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/brkstring.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
17 #include "h/signals.h"
18 #include "sbr/m_maildir.h"
19 #include "sbr/m_mktemp.h"
22 # define SASLminc(a) (a)
23 #else /* CYRUS_SASL */
24 # define SASLminc(a) 0
25 #endif /* CYRUS_SASL */
28 # define TLSminc(a) (a)
29 #else /* TLS_SUPPORT */
31 #endif /* TLS_SUPPORT */
33 #define WHOM_SWITCHES \
34 X("alias aliasfile", 0, ALIASW) \
35 X("check", 0, CHKSW) \
36 X("nocheck", 0, NOCHKSW) \
37 X("draft", 0, DRAFTSW) \
38 X("draftfolder +folder", 6, DFOLDSW) \
39 X("draftmessage msg", 6, DMSGSW) \
40 X("nodraftfolder", 0, NDFLDSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("client host", -6, CLIESW) \
44 X("server host", 0, SERVSW) \
45 X("snoop", 0, SNOOPSW) \
46 X("sasl", SASLminc(4), SASLSW) \
47 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
48 X("user username", SASLminc(-4), USERSW) \
49 X("port server port name/number", 4, PORTSW) \
50 X("tls", TLSminc(-3), TLSSW) \
51 X("initialtls", TLSminc(-10), INITTLSSW) \
52 X("notls", TLSminc(-5), NTLSSW) \
53 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(WHOM
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(WHOM
, switches
);
65 main (int argc
, char **argv
)
71 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
72 char *msg
= NULL
, **ap
, **argp
, backup
[BUFSIZ
];
73 char buf
[BUFSIZ
], **arguments
, *vec
[MAXARGS
];
75 if (nmh_init(argv
[0], true, false)) { return 1; }
77 arguments
= getarguments (invo_name
, argc
, argv
, 1);
80 vec
[vecp
++] = invo_name
;
81 vec
[vecp
++] = "-whom";
82 vec
[vecp
++] = "-library";
83 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
85 if ((cp
= context_find ("credentials"))) {
86 /* post doesn't read context so need to pass credentials. */
87 vec
[vecp
++] = "-credentials";
91 /* Don't need to feed fileproc or mhlproc to post because
92 it doesn't use them when used for whom. */
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
101 die("-%s unknown", cp
);
104 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
105 print_help (buf
, switches
, 1);
108 print_version(invo_name
);
127 die("only one draft folder at a time!");
128 if (!(cp
= *argp
++) || *cp
== '-')
129 die("missing argument to %s", argp
[-2]);
130 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
131 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
135 die("only one draft message at a time!");
136 if (!(dmsg
= *argp
++) || *dmsg
== '-')
137 die("missing argument to %s", argp
[-2]);
152 if (!(cp
= *argp
++) || *cp
== '-')
153 die("missing argument to %s", argp
[-2]);
159 die("only one draft at a time!");
160 vec
[vecp
++] = msg
= cp
;
163 /* allow Aliasfile: profile entry */
164 if ((cp
= context_find ("Aliasfile"))) {
167 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
168 vec
[vecp
++] = "-alias";
174 cp
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
175 msg
= vec
[vecp
++] = cp
;
178 distsw
= (cp
= getenv("mhdist")) && *cp
&& atoi(cp
) &&
179 (cp
= getenv("mhaltmsg")) && *cp
;
181 if (distout (msg
, cp
, backup
) == NOTOK
)
183 vec
[vecp
++] = "-dist";
190 if ((child_id
= fork()) == -1)
191 adios("fork", "failed:");
194 SIGNAL (SIGHUP
, SIG_IGN
);
195 SIGNAL (SIGINT
, SIG_IGN
);
196 SIGNAL (SIGQUIT
, SIG_IGN
);
197 SIGNAL (SIGTERM
, SIG_IGN
);
199 status
= pidwait(child_id
, OK
);
201 (void) m_unlink (msg
);
202 if (rename (backup
, msg
) == NOTOK
)
203 adios (msg
, "unable to rename %s to", backup
);
208 /* Either the child, or no fork occurred. */
209 execvp (postproc
, vec
);
210 fprintf (stderr
, "unable to exec ");