]>
diplodocus.org Git - nmh/blob - uip/comp.c
3 * comp.c -- compose a message
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 static struct swit switches
[] = {
17 { "draftfolder +folder", 0 },
19 { "draftmessage msg", 0 },
21 { "nodraftfolder", 0 },
23 { "editor editor", 0 },
29 { "form formfile", 0 },
35 { "whatnowproc program", 0 },
37 { "nowhatnowproc", 0 },
45 static struct swit aqrunl
[] = {
55 { "refile +folder", 0 },
61 static struct swit aqrul
[] = {
72 main (int argc
, char **argv
)
74 int use
= NOUSE
, nedit
= 0, nwhat
= 0;
75 int i
, in
, isdf
= 0, out
;
76 char *cp
, *cwd
, *maildir
, *dfolder
= NULL
;
77 char *ed
= NULL
, *file
= NULL
, *form
= NULL
;
78 char *folder
= NULL
, *msg
= NULL
, buf
[BUFSIZ
];
79 char drft
[BUFSIZ
], **argp
, **arguments
;
80 struct msgs
*mp
= NULL
;
84 setlocale(LC_ALL
, "");
86 invo_name
= r1bindex (argv
[0], '/');
88 /* read user profile/context */
91 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
101 adios (NULL
, "-%s unknown", cp
);
104 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
106 print_help (buf
, switches
, 1);
109 print_version(invo_name
);
113 if (!(ed
= *argp
++) || *ed
== '-')
114 adios (NULL
, "missing argument to %s", argp
[-2]);
122 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
123 adios (NULL
, "missing argument to %s", argp
[-2]);
131 if (!(form
= *argp
++) || *form
== '-')
132 adios (NULL
, "missing argument to %s", argp
[-2]);
142 case FILESW
: /* compatibility */
144 adios (NULL
, "only one file at a time!");
145 if (!(file
= *argp
++) || *file
== '-')
146 adios (NULL
, "missing argument to %s", argp
[-2]);
152 adios (NULL
, "only one draft folder at a time!");
153 if (!(cp
= *argp
++) || *cp
== '-')
154 adios (NULL
, "missing argument to %s", argp
[-2]);
155 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
156 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
160 adios (NULL
, "only one draft message at a time!");
161 if (!(file
= *argp
++) || *file
== '-')
162 adios (NULL
, "missing argument to %s", argp
[-2]);
170 if (*cp
== '+' || *cp
== '@') {
172 adios (NULL
, "only one folder at a time!");
174 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
177 adios (NULL
, "only one message at a time!");
183 cwd
= getcpy (pwd ());
185 if (!context_find ("path"))
186 free (path ("./", TFOLDER
));
189 * Check if we are using a draft folder
190 * and have specified a message in it.
192 if ((dfolder
|| context_find ("Draft-Folder")) && !folder
&& msg
&& !file
) {
196 if (form
&& (folder
|| msg
))
197 adios (NULL
, "can't mix forms and folders/msgs");
201 * Use a message as the "form" for the new message.
206 folder
= getfolder (1);
207 maildir
= m_maildir (folder
);
209 if (chdir (maildir
) == NOTOK
)
210 adios (maildir
, "unable to change directory to");
212 /* read folder and create message structure */
213 if (!(mp
= folder_read (folder
)))
214 adios (NULL
, "unable to read folder %s", folder
);
216 /* check for empty folder */
218 adios (NULL
, "no messages in %s", folder
);
220 /* parse the message range/sequence/name and set SELECTED */
221 if (!m_convert (mp
, msg
))
223 seq_setprev (mp
); /* set the previous-sequence */
226 adios (NULL
, "only one message at a time!");
228 if ((in
= open (form
= getcpy (m_name (mp
->lowsel
)), O_RDONLY
)) == NOTOK
)
229 adios (form
, "unable to open message");
232 * Open a component or forms file
235 if ((in
= open (etcpath (form
), O_RDONLY
)) == NOTOK
)
236 adios (form
, "unable to open form file");
238 if ((in
= open (etcpath (components
), O_RDONLY
)) == NOTOK
)
239 adios (components
, "unable to open default components file");
245 strncpy (drft
, m_draft (dfolder
, file
, use
, &isdf
), sizeof(drft
));
248 * Check if we have an existing draft
250 if ((out
= open (drft
, O_RDONLY
)) != NOTOK
) {
251 i
= fdcompare (in
, out
);
255 * If we have given -use flag, or if the
256 * draft is just the same as the components
257 * file, then no need to ask any questions.
262 if (stat (drft
, &st
) == NOTOK
)
263 adios (drft
, "unable to stat");
264 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
265 for (i
= LISTDSW
; i
!= YESW
;) {
266 if (!(argp
= getans ("\nDisposition? ", isdf
? aqrunl
: aqrul
)))
268 switch (i
= smatch (*argp
, isdf
? aqrunl
: aqrul
)) {
281 showfile (++argp
, drft
);
284 if (refile (++argp
, drft
) == 0)
288 advise (NULL
, "say what?");
294 adios (drft
, "unable to open");
297 if ((out
= creat (drft
, m_gmprot ())) == NOTOK
)
298 adios (drft
, "unable to create");
299 cpydata (in
, out
, form
, drft
);
304 context_save (); /* save the context file */
308 what_now (ed
, nedit
, use
, drft
, NULL
, 0, NULLMP
, NULL
, 0, cwd
);