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