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