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