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