]>
diplodocus.org Git - nmh/blob - uip/comp.c
027ba3db88d565d8c750013ea27a761ba810faab
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/m_draft.h"
10 #include "sbr/m_convert.h"
11 #include "sbr/getfolder.h"
12 #include "sbr/fdcompare.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_find.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/error.h"
22 #include "h/fmt_scan.h"
24 #include "sbr/m_maildir.h"
27 #define COMP_SWITCHES \
28 X("draftfolder +folder", 0, DFOLDSW) \
29 X("draftmessage msg", 0, DMSGSW) \
30 X("nodraftfolder", 0, NDFLDSW) \
31 X("editor editor", 0, EDITRSW) \
32 X("noedit", 0, NEDITSW) \
33 X("file file", 0, FILESW) \
34 X("form formfile", 0, FORMSW) \
36 X("nouse", 0, NUSESW) \
37 X("whatnowproc program", 0, WHATSW) \
38 X("nowhatnowproc", 0, NWHATSW) \
39 X("build", 5, BILDSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
42 X("to address", 0, TOSW) \
43 X("cc address", 0, CCSW) \
44 X("from address", 0, FROMSW) \
45 X("fcc mailbox", 0, FCCSW) \
46 X("width columns", 0, WIDTHSW) \
47 X("subject text", 0, SUBJECTSW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(COMP
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(COMP
, switches
);
57 #define DISPO_SWITCHES \
59 X("replace", 0, YESW) \
61 X("list", 0, LISTDSW) \
62 X("refile +folder", 0, REFILSW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(DISPO
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
73 static struct swit aqrul
[] = {
75 { "replace", 0, YESW
},
77 { "list", 0, LISTDSW
},
78 { "refile", 0, REFILSW
},
83 main (int argc
, char **argv
)
89 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
90 int outputlinelen
= OUTPUTLINELEN
;
91 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
92 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
93 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
94 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
96 char drft
[BUFSIZ
], **argp
, **arguments
;
97 struct msgs
*mp
= NULL
;
101 if (nmh_init(argv
[0], true, true)) { return 1; }
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
106 while ((cp
= *argp
++)) {
108 switch (smatch (++cp
, switches
)) {
110 ambigsw (cp
, switches
);
113 die("-%s unknown", cp
);
116 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
118 print_help (buf
, switches
, 1);
121 print_version(invo_name
);
125 if (!(ed
= *argp
++) || *ed
== '-')
126 die("missing argument to %s", argp
[-2]);
134 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
135 die("missing argument to %s", argp
[-2]);
147 if (!(form
= *argp
++) || *form
== '-')
148 die("missing argument to %s", argp
[-2]);
158 case FILESW
: /* compatibility */
160 die("only one file at a time!");
161 if (!(file
= *argp
++) || *file
== '-')
162 die("missing argument to %s", argp
[-2]);
168 die("only one draft folder at a time!");
169 if (!(cp
= *argp
++) || *cp
== '-')
170 die("missing argument to %s", argp
[-2]);
171 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
172 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
176 die("only one draft message at a time!");
177 if (!(file
= *argp
++) || *file
== '-')
178 die("missing argument to %s", argp
[-2]);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
188 to
= addlist(to
, cp
);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 cc
= addlist(cc
, cp
);
198 if (!(cp
= *argp
++) || *cp
== '-')
199 die("missing argument to %s", argp
[-2]);
200 from
= addlist(from
, cp
);
204 if (!(cp
= *argp
++) || *cp
== '-')
205 die("missing argument to %s", argp
[-2]);
208 cp
= dp
= path(cp
+ 1, TSUBCWF
);
209 fcc
= addlist(fcc
, cp
);
214 if (!(cp
= *argp
++) || *cp
== '-')
215 die("missing argument to %s", argp
[-2]);
216 if ((outputlinelen
= atoi(cp
)) < 10)
217 die("impossible width %d", outputlinelen
);
221 if (!(cp
= *argp
++) || *cp
== '-')
222 die("missing argument to %s", argp
[-2]);
227 if (*cp
== '+' || *cp
== '@') {
229 die("only one folder at a time!");
230 folder
= pluspath (cp
);
233 die("only one message at a time!");
238 cwd
= mh_xstrdup(pwd ());
240 if (!context_find ("path"))
241 free (path ("./", TFOLDER
));
244 * Check if we are using a draft folder
245 * and have specified a message in it.
247 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
251 if (form
&& (folder
|| msg
))
252 die("can't mix forms and folders/msgs");
258 * Use a message as the "form" for the new message.
263 folder
= getfolder (1);
264 maildir
= m_maildir (folder
);
266 if (chdir (maildir
) == NOTOK
)
267 adios (maildir
, "unable to change directory to");
269 /* read folder and create message structure */
270 if (!(mp
= folder_read (folder
, 1)))
271 die("unable to read folder %s", folder
);
273 /* check for empty folder */
275 die("no messages in %s", folder
);
277 /* parse the message range/sequence/name and set SELECTED */
278 if (!m_convert (mp
, msg
))
280 seq_setprev (mp
); /* set the previous-sequence */
284 die("only one message at a time!");
286 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
287 adios (form
, "unable to open message");
294 cp
= new_fs(form
, NULL
, NULL
);
295 format_len
= strlen(cp
);
296 fmt_compile(cp
, &fmt
, 1);
299 * Set up any components that were fed to us on the command line
303 cptr
= fmt_findcomp("from");
308 cptr
= fmt_findcomp("to");
313 cptr
= fmt_findcomp("cc");
318 cptr
= fmt_findcomp("fcc");
323 cptr
= fmt_findcomp("subject");
325 cptr
->c_text
= subject
;
330 strncpy (drft
, build
? m_maildir ("draft")
331 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
334 * Check if we have an existing draft
336 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
337 i
= fdcompare (in
, out
);
341 * If we have given -use flag, or if the
342 * draft is just the same as the components
343 * file, then no need to ask any questions.
348 if (stat (drft
, &st
) == NOTOK
)
349 adios (drft
, "unable to stat");
350 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
351 for (i
= LISTDSW
; i
!= YESW
;) {
352 if (!(argp
= read_switch_multiword ("\nDisposition? ",
353 isdf
? aqrunl
: aqrul
)))
355 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
368 showfile (++argp
, drft
);
371 if (refile (++argp
, drft
) == 0)
381 adios (drft
, "unable to open");
384 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
385 adios (drft
, "unable to create");
389 i
= format_len
+ 1024;
390 scanl
= charstring_create (i
+ 2);
394 dat
[3] = outputlinelen
;
396 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
397 if (write(out
, charstring_buffer (scanl
),
398 charstring_bytes (scanl
)) < 0) {
399 advise (drft
, "write");
401 charstring_free(scanl
);
403 cpydata (in
, out
, form
, drft
);
409 context_save (); /* save the context file */
413 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);