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