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