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 },
53 { "subject text", 0 },
57 static struct swit aqrunl[] = {
67 { "refile +folder", 0 },
73 static struct swit aqrul[] = {
83 * Add an item to a comma seperated list
86 static char *addlist(char *, char *);
89 main (int argc, char **argv)
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;
99 char drft[BUFSIZ], **argp, **arguments;
100 struct msgs *mp = NULL;
105 setlocale(LC_ALL, "");
107 invo_name = r1bindex (argv[0], '/');
109 /* read user profile/context */
112 arguments = getarguments (invo_name, argc, argv, 1);
115 while ((cp = *argp++)) {
117 switch (smatch (++cp, switches)) {
119 ambigsw (cp, switches);
122 adios (NULL, "-%s unknown", cp);
125 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
127 print_help (buf, switches, 1);
130 print_version(invo_name);
134 if (!(ed = *argp++) || *ed == '-')
135 adios (NULL, "missing argument to %s", argp[-2]);
143 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
152 if (!(form = *argp++) || *form == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
163 case FILESW: /* compatibility */
165 adios (NULL, "only one file at a time!");
166 if (!(file = *argp++) || *file == '-')
167 adios (NULL, "missing argument to %s", argp[-2]);
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);
181 adios (NULL, "only one draft message at a time!");
182 if (!(file = *argp++) || *file == '-')
183 adios (NULL, "missing argument to %s", argp[-2]);
191 if (!(cp = *argp++) || *cp == '-')
192 adios (NULL, "missing argument to %s", argp[-2]);
193 to = addlist(to, cp);
197 if (!(cp = *argp++) || *cp == '-')
198 adios (NULL, "missing argument to %s", argp[-2]);
199 cc = addlist(cc, cp);
203 if (!(cp = *argp++) || *cp == '-')
204 adios (NULL, "missing argument to %s", argp[-2]);
205 from = addlist(from, cp);
209 if (!(cp = *argp++) || *cp == '-')
210 adios (NULL, "missing argument to %s", argp[-2]);
213 cp = dp = path(cp + 1, TSUBCWF);
214 fcc = addlist(fcc, cp);
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);
227 if (!(cp = *argp++) || *cp == '-')
228 adios (NULL, "missing argument to %s", argp[-2]);
233 if (*cp == '+' || *cp == '@') {
235 adios (NULL, "only one folder at a time!");
237 folder = pluspath (cp);
240 adios (NULL, "only one message at a time!");
246 cwd = getcpy (pwd ());
248 if (!context_find ("path"))
249 free (path ("./", TFOLDER));
252 * Check if we are using a draft folder
253 * and have specified a message in it.
255 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
259 if (form && (folder || msg))
260 adios (NULL, "can't mix forms and folders/msgs");
266 * Use a message as the "form" for the new message.
271 folder = getfolder (1);
272 maildir = m_maildir (folder);
274 if (chdir (maildir) == NOTOK)
275 adios (maildir, "unable to change directory to");
277 /* read folder and create message structure */
278 if (!(mp = folder_read (folder)))
279 adios (NULL, "unable to read folder %s", folder);
281 /* check for empty folder */
283 adios (NULL, "no messages in %s", folder);
285 /* parse the message range/sequence/name and set SELECTED */
286 if (!m_convert (mp, msg))
288 seq_setprev (mp); /* set the previous-sequence */
291 adios (NULL, "only one message at a time!");
293 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
294 adios (form, "unable to open message");
301 cp = new_fs(form, NULL, NULL);
302 format_len = strlen(cp);
303 fmt_compile(cp, &fmt);
306 * Set up any components that were fed to us on the command line
310 FINDCOMP(cptr, "from");
315 FINDCOMP(cptr, "to");
320 FINDCOMP(cptr, "cc");
325 FINDCOMP(cptr, "fcc");
330 FINDCOMP(cptr, "subject");
332 cptr->c_text = subject;
337 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
340 * Check if we have an existing draft
342 if ((out = open (drft, O_RDONLY)) != NOTOK) {
343 i = fdcompare (in, out);
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.
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)))
360 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
373 showfile (++argp, drft);
376 if (refile (++argp, drft) == 0)
380 advise (NULL, "say what?");
386 adios (drft, "unable to open");
389 if ((out = creat (drft, m_gmprot ())) == NOTOK)
390 adios (drft, "unable to create");
394 i = format_len + 1024;
395 scanl = mh_xmalloc((size_t) i + 2);
399 dat[3] = outputlinelen;
401 fmt_scan(fmt, scanl, i, dat);
402 write(out, scanl, strlen(scanl));
405 cpydata (in, out, form, drft);
411 context_save (); /* save the context file */
415 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);
421 * Append an item to a comma separated list
425 addlist (char *list, char *item)
428 list = add (", ", list);
430 return add (item, list);