]>
diplodocus.org Git - nmh/blob - uip/comp.c
f543db6db6efe283667647e3d187fdf67b2dc1fd
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/fdcompare.h"
10 #include "sbr/folder_read.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_find.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
19 #include "h/fmt_scan.h"
21 #include "sbr/m_maildir.h"
24 #define COMP_SWITCHES \
25 X("draftfolder +folder", 0, DFOLDSW) \
26 X("draftmessage msg", 0, DMSGSW) \
27 X("nodraftfolder", 0, NDFLDSW) \
28 X("editor editor", 0, EDITRSW) \
29 X("noedit", 0, NEDITSW) \
30 X("file file", 0, FILESW) \
31 X("form formfile", 0, FORMSW) \
33 X("nouse", 0, NUSESW) \
34 X("whatnowproc program", 0, WHATSW) \
35 X("nowhatnowproc", 0, NWHATSW) \
36 X("build", 5, BILDSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("to address", 0, TOSW) \
40 X("cc address", 0, CCSW) \
41 X("from address", 0, FROMSW) \
42 X("fcc mailbox", 0, FCCSW) \
43 X("width columns", 0, WIDTHSW) \
44 X("subject text", 0, SUBJECTSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(COMP
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(COMP
, switches
);
54 #define DISPO_SWITCHES \
56 X("replace", 0, YESW) \
58 X("list", 0, LISTDSW) \
59 X("refile +folder", 0, REFILSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(DISPO
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
70 static struct swit aqrul
[] = {
72 { "replace", 0, YESW
},
74 { "list", 0, LISTDSW
},
75 { "refile", 0, REFILSW
},
80 main (int argc
, char **argv
)
86 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
87 int outputlinelen
= OUTPUTLINELEN
;
88 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
89 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
90 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
91 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
93 char drft
[BUFSIZ
], **argp
, **arguments
;
94 struct msgs
*mp
= NULL
;
98 if (nmh_init(argv
[0], true, true)) { return 1; }
100 arguments
= getarguments (invo_name
, argc
, argv
, 1);
103 while ((cp
= *argp
++)) {
105 switch (smatch (++cp
, switches
)) {
107 ambigsw (cp
, switches
);
110 die("-%s unknown", cp
);
113 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
115 print_help (buf
, switches
, 1);
118 print_version(invo_name
);
122 if (!(ed
= *argp
++) || *ed
== '-')
123 die("missing argument to %s", argp
[-2]);
131 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
132 die("missing argument to %s", argp
[-2]);
144 if (!(form
= *argp
++) || *form
== '-')
145 die("missing argument to %s", argp
[-2]);
155 case FILESW
: /* compatibility */
157 die("only one file at a time!");
158 if (!(file
= *argp
++) || *file
== '-')
159 die("missing argument to %s", argp
[-2]);
165 die("only one draft folder at a time!");
166 if (!(cp
= *argp
++) || *cp
== '-')
167 die("missing argument to %s", argp
[-2]);
168 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
169 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
173 die("only one draft message at a time!");
174 if (!(file
= *argp
++) || *file
== '-')
175 die("missing argument to %s", argp
[-2]);
183 if (!(cp
= *argp
++) || *cp
== '-')
184 die("missing argument to %s", argp
[-2]);
185 to
= addlist(to
, cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 cc
= addlist(cc
, cp
);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 die("missing argument to %s", argp
[-2]);
197 from
= addlist(from
, cp
);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
205 cp
= dp
= path(cp
+ 1, TSUBCWF
);
206 fcc
= addlist(fcc
, cp
);
211 if (!(cp
= *argp
++) || *cp
== '-')
212 die("missing argument to %s", argp
[-2]);
213 if ((outputlinelen
= atoi(cp
)) < 10)
214 die("impossible width %d", outputlinelen
);
218 if (!(cp
= *argp
++) || *cp
== '-')
219 die("missing argument to %s", argp
[-2]);
224 if (*cp
== '+' || *cp
== '@') {
226 die("only one folder at a time!");
227 folder
= pluspath (cp
);
230 die("only one message at a time!");
235 cwd
= mh_xstrdup(pwd ());
237 if (!context_find ("path"))
238 free (path ("./", TFOLDER
));
241 * Check if we are using a draft folder
242 * and have specified a message in it.
244 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
248 if (form
&& (folder
|| msg
))
249 die("can't mix forms and folders/msgs");
255 * Use a message as the "form" for the new message.
260 folder
= getfolder (1);
261 maildir
= m_maildir (folder
);
263 if (chdir (maildir
) == NOTOK
)
264 adios (maildir
, "unable to change directory to");
266 /* read folder and create message structure */
267 if (!(mp
= folder_read (folder
, 1)))
268 die("unable to read folder %s", folder
);
270 /* check for empty folder */
272 die("no messages in %s", folder
);
274 /* parse the message range/sequence/name and set SELECTED */
275 if (!m_convert (mp
, msg
))
277 seq_setprev (mp
); /* set the previous-sequence */
281 die("only one message at a time!");
283 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
284 adios (form
, "unable to open message");
291 cp
= new_fs(form
, NULL
, NULL
);
292 format_len
= strlen(cp
);
293 fmt_compile(cp
, &fmt
, 1);
296 * Set up any components that were fed to us on the command line
300 cptr
= fmt_findcomp("from");
305 cptr
= fmt_findcomp("to");
310 cptr
= fmt_findcomp("cc");
315 cptr
= fmt_findcomp("fcc");
320 cptr
= fmt_findcomp("subject");
322 cptr
->c_text
= subject
;
327 strncpy (drft
, build
? m_maildir ("draft")
328 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
331 * Check if we have an existing draft
333 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
334 i
= fdcompare (in
, out
);
338 * If we have given -use flag, or if the
339 * draft is just the same as the components
340 * file, then no need to ask any questions.
345 if (stat (drft
, &st
) == NOTOK
)
346 adios (drft
, "unable to stat");
347 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
348 for (i
= LISTDSW
; i
!= YESW
;) {
349 if (!(argp
= read_switch_multiword ("\nDisposition? ",
350 isdf
? aqrunl
: aqrul
)))
352 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
365 showfile (++argp
, drft
);
368 if (refile (++argp
, drft
) == 0)
378 adios (drft
, "unable to open");
381 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
382 adios (drft
, "unable to create");
386 i
= format_len
+ 1024;
387 scanl
= charstring_create (i
+ 2);
391 dat
[3] = outputlinelen
;
393 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
394 if (write(out
, charstring_buffer (scanl
),
395 charstring_bytes (scanl
)) < 0) {
396 advise (drft
, "write");
398 charstring_free(scanl
);
400 cpydata (in
, out
, form
, drft
);
406 context_save (); /* save the context file */
410 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);