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