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