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