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