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