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