/*
 *
 *  AM --- AmigaMail
 *  (C) 1991, 1992 by Christian Riede
 *
 *  AM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY.  No author or distributor accepts responsibility to anyone
 *  for the consequences of using it or for whether it serves any
 *  particular purpose or works at all, unless he says so in writing.
 *  Refer to the GNU General Public License, Version 1, for full details.
 *  
 *  Everyone is granted permission to copy, modify and redistribute AM,
 *  but only under the conditions described in the GNU General Public
 *  License, Version 1.  A copy of this license is supposed to have been 
 *  given to you along with AM so you can know your rights and responsi-
 *  bilities.  It should be in a file named COPYING.  Among other things,
 *  the copyright notice and this notice must be preserved on all copies.
 *
 *  
 *
 */

#include "am.h"


/* search for string s in given mail (saved header fields only), */
/* return TRUE if found */
int SearchTextH(regexp *pat,struct Mail *Mail)
{
	if (!Mail) return(FALSE);
	if (!pat) return(FALSE);

	if (regexec(pat,Mail->From)) return(TRUE);
	if (regexec(pat,Mail->To)) return(TRUE);
	if (regexec(pat,Mail->Subject)) return(TRUE);

	return(FALSE);
}

/* search for string s in given mail (complete mail, header & body) */
/* return TRUE if found */
/* BUG: doesn't find pattern if it is exactly on a 10BK boundary */
int SearchText(struct regexp *pat,struct Mail *Mail)
{
	char Filename[160];
	BPTR File;
	UBYTE *Buffer;
	int ret;

#define SBUFSIZE 10*1024

	if (!Mail) return(FALSE);
	if (!pat) return(FALSE);

	sprintf(Filename,"%s/%d",Folder,Mail->Number);
	
	if (!(Buffer = AllocMem(SBUFSIZE,MEMF_CLEAR)))
		return(FALSE);

	if (!(File = Open((UBYTE *)Filename,MODE_OLDFILE)))
	{
		FreeMem(Buffer,SBUFSIZE);
		return(FALSE);
	}
		
	ret = FALSE;

	while (Read(File,Buffer,SBUFSIZE))
		if (regexec(pat,(char *)Buffer))
		{
			ret = TRUE;
			break;
		}

	Close(File);

	FreeMem(Buffer,SBUFSIZE);
	
	return(ret);
}
