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