]>
diplodocus.org Git - nmh/blob - uip/comp.c
0d59785153f09a851675fb942149ae1d8b93e0c8
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.
11 #include "h/fmt_scan.h"
13 #include "sbr/m_maildir.h"
16 #define COMP_SWITCHES \
17 X("draftfolder +folder", 0, DFOLDSW) \
18 X("draftmessage msg", 0, DMSGSW) \
19 X("nodraftfolder", 0, NDFLDSW) \
20 X("editor editor", 0, EDITRSW) \
21 X("noedit", 0, NEDITSW) \
22 X("file file", 0, FILESW) \
23 X("form formfile", 0, FORMSW) \
25 X("nouse", 0, NUSESW) \
26 X("whatnowproc program", 0, WHATSW) \
27 X("nowhatnowproc", 0, NWHATSW) \
28 X("build", 5, BILDSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
31 X("to address", 0, TOSW) \
32 X("cc address", 0, CCSW) \
33 X("from address", 0, FROMSW) \
34 X("fcc mailbox", 0, FCCSW) \
35 X("width columns", 0, WIDTHSW) \
36 X("subject text", 0, SUBJECTSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(COMP
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(COMP
, switches
);
46 #define DISPO_SWITCHES \
48 X("replace", 0, YESW) \
50 X("list", 0, LISTDSW) \
51 X("refile +folder", 0, REFILSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(DISPO
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(DISPO
, aqrunl
);
62 static struct swit aqrul
[] = {
64 { "replace", 0, YESW
},
66 { "list", 0, LISTDSW
},
67 { "refile", 0, REFILSW
},
72 main (int argc
, char **argv
)
78 int i
, in
= NOTOK
, isdf
= 0, out
, dat
[5], format_len
= 0;
79 int outputlinelen
= OUTPUTLINELEN
;
80 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
81 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
82 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
83 char *to
= NULL
, *from
= NULL
, *cc
= NULL
, *fcc
= NULL
, *dp
;
85 char drft
[BUFSIZ
], **argp
, **arguments
;
86 struct msgs
*mp
= NULL
;
90 if (nmh_init(argv
[0], true, true)) { return 1; }
92 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 while ((cp
= *argp
++)) {
97 switch (smatch (++cp
, switches
)) {
99 ambigsw (cp
, switches
);
102 die("-%s unknown", cp
);
105 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
107 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
114 if (!(ed
= *argp
++) || *ed
== '-')
115 die("missing argument to %s", argp
[-2]);
123 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
124 die("missing argument to %s", argp
[-2]);
136 if (!(form
= *argp
++) || *form
== '-')
137 die("missing argument to %s", argp
[-2]);
147 case FILESW
: /* compatibility */
149 die("only one file at a time!");
150 if (!(file
= *argp
++) || *file
== '-')
151 die("missing argument to %s", argp
[-2]);
157 die("only one draft folder at a time!");
158 if (!(cp
= *argp
++) || *cp
== '-')
159 die("missing argument to %s", argp
[-2]);
160 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
161 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
165 die("only one draft message at a time!");
166 if (!(file
= *argp
++) || *file
== '-')
167 die("missing argument to %s", argp
[-2]);
175 if (!(cp
= *argp
++) || *cp
== '-')
176 die("missing argument to %s", argp
[-2]);
177 to
= addlist(to
, cp
);
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 cc
= addlist(cc
, cp
);
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 from
= addlist(from
, cp
);
193 if (!(cp
= *argp
++) || *cp
== '-')
194 die("missing argument to %s", argp
[-2]);
197 cp
= dp
= path(cp
+ 1, TSUBCWF
);
198 fcc
= addlist(fcc
, cp
);
203 if (!(cp
= *argp
++) || *cp
== '-')
204 die("missing argument to %s", argp
[-2]);
205 if ((outputlinelen
= atoi(cp
)) < 10)
206 die("impossible width %d", outputlinelen
);
210 if (!(cp
= *argp
++) || *cp
== '-')
211 die("missing argument to %s", argp
[-2]);
216 if (*cp
== '+' || *cp
== '@') {
218 die("only one folder at a time!");
219 folder
= pluspath (cp
);
222 die("only one message at a time!");
227 cwd
= mh_xstrdup(pwd ());
229 if (!context_find ("path"))
230 free (path ("./", TFOLDER
));
233 * Check if we are using a draft folder
234 * and have specified a message in it.
236 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
240 if (form
&& (folder
|| msg
))
241 die("can't mix forms and folders/msgs");
247 * Use a message as the "form" for the new message.
252 folder
= getfolder (1);
253 maildir
= m_maildir (folder
);
255 if (chdir (maildir
) == NOTOK
)
256 adios (maildir
, "unable to change directory to");
258 /* read folder and create message structure */
259 if (!(mp
= folder_read (folder
, 1)))
260 die("unable to read folder %s", folder
);
262 /* check for empty folder */
264 die("no messages in %s", folder
);
266 /* parse the message range/sequence/name and set SELECTED */
267 if (!m_convert (mp
, msg
))
269 seq_setprev (mp
); /* set the previous-sequence */
273 die("only one message at a time!");
275 if ((in
= open (form
= mh_xstrdup(m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
276 adios (form
, "unable to open message");
283 cp
= new_fs(form
, NULL
, NULL
);
284 format_len
= strlen(cp
);
285 fmt_compile(cp
, &fmt
, 1);
288 * Set up any components that were fed to us on the command line
292 cptr
= fmt_findcomp("from");
297 cptr
= fmt_findcomp("to");
302 cptr
= fmt_findcomp("cc");
307 cptr
= fmt_findcomp("fcc");
312 cptr
= fmt_findcomp("subject");
314 cptr
->c_text
= subject
;
319 strncpy (drft
, build
? m_maildir ("draft")
320 : m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
323 * Check if we have an existing draft
325 if (!build
&& (out
= open (drft
, O_RDONLY
)) != NOTOK
) {
326 i
= fdcompare (in
, out
);
330 * If we have given -use flag, or if the
331 * draft is just the same as the components
332 * file, then no need to ask any questions.
337 if (stat (drft
, &st
) == NOTOK
)
338 adios (drft
, "unable to stat");
339 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
340 for (i
= LISTDSW
; i
!= YESW
;) {
341 if (!(argp
= read_switch_multiword ("\nDisposition? ",
342 isdf
? aqrunl
: aqrul
)))
344 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
357 showfile (++argp
, drft
);
360 if (refile (++argp
, drft
) == 0)
370 adios (drft
, "unable to open");
373 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
374 adios (drft
, "unable to create");
378 i
= format_len
+ 1024;
379 scanl
= charstring_create (i
+ 2);
383 dat
[3] = outputlinelen
;
385 fmt_scan(fmt
, scanl
, i
, dat
, NULL
);
386 if (write(out
, charstring_buffer (scanl
),
387 charstring_bytes (scanl
)) < 0) {
388 advise (drft
, "write");
390 charstring_free(scanl
);
392 cpydata (in
, out
, form
, drft
);
398 context_save (); /* save the context file */
402 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULL
, NULL
, 0, cwd
, 0);