]>
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>
11 #include "../sbr/m_maildir.h"
14 #define COMP_SWITCHES \
15 X("draftfolder +folder", 0, DFOLDSW) \
16 X("draftmessage msg", 0, DMSGSW) \
17 X("nodraftfolder", 0, NDFLDSW) \
18 X("editor editor", 0, EDITRSW) \
19 X("noedit", 0, NEDITSW) \
20 X("file file", 0, FILESW) \
21 X("form formfile", 0, FORMSW) \
23 X("nouse", 0, NUSESW) \
24 X("whatnowproc program", 0, WHATSW) \
25 X("nowhatnowproc", 0, NWHATSW) \
26 X("build", 5, BILDSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
29 X("to address", 0, TOSW) \
30 X("cc address", 0, CCSW) \
31 X("from address", 0, FROMSW) \
32 X("fcc mailbox", 0, FCCSW) \
33 X("width columns", 0, WIDTHSW) \
34 X("subject text", 0, SUBJECTSW) \
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(COMP
);
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(COMP
, switches
);
44 #define DISPO_SWITCHES \
46 X("replace", 0, YESW) \
48 X("list", 0, LISTDSW) \
49 X("refile +folder", 0, REFILSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(DISPO
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
60 static struct swit aqrul
[] = {
62 { "replace", 0, YESW
},
64 { "list", 0, LISTDSW
},
65 { "refile", 0, REFILSW
},
70 main (int argc
, char **argv
)
72 int use
= NOUSE
, nedit
= 0, nwhat
= 0, build
= 0;
73 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
74 int outputlinelen
= OUTPUTLINELEN
;
75 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
76 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
77 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
78 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
80 char drft
[BUFSIZ
], **argp
, **arguments
;
81 struct msgs
*mp
= NULL
;
85 if (nmh_init(argv
[0], 1)) { return 1; }
87 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 adios (NULL
, "-%s unknown", cp
);
100 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
102 print_help (buf
, switches
, 1);
105 print_version(invo_name
);
109 if (!(ed
= *argp
++) || *ed
== '-')
110 adios (NULL
, "missing argument to %s", argp
[-2]);
118 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
119 adios (NULL
, "missing argument to %s", argp
[-2]);
131 if (!(form
= *argp
++) || *form
== '-')
132 adios (NULL
, "missing argument to %s", argp
[-2]);
142 case FILESW
: /* compatibility */
144 adios (NULL
, "only one file at a time!");
145 if (!(file
= *argp
++) || *file
== '-')
146 adios (NULL
, "missing argument to %s", argp
[-2]);
152 adios (NULL
, "only one draft folder at a time!");
153 if (!(cp
= *argp
++) || *cp
== '-')
154 adios (NULL
, "missing argument to %s", argp
[-2]);
155 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
156 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
160 adios (NULL
, "only one draft message at a time!");
161 if (!(file
= *argp
++) || *file
== '-')
162 adios (NULL
, "missing argument to %s", argp
[-2]);
170 if (!(cp
= *argp
++) || *cp
== '-')
171 adios (NULL
, "missing argument to %s", argp
[-2]);
172 to
= addlist(to
, cp
);
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
178 cc
= addlist(cc
, cp
);
182 if (!(cp
= *argp
++) || *cp
== '-')
183 adios (NULL
, "missing argument to %s", argp
[-2]);
184 from
= addlist(from
, cp
);
188 if (!(cp
= *argp
++) || *cp
== '-')
189 adios (NULL
, "missing argument to %s", argp
[-2]);
192 cp
= dp
= path(cp
+ 1, TSUBCWF
);
193 fcc
= addlist(fcc
, cp
);
198 if (!(cp
= *argp
++) || *cp
== '-')
199 adios (NULL
, "missing argument to %s", argp
[-2]);
200 if ((outputlinelen
= atoi(cp
)) < 10)
201 adios (NULL
, "impossible width %d", outputlinelen
);
205 if (!(cp
= *argp
++) || *cp
== '-')
206 adios (NULL
, "missing argument to %s", argp
[-2]);
211 if (*cp
== '+' || *cp
== '@') {
213 adios (NULL
, "only one folder at a time!");
215 folder
= pluspath (cp
);
218 adios (NULL
, "only one message at a time!");
224 cwd
= mh_xstrdup(pwd ());
226 if (!context_find ("path"))
227 free (path ("./", TFOLDER
));
230 * Check if we are using a draft folder
231 * and have specified a message in it.
233 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
237 if (form
&& (folder
|| msg
))
238 adios (NULL
, "can't mix forms and folders/msgs");
244 * Use a message as the "form" for the new message.
249 folder
= getfolder (1);
250 maildir
= m_maildir (folder
);
252 if (chdir (maildir
) == NOTOK
)
253 adios (maildir
, "unable to change directory to");
255 /* read folder and create message structure */
256 if (!(mp
= folder_read (folder
, 1)))
257 adios (NULL
, "unable to read folder %s", folder
);
259 /* check for empty folder */
261 adios (NULL
, "no messages in %s", folder
);
263 /* parse the message range/sequence/name and set SELECTED */
264 if (!m_convert (mp
, msg
))
266 seq_setprev (mp
); /* set the previous-sequence */
270 adios (NULL
, "only one message at a time!");
272 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
273 adios (form
, "unable to open message");
280 cp
= new_fs(form
, NULL
, NULL
);
281 format_len
= strlen(cp
);
282 fmt_compile(cp
, &fmt
, 1);
285 * Set up any components that were fed to us on the command line
289 cptr
= fmt_findcomp("from");
294 cptr
= fmt_findcomp("to");
299 cptr
= fmt_findcomp("cc");
304 cptr
= fmt_findcomp("fcc");
309 cptr
= fmt_findcomp("subject");
311 cptr
->c_text
= subject
;
316 strncpy (drft
, build
? m_maildir ("draft")
317 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
320 * Check if we have an existing draft
322 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
323 i
= fdcompare (in
, out
);
327 * If we have given -use flag, or if the
328 * draft is just the same as the components
329 * file, then no need to ask any questions.
334 if (stat (drft
, &st
) == NOTOK
)
335 adios (drft
, "unable to stat");
336 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
337 for (i
= LISTDSW
; i
!= YESW
;) {
338 if (!(argp
= read_switch_multiword ("\nDisposition? ",
339 isdf
? aqrunl
: aqrul
)))
341 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
354 showfile (++argp
, drft
);
357 if (refile (++argp
, drft
) == 0)
367 adios (drft
, "unable to open");
370 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
371 adios (drft
, "unable to create");
375 i
= format_len
+ 1024;
376 scanl
= charstring_create (i
+ 2);
380 dat
[3] = outputlinelen
;
382 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
383 if (write(out
, charstring_buffer (scanl
),
384 charstring_bytes (scanl
)) < 0) {
385 advise (drft
, "write");
387 charstring_free(scanl
);
389 cpydata (in
, out
, form
, drft
);
395 context_save (); /* save the context file */
399 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);