]>
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 "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
14 #include "h/fmt_scan.h"
16 #include "sbr/m_maildir.h"
19 #define COMP_SWITCHES \
20 X("draftfolder +folder", 0, DFOLDSW) \
21 X("draftmessage msg", 0, DMSGSW) \
22 X("nodraftfolder", 0, NDFLDSW) \
23 X("editor editor", 0, EDITRSW) \
24 X("noedit", 0, NEDITSW) \
25 X("file file", 0, FILESW) \
26 X("form formfile", 0, FORMSW) \
28 X("nouse", 0, NUSESW) \
29 X("whatnowproc program", 0, WHATSW) \
30 X("nowhatnowproc", 0, NWHATSW) \
31 X("build", 5, BILDSW) \
32 X("version", 0, VERSIONSW) \
33 X("help", 0, HELPSW) \
34 X("to address", 0, TOSW) \
35 X("cc address", 0, CCSW) \
36 X("from address", 0, FROMSW) \
37 X("fcc mailbox", 0, FCCSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("subject text", 0, SUBJECTSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(COMP
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(COMP
, switches
);
49 #define DISPO_SWITCHES \
51 X("replace", 0, YESW) \
53 X("list", 0, LISTDSW) \
54 X("refile +folder", 0, REFILSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(DISPO
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
65 static struct swit aqrul
[] = {
67 { "replace", 0, YESW
},
69 { "list", 0, LISTDSW
},
70 { "refile", 0, REFILSW
},
75 main (int argc
, char **argv
)
81 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
82 int outputlinelen
= OUTPUTLINELEN
;
83 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
84 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
85 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
86 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
88 char drft
[BUFSIZ
], **argp
, **arguments
;
89 struct msgs
*mp
= NULL
;
93 if (nmh_init(argv
[0], true, true)) { return 1; }
95 arguments
= getarguments (invo_name
, argc
, argv
, 1);
98 while ((cp
= *argp
++)) {
100 switch (smatch (++cp
, switches
)) {
102 ambigsw (cp
, switches
);
105 die("-%s unknown", cp
);
108 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
110 print_help (buf
, switches
, 1);
113 print_version(invo_name
);
117 if (!(ed
= *argp
++) || *ed
== '-')
118 die("missing argument to %s", argp
[-2]);
126 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
127 die("missing argument to %s", argp
[-2]);
139 if (!(form
= *argp
++) || *form
== '-')
140 die("missing argument to %s", argp
[-2]);
150 case FILESW
: /* compatibility */
152 die("only one file at a time!");
153 if (!(file
= *argp
++) || *file
== '-')
154 die("missing argument to %s", argp
[-2]);
160 die("only one draft folder at a time!");
161 if (!(cp
= *argp
++) || *cp
== '-')
162 die("missing argument to %s", argp
[-2]);
163 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
164 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
168 die("only one draft message at a time!");
169 if (!(file
= *argp
++) || *file
== '-')
170 die("missing argument to %s", argp
[-2]);
178 if (!(cp
= *argp
++) || *cp
== '-')
179 die("missing argument to %s", argp
[-2]);
180 to
= addlist(to
, cp
);
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
186 cc
= addlist(cc
, cp
);
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
192 from
= addlist(from
, cp
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
200 cp
= dp
= path(cp
+ 1, TSUBCWF
);
201 fcc
= addlist(fcc
, cp
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 die("missing argument to %s", argp
[-2]);
208 if ((outputlinelen
= atoi(cp
)) < 10)
209 die("impossible width %d", outputlinelen
);
213 if (!(cp
= *argp
++) || *cp
== '-')
214 die("missing argument to %s", argp
[-2]);
219 if (*cp
== '+' || *cp
== '@') {
221 die("only one folder at a time!");
222 folder
= pluspath (cp
);
225 die("only one message at a time!");
230 cwd
= mh_xstrdup(pwd ());
232 if (!context_find ("path"))
233 free (path ("./", TFOLDER
));
236 * Check if we are using a draft folder
237 * and have specified a message in it.
239 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
243 if (form
&& (folder
|| msg
))
244 die("can't mix forms and folders/msgs");
250 * Use a message as the "form" for the new message.
255 folder
= getfolder (1);
256 maildir
= m_maildir (folder
);
258 if (chdir (maildir
) == NOTOK
)
259 adios (maildir
, "unable to change directory to");
261 /* read folder and create message structure */
262 if (!(mp
= folder_read (folder
, 1)))
263 die("unable to read folder %s", folder
);
265 /* check for empty folder */
267 die("no messages in %s", folder
);
269 /* parse the message range/sequence/name and set SELECTED */
270 if (!m_convert (mp
, msg
))
272 seq_setprev (mp
); /* set the previous-sequence */
276 die("only one message at a time!");
278 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
279 adios (form
, "unable to open message");
286 cp
= new_fs(form
, NULL
, NULL
);
287 format_len
= strlen(cp
);
288 fmt_compile(cp
, &fmt
, 1);
291 * Set up any components that were fed to us on the command line
295 cptr
= fmt_findcomp("from");
300 cptr
= fmt_findcomp("to");
305 cptr
= fmt_findcomp("cc");
310 cptr
= fmt_findcomp("fcc");
315 cptr
= fmt_findcomp("subject");
317 cptr
->c_text
= subject
;
322 strncpy (drft
, build
? m_maildir ("draft")
323 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
326 * Check if we have an existing draft
328 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
329 i
= fdcompare (in
, out
);
333 * If we have given -use flag, or if the
334 * draft is just the same as the components
335 * file, then no need to ask any questions.
340 if (stat (drft
, &st
) == NOTOK
)
341 adios (drft
, "unable to stat");
342 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
343 for (i
= LISTDSW
; i
!= YESW
;) {
344 if (!(argp
= read_switch_multiword ("\nDisposition? ",
345 isdf
? aqrunl
: aqrul
)))
347 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
360 showfile (++argp
, drft
);
363 if (refile (++argp
, drft
) == 0)
373 adios (drft
, "unable to open");
376 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
377 adios (drft
, "unable to create");
381 i
= format_len
+ 1024;
382 scanl
= charstring_create (i
+ 2);
386 dat
[3] = outputlinelen
;
388 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
389 if (write(out
, charstring_buffer (scanl
),
390 charstring_bytes (scanl
)) < 0) {
391 advise (drft
, "write");
393 charstring_free(scanl
);
395 cpydata (in
, out
, form
, drft
);
401 context_save (); /* save the context file */
405 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);