]>
diplodocus.org Git - nmh/blob - uip/comp.c
ce67c1d7bf6df3a4b14c8e4ac25295142fecffaf
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/seq_save.h"
10 #include "sbr/showfile.h"
11 #include "sbr/smatch.h"
12 #include "sbr/refile.h"
13 #include "sbr/cpydata.h"
14 #include "sbr/m_draft.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/fdcompare.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_find.h"
21 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/error.h"
27 #include "h/fmt_scan.h"
29 #include "sbr/m_maildir.h"
32 #define COMP_SWITCHES \
33 X("draftfolder +folder", 0, DFOLDSW) \
34 X("draftmessage msg", 0, DMSGSW) \
35 X("nodraftfolder", 0, NDFLDSW) \
36 X("editor editor", 0, EDITRSW) \
37 X("noedit", 0, NEDITSW) \
38 X("file file", 0, FILESW) \
39 X("form formfile", 0, FORMSW) \
41 X("nouse", 0, NUSESW) \
42 X("whatnowproc program", 0, WHATSW) \
43 X("nowhatnowproc", 0, NWHATSW) \
44 X("build", 5, BILDSW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
47 X("to address", 0, TOSW) \
48 X("cc address", 0, CCSW) \
49 X("from address", 0, FROMSW) \
50 X("fcc mailbox", 0, FCCSW) \
51 X("width columns", 0, WIDTHSW) \
52 X("subject text", 0, SUBJECTSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(COMP
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(COMP
, switches
);
62 #define DISPO_SWITCHES \
64 X("replace", 0, YESW) \
66 X("list", 0, LISTDSW) \
67 X("refile +folder", 0, REFILSW) \
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(DISPO
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
78 static struct swit aqrul
[] = {
80 { "replace", 0, YESW
},
82 { "list", 0, LISTDSW
},
83 { "refile", 0, REFILSW
},
88 main (int argc
, char **argv
)
94 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
95 int outputlinelen
= OUTPUTLINELEN
;
96 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
97 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
98 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
99 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
100 char *subject
= NULL
;
101 char drft
[BUFSIZ
], **argp
, **arguments
;
102 struct msgs
*mp
= NULL
;
106 if (nmh_init(argv
[0], true, true)) { return 1; }
108 arguments
= getarguments (invo_name
, argc
, argv
, 1);
111 while ((cp
= *argp
++)) {
113 switch (smatch (++cp
, switches
)) {
115 ambigsw (cp
, switches
);
118 die("-%s unknown", cp
);
121 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
123 print_help (buf
, switches
, 1);
126 print_version(invo_name
);
130 if (!(ed
= *argp
++) || *ed
== '-')
131 die("missing argument to %s", argp
[-2]);
139 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
140 die("missing argument to %s", argp
[-2]);
152 if (!(form
= *argp
++) || *form
== '-')
153 die("missing argument to %s", argp
[-2]);
163 case FILESW
: /* compatibility */
165 die("only one file at a time!");
166 if (!(file
= *argp
++) || *file
== '-')
167 die("missing argument to %s", argp
[-2]);
173 die("only one draft folder at a time!");
174 if (!(cp
= *argp
++) || *cp
== '-')
175 die("missing argument to %s", argp
[-2]);
176 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
177 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
181 die("only one draft message at a time!");
182 if (!(file
= *argp
++) || *file
== '-')
183 die("missing argument to %s", argp
[-2]);
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
193 to
= addlist(to
, cp
);
197 if (!(cp
= *argp
++) || *cp
== '-')
198 die("missing argument to %s", argp
[-2]);
199 cc
= addlist(cc
, cp
);
203 if (!(cp
= *argp
++) || *cp
== '-')
204 die("missing argument to %s", argp
[-2]);
205 from
= addlist(from
, cp
);
209 if (!(cp
= *argp
++) || *cp
== '-')
210 die("missing argument to %s", argp
[-2]);
213 cp
= dp
= path(cp
+ 1, TSUBCWF
);
214 fcc
= addlist(fcc
, cp
);
219 if (!(cp
= *argp
++) || *cp
== '-')
220 die("missing argument to %s", argp
[-2]);
221 if ((outputlinelen
= atoi(cp
)) < 10)
222 die("impossible width %d", outputlinelen
);
226 if (!(cp
= *argp
++) || *cp
== '-')
227 die("missing argument to %s", argp
[-2]);
232 if (*cp
== '+' || *cp
== '@') {
234 die("only one folder at a time!");
235 folder
= pluspath (cp
);
238 die("only one message at a time!");
243 cwd
= mh_xstrdup(pwd ());
245 if (!context_find ("path"))
246 free (path ("./", TFOLDER
));
249 * Check if we are using a draft folder
250 * and have specified a message in it.
252 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
256 if (form
&& (folder
|| msg
))
257 die("can't mix forms and folders/msgs");
263 * Use a message as the "form" for the new message.
268 folder
= getfolder (1);
269 maildir
= m_maildir (folder
);
271 if (chdir (maildir
) == NOTOK
)
272 adios (maildir
, "unable to change directory to");
274 /* read folder and create message structure */
275 if (!(mp
= folder_read (folder
, 1)))
276 die("unable to read folder %s", folder
);
278 /* check for empty folder */
280 die("no messages in %s", folder
);
282 /* parse the message range/sequence/name and set SELECTED */
283 if (!m_convert (mp
, msg
))
285 seq_setprev (mp
); /* set the previous-sequence */
289 die("only one message at a time!");
291 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
292 adios (form
, "unable to open message");
299 cp
= new_fs(form
, NULL
, NULL
);
300 format_len
= strlen(cp
);
301 fmt_compile(cp
, &fmt
, 1);
304 * Set up any components that were fed to us on the command line
308 cptr
= fmt_findcomp("from");
313 cptr
= fmt_findcomp("to");
318 cptr
= fmt_findcomp("cc");
323 cptr
= fmt_findcomp("fcc");
328 cptr
= fmt_findcomp("subject");
330 cptr
->c_text
= subject
;
335 strncpy (drft
, build
? m_maildir ("draft")
336 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
339 * Check if we have an existing draft
341 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
342 i
= fdcompare (in
, out
);
346 * If we have given -use flag, or if the
347 * draft is just the same as the components
348 * file, then no need to ask any questions.
353 if (stat (drft
, &st
) == NOTOK
)
354 adios (drft
, "unable to stat");
355 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
356 for (i
= LISTDSW
; i
!= YESW
;) {
357 if (!(argp
= read_switch_multiword ("\nDisposition? ",
358 isdf
? aqrunl
: aqrul
)))
360 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
373 showfile (++argp
, drft
);
376 if (refile (++argp
, drft
) == 0)
386 adios (drft
, "unable to open");
389 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
390 adios (drft
, "unable to create");
394 i
= format_len
+ 1024;
395 scanl
= charstring_create (i
+ 2);
399 dat
[3] = outputlinelen
;
401 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
402 if (write(out
, charstring_buffer (scanl
),
403 charstring_bytes (scanl
)) < 0) {
404 advise (drft
, "write");
406 charstring_free(scanl
);
408 cpydata (in
, out
, form
, drft
);
414 context_save (); /* save the context file */
418 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);