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