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