]> diplodocus.org Git - nmh/blob - uip/comp.c
pending-release-notes: add mhshow's "-prefer", and mh-format's %(kibi/kilo)
[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 colums", 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 if (dp)
196 free(dp);
197 continue;
198
199 case WIDTHSW:
200 if (!(cp = *argp++) || *cp == '-')
201 adios (NULL, "missing argument to %s", argp[-2]);
202 if ((outputlinelen = atoi(cp)) < 10)
203 adios (NULL, "impossible width %d", outputlinelen);
204 continue;
205
206 case SUBJECTSW:
207 if (!(cp = *argp++) || *cp == '-')
208 adios (NULL, "missing argument to %s", argp[-2]);
209 subject = cp;
210 continue;
211 }
212 }
213 if (*cp == '+' || *cp == '@') {
214 if (folder)
215 adios (NULL, "only one folder at a time!");
216 else
217 folder = pluspath (cp);
218 } else {
219 if (msg)
220 adios (NULL, "only one message at a time!");
221 else
222 msg = cp;
223 }
224 }
225
226 cwd = getcpy (pwd ());
227
228 if (!context_find ("path"))
229 free (path ("./", TFOLDER));
230
231 /*
232 * Check if we are using a draft folder
233 * and have specified a message in it.
234 */
235 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
236 file = msg;
237 msg = NULL;
238 }
239 if (form && (folder || msg))
240 adios (NULL, "can't mix forms and folders/msgs");
241
242 cp = NULL;
243
244 if (folder || msg) {
245 /*
246 * Use a message as the "form" for the new message.
247 */
248 if (!msg)
249 msg = "cur";
250 if (!folder)
251 folder = getfolder (1);
252 maildir = m_maildir (folder);
253
254 if (chdir (maildir) == NOTOK)
255 adios (maildir, "unable to change directory to");
256
257 /* read folder and create message structure */
258 if (!(mp = folder_read (folder, 1)))
259 adios (NULL, "unable to read folder %s", folder);
260
261 /* check for empty folder */
262 if (mp->nummsg == 0)
263 adios (NULL, "no messages in %s", folder);
264
265 /* parse the message range/sequence/name and set SELECTED */
266 if (!m_convert (mp, msg))
267 done (1);
268 seq_setprev (mp); /* set the previous-sequence */
269 seq_save (mp);
270
271 if (mp->numsel > 1)
272 adios (NULL, "only one message at a time!");
273
274 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
275 adios (form, "unable to open message");
276 } else {
277 struct comp *cptr;
278
279 if (! form)
280 form = components;
281
282 cp = new_fs(form, NULL, NULL);
283 format_len = strlen(cp);
284 fmt_compile(cp, &fmt, 1);
285
286 /*
287 * Set up any components that were fed to us on the command line
288 */
289
290 if (from) {
291 cptr = fmt_findcomp("from");
292 if (cptr)
293 cptr->c_text = from;
294 }
295 if (to) {
296 cptr = fmt_findcomp("to");
297 if (cptr)
298 cptr->c_text = to;
299 }
300 if (cc) {
301 cptr = fmt_findcomp("cc");
302 if (cptr)
303 cptr->c_text = cc;
304 }
305 if (fcc) {
306 cptr = fmt_findcomp("fcc");
307 if (cptr)
308 cptr->c_text = fcc;
309 }
310 if (subject) {
311 cptr = fmt_findcomp("subject");
312 if (cptr)
313 cptr->c_text = subject;
314 }
315 }
316
317 try_it_again:
318 strncpy (drft, build ? m_maildir ("draft")
319 : m_draft (dfolder, file, use, &isdf), sizeof(drft));
320
321 /*
322 * Check if we have an existing draft
323 */
324 if (!build && (out = open (drft, O_RDONLY)) != NOTOK) {
325 i = fdcompare (in, out);
326 close (out);
327
328 /*
329 * If we have given -use flag, or if the
330 * draft is just the same as the components
331 * file, then no need to ask any questions.
332 */
333 if (use || i)
334 goto edit_it;
335
336 if (stat (drft, &st) == NOTOK)
337 adios (drft, "unable to stat");
338 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
339 for (i = LISTDSW; i != YESW;) {
340 if (!(argp = getans ("\nDisposition? ", 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 }