]>
diplodocus.org Git - nmh/blob - uip/comp.c
382be84deb69daec86b6d656dcc1698ccebb9476
1 /* comp.c -- compose a message
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/context_save.h"
10 #include "sbr/context_find.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
17 #include "h/fmt_scan.h"
19 #include "sbr/m_maildir.h"
22 #define COMP_SWITCHES \
23 X("draftfolder +folder", 0, DFOLDSW) \
24 X("draftmessage msg", 0, DMSGSW) \
25 X("nodraftfolder", 0, NDFLDSW) \
26 X("editor editor", 0, EDITRSW) \
27 X("noedit", 0, NEDITSW) \
28 X("file file", 0, FILESW) \
29 X("form formfile", 0, FORMSW) \
31 X("nouse", 0, NUSESW) \
32 X("whatnowproc program", 0, WHATSW) \
33 X("nowhatnowproc", 0, NWHATSW) \
34 X("build", 5, BILDSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
37 X("to address", 0, TOSW) \
38 X("cc address", 0, CCSW) \
39 X("from address", 0, FROMSW) \
40 X("fcc mailbox", 0, FCCSW) \
41 X("width columns", 0, WIDTHSW) \
42 X("subject text", 0, SUBJECTSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(COMP
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(COMP
, switches
);
52 #define DISPO_SWITCHES \
54 X("replace", 0, YESW) \
56 X("list", 0, LISTDSW) \
57 X("refile +folder", 0, REFILSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(DISPO
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
68 static struct swit aqrul
[] = {
70 { "replace", 0, YESW
},
72 { "list", 0, LISTDSW
},
73 { "refile", 0, REFILSW
},
78 main (int argc
, char **argv
)
84 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
85 int outputlinelen
= OUTPUTLINELEN
;
86 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
87 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
88 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
89 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
91 char drft
[BUFSIZ
], **argp
, **arguments
;
92 struct msgs
*mp
= NULL
;
96 if (nmh_init(argv
[0], true, true)) { return 1; }
98 arguments
= getarguments (invo_name
, argc
, argv
, 1);
101 while ((cp
= *argp
++)) {
103 switch (smatch (++cp
, switches
)) {
105 ambigsw (cp
, switches
);
108 die("-%s unknown", cp
);
111 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
113 print_help (buf
, switches
, 1);
116 print_version(invo_name
);
120 if (!(ed
= *argp
++) || *ed
== '-')
121 die("missing argument to %s", argp
[-2]);
129 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
130 die("missing argument to %s", argp
[-2]);
142 if (!(form
= *argp
++) || *form
== '-')
143 die("missing argument to %s", argp
[-2]);
153 case FILESW
: /* compatibility */
155 die("only one file at a time!");
156 if (!(file
= *argp
++) || *file
== '-')
157 die("missing argument to %s", argp
[-2]);
163 die("only one draft folder at a time!");
164 if (!(cp
= *argp
++) || *cp
== '-')
165 die("missing argument to %s", argp
[-2]);
166 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
167 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
171 die("only one draft message at a time!");
172 if (!(file
= *argp
++) || *file
== '-')
173 die("missing argument to %s", argp
[-2]);
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 to
= addlist(to
, cp
);
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 cc
= addlist(cc
, cp
);
193 if (!(cp
= *argp
++) || *cp
== '-')
194 die("missing argument to %s", argp
[-2]);
195 from
= addlist(from
, cp
);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 die("missing argument to %s", argp
[-2]);
203 cp
= dp
= path(cp
+ 1, TSUBCWF
);
204 fcc
= addlist(fcc
, cp
);
209 if (!(cp
= *argp
++) || *cp
== '-')
210 die("missing argument to %s", argp
[-2]);
211 if ((outputlinelen
= atoi(cp
)) < 10)
212 die("impossible width %d", outputlinelen
);
216 if (!(cp
= *argp
++) || *cp
== '-')
217 die("missing argument to %s", argp
[-2]);
222 if (*cp
== '+' || *cp
== '@') {
224 die("only one folder at a time!");
225 folder
= pluspath (cp
);
228 die("only one message at a time!");
233 cwd
= mh_xstrdup(pwd ());
235 if (!context_find ("path"))
236 free (path ("./", TFOLDER
));
239 * Check if we are using a draft folder
240 * and have specified a message in it.
242 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
246 if (form
&& (folder
|| msg
))
247 die("can't mix forms and folders/msgs");
253 * Use a message as the "form" for the new message.
258 folder
= getfolder (1);
259 maildir
= m_maildir (folder
);
261 if (chdir (maildir
) == NOTOK
)
262 adios (maildir
, "unable to change directory to");
264 /* read folder and create message structure */
265 if (!(mp
= folder_read (folder
, 1)))
266 die("unable to read folder %s", folder
);
268 /* check for empty folder */
270 die("no messages in %s", folder
);
272 /* parse the message range/sequence/name and set SELECTED */
273 if (!m_convert (mp
, msg
))
275 seq_setprev (mp
); /* set the previous-sequence */
279 die("only one message at a time!");
281 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
282 adios (form
, "unable to open message");
289 cp
= new_fs(form
, NULL
, NULL
);
290 format_len
= strlen(cp
);
291 fmt_compile(cp
, &fmt
, 1);
294 * Set up any components that were fed to us on the command line
298 cptr
= fmt_findcomp("from");
303 cptr
= fmt_findcomp("to");
308 cptr
= fmt_findcomp("cc");
313 cptr
= fmt_findcomp("fcc");
318 cptr
= fmt_findcomp("subject");
320 cptr
->c_text
= subject
;
325 strncpy (drft
, build
? m_maildir ("draft")
326 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
329 * Check if we have an existing draft
331 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
332 i
= fdcompare (in
, out
);
336 * If we have given -use flag, or if the
337 * draft is just the same as the components
338 * file, then no need to ask any questions.
343 if (stat (drft
, &st
) == NOTOK
)
344 adios (drft
, "unable to stat");
345 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
346 for (i
= LISTDSW
; i
!= YESW
;) {
347 if (!(argp
= read_switch_multiword ("\nDisposition? ",
348 isdf
? aqrunl
: aqrul
)))
350 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
363 showfile (++argp
, drft
);
366 if (refile (++argp
, drft
) == 0)
376 adios (drft
, "unable to open");
379 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
380 adios (drft
, "unable to create");
384 i
= format_len
+ 1024;
385 scanl
= charstring_create (i
+ 2);
389 dat
[3] = outputlinelen
;
391 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
392 if (write(out
, charstring_buffer (scanl
),
393 charstring_bytes (scanl
)) < 0) {
394 advise (drft
, "write");
396 charstring_free(scanl
);
398 cpydata (in
, out
, form
, drft
);
404 context_save (); /* save the context file */
408 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);