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