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 = mhbasename(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]", invo_name);
86 print_help(buf, switches, 1);
89 print_version(invo_name);
93 if (!(ed = *argp++) || *ed == '-')
94 adios(NULL, "missing argument to %s", argp[-2]);
102 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
103 adios(NULL, "missing argument to %s", argp[-2]);
111 if (!(form = *argp++) || *form == '-')
112 adios(NULL, "missing argument to %s", argp[-2]);
123 if (*cp == '+' || *cp == '@') {
125 adios(NULL, "only one folder at a time!");
127 folder = pluspath(cp);
130 adios(NULL, "only one message at a time!");
138 if (form && (folder || msg))
139 adios(NULL, "can't mix forms and folders/msgs");
141 if (!use && (folder || msg)) {
143 ** Take a message as the "form" for the new message.
148 folder = getfolder(FCUR);
149 maildir = m_maildir(folder);
151 if (chdir(maildir) == NOTOK)
152 adios(maildir, "unable to change directory to");
154 /* read folder and create message structure */
155 if (!(mp = folder_read(folder)))
156 adios(NULL, "unable to read folder %s", folder);
158 /* check for empty folder */
160 adios(NULL, "no messages in %s", folder);
162 /* parse the message range/sequence/name and set SELECTED */
163 if (!m_convert(mp, msg))
165 seq_setprev(mp); /* set the previous-sequence */
168 adios(NULL, "only one message at a time!");
170 if ((in = open(form = getcpy(m_name(mp->lowsel)),
172 adios(form, "unable to open message");
174 in = open_form(&form, components);
177 strncpy(drft, m_draft(use ? (msg?msg:"cur") : "new"), sizeof(drft));
180 ** Check if we have an existing draft
182 if ((out = open(drft, O_RDONLY)) != NOTOK) {
183 i = fdcompare(in, out);
187 ** If we have given -use flag, or if the
188 ** draft is just the same as the components
189 ** file, then no need to ask any questions.
194 if (stat(drft, &st) == NOTOK)
195 adios(drft, "unable to stat");
196 printf("Draft \"%s\" exists (%ld bytes).",
197 drft, (long) st.st_size);
198 for (i = LISTDSW; i != YESW;) {
199 if (!(argp = getans("\nDisposition? ", aqrunl)))
201 switch (i = smatch(*argp, aqrunl)) {
213 showfile(++argp, drft);
216 if (refile(++argp, drft) == 0)
220 advise(NULL, "say what?");
225 adios(drft, "unable to open");
228 if ((out = creat(drft, m_gmprot())) == NOTOK)
229 adios(drft, "unable to create");
230 cpydata(in, out, form, drft);
235 context_save(); /* save the context file */
239 what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);