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