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