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