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