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