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