]>
diplodocus.org Git - nmh/blob - uip/comp.c
fe7798dfd7c5d53fd3937ad7d1fb0759858312fe
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/context_find.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
16 #include "h/fmt_scan.h"
18 #include "sbr/m_maildir.h"
21 #define COMP_SWITCHES \
22 X("draftfolder +folder", 0, DFOLDSW) \
23 X("draftmessage msg", 0, DMSGSW) \
24 X("nodraftfolder", 0, NDFLDSW) \
25 X("editor editor", 0, EDITRSW) \
26 X("noedit", 0, NEDITSW) \
27 X("file file", 0, FILESW) \
28 X("form formfile", 0, FORMSW) \
30 X("nouse", 0, NUSESW) \
31 X("whatnowproc program", 0, WHATSW) \
32 X("nowhatnowproc", 0, NWHATSW) \
33 X("build", 5, BILDSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("to address", 0, TOSW) \
37 X("cc address", 0, CCSW) \
38 X("from address", 0, FROMSW) \
39 X("fcc mailbox", 0, FCCSW) \
40 X("width columns", 0, WIDTHSW) \
41 X("subject text", 0, SUBJECTSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(COMP
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(COMP
, switches
);
51 #define DISPO_SWITCHES \
53 X("replace", 0, YESW) \
55 X("list", 0, LISTDSW) \
56 X("refile +folder", 0, REFILSW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(DISPO
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
67 static struct swit aqrul
[] = {
69 { "replace", 0, YESW
},
71 { "list", 0, LISTDSW
},
72 { "refile", 0, REFILSW
},
77 main (int argc
, char **argv
)
83 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
84 int outputlinelen
= OUTPUTLINELEN
;
85 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
86 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
87 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
88 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
90 char drft
[BUFSIZ
], **argp
, **arguments
;
91 struct msgs
*mp
= NULL
;
95 if (nmh_init(argv
[0], true, true)) { return 1; }
97 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++)) {
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 die("-%s unknown", cp
);
110 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
112 print_help (buf
, switches
, 1);
115 print_version(invo_name
);
119 if (!(ed
= *argp
++) || *ed
== '-')
120 die("missing argument to %s", argp
[-2]);
128 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
129 die("missing argument to %s", argp
[-2]);
141 if (!(form
= *argp
++) || *form
== '-')
142 die("missing argument to %s", argp
[-2]);
152 case FILESW
: /* compatibility */
154 die("only one file at a time!");
155 if (!(file
= *argp
++) || *file
== '-')
156 die("missing argument to %s", argp
[-2]);
162 die("only one draft folder at a time!");
163 if (!(cp
= *argp
++) || *cp
== '-')
164 die("missing argument to %s", argp
[-2]);
165 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
166 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
170 die("only one draft message at a time!");
171 if (!(file
= *argp
++) || *file
== '-')
172 die("missing argument to %s", argp
[-2]);
180 if (!(cp
= *argp
++) || *cp
== '-')
181 die("missing argument to %s", argp
[-2]);
182 to
= addlist(to
, cp
);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
188 cc
= addlist(cc
, cp
);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 from
= addlist(from
, cp
);
198 if (!(cp
= *argp
++) || *cp
== '-')
199 die("missing argument to %s", argp
[-2]);
202 cp
= dp
= path(cp
+ 1, TSUBCWF
);
203 fcc
= addlist(fcc
, cp
);
208 if (!(cp
= *argp
++) || *cp
== '-')
209 die("missing argument to %s", argp
[-2]);
210 if ((outputlinelen
= atoi(cp
)) < 10)
211 die("impossible width %d", outputlinelen
);
215 if (!(cp
= *argp
++) || *cp
== '-')
216 die("missing argument to %s", argp
[-2]);
221 if (*cp
== '+' || *cp
== '@') {
223 die("only one folder at a time!");
224 folder
= pluspath (cp
);
227 die("only one message at a time!");
232 cwd
= mh_xstrdup(pwd ());
234 if (!context_find ("path"))
235 free (path ("./", TFOLDER
));
238 * Check if we are using a draft folder
239 * and have specified a message in it.
241 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
245 if (form
&& (folder
|| msg
))
246 die("can't mix forms and folders/msgs");
252 * Use a message as the "form" for the new message.
257 folder
= getfolder (1);
258 maildir
= m_maildir (folder
);
260 if (chdir (maildir
) == NOTOK
)
261 adios (maildir
, "unable to change directory to");
263 /* read folder and create message structure */
264 if (!(mp
= folder_read (folder
, 1)))
265 die("unable to read folder %s", folder
);
267 /* check for empty folder */
269 die("no messages in %s", folder
);
271 /* parse the message range/sequence/name and set SELECTED */
272 if (!m_convert (mp
, msg
))
274 seq_setprev (mp
); /* set the previous-sequence */
278 die("only one message at a time!");
280 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
281 adios (form
, "unable to open message");
288 cp
= new_fs(form
, NULL
, NULL
);
289 format_len
= strlen(cp
);
290 fmt_compile(cp
, &fmt
, 1);
293 * Set up any components that were fed to us on the command line
297 cptr
= fmt_findcomp("from");
302 cptr
= fmt_findcomp("to");
307 cptr
= fmt_findcomp("cc");
312 cptr
= fmt_findcomp("fcc");
317 cptr
= fmt_findcomp("subject");
319 cptr
->c_text
= subject
;
324 strncpy (drft
, build
? m_maildir ("draft")
325 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
328 * Check if we have an existing draft
330 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
331 i
= fdcompare (in
, out
);
335 * If we have given -use flag, or if the
336 * draft is just the same as the components
337 * file, then no need to ask any questions.
342 if (stat (drft
, &st
) == NOTOK
)
343 adios (drft
, "unable to stat");
344 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
345 for (i
= LISTDSW
; i
!= YESW
;) {
346 if (!(argp
= read_switch_multiword ("\nDisposition? ",
347 isdf
? aqrunl
: aqrul
)))
349 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
362 showfile (++argp
, drft
);
365 if (refile (++argp
, drft
) == 0)
375 adios (drft
, "unable to open");
378 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
379 adios (drft
, "unable to create");
383 i
= format_len
+ 1024;
384 scanl
= charstring_create (i
+ 2);
388 dat
[3] = outputlinelen
;
390 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
391 if (write(out
, charstring_buffer (scanl
),
392 charstring_bytes (scanl
)) < 0) {
393 advise (drft
, "write");
395 charstring_free(scanl
);
397 cpydata (in
, out
, form
, drft
);
403 context_save (); /* save the context file */
407 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);