]>
diplodocus.org Git - nmh/blob - uip/comp.c
b07a0dc0eb55a050a20309dd7b10d1bdb38b44d6
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/getfolder.h"
10 #include "sbr/fdcompare.h"
11 #include "sbr/folder_read.h"
12 #include "sbr/context_save.h"
13 #include "sbr/context_find.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
20 #include "h/fmt_scan.h"
22 #include "sbr/m_maildir.h"
25 #define COMP_SWITCHES \
26 X("draftfolder +folder", 0, DFOLDSW) \
27 X("draftmessage msg", 0, DMSGSW) \
28 X("nodraftfolder", 0, NDFLDSW) \
29 X("editor editor", 0, EDITRSW) \
30 X("noedit", 0, NEDITSW) \
31 X("file file", 0, FILESW) \
32 X("form formfile", 0, FORMSW) \
34 X("nouse", 0, NUSESW) \
35 X("whatnowproc program", 0, WHATSW) \
36 X("nowhatnowproc", 0, NWHATSW) \
37 X("build", 5, BILDSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
40 X("to address", 0, TOSW) \
41 X("cc address", 0, CCSW) \
42 X("from address", 0, FROMSW) \
43 X("fcc mailbox", 0, FCCSW) \
44 X("width columns", 0, WIDTHSW) \
45 X("subject text", 0, SUBJECTSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(COMP
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(COMP
, switches
);
55 #define DISPO_SWITCHES \
57 X("replace", 0, YESW) \
59 X("list", 0, LISTDSW) \
60 X("refile +folder", 0, REFILSW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(DISPO
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
71 static struct swit aqrul
[] = {
73 { "replace", 0, YESW
},
75 { "list", 0, LISTDSW
},
76 { "refile", 0, REFILSW
},
81 main (int argc
, char **argv
)
87 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
88 int outputlinelen
= OUTPUTLINELEN
;
89 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
90 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
91 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
92 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
94 char drft
[BUFSIZ
], **argp
, **arguments
;
95 struct msgs
*mp
= NULL
;
99 if (nmh_init(argv
[0], true, true)) { return 1; }
101 arguments
= getarguments (invo_name
, argc
, argv
, 1);
104 while ((cp
= *argp
++)) {
106 switch (smatch (++cp
, switches
)) {
108 ambigsw (cp
, switches
);
111 die("-%s unknown", cp
);
114 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
116 print_help (buf
, switches
, 1);
119 print_version(invo_name
);
123 if (!(ed
= *argp
++) || *ed
== '-')
124 die("missing argument to %s", argp
[-2]);
132 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
133 die("missing argument to %s", argp
[-2]);
145 if (!(form
= *argp
++) || *form
== '-')
146 die("missing argument to %s", argp
[-2]);
156 case FILESW
: /* compatibility */
158 die("only one file at a time!");
159 if (!(file
= *argp
++) || *file
== '-')
160 die("missing argument to %s", argp
[-2]);
166 die("only one draft folder at a time!");
167 if (!(cp
= *argp
++) || *cp
== '-')
168 die("missing argument to %s", argp
[-2]);
169 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
170 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
174 die("only one draft message at a time!");
175 if (!(file
= *argp
++) || *file
== '-')
176 die("missing argument to %s", argp
[-2]);
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
186 to
= addlist(to
, cp
);
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
192 cc
= addlist(cc
, cp
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
198 from
= addlist(from
, cp
);
202 if (!(cp
= *argp
++) || *cp
== '-')
203 die("missing argument to %s", argp
[-2]);
206 cp
= dp
= path(cp
+ 1, TSUBCWF
);
207 fcc
= addlist(fcc
, cp
);
212 if (!(cp
= *argp
++) || *cp
== '-')
213 die("missing argument to %s", argp
[-2]);
214 if ((outputlinelen
= atoi(cp
)) < 10)
215 die("impossible width %d", outputlinelen
);
219 if (!(cp
= *argp
++) || *cp
== '-')
220 die("missing argument to %s", argp
[-2]);
225 if (*cp
== '+' || *cp
== '@') {
227 die("only one folder at a time!");
228 folder
= pluspath (cp
);
231 die("only one message at a time!");
236 cwd
= mh_xstrdup(pwd ());
238 if (!context_find ("path"))
239 free (path ("./", TFOLDER
));
242 * Check if we are using a draft folder
243 * and have specified a message in it.
245 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
249 if (form
&& (folder
|| msg
))
250 die("can't mix forms and folders/msgs");
256 * Use a message as the "form" for the new message.
261 folder
= getfolder (1);
262 maildir
= m_maildir (folder
);
264 if (chdir (maildir
) == NOTOK
)
265 adios (maildir
, "unable to change directory to");
267 /* read folder and create message structure */
268 if (!(mp
= folder_read (folder
, 1)))
269 die("unable to read folder %s", folder
);
271 /* check for empty folder */
273 die("no messages in %s", folder
);
275 /* parse the message range/sequence/name and set SELECTED */
276 if (!m_convert (mp
, msg
))
278 seq_setprev (mp
); /* set the previous-sequence */
282 die("only one message at a time!");
284 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
285 adios (form
, "unable to open message");
292 cp
= new_fs(form
, NULL
, NULL
);
293 format_len
= strlen(cp
);
294 fmt_compile(cp
, &fmt
, 1);
297 * Set up any components that were fed to us on the command line
301 cptr
= fmt_findcomp("from");
306 cptr
= fmt_findcomp("to");
311 cptr
= fmt_findcomp("cc");
316 cptr
= fmt_findcomp("fcc");
321 cptr
= fmt_findcomp("subject");
323 cptr
->c_text
= subject
;
328 strncpy (drft
, build
? m_maildir ("draft")
329 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
332 * Check if we have an existing draft
334 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
335 i
= fdcompare (in
, out
);
339 * If we have given -use flag, or if the
340 * draft is just the same as the components
341 * file, then no need to ask any questions.
346 if (stat (drft
, &st
) == NOTOK
)
347 adios (drft
, "unable to stat");
348 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
349 for (i
= LISTDSW
; i
!= YESW
;) {
350 if (!(argp
= read_switch_multiword ("\nDisposition? ",
351 isdf
? aqrunl
: aqrul
)))
353 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
366 showfile (++argp
, drft
);
369 if (refile (++argp
, drft
) == 0)
379 adios (drft
, "unable to open");
382 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
383 adios (drft
, "unable to create");
387 i
= format_len
+ 1024;
388 scanl
= charstring_create (i
+ 2);
392 dat
[3] = outputlinelen
;
394 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
395 if (write(out
, charstring_buffer (scanl
),
396 charstring_bytes (scanl
)) < 0) {
397 advise (drft
, "write");
399 charstring_free(scanl
);
401 cpydata (in
, out
, form
, drft
);
407 context_save (); /* save the context file */
411 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);