]> diplodocus.org Git - nmh/blob - uip/send.c
vector.c: Move interface to own file.
[nmh] / uip / send.c
1 /* send.c -- send a composed message
2 *
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.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/push.h"
10 #include "sbr/path.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/arglist.h"
14 #include "sbr/error.h"
15 #include <fcntl.h>
16 #include "h/done.h"
17 #include "h/utils.h"
18 #ifdef OAUTH_SUPPORT
19 #include "h/oauth.h"
20 #endif
21 #include "sbr/m_maildir.h"
22 #include "sbr/m_mktemp.h"
23
24 #ifndef CYRUS_SASL
25 # define SASLminc(a) (a)
26 #else /* CYRUS_SASL */
27 # define SASLminc(a) 0
28 #endif /* CYRUS_SASL */
29
30 #ifndef TLS_SUPPORT
31 # define TLSminc(a) (a)
32 #else /* TLS_SUPPORT */
33 # define TLSminc(a) 0
34 #endif /* TLS_SUPPORT */
35
36 #define SEND_SWITCHES \
37 X("alias aliasfile", 0, ALIASW) \
38 X("debug", -5, DEBUGSW) \
39 X("draft", 0, DRAFTSW) \
40 X("draftfolder +folder", 6, DFOLDSW) \
41 X("draftmessage msg", 6, DMSGSW) \
42 X("nodraftfolder", 0, NDFLDSW) \
43 X("filter filterfile", 0, FILTSW) \
44 X("nofilter", 0, NFILTSW) \
45 X("format", 0, FRMTSW) \
46 X("noformat", 0, NFRMTSW) \
47 X("forward", 0, FORWSW) \
48 X("noforward", 0, NFORWSW) \
49 X("mime", 0, MIMESW) \
50 X("nomime", 0, NMIMESW) \
51 X("msgid", 0, MSGDSW) \
52 X("nomsgid", 0, NMSGDSW) \
53 X("push", 0, PUSHSW) \
54 X("nopush", 0, NPUSHSW) \
55 X("split seconds", 0, SPLITSW) \
56 X("unique", -6, UNIQSW) \
57 X("nounique", -8, NUNIQSW) \
58 X("verbose", 0, VERBSW) \
59 X("noverbose", 0, NVERBSW) \
60 X("watch", 0, WATCSW) \
61 X("nowatch", 0, NWATCSW) \
62 X("width columns", 0, WIDTHSW) \
63 X("version", 0, VERSIONSW) \
64 X("help", 0, HELPSW) \
65 X("dashstuffing", -12, BITSTUFFSW) \
66 X("nodashstuffing", -14, NBITSTUFFSW) \
67 X("client host", -6, CLIESW) \
68 X("server host", 6, SERVSW) \
69 X("snoop", 5, SNOOPSW) \
70 X("sasl", SASLminc(4), SASLSW) \
71 X("nosasl", SASLminc(6), NOSASLSW) \
72 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
73 X("authservice", SASLminc(0), AUTHSERVICESW) \
74 X("user username", SASLminc(-4), USERSW) \
75 X("port server-port-name/number", 4, PORTSW) \
76 X("tls", TLSminc(-3), TLSSW) \
77 X("initialtls", TLSminc(-10), INITTLSSW) \
78 X("notls", TLSminc(-5), NTLSSW) \
79 X("certverify", TLSminc(-10), CERTVERSW) \
80 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
81 X("sendmail program", 0, MTSSM) \
82 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
83 X("messageid localname|random", 2, MESSAGEIDSW) \
84
85 #define X(sw, minchars, id) id,
86 DEFINE_SWITCH_ENUM(SEND);
87 #undef X
88
89 #define X(sw, minchars, id) { sw, minchars, id },
90 DEFINE_SWITCH_ARRAY(SEND, switches);
91 #undef X
92
93 #define USE_SWITCHES \
94 X("no", 0, NOSW) \
95 X("yes", 0, YESW) \
96 X("list", 0, LISTDSW) \
97
98 #define X(sw, minchars, id) id,
99 DEFINE_SWITCH_ENUM(USE);
100 #undef X
101
102 #define X(sw, minchars, id) { sw, minchars, id },
103 DEFINE_SWITCH_ARRAY(USE, anyl);
104 #undef X
105
106 extern int debugsw; /* from sendsbr.c */
107 extern bool forwsw;
108 extern int inplace;
109 extern bool pushsw;
110 extern int splitsw;
111 extern bool unique;
112 extern bool verbsw;
113
114 extern char *altmsg; /* .. */
115 extern char *annotext;
116 extern char *distfile;
117
118
119 int
120 main (int argc, char **argv)
121 {
122 int msgp = 0, vecp;
123 int isdf = 0;
124 int msgnum, status;
125 char *cp, *dfolder = NULL, *maildir = NULL;
126 char buf[BUFSIZ], **ap, **argp, **arguments, *program;
127 char *msgs[MAXARGS], **vec;
128 const char *user = NULL, *saslmech = NULL;
129 struct msgs *mp;
130 struct stat st;
131 char *auth_svc = NULL;
132
133 if (nmh_init(argv[0], true, true)) { return 1; }
134
135 arguments = getarguments (invo_name, argc, argv, 1);
136 argp = arguments;
137
138 vec = argsplit(postproc, &program, &vecp);
139
140 vec[vecp++] = "-library";
141 vec[vecp++] = mh_xstrdup(m_maildir(""));
142
143 if ((cp = context_find ("fileproc"))) {
144 vec[vecp++] = "-fileproc";
145 vec[vecp++] = cp;
146 }
147
148 if ((cp = context_find ("mhlproc"))) {
149 vec[vecp++] = "-mhlproc";
150 vec[vecp++] = cp;
151 }
152
153 if ((cp = context_find ("credentials"))) {
154 /* post doesn't read context so need to pass credentials. */
155 vec[vecp++] = "-credentials";
156 vec[vecp++] = cp;
157 }
158
159 while ((cp = *argp++)) {
160 if (*cp == '-') {
161 switch (smatch (++cp, switches)) {
162 case AMBIGSW:
163 ambigsw (cp, switches);
164 done (1);
165 case UNKWNSW:
166 die("-%s unknown\n", cp);
167
168 case HELPSW:
169 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
170 print_help (buf, switches, 1);
171 done (0);
172 case VERSIONSW:
173 print_version(invo_name);
174 done (0);
175
176 case DRAFTSW:
177 msgs[msgp++] = draft;
178 continue;
179
180 case DFOLDSW:
181 if (dfolder)
182 die("only one draft folder at a time!");
183 if (!(cp = *argp++) || *cp == '-')
184 die("missing argument to %s", argp[-2]);
185 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
186 *cp != '@' ? TFOLDER : TSUBCWF);
187 continue;
188 case DMSGSW:
189 if (!(cp = *argp++) || *cp == '-')
190 die("missing argument to %s", argp[-2]);
191 msgs[msgp++] = cp;
192 continue;
193 case NDFLDSW:
194 dfolder = NULL;
195 isdf = NOTOK;
196 continue;
197
198 case PUSHSW:
199 pushsw = true;
200 continue;
201 case NPUSHSW:
202 pushsw = false;
203 continue;
204
205 case SPLITSW:
206 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
207 die("missing argument to %s", argp[-2]);
208 continue;
209
210 case UNIQSW:
211 unique = true;
212 continue;
213 case NUNIQSW:
214 unique = false;
215 continue;
216
217 case FORWSW:
218 forwsw = true;
219 continue;
220 case NFORWSW:
221 forwsw = false;
222 continue;
223
224 case VERBSW:
225 verbsw = true;
226 vec[vecp++] = --cp;
227 continue;
228 case NVERBSW:
229 verbsw = false;
230 vec[vecp++] = --cp;
231 continue;
232
233 case MIMESW:
234 vec[vecp++] = --cp;
235 continue;
236 case NMIMESW:
237 vec[vecp++] = --cp;
238 continue;
239
240 case SNOOPSW:
241 vec[vecp++] = --cp;
242 continue;
243
244 case DEBUGSW:
245 debugsw++;
246 /* FALLTHRU */
247 case NFILTSW:
248 case FRMTSW:
249 case NFRMTSW:
250 case BITSTUFFSW:
251 case NBITSTUFFSW:
252 case MSGDSW:
253 case NMSGDSW:
254 case WATCSW:
255 case NWATCSW:
256 case SASLSW:
257 case NOSASLSW:
258 case TLSSW:
259 case INITTLSSW:
260 case NTLSSW:
261 case CERTVERSW:
262 case NOCERTVERSW:
263 vec[vecp++] = --cp;
264 continue;
265
266 case USERSW:
267 vec[vecp++] = --cp;
268 if (!(cp = *argp++) || *cp == '-')
269 die("missing argument to %s", argp[-2]);
270 vec[vecp++] = cp;
271 user = cp;
272 continue;
273
274 case AUTHSERVICESW:
275 #ifdef OAUTH_SUPPORT
276 if (!(auth_svc = *argp++) || *auth_svc == '-')
277 die("missing argument to %s", argp[-2]);
278 #else
279 die("not built with OAuth support");
280 #endif
281 continue;
282
283 case SASLMECHSW:
284 if (!(saslmech = *argp) || *saslmech == '-')
285 die("missing argument to %s", argp[-1]);
286 /* FALLTHRU */
287
288 case ALIASW:
289 case FILTSW:
290 case WIDTHSW:
291 case CLIESW:
292 case SERVSW:
293 case PORTSW:
294 case MTSSM:
295 case MTSSW:
296 case MESSAGEIDSW:
297 vec[vecp++] = --cp;
298 if (!(cp = *argp++) || *cp == '-')
299 die("missing argument to %s", argp[-2]);
300 vec[vecp++] = cp;
301 continue;
302 }
303 } else {
304 msgs[msgp++] = cp;
305 }
306 }
307
308 /*
309 * check for "Aliasfile:" profile entry
310 */
311 if ((cp = context_find ("Aliasfile"))) {
312 char *dp = NULL;
313
314 for (ap = brkstring(dp = mh_xstrdup(cp), " ", "\n"); ap && *ap; ap++) {
315 vec[vecp++] = "-alias";
316 vec[vecp++] = *ap;
317 }
318 }
319
320 if (dfolder == NULL) {
321 if (msgp == 0) {
322 msgs[msgp++] = mh_xstrdup(m_draft(NULL, NULL, 1, &isdf));
323 if (stat (msgs[0], &st) == NOTOK)
324 adios (msgs[0], "unable to stat draft file");
325 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
326 for (status = LISTDSW; status != YESW;) {
327 if (!(argp = read_switch_multiword (cp, anyl)))
328 done (1);
329 switch (status = smatch (*argp, anyl)) {
330 case NOSW:
331 done (0);
332 case YESW:
333 break;
334 case LISTDSW:
335 showfile (++argp, msgs[0]);
336 break;
337 default:
338 inform("say what?");
339 break;
340 }
341 }
342 } else {
343 for (msgnum = 0; msgnum < msgp; msgnum++)
344 msgs[msgnum] = mh_xstrdup(m_maildir(msgs[msgnum]));
345 }
346 } else {
347 if (!context_find ("path"))
348 free (path ("./", TFOLDER));
349
350 if (!msgp)
351 msgs[msgp++] = "cur";
352 maildir = m_maildir (dfolder);
353
354 if (chdir (maildir) == NOTOK)
355 adios (maildir, "unable to change directory to");
356
357 /* read folder and create message structure */
358 if (!(mp = folder_read (dfolder, 1)))
359 die("unable to read folder %s", dfolder);
360
361 /* check for empty folder */
362 if (mp->nummsg == 0)
363 die("no messages in %s", dfolder);
364
365 /* parse all the message ranges/sequences and set SELECTED */
366 for (msgnum = 0; msgnum < msgp; msgnum++)
367 if (!m_convert (mp, msgs[msgnum]))
368 done (1);
369 seq_setprev (mp); /* set the previous-sequence */
370
371 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
372 if (is_selected (mp, msgnum)) {
373 msgs[msgp++] = mh_xstrdup(m_name (msgnum));
374 unset_exists (mp, msgnum);
375 }
376 }
377
378 mp->msgflags |= SEQMOD;
379 seq_save (mp);
380 }
381
382 #ifdef WHATNOW
383 go_to_it:
384 #endif /* WHATNOW */
385
386 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
387 if ((cp = context_find ("signature")) && *cp)
388 setenv("SIGNATURE", cp, 1);
389
390 for (msgnum = 0; msgnum < msgp; msgnum++)
391 if (stat (msgs[msgnum], &st) == NOTOK)
392 adios (msgs[msgnum], "unable to stat draft file");
393
394 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
395 annotext = NULL;
396 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
397 inplace = atoi (cp);
398 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
399 altmsg = NULL; /* used by dist interface - see below */
400
401 if ((cp = getenv ("mhdist"))
402 && *cp
403 && atoi(cp)
404 && altmsg) {
405 vec[vecp++] = "-dist";
406 if ((cp = m_mktemp2(altmsg, invo_name, NULL, NULL)) == NULL) {
407 die("unable to create temporary file");
408 }
409 distfile = mh_xstrdup(cp);
410 (void) m_unlink(distfile);
411 if (link (altmsg, distfile) == NOTOK) {
412 /* Cygwin with FAT32 filesystem produces EPERM. */
413 if (errno != EXDEV && errno != EPERM
414 #ifdef EISREMOTE
415 && errno != EISREMOTE
416 #endif /* EISREMOTE */
417 )
418 adios (distfile, "unable to link %s to", altmsg);
419 free (distfile);
420 if ((cp = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
421 die("unable to create temporary file in %s",
422 get_temp_dir());
423 }
424 distfile = mh_xstrdup(cp);
425 {
426 int in, out;
427 struct stat st;
428
429 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
430 adios (altmsg, "unable to open");
431 fstat(in, &st);
432 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
433 adios (distfile, "unable to write");
434 cpydata (in, out, altmsg, distfile);
435 close (in);
436 close (out);
437 }
438 }
439 } else {
440 distfile = NULL;
441 }
442
443 #ifdef OAUTH_SUPPORT
444 if (auth_svc == NULL) {
445 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
446 die("must specify -authservice with -saslmech xoauth2");
447 }
448 } else {
449 if (user == NULL) {
450 die("must specify -user with -saslmech xoauth2");
451 }
452 }
453 #else
454 NMH_UNUSED(auth_svc);
455 NMH_UNUSED(user);
456 NMH_UNUSED(saslmech);
457 #endif /* OAUTH_SUPPORT */
458
459 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
460 st.st_mtime = 0;
461 st.st_dev = 0;
462 st.st_ino = 0;
463 }
464 if (pushsw)
465 push ();
466
467 status = 0;
468 closefds (3);
469
470 for (msgnum = 0; msgnum < msgp; msgnum++) {
471 switch (sendsbr (vec, vecp, program, msgs[msgnum], &st, 1, auth_svc)) {
472 case DONE:
473 done (++status);
474 /* FALLTHRU */
475 case NOTOK:
476 status++;
477 /* FALLTHRU */
478 case OK:
479 break;
480 }
481 }
482
483 context_save (); /* save the context file */
484 done (status);
485 return 1;
486 }