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