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