]> diplodocus.org Git - nmh/blob - uip/send.c
Added const to argument of getname().
[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 while ((cp = *argp++)) {
150 if (*cp == '-') {
151 switch (smatch (++cp, switches)) {
152 case AMBIGSW:
153 ambigsw (cp, switches);
154 done (1);
155 case UNKWNSW:
156 adios (NULL, "-%s unknown\n", cp);
157
158 case HELPSW:
159 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
160 print_help (buf, switches, 1);
161 done (0);
162 case VERSIONSW:
163 print_version(invo_name);
164 done (0);
165
166 case DRAFTSW:
167 msgs[msgp++] = draft;
168 continue;
169
170 case DFOLDSW:
171 if (dfolder)
172 adios (NULL, "only one draft folder at a time!");
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
176 *cp != '@' ? TFOLDER : TSUBCWF);
177 continue;
178 case DMSGSW:
179 if (!(cp = *argp++) || *cp == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
181 msgs[msgp++] = cp;
182 continue;
183 case NDFLDSW:
184 dfolder = NULL;
185 isdf = NOTOK;
186 continue;
187
188 case PUSHSW:
189 pushsw++;
190 continue;
191 case NPUSHSW:
192 pushsw = 0;
193 continue;
194
195 case SPLITSW:
196 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
197 adios (NULL, "missing argument to %s", argp[-2]);
198 continue;
199
200 case UNIQSW:
201 unique++;
202 continue;
203 case NUNIQSW:
204 unique = 0;
205 continue;
206
207 case FORWSW:
208 forwsw++;
209 continue;
210 case NFORWSW:
211 forwsw = 0;
212 continue;
213
214 case VERBSW:
215 verbsw++;
216 vec[vecp++] = --cp;
217 continue;
218 case NVERBSW:
219 verbsw = 0;
220 vec[vecp++] = --cp;
221 continue;
222
223 case MIMESW:
224 mime++;
225 vec[vecp++] = --cp;
226 continue;
227 case NMIMESW:
228 mime = 0;
229 vec[vecp++] = --cp;
230 continue;
231
232 case DEBUGSW:
233 debugsw++; /* fall */
234 case NFILTSW:
235 case FRMTSW:
236 case NFRMTSW:
237 case BITSTUFFSW:
238 case NBITSTUFFSW:
239 case MSGDSW:
240 case NMSGDSW:
241 case WATCSW:
242 case NWATCSW:
243 case SNOOPSW:
244 case SASLSW:
245 case NOSASLSW:
246 case TLSSW:
247 case INITTLSSW:
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 }