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 { "editor editor", 0 },
19 { "form formfile", 0 },
25 { "whatnowproc program", 0 },
27 { "nowhatnowproc", 0 },
35 static struct swit aqrunl[] = {
45 { "refile +folder", 0 },
53 main (int argc, char **argv)
55 int use = NOUSE, nedit = 0, nwhat = 0;
57 char *cp, *cwd, *maildir;
58 char *ed = NULL, *form = NULL;
59 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
60 char drft[BUFSIZ], **argp, **arguments;
61 struct msgs *mp = NULL;
65 setlocale(LC_ALL, "");
67 invo_name = r1bindex (argv[0], '/');
69 /* read user profile/context */
72 arguments = getarguments (invo_name, argc, argv, 1);
75 while ((cp = *argp++)) {
77 switch (smatch (++cp, switches)) {
79 ambigsw (cp, switches);
82 adios (NULL, "-%s unknown", cp);
85 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
87 print_help (buf, switches, 1);
90 print_version(invo_name);
94 if (!(ed = *argp++) || *ed == '-')
95 adios (NULL, "missing argument to %s", argp[-2]);
103 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
104 adios (NULL, "missing argument to %s", argp[-2]);
112 if (!(form = *argp++) || *form == '-')
113 adios (NULL, "missing argument to %s", argp[-2]);
124 if (*cp == '+' || *cp == '@') {
126 adios (NULL, "only one folder at a time!");
128 folder = pluspath (cp);
131 adios (NULL, "only one message at a time!");
137 cwd = getcpy (pwd ());
139 if (!context_find ("path"))
140 free (path ("./", TFOLDER));
142 if (form && (folder || msg))
143 adios (NULL, "can't mix forms and folders/msgs");
145 if (!use && (folder || msg)) {
147 * Take a message as the "form" for the new message.
152 folder = getfolder (1);
153 maildir = m_maildir (folder);
155 if (chdir (maildir) == NOTOK)
156 adios (maildir, "unable to change directory to");
158 /* read folder and create message structure */
159 if (!(mp = folder_read (folder)))
160 adios (NULL, "unable to read folder %s", folder);
162 /* check for empty folder */
164 adios (NULL, "no messages in %s", folder);
166 /* parse the message range/sequence/name and set SELECTED */
167 if (!m_convert (mp, msg))
169 seq_setprev (mp); /* set the previous-sequence */
172 adios (NULL, "only one message at a time!");
174 if ((in = open (form = getcpy (m_name (mp->lowsel)),
176 adios (form, "unable to open message");
178 in = open_form(&form, components);
181 strncpy (drft, m_draft(use ? (msg?msg:"cur") : "new"), sizeof(drft));
184 * Check if we have an existing draft
186 if ((out = open (drft, O_RDONLY)) != NOTOK) {
187 i = fdcompare (in, out);
191 * If we have given -use flag, or if the
192 * draft is just the same as the components
193 * file, then no need to ask any questions.
198 if (stat (drft, &st) == NOTOK)
199 adios (drft, "unable to stat");
200 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
201 for (i = LISTDSW; i != YESW;) {
202 if (!(argp = getans ("\nDisposition? ", aqrunl)))
204 switch (i = smatch (*argp, aqrunl)) {
216 showfile (++argp, drft);
219 if (refile (++argp, drft) == 0)
223 advise (NULL, "say what?");
229 adios (drft, "unable to open");
232 if ((out = creat (drft, m_gmprot ())) == NOTOK)
233 adios (drft, "unable to create");
234 cpydata (in, out, form, drft);
239 context_save (); /* save the context file */
243 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);