static char rcsid[] = "@(#)$Id: alias_pattern.c,v 1.6 2006/04/09 07:37:42 hurtta Exp $";

/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.6 $   $State: Exp $
 *
 *  Author: Kari Hurtta <hurtta+elm@posti.FMI.FI> 
 ******************************************************************************
 * Inludes code from pattern.c which have following copyright:
 *
 *  The Elm Mail System 
 *
 *			Copyright (c) 1988-1992 USENET Community Trust
 *			Copyright (c) 1986,1987 Dave Taylor
 ****************************************************************************/

#include "def_mcommon.h"

static unsigned char *s2us P_((char *str));
static unsigned char *s2us(str) 
     char *str;
{
    return (unsigned char *)str;
}

static int name_matches P_((struct alias_rec * a, struct string *pat));
static int alias_matches P_((struct alias_rec * a, struct string *pat));
static int comment_matches P_((struct alias_rec * a, struct string *pat));
static int address_matches P_((struct alias_rec * a, struct string *pat,
			       struct AliasView *aview));

int mc_matches_alias(u,idx, meta_pattern)
     union mcommon_union *u;
     int idx;
     struct string * meta_pattern;
{
    struct AliasView *aview = u->alias.aw;
    struct alias_rec *a = give_alias(aview,idx);
    int r = 0;
    
    if (a) {
	r = name_matches(a, meta_pattern) || alias_matches(a, meta_pattern); 
    }

    return r;
}

static int name_matches(a, pattern)
     struct alias_rec * a;
     struct string * pattern;
{
    /** Returns true iff the pattern occurs in it's entirety
	in the proper name of the indicated alias **/
    
    int r = 0;

    if (a) {
	struct string *s = new_string2(display_charset,
				       s2us(a->name));

	if (find_pattern_from_string(s,pattern,1))
	    r++;

	free_string(&s);
    }

    return r;
}

static int alias_matches(a, pattern)
     struct alias_rec * a;
     struct string * pattern;
{
    /** Returns true iff the pattern occurs in it's entirety
	in the alias name of the indicated alias **/
    
    int r = 0;
    
    if (a) {
	struct string *s = new_string2(display_charset,
				       s2us(a->alias));
	
	if (find_pattern_from_string(s,pattern,1))
	    r++;
	
	free_string(&s);
    }

    return r;
}


static int comment_matches(a, pattern)
     struct alias_rec * a;
     struct string * pattern;
{
    /** Returns true iff the pattern occurs in it's entirety
	in the comment field of the indicated alias **/

    int r = 0;
    
    if (a) {
	struct string *s = new_string2(display_charset,
				       s2us(a->comment));
	
	if (find_pattern_from_string(s,pattern,1))
	    r++;
	
	free_string(&s);
    }
    return r;

}

static int address_matches(a,pattern, aview)
     struct alias_rec * a;
     struct string * pattern;
     struct AliasView *aview;
{
    struct addr_item *result;
    int dummy;
    int res = 0;
        
    /** Returns true iff the pattern occurs in it's entirety
	in the fully expanded address of the indicated alias **/
    
    if (a) {
	result = get_alias_address_l(a->alias,TRUE,
				     &dummy, NULL, aview);

	if (result != NULL) {
	    struct addr_item *walk;
	    for (walk = result; 
		 walk->addr && walk ->fullname && walk->comment && !res;
		 walk++) {
		
		struct string *s = new_string2(ASCII_SET,s2us(walk->addr));
		
		if (find_pattern_from_string(walk->fullname,pattern,1))
		    res = 1;
		if (find_pattern_from_string(walk->comment,pattern,1))
		    res = 1;
		
		if (find_pattern_from_string(s,pattern,1))
		    res = 1;
		
		free_string(&s);
	    }
	    free_addr_items(result);	
	}
    }

    return(res);
}

int mc_match_in_text_alias(u,meta_pattern, page, LOC)
     union mcommon_union *u;
     struct string * meta_pattern;
     struct menu_context  *page;
     struct screen_parts *LOC; 
{   
    struct AliasView *aview = u->alias.aw;

    /* From pattern_match() */

    int current = get_alias_current(aview);
    int ac = get_alias_count(aview);
    int selected = get_alias_selected(aview);
    int i;
    int ret = 0;

    for (i = current; i < ac; i++) {
	struct alias_rec * a = give_alias(aview,i);
	
	if (a && (
		  name_matches(a, meta_pattern) || 
		  alias_matches(a, meta_pattern) ||
		  comment_matches(a, meta_pattern) ||
		  address_matches(a, meta_pattern, aview)
		  )) {
	    
	    if (!selected || 
		(selected && a->status & VISIBLE)) {
		
		current = ++i;
		set_alias_current(aview,current);
		
		ret = 1;
		goto out;
	    }
	}
    }
 out:

    return ret;
}

/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:4
 *  buffer-file-coding-system: iso-8859-1
 * End:
 */



syntax highlighted by Code2HTML, v. 0.9.1