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