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