/*
 * (POP3Lite) UserConf - 3lite POP3 Daemon (per-user configuration)
 * Copyright (C) 2000, 2001 Gergely Nagy <8@free.bsd.hu>
 *
 * This file is part of POP3Lite.
 *
 * POP3Lite 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 of the License, or
 * (at your option) any later version.
 *
 * POP3Lite 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
 */

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include <pop3lite.h>
#include <glib.h>

#ifdef DEBUG
#	include <syslog.h>
#endif

#include "userconf.h"

static const char rcsid[]="$Id: userconf.c,v 1.5 2001/01/12 15:58:59 algernon Exp $";

int userconf_LTX_module_done ( P3LControl *control );
int userconf_LTX_module_init ( P3LControl *control );

static P3LControl_trans_init B_userconf_trans_init;
static P3LHook_userconf_load_config B_userconf_load_config;

static CommandResponse *userconf_trans_init ( P3LControl *control );
static void userconf_load_config ( P3LControl *control,
				   const char *location, GHashTable *table );

static void
userconf_load_config ( P3LControl *control,
		       const char *location, GHashTable *table )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: Handling USERCONF-LOAD-CONFIGURATION",
			       __FILE__, __LINE__ );
#endif
	p3l_load_config ( control, location, table );
}

static CommandResponse *
userconf_trans_init ( P3LControl *control )
{
	UserInfo *ui;
	GHashTable *newconfig;
	GList *allowed_opts;
	GList *new_opts=NULL;
	unsigned long i;
	char *cmd;

#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: TRANSACTION state started (%s)",
			       __FILE__, __LINE__, P3L_GET_DATA ( "USER" ) );
#endif

	ui = control->system->getuinam ( control, P3L_GET_DATA ( "USER" ) );

	if ( ui != NULL )
	{
		newconfig = g_hash_table_new ( (GHashFunc) g_str_hash,
					       (GCompareFunc) g_str_equal );
		P3L_CALL_HOOK ( P3LHook_userconf_load_config, "USERCONF-LOAD-CONFIGURATION" )
			( control, g_strdup_printf ( "%s/%s", ui->home,
					P3L_GET_FIRST_OPTION ("USERCONF.CONFFILE" ) ), newconfig );

		/*
		 * First, overwrite options...
		 */

		allowed_opts = P3L_GET_OPTION ( "USERCONF.USER_OPTIONS" );
		if ( allowed_opts != 0 )
		{
			for ( i = 0; i < g_list_length ( allowed_opts ); i++ )
			{
				cmd = g_strdup ( (char *) g_list_nth_data ( allowed_opts, i ) );
				g_strup ( cmd );
				g_hash_table_insert ( control->config, g_strdup ( cmd ),
						      g_hash_table_lookup ( newconfig, cmd ) );
				g_free ( cmd );
			}
		}

		/*
		 * Then append to other options...
		 */

		allowed_opts = P3L_GET_OPTION ( "USERCONF.USER_APPEND" );
		if ( allowed_opts != 0 )
		{
			for ( i = 0 ; i < g_list_length ( allowed_opts ); i++ )
			{
				cmd = g_strdup ( (char *) g_list_nth_data ( allowed_opts, i ) );
				g_strup ( cmd );
				new_opts = g_hash_table_lookup ( control->config, cmd );
				new_opts = g_list_concat ( new_opts,
							   g_hash_table_lookup ( newconfig, cmd ) );
				g_hash_table_insert ( control->config, g_strdup ( cmd ), new_opts );
				g_free ( cmd );
			}
		}

		g_hash_table_destroy ( newconfig );
	}

	return (*B_userconf_trans_init) ( control );
}

int
userconf_LTX_module_init ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: init mod-UserConf",
			       __FILE__, __LINE__ );
#endif

	if ( control->trans_init != NULL )
	{
		B_userconf_trans_init = control->trans_init;
		control->trans_init = userconf_trans_init;
		B_userconf_load_config = (P3LHook_userconf_load_config)
			p3l_command_replace ( control->hooks, "USERCONF-LOAD-CONFIGURATION",
					      (gpointer) userconf_load_config );
		return 0;
	}

	return -1;
}

int
userconf_LTX_module_done ( P3LControl *control )
{
#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: done mod-UserConf",
			       __FILE__, __LINE__ );
#endif

	control->trans_init = B_userconf_trans_init;

	g_hash_table_insert ( control->hooks, "USERCONF-LOAD-CONFIGURATION",
			      (gpointer) B_userconf_load_config );

	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1