]> diplodocus.org Git - nmh/blob - uip/send.c
sbr/utils.c: Add HasSuffix(s, suffix).
[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("sendmail program", 0, MTSSM) \
77 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
78 X("messageid localname|random", 2, MESSAGEIDSW) \
79
80 #define X(sw, minchars, id) id,
81 DEFINE_SWITCH_ENUM(SEND);
82 #undef X
83
84 #define X(sw, minchars, id) { sw, minchars, id },
85 DEFINE_SWITCH_ARRAY(SEND, switches);
86 #undef X
87
88 #define USE_SWITCHES \
89 X("no", 0, NOSW) \
90 X("yes", 0, YESW) \
91 X("list", 0, LISTDSW) \
92
93 #define X(sw, minchars, id) id,
94 DEFINE_SWITCH_ENUM(USE);
95 #undef X
96
97 #define X(sw, minchars, id) { sw, minchars, id },
98 DEFINE_SWITCH_ARRAY(USE, anyl);
99 #undef X
100
101 extern int debugsw; /* from sendsbr.c */
102 extern int forwsw;
103 extern int inplace;
104 extern int pushsw;
105 extern int splitsw;
106 extern int unique;
107 extern int verbsw;
108
109 extern char *altmsg; /* .. */
110 extern char *annotext;
111 extern char *distfile;
112
113
114 int
115 main (int argc, char **argv)
116 {
117 int msgp = 0, distsw = 0, vecp;
118 int isdf = 0, mime = 0;
119 int msgnum, status;
120 char *cp, *dfolder = NULL, *maildir = NULL;
121 char buf[BUFSIZ], **ap, **argp, **arguments, *program;
122 char *msgs[MAXARGS], **vec;
123 const char *user = NULL, *saslmech = NULL;
124 struct msgs *mp;
125 struct stat st;
126 int snoop = 0;
127 char *auth_svc = NULL;
128
129 if (nmh_init(argv[0], 1)) { return 1; }
130
131 arguments = getarguments (invo_name, argc, argv, 1);
132 argp = arguments;
133
134 vec = argsplit(postproc, &program, &vecp);
135
136 vec[vecp++] = "-library";
137 vec[vecp++] = getcpy (m_maildir (""));
138
139 if ((cp = context_find ("fileproc"))) {
140 vec[vecp++] = "-fileproc";
141 vec[vecp++] = cp;
142 }
143
144 if ((cp = context_find ("mhlproc"))) {
145 vec[vecp++] = "-mhlproc";
146 vec[vecp++] = cp;
147 }
148
149 if ((cp = context_find ("credentials"))) {
150 /* post doesn't read context so need to pass credentials. */
151 vec[vecp++] = "-credentials";
152 vec[vecp++] = cp;
153 }
154
155 while ((cp = *argp++)) {
156 if (*cp == '-') {
157 switch (smatch (++cp, switches)) {
158 case AMBIGSW:
159 ambigsw (cp, switches);
160 done (1);
161 case UNKWNSW:
162 adios (NULL, "-%s unknown\n", cp);
163
164 case HELPSW:
165 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
166 print_help (buf, switches, 1);
167 done (0);
168 case VERSIONSW:
169 print_version(invo_name);
170 done (0);
171
172 case DRAFTSW:
173 msgs[msgp++] = draft;
174 continue;
175
176 case DFOLDSW:
177 if (dfolder)
178 adios (NULL, "only one draft folder at a time!");
179 if (!(cp = *argp++) || *cp == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
181 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
182 *cp != '@' ? TFOLDER : TSUBCWF);
183 continue;
184 case DMSGSW:
185 if (!(cp = *argp++) || *cp == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 msgs[msgp++] = cp;
188 continue;
189 case NDFLDSW:
190 dfolder = NULL;
191 isdf = NOTOK;
192 continue;
193
194 case PUSHSW:
195 pushsw++;
196 continue;
197 case NPUSHSW:
198 pushsw = 0;
199 continue;
200
201 case SPLITSW:
202 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
203 adios (NULL, "missing argument to %s", argp[-2]);
204 continue;
205
206 case UNIQSW:
207 unique++;
208 continue;
209 case NUNIQSW:
210 unique = 0;
211 continue;
212
213 case FORWSW:
214 forwsw++;
215 continue;
216 case NFORWSW:
217 forwsw = 0;
218 continue;
219
220 case VERBSW:
221 verbsw++;
222 vec[vecp++] = --cp;
223 continue;
224 case NVERBSW:
225 verbsw = 0;
226 vec[vecp++] = --cp;
227 continue;
228
229 case MIMESW:
230 mime++;
231 vec[vecp++] = --cp;
232 continue;
233 case NMIMESW:
234 mime = 0;
235 vec[vecp++] = --cp;
236 continue;
237
238 case SNOOPSW:
239 snoop++;
240 vec[vecp++] = --cp;
241 continue;
242
243 case DEBUGSW:
244 debugsw++; /* fall */
245 case NFILTSW:
246 case FRMTSW:
247 case NFRMTSW:
248 case BITSTUFFSW:
249 case NBITSTUFFSW:
250 case MSGDSW:
251 case NMSGDSW:
252 case WATCSW:
253 case NWATCSW:
254 case SASLSW:
255 case NOSASLSW:
256 case TLSSW:
257 case INITTLSSW:
258 case NTLSSW:
259 vec[vecp++] = --cp;
260 continue;
261
262 case USERSW:
263 vec[vecp++] = --cp;
264 if (!(cp = *argp++) || *cp == '-')
265 adios (NULL, "missing argument to %s", argp[-2]);
266 vec[vecp++] = cp;
267 user = cp;
268 continue;
269
270 case AUTHSERVICESW:
271 #ifdef OAUTH_SUPPORT
272 if (!(auth_svc = *argp++) || *auth_svc == '-')
273 adios (NULL, "missing argument to %s", argp[-2]);
274 #else
275 adios (NULL, "not built with OAuth support");
276 #endif
277 continue;
278
279 case SASLMECHSW:
280 if (!(saslmech = *argp) || *saslmech == '-')
281 adios (NULL, "missing argument to %s", argp[-2]);
282 /* Fall through */
283
284 case ALIASW:
285 case FILTSW:
286 case WIDTHSW:
287 case CLIESW:
288 case SERVSW:
289 case PORTSW:
290 case MTSSM:
291 case MTSSW:
292 case MESSAGEIDSW:
293 vec[vecp++] = --cp;
294 if (!(cp = *argp++) || *cp == '-')
295 adios (NULL, "missing argument to %s", argp[-2]);
296 vec[vecp++] = cp;
297 continue;
298
299 case ATTACHSW:
300 advise(NULL, "The -attach switch is deprecated");
301 continue;
302 case NOATTACHSW:
303 advise(NULL, "The -noattach switch is deprecated");
304 continue;
305
306 case ATTACHFORMATSW:
307 advise(NULL, "The -attachformat switch is deprecated");
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++] = getcpy (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 advise (NULL, "say what?");
346 break;
347 }
348 }
349 } else {
350 for (msgnum = 0; msgnum < msgp; msgnum++)
351 msgs[msgnum] = getcpy (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 adios (NULL, "unable to read folder %s", dfolder);
367
368 /* check for empty folder */
369 if (mp->nummsg == 0)
370 adios (NULL, "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 m_putenv ("SIGNATURE", cp);
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 && (distsw = atoi (cp))
411 && altmsg) {
412 vec[vecp++] = "-dist";
413 if ((cp = m_mktemp2(altmsg, invo_name, NULL, NULL)) == NULL) {
414 adios(NULL, "unable to create temporary file in %s",
415 get_temp_dir());
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 adios(NULL, "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 adios (NULL, "must specify -authservice with -saslmech xoauth2");
455 }
456 } else {
457 if (user == NULL) {
458 adios (NULL, "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 case NOTOK:
483 status++; /* fall */
484 case OK:
485 break;
486 }
487 }
488
489 context_save (); /* save the context file */
490 done (status);
491 return 1;
492 }