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.
14 static struct swit switches[] = {
16 { "draftfolder +folder", 0 },
18 { "draftmessage msg", 0 },
20 { "nodraftfolder", 0 },
22 { "editor editor", 0 },
28 { "form formfile", 0 },
34 { "whatnowproc program", 0 },
36 { "nowhatnowproc", 0 },
44 static struct swit aqrunl[] = {
54 { "refile +folder", 0 },
60 static struct swit aqrul[] = {
71 main (int argc, char **argv)
73 int use = NOUSE, nedit = 0, nwhat = 0;
74 int i, in, isdf = 0, out;
75 char *cp, *cwd, *maildir, *dfolder = NULL;
76 char *ed = NULL, *file = NULL, *form = NULL;
77 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
78 char drft[BUFSIZ], **argp, **arguments;
79 struct msgs *mp = NULL;
83 setlocale(LC_ALL, "");
85 invo_name = r1bindex (argv[0], '/');
87 /* read user profile/context */
90 arguments = getarguments (invo_name, argc, argv, 1);
93 while ((cp = *argp++)) {
95 switch (smatch (++cp, switches)) {
97 ambigsw (cp, switches);
100 adios (NULL, "-%s unknown", cp);
103 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
105 print_help (buf, switches, 1);
108 print_version(invo_name);
112 if (!(ed = *argp++) || *ed == '-')
113 adios (NULL, "missing argument to %s", argp[-2]);
121 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
122 adios (NULL, "missing argument to %s", argp[-2]);
130 if (!(form = *argp++) || *form == '-')
131 adios (NULL, "missing argument to %s", argp[-2]);
141 case FILESW: /* compatibility */
143 adios (NULL, "only one file at a time!");
144 if (!(file = *argp++) || *file == '-')
145 adios (NULL, "missing argument to %s", argp[-2]);
151 adios (NULL, "only one draft folder at a time!");
152 if (!(cp = *argp++) || *cp == '-')
153 adios (NULL, "missing argument to %s", argp[-2]);
154 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
155 *cp != '@' ? TFOLDER : TSUBCWF);
159 adios (NULL, "only one draft message at a time!");
160 if (!(file = *argp++) || *file == '-')
161 adios (NULL, "missing argument to %s", argp[-2]);
169 if (*cp == '+' || *cp == '@') {
171 adios (NULL, "only one folder at a time!");
173 folder = pluspath (cp);
176 adios (NULL, "only one message at a time!");
182 cwd = getcpy (pwd ());
184 if (!context_find ("path"))
185 free (path ("./", TFOLDER));
188 * Check if we are using a draft folder
189 * and have specified a message in it.
191 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
195 if (form && (folder || msg))
196 adios (NULL, "can't mix forms and folders/msgs");
200 * Use a message as the "form" for the new message.
205 folder = getfolder (1);
206 maildir = m_maildir (folder);
208 if (chdir (maildir) == NOTOK)
209 adios (maildir, "unable to change directory to");
211 /* read folder and create message structure */
212 if (!(mp = folder_read (folder)))
213 adios (NULL, "unable to read folder %s", folder);
215 /* check for empty folder */
217 adios (NULL, "no messages in %s", folder);
219 /* parse the message range/sequence/name and set SELECTED */
220 if (!m_convert (mp, msg))
222 seq_setprev (mp); /* set the previous-sequence */
225 adios (NULL, "only one message at a time!");
227 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
228 adios (form, "unable to open message");
230 in = open_form(&form, components);
233 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
236 * Check if we have an existing draft
238 if ((out = open (drft, O_RDONLY)) != NOTOK) {
239 i = fdcompare (in, out);
243 * If we have given -use flag, or if the
244 * draft is just the same as the components
245 * file, then no need to ask any questions.
250 if (stat (drft, &st) == NOTOK)
251 adios (drft, "unable to stat");
252 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
253 for (i = LISTDSW; i != YESW;) {
254 if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
256 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
269 showfile (++argp, drft);
272 if (refile (++argp, drft) == 0)
276 advise (NULL, "say what?");
282 adios (drft, "unable to open");
285 if ((out = creat (drft, m_gmprot ())) == NOTOK)
286 adios (drft, "unable to create");
287 cpydata (in, out, form, drft);
292 context_save (); /* save the context file */
296 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);