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