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