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