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