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