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