/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved
 * Use of this product is subject to license terms.
 */
/************************************************************

 computed.c

 This source file shows how to handle computed attribute values.

 TRYING THIS PLUG-IN
 -------------------
 1. Build the library containing the plug-in.
 
 2. Configure the server to load the plug-in.
     
    $ dsconf create-plugin -H <lib-path> -F compute_init -Y object "Computed"
    $ dsconf enable-plugin "Computed"
    Directory Server needs to be restarted for changes to take effect
    $ dsadm restart <inst-path>

 3. Read an entry to see the server compute the attribute.

    $ ldapsearch -b "" -s base "(objectclass=*)" currentTime
    version: 1
    dn:
    currentTime: Fri Feb 17 09:30:28 2006

************************************************************/
#include <string.h>
#include <time.h>
#include "slapi-plugin.h"

extern void attr_done(Slapi_Attr *a);

Slapi_PluginDesc comp_desc = { 
    "Computed",                        /* plug-in identifier      */
    "Oracle Corporation (test)",       /* vendor name             */
    "11.1.1.3.0",                      /* plug-in revision number */
    "Computed attribute demo"          /* plug-in description     */
};

/* Compute an attribute called currentTime                        */
static int
compute_attrs(
    computed_attr_context  * context,
    char                   * type,
    Slapi_Entry            * entry,
    slapi_compute_output_t   outfn
)
{
    /* If we do not recognize the attribute type return -1.       */
    int rc = -1;

    if (slapi_attr_type_cmp("currentTime", type, 2) == 0) {
        Slapi_Attr  * time_attr;
        time_t        now;
        char          current_time[30];
        Slapi_Value * time_value;
        int           rc = 0;

        /* Construct the computed attribute.                      */
        time_attr = slapi_attr_new();
        slapi_attr_init(time_attr, type);
        now = time(NULL);
		memset(current_time, 0, 30);
		memcpy(current_time, ctime(&now), 30);
        current_time[(int)strlen(current_time) - 1] = '\0';
        time_value = slapi_value_new_string(current_time);
        slapi_attr_add_value(time_attr, time_value);
        slapi_value_free(&time_value);

        /* Add the attribute to the result returned.              */
        rc = (*outfn)(context, time_attr, entry);
        attr_done(time_attr);
        return rc;

    /* Handle other computed attributes...
    } else if (slapi_attr_type_cmp("myCompAttr", type, 2) == 0) {
    */

    }
    return rc;
}

/* Register the plug-in with the server */
#ifdef _WIN32
__declspec(dllexport)
#endif
int
compute_init(Slapi_PBlock * pb)
{
    int rc = 0;                        /* 0 means success         */
    rc |= slapi_pblock_set(            /* Plug-in API version     */
        pb,
        SLAPI_PLUGIN_VERSION,
        SLAPI_PLUGIN_CURRENT_VERSION
    );
    rc |= slapi_pblock_set(            /* Plug-in description     */
        pb,
        SLAPI_PLUGIN_DESCRIPTION,
        (void *) &comp_desc
    );
    rc |= slapi_compute_add_evaluator(compute_attrs);
    return rc;
}
