]>
diplodocus.org Git - nmh/blob - uip/comp.c
00330e1108a5c0ec1ea39c3f502bc9a876bdad84
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
13 #include "h/fmt_scan.h"
15 #include "sbr/m_maildir.h"
18 #define COMP_SWITCHES \
19 X("draftfolder +folder", 0, DFOLDSW) \
20 X("draftmessage msg", 0, DMSGSW) \
21 X("nodraftfolder", 0, NDFLDSW) \
22 X("editor editor", 0, EDITRSW) \
23 X("noedit", 0, NEDITSW) \
24 X("file file", 0, FILESW) \
25 X("form formfile", 0, FORMSW) \
27 X("nouse", 0, NUSESW) \
28 X("whatnowproc program", 0, WHATSW) \
29 X("nowhatnowproc", 0, NWHATSW) \
30 X("build", 5, BILDSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
33 X("to address", 0, TOSW) \
34 X("cc address", 0, CCSW) \
35 X("from address", 0, FROMSW) \
36 X("fcc mailbox", 0, FCCSW) \
37 X("width columns", 0, WIDTHSW) \
38 X("subject text", 0, SUBJECTSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(COMP
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(COMP
, switches
);
48 #define DISPO_SWITCHES \
50 X("replace", 0, YESW) \
52 X("list", 0, LISTDSW) \
53 X("refile +folder", 0, REFILSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(DISPO
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
64 static struct swit aqrul
[] = {
66 { "replace", 0, YESW
},
68 { "list", 0, LISTDSW
},
69 { "refile", 0, REFILSW
},
74 main (int argc
, char **argv
)
80 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
81 int outputlinelen
= OUTPUTLINELEN
;
82 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
83 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
84 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
85 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
87 char drft
[BUFSIZ
], **argp
, **arguments
;
88 struct msgs
*mp
= NULL
;
92 if (nmh_init(argv
[0], true, true)) { return 1; }
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
97 while ((cp
= *argp
++)) {
99 switch (smatch (++cp
, switches
)) {
101 ambigsw (cp
, switches
);
104 die("-%s unknown", cp
);
107 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
109 print_help (buf
, switches
, 1);
112 print_version(invo_name
);
116 if (!(ed
= *argp
++) || *ed
== '-')
117 die("missing argument to %s", argp
[-2]);
125 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
126 die("missing argument to %s", argp
[-2]);
138 if (!(form
= *argp
++) || *form
== '-')
139 die("missing argument to %s", argp
[-2]);
149 case FILESW
: /* compatibility */
151 die("only one file at a time!");
152 if (!(file
= *argp
++) || *file
== '-')
153 die("missing argument to %s", argp
[-2]);
159 die("only one draft folder at a time!");
160 if (!(cp
= *argp
++) || *cp
== '-')
161 die("missing argument to %s", argp
[-2]);
162 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
163 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
167 die("only one draft message at a time!");
168 if (!(file
= *argp
++) || *file
== '-')
169 die("missing argument to %s", argp
[-2]);
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
179 to
= addlist(to
, cp
);
183 if (!(cp
= *argp
++) || *cp
== '-')
184 die("missing argument to %s", argp
[-2]);
185 cc
= addlist(cc
, cp
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 from
= addlist(from
, cp
);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 die("missing argument to %s", argp
[-2]);
199 cp
= dp
= path(cp
+ 1, TSUBCWF
);
200 fcc
= addlist(fcc
, cp
);
205 if (!(cp
= *argp
++) || *cp
== '-')
206 die("missing argument to %s", argp
[-2]);
207 if ((outputlinelen
= atoi(cp
)) < 10)
208 die("impossible width %d", outputlinelen
);
212 if (!(cp
= *argp
++) || *cp
== '-')
213 die("missing argument to %s", argp
[-2]);
218 if (*cp
== '+' || *cp
== '@') {
220 die("only one folder at a time!");
221 folder
= pluspath (cp
);
224 die("only one message at a time!");
229 cwd
= mh_xstrdup(pwd ());
231 if (!context_find ("path"))
232 free (path ("./", TFOLDER
));
235 * Check if we are using a draft folder
236 * and have specified a message in it.
238 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
242 if (form
&& (folder
|| msg
))
243 die("can't mix forms and folders/msgs");
249 * Use a message as the "form" for the new message.
254 folder
= getfolder (1);
255 maildir
= m_maildir (folder
);
257 if (chdir (maildir
) == NOTOK
)
258 adios (maildir
, "unable to change directory to");
260 /* read folder and create message structure */
261 if (!(mp
= folder_read (folder
, 1)))
262 die("unable to read folder %s", folder
);
264 /* check for empty folder */
266 die("no messages in %s", folder
);
268 /* parse the message range/sequence/name and set SELECTED */
269 if (!m_convert (mp
, msg
))
271 seq_setprev (mp
); /* set the previous-sequence */
275 die("only one message at a time!");
277 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
278 adios (form
, "unable to open message");
285 cp
= new_fs(form
, NULL
, NULL
);
286 format_len
= strlen(cp
);
287 fmt_compile(cp
, &fmt
, 1);
290 * Set up any components that were fed to us on the command line
294 cptr
= fmt_findcomp("from");
299 cptr
= fmt_findcomp("to");
304 cptr
= fmt_findcomp("cc");
309 cptr
= fmt_findcomp("fcc");
314 cptr
= fmt_findcomp("subject");
316 cptr
->c_text
= subject
;
321 strncpy (drft
, build
? m_maildir ("draft")
322 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
325 * Check if we have an existing draft
327 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
328 i
= fdcompare (in
, out
);
332 * If we have given -use flag, or if the
333 * draft is just the same as the components
334 * file, then no need to ask any questions.
339 if (stat (drft
, &st
) == NOTOK
)
340 adios (drft
, "unable to stat");
341 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
342 for (i
= LISTDSW
; i
!= YESW
;) {
343 if (!(argp
= read_switch_multiword ("\nDisposition? ",
344 isdf
? aqrunl
: aqrul
)))
346 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
359 showfile (++argp
, drft
);
362 if (refile (++argp
, drft
) == 0)
372 adios (drft
, "unable to open");
375 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
376 adios (drft
, "unable to create");
380 i
= format_len
+ 1024;
381 scanl
= charstring_create (i
+ 2);
385 dat
[3] = outputlinelen
;
387 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
388 if (write(out
, charstring_buffer (scanl
),
389 charstring_bytes (scanl
)) < 0) {
390 advise (drft
, "write");
392 charstring_free(scanl
);
394 cpydata (in
, out
, form
, drft
);
400 context_save (); /* save the context file */
404 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);