]>
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.
10 #include <h/fmt_scan.h>
12 #include "sbr/m_maildir.h"
15 #define COMP_SWITCHES \
16 X("draftfolder +folder", 0, DFOLDSW) \
17 X("draftmessage msg", 0, DMSGSW) \
18 X("nodraftfolder", 0, NDFLDSW) \
19 X("editor editor", 0, EDITRSW) \
20 X("noedit", 0, NEDITSW) \
21 X("file file", 0, FILESW) \
22 X("form formfile", 0, FORMSW) \
24 X("nouse", 0, NUSESW) \
25 X("whatnowproc program", 0, WHATSW) \
26 X("nowhatnowproc", 0, NWHATSW) \
27 X("build", 5, BILDSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
30 X("to address", 0, TOSW) \
31 X("cc address", 0, CCSW) \
32 X("from address", 0, FROMSW) \
33 X("fcc mailbox", 0, FCCSW) \
34 X("width columns", 0, WIDTHSW) \
35 X("subject text", 0, SUBJECTSW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(COMP
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(COMP
, switches
);
45 #define DISPO_SWITCHES \
47 X("replace", 0, YESW) \
49 X("list", 0, LISTDSW) \
50 X("refile +folder", 0, REFILSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(DISPO
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
61 static struct swit aqrul
[] = {
63 { "replace", 0, YESW
},
65 { "list", 0, LISTDSW
},
66 { "refile", 0, REFILSW
},
71 main (int argc
, char **argv
)
77 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
78 int outputlinelen
= OUTPUTLINELEN
;
79 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
80 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
81 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
82 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
84 char drft
[BUFSIZ
], **argp
, **arguments
;
85 struct msgs
*mp
= NULL
;
89 if (nmh_init(argv
[0], true, true)) { return 1; }
91 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
101 die("-%s unknown", cp
);
104 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
106 print_help (buf
, switches
, 1);
109 print_version(invo_name
);
113 if (!(ed
= *argp
++) || *ed
== '-')
114 die("missing argument to %s", argp
[-2]);
122 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
123 die("missing argument to %s", argp
[-2]);
135 if (!(form
= *argp
++) || *form
== '-')
136 die("missing argument to %s", argp
[-2]);
146 case FILESW
: /* compatibility */
148 die("only one file at a time!");
149 if (!(file
= *argp
++) || *file
== '-')
150 die("missing argument to %s", argp
[-2]);
156 die("only one draft folder at a time!");
157 if (!(cp
= *argp
++) || *cp
== '-')
158 die("missing argument to %s", argp
[-2]);
159 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
160 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
164 die("only one draft message at a time!");
165 if (!(file
= *argp
++) || *file
== '-')
166 die("missing argument to %s", argp
[-2]);
174 if (!(cp
= *argp
++) || *cp
== '-')
175 die("missing argument to %s", argp
[-2]);
176 to
= addlist(to
, cp
);
180 if (!(cp
= *argp
++) || *cp
== '-')
181 die("missing argument to %s", argp
[-2]);
182 cc
= addlist(cc
, cp
);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
188 from
= addlist(from
, cp
);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
196 cp
= dp
= path(cp
+ 1, TSUBCWF
);
197 fcc
= addlist(fcc
, cp
);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 die("missing argument to %s", argp
[-2]);
204 if ((outputlinelen
= atoi(cp
)) < 10)
205 die("impossible width %d", outputlinelen
);
209 if (!(cp
= *argp
++) || *cp
== '-')
210 die("missing argument to %s", argp
[-2]);
215 if (*cp
== '+' || *cp
== '@') {
217 die("only one folder at a time!");
218 folder
= pluspath (cp
);
221 die("only one message at a time!");
226 cwd
= mh_xstrdup(pwd ());
228 if (!context_find ("path"))
229 free (path ("./", TFOLDER
));
232 * Check if we are using a draft folder
233 * and have specified a message in it.
235 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
239 if (form
&& (folder
|| msg
))
240 die("can't mix forms and folders/msgs");
246 * Use a message as the "form" for the new message.
251 folder
= getfolder (1);
252 maildir
= m_maildir (folder
);
254 if (chdir (maildir
) == NOTOK
)
255 adios (maildir
, "unable to change directory to");
257 /* read folder and create message structure */
258 if (!(mp
= folder_read (folder
, 1)))
259 die("unable to read folder %s", folder
);
261 /* check for empty folder */
263 die("no messages in %s", folder
);
265 /* parse the message range/sequence/name and set SELECTED */
266 if (!m_convert (mp
, msg
))
268 seq_setprev (mp
); /* set the previous-sequence */
272 die("only one message at a time!");
274 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
275 adios (form
, "unable to open message");
282 cp
= new_fs(form
, NULL
, NULL
);
283 format_len
= strlen(cp
);
284 fmt_compile(cp
, &fmt
, 1);
287 * Set up any components that were fed to us on the command line
291 cptr
= fmt_findcomp("from");
296 cptr
= fmt_findcomp("to");
301 cptr
= fmt_findcomp("cc");
306 cptr
= fmt_findcomp("fcc");
311 cptr
= fmt_findcomp("subject");
313 cptr
->c_text
= subject
;
318 strncpy (drft
, build
? m_maildir ("draft")
319 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
322 * Check if we have an existing draft
324 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
325 i
= fdcompare (in
, out
);
329 * If we have given -use flag, or if the
330 * draft is just the same as the components
331 * file, then no need to ask any questions.
336 if (stat (drft
, &st
) == NOTOK
)
337 adios (drft
, "unable to stat");
338 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
339 for (i
= LISTDSW
; i
!= YESW
;) {
340 if (!(argp
= read_switch_multiword ("\nDisposition? ",
341 isdf
? aqrunl
: aqrul
)))
343 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
356 showfile (++argp
, drft
);
359 if (refile (++argp
, drft
) == 0)
369 adios (drft
, "unable to open");
372 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
373 adios (drft
, "unable to create");
377 i
= format_len
+ 1024;
378 scanl
= charstring_create (i
+ 2);
382 dat
[3] = outputlinelen
;
384 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
385 if (write(out
, charstring_buffer (scanl
),
386 charstring_bytes (scanl
)) < 0) {
387 advise (drft
, "write");
389 charstring_free(scanl
);
391 cpydata (in
, out
, form
, drft
);
397 context_save (); /* save the context file */
401 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);