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