]>
diplodocus.org Git - nmh/blob - uip/comp.c
aadc17ed978a920827deca702d30bc17713f821f
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/print_help.h"
10 #include "sbr/error.h"
12 #include "h/fmt_scan.h"
14 #include "sbr/m_maildir.h"
17 #define COMP_SWITCHES \
18 X("draftfolder +folder", 0, DFOLDSW) \
19 X("draftmessage msg", 0, DMSGSW) \
20 X("nodraftfolder", 0, NDFLDSW) \
21 X("editor editor", 0, EDITRSW) \
22 X("noedit", 0, NEDITSW) \
23 X("file file", 0, FILESW) \
24 X("form formfile", 0, FORMSW) \
26 X("nouse", 0, NUSESW) \
27 X("whatnowproc program", 0, WHATSW) \
28 X("nowhatnowproc", 0, NWHATSW) \
29 X("build", 5, BILDSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
32 X("to address", 0, TOSW) \
33 X("cc address", 0, CCSW) \
34 X("from address", 0, FROMSW) \
35 X("fcc mailbox", 0, FCCSW) \
36 X("width columns", 0, WIDTHSW) \
37 X("subject text", 0, SUBJECTSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(COMP
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(COMP
, switches
);
47 #define DISPO_SWITCHES \
49 X("replace", 0, YESW) \
51 X("list", 0, LISTDSW) \
52 X("refile +folder", 0, REFILSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(DISPO
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
63 static struct swit aqrul
[] = {
65 { "replace", 0, YESW
},
67 { "list", 0, LISTDSW
},
68 { "refile", 0, REFILSW
},
73 main (int argc
, char **argv
)
79 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
80 int outputlinelen
= OUTPUTLINELEN
;
81 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
82 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
83 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
84 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
86 char drft
[BUFSIZ
], **argp
, **arguments
;
87 struct msgs
*mp
= NULL
;
91 if (nmh_init(argv
[0], true, true)) { return 1; }
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 die("-%s unknown", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
115 if (!(ed
= *argp
++) || *ed
== '-')
116 die("missing argument to %s", argp
[-2]);
124 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
125 die("missing argument to %s", argp
[-2]);
137 if (!(form
= *argp
++) || *form
== '-')
138 die("missing argument to %s", argp
[-2]);
148 case FILESW
: /* compatibility */
150 die("only one file at a time!");
151 if (!(file
= *argp
++) || *file
== '-')
152 die("missing argument to %s", argp
[-2]);
158 die("only one draft folder at a time!");
159 if (!(cp
= *argp
++) || *cp
== '-')
160 die("missing argument to %s", argp
[-2]);
161 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
162 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
166 die("only one draft message at a time!");
167 if (!(file
= *argp
++) || *file
== '-')
168 die("missing argument to %s", argp
[-2]);
176 if (!(cp
= *argp
++) || *cp
== '-')
177 die("missing argument to %s", argp
[-2]);
178 to
= addlist(to
, cp
);
182 if (!(cp
= *argp
++) || *cp
== '-')
183 die("missing argument to %s", argp
[-2]);
184 cc
= addlist(cc
, cp
);
188 if (!(cp
= *argp
++) || *cp
== '-')
189 die("missing argument to %s", argp
[-2]);
190 from
= addlist(from
, cp
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
198 cp
= dp
= path(cp
+ 1, TSUBCWF
);
199 fcc
= addlist(fcc
, cp
);
204 if (!(cp
= *argp
++) || *cp
== '-')
205 die("missing argument to %s", argp
[-2]);
206 if ((outputlinelen
= atoi(cp
)) < 10)
207 die("impossible width %d", outputlinelen
);
211 if (!(cp
= *argp
++) || *cp
== '-')
212 die("missing argument to %s", argp
[-2]);
217 if (*cp
== '+' || *cp
== '@') {
219 die("only one folder at a time!");
220 folder
= pluspath (cp
);
223 die("only one message at a time!");
228 cwd
= mh_xstrdup(pwd ());
230 if (!context_find ("path"))
231 free (path ("./", TFOLDER
));
234 * Check if we are using a draft folder
235 * and have specified a message in it.
237 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
241 if (form
&& (folder
|| msg
))
242 die("can't mix forms and folders/msgs");
248 * Use a message as the "form" for the new message.
253 folder
= getfolder (1);
254 maildir
= m_maildir (folder
);
256 if (chdir (maildir
) == NOTOK
)
257 adios (maildir
, "unable to change directory to");
259 /* read folder and create message structure */
260 if (!(mp
= folder_read (folder
, 1)))
261 die("unable to read folder %s", folder
);
263 /* check for empty folder */
265 die("no messages in %s", folder
);
267 /* parse the message range/sequence/name and set SELECTED */
268 if (!m_convert (mp
, msg
))
270 seq_setprev (mp
); /* set the previous-sequence */
274 die("only one message at a time!");
276 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
277 adios (form
, "unable to open message");
284 cp
= new_fs(form
, NULL
, NULL
);
285 format_len
= strlen(cp
);
286 fmt_compile(cp
, &fmt
, 1);
289 * Set up any components that were fed to us on the command line
293 cptr
= fmt_findcomp("from");
298 cptr
= fmt_findcomp("to");
303 cptr
= fmt_findcomp("cc");
308 cptr
= fmt_findcomp("fcc");
313 cptr
= fmt_findcomp("subject");
315 cptr
->c_text
= subject
;
320 strncpy (drft
, build
? m_maildir ("draft")
321 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
324 * Check if we have an existing draft
326 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
327 i
= fdcompare (in
, out
);
331 * If we have given -use flag, or if the
332 * draft is just the same as the components
333 * file, then no need to ask any questions.
338 if (stat (drft
, &st
) == NOTOK
)
339 adios (drft
, "unable to stat");
340 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
341 for (i
= LISTDSW
; i
!= YESW
;) {
342 if (!(argp
= read_switch_multiword ("\nDisposition? ",
343 isdf
? aqrunl
: aqrul
)))
345 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
358 showfile (++argp
, drft
);
361 if (refile (++argp
, drft
) == 0)
371 adios (drft
, "unable to open");
374 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
375 adios (drft
, "unable to create");
379 i
= format_len
+ 1024;
380 scanl
= charstring_create (i
+ 2);
384 dat
[3] = outputlinelen
;
386 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
387 if (write(out
, charstring_buffer (scanl
),
388 charstring_bytes (scanl
)) < 0) {
389 advise (drft
, "write");
391 charstring_free(scanl
);
393 cpydata (in
, out
, form
, drft
);
399 context_save (); /* save the context file */
403 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);