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