3 * comp.c -- compose a message
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.
12 #include <h/fmt_scan.h>
15 static struct swit switches[] = {
17 { "draftfolder +folder", 0 },
19 { "draftmessage msg", 0 },
21 { "nodraftfolder", 0 },
23 { "editor editor", 0 },
29 { "form formfile", 0 },
35 { "whatnowproc program", 0 },
37 { "nowhatnowproc", 0 },
47 { "from address", 0 },
51 { "width colums", 0 },
55 static struct swit aqrunl[] = {
65 { "refile +folder", 0 },
71 static struct swit aqrul[] = {
81 * Add an item to a comma seperated list
84 static char *addlist(char *, char *);
87 main (int argc, char **argv)
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;
102 setlocale(LC_ALL, "");
104 invo_name = r1bindex (argv[0], '/');
106 /* read user profile/context */
109 arguments = getarguments (invo_name, argc, argv, 1);
112 while ((cp = *argp++)) {
114 switch (smatch (++cp, switches)) {
116 ambigsw (cp, switches);
119 adios (NULL, "-%s unknown", cp);
122 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
124 print_help (buf, switches, 1);
127 print_version(invo_name);
131 if (!(ed = *argp++) || *ed == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
140 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
149 if (!(form = *argp++) || *form == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
160 case FILESW: /* compatibility */
162 adios (NULL, "only one file at a time!");
163 if (!(file = *argp++) || *file == '-')
164 adios (NULL, "missing argument to %s", argp[-2]);
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);
178 adios (NULL, "only one draft message at a time!");
179 if (!(file = *argp++) || *file == '-')
180 adios (NULL, "missing argument to %s", argp[-2]);
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
190 to = addlist(to, cp);
194 if (!(cp = *argp++) || *cp == '-')
195 adios (NULL, "missing argument to %s", argp[-2]);
196 cc = addlist(cc, cp);
200 if (!(cp = *argp++) || *cp == '-')
201 adios (NULL, "missing argument to %s", argp[-2]);
202 from = addlist(from, cp);
206 if (!(cp = *argp++) || *cp == '-')
207 adios (NULL, "missing argument to %s", argp[-2]);
210 cp = dp = path(cp + 1, TSUBCWF);
211 fcc = addlist(fcc, cp);
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);
224 if (*cp == '+' || *cp == '@') {
226 adios (NULL, "only one folder at a time!");
228 folder = pluspath (cp);
231 adios (NULL, "only one message at a time!");
237 cwd = getcpy (pwd ());
239 if (!context_find ("path"))
240 free (path ("./", TFOLDER));
243 * Check if we are using a draft folder
244 * and have specified a message in it.
246 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
250 if (form && (folder || msg))
251 adios (NULL, "can't mix forms and folders/msgs");
257 * Use a message as the "form" for the new message.
262 folder = getfolder (1);
263 maildir = m_maildir (folder);
265 if (chdir (maildir) == NOTOK)
266 adios (maildir, "unable to change directory to");
268 /* read folder and create message structure */
269 if (!(mp = folder_read (folder)))
270 adios (NULL, "unable to read folder %s", folder);
272 /* check for empty folder */
274 adios (NULL, "no messages in %s", folder);
276 /* parse the message range/sequence/name and set SELECTED */
277 if (!m_convert (mp, msg))
279 seq_setprev (mp); /* set the previous-sequence */
282 adios (NULL, "only one message at a time!");
284 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
285 adios (form, "unable to open message");
292 cp = new_fs(form, NULL, NULL);
293 format_len = strlen(cp);
294 ncomps = fmt_compile(cp, &fmt);
297 * Set up any components that were fed to us on the command line
301 FINDCOMP(cptr, "from");
306 FINDCOMP(cptr, "to");
311 FINDCOMP(cptr, "cc");
316 FINDCOMP(cptr, "fcc");
323 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
326 * Check if we have an existing draft
328 if ((out = open (drft, O_RDONLY)) != NOTOK) {
329 i = fdcompare (in, out);
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.
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)))
346 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
359 showfile (++argp, drft);
362 if (refile (++argp, drft) == 0)
366 advise (NULL, "say what?");
372 adios (drft, "unable to open");
375 if ((out = creat (drft, m_gmprot ())) == NOTOK)
376 adios (drft, "unable to create");
380 i = format_len + 1024;
381 scanl = mh_xmalloc((size_t) i + 2);
385 dat[3] = outputlinelen;
387 fmt_scan(fmt, scanl, i, dat);
388 write(out, scanl, strlen(scanl));
391 cpydata (in, out, form, drft);
397 context_save (); /* save the context file */
401 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);
407 * Append an item to a comma separated list
411 addlist (char *list, char *item)
414 list = add (", ", list);
416 return add (item, list);