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