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