xref: /netbsd-src/usr.bin/mail/cmd2.c (revision 4fe1ef32f37334167b71a62286b21da7d9542909)
1*4fe1ef32Schristos /*	$NetBSD: cmd2.c,v 1.26 2017/11/09 20:27:50 christos Exp $	*/
288b833a7Schristos 
361f28255Scgd /*
42cb5542fSderaadt  * Copyright (c) 1980, 1993
52cb5542fSderaadt  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
327c81c8f3Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3488b833a7Schristos #if 0
3588b833a7Schristos static char sccsid[] = "@(#)cmd2.c	8.1 (Berkeley) 6/6/93";
3688b833a7Schristos #else
37*4fe1ef32Schristos __RCSID("$NetBSD: cmd2.c,v 1.26 2017/11/09 20:27:50 christos Exp $");
3888b833a7Schristos #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #include "rcv.h"
428207b28aSchristos #include <util.h>
432cb5542fSderaadt #include "extern.h"
44f3098750Schristos #ifdef MIME_SUPPORT
45f3098750Schristos #include "mime.h"
46f3098750Schristos #endif
47f3098750Schristos #include "thread.h"
4861f28255Scgd 
4961f28255Scgd /*
5061f28255Scgd  * Mail -- a mail program
5161f28255Scgd  *
5261f28255Scgd  * More user commands.
5361f28255Scgd  */
5461f28255Scgd 
5561f28255Scgd /*
5661f28255Scgd  * If any arguments were given, go to the next applicable argument
5761f28255Scgd  * following dot, otherwise, go to the next applicable message.
5861f28255Scgd  * If given as first command with no arguments, print first message.
5961f28255Scgd  */
60f3098750Schristos PUBLIC int
next(void * v)61b127ccccSwiz next(void *v)
6261f28255Scgd {
63ca13337dSchristos 	int *msgvec;
647c81c8f3Slukem 	struct message *mp;
657c81c8f3Slukem 	int *ip, *ip2;
6661f28255Scgd 	int list[2], mdot;
6761f28255Scgd 
68ca13337dSchristos 	msgvec = v;
69f890b048Spk 	if (*msgvec != 0) {
7061f28255Scgd 
7161f28255Scgd 		/*
7261f28255Scgd 		 * If some messages were supplied, find the
7361f28255Scgd 		 * first applicable one following dot using
7461f28255Scgd 		 * wrap around.
7561f28255Scgd 		 */
76f3098750Schristos 		mdot = get_msgnum(dot);
7761f28255Scgd 
7861f28255Scgd 		/*
7961f28255Scgd 		 * Find the first message in the supplied
8061f28255Scgd 		 * message list which follows dot.
8161f28255Scgd 		 */
8261f28255Scgd 
83f890b048Spk 		for (ip = msgvec; *ip != 0; ip++)
8461f28255Scgd 			if (*ip > mdot)
8561f28255Scgd 				break;
86f890b048Spk 		if (*ip == 0)
8761f28255Scgd 			ip = msgvec;
8861f28255Scgd 		ip2 = ip;
8961f28255Scgd 		do {
90f3098750Schristos 			mp = get_message(*ip2);
9161f28255Scgd 			if ((mp->m_flag & MDELETED) == 0) {
9261f28255Scgd 				dot = mp;
9361f28255Scgd 				goto hitit;
9461f28255Scgd 			}
95f890b048Spk 			if (*ip2 != 0)
9661f28255Scgd 				ip2++;
97f890b048Spk 			if (*ip2 == 0)
9861f28255Scgd 				ip2 = msgvec;
9961f28255Scgd 		} while (ip2 != ip);
100ca286310Schristos 		(void)printf("No messages applicable\n");
101f3098750Schristos 		return 1;
10261f28255Scgd 	}
10361f28255Scgd 
10461f28255Scgd 	/*
10561f28255Scgd 	 * If this is the first command, select message 1.
10661f28255Scgd 	 * Note that this must exist for us to get here at all.
10761f28255Scgd 	 */
10861f28255Scgd 
10961f28255Scgd 	if (!sawcom)
11061f28255Scgd 		goto hitit;
11161f28255Scgd 
11261f28255Scgd 	/*
11361f28255Scgd 	 * Just find the next good message after dot, no
11461f28255Scgd 	 * wraparound.
11561f28255Scgd 	 */
11661f28255Scgd 
117f3098750Schristos 	for (mp = next_message(dot); mp; mp = next_message(mp))
11861f28255Scgd 		if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
11961f28255Scgd 			break;
120f3098750Schristos 
121f3098750Schristos 	if (mp == NULL) {
122ca286310Schristos 		(void)printf("At EOF\n");
123f3098750Schristos 		return 0;
12461f28255Scgd 	}
12561f28255Scgd 	dot = mp;
12661f28255Scgd hitit:
12761f28255Scgd 	/*
12861f28255Scgd 	 * Print dot.
12961f28255Scgd 	 */
13061f28255Scgd 
131f3098750Schristos 	list[0] = get_msgnum(dot);
132f890b048Spk 	list[1] = 0;
133f3098750Schristos 	return type(list);
13461f28255Scgd }
13561f28255Scgd 
13661f28255Scgd /*
13761f28255Scgd  * Snarf the file from the end of the command line and
13861f28255Scgd  * return a pointer to it.  If there is no file attached,
139ab850155Swiz  * just return NULL.  Put a null in front of the file
14061f28255Scgd  * name so that the message list processing won't see it,
14161f28255Scgd  * unless the file name is the only thing on the line, in
14261f28255Scgd  * which case, return 0 in the reference flag variable.
14361f28255Scgd  */
144f3098750Schristos static char *
snarf(char linebuf[],int * flag,const char * string)145f3098750Schristos snarf(char linebuf[], int *flag, const char *string)
14661f28255Scgd {
1477c81c8f3Slukem 	char *cp;
14861f28255Scgd 
14961f28255Scgd 	*flag = 1;
15061f28255Scgd 	cp = strlen(linebuf) + linebuf - 1;
15161f28255Scgd 
15261f28255Scgd 	/*
15361f28255Scgd 	 * Strip away trailing blanks.
15461f28255Scgd 	 */
155ca13337dSchristos 	while (cp >= linebuf && isspace((unsigned char)*cp))
15661f28255Scgd 		cp--;
157ca13337dSchristos 	*++cp = '\0';
15861f28255Scgd 
15961f28255Scgd 	/*
16061f28255Scgd 	 * Now search for the beginning of the file name.
16161f28255Scgd 	 */
16209edb003Schristos 	while (cp > linebuf && !isspace((unsigned char)*cp))
16361f28255Scgd 		cp--;
16461f28255Scgd 	if (*cp == '\0') {
165f3098750Schristos 		(void)printf("No %s specified.\n", string);
166f3098750Schristos 		return NULL;
16761f28255Scgd 	}
16809edb003Schristos 	if (isspace((unsigned char)*cp))
169ca13337dSchristos 		*cp++ = '\0';
17061f28255Scgd 	else
17161f28255Scgd 		*flag = 0;
172f3098750Schristos 	return cp;
173f3098750Schristos }
174f3098750Schristos 
175f3098750Schristos struct save1_core_args_s {
176f3098750Schristos 	FILE *obuf;
177f3098750Schristos 	struct ignoretab *igtab;
178f3098750Schristos 	int markmsg;
179f3098750Schristos };
180f3098750Schristos static int
save1_core(struct message * mp,void * v)181f3098750Schristos save1_core(struct message *mp, void *v)
182f3098750Schristos {
183f3098750Schristos 	struct save1_core_args_s *args;
184f3098750Schristos 
185ca13337dSchristos 	args = v;
186f3098750Schristos 	touch(mp);
187f3098750Schristos 
188f3098750Schristos 	if (sendmessage(mp, args->obuf, args->igtab, NULL, NULL) < 0)
189f3098750Schristos 		return -1;
190f3098750Schristos 
191f3098750Schristos 	if (args->markmsg)
192f3098750Schristos 		mp->m_flag |= MSAVED;
193f3098750Schristos 
194f3098750Schristos 	return 0;
195f3098750Schristos }
196f3098750Schristos 
197f3098750Schristos /*
198f3098750Schristos  * Save/copy the indicated messages at the end of the passed file name.
199f3098750Schristos  * If markmsg is true, mark the message "saved."
200f3098750Schristos  */
201f3098750Schristos static int
save1(char str[],int markmsg,const char * cmd,struct ignoretab * igtab)202f3098750Schristos save1(char str[], int markmsg, const char *cmd, struct ignoretab *igtab)
203f3098750Schristos {
204f3098750Schristos 	int *ip;
205f3098750Schristos 	const char *fn;
206f3098750Schristos 	const char *disp;
207f3098750Schristos 	int f, *msgvec;
208f3098750Schristos 	int msgCount;
209f3098750Schristos 	FILE *obuf;
210f3098750Schristos 
211f3098750Schristos 	msgCount = get_msgCount();
2124556f89aSchristos 	msgvec = salloc((msgCount + 2) * sizeof(*msgvec));
213f3098750Schristos 	if ((fn = snarf(str, &f, "file")) == NULL)
214f3098750Schristos 		return 1;
215f3098750Schristos 	if (!f) {
216f3098750Schristos 		*msgvec = first(0, MMNORM);
217f3098750Schristos 		if (*msgvec == 0) {
218f3098750Schristos 			(void)printf("No messages to %s.\n", cmd);
219f3098750Schristos 			return 1;
220f3098750Schristos 		}
221f3098750Schristos 		msgvec[1] = 0;
222f3098750Schristos 	}
223f3098750Schristos 	if (f && getmsglist(str, msgvec, 0) < 0)
224f3098750Schristos 		return 1;
225f3098750Schristos 	if ((fn = expand(fn)) == NULL)
226f3098750Schristos 		return 1;
227f3098750Schristos 	(void)printf("\"%s\" ", fn);
228f3098750Schristos 	(void)fflush(stdout);
229f3098750Schristos 	if (access(fn, 0) >= 0)
230f3098750Schristos 		disp = "[Appended]";
231f3098750Schristos 	else
232f3098750Schristos 		disp = "[New file]";
233*4fe1ef32Schristos 	if ((obuf = Fopen(fn, "aef")) == NULL) {
234f3098750Schristos 		warn(NULL);
235f3098750Schristos 		return 1;
236f3098750Schristos 	}
237f3098750Schristos 	for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
238f3098750Schristos 		struct save1_core_args_s args;
239f3098750Schristos 		struct message *mp;
240f3098750Schristos 
241f3098750Schristos 		args.obuf = obuf;
242f3098750Schristos 		args.igtab = igtab;
243f3098750Schristos 		args.markmsg = markmsg;
244f3098750Schristos 		mp = get_message(*ip);
245f3098750Schristos 		if (thread_recursion(mp, save1_core, &args)) {
246f3098750Schristos 			warn("%s", fn);
247f3098750Schristos 			(void)Fclose(obuf);
248f3098750Schristos 			return 1;
249f3098750Schristos 		}
250f3098750Schristos 	}
251f3098750Schristos 	(void)fflush(obuf);
252f3098750Schristos 	if (ferror(obuf))
253f3098750Schristos 		warn("%s", fn);
254f3098750Schristos 	(void)Fclose(obuf);
255f3098750Schristos 	(void)printf("%s\n", disp);
256f3098750Schristos 	return 0;
257f3098750Schristos }
258f3098750Schristos 
259f3098750Schristos /*
260f3098750Schristos  * Save a message in a file.  Mark the message as saved
261f3098750Schristos  * so we can discard when the user quits.
262f3098750Schristos  */
263f3098750Schristos PUBLIC int
save(void * v)264f3098750Schristos save(void *v)
265f3098750Schristos {
266ca13337dSchristos 	char *str;
267f3098750Schristos 
268ca13337dSchristos 	str = v;
269f3098750Schristos 	return save1(str, 1, "save", saveignore);
270f3098750Schristos }
271f3098750Schristos 
272f3098750Schristos /*
273f3098750Schristos  * Save a message in a file.  Mark the message as saved
274f3098750Schristos  * so we can discard when the user quits.  Save all fields
275f3098750Schristos  * overriding saveignore and saveretain.
276f3098750Schristos  */
277f3098750Schristos PUBLIC int
Save(void * v)278f3098750Schristos Save(void *v)
279f3098750Schristos {
280ca13337dSchristos 	char *str;
281f3098750Schristos 
282ca13337dSchristos 	str = v;
283f3098750Schristos 	return save1(str, 1, "Save", NULL);
284f3098750Schristos }
285f3098750Schristos 
286f3098750Schristos /*
287f3098750Schristos  * Copy a message to a file without affected its saved-ness
288f3098750Schristos  */
289f3098750Schristos PUBLIC int
copycmd(void * v)290f3098750Schristos copycmd(void *v)
291f3098750Schristos {
292ca13337dSchristos 	char *str;
293f3098750Schristos 
294ca13337dSchristos 	str = v;
295f3098750Schristos 	return save1(str, 0, "copy", saveignore);
296f3098750Schristos }
297f3098750Schristos 
298f3098750Schristos /*
299f3098750Schristos  * Write the indicated messages at the end of the passed
300f3098750Schristos  * file name, minus header and trailing blank line.
301f3098750Schristos  */
302f3098750Schristos PUBLIC int
swrite(void * v)303f3098750Schristos swrite(void *v)
304f3098750Schristos {
305ca13337dSchristos 	char *str;
306f3098750Schristos 
307ca13337dSchristos 	str = v;
308f3098750Schristos 	return save1(str, 1, "write", ignoreall);
309f3098750Schristos }
310f3098750Schristos 
311f3098750Schristos /*
312f3098750Schristos  * Delete the indicated messages.
313f3098750Schristos  * Set dot to some nice place afterwards.
314f3098750Schristos  * Internal interface.
315f3098750Schristos  */
316f3098750Schristos static int
delm(int * msgvec)317f3098750Schristos delm(int *msgvec)
318f3098750Schristos {
319f3098750Schristos 	struct message *mp;
320f3098750Schristos 	int *ip;
321f3098750Schristos 	int last;
322f3098750Schristos 
323f3098750Schristos 	last = 0;
324f3098750Schristos 	for (ip = msgvec; *ip != 0; ip++) {
325f3098750Schristos 		mp = set_m_flag(*ip,
326f3098750Schristos 		    ~(MPRESERVE|MSAVED|MBOX|MDELETED|MTOUCH), MDELETED|MTOUCH);
327f3098750Schristos 		touch(mp);
328f3098750Schristos 		last = *ip;
329f3098750Schristos 	}
330f3098750Schristos 	if (last != 0) {
331f3098750Schristos 		dot = get_message(last);
332f3098750Schristos 		last = first(0, MDELETED);
333f3098750Schristos 		if (last != 0) {
334f3098750Schristos 			dot = get_message(last);
335f3098750Schristos 			return 0;
336f3098750Schristos 		}
337f3098750Schristos 		else {
338f3098750Schristos 			dot = get_message(1);
339f3098750Schristos 			return -1;
340f3098750Schristos 		}
341f3098750Schristos 	}
342f3098750Schristos 
343f3098750Schristos 	/*
344f3098750Schristos 	 * Following can't happen -- it keeps lint happy
345f3098750Schristos 	 */
346f3098750Schristos 	return -1;
34761f28255Scgd }
34861f28255Scgd 
34961f28255Scgd /*
35061f28255Scgd  * Delete messages.
35161f28255Scgd  */
352f3098750Schristos PUBLIC int
delete(void * v)353b127ccccSwiz delete(void *v)
35461f28255Scgd {
355ca13337dSchristos 	int *msgvec;
356ca13337dSchristos 
357ca13337dSchristos 	msgvec = v;
358ca286310Schristos 	(void)delm(msgvec);
35961f28255Scgd 	return 0;
36061f28255Scgd }
36161f28255Scgd 
36261f28255Scgd /*
36361f28255Scgd  * Delete messages, then type the new dot.
36461f28255Scgd  */
365f3098750Schristos PUBLIC int
deltype(void * v)366b127ccccSwiz deltype(void *v)
36761f28255Scgd {
368ca13337dSchristos 	int *msgvec;
36961f28255Scgd 	int list[2];
37061f28255Scgd 	int lastdot;
37161f28255Scgd 
372ca13337dSchristos 	msgvec = v;
373f3098750Schristos 	lastdot = get_msgnum(dot);
37461f28255Scgd 	if (delm(msgvec) >= 0) {
375f3098750Schristos 		list[0] = get_msgnum(dot);
37661f28255Scgd 		if (list[0] > lastdot) {
37761f28255Scgd 			touch(dot);
378f890b048Spk 			list[1] = 0;
379f3098750Schristos 			return type(list);
38061f28255Scgd 		}
381ca286310Schristos 		(void)printf("At EOF\n");
38261f28255Scgd 	} else
383ca286310Schristos 		(void)printf("No more messages\n");
384f3098750Schristos 	return 0;
38561f28255Scgd }
38661f28255Scgd 
38761f28255Scgd /*
38861f28255Scgd  * Undelete the indicated messages.
38961f28255Scgd  */
390f3098750Schristos PUBLIC int
undeletecmd(void * v)391b127ccccSwiz undeletecmd(void *v)
39261f28255Scgd {
393f3098750Schristos 	int msgCount;
394f3098750Schristos 	int *msgvec;
3957c81c8f3Slukem 	int *ip;
39661f28255Scgd 
397f3098750Schristos 	msgvec = v;
398f3098750Schristos 	msgCount = get_msgCount();
39961f28255Scgd 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
400f3098750Schristos 		dot = set_m_flag(*ip, ~MDELETED, 0);
401f3098750Schristos 		touch(dot);
402f3098750Schristos 		dot->m_flag &= ~MDELETED;
40361f28255Scgd 	}
40461f28255Scgd 	return 0;
40561f28255Scgd }
40661f28255Scgd 
407f3098750Schristos /*************************************************************************/
408f3098750Schristos 
40961f28255Scgd /*
41061f28255Scgd  * Interactively dump core on "core"
41161f28255Scgd  */
412ca286310Schristos /*ARGSUSED*/
413f3098750Schristos PUBLIC int
core(void * v __unused)4148207b28aSchristos core(void *v __unused)
41561f28255Scgd {
41661f28255Scgd 	int pid;
41761f28255Scgd 
41861f28255Scgd 	switch (pid = vfork()) {
41961f28255Scgd 	case -1:
420ae38aa87Swiz 		warn("fork");
421f3098750Schristos 		return 1;
42261f28255Scgd 	case 0:
42361f28255Scgd 		abort();
42461f28255Scgd 		_exit(1);
42561f28255Scgd 	}
426ca286310Schristos 	(void)printf("Okie dokie");
427ca286310Schristos 	(void)fflush(stdout);
428ca286310Schristos 	(void)wait_child(pid);
42909edb003Schristos 	if (WCOREDUMP(wait_status))
430ca286310Schristos 		(void)printf(" -- Core dumped.\n");
43161f28255Scgd 	else
432ca286310Schristos 		(void)printf(" -- Can't dump core.\n");
43361f28255Scgd 	return 0;
43461f28255Scgd }
43561f28255Scgd 
43661f28255Scgd /*
437f3098750Schristos  * Clobber the stack.
438f3098750Schristos  */
439f3098750Schristos static void
clob1(int n)440f3098750Schristos clob1(int n)
441f3098750Schristos {
442f3098750Schristos 	char buf[512];
443f3098750Schristos 	char *cp;
444f3098750Schristos 
445f3098750Schristos 	if (n <= 0)
446f3098750Schristos 		return;
447f3098750Schristos 	for (cp = buf; cp < &buf[512]; *cp++ = (char)0xFF)
448f3098750Schristos 		continue;
449f3098750Schristos 	clob1(n - 1);
450f3098750Schristos }
451f3098750Schristos 
452f3098750Schristos /*
45361f28255Scgd  * Clobber as many bytes of stack as the user requests.
45461f28255Scgd  */
455f3098750Schristos PUBLIC int
clobber(void * v)456b127ccccSwiz clobber(void *v)
45761f28255Scgd {
458ca13337dSchristos 	char **argv;
4597c81c8f3Slukem 	int times;
46061f28255Scgd 
461ca13337dSchristos 	argv = v;
46261f28255Scgd 	if (argv[0] == 0)
46361f28255Scgd 		times = 1;
46461f28255Scgd 	else
46561f28255Scgd 		times = (atoi(argv[0]) + 511) / 512;
46661f28255Scgd 	clob1(times);
46761f28255Scgd 	return 0;
46861f28255Scgd }
46961f28255Scgd 
47061f28255Scgd /*
471f3098750Schristos  * Compare two names for sorting ignored field list.
47261f28255Scgd  */
473f3098750Schristos static int
igcomp(const void * l,const void * r)474f3098750Schristos igcomp(const void *l, const void *r)
47561f28255Scgd {
476f3098750Schristos 	return strcmp(*(const char *const *)l, *(const char *const *)r);
47761f28255Scgd }
47861f28255Scgd 
47961f28255Scgd /*
480f3098750Schristos  * Print out all currently retained fields.
48161f28255Scgd  */
482f3098750Schristos static int
igshow(struct ignoretab * tab,const char * which)483f3098750Schristos igshow(struct ignoretab *tab, const char *which)
48461f28255Scgd {
485f3098750Schristos 	int h;
486f3098750Schristos 	struct ignore *igp;
487f3098750Schristos 	char **ap, **ring;
48861f28255Scgd 
489f3098750Schristos 	if (tab->i_count == 0) {
490f3098750Schristos 		(void)printf("No fields currently being %s.\n", which);
491f3098750Schristos 		return 0;
492f3098750Schristos 	}
493f3098750Schristos 	ring = salloc((tab->i_count + 1) * sizeof(char *));
494f3098750Schristos 	ap = ring;
495f3098750Schristos 	for (h = 0; h < HSHSIZE; h++)
496f3098750Schristos 		for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
497f3098750Schristos 			*ap++ = igp->i_field;
498f3098750Schristos 	*ap = 0;
499f3098750Schristos 	qsort(ring, tab->i_count, sizeof(char *), igcomp);
500f3098750Schristos 	for (ap = ring; *ap != 0; ap++)
501f3098750Schristos 		(void)printf("%s\n", *ap);
502f3098750Schristos 	return 0;
50361f28255Scgd }
50461f28255Scgd 
50561f28255Scgd /*
506f3098750Schristos  * core ignore routine.
50761f28255Scgd  */
508f3098750Schristos static int
ignore1(char * list[],struct ignoretab * tab,const char * which)509ece0fd5cSchristos ignore1(char *list[], struct ignoretab *tab, const char *which)
51061f28255Scgd {
51161f28255Scgd 	char **ap;
51261f28255Scgd 
513ab850155Swiz 	if (*list == NULL)
51461f28255Scgd 		return igshow(tab, which);
5154556f89aSchristos 
5164556f89aSchristos 	for (ap = list; *ap != 0; ap++)
5174556f89aSchristos 		add_ignore(*ap, tab);
5184556f89aSchristos 
51961f28255Scgd 	return 0;
52061f28255Scgd }
52161f28255Scgd 
52261f28255Scgd /*
523f3098750Schristos  * Add the given header fields to the retained list.
524f3098750Schristos  * If no arguments, print the current list of retained fields.
52561f28255Scgd  */
526f3098750Schristos PUBLIC int
retfield(void * v)527f3098750Schristos retfield(void *v)
52861f28255Scgd {
529ca13337dSchristos 	char **list;
53061f28255Scgd 
531ca13337dSchristos 	list = v;
532f3098750Schristos 	return ignore1(list, ignore + 1, "retained");
53361f28255Scgd }
534f3098750Schristos 
535f3098750Schristos /*
536f3098750Schristos  * Add the given header fields to the ignored list.
537f3098750Schristos  * If no arguments, print the current list of ignored fields.
538f3098750Schristos  */
539f3098750Schristos PUBLIC int
igfield(void * v)540f3098750Schristos igfield(void *v)
541f3098750Schristos {
542ca13337dSchristos 	char **list;
543f3098750Schristos 
544ca13337dSchristos 	list = v;
545f3098750Schristos 	return ignore1(list, ignore, "ignored");
546f3098750Schristos }
547f3098750Schristos 
548f3098750Schristos /*
549f3098750Schristos  * Add the given header fields to the save retained list.
550f3098750Schristos  * If no arguments, print the current list of save retained fields.
551f3098750Schristos  */
552f3098750Schristos PUBLIC int
saveretfield(void * v)553f3098750Schristos saveretfield(void *v)
554f3098750Schristos {
555ca13337dSchristos 	char **list;
556f3098750Schristos 
557ca13337dSchristos 	list = v;
558f3098750Schristos 	return ignore1(list, saveignore + 1, "retained");
559f3098750Schristos }
560f3098750Schristos 
561f3098750Schristos /*
562f3098750Schristos  * Add the given header fields to the save ignored list.
563f3098750Schristos  * If no arguments, print the current list of save ignored fields.
564f3098750Schristos  */
565f3098750Schristos PUBLIC int
saveigfield(void * v)566f3098750Schristos saveigfield(void *v)
567f3098750Schristos {
568ca13337dSchristos 	char **list;
569f3098750Schristos 
570ca13337dSchristos 	list = v;
571f3098750Schristos 	return ignore1(list, saveignore, "ignored");
572f3098750Schristos }
573f3098750Schristos 
574f3098750Schristos #ifdef MIME_SUPPORT
575f3098750Schristos 
576f3098750Schristos static char*
check_dirname(char * filename)577f3098750Schristos check_dirname(char *filename)
578f3098750Schristos {
579f3098750Schristos 	struct stat sb;
580f3098750Schristos 	char *fname;
581f3098750Schristos 	char canon_buf[MAXPATHLEN];
582f3098750Schristos 	char *canon_name;
583f3098750Schristos 
584f3098750Schristos 	canon_name = canon_buf;
585f3098750Schristos 	fname = filename;
586f3098750Schristos 	if (fname[0] == '~' && fname[1] == '/') {
587f3098750Schristos 		if (homedir && homedir[0] != '~')
588f3098750Schristos 			(void)easprintf(&fname, "%s/%s",
589f3098750Schristos 			    homedir, fname + 2);
590f3098750Schristos 	}
591f3098750Schristos 	if (realpath(fname, canon_name) == NULL) {
592f3098750Schristos 		warn("realpath: %s", filename);
593f3098750Schristos 		canon_name = NULL;
594f3098750Schristos 		goto done;
595f3098750Schristos 	}
596f3098750Schristos 	if (stat(canon_name, &sb) == -1) {
597f3098750Schristos 		warn("stat: %s", canon_name);
598f3098750Schristos 		canon_name = NULL;
599f3098750Schristos 		goto done;
600f3098750Schristos 	}
601f3098750Schristos 	if (!S_ISDIR(sb.st_mode)) {
602f3098750Schristos 		warnx("stat: %s is not a directory", canon_name);
603f3098750Schristos 		canon_name = NULL;
604f3098750Schristos 		goto done;
605f3098750Schristos 	}
606f3098750Schristos 	if (access(canon_name, W_OK|X_OK) == -1) {
607f3098750Schristos 		warnx("access: %s is not writable", canon_name);
608f3098750Schristos 		canon_name = NULL;
609ca13337dSchristos 		goto done;
610f3098750Schristos 	}
611f3098750Schristos  done:
612f3098750Schristos 	if (fname != filename)
613f3098750Schristos 		free(fname);
614f3098750Schristos 
615f3098750Schristos 	return canon_name ? savestr(canon_name) : NULL;
616f3098750Schristos }
617f3098750Schristos 
618f3098750Schristos struct detach1_core_args_s {
619f3098750Schristos 	struct message *parent;
620f3098750Schristos 	struct ignoretab *igtab;
621f3098750Schristos 	const char *dstdir;
622f3098750Schristos };
623f3098750Schristos static int
detach1_core(struct message * mp,void * v)624f3098750Schristos detach1_core(struct message *mp, void *v)
625f3098750Schristos {
626f3098750Schristos 	struct mime_info *mip;
627f3098750Schristos 	struct detach1_core_args_s *args;
628f3098750Schristos 
629f3098750Schristos 	args = v;
630f3098750Schristos 	touch(mp);
631f3098750Schristos 	show_msgnum(stdout, mp, args->parent);
632f3098750Schristos 	mip = mime_decode_open(mp);
633f3098750Schristos 	mime_detach_msgnum(mip, sget_msgnum(mp, args->parent));
634f3098750Schristos 	(void)mime_sendmessage(mp, NULL, args->igtab, args->dstdir, mip);
635f3098750Schristos 	mime_decode_close(mip);
63661f28255Scgd 	return 0;
63761f28255Scgd }
63861f28255Scgd 
63961f28255Scgd /*
640f3098750Schristos  * detach attachments.
64161f28255Scgd  */
64288b833a7Schristos static int
detach1(void * v,int do_unnamed)643f3098750Schristos detach1(void *v, int do_unnamed)
64461f28255Scgd {
645f3098750Schristos 	int recursive;
646f3098750Schristos 	int f;
647f3098750Schristos 	int msgCount;
648f3098750Schristos 	int *msgvec;
649f3098750Schristos 	int *ip;
650f3098750Schristos 	char *str;
651f3098750Schristos 	char *dstdir;
652f3098750Schristos 
653f3098750Schristos 	str = v;
654f3098750Schristos 
655f3098750Schristos 	/*
656f3098750Schristos 	 * Get the destination directory.
657f3098750Schristos 	 */
658f3098750Schristos 	if ((dstdir = snarf(str, &f, "directory")) == NULL &&
659f3098750Schristos 	    (dstdir = value(ENAME_MIME_DETACH_DIR)) == NULL &&
660f3098750Schristos 	    (dstdir = origdir) == NULL)
661f3098750Schristos 		return 1;
662f3098750Schristos 
663f3098750Schristos 	if ((dstdir = check_dirname(dstdir)) == NULL)
664f3098750Schristos 		return 1;
665f3098750Schristos 
666f3098750Schristos 	/*
667f3098750Schristos 	 * Setup the message list.
668f3098750Schristos 	 */
669f3098750Schristos 	msgCount = get_msgCount();
6704556f89aSchristos 	msgvec = salloc((msgCount + 2) * sizeof(*msgvec));
671f3098750Schristos 	if (!f) {
672f3098750Schristos 		*msgvec = first(0, MMNORM);
673f3098750Schristos 		if (*msgvec == 0) {
674f3098750Schristos 			(void)printf("No messages to detach.\n");
675f3098750Schristos 			return 1;
67661f28255Scgd 		}
677f3098750Schristos 		msgvec[1] = 0;
678f3098750Schristos 	}
679f3098750Schristos 	if (f && getmsglist(str, msgvec, 0) < 0)
680f3098750Schristos 		return 1;
681f3098750Schristos 
682f3098750Schristos 	if (mime_detach_control() != 0)
683f3098750Schristos 		return 1;
684f3098750Schristos 
685f3098750Schristos 	/*
686f3098750Schristos 	 * do 'dot' if nothing else was selected.
687f3098750Schristos 	 */
688f3098750Schristos 	if (msgvec[0] == 0 && dot != NULL) {
689f3098750Schristos 		msgvec[0] = get_msgnum(dot);
690f3098750Schristos 		msgvec[1] = 0;
691f3098750Schristos 	}
692f3098750Schristos 	recursive = do_recursion();
693f3098750Schristos 	for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
694f3098750Schristos 		struct detach1_core_args_s args;
695f3098750Schristos 		struct message *mp;
696ca13337dSchristos 
697f3098750Schristos 		mp = get_message(*ip);
698f3098750Schristos 		dot = mp;
699f3098750Schristos 		args.parent = recursive ? mp : NULL;
700f3098750Schristos 		args.igtab = do_unnamed ? detachall : ignoreall;
701f3098750Schristos 		args.dstdir = dstdir;
702f3098750Schristos 		(void)thread_recursion(mp, detach1_core, &args);
703f3098750Schristos 	}
704f3098750Schristos 	return 0;
705f3098750Schristos }
706f3098750Schristos 
707f3098750Schristos /*
708f3098750Schristos  * detach named attachments.
709f3098750Schristos  */
710f3098750Schristos PUBLIC int
detach(void * v)711f3098750Schristos detach(void *v)
712f3098750Schristos {
713ca13337dSchristos 
714f3098750Schristos 	return detach1(v, 0);
715f3098750Schristos }
716f3098750Schristos 
717f3098750Schristos /*
718f3098750Schristos  * detach all attachments.
719f3098750Schristos  */
720f3098750Schristos PUBLIC int
Detach(void * v)721f3098750Schristos Detach(void *v)
722f3098750Schristos {
723ca13337dSchristos 
724f3098750Schristos 	return detach1(v, 1);
725f3098750Schristos }
726f3098750Schristos #endif /* MIME_SUPPORT */
727