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