3 * comp.c -- compose a message
11 static struct swit switches[] = {
13 { "draftfolder +folder", 0 },
15 { "draftmessage msg", 0 },
17 { "nodraftfolder", 0 },
19 { "editor editor", 0 },
25 { "form formfile", 0 },
31 { "whatnowproc program", 0 },
33 { "nowhatnowproc", 0 },
41 static struct swit aqrunl[] = {
51 { "refile +folder", 0 },
57 static struct swit aqrul[] = {
68 main (int argc, char **argv)
70 int use = NOUSE, nedit = 0, nwhat = 0;
71 int i, in, isdf = 0, out;
72 char *cp, *cwd, *maildir, *dfolder = NULL;
73 char *ed = NULL, *file = NULL, *form = NULL;
74 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
75 char drft[BUFSIZ], **argp, **arguments;
76 struct msgs *mp = NULL;
80 setlocale(LC_ALL, "");
82 invo_name = r1bindex (argv[0], '/');
84 /* read user profile/context */
87 arguments = getarguments (invo_name, argc, argv, 1);
90 while ((cp = *argp++)) {
92 switch (smatch (++cp, switches)) {
94 ambigsw (cp, switches);
97 adios (NULL, "-%s unknown", cp);
100 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
102 print_help (buf, switches, 1);
105 print_version(invo_name);
109 if (!(ed = *argp++) || *ed == '-')
110 adios (NULL, "missing argument to %s", argp[-2]);
118 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
119 adios (NULL, "missing argument to %s", argp[-2]);
127 if (!(form = *argp++) || *form == '-')
128 adios (NULL, "missing argument to %s", argp[-2]);
138 case FILESW: /* compatibility */
140 adios (NULL, "only one file at a time!");
141 if (!(file = *argp++) || *file == '-')
142 adios (NULL, "missing argument to %s", argp[-2]);
148 adios (NULL, "only one draft folder at a time!");
149 if (!(cp = *argp++) || *cp == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
151 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
152 *cp != '@' ? TFOLDER : TSUBCWF);
156 adios (NULL, "only one draft message at a time!");
157 if (!(file = *argp++) || *file == '-')
158 adios (NULL, "missing argument to %s", argp[-2]);
166 if (*cp == '+' || *cp == '@') {
168 adios (NULL, "only one folder at a time!");
170 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
173 adios (NULL, "only one message at a time!");
179 cwd = getcpy (pwd ());
181 if (!context_find ("path"))
182 free (path ("./", TFOLDER));
185 * Check if we are using a draft folder
186 * and have specified a message in it.
188 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
192 if (form && (folder || msg))
193 adios (NULL, "can't mix forms and folders/msgs");
197 * Use a message as the "form" for the new message.
202 folder = getfolder (1);
203 maildir = m_maildir (folder);
205 if (chdir (maildir) == NOTOK)
206 adios (maildir, "unable to change directory to");
208 /* read folder and create message structure */
209 if (!(mp = folder_read (folder)))
210 adios (NULL, "unable to read folder %s", folder);
212 /* check for empty folder */
214 adios (NULL, "no messages in %s", folder);
216 /* parse the message range/sequence/name and set SELECTED */
217 if (!m_convert (mp, msg))
219 seq_setprev (mp); /* set the previous-sequence */
222 adios (NULL, "only one message at a time!");
224 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
225 adios (form, "unable to open message");
228 * Open a component or forms file
231 if ((in = open (etcpath (form), O_RDONLY)) == NOTOK)
232 adios (form, "unable to open form file");
234 if ((in = open (etcpath (components), O_RDONLY)) == NOTOK)
235 adios (components, "unable to open default components file");
241 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
244 * Check if we have an existing draft
246 if ((out = open (drft, O_RDONLY)) != NOTOK) {
247 i = fdcompare (in, out);
251 * If we have given -use flag, or if the
252 * draft is just the same as the components
253 * file, then no need to ask any questions.
258 if (stat (drft, &st) == NOTOK)
259 adios (drft, "unable to stat");
260 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
261 for (i = LISTDSW; i != YESW;) {
262 if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
264 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
277 showfile (++argp, drft);
280 if (refile (++argp, drft) == 0)
284 advise (NULL, "say what?");
290 adios (drft, "unable to open");
293 if ((out = creat (drft, m_gmprot ())) == NOTOK)
294 adios (drft, "unable to create");
295 cpydata (in, out, form, drft);
300 context_save (); /* save the context file */
304 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);