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