
/*****************************************************************************
**
**  This program is free software; you can redistribute it and/or
**  modify it, however, you cannot sell it.
**
**  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.
**
**  You should have received a copy of the license attached to the
**  use of this software.  If not, visit www.shmoo.com/osiris for
**  details.
**
******************************************************************************/

/*****************************************************************************
**
**  File:    notify_filter.c
**  Date:    September 17, 2003
**
**  Author:  Brian Wotring
**
**  Purpose: structure representing a notification filter object
**           used to filter out deltas that will trigger a notification.
**
*****************************************************************************/

#include "libosirism.h"
#include <string.h>

void wrap_notify_filter( OSI_NOTIFY_FILTER *filter )
{
    if( filter == NULL )
    {
        return;
    }

    filter->type = HTONLL( filter->type );
    filter->mask = HTONLL( filter->mask );
}

void unwrap_notify_filter( OSI_NOTIFY_FILTER *filter )
{
    if( filter == NULL )
    {
        return;
    }

    filter->type = NTOHLL( filter->type );
    filter->mask = NTOHLL( filter->mask );
}

unsigned long mask_from_notify_keywords( const char *keywords )
{
    int index   = 0;
    int valid   = 0;

    unsigned long mask = 0;

    char *token = NULL;
    char temp_values[MAX_LINE_LENGTH] = "";

    if( keywords == NULL )
    {
        return 0;
    }

    /* strtok will ruin the string we were given */
    /* so we copy the values data.               */

    osi_strlcpy( temp_values, keywords, ( strlen( keywords ) + 1 ) );

    /* only one value, don't seperate. */

    token = strtok( temp_values, "," );
    token = trim_white_space( token );

    while( token )
    {
        valid = 0;
        lowercase_string( token );

        for( index = 0; notify_keywords[index].word != NULL; index++ )
        {
            if( strcmp( token, notify_keywords[index].word ) == 0 )
            {
                mask |= notify_keywords[index].type;
            }
        }

        token = strtok( NULL, "," );
        token = trim_white_space( token );
    }

    return mask;
}

void print_notify_filter( OSI_NOTIFY_FILTER *filter )
{
    int index;

    if( filter == NULL )
    {
        return;
    }

    fprintf( stdout, "host=%s;path=%s;", filter->host, filter->path );

    switch( (int)filter->type )
    {
        case NOTIFY_FILTER_TYPE_INCLUDE_ONLY:
            fprintf( stdout, "include only: " );
            break;

        case NOTIFY_FILTER_TYPE_EXCLUDE:
            fprintf( stdout, "exclude: " );
            break;
        
        default:
            fprintf( stdout, ";" );
            break;
    }

    for( index = 0; notify_keywords[index].word != NULL; index++ )
    {
        if( filter->mask & notify_keywords[index].type )
        {
            fprintf( stdout, "%s ",  notify_keywords[index].word );
        }
    }

    fprintf( stdout, ";" );
}

void print_notify_attribute_mask( osi_uint64 attr_list )
{
    int count = 0;
    int index;

    unsigned int attr = (unsigned int)attr_list;

    for( index = 0; notify_keywords[index].word != NULL; index++ )
    {
        if( attr & notify_keywords[index].type )
        {
            if( count > 0 )
            {
                fprintf( stdout, "," );
            }

            fprintf( stdout, "%s",  notify_keywords[index].word );
            count++;
        }
    }
}

char * osi_get_name_for_notify_attribute( osi_uint64 attr )
{
    int index;
    char *name = "?";

    for( index = 0; notify_keywords[index].word != NULL; index++ )
    {
        if( attr == notify_keywords[index].type )
        {
            name = notify_keywords[index].word;
            break;
        }
    }

    return name;
}

void print_valid_notify_filter_attributes()
{
    int index;

    fprintf( stdout, "\n" );

    for( index = 0; notify_keywords[index].word != NULL; index++ )
    {
        fprintf( stdout, "  %10s - %s\n",
                 notify_keywords[index].word,
                 notify_descriptions[index].word );
    }
}

unsigned int pack_notify_filter( OSI_NOTIFY_FILTER *filter, char *buffer,
                                 int buffer_size )
{
    char *temp = buffer;

    if( ( filter == NULL ) || ( buffer == NULL ) || ( buffer_size <= 0 ) )
    {
        return 0;
    }

    memcpy( temp, filter->host, sizeof( filter->host ) );
    temp += sizeof( filter->host );
    memcpy( temp, filter->path, sizeof( filter->path ) );
    temp += sizeof( filter->path );
    memcpy( temp, &filter->type, sizeof( filter->type ) );
    temp += sizeof( filter->type );
    memcpy( temp, &filter->mask, sizeof( filter->mask ) );
    temp += sizeof( filter->mask );

    return ( temp - buffer );
}

void unpack_notify_filter( OSI_NOTIFY_FILTER *filter, char *buffer,
                           int buffer_size )
{
    char *temp = buffer;

    if( ( filter == NULL ) || ( buffer == NULL ) || ( buffer_size <= 0 ) )
    {
        return;
    }

    memcpy( filter->host, temp, sizeof( filter->host ) );
    temp += sizeof( filter->host );
    memcpy( filter->path, temp, sizeof( filter->path ) );
    temp += sizeof( filter->path );
    memcpy( &filter->type, temp, sizeof( filter->type ) );
    temp += sizeof( filter->type );
    memcpy( &filter->mask, temp, sizeof( filter->mask ) );
}

