]>
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!");
214 folder
= pluspath (cp
);
217 adios (NULL
, "only one message at a time!");
222 cwd
= mh_xstrdup(pwd ());
224 if (!context_find ("path"))
225 free (path ("./", TFOLDER
));
228 * Check if we are using a draft folder
229 * and have specified a message in it.
231 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
235 if (form
&& (folder
|| msg
))
236 adios (NULL
, "can't mix forms and folders/msgs");
242 * Use a message as the "form" for the new message.
247 folder
= getfolder (1);
248 maildir
= m_maildir (folder
);
250 if (chdir (maildir
) == NOTOK
)
251 adios (maildir
, "unable to change directory to");
253 /* read folder and create message structure */
254 if (!(mp
= folder_read (folder
, 1)))
255 adios (NULL
, "unable to read folder %s", folder
);
257 /* check for empty folder */
259 adios (NULL
, "no messages in %s", folder
);
261 /* parse the message range/sequence/name and set SELECTED */
262 if (!m_convert (mp
, msg
))
264 seq_setprev (mp
); /* set the previous-sequence */
268 adios (NULL
, "only one message at a time!");
270 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
271 adios (form
, "unable to open message");
278 cp
= new_fs(form
, NULL
, NULL
);
279 format_len
= strlen(cp
);
280 fmt_compile(cp
, &fmt
, 1);
283 * Set up any components that were fed to us on the command line
287 cptr
= fmt_findcomp("from");
292 cptr
= fmt_findcomp("to");
297 cptr
= fmt_findcomp("cc");
302 cptr
= fmt_findcomp("fcc");
307 cptr
= fmt_findcomp("subject");
309 cptr
->c_text
= subject
;
314 strncpy (drft
, build
? m_maildir ("draft")
315 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
318 * Check if we have an existing draft
320 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
321 i
= fdcompare (in
, out
);
325 * If we have given -use flag, or if the
326 * draft is just the same as the components
327 * file, then no need to ask any questions.
332 if (stat (drft
, &st
) == NOTOK
)
333 adios (drft
, "unable to stat");
334 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
335 for (i
= LISTDSW
; i
!= YESW
;) {
336 if (!(argp
= read_switch_multiword ("\nDisposition? ",
337 isdf
? aqrunl
: aqrul
)))
339 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
352 showfile (++argp
, drft
);
355 if (refile (++argp
, drft
) == 0)
365 adios (drft
, "unable to open");
368 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
369 adios (drft
, "unable to create");
373 i
= format_len
+ 1024;
374 scanl
= charstring_create (i
+ 2);
378 dat
[3] = outputlinelen
;
380 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
381 if (write(out
, charstring_buffer (scanl
),
382 charstring_bytes (scanl
)) < 0) {
383 advise (drft
, "write");
385 charstring_free(scanl
);
387 cpydata (in
, out
, form
, drft
);
393 context_save (); /* save the context file */
397 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);