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 (!context_find("path"))
139 free(path("./", TFOLDER));
141 if (form && (folder || msg))
142 adios(NULL, "can't mix forms and folders/msgs");
144 if (!use && (folder || msg)) {
146 ** Take a message as the "form" for the new message.
151 folder = getfolder(FCUR);
152 maildir = m_maildir(folder);
154 if (chdir(maildir) == NOTOK)
155 adios(maildir, "unable to change directory to");
157 /* read folder and create message structure */
158 if (!(mp = folder_read(folder)))
159 adios(NULL, "unable to read folder %s", folder);
161 /* check for empty folder */
163 adios(NULL, "no messages in %s", folder);
165 /* parse the message range/sequence/name and set SELECTED */
166 if (!m_convert(mp, msg))
168 seq_setprev(mp); /* set the previous-sequence */
171 adios(NULL, "only one message at a time!");
173 if ((in = open(form = getcpy(m_name(mp->lowsel)),
175 adios(form, "unable to open message");
177 in = open_form(&form, components);
180 strncpy(drft, m_draft(use ? (msg?msg:"cur") : "new"), sizeof(drft));
183 ** Check if we have an existing draft
185 if ((out = open(drft, O_RDONLY)) != NOTOK) {
186 i = fdcompare(in, out);
190 ** If we have given -use flag, or if the
191 ** draft is just the same as the components
192 ** file, then no need to ask any questions.
197 if (stat(drft, &st) == NOTOK)
198 adios(drft, "unable to stat");
199 printf("Draft \"%s\" exists (%ld bytes).",
200 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?");
228 adios(drft, "unable to open");
231 if ((out = creat(drft, m_gmprot())) == NOTOK)
232 adios(drft, "unable to create");
233 cpydata(in, out, form, drft);
238 context_save(); /* save the context file */
242 what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);