]>
diplodocus.org Git - nmh/blob - uip/comp.c
34d46bb276fc0a4181c7a078f8dbd68cbba4d689
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/getarguments.h"
10 #include "sbr/read_switch_multiword.h"
11 #include "sbr/seq_setprev.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/showfile.h"
14 #include "sbr/smatch.h"
15 #include "sbr/refile.h"
16 #include "sbr/cpydata.h"
17 #include "sbr/m_draft.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/fdcompare.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/context_save.h"
23 #include "sbr/context_find.h"
24 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/error.h"
30 #include "h/fmt_scan.h"
32 #include "sbr/m_maildir.h"
35 #define COMP_SWITCHES \
36 X("draftfolder +folder", 0, DFOLDSW) \
37 X("draftmessage msg", 0, DMSGSW) \
38 X("nodraftfolder", 0, NDFLDSW) \
39 X("editor editor", 0, EDITRSW) \
40 X("noedit", 0, NEDITSW) \
41 X("file file", 0, FILESW) \
42 X("form formfile", 0, FORMSW) \
44 X("nouse", 0, NUSESW) \
45 X("whatnowproc program", 0, WHATSW) \
46 X("nowhatnowproc", 0, NWHATSW) \
47 X("build", 5, BILDSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
50 X("to address", 0, TOSW) \
51 X("cc address", 0, CCSW) \
52 X("from address", 0, FROMSW) \
53 X("fcc mailbox", 0, FCCSW) \
54 X("width columns", 0, WIDTHSW) \
55 X("subject text", 0, SUBJECTSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(COMP
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(COMP
, switches
);
65 #define DISPO_SWITCHES \
67 X("replace", 0, YESW) \
69 X("list", 0, LISTDSW) \
70 X("refile +folder", 0, REFILSW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(DISPO
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
81 static struct swit aqrul
[] = {
83 { "replace", 0, YESW
},
85 { "list", 0, LISTDSW
},
86 { "refile", 0, REFILSW
},
91 main (int argc
, char **argv
)
97 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
98 int outputlinelen
= OUTPUTLINELEN
;
99 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
100 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
101 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
102 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
103 char *subject
= NULL
;
104 char drft
[BUFSIZ
], **argp
, **arguments
;
105 struct msgs
*mp
= NULL
;
109 if (nmh_init(argv
[0], true, true)) { return 1; }
111 arguments
= getarguments (invo_name
, argc
, argv
, 1);
114 while ((cp
= *argp
++)) {
116 switch (smatch (++cp
, switches
)) {
118 ambigsw (cp
, switches
);
121 die("-%s unknown", cp
);
124 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
126 print_help (buf
, switches
, 1);
129 print_version(invo_name
);
133 if (!(ed
= *argp
++) || *ed
== '-')
134 die("missing argument to %s", argp
[-2]);
142 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
143 die("missing argument to %s", argp
[-2]);
155 if (!(form
= *argp
++) || *form
== '-')
156 die("missing argument to %s", argp
[-2]);
166 case FILESW
: /* compatibility */
168 die("only one file at a time!");
169 if (!(file
= *argp
++) || *file
== '-')
170 die("missing argument to %s", argp
[-2]);
176 die("only one draft folder at a time!");
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
179 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
180 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
184 die("only one draft message at a time!");
185 if (!(file
= *argp
++) || *file
== '-')
186 die("missing argument to %s", argp
[-2]);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
196 to
= addlist(to
, cp
);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 die("missing argument to %s", argp
[-2]);
202 cc
= addlist(cc
, cp
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 die("missing argument to %s", argp
[-2]);
208 from
= addlist(from
, cp
);
212 if (!(cp
= *argp
++) || *cp
== '-')
213 die("missing argument to %s", argp
[-2]);
216 cp
= dp
= path(cp
+ 1, TSUBCWF
);
217 fcc
= addlist(fcc
, cp
);
222 if (!(cp
= *argp
++) || *cp
== '-')
223 die("missing argument to %s", argp
[-2]);
224 if ((outputlinelen
= atoi(cp
)) < 10)
225 die("impossible width %d", outputlinelen
);
229 if (!(cp
= *argp
++) || *cp
== '-')
230 die("missing argument to %s", argp
[-2]);
235 if (*cp
== '+' || *cp
== '@') {
237 die("only one folder at a time!");
238 folder
= pluspath (cp
);
241 die("only one message at a time!");
246 cwd
= mh_xstrdup(pwd ());
248 if (!context_find ("path"))
249 free (path ("./", TFOLDER
));
252 * Check if we are using a draft folder
253 * and have specified a message in it.
255 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
259 if (form
&& (folder
|| msg
))
260 die("can't mix forms and folders/msgs");
266 * Use a message as the "form" for the new message.
271 folder
= getfolder (1);
272 maildir
= m_maildir (folder
);
274 if (chdir (maildir
) == NOTOK
)
275 adios (maildir
, "unable to change directory to");
277 /* read folder and create message structure */
278 if (!(mp
= folder_read (folder
, 1)))
279 die("unable to read folder %s", folder
);
281 /* check for empty folder */
283 die("no messages in %s", folder
);
285 /* parse the message range/sequence/name and set SELECTED */
286 if (!m_convert (mp
, msg
))
288 seq_setprev (mp
); /* set the previous-sequence */
292 die("only one message at a time!");
294 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
295 adios (form
, "unable to open message");
302 cp
= new_fs(form
, NULL
, NULL
);
303 format_len
= strlen(cp
);
304 fmt_compile(cp
, &fmt
, 1);
307 * Set up any components that were fed to us on the command line
311 cptr
= fmt_findcomp("from");
316 cptr
= fmt_findcomp("to");
321 cptr
= fmt_findcomp("cc");
326 cptr
= fmt_findcomp("fcc");
331 cptr
= fmt_findcomp("subject");
333 cptr
->c_text
= subject
;
338 strncpy (drft
, build
? m_maildir ("draft")
339 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
342 * Check if we have an existing draft
344 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
345 i
= fdcompare (in
, out
);
349 * If we have given -use flag, or if the
350 * draft is just the same as the components
351 * file, then no need to ask any questions.
356 if (stat (drft
, &st
) == NOTOK
)
357 adios (drft
, "unable to stat");
358 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
359 for (i
= LISTDSW
; i
!= YESW
;) {
360 if (!(argp
= read_switch_multiword ("\nDisposition? ",
361 isdf
? aqrunl
: aqrul
)))
363 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
376 showfile (++argp
, drft
);
379 if (refile (++argp
, drft
) == 0)
389 adios (drft
, "unable to open");
392 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
393 adios (drft
, "unable to create");
397 i
= format_len
+ 1024;
398 scanl
= charstring_create (i
+ 2);
402 dat
[3] = outputlinelen
;
404 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
405 if (write(out
, charstring_buffer (scanl
),
406 charstring_bytes (scanl
)) < 0) {
407 advise (drft
, "write");
409 charstring_free(scanl
);
411 cpydata (in
, out
, form
, drft
);
417 context_save (); /* save the context file */
421 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);