/*
 * (POP3Lite) Disable - 3lite POP3 Daemon (command disabler)
 * 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

static const char rcsid[]="$Id: disable.c,v 1.4 2001/01/12 15:58:59 algernon Exp $";

int disable_LTX_module_done ( P3LControl *control );
int disable_LTX_module_init ( P3LControl *control );

static GHashTable *disabled_cmd_auth;
static GHashTable *disabled_cmd_trans;

int
disable_LTX_module_init ( P3LControl *control )
{
	unsigned long i;
	char *cmd;
	GList *list;

#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: init mod-Disable",
			       __FILE__, __LINE__ );
#endif

	disabled_cmd_auth = g_hash_table_new ( (GHashFunc) g_str_hash,
					       (GCompareFunc) g_str_equal );
	disabled_cmd_trans = g_hash_table_new ( (GHashFunc) g_str_hash,
						(GCompareFunc) g_str_equal );

	/** Disable commands in the authentication state **/
	list = P3L_GET_OPTION ( "DISABLE.COMMANDS.AUTH" );
	for ( i = 0; i < g_list_length ( list ); i++ )
	{
		cmd = g_list_nth_data ( list, i );
		g_strup ( cmd );
		g_hash_table_insert ( disabled_cmd_auth, cmd,
			g_hash_table_lookup ( control->auth_commands, cmd ) );
		g_hash_table_remove ( control->auth_commands, cmd );
		g_free ( cmd );
	}

	/** Disable commands in the transaction state **/
	list = P3L_GET_OPTION ( "DISABLE.COMMANDS.TRANS" );
	for ( i = 0; i < g_list_length ( list ); i++ )
	{
		cmd = g_list_nth_data ( list, i );
		g_strup ( cmd );
		g_hash_table_insert ( disabled_cmd_trans, cmd,
			g_hash_table_lookup ( control->trans_commands, cmd ) );
		g_hash_table_remove ( control->trans_commands, cmd );
		g_free ( cmd );
	}

	return 0;
}

int
disable_LTX_module_done ( P3LControl *control )
{
	unsigned long i;
	char *cmd;
	GList *list;

#ifdef DEBUG
	control->system->log ( control, LOG_DEBUG, "%s:%d: done mod-Disable",
			       __FILE__, __LINE__ );
#endif

	/** Restore commands in the authentication state **/
	list = P3L_GET_OPTION ( "DISABLE.COMMANDS.AUTH" );
	for ( i = 0; i < g_list_length ( list ); i++ )
	{
		cmd = g_list_nth_data ( list, i );
		g_strup ( cmd );
		g_hash_table_insert ( control->auth_commands, cmd,
			g_hash_table_lookup ( disabled_cmd_auth, cmd ) );
		g_free ( cmd );
	}

	/** Restore commands in the transaction state **/
	list = P3L_GET_OPTION ( "DISABLE.COMMANDS.TRANS" );
	for ( i = 0; i < g_list_length ( list ); i++ )
	{
		cmd = g_list_nth_data ( list, i );
		g_strup ( cmd );
		g_hash_table_insert ( control->trans_commands, cmd,
			g_hash_table_lookup ( disabled_cmd_trans, cmd ) );
		g_free ( cmd );
	}

	return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1