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