]>
diplodocus.org Git - nmh/blob - uip/comp.c
3 * comp.c -- compose a message
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/fmt_scan.h>
15 #define COMP_SWITCHES \
16 X("draftfolder +folder", 0, DFOLDSW) \
17 X("draftmessage msg", 0, DMSGSW) \
18 X("nodraftfolder", 0, NDFLDSW) \
19 X("editor editor", 0, EDITRSW) \
20 X("noedit", 0, NEDITSW) \
21 X("file file", 0, FILESW) \
22 X("form formfile", 0, FORMSW) \
24 X("nouse", 0, NUSESW) \
25 X("whatnowproc program", 0, WHATSW) \
26 X("nowhatnowproc", 0, NWHATSW) \
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 colums", 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;
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
;
86 setlocale(LC_ALL
, "");
88 invo_name
= r1bindex (argv
[0], '/');
90 /* read user profile/context */
93 arguments
= getarguments (invo_name
, argc
, argv
, 1);
96 while ((cp
= *argp
++)) {
98 switch (smatch (++cp
, switches
)) {
100 ambigsw (cp
, switches
);
103 adios (NULL
, "-%s unknown", cp
);
106 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
108 print_help (buf
, switches
, 1);
111 print_version(invo_name
);
115 if (!(ed
= *argp
++) || *ed
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
124 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
125 adios (NULL
, "missing argument to %s", argp
[-2]);
133 if (!(form
= *argp
++) || *form
== '-')
134 adios (NULL
, "missing argument to %s", argp
[-2]);
144 case FILESW
: /* compatibility */
146 adios (NULL
, "only one file at a time!");
147 if (!(file
= *argp
++) || *file
== '-')
148 adios (NULL
, "missing argument to %s", argp
[-2]);
154 adios (NULL
, "only one draft folder at a time!");
155 if (!(cp
= *argp
++) || *cp
== '-')
156 adios (NULL
, "missing argument to %s", argp
[-2]);
157 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
158 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
162 adios (NULL
, "only one draft message at a time!");
163 if (!(file
= *argp
++) || *file
== '-')
164 adios (NULL
, "missing argument to %s", argp
[-2]);
172 if (!(cp
= *argp
++) || *cp
== '-')
173 adios (NULL
, "missing argument to %s", argp
[-2]);
174 to
= addlist(to
, cp
);
178 if (!(cp
= *argp
++) || *cp
== '-')
179 adios (NULL
, "missing argument to %s", argp
[-2]);
180 cc
= addlist(cc
, cp
);
184 if (!(cp
= *argp
++) || *cp
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
186 from
= addlist(from
, cp
);
190 if (!(cp
= *argp
++) || *cp
== '-')
191 adios (NULL
, "missing argument to %s", argp
[-2]);
194 cp
= dp
= path(cp
+ 1, TSUBCWF
);
195 fcc
= addlist(fcc
, cp
);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 adios (NULL
, "missing argument to %s", argp
[-2]);
203 if ((outputlinelen
= atoi(cp
)) < 10)
204 adios (NULL
, "impossible width %d", outputlinelen
);
208 if (!(cp
= *argp
++) || *cp
== '-')
209 adios (NULL
, "missing argument to %s", argp
[-2]);
214 if (*cp
== '+' || *cp
== '@') {
216 adios (NULL
, "only one folder at a time!");
218 folder
= pluspath (cp
);
221 adios (NULL
, "only one message at a time!");
227 cwd
= getcpy (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 adios (NULL
, "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
)))
260 adios (NULL
, "unable to read folder %s", folder
);
262 /* check for empty folder */
264 adios (NULL
, "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 */
272 adios (NULL
, "only one message at a time!");
274 if ((in
= open (form
= getcpy (m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
275 adios (form
, "unable to open message");
282 cp
= new_fs(form
, NULL
, NULL
);
283 format_len
= strlen(cp
);
284 fmt_compile(cp
, &fmt
, 1);
287 * Set up any components that were fed to us on the command line
291 cptr
= fmt_findcomp("from");
296 cptr
= fmt_findcomp("to");
301 cptr
= fmt_findcomp("cc");
306 cptr
= fmt_findcomp("fcc");
311 cptr
= fmt_findcomp("subject");
313 cptr
->c_text
= subject
;
318 strncpy (drft
, m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
321 * Check if we have an existing draft
323 if ((out
= open (drft
, O_RDONLY
)) != NOTOK
) {
324 i
= fdcompare (in
, out
);
328 * If we have given -use flag, or if the
329 * draft is just the same as the components
330 * file, then no need to ask any questions.
335 if (stat (drft
, &st
) == NOTOK
)
336 adios (drft
, "unable to stat");
337 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
338 for (i
= LISTDSW
; i
!= YESW
;) {
339 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrunl
: aqrul
)))
341 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
354 showfile (++argp
, drft
);
357 if (refile (++argp
, drft
) == 0)
361 advise (NULL
, "say what?");
367 adios (drft
, "unable to open");
370 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
371 adios (drft
, "unable to create");
375 i
= format_len
+ 1024;
376 scanl
= mh_xmalloc((size_t) i
+ 2);
380 dat
[3] = outputlinelen
;
382 fmt_scan(fmt
, scanl
, i
+ 1, i
, dat
);
383 write(out
, scanl
, strlen(scanl
));
386 cpydata (in
, out
, form
, drft
);
392 context_save (); /* save the context file */
396 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULLMP
, NULL
, 0, cwd
, 0);