]>
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.
9 #include "sbr/cpydata.h"
10 #include "sbr/m_draft.h"
11 #include "sbr/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/fdcompare.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
23 #include "h/fmt_scan.h"
25 #include "sbr/m_maildir.h"
28 #define COMP_SWITCHES \
29 X("draftfolder +folder", 0, DFOLDSW) \
30 X("draftmessage msg", 0, DMSGSW) \
31 X("nodraftfolder", 0, NDFLDSW) \
32 X("editor editor", 0, EDITRSW) \
33 X("noedit", 0, NEDITSW) \
34 X("file file", 0, FILESW) \
35 X("form formfile", 0, FORMSW) \
37 X("nouse", 0, NUSESW) \
38 X("whatnowproc program", 0, WHATSW) \
39 X("nowhatnowproc", 0, NWHATSW) \
40 X("build", 5, BILDSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("to address", 0, TOSW) \
44 X("cc address", 0, CCSW) \
45 X("from address", 0, FROMSW) \
46 X("fcc mailbox", 0, FCCSW) \
47 X("width columns", 0, WIDTHSW) \
48 X("subject text", 0, SUBJECTSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(COMP
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(COMP
, switches
);
58 #define DISPO_SWITCHES \
60 X("replace", 0, YESW) \
62 X("list", 0, LISTDSW) \
63 X("refile +folder", 0, REFILSW) \
66 #define X(sw, minchars, id) id,
67 DEFINE_SWITCH_ENUM(DISPO
);
70 #define X(sw, minchars, id) { sw, minchars, id },
71 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
74 static struct swit aqrul
[] = {
76 { "replace", 0, YESW
},
78 { "list", 0, LISTDSW
},
79 { "refile", 0, REFILSW
},
84 main (int argc
, char **argv
)
90 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
91 int outputlinelen
= OUTPUTLINELEN
;
92 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
93 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
94 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
95 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
97 char drft
[BUFSIZ
], **argp
, **arguments
;
98 struct msgs
*mp
= NULL
;
102 if (nmh_init(argv
[0], true, true)) { return 1; }
104 arguments
= getarguments (invo_name
, argc
, argv
, 1);
107 while ((cp
= *argp
++)) {
109 switch (smatch (++cp
, switches
)) {
111 ambigsw (cp
, switches
);
114 die("-%s unknown", cp
);
117 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
119 print_help (buf
, switches
, 1);
122 print_version(invo_name
);
126 if (!(ed
= *argp
++) || *ed
== '-')
127 die("missing argument to %s", argp
[-2]);
135 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
136 die("missing argument to %s", argp
[-2]);
148 if (!(form
= *argp
++) || *form
== '-')
149 die("missing argument to %s", argp
[-2]);
159 case FILESW
: /* compatibility */
161 die("only one file at a time!");
162 if (!(file
= *argp
++) || *file
== '-')
163 die("missing argument to %s", argp
[-2]);
169 die("only one draft folder at a time!");
170 if (!(cp
= *argp
++) || *cp
== '-')
171 die("missing argument to %s", argp
[-2]);
172 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
173 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
177 die("only one draft message at a time!");
178 if (!(file
= *argp
++) || *file
== '-')
179 die("missing argument to %s", argp
[-2]);
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 to
= addlist(to
, cp
);
193 if (!(cp
= *argp
++) || *cp
== '-')
194 die("missing argument to %s", argp
[-2]);
195 cc
= addlist(cc
, cp
);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 die("missing argument to %s", argp
[-2]);
201 from
= addlist(from
, cp
);
205 if (!(cp
= *argp
++) || *cp
== '-')
206 die("missing argument to %s", argp
[-2]);
209 cp
= dp
= path(cp
+ 1, TSUBCWF
);
210 fcc
= addlist(fcc
, cp
);
215 if (!(cp
= *argp
++) || *cp
== '-')
216 die("missing argument to %s", argp
[-2]);
217 if ((outputlinelen
= atoi(cp
)) < 10)
218 die("impossible width %d", outputlinelen
);
222 if (!(cp
= *argp
++) || *cp
== '-')
223 die("missing argument to %s", argp
[-2]);
228 if (*cp
== '+' || *cp
== '@') {
230 die("only one folder at a time!");
231 folder
= pluspath (cp
);
234 die("only one message at a time!");
239 cwd
= mh_xstrdup(pwd ());
241 if (!context_find ("path"))
242 free (path ("./", TFOLDER
));
245 * Check if we are using a draft folder
246 * and have specified a message in it.
248 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
252 if (form
&& (folder
|| msg
))
253 die("can't mix forms and folders/msgs");
259 * Use a message as the "form" for the new message.
264 folder
= getfolder (1);
265 maildir
= m_maildir (folder
);
267 if (chdir (maildir
) == NOTOK
)
268 adios (maildir
, "unable to change directory to");
270 /* read folder and create message structure */
271 if (!(mp
= folder_read (folder
, 1)))
272 die("unable to read folder %s", folder
);
274 /* check for empty folder */
276 die("no messages in %s", folder
);
278 /* parse the message range/sequence/name and set SELECTED */
279 if (!m_convert (mp
, msg
))
281 seq_setprev (mp
); /* set the previous-sequence */
285 die("only one message at a time!");
287 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
288 adios (form
, "unable to open message");
295 cp
= new_fs(form
, NULL
, NULL
);
296 format_len
= strlen(cp
);
297 fmt_compile(cp
, &fmt
, 1);
300 * Set up any components that were fed to us on the command line
304 cptr
= fmt_findcomp("from");
309 cptr
= fmt_findcomp("to");
314 cptr
= fmt_findcomp("cc");
319 cptr
= fmt_findcomp("fcc");
324 cptr
= fmt_findcomp("subject");
326 cptr
->c_text
= subject
;
331 strncpy (drft
, build
? m_maildir ("draft")
332 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
335 * Check if we have an existing draft
337 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
338 i
= fdcompare (in
, out
);
342 * If we have given -use flag, or if the
343 * draft is just the same as the components
344 * file, then no need to ask any questions.
349 if (stat (drft
, &st
) == NOTOK
)
350 adios (drft
, "unable to stat");
351 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
352 for (i
= LISTDSW
; i
!= YESW
;) {
353 if (!(argp
= read_switch_multiword ("\nDisposition? ",
354 isdf
? aqrunl
: aqrul
)))
356 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
369 showfile (++argp
, drft
);
372 if (refile (++argp
, drft
) == 0)
382 adios (drft
, "unable to open");
385 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
386 adios (drft
, "unable to create");
390 i
= format_len
+ 1024;
391 scanl
= charstring_create (i
+ 2);
395 dat
[3] = outputlinelen
;
397 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
398 if (write(out
, charstring_buffer (scanl
),
399 charstring_bytes (scanl
)) < 0) {
400 advise (drft
, "write");
402 charstring_free(scanl
);
404 cpydata (in
, out
, form
, drft
);
410 context_save (); /* save the context file */
414 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);