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