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