]>
diplodocus.org Git - nmh/blob - uip/comp.c
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/fmt_new.h"
10 #include "whatnowproc.h"
11 #include "sbr/m_name.h"
12 #include "sbr/m_gmprot.h"
13 #include "sbr/getarguments.h"
14 #include "sbr/read_switch_multiword.h"
15 #include "sbr/seq_setprev.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/showfile.h"
18 #include "sbr/smatch.h"
19 #include "sbr/refile.h"
20 #include "sbr/cpydata.h"
21 #include "sbr/m_draft.h"
22 #include "sbr/m_convert.h"
23 #include "sbr/getfolder.h"
24 #include "sbr/fdcompare.h"
25 #include "sbr/folder_read.h"
26 #include "sbr/context_save.h"
27 #include "sbr/context_find.h"
28 #include "sbr/ambigsw.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/error.h"
34 #include "h/fmt_scan.h"
36 #include "sbr/m_maildir.h"
39 #define COMP_SWITCHES \
40 X("draftfolder +folder", 0, DFOLDSW) \
41 X("draftmessage msg", 0, DMSGSW) \
42 X("nodraftfolder", 0, NDFLDSW) \
43 X("editor editor", 0, EDITRSW) \
44 X("noedit", 0, NEDITSW) \
45 X("file file", 0, FILESW) \
46 X("form formfile", 0, FORMSW) \
48 X("nouse", 0, NUSESW) \
49 X("whatnowproc program", 0, WHATSW) \
50 X("nowhatnowproc", 0, NWHATSW) \
51 X("build", 5, BILDSW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
54 X("to address", 0, TOSW) \
55 X("cc address", 0, CCSW) \
56 X("from address", 0, FROMSW) \
57 X("fcc mailbox", 0, FCCSW) \
58 X("width columns", 0, WIDTHSW) \
59 X("subject text", 0, SUBJECTSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(COMP
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(COMP
, switches
);
69 #define DISPO_SWITCHES \
71 X("replace", 0, YESW) \
73 X("list", 0, LISTDSW) \
74 X("refile +folder", 0, REFILSW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(DISPO
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
85 static struct swit aqrul
[] = {
87 { "replace", 0, YESW
},
89 { "list", 0, LISTDSW
},
90 { "refile", 0, REFILSW
},
95 main (int argc
, char **argv
)
101 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
102 int outputlinelen
= OUTPUTLINELEN
;
103 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
104 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
105 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
106 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
107 char *subject
= NULL
;
108 char drft
[BUFSIZ
], **argp
, **arguments
;
109 struct msgs
*mp
= NULL
;
113 if (nmh_init(argv
[0], true, true)) { return 1; }
115 arguments
= getarguments (invo_name
, argc
, argv
, 1);
118 while ((cp
= *argp
++)) {
120 switch (smatch (++cp
, switches
)) {
122 ambigsw (cp
, switches
);
125 die("-%s unknown", cp
);
128 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
130 print_help (buf
, switches
, 1);
133 print_version(invo_name
);
137 if (!(ed
= *argp
++) || *ed
== '-')
138 die("missing argument to %s", argp
[-2]);
146 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
147 die("missing argument to %s", argp
[-2]);
159 if (!(form
= *argp
++) || *form
== '-')
160 die("missing argument to %s", argp
[-2]);
170 case FILESW
: /* compatibility */
172 die("only one file at a time!");
173 if (!(file
= *argp
++) || *file
== '-')
174 die("missing argument to %s", argp
[-2]);
180 die("only one draft folder at a time!");
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
184 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
188 die("only one draft message at a time!");
189 if (!(file
= *argp
++) || *file
== '-')
190 die("missing argument to %s", argp
[-2]);
198 if (!(cp
= *argp
++) || *cp
== '-')
199 die("missing argument to %s", argp
[-2]);
200 to
= addlist(to
, cp
);
204 if (!(cp
= *argp
++) || *cp
== '-')
205 die("missing argument to %s", argp
[-2]);
206 cc
= addlist(cc
, cp
);
210 if (!(cp
= *argp
++) || *cp
== '-')
211 die("missing argument to %s", argp
[-2]);
212 from
= addlist(from
, cp
);
216 if (!(cp
= *argp
++) || *cp
== '-')
217 die("missing argument to %s", argp
[-2]);
220 cp
= dp
= path(cp
+ 1, TSUBCWF
);
221 fcc
= addlist(fcc
, cp
);
226 if (!(cp
= *argp
++) || *cp
== '-')
227 die("missing argument to %s", argp
[-2]);
228 if ((outputlinelen
= atoi(cp
)) < 10)
229 die("impossible width %d", outputlinelen
);
233 if (!(cp
= *argp
++) || *cp
== '-')
234 die("missing argument to %s", argp
[-2]);
239 if (*cp
== '+' || *cp
== '@') {
241 die("only one folder at a time!");
242 folder
= pluspath (cp
);
245 die("only one message at a time!");
250 cwd
= mh_xstrdup(pwd ());
252 if (!context_find ("path"))
253 free (path ("./", TFOLDER
));
256 * Check if we are using a draft folder
257 * and have specified a message in it.
259 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
263 if (form
&& (folder
|| msg
))
264 die("can't mix forms and folders/msgs");
270 * Use a message as the "form" for the new message.
275 folder
= getfolder (1);
276 maildir
= m_maildir (folder
);
278 if (chdir (maildir
) == NOTOK
)
279 adios (maildir
, "unable to change directory to");
281 /* read folder and create message structure */
282 if (!(mp
= folder_read (folder
, 1)))
283 die("unable to read folder %s", folder
);
285 /* check for empty folder */
287 die("no messages in %s", folder
);
289 /* parse the message range/sequence/name and set SELECTED */
290 if (!m_convert (mp
, msg
))
292 seq_setprev (mp
); /* set the previous-sequence */
296 die("only one message at a time!");
298 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
299 adios (form
, "unable to open message");
306 cp
= new_fs(form
, NULL
, NULL
);
307 format_len
= strlen(cp
);
308 fmt_compile(cp
, &fmt
, 1);
311 * Set up any components that were fed to us on the command line
315 cptr
= fmt_findcomp("from");
320 cptr
= fmt_findcomp("to");
325 cptr
= fmt_findcomp("cc");
330 cptr
= fmt_findcomp("fcc");
335 cptr
= fmt_findcomp("subject");
337 cptr
->c_text
= subject
;
342 strncpy (drft
, build
? m_maildir ("draft")
343 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
346 * Check if we have an existing draft
348 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
349 i
= fdcompare (in
, out
);
353 * If we have given -use flag, or if the
354 * draft is just the same as the components
355 * file, then no need to ask any questions.
360 if (stat (drft
, &st
) == NOTOK
)
361 adios (drft
, "unable to stat");
362 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
363 for (i
= LISTDSW
; i
!= YESW
;) {
364 if (!(argp
= read_switch_multiword ("\nDisposition? ",
365 isdf
? aqrunl
: aqrul
)))
367 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
380 showfile (++argp
, drft
);
383 if (refile (++argp
, drft
) == 0)
393 adios (drft
, "unable to open");
396 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
397 adios (drft
, "unable to create");
401 i
= format_len
+ 1024;
402 scanl
= charstring_create (i
+ 2);
406 dat
[3] = outputlinelen
;
408 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
409 if (write(out
, charstring_buffer (scanl
),
410 charstring_bytes (scanl
)) < 0) {
411 advise (drft
, "write");
413 charstring_free(scanl
);
415 cpydata (in
, out
, form
, drft
);
421 context_save (); /* save the context file */
425 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);