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