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