2 * comp.c -- compose a message
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
13 static struct swit switches[] = {
15 { "draftfolder +folder", 0 },
17 { "draftmessage msg", 0 },
19 { "nodraftfolder", 0 },
21 { "editor editor", 0 },
27 { "form formfile", 0 },
33 { "whatnowproc program", 0 },
35 { "nowhatnowproc", 0 },
43 static struct swit aqrunl[] = {
53 { "refile +folder", 0 },
59 static struct swit aqrul[] = {
70 main (int argc, char **argv)
72 int use = NOUSE, nedit = 0, nwhat = 0;
73 int i, in, isdf = 0, out;
74 char *cp, *cwd, *maildir, *dfolder = NULL;
75 char *ed = NULL, *file = NULL, *form = NULL;
76 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
77 char drft[BUFSIZ], **argp, **arguments;
78 struct msgs *mp = NULL;
82 setlocale(LC_ALL, "");
84 invo_name = r1bindex (argv[0], '/');
86 /* read user profile/context */
89 arguments = getarguments (invo_name, argc, argv, 1);
92 while ((cp = *argp++)) {
94 switch (smatch (++cp, switches)) {
96 ambigsw (cp, switches);
99 adios (NULL, "-%s unknown", cp);
102 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
104 print_help (buf, switches, 1);
107 print_version(invo_name);
111 if (!(ed = *argp++) || *ed == '-')
112 adios (NULL, "missing argument to %s", argp[-2]);
120 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
121 adios (NULL, "missing argument to %s", argp[-2]);
129 if (!(form = *argp++) || *form == '-')
130 adios (NULL, "missing argument to %s", argp[-2]);
140 case FILESW: /* compatibility */
142 adios (NULL, "only one file at a time!");
143 if (!(file = *argp++) || *file == '-')
144 adios (NULL, "missing argument to %s", argp[-2]);
150 adios (NULL, "only one draft folder at a time!");
151 if (!(cp = *argp++) || *cp == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
153 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
154 *cp != '@' ? TFOLDER : TSUBCWF);
158 adios (NULL, "only one draft message at a time!");
159 if (!(file = *argp++) || *file == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
168 if (*cp == '+' || *cp == '@') {
170 adios (NULL, "only one folder at a time!");
172 folder = pluspath (cp);
175 adios (NULL, "only one message at a time!");
181 cwd = getcpy (pwd ());
183 if (!context_find ("path"))
184 free (path ("./", TFOLDER));
187 * Check if we are using a draft folder
188 * and have specified a message in it.
190 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
194 if (form && (folder || msg))
195 adios (NULL, "can't mix forms and folders/msgs");
199 * Use a message as the "form" for the new message.
204 folder = getfolder (1);
205 maildir = m_maildir (folder);
207 if (chdir (maildir) == NOTOK)
208 adios (maildir, "unable to change directory to");
210 /* read folder and create message structure */
211 if (!(mp = folder_read (folder)))
212 adios (NULL, "unable to read folder %s", folder);
214 /* check for empty folder */
216 adios (NULL, "no messages in %s", folder);
218 /* parse the message range/sequence/name and set SELECTED */
219 if (!m_convert (mp, msg))
221 seq_setprev (mp); /* set the previous-sequence */
224 adios (NULL, "only one message at a time!");
226 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
227 adios (form, "unable to open message");
229 in = open_form(&form, components);
232 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
235 * Check if we have an existing draft
237 if ((out = open (drft, O_RDONLY)) != NOTOK) {
238 i = fdcompare (in, out);
242 * If we have given -use flag, or if the
243 * draft is just the same as the components
244 * file, then no need to ask any questions.
249 if (stat (drft, &st) == NOTOK)
250 adios (drft, "unable to stat");
251 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
252 for (i = LISTDSW; i != YESW;) {
253 if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
255 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
268 showfile (++argp, drft);
271 if (refile (++argp, drft) == 0)
275 advise (NULL, "say what?");
281 adios (drft, "unable to open");
284 if ((out = creat (drft, m_gmprot ())) == NOTOK)
285 adios (drft, "unable to create");
286 cpydata (in, out, form, drft);
291 context_save (); /* save the context file */
295 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);