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 main (int argc, char **argv)
85 int use = NOUSE, nedit = 0, nwhat = 0;
86 int i, in = NOTOK, isdf = 0, out, dat[5], format_len = 0;
87 int outputlinelen = OUTPUTLINELEN;
88 char *cp, *cwd, *maildir, *dfolder = NULL;
89 char *ed = NULL, *file = NULL, *form = NULL;
90 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
91 char *to = NULL, *from = NULL, *cc = NULL, *fcc = NULL, *dp;
93 char drft[BUFSIZ], **argp, **arguments;
94 struct msgs *mp = NULL;
99 setlocale(LC_ALL, "");
101 invo_name = r1bindex (argv[0], '/');
103 /* read user profile/context */
106 arguments = getarguments (invo_name, argc, argv, 1);
109 while ((cp = *argp++)) {
111 switch (smatch (++cp, switches)) {
113 ambigsw (cp, switches);
116 adios (NULL, "-%s unknown", cp);
119 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
121 print_help (buf, switches, 1);
124 print_version(invo_name);
128 if (!(ed = *argp++) || *ed == '-')
129 adios (NULL, "missing argument to %s", argp[-2]);
137 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
146 if (!(form = *argp++) || *form == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
157 case FILESW: /* compatibility */
159 adios (NULL, "only one file at a time!");
160 if (!(file = *argp++) || *file == '-')
161 adios (NULL, "missing argument to %s", argp[-2]);
167 adios (NULL, "only one draft folder at a time!");
168 if (!(cp = *argp++) || *cp == '-')
169 adios (NULL, "missing argument to %s", argp[-2]);
170 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
171 *cp != '@' ? TFOLDER : TSUBCWF);
175 adios (NULL, "only one draft message at a time!");
176 if (!(file = *argp++) || *file == '-')
177 adios (NULL, "missing argument to %s", argp[-2]);
185 if (!(cp = *argp++) || *cp == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 to = addlist(to, cp);
191 if (!(cp = *argp++) || *cp == '-')
192 adios (NULL, "missing argument to %s", argp[-2]);
193 cc = addlist(cc, cp);
197 if (!(cp = *argp++) || *cp == '-')
198 adios (NULL, "missing argument to %s", argp[-2]);
199 from = addlist(from, cp);
203 if (!(cp = *argp++) || *cp == '-')
204 adios (NULL, "missing argument to %s", argp[-2]);
207 cp = dp = path(cp + 1, TSUBCWF);
208 fcc = addlist(fcc, cp);
214 if (!(cp = *argp++) || *cp == '-')
215 adios (NULL, "missing argument to %s", argp[-2]);
216 if ((outputlinelen = atoi(cp)) < 10)
217 adios (NULL, "impossible width %d", outputlinelen);
221 if (!(cp = *argp++) || *cp == '-')
222 adios (NULL, "missing argument to %s", argp[-2]);
227 if (*cp == '+' || *cp == '@') {
229 adios (NULL, "only one folder at a time!");
231 folder = pluspath (cp);
234 adios (NULL, "only one message at a time!");
240 cwd = getcpy (pwd ());
242 if (!context_find ("path"))
243 free (path ("./", TFOLDER));
246 * Check if we are using a draft folder
247 * and have specified a message in it.
249 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
253 if (form && (folder || msg))
254 adios (NULL, "can't mix forms and folders/msgs");
260 * Use a message as the "form" for the new message.
265 folder = getfolder (1);
266 maildir = m_maildir (folder);
268 if (chdir (maildir) == NOTOK)
269 adios (maildir, "unable to change directory to");
271 /* read folder and create message structure */
272 if (!(mp = folder_read (folder)))
273 adios (NULL, "unable to read folder %s", folder);
275 /* check for empty folder */
277 adios (NULL, "no messages in %s", folder);
279 /* parse the message range/sequence/name and set SELECTED */
280 if (!m_convert (mp, msg))
282 seq_setprev (mp); /* set the previous-sequence */
285 adios (NULL, "only one message at a time!");
287 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
288 adios (form, "unable to open message");
295 cp = new_fs(form, NULL, NULL);
296 format_len = strlen(cp);
297 fmt_compile(cp, &fmt);
300 * Set up any components that were fed to us on the command line
304 FINDCOMP(cptr, "from");
309 FINDCOMP(cptr, "to");
314 FINDCOMP(cptr, "cc");
319 FINDCOMP(cptr, "fcc");
324 FINDCOMP(cptr, "subject");
326 cptr->c_text = subject;
331 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
334 * Check if we have an existing draft
336 if ((out = open (drft, O_RDONLY)) != NOTOK) {
337 i = fdcompare (in, out);
341 * If we have given -use flag, or if the
342 * draft is just the same as the components
343 * file, then no need to ask any questions.
348 if (stat (drft, &st) == NOTOK)
349 adios (drft, "unable to stat");
350 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
351 for (i = LISTDSW; i != YESW;) {
352 if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
354 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
367 showfile (++argp, drft);
370 if (refile (++argp, drft) == 0)
374 advise (NULL, "say what?");
380 adios (drft, "unable to open");
383 if ((out = creat (drft, m_gmprot ())) == NOTOK)
384 adios (drft, "unable to create");
388 i = format_len + 1024;
389 scanl = mh_xmalloc((size_t) i + 2);
393 dat[3] = outputlinelen;
395 fmt_scan(fmt, scanl, i, dat);
396 write(out, scanl, strlen(scanl));
399 cpydata (in, out, form, drft);
405 context_save (); /* save the context file */
409 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);