]>
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.
9 #include "sbr/context_find.h"
10 #include "sbr/brkstring.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
18 #include "h/signals.h"
19 #include "sbr/m_maildir.h"
20 #include "sbr/m_mktemp.h"
23 # define SASLminc(a) (a)
24 #else /* CYRUS_SASL */
25 # define SASLminc(a) 0
26 #endif /* CYRUS_SASL */
29 # define TLSminc(a) (a)
30 #else /* TLS_SUPPORT */
32 #endif /* TLS_SUPPORT */
34 #define WHOM_SWITCHES \
35 X("alias aliasfile", 0, ALIASW) \
36 X("check", 0, CHKSW) \
37 X("nocheck", 0, NOCHKSW) \
38 X("draft", 0, DRAFTSW) \
39 X("draftfolder +folder", 6, DFOLDSW) \
40 X("draftmessage msg", 6, DMSGSW) \
41 X("nodraftfolder", 0, NDFLDSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("client host", -6, CLIESW) \
45 X("server host", 0, SERVSW) \
46 X("snoop", 0, SNOOPSW) \
47 X("sasl", SASLminc(4), SASLSW) \
48 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
49 X("user username", SASLminc(-4), USERSW) \
50 X("port server port name/number", 4, PORTSW) \
51 X("tls", TLSminc(-3), TLSSW) \
52 X("initialtls", TLSminc(-10), INITTLSSW) \
53 X("notls", TLSminc(-5), NTLSSW) \
54 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(WHOM
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(WHOM
, switches
);
66 main (int argc
, char **argv
)
72 char *cp
, *dfolder
= NULL
, *dmsg
= NULL
;
73 char *msg
= NULL
, **ap
, **argp
, backup
[BUFSIZ
];
74 char buf
[BUFSIZ
], **arguments
, *vec
[MAXARGS
];
76 if (nmh_init(argv
[0], true, false)) { return 1; }
78 arguments
= getarguments (invo_name
, argc
, argv
, 1);
81 vec
[vecp
++] = invo_name
;
82 vec
[vecp
++] = "-whom";
83 vec
[vecp
++] = "-library";
84 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
86 if ((cp
= context_find ("credentials"))) {
87 /* post doesn't read context so need to pass credentials. */
88 vec
[vecp
++] = "-credentials";
92 /* Don't need to feed fileproc or mhlproc to post because
93 it doesn't use them when used for whom. */
95 while ((cp
= *argp
++)) {
97 switch (smatch (++cp
, switches
)) {
99 ambigsw (cp
, switches
);
102 die("-%s unknown", cp
);
105 snprintf (buf
, sizeof(buf
), "%s [switches] [file]", invo_name
);
106 print_help (buf
, switches
, 1);
109 print_version(invo_name
);
128 die("only one draft folder at a time!");
129 if (!(cp
= *argp
++) || *cp
== '-')
130 die("missing argument to %s", argp
[-2]);
131 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
132 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
136 die("only one draft message at a time!");
137 if (!(dmsg
= *argp
++) || *dmsg
== '-')
138 die("missing argument to %s", argp
[-2]);
153 if (!(cp
= *argp
++) || *cp
== '-')
154 die("missing argument to %s", argp
[-2]);
160 die("only one draft at a time!");
161 vec
[vecp
++] = msg
= cp
;
164 /* allow Aliasfile: profile entry */
165 if ((cp
= context_find ("Aliasfile"))) {
168 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
169 vec
[vecp
++] = "-alias";
175 cp
= mh_xstrdup(m_draft(dfolder
, dmsg
, 1, &isdf
));
176 msg
= vec
[vecp
++] = cp
;
179 distsw
= (cp
= getenv("mhdist")) && *cp
&& atoi(cp
) &&
180 (cp
= getenv("mhaltmsg")) && *cp
;
182 if (distout (msg
, cp
, backup
) == NOTOK
)
184 vec
[vecp
++] = "-dist";
191 if ((child_id
= fork()) == -1)
192 adios("fork", "failed:");
195 SIGNAL (SIGHUP
, SIG_IGN
);
196 SIGNAL (SIGINT
, SIG_IGN
);
197 SIGNAL (SIGQUIT
, SIG_IGN
);
198 SIGNAL (SIGTERM
, SIG_IGN
);
200 status
= pidwait(child_id
, OK
);
202 (void) m_unlink (msg
);
203 if (rename (backup
, msg
) == NOTOK
)
204 adios (msg
, "unable to rename %s to", backup
);
209 /* Either the child, or no fork occurred. */
210 execvp (postproc
, vec
);
211 fprintf (stderr
, "unable to exec ");