3 * comp.c -- compose a message
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
16 static struct swit switches[] = {
18 { "draftfolder +folder", 0 },
20 { "draftmessage msg", 0 },
22 { "nodraftfolder", 0 },
24 { "editor editor", 0 },
30 { "form formfile", 0 },
36 { "whatnowproc program", 0 },
38 { "nowhatnowproc", 0 },
46 static struct swit aqrunl[] = {
56 { "refile +folder", 0 },
62 static struct swit aqrul[] = {
73 main (int argc, char **argv)
75 int use = NOUSE, nedit = 0, nwhat = 0;
76 int i, in, isdf = 0, out;
77 char *cp, *cwd, *maildir, *dfolder = NULL;
78 char *ed = NULL, *file = NULL, *form = NULL;
79 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
80 char drft[BUFSIZ], **argp, **arguments;
81 struct msgs *mp = NULL;
85 setlocale(LC_ALL, "");
87 invo_name = r1bindex (argv[0], '/');
89 /* read user profile/context */
92 arguments = getarguments (invo_name, argc, argv, 1);
95 while ((cp = *argp++)) {
97 switch (smatch (++cp, switches)) {
99 ambigsw (cp, switches);
102 adios (NULL, "-%s unknown", cp);
105 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
107 print_help (buf, switches, 1);
110 print_version(invo_name);
114 if (!(ed = *argp++) || *ed == '-')
115 adios (NULL, "missing argument to %s", argp[-2]);
123 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
124 adios (NULL, "missing argument to %s", argp[-2]);
132 if (!(form = *argp++) || *form == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
143 case FILESW: /* compatibility */
145 adios (NULL, "only one file at a time!");
146 if (!(file = *argp++) || *file == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
153 adios (NULL, "only one draft folder at a time!");
154 if (!(cp = *argp++) || *cp == '-')
155 adios (NULL, "missing argument to %s", argp[-2]);
156 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
157 *cp != '@' ? TFOLDER : TSUBCWF);
161 adios (NULL, "only one draft message at a time!");
162 if (!(file = *argp++) || *file == '-')
163 adios (NULL, "missing argument to %s", argp[-2]);
171 if (*cp == '+' || *cp == '@') {
173 adios (NULL, "only one folder at a time!");
175 folder = pluspath (cp);
178 adios (NULL, "only one message at a time!");
184 cwd = getcpy (pwd ());
186 if (!context_find ("path"))
187 free (path ("./", TFOLDER));
190 * Check if we are using a draft folder
191 * and have specified a message in it.
193 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
197 if (form && (folder || msg))
198 adios (NULL, "can't mix forms and folders/msgs");
202 * Use a message as the "form" for the new message.
207 folder = getfolder (1);
208 maildir = m_maildir (folder);
210 if (chdir (maildir) == NOTOK)
211 adios (maildir, "unable to change directory to");
213 /* read folder and create message structure */
214 if (!(mp = folder_read (folder)))
215 adios (NULL, "unable to read folder %s", folder);
217 /* check for empty folder */
219 adios (NULL, "no messages in %s", folder);
221 /* parse the message range/sequence/name and set SELECTED */
222 if (!m_convert (mp, msg))
224 seq_setprev (mp); /* set the previous-sequence */
227 adios (NULL, "only one message at a time!");
229 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
230 adios (form, "unable to open message");
232 in = open_form(&form, components);
235 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
238 * Check if we have an existing draft
240 if ((out = open (drft, O_RDONLY)) != NOTOK) {
241 i = fdcompare (in, out);
245 * If we have given -use flag, or if the
246 * draft is just the same as the components
247 * file, then no need to ask any questions.
252 if (stat (drft, &st) == NOTOK)
253 adios (drft, "unable to stat");
254 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
255 for (i = LISTDSW; i != YESW;) {
256 if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
258 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
271 showfile (++argp, drft);
274 if (refile (++argp, drft) == 0)
278 advise (NULL, "say what?");
284 adios (drft, "unable to open");
287 if ((out = creat (drft, m_gmprot ())) == NOTOK)
288 adios (drft, "unable to create");
289 cpydata (in, out, form, drft);
294 context_save (); /* save the context file */
298 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);