29ca1b77d0dad824ad6cad078031dc66b3281362
[mmh] / uip / comp.c
1
2 /*
3  * comp.c -- compose a 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 <h/utils.h>
12 #include <h/fmt_scan.h>
13 #include <fcntl.h>
14
15 static struct swit switches[] = {
16 #define DFOLDSW                0
17     { "draftfolder +folder", 0 },
18 #define DMSGSW                 1
19     { "draftmessage msg", 0 },
20 #define NDFLDSW                2
21     { "nodraftfolder", 0 },
22 #define EDITRSW                3
23     { "editor editor", 0 },
24 #define NEDITSW                4
25     { "noedit", 0 },
26 #define FILESW                 5
27     { "file file", 0 },
28 #define FORMSW                 6
29     { "form formfile", 0 },
30 #define USESW                  7
31     { "use", 0 },
32 #define NUSESW                 8
33     { "nouse", 0 },
34 #define WHATSW                 9
35     { "whatnowproc program", 0 },
36 #define NWHATSW               10
37     { "nowhatnowproc", 0 },
38 #define VERSIONSW             11
39     { "version", 0 },
40 #define HELPSW                12
41     { "help", 0 },
42 #define TOSW                  13
43     { "to address", 0 },
44 #define CCSW                  14
45     { "cc address", 0 },
46 #define FROMSW                15
47     { "from address", 0 },
48 #define FCCSW                 16
49     { "fcc mailbox", 0 },
50 #define WIDTHSW               17
51     { "width colums", 0 },
52 #define SUBJECTSW             18
53     { "subject text", 0 },
54     { NULL, 0 }
55 };
56
57 static struct swit aqrunl[] = {
58 #define NOSW          0
59     { "quit", 0 },
60 #define YESW          1
61     { "replace", 0 },
62 #define USELSW        2
63     { "use", 0 },
64 #define LISTDSW       3
65     { "list", 0 },
66 #define REFILSW       4
67     { "refile +folder", 0 },
68 #define NEWSW         5
69     { "new", 0 },
70     { NULL, 0 }
71 };
72
73 static struct swit aqrul[] = {
74     { "quit", 0 },
75     { "replace", 0 },
76     { "use", 0 },
77     { "list", 0 },
78     { "refile", 0 },
79     { NULL, 0 }
80 };
81
82 int
83 main (int argc, char **argv)
84 {
85     int use = NOUSE, nedit = 0, nwhat = 0;
86     int i, in = NOTOK, isdf = 0, out, dat[5], format_len = 0;
87     int outputlinelen = OUTPUTLINELEN;
88     char *cp, *cwd, *maildir, *dfolder = NULL;
89     char *ed = NULL, *file = NULL, *form = NULL;
90     char *folder = NULL, *msg = NULL, buf[BUFSIZ];
91     char *to = NULL, *from = NULL, *cc = NULL, *fcc = NULL, *dp;
92     char *subject = NULL;
93     char drft[BUFSIZ], **argp, **arguments;
94     struct msgs *mp = NULL;
95     struct format *fmt;
96     struct stat st;
97
98 #ifdef LOCALE
99     setlocale(LC_ALL, "");
100 #endif
101     invo_name = r1bindex (argv[0], '/');
102
103     /* read user profile/context */
104     context_read();
105
106     arguments = getarguments (invo_name, argc, argv, 1);
107     argp = arguments;
108
109     while ((cp = *argp++)) {
110         if (*cp == '-') {
111             switch (smatch (++cp, switches)) {
112                 case AMBIGSW: 
113                     ambigsw (cp, switches);
114                     done (1);
115                 case UNKWNSW: 
116                     adios (NULL, "-%s unknown", cp);
117
118                 case HELPSW: 
119                     snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
120                         invo_name);
121                     print_help (buf, switches, 1);
122                     done (0);
123                 case VERSIONSW:
124                     print_version(invo_name);
125                     done (0);
126
127                 case EDITRSW: 
128                     if (!(ed = *argp++) || *ed == '-')
129                         adios (NULL, "missing argument to %s", argp[-2]);
130                     nedit = 0;
131                     continue;
132                 case NEDITSW: 
133                     nedit++;
134                     continue;
135
136                 case WHATSW: 
137                     if (!(whatnowproc = *argp++) || *whatnowproc == '-')
138                         adios (NULL, "missing argument to %s", argp[-2]);
139                     nwhat = 0;
140                     continue;
141                 case NWHATSW: 
142                     nwhat++;
143                     continue;
144
145                 case FORMSW: 
146                     if (!(form = *argp++) || *form == '-')
147                         adios (NULL, "missing argument to %s", argp[-2]);
148                     continue;
149
150                 case USESW: 
151                     use++;
152                     continue;
153                 case NUSESW: 
154                     use = NOUSE;
155                     continue;
156
157                 case FILESW:    /* compatibility */
158                     if (file)
159                         adios (NULL, "only one file at a time!");
160                     if (!(file = *argp++) || *file == '-')
161                         adios (NULL, "missing argument to %s", argp[-2]);
162                     isdf = NOTOK;
163                     continue;
164
165                 case DFOLDSW: 
166                     if (dfolder)
167                         adios (NULL, "only one draft folder at a time!");
168                     if (!(cp = *argp++) || *cp == '-')
169                         adios (NULL, "missing argument to %s", argp[-2]);
170                     dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
171                             *cp != '@' ? TFOLDER : TSUBCWF);
172                     continue;
173                 case DMSGSW: 
174                     if (file)
175                         adios (NULL, "only one draft message at a time!");
176                     if (!(file = *argp++) || *file == '-')
177                         adios (NULL, "missing argument to %s", argp[-2]);
178                     continue;
179                 case NDFLDSW: 
180                     dfolder = NULL;
181                     isdf = NOTOK;
182                     continue;
183
184                 case TOSW:
185                     if (!(cp = *argp++) || *cp == '-')
186                         adios (NULL, "missing argument to %s", argp[-2]);
187                     to = addlist(to, cp);
188                     continue;
189
190                 case CCSW:
191                     if (!(cp = *argp++) || *cp == '-')
192                         adios (NULL, "missing argument to %s", argp[-2]);
193                     cc = addlist(cc, cp);
194                     continue;
195
196                 case FROMSW:
197                     if (!(cp = *argp++) || *cp == '-')
198                         adios (NULL, "missing argument to %s", argp[-2]);
199                     from = addlist(from, cp);
200                     continue;
201
202                 case FCCSW:
203                     if (!(cp = *argp++) || *cp == '-')
204                         adios (NULL, "missing argument to %s", argp[-2]);
205                     dp = NULL;
206                     if (*cp == '@')
207                         cp = dp = path(cp + 1, TSUBCWF);
208                     fcc = addlist(fcc, cp);
209                     if (dp)
210                         free(dp);
211                     continue;
212
213                 case WIDTHSW:
214                     if (!(cp = *argp++) || *cp == '-')
215                         adios (NULL, "missing argument to %s", argp[-2]);
216                     if ((outputlinelen = atoi(cp)) < 10)
217                         adios (NULL, "impossible width %d", outputlinelen);
218                     continue;
219
220                 case SUBJECTSW:
221                     if (!(cp = *argp++) || *cp == '-')
222                         adios (NULL, "missing argument to %s", argp[-2]);
223                     subject = cp;
224                     continue;
225             }
226         }
227         if (*cp == '+' || *cp == '@') {
228             if (folder)
229                 adios (NULL, "only one folder at a time!");
230             else
231                 folder = pluspath (cp);
232         } else {
233             if (msg)
234                 adios (NULL, "only one message at a time!");
235             else
236                 msg = cp;
237         }
238     }
239
240     cwd = getcpy (pwd ());
241
242     if (!context_find ("path"))
243         free (path ("./", TFOLDER));
244
245     /*
246      * Check if we are using a draft folder
247      * and have specified a message in it.
248      */
249     if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
250         file = msg;
251         msg = NULL;
252     }
253     if (form && (folder || msg))
254             adios (NULL, "can't mix forms and folders/msgs");
255
256     cp = NULL;
257
258     if (folder || msg) {
259         /*
260          * Use a message as the "form" for the new message.
261          */
262         if (!msg)
263             msg = "cur";
264         if (!folder)
265             folder = getfolder (1);
266         maildir = m_maildir (folder);
267
268         if (chdir (maildir) == NOTOK)
269             adios (maildir, "unable to change directory to");
270
271         /* read folder and create message structure */
272         if (!(mp = folder_read (folder)))
273             adios (NULL, "unable to read folder %s", folder);
274
275         /* check for empty folder */
276         if (mp->nummsg == 0)
277             adios (NULL, "no messages in %s", folder);
278
279         /* parse the message range/sequence/name and set SELECTED */
280         if (!m_convert (mp, msg))
281             done (1);
282         seq_setprev (mp);       /* set the previous-sequence */
283
284         if (mp->numsel > 1)
285             adios (NULL, "only one message at a time!");
286
287         if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
288             adios (form, "unable to open message");
289     } else {
290         struct comp *cptr;
291
292         if (! form)
293             form = components;
294
295         cp = new_fs(form, NULL, NULL);
296         format_len = strlen(cp);
297         fmt_compile(cp, &fmt);
298
299         /*
300          * Set up any components that were fed to us on the command line
301          */
302
303         if (from) {
304             FINDCOMP(cptr, "from");
305             if (cptr)
306                 cptr->c_text = from;
307         }
308         if (to) {
309             FINDCOMP(cptr, "to");
310             if (cptr)
311                 cptr->c_text = to;
312         }
313         if (cc) {
314             FINDCOMP(cptr, "cc");
315             if (cptr)
316                 cptr->c_text = cc;
317         }
318         if (fcc) {
319             FINDCOMP(cptr, "fcc");
320             if (cptr)
321                 cptr->c_text = fcc;
322         }
323         if (subject) {
324             FINDCOMP(cptr, "subject");
325             if (cptr)
326                 cptr->c_text = subject;
327         }
328     }
329
330 try_it_again:
331     strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
332
333     /*
334      * Check if we have an existing draft
335      */
336     if ((out = open (drft, O_RDONLY)) != NOTOK) {
337         i = fdcompare (in, out);
338         close (out);
339
340         /*
341          * If we have given -use flag, or if the
342          * draft is just the same as the components
343          * file, then no need to ask any questions.
344          */
345         if (use || i)
346             goto edit_it;
347
348         if (stat (drft, &st) == NOTOK)
349             adios (drft, "unable to stat");
350         printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
351         for (i = LISTDSW; i != YESW;) {
352             if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
353                 done (1);
354             switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
355                 case NOSW: 
356                     done (0);
357                 case NEWSW: 
358                     file = NULL;
359                     use = NOUSE;
360                     goto try_it_again;
361                 case YESW: 
362                     break;
363                 case USELSW:
364                     use++;
365                     goto edit_it;
366                 case LISTDSW: 
367                     showfile (++argp, drft);
368                     break;
369                 case REFILSW: 
370                     if (refile (++argp, drft) == 0)
371                         i = YESW;
372                     break;
373                 default: 
374                     advise (NULL, "say what?");
375                     break;
376             }
377         }
378     } else {
379         if (use)
380             adios (drft, "unable to open");
381     }
382
383     if ((out = creat (drft, m_gmprot ())) == NOTOK)
384         adios (drft, "unable to create");
385     if (cp) {
386         char *scanl;
387
388         i = format_len + 1024;
389         scanl = mh_xmalloc((size_t) i + 2);
390         dat[0] = 0;
391         dat[1] = 0;
392         dat[2] = 0;
393         dat[3] = outputlinelen;
394         dat[4] = 0;
395         fmt_scan(fmt, scanl, i, dat);
396         write(out, scanl, strlen(scanl));
397         free(scanl);
398     } else {
399         cpydata (in, out, form, drft);
400         close (in);
401     }
402     close (out);
403
404 edit_it:
405     context_save ();    /* save the context file */
406
407     if (nwhat)
408         done (0);
409     what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd, 0);
410     done (1);
411     return 1;
412 }