]> diplodocus.org Git - nmh/blob - uip/send.c
Fix stupid accidental dependence on a bash quirk in previous
[nmh] / uip / send.c
1
2 /*
3 * send.c -- send a composed message
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <signal.h>
16
17
18 #ifndef CYRUS_SASL
19 # define SASLminc(a) (a)
20 #else /* CYRUS_SASL */
21 # define SASLminc(a) 0
22 #endif /* CYRUS_SASL */
23
24 static struct swit switches[] = {
25 #define ALIASW 0
26 { "alias aliasfile", 0 },
27 #define DEBUGSW 1
28 { "debug", -5 },
29 #define DRAFTSW 2
30 { "draft", 0 },
31 #define DFOLDSW 3
32 { "draftfolder +folder", 6 },
33 #define DMSGSW 4
34 { "draftmessage msg", 6 },
35 #define NDFLDSW 5
36 { "nodraftfolder", 0 },
37 #define FILTSW 6
38 { "filter filterfile", 0 },
39 #define NFILTSW 7
40 { "nofilter", 0 },
41 #define FRMTSW 8
42 { "format", 0 },
43 #define NFRMTSW 9
44 { "noformat", 0 },
45 #define FORWSW 10
46 { "forward", 0 },
47 #define NFORWSW 11
48 { "noforward", 0 },
49 #define MIMESW 12
50 { "mime", 0 },
51 #define NMIMESW 13
52 { "nomime", 0 },
53 #define MSGDSW 14
54 { "msgid", 0 },
55 #define NMSGDSW 15
56 { "nomsgid", 0 },
57 #define PUSHSW 16
58 { "push", 0 },
59 #define NPUSHSW 17
60 { "nopush", 0 },
61 #define SPLITSW 18
62 { "split seconds", 0 },
63 #define UNIQSW 19
64 { "unique", -6 },
65 #define NUNIQSW 20
66 { "nounique", -8 },
67 #define VERBSW 21
68 { "verbose", 0 },
69 #define NVERBSW 22
70 { "noverbose", 0 },
71 #define WATCSW 23
72 { "watch", 0 },
73 #define NWATCSW 24
74 { "nowatch", 0 },
75 #define WIDTHSW 25
76 { "width columns", 0 },
77 #define VERSIONSW 26
78 { "version", 0 },
79 #define HELPSW 27
80 { "help", 0 },
81 #define BITSTUFFSW 28
82 { "dashstuffing", -12 },
83 #define NBITSTUFFSW 29
84 { "nodashstuffing", -14 },
85 #define MAILSW 30
86 { "mail", -4 },
87 #define SAMLSW 31
88 { "saml", -4 },
89 #define SENDSW 32
90 { "send", -4 },
91 #define SOMLSW 33
92 { "soml", -4 },
93 #define CLIESW 34
94 { "client host", -6 },
95 #define SERVSW 35
96 { "server host", -6 },
97 #define SNOOPSW 36
98 { "snoop", -5 },
99 #define SASLSW 37
100 { "sasl", SASLminc(-4) },
101 #define SASLMECHSW 38
102 { "saslmech", SASLminc(-5) },
103 #define USERSW 39
104 { "user", SASLminc(-4) },
105 #define ATTACHSW 40
106 { "attach", 6 },
107 { NULL, 0 }
108 };
109
110 static struct swit anyl[] = {
111 #define NOSW 0
112 { "no", 0 },
113 #define YESW 1
114 { "yes", 0 },
115 #define LISTDSW 2
116 { "list", 0 },
117 { NULL, 0 }
118 };
119
120 extern int debugsw; /* from sendsbr.c */
121 extern int forwsw;
122 extern int inplace;
123 extern int pushsw;
124 extern int splitsw;
125 extern int unique;
126 extern int verbsw;
127
128 extern char *altmsg; /* .. */
129 extern char *annotext;
130 extern char *distfile;
131
132
133 int
134 main (int argc, char **argv)
135 {
136 int msgp = 0, distsw = 0, vecp = 1;
137 int isdf = 0, mime = 0;
138 int msgnum, status;
139 char *cp, *dfolder = NULL, *maildir = NULL;
140 char buf[BUFSIZ], **ap, **argp, **arguments;
141 char *msgs[MAXARGS], *vec[MAXARGS];
142 struct msgs *mp;
143 struct stat st;
144 char *attach = (char *)0; /* header field name for attachments */
145 #ifdef UCI
146 FILE *fp;
147 #endif /* UCI */
148
149 #ifdef LOCALE
150 setlocale(LC_ALL, "");
151 #endif
152 invo_name = r1bindex (argv[0], '/');
153
154 /* read user profile/context */
155 context_read();
156
157 arguments = getarguments (invo_name, argc, argv, 1);
158 argp = arguments;
159
160 vec[vecp++] = "-library";
161 vec[vecp++] = getcpy (m_maildir (""));
162
163 while ((cp = *argp++)) {
164 if (*cp == '-') {
165 switch (smatch (++cp, switches)) {
166 case AMBIGSW:
167 ambigsw (cp, switches);
168 done (1);
169 case UNKWNSW:
170 adios (NULL, "-%s unknown\n", cp);
171
172 case HELPSW:
173 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
174 print_help (buf, switches, 1);
175 done (1);
176 case VERSIONSW:
177 print_version(invo_name);
178 done (1);
179
180 case DRAFTSW:
181 msgs[msgp++] = draft;
182 continue;
183
184 case DFOLDSW:
185 if (dfolder)
186 adios (NULL, "only one draft folder at a time!");
187 if (!(cp = *argp++) || *cp == '-')
188 adios (NULL, "missing argument to %s", argp[-2]);
189 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
190 *cp != '@' ? TFOLDER : TSUBCWF);
191 continue;
192 case DMSGSW:
193 if (!(cp = *argp++) || *cp == '-')
194 adios (NULL, "missing argument to %s", argp[-2]);
195 msgs[msgp++] = cp;
196 continue;
197 case NDFLDSW:
198 dfolder = NULL;
199 isdf = NOTOK;
200 continue;
201
202 case PUSHSW:
203 pushsw++;
204 continue;
205 case NPUSHSW:
206 pushsw = 0;
207 continue;
208
209 case SPLITSW:
210 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
211 adios (NULL, "missing argument to %s", argp[-2]);
212 continue;
213
214 case UNIQSW:
215 unique++;
216 continue;
217 case NUNIQSW:
218 unique = 0;
219 continue;
220
221 case FORWSW:
222 forwsw++;
223 continue;
224 case NFORWSW:
225 forwsw = 0;
226 continue;
227
228 case VERBSW:
229 verbsw++;
230 vec[vecp++] = --cp;
231 continue;
232 case NVERBSW:
233 verbsw = 0;
234 vec[vecp++] = --cp;
235 continue;
236
237 case MIMESW:
238 mime++;
239 vec[vecp++] = --cp;
240 continue;
241 case NMIMESW:
242 mime = 0;
243 vec[vecp++] = --cp;
244 continue;
245
246 case DEBUGSW:
247 debugsw++; /* fall */
248 case NFILTSW:
249 case FRMTSW:
250 case NFRMTSW:
251 case BITSTUFFSW:
252 case NBITSTUFFSW:
253 case MSGDSW:
254 case NMSGDSW:
255 case WATCSW:
256 case NWATCSW:
257 case MAILSW:
258 case SAMLSW:
259 case SENDSW:
260 case SOMLSW:
261 case SNOOPSW:
262 case SASLSW:
263 vec[vecp++] = --cp;
264 continue;
265
266 case ALIASW:
267 case FILTSW:
268 case WIDTHSW:
269 case CLIESW:
270 case SERVSW:
271 case SASLMECHSW:
272 case USERSW:
273 vec[vecp++] = --cp;
274 if (!(cp = *argp++) || *cp == '-')
275 adios (NULL, "missing argument to %s", argp[-2]);
276 vec[vecp++] = cp;
277 continue;
278
279 case ATTACHSW:
280 if (!(attach = *argp++) || *attach == '-')
281 adios (NULL, "missing argument to %s", argp[-2]);
282 continue;
283 }
284 } else {
285 msgs[msgp++] = cp;
286 }
287 }
288
289 /*
290 * check for "Aliasfile:" profile entry
291 */
292 if ((cp = context_find ("Aliasfile"))) {
293 char *dp = NULL;
294
295 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
296 vec[vecp++] = "-alias";
297 vec[vecp++] = *ap;
298 }
299 }
300
301 if (dfolder == NULL) {
302 if (msgp == 0) {
303 #ifdef WHATNOW
304 if ((cp = getenv ("mhdraft")) && *cp) {
305 msgs[msgp++] = cp;
306 goto go_to_it;
307 }
308 #endif /* WHATNOW */
309 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
310 if (stat (msgs[0], &st) == NOTOK)
311 adios (msgs[0], "unable to stat draft file");
312 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
313 for (status = LISTDSW; status != YESW;) {
314 if (!(argp = getans (cp, anyl)))
315 done (1);
316 switch (status = smatch (*argp, anyl)) {
317 case NOSW:
318 done (0);
319 case YESW:
320 break;
321 case LISTDSW:
322 showfile (++argp, msgs[0]);
323 break;
324 default:
325 advise (NULL, "say what?");
326 break;
327 }
328 }
329 } else {
330 for (msgnum = 0; msgnum < msgp; msgnum++)
331 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
332 }
333 } else {
334 if (!context_find ("path"))
335 free (path ("./", TFOLDER));
336
337 if (!msgp)
338 msgs[msgp++] = "cur";
339 maildir = m_maildir (dfolder);
340
341 if (chdir (maildir) == NOTOK)
342 adios (maildir, "unable to change directory to");
343
344 /* read folder and create message structure */
345 if (!(mp = folder_read (dfolder)))
346 adios (NULL, "unable to read folder %s", dfolder);
347
348 /* check for empty folder */
349 if (mp->nummsg == 0)
350 adios (NULL, "no messages in %s", dfolder);
351
352 /* parse all the message ranges/sequences and set SELECTED */
353 for (msgnum = 0; msgnum < msgp; msgnum++)
354 if (!m_convert (mp, msgs[msgnum]))
355 done (1);
356 seq_setprev (mp); /* set the previous-sequence */
357
358 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
359 if (is_selected (mp, msgnum)) {
360 msgs[msgp++] = getcpy (m_name (msgnum));
361 unset_exists (mp, msgnum);
362 }
363 }
364
365 mp->msgflags |= SEQMOD;
366 seq_save (mp);
367 }
368
369 #ifdef WHATNOW
370 go_to_it:
371 #endif /* WHATNOW */
372
373 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
374 if ((cp = context_find ("signature")) && *cp)
375 m_putenv ("SIGNATURE", cp);
376 #ifdef UCI
377 else {
378 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
379 if ((fp = fopen (buf, "r")) != NULL
380 && fgets (buf, sizeof buf, fp) != NULL) {
381 fclose (fp);
382 if (cp = strchr (buf, '\n'))
383 *cp = 0;
384 m_putenv ("SIGNATURE", buf);
385 }
386 }
387 #endif /* UCI */
388
389 for (msgnum = 0; msgnum < msgp; msgnum++)
390 if (stat (msgs[msgnum], &st) == NOTOK)
391 adios (msgs[msgnum], "unable to stat draft file");
392
393 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
394 annotext = NULL;
395 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
396 inplace = atoi (cp);
397 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
398 altmsg = NULL; /* used by dist interface - see below */
399
400 if ((cp = getenv ("mhdist"))
401 && *cp
402 && (distsw = atoi (cp))
403 && altmsg) {
404 vec[vecp++] = "-dist";
405 distfile = getcpy (m_scratch (altmsg, invo_name));
406 if (link (altmsg, distfile) == NOTOK) {
407 if (errno != EXDEV
408 #ifdef EISREMOTE
409 && errno != EISREMOTE
410 #endif /* EISREMOTE */
411 )
412 adios (distfile, "unable to link %s to", altmsg);
413 free (distfile);
414 distfile = getcpy (m_tmpfil (invo_name));
415 {
416 int in, out;
417 struct stat st;
418
419 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
420 adios (altmsg, "unable to open");
421 fstat(in, &st);
422 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
423 adios (distfile, "unable to write");
424 cpydata (in, out, altmsg, distfile);
425 close (in);
426 close (out);
427 }
428 }
429 } else {
430 distfile = NULL;
431 }
432
433 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
434 st.st_mtime = 0;
435 st.st_dev = 0;
436 st.st_ino = 0;
437 }
438 if (pushsw)
439 push ();
440
441 status = 0;
442 vec[0] = r1bindex (postproc, '/');
443 closefds (3);
444
445 for (msgnum = 0; msgnum < msgp; msgnum++) {
446 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach)) {
447 case DONE:
448 done (++status);
449 case NOTOK:
450 status++; /* fall */
451 case OK:
452 break;
453 }
454 }
455
456 context_save (); /* save the context file */
457 return done (status);
458 }