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