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