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