]>
diplodocus.org Git - nmh/blob - uip/comp.c
6d088d872943b31649492abcc4e2b4af2b276c6d
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/showfile.h"
10 #include "sbr/smatch.h"
11 #include "sbr/refile.h"
12 #include "sbr/cpydata.h"
13 #include "sbr/m_draft.h"
14 #include "sbr/m_convert.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/fdcompare.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_find.h"
20 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/error.h"
26 #include "h/fmt_scan.h"
28 #include "sbr/m_maildir.h"
31 #define COMP_SWITCHES \
32 X("draftfolder +folder", 0, DFOLDSW) \
33 X("draftmessage msg", 0, DMSGSW) \
34 X("nodraftfolder", 0, NDFLDSW) \
35 X("editor editor", 0, EDITRSW) \
36 X("noedit", 0, NEDITSW) \
37 X("file file", 0, FILESW) \
38 X("form formfile", 0, FORMSW) \
40 X("nouse", 0, NUSESW) \
41 X("whatnowproc program", 0, WHATSW) \
42 X("nowhatnowproc", 0, NWHATSW) \
43 X("build", 5, BILDSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
46 X("to address", 0, TOSW) \
47 X("cc address", 0, CCSW) \
48 X("from address", 0, FROMSW) \
49 X("fcc mailbox", 0, FCCSW) \
50 X("width columns", 0, WIDTHSW) \
51 X("subject text", 0, SUBJECTSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(COMP
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(COMP
, switches
);
61 #define DISPO_SWITCHES \
63 X("replace", 0, YESW) \
65 X("list", 0, LISTDSW) \
66 X("refile +folder", 0, REFILSW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(DISPO
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
77 static struct swit aqrul
[] = {
79 { "replace", 0, YESW
},
81 { "list", 0, LISTDSW
},
82 { "refile", 0, REFILSW
},
87 main (int argc
, char **argv
)
93 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
94 int outputlinelen
= OUTPUTLINELEN
;
95 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
96 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
97 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
98 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
100 char drft
[BUFSIZ
], **argp
, **arguments
;
101 struct msgs
*mp
= NULL
;
105 if (nmh_init(argv
[0], true, true)) { return 1; }
107 arguments
= getarguments (invo_name
, argc
, argv
, 1);
110 while ((cp
= *argp
++)) {
112 switch (smatch (++cp
, switches
)) {
114 ambigsw (cp
, switches
);
117 die("-%s unknown", cp
);
120 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
122 print_help (buf
, switches
, 1);
125 print_version(invo_name
);
129 if (!(ed
= *argp
++) || *ed
== '-')
130 die("missing argument to %s", argp
[-2]);
138 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
139 die("missing argument to %s", argp
[-2]);
151 if (!(form
= *argp
++) || *form
== '-')
152 die("missing argument to %s", argp
[-2]);
162 case FILESW
: /* compatibility */
164 die("only one file at a time!");
165 if (!(file
= *argp
++) || *file
== '-')
166 die("missing argument to %s", argp
[-2]);
172 die("only one draft folder at a time!");
173 if (!(cp
= *argp
++) || *cp
== '-')
174 die("missing argument to %s", argp
[-2]);
175 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
176 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
180 die("only one draft message at a time!");
181 if (!(file
= *argp
++) || *file
== '-')
182 die("missing argument to %s", argp
[-2]);
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
192 to
= addlist(to
, cp
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
198 cc
= addlist(cc
, cp
);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 die("missing argument to %s", argp
[-2]);
204 from
= addlist(from
, cp
);
208 if (!(cp
= *argp
++) || *cp
== '-')
209 die("missing argument to %s", argp
[-2]);
212 cp
= dp
= path(cp
+ 1, TSUBCWF
);
213 fcc
= addlist(fcc
, cp
);
218 if (!(cp
= *argp
++) || *cp
== '-')
219 die("missing argument to %s", argp
[-2]);
220 if ((outputlinelen
= atoi(cp
)) < 10)
221 die("impossible width %d", outputlinelen
);
225 if (!(cp
= *argp
++) || *cp
== '-')
226 die("missing argument to %s", argp
[-2]);
231 if (*cp
== '+' || *cp
== '@') {
233 die("only one folder at a time!");
234 folder
= pluspath (cp
);
237 die("only one message at a time!");
242 cwd
= mh_xstrdup(pwd ());
244 if (!context_find ("path"))
245 free (path ("./", TFOLDER
));
248 * Check if we are using a draft folder
249 * and have specified a message in it.
251 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
255 if (form
&& (folder
|| msg
))
256 die("can't mix forms and folders/msgs");
262 * Use a message as the "form" for the new message.
267 folder
= getfolder (1);
268 maildir
= m_maildir (folder
);
270 if (chdir (maildir
) == NOTOK
)
271 adios (maildir
, "unable to change directory to");
273 /* read folder and create message structure */
274 if (!(mp
= folder_read (folder
, 1)))
275 die("unable to read folder %s", folder
);
277 /* check for empty folder */
279 die("no messages in %s", folder
);
281 /* parse the message range/sequence/name and set SELECTED */
282 if (!m_convert (mp
, msg
))
284 seq_setprev (mp
); /* set the previous-sequence */
288 die("only one message at a time!");
290 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
291 adios (form
, "unable to open message");
298 cp
= new_fs(form
, NULL
, NULL
);
299 format_len
= strlen(cp
);
300 fmt_compile(cp
, &fmt
, 1);
303 * Set up any components that were fed to us on the command line
307 cptr
= fmt_findcomp("from");
312 cptr
= fmt_findcomp("to");
317 cptr
= fmt_findcomp("cc");
322 cptr
= fmt_findcomp("fcc");
327 cptr
= fmt_findcomp("subject");
329 cptr
->c_text
= subject
;
334 strncpy (drft
, build
? m_maildir ("draft")
335 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
338 * Check if we have an existing draft
340 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
341 i
= fdcompare (in
, out
);
345 * If we have given -use flag, or if the
346 * draft is just the same as the components
347 * file, then no need to ask any questions.
352 if (stat (drft
, &st
) == NOTOK
)
353 adios (drft
, "unable to stat");
354 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
355 for (i
= LISTDSW
; i
!= YESW
;) {
356 if (!(argp
= read_switch_multiword ("\nDisposition? ",
357 isdf
? aqrunl
: aqrul
)))
359 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
372 showfile (++argp
, drft
);
375 if (refile (++argp
, drft
) == 0)
385 adios (drft
, "unable to open");
388 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
389 adios (drft
, "unable to create");
393 i
= format_len
+ 1024;
394 scanl
= charstring_create (i
+ 2);
398 dat
[3] = outputlinelen
;
400 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
401 if (write(out
, charstring_buffer (scanl
),
402 charstring_bytes (scanl
)) < 0) {
403 advise (drft
, "write");
405 charstring_free(scanl
);
407 cpydata (in
, out
, form
, drft
);
413 context_save (); /* save the context file */
417 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);