]> diplodocus.org Git - nmh/blob - uip/send.c
Removed temporary probes added in commit
[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", 0, 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("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
73 X("messageid localname|random", 2, MESSAGEIDSW) \
74
75 #define X(sw, minchars, id) id,
76 DEFINE_SWITCH_ENUM(SEND);
77 #undef X
78
79 #define X(sw, minchars, id) { sw, minchars, id },
80 DEFINE_SWITCH_ARRAY(SEND, switches);
81 #undef X
82
83 #define USE_SWITCHES \
84 X("no", 0, NOSW) \
85 X("yes", 0, YESW) \
86 X("list", 0, LISTDSW) \
87
88 #define X(sw, minchars, id) id,
89 DEFINE_SWITCH_ENUM(USE);
90 #undef X
91
92 #define X(sw, minchars, id) { sw, minchars, id },
93 DEFINE_SWITCH_ARRAY(USE, anyl);
94 #undef X
95
96 extern int debugsw; /* from sendsbr.c */
97 extern int forwsw;
98 extern int inplace;
99 extern int pushsw;
100 extern int splitsw;
101 extern int unique;
102 extern int verbsw;
103
104 extern char *altmsg; /* .. */
105 extern char *annotext;
106 extern char *distfile;
107
108
109 int
110 main (int argc, char **argv)
111 {
112 int msgp = 0, distsw = 0, vecp;
113 int isdf = 0, mime = 0;
114 int msgnum, status;
115 char *cp, *dfolder = NULL, *maildir = NULL;
116 char buf[BUFSIZ], **ap, **argp, **arguments, *program;
117 char *msgs[MAXARGS], **vec;
118 struct msgs *mp;
119 struct stat st;
120 char *attach = NMH_ATTACH_HEADER; /* header field name for attachments */
121 int attachformat = 1; /* mhbuild format specifier for attachments */
122
123 #ifdef LOCALE
124 setlocale(LC_ALL, "");
125 #endif
126 invo_name = r1bindex (argv[0], '/');
127
128 /* read user profile/context */
129 context_read();
130
131 arguments = getarguments (invo_name, argc, argv, 1);
132 argp = arguments;
133
134 vec = argsplit(postproc, &program, &vecp);
135
136 vec[vecp++] = "-library";
137 vec[vecp++] = getcpy (m_maildir (""));
138
139 if ((cp = context_find ("fileproc"))) {
140 vec[vecp++] = "-fileproc";
141 vec[vecp++] = cp;
142 }
143
144 if ((cp = context_find ("mhlproc"))) {
145 vec[vecp++] = "-mhlproc";
146 vec[vecp++] = cp;
147 }
148
149 if ((cp = context_find ("credentials"))) {
150 /* post doesn't read context so need to pass credentials. */
151 vec[vecp++] = "-credentials";
152 vec[vecp++] = cp;
153 }
154
155 while ((cp = *argp++)) {
156 if (*cp == '-') {
157 switch (smatch (++cp, switches)) {
158 case AMBIGSW:
159 ambigsw (cp, switches);
160 done (1);
161 case UNKWNSW:
162 adios (NULL, "-%s unknown\n", cp);
163
164 case HELPSW:
165 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
166 print_help (buf, switches, 1);
167 done (0);
168 case VERSIONSW:
169 print_version(invo_name);
170 done (0);
171
172 case DRAFTSW:
173 msgs[msgp++] = draft;
174 continue;
175
176 case DFOLDSW:
177 if (dfolder)
178 adios (NULL, "only one draft folder at a time!");
179 if (!(cp = *argp++) || *cp == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
181 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
182 *cp != '@' ? TFOLDER : TSUBCWF);
183 continue;
184 case DMSGSW:
185 if (!(cp = *argp++) || *cp == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 msgs[msgp++] = cp;
188 continue;
189 case NDFLDSW:
190 dfolder = NULL;
191 isdf = NOTOK;
192 continue;
193
194 case PUSHSW:
195 pushsw++;
196 continue;
197 case NPUSHSW:
198 pushsw = 0;
199 continue;
200
201 case SPLITSW:
202 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
203 adios (NULL, "missing argument to %s", argp[-2]);
204 continue;
205
206 case UNIQSW:
207 unique++;
208 continue;
209 case NUNIQSW:
210 unique = 0;
211 continue;
212
213 case FORWSW:
214 forwsw++;
215 continue;
216 case NFORWSW:
217 forwsw = 0;
218 continue;
219
220 case VERBSW:
221 verbsw++;
222 vec[vecp++] = --cp;
223 continue;
224 case NVERBSW:
225 verbsw = 0;
226 vec[vecp++] = --cp;
227 continue;
228
229 case MIMESW:
230 mime++;
231 vec[vecp++] = --cp;
232 continue;
233 case NMIMESW:
234 mime = 0;
235 vec[vecp++] = --cp;
236 continue;
237
238 case DEBUGSW:
239 debugsw++; /* fall */
240 case NFILTSW:
241 case FRMTSW:
242 case NFRMTSW:
243 case BITSTUFFSW:
244 case NBITSTUFFSW:
245 case MSGDSW:
246 case NMSGDSW:
247 case WATCSW:
248 case NWATCSW:
249 case SNOOPSW:
250 case SASLSW:
251 case NOSASLSW:
252 case TLSSW:
253 case INITTLSSW:
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, 1)))
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 closefds (3);
447
448 for (msgnum = 0; msgnum < msgp; msgnum++) {
449 switch (sendsbr (vec, vecp, program, msgs[msgnum], &st, 1, attach,
450 attachformat)) {
451 case DONE:
452 done (++status);
453 case NOTOK:
454 status++; /* fall */
455 case OK:
456 break;
457 }
458 }
459
460 context_save (); /* save the context file */
461 done (status);
462 return 1;
463 }