]> diplodocus.org Git - nmh/blob - uip/whom.c
8973c76222aca1472a932013719cd02ffeae3a1c
[nmh] / uip / whom.c
1
2 /*
3 * whom.c -- report to whom a message would be sent
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12
13 #ifndef CYRUS_SASL
14 # define SASLminc(a) (a)
15 #else /* CYRUS_SASL */
16 # define SASLminc(a) 0
17 #endif /* CYRUS_SASL */
18
19 #ifndef TLS_SUPPORT
20 # define TLSminc(a) (a)
21 #else /* TLS_SUPPORT */
22 # define TLSminc(a) 0
23 #endif /* TLS_SUPPORT */
24
25 #define WHOM_SWITCHES \
26 X("alias aliasfile", 0, ALIASW) \
27 X("check", 0, CHKSW) \
28 X("nocheck", 0, NOCHKSW) \
29 X("draft", 0, DRAFTSW) \
30 X("draftfolder +folder", 6, DFOLDSW) \
31 X("draftmessage msg", 6, DMSGSW) \
32 X("nodraftfolder", 0, NDFLDSW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
35 X("client host", -6, CLIESW) \
36 X("server host", -6, SERVSW) \
37 X("snoop", -5, SNOOPSW) \
38 X("sasl", SASLminc(4), SASLSW) \
39 X("saslmech mechanism", SASLminc(-5), SASLMECHSW) \
40 X("user username", SASLminc(-4), USERSW) \
41 X("port server port name/number", 4, PORTSW) \
42 X("tls", TLSminc(-3), TLSSW) \
43 X("initialtls", TLSminc(-10), INITTLSSW) \
44 X("notls", TLSminc(-5), NTLSSW) \
45 X("mts smtp|sendmail/smtp|sendmail/pipe", 0, MTSSW) \
46
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(WHOM);
49 #undef X
50
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(WHOM, switches);
53 #undef X
54
55
56 int
57 main (int argc, char **argv)
58 {
59 pid_t child_id = OK;
60 int i, status, isdf = 0;
61 int distsw = 0, vecp = 0;
62 char *cp, *dfolder = NULL, *dmsg = NULL;
63 char *msg = NULL, **ap, **argp, backup[BUFSIZ];
64 char buf[BUFSIZ], **arguments, *vec[MAXARGS];
65
66 setlocale(LC_ALL, "");
67 invo_name = r1bindex (argv[0], '/');
68
69 /* read user profile/context */
70 context_read();
71
72 arguments = getarguments (invo_name, argc, argv, 1);
73 argp = arguments;
74
75 vec[vecp++] = invo_name;
76 vec[vecp++] = "-whom";
77 vec[vecp++] = "-library";
78 vec[vecp++] = getcpy (m_maildir (""));
79
80 /* Don't need to feed fileproc or mhlproc to post because
81 it doesn't use them when used for whom. */
82
83 while ((cp = *argp++)) {
84 if (*cp == '-') {
85 switch (smatch (++cp, switches)) {
86 case AMBIGSW:
87 ambigsw (cp, switches);
88 done (1);
89 case UNKWNSW:
90 adios (NULL, "-%s unknown", cp);
91
92 case HELPSW:
93 snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
94 print_help (buf, switches, 1);
95 done (0);
96 case VERSIONSW:
97 print_version(invo_name);
98 done (0);
99
100 case CHKSW:
101 case NOCHKSW:
102 case SNOOPSW:
103 case SASLSW:
104 case TLSSW:
105 case INITTLSSW:
106 case NTLSSW:
107 vec[vecp++] = --cp;
108 continue;
109
110 case DRAFTSW:
111 msg = draft;
112 continue;
113
114 case DFOLDSW:
115 if (dfolder)
116 adios (NULL, "only one draft folder at a time!");
117 if (!(cp = *argp++) || *cp == '-')
118 adios (NULL, "missing argument to %s", argp[-2]);
119 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
120 *cp != '@' ? TFOLDER : TSUBCWF);
121 continue;
122 case DMSGSW:
123 if (dmsg)
124 adios (NULL, "only one draft message at a time!");
125 if (!(dmsg = *argp++) || *dmsg == '-')
126 adios (NULL, "missing argument to %s", argp[-2]);
127 continue;
128 case NDFLDSW:
129 dfolder = NULL;
130 isdf = NOTOK;
131 continue;
132
133 case ALIASW:
134 case CLIESW:
135 case SERVSW:
136 case USERSW:
137 case PORTSW:
138 case SASLMECHSW:
139 case MTSSW:
140 vec[vecp++] = --cp;
141 if (!(cp = *argp++) || *cp == '-')
142 adios (NULL, "missing argument to %s", argp[-2]);
143 vec[vecp++] = cp;
144 continue;
145 }
146 }
147 if (msg)
148 adios (NULL, "only one draft at a time!");
149 else
150 vec[vecp++] = msg = cp;
151 }
152
153 /* allow Aliasfile: profile entry */
154 if ((cp = context_find ("Aliasfile"))) {
155 char *dp = NULL;
156
157 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
158 vec[vecp++] = "-alias";
159 vec[vecp++] = *ap;
160 }
161 }
162
163 if (msg == NULL) {
164 cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
165 msg = vec[vecp++] = cp;
166 }
167 if ((cp = getenv ("mhdist"))
168 && *cp
169 && (distsw = atoi (cp))
170 && (cp = getenv ("mhaltmsg"))
171 && *cp) {
172 if (distout (msg, cp, backup) == NOTOK)
173 done (1);
174 vec[vecp++] = "-dist";
175 distsw++;
176 }
177 vec[vecp] = NULL;
178
179 closefds (3);
180
181 if (distsw) {
182 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
183 sleep (5);
184 }
185
186 switch (distsw ? child_id : OK) {
187 case NOTOK:
188 advise (NULL, "unable to fork, so checking directly...");
189 case OK:
190 execvp (postproc, vec);
191 fprintf (stderr, "unable to exec ");
192 perror (postproc);
193 _exit (-1);
194
195 default:
196 SIGNAL (SIGHUP, SIG_IGN);
197 SIGNAL (SIGINT, SIG_IGN);
198 SIGNAL (SIGQUIT, SIG_IGN);
199 SIGNAL (SIGTERM, SIG_IGN);
200
201 status = pidwait(child_id, OK);
202
203 unlink (msg);
204 if (rename (backup, msg) == NOTOK)
205 adios (msg, "unable to rename %s to", backup);
206 done (status);
207 }
208
209 return 0; /* dead code to satisfy the compiler */
210 }