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