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