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