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