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