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