skeleton file : filter.c



	Hi all,
here is the skeleton file of filter.c containing the "implementation " 
of filter_run_* functions. I've just implemented the FILTER_SIMPLE 
filter type.
There is no error handling (e.g. if libbalsa_message_copy/move... 
fails).
What are we supposed to do for an action type==FILTER_RUN ?
Comments are welcome.
Bye
Manu
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
 *
 * Copyright (C) 1997-2000 Stuart Parmenter and others,
 *                         See the file AUTHORS for a list.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */
/*
 * filter.c
 * 
 * The mail filtering porting of balsa
 *
 * Mostly skeletonic
 *
 * Author:  Emmanuel ALLAUD
 */

#include "balsa_app.h"
#include "libbalsa.h"
#include "filter.h"

/* FIXME : misc.h in included for libbalsa_make_string_from_list
   which should be moved in address.h.
*/
#include "misc.h"

/*
 * Run all filters until one matches (so order of filter is important)
 * Return 0 for no match, 1 for match
 */

gint filter_run_all(GList * filter_list, LibBalsaMessage * message)
{
    gint match=0;

    while (filter_list  && !match) {
	match=filter_run_single((filter*)filter_list->data,message);
	filter_list=g_list_next(filter_list);
    }
    return match;
}

/*
 * Run all filters of the specified group  until one matches
 * (so order of filter is important)
 * Return 0 for no match, 1 for match
 */

gint filter_run_group(GList * filter_list,
		      LibBalsaMessage * message, gint group)
{
    gint match=0;

    while (filter_list  && !match) {
	if (((filter*)filter_list->data)->group=group)
	    match=filter_run_single((filter*)filter_list->data,message);
	filter_list=g_list_next(filter_list);
    }
    return match;
}

/*
 * Run the n_th filter
 * (so order of filter is important)
 * Return 0 for no match, 1 for match
 */

gint filter_run_nth(GList * filter_list,
		    LibBalsaMessage * message, gint n)
{
    return filter_run_single((filter*)g_list_nth(filter_list,n)->data,message);
}

gint filter_run_single(filter * filt, LibBalsaMessage * message)
{
    gint field,match=0;
    gboolean free=FALSE;
    gchar * p;
    LibBalsaMailbox *mbox;

    /* FIXME : What do we do if filt->type==FILTER_NON ??? */

    /* First let's see if message matches the filter */

    /* No match if the filter is disabled */
    if (!FILTER_CHKFLAGS(filt,FILTER_ENABLED)) return 0;

    if (filt->match_when!=FILTER_ALLWAYS) {
	if (filt->type==FILTER_SIMPLE) {
	    /* First test the header */
	    field=FILTER_MATCH_TO;
	    while ((field<=FILTER_MATCH_SUBJECT) && !match) {
		if (FILTER_CHKMATCH(filter,field)
		    || FILTER_CHKMATCH(filter,FILTER_MATCH_HEADER)) {
		    switch (field) {
		    case FILTER_MATCH_TO:
			p=libbalsa_make_string_from_list(message->to_list);
			free=TRUE;
			break;
		    case FILTER_MATCH_FROM:
			p=libbalsa_address_to_gchar(message->from);
			free=TRUE;
			break;
		    case FILTER_MATCH_SUBJECT:
			p=message->subject;
		    }
		    match=strstr(p,filt->match.string)!=NULL;
		    if (free) {
			g_free(p);
			free=FALSE;
		    }
		}
		/* Next field */
		field <<=1;
	    }

	}
	else if (filt->type==FILTER_EXEC) {
	    /* FIXME : to be implemented */
	}
	else if (filt->type==FILTER_REGEX) {
	    /* FIXME : to be implemented */
	}
    }
    /* If message haven't matched yet, test the body if we have to */
    
    if (!match 
	&&(FILTER_CHKMATCH(filt,FILTER_MATCH_BODY)
	   || FILTER_CHKMATCH(filt,FILTER_MATCH_ALL))) {
    }
    
    if (!match) return 0;
    
    /* The message matches the filter, let's act now! */

    if (FILTER_CHKFLAGS(filt,FILTER_SOUND)) {
	/* FIXME : Emit sound */
    }
    if (FILTER_CHKFLAGS(filt,FILTER_POPUP)) {
	/* FIXME : Print popup text */
    }
    switch (filt->action) {
    case FILTER_COPY:
	mbox = mblist_find_mbox_by_name(balsa_app.mblist,filt->action_string);
	if (balsa_app.debug)
	    fprintf(stderr, "open_mailboxes_idle_cb: opening %s => %p..\n",
		    filt->action_string, mbox);
	if (mbox)
	    libbalsa_message_copy(message,mbox);
	break;
    case FILTER_TRASH:
	if (balsa_app.trash)
	    libbalsa_message_move(balsa_app.trash);
	break;
    case FILTER_MOVE:
	mbox = mblist_find_mbox_by_name(balsa_app.mblist,filt->action_string);
	if (balsa_app.debug)
	    fprintf(stderr, "open_mailboxes_idle_cb: opening %s => %p..\n",
		    filt->action_string, mbox);
	if (mbox)
	    libbalsa_message_move(message,mbox);
	break;
    case FILTER_PRINT:
	/* FIXME : to be implemented */
       	break;
    case FILTER_RUN:
	/* FIXME : to be implemented */
	break;
    case FILTER_NOTHING:
	/* Nothing to do */
    }
    return 1;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]