/* ARISA - Admin Interface - Settings Menu
 * Copyright (C) 2003 Carl Ritson
 *
 * 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 of the License, 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
 */

/** Settings Menu Commands **/

MENU_CMD(cmd_settings_save) {
	int ret = save_global(global);
	if(ret != -1)
		admin_msg(ac,"Save Successful.");
	else
		admin_msg(ac,"Save Failed.");
	return 0;
}

MENU_CMD(cmd_settings_global) {
	if(no_args == 1) {
		pqueue_t settings;
		pqueue_init(&settings,0);
		global_read_settings(&settings);
		output_settings(ac,"-- Global Settings --",NULL,&settings);
	} else if(no_args >= 3) {
		if(global_apply_setting(args[1],args[2]) == -1)
			admin_msg(ac,"Invalid setting: %s",args[1]);
	} else
		admin_msg(ac,"%s [<setting> <value>]",args[0]);
	return 0;
}

static int cmd_ignore_list(acontext_t *ac, ignore_list_t *l, 
		char **args, int no_args) {
	time_t now = xtime();
	int i;
	
	if(no_args < 2) {
		char tbuf[32];
		int no;
		
		LOCK(l);
		admin_msg(ac,"-- Ignore List Settings --");
		admin_msg(ac,"trigger: %d (0=disabled)",l->trigger);
		admin_msg(ac,"period: %d (seconds)",l->period);
		admin_msg(ac,"-- Ignore List Entries --");
		for(i = 0, no = 1; i < l->no_entries; ++i) {
			ignore_entry_t *e = l->entries[i];
			if(e->expires > now || (e->flags & IGNORE_FOREVER)) {
				admin_msg(ac,"% 3d: %s %s%s%s",no++,
						e->mask,
						e->flags & IGNORE_FOREVER ?
						 "Forever" : "Till: ",
						e->flags & IGNORE_FOREVER ? "" :
						 timestr(e->expires,tbuf,sizeof(tbuf)),
						e->flags & IGNORE_INVERSION ?
						 ", Inverse" : "");
			}
		}
		UNLOCK(l);
	} else if(tolower(args[1][0]) == 'a') {
		if(no_args >= 3) {
			long period = 0;
			int flags = IGNORE_FOREVER;
			if(no_args >= 4) {
				if(args[3][0] == '-')
					flags |= IGNORE_INVERSION;
				period = strtol(args[3],NULL,0);
				if(period <= 0)
					period = -period;
				if(period != 0)
					flags &= ~IGNORE_FOREVER;
			}
			ignore_add(l,args[2],flags,xtime() + period);
		} else
			admin_msg(ac,"%s add <mask> [[-]<duration>]",args[0]);
	} else if(tolower(args[1][0]) == 'd' || tolower(args[1][0]) == 'r') {
		if(no_args >= 3) {
			int no = atoi(args[2]);
			if(no != 0) {
				if(ignore_del(l,no-1,NULL) == -1)
					i = ignore_del(l,0,args[2]);
				else
					i = 0;
			} else
				i = ignore_del(l,0,args[2]);
			if(i != 0)
				admin_msg(ac,"Unknown entry: %s (%d)",args[2],no);
		} else 
			admin_msg(ac,"%s %s [<mask>|<number>]",args[0],
					tolower(args[1][0]) == 'd' ?
					"del" : "remove");
	} else if(tolower(args[1][0]) == 't') {
		if(no_args >= 3) {
			LOCK(l);
			i = atoi(args[2]);
			if(i >= 0)
				l->trigger = i;
			UNLOCK(l);
		} else
			admin_msg(ac,"%s trigger <int>",args[0]);
	} else if(tolower(args[1][0]) == 'p') {
		if(no_args >= 3) {
			LOCK(l);
			i = atoi(args[2]);
			if(i >= 0)
				l->period = i;
			UNLOCK(l);
		} else
			admin_msg(ac,"%s period <seconds>",args[0]);
	} else if(tolower(args[1][0]) == 'm') {
		if(no_args >= 4) {
			int j;
			i = atoi(args[2]);
			j = atoi(args[3]);
			LOCK(l);
			if(PTRARR_MOVE(&(l->entries),&(l->no_entries),i,j) != 0)
				admin_msg(ac,"Invalid source or destination");
			UNLOCK(l);
		} else
			admin_msg(ac,"%s move <src> <dst>",args[0]);
	} else if(tolower(args[1][0]) == 'w') {
		ignore_wipe(l);
	} else
		admin_msg(ac,"%s [add|move|period|remove|trigger|wipe]",args[0]);

	return 0;
}

MENU_CMD(cmd_settings_network) {
	LOCK(global);
	if(no_args == 1) {
		pqueue_t settings;
		pqueue_init(&settings,0);
		netset_read(global->network_settings,&settings);
		output_settings(ac,"-- Global Default Network Settings --",
				NULL,&settings);
	} else if(no_args >= 3) {
		if(netset_apply(global->network_settings,args[1],args[2]) == -1)
			admin_msg(ac,"Invalid setting: %s",args[1]);
	} else
		admin_msg(ac,"%s [<setting> <value>]",args[0]);
	UNLOCK(global);
	return 0;
}

MENU_CMD(cmd_settings_ignore_list) {
	return cmd_ignore_list(ac,global->ignore_list,args,no_args);
}

/** Settings Menu Structure **/

static const cmd_t settings_menu[] = {
	{"global",	cmd_settings_global,	0,3,1,0,PRIVS_EMPTY,NULL},
	{"ignore-list",	cmd_settings_ignore_list,0,3,1,0,PRIVS_EMPTY,NULL},
	{"network",	cmd_settings_network,	0,3,1,0,PRIVS_EMPTY,NULL},
	{"save",	cmd_settings_save,	0,0,1,0,PRIVS_EMPTY,NULL},
	{NULL}
};

static void register_settings_menu(void) {
	menu_t *s_menu = menu_from_cmds(
			"settings",			// name
			0,				// min_args
			0,				// max_args
			1,				// log
			0,				// hide
			PRIV_TO_MASK(PRIV_SETTINGS),	// privs
			NULL,				// func
			NULL,				// helper
			(cmd_t *)settings_menu);
	
	menu_add(main_menu_root,s_menu);
	menu_deref(s_menu);
}
