]>
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/seq_setprev.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/showfile.h"
12 #include "sbr/smatch.h"
13 #include "sbr/refile.h"
14 #include "sbr/cpydata.h"
15 #include "sbr/m_draft.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/fdcompare.h"
19 #include "sbr/folder_read.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
28 #include "h/fmt_scan.h"
30 #include "sbr/m_maildir.h"
33 #define COMP_SWITCHES \
34 X("draftfolder +folder", 0, DFOLDSW) \
35 X("draftmessage msg", 0, DMSGSW) \
36 X("nodraftfolder", 0, NDFLDSW) \
37 X("editor editor", 0, EDITRSW) \
38 X("noedit", 0, NEDITSW) \
39 X("file file", 0, FILESW) \
40 X("form formfile", 0, FORMSW) \
42 X("nouse", 0, NUSESW) \
43 X("whatnowproc program", 0, WHATSW) \
44 X("nowhatnowproc", 0, NWHATSW) \
45 X("build", 5, BILDSW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
48 X("to address", 0, TOSW) \
49 X("cc address", 0, CCSW) \
50 X("from address", 0, FROMSW) \
51 X("fcc mailbox", 0, FCCSW) \
52 X("width columns", 0, WIDTHSW) \
53 X("subject text", 0, SUBJECTSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(COMP
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(COMP
, switches
);
63 #define DISPO_SWITCHES \
65 X("replace", 0, YESW) \
67 X("list", 0, LISTDSW) \
68 X("refile +folder", 0, REFILSW) \
71 #define X(sw, minchars, id) id,
72 DEFINE_SWITCH_ENUM(DISPO
);
75 #define X(sw, minchars, id) { sw, minchars, id },
76 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
79 static struct swit aqrul
[] = {
81 { "replace", 0, YESW
},
83 { "list", 0, LISTDSW
},
84 { "refile", 0, REFILSW
},
89 main (int argc
, char **argv
)
95 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
96 int outputlinelen
= OUTPUTLINELEN
;
97 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
98 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
99 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
100 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
101 char *subject
= NULL
;
102 char drft
[BUFSIZ
], **argp
, **arguments
;
103 struct msgs
*mp
= NULL
;
107 if (nmh_init(argv
[0], true, true)) { return 1; }
109 arguments
= getarguments (invo_name
, argc
, argv
, 1);
112 while ((cp
= *argp
++)) {
114 switch (smatch (++cp
, switches
)) {
116 ambigsw (cp
, switches
);
119 die("-%s unknown", cp
);
122 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
124 print_help (buf
, switches
, 1);
127 print_version(invo_name
);
131 if (!(ed
= *argp
++) || *ed
== '-')
132 die("missing argument to %s", argp
[-2]);
140 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
141 die("missing argument to %s", argp
[-2]);
153 if (!(form
= *argp
++) || *form
== '-')
154 die("missing argument to %s", argp
[-2]);
164 case FILESW
: /* compatibility */
166 die("only one file at a time!");
167 if (!(file
= *argp
++) || *file
== '-')
168 die("missing argument to %s", argp
[-2]);
174 die("only one draft folder at a time!");
175 if (!(cp
= *argp
++) || *cp
== '-')
176 die("missing argument to %s", argp
[-2]);
177 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
178 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
182 die("only one draft message at a time!");
183 if (!(file
= *argp
++) || *file
== '-')
184 die("missing argument to %s", argp
[-2]);
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 to
= addlist(to
, cp
);
198 if (!(cp
= *argp
++) || *cp
== '-')
199 die("missing argument to %s", argp
[-2]);
200 cc
= addlist(cc
, cp
);
204 if (!(cp
= *argp
++) || *cp
== '-')
205 die("missing argument to %s", argp
[-2]);
206 from
= addlist(from
, cp
);
210 if (!(cp
= *argp
++) || *cp
== '-')
211 die("missing argument to %s", argp
[-2]);
214 cp
= dp
= path(cp
+ 1, TSUBCWF
);
215 fcc
= addlist(fcc
, cp
);
220 if (!(cp
= *argp
++) || *cp
== '-')
221 die("missing argument to %s", argp
[-2]);
222 if ((outputlinelen
= atoi(cp
)) < 10)
223 die("impossible width %d", outputlinelen
);
227 if (!(cp
= *argp
++) || *cp
== '-')
228 die("missing argument to %s", argp
[-2]);
233 if (*cp
== '+' || *cp
== '@') {
235 die("only one folder at a time!");
236 folder
= pluspath (cp
);
239 die("only one message at a time!");
244 cwd
= mh_xstrdup(pwd ());
246 if (!context_find ("path"))
247 free (path ("./", TFOLDER
));
250 * Check if we are using a draft folder
251 * and have specified a message in it.
253 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
257 if (form
&& (folder
|| msg
))
258 die("can't mix forms and folders/msgs");
264 * Use a message as the "form" for the new message.
269 folder
= getfolder (1);
270 maildir
= m_maildir (folder
);
272 if (chdir (maildir
) == NOTOK
)
273 adios (maildir
, "unable to change directory to");
275 /* read folder and create message structure */
276 if (!(mp
= folder_read (folder
, 1)))
277 die("unable to read folder %s", folder
);
279 /* check for empty folder */
281 die("no messages in %s", folder
);
283 /* parse the message range/sequence/name and set SELECTED */
284 if (!m_convert (mp
, msg
))
286 seq_setprev (mp
); /* set the previous-sequence */
290 die("only one message at a time!");
292 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
293 adios (form
, "unable to open message");
300 cp
= new_fs(form
, NULL
, NULL
);
301 format_len
= strlen(cp
);
302 fmt_compile(cp
, &fmt
, 1);
305 * Set up any components that were fed to us on the command line
309 cptr
= fmt_findcomp("from");
314 cptr
= fmt_findcomp("to");
319 cptr
= fmt_findcomp("cc");
324 cptr
= fmt_findcomp("fcc");
329 cptr
= fmt_findcomp("subject");
331 cptr
->c_text
= subject
;
336 strncpy (drft
, build
? m_maildir ("draft")
337 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
340 * Check if we have an existing draft
342 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
343 i
= fdcompare (in
, out
);
347 * If we have given -use flag, or if the
348 * draft is just the same as the components
349 * file, then no need to ask any questions.
354 if (stat (drft
, &st
) == NOTOK
)
355 adios (drft
, "unable to stat");
356 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
357 for (i
= LISTDSW
; i
!= YESW
;) {
358 if (!(argp
= read_switch_multiword ("\nDisposition? ",
359 isdf
? aqrunl
: aqrul
)))
361 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
374 showfile (++argp
, drft
);
377 if (refile (++argp
, drft
) == 0)
387 adios (drft
, "unable to open");
390 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
391 adios (drft
, "unable to create");
395 i
= format_len
+ 1024;
396 scanl
= charstring_create (i
+ 2);
400 dat
[3] = outputlinelen
;
402 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
403 if (write(out
, charstring_buffer (scanl
),
404 charstring_bytes (scanl
)) < 0) {
405 advise (drft
, "write");
407 charstring_free(scanl
);
409 cpydata (in
, out
, form
, drft
);
415 context_save (); /* save the context file */
419 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);