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