/************************************************************************
 *   IRC - Internet Relay Chat, modules/m_set.c
 *
 *   Copyright (C) 2000-2003 TR-IRCD Development
 *
 *   Copyright (C) 1990 Jarkko Oikarinen and
 *                      University of Oulu, Co Center
 *
 *   See file AUTHORS in IRC package for additional names of
 *   the programmers.
 *
 *   This program is free softwmare; 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "channel.h"
#include "throttle.h"
#include "s_conf.h"
#include "h.h"

static struct Message s_msgtab[] = {
    {MSG_SET, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_permission, m_set, m_ignore, m_ignore}
};
static struct Message ns_msgtab[] = {
    {MSG_NETSET, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_permission, m_netset, m_netset, m_ignore}
};

static char *token1 = TOK1_NETSET;

static int log_set_debuglevel = -1;

void set_max(aClient *cptr, aClient *sptr, int parc, char **parv);
void set_spamnum(aClient *cptr, aClient *sptr, int parc, char **parv);   
void set_spamtime(aClient *cptr, aClient *sptr, int parc, char **parv); 
void set_proxymon(aClient *cptr, aClient *sptr, int parc, char **parv);
void set_logging(aClient *cptr, aClient *sptr, int parc, char **parv);
void set_debug(aClient *cptr, aClient *sptr, int parc, char **parv);
void set_throttle(aClient *cptr, aClient *sptr, int parc, char **parv);
void set_help(aClient *cptr, aClient *sptr, int parc, char **parv);
void set_identdcomplain(aClient *cptr, aClient *sptr, int parc, char **parv);

#ifndef STATIC_MODULES

char *_version = "$Revision: 1.6 $";

void _modinit(void)
{
    mod_add_cmd(s_msgtab);
    mod_add_cmd(ns_msgtab);
    tok1_msgtab[(u_char) *token1].msg = ns_msgtab;
    log_set_debuglevel = logevent_register("set debuglevel", LOG_ON_LOG, LOG_LOGFILE,
					   LOG_NOTICE, "\nIRCLOG: New DEBUGLEVEL %d at %s\n");
}

void _moddeinit(void)
{
    mod_del_cmd(s_msgtab);
    mod_del_cmd(ns_msgtab);
    tok1_msgtab[(u_char) *token1].msg = NULL;
}
#else
void m_set_init(void)
{
    mod_add_cmd(s_msgtab);
    mod_add_cmd(ns_msgtab);
    tok1_msgtab[(u_char) *token1].msg = ns_msgtab;
}
#endif

typedef struct {
    char *name;
    char *usage;
    void (*function) ();
} SetItem;

static SetItem slist[] = {
    {"MAX", "MAX: TO set the new max number of clients", set_max},
    {"SPAMNUM", "SPAMNUM: Lines to accept before spambot notices arrive", set_spamnum},
    {"SPAMTIME",
     "SPAMTIME: Number of seconds in which the lines are accepted before spambot notice arrive",
     set_spamtime},
    {"PROXYMON", "PROXYMON: Activate or deactivate the proxy scanner", set_proxymon},
    {"LOGGING", "LOGGING: Activate or deactivate the logging functions", set_logging},
    {"DEBUG", "DEBUG: Set the debug level, or deactivate debug", set_debug},
    {"THROTTLE", "THROTTLE Settings: THROTTLE <COUNT|TIME|BANTIME|HASH>", set_throttle},
    {"IDENTCOMPLAIN", "IDENTCOMPLAIN: Deny client connections without ident reply", set_identdcomplain},
    {"HELP", "HELP: Display help", set_help},
    {NULL, NULL, NULL},
};

void set_max(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (parc > 2) {
    int new_value = atoi(parv[2]);

	if (new_value > MAXCONNECTIONS) {
	    send_me_notice(sptr, ":You cannot set MAXCLIENTS to > (%d)", MAXCONNECTIONS);
	    return;
	}
	if (new_value < 32) {
	    send_me_notice(sptr, ":You cannot set MAXCLIENTS to < 32 (%d)", GeneralOpts.maxclients);
	    return;
	}
	GeneralOpts.maxclients = new_value;
	send_me_notice(sptr,
		       ":NEW MAXCLIENTS = %d (Current = %d)", GeneralOpts.maxclients, Count.local);
	sendto_ops("%C set new MAXCLIENTS to %d (%d current)", sptr,
		   GeneralOpts.maxclients, Count.local);
    }
    send_me_notice(sptr, ":Current Maxclients = %d (%d)", GeneralOpts.maxclients, Count.local);
}

void set_spamnum(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (parc > 2) {
    int newval = atoi(parv[2]);

	if (newval <= 0) {
	    send_me_notice(sptr, ":spam NUM must be > 0");
	    return;
	}
	if (newval < MIN_SPAM_NUM)
	    GeneralOpts.spam_num = MIN_SPAM_NUM;
	else
	    GeneralOpts.spam_num = newval;
	sendto_ops("%s has changed spam NUM to %i", parv[0], GeneralOpts.spam_num);
	send_me_notice(sptr, ":spam NUM is now set to %i", GeneralOpts.spam_num);
    }
    send_me_notice(sptr, ":spam NUM is currently %i", GeneralOpts.spam_num);
}

void set_spamtime(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (parc > 2) {
    int newval = atoi(parv[2]);

	if (newval <= 0) {
	    send_me_notice(sptr, ":spam TIME must be > 0");
	    return;
	}
	if (newval < MIN_SPAM_TIME)
	    GeneralOpts.spam_time = MIN_SPAM_TIME;
	else
	    GeneralOpts.spam_time = newval;
	sendto_ops("%s has changed spam TIME to %i", parv[0], GeneralOpts.spam_time);
	send_me_notice(sptr, ":SPAM TIME is now set to %i", GeneralOpts.spam_time);
    }
    send_me_notice(sptr, ":spam TIME is currently %i", GeneralOpts.spam_time);
}

void set_proxymon(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (parc > 2) {
	if (!irc_strncmp(parv[2], "ON", 2)) {
	    send_me_notice(sptr, ":The proxy scanner is activated");
	    sendto_ops("%C activated proxy scanner", sptr);
	    GeneralOpts.enable_proxymonitor = 1;
	} else if (!irc_strncmp(parv[2], "OFF", 3)) {
	    send_me_notice(sptr, ":The proxy scanner is deactivated");
	    sendto_ops("%C deactivated the proxy scanner", sptr);
	    GeneralOpts.enable_proxymonitor = 0;
	} else {
	    send_me_notice(sptr, ":The correct setting is either" "\002ON\002 or \002OFF\002");
	}
    }
    send_me_notice(sptr, ":Proxy Scanner is now %s",
		   (GeneralOpts.enable_proxymonitor ? "\002ACTIVE\002" : "\002INACTIVE\002"));
}

void set_logging(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (parc > 2) {

	if (!irc_strncmp(parv[2], "ON", 2)) {
	    send_me_notice(sptr, ":The ircd stats logging");
	    sendto_ops("%C activated LOGGING", sptr);
	    GeneralOpts.enable_logging = 1;
	} else if (!irc_strncmp(parv[2], "OFF", 3)) {
	    send_me_notice(sptr, ":The ircd stops logging");
	    sendto_ops("%C deactivated LOGGING", sptr);
	    GeneralOpts.enable_logging = 0;
	} else {
	    send_me_notice(sptr, ":The correct setting is either" "\002ON\002 or \002OFF\002");
	}
    }
    send_me_notice(sptr, ":LOGGING is now %s",
		   (GeneralOpts.enable_logging ? "\002ACTIVE\002" : "\002INACTIVE\002"));
}

void set_identdcomplain(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (parc > 2) {

        if (!irc_strncmp(parv[2], "ON", 2)) {
            send_me_notice(sptr, ":The ircd will deny connections without ident reply");
            sendto_ops("%C activated identd complains", sptr);
	    ServerOpts.identd_complain = 1;
        } else if (!irc_strncmp(parv[2], "OFF", 3)) {
            send_me_notice(sptr, ":The ircd will not complain on missing ident reply");
            sendto_ops("%C deactivated identd complains", sptr);
	    ServerOpts.identd_complain = 0;
        } else {   
            send_me_notice(sptr, ":The correct setting is either" "\002ON\002 or \002OFF\002");
        }
    }
    send_me_notice(sptr, ":IDENTCOMPLAIN is now %s",
	    (ServerOpts.identd_complain ? "\002ACTIVE\002" : "\002INACTIVE\002"));
}

void set_debug(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (parc > 2 && IsAdmin(sptr)) {
	if (atoi(parv[2])) {
	    GeneralOpts.debuglevel = atoi(parv[2]);
	    send_me_notice(sptr, ":DEBUGMODE set to level \002%d\002", GeneralOpts.debuglevel);
	    logevent_call(log_set_debuglevel, GeneralOpts.debuglevel, smalldate(time(NULL)));

	} else if (!irc_strncmp(parv[2], "OFF", 3)) {
	    logevent_call(log_set_debuglevel, -1, smalldate(time(NULL)));
	    GeneralOpts.debuglevel = 0;
	    send_me_notice(sptr, ":DEBUGMODE is now \002OFF\002");
	} else {
	    send_me_notice(sptr, ":Possible settings are " "\0021\002 to \0029\002 or \002OFF\002");
	}
    }
    send_me_notice(sptr,
		   ":DEBUGMODE is set to \002%d\002",
		   GeneralOpts.debuglevel ? GeneralOpts.debuglevel : 0);
}

void set_throttle(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    char *changed = NULL;
    char *to = NULL;
    /* several values available:
     * ENABLE [on|off] to enable the code
     * COUNT [n] to set a max count, must be > 1
     * TIME [n] to set a max time before expiry, must be > 5
     * BANTIME [n] to set a time for the throttle bans to expire
     * HASH [n] to set the size of the hash table, must be bigger than
     *          the default */

    /* only handle individual settings if parc > 3 (they're actually
     * changing stuff) */
    if (parc > 3) {
	if (!irc_strcmp(parv[2], "COUNT")) {
    int cnt;
	    changed = "COUNT";
	    cnt = atoi(parv[3]);
	    if (cnt > 1) {
		throttle_tcount = cnt;
		to = parv[3];
	    }
	} else if (!irc_strcmp(parv[2], "TIME")) {
    int cnt;
	    changed = "TIME";
	    cnt = atoi(parv[3]);
	    if (cnt >= 5) {
		throttle_ttime = cnt;
		to = parv[3];
	    }
	} else if (!irc_strcmp(parv[2], "BANTIME")) {
    int cnt;
	    changed = "BANTIME";
	    cnt = atoi(parv[3]);
	    if (cnt >= 30) {
		throttle_ztime = cnt;
		to = parv[3];
	    }
	} else if (!irc_strcmp(parv[2], "HASH")) {
    int cnt;
	    changed = "HASH";
	    cnt = atoi(parv[3]);
	    if (cnt >= THROTTLE_HASHSIZE) {
		throttle_resize(cnt);
		to = parv[3];
	    }
	}

	if (to != NULL) {
	    sendto_ops("%s has changed throttle %s to %s", parv[0], changed, to);
	    sendto_one(sptr, ":%s NOTICE %s :set throttle %s to %s", me.name, parv[0], changed, to);
	}
    } else {
	/* report various things, we cannot easily get the hash size, so
	 * leave that alone. */
	send_me_notice(sptr, ":THROTTLE COUNT=%d", throttle_tcount);
	send_me_notice(sptr, ":THROTTLE TIME=%d", throttle_ttime);
	send_me_notice(sptr, ":THROTTLE BANTIME=%d", throttle_ztime);
    }
}

void set_help(aClient *cptr, aClient *sptr, int parc, char **parv)
{
    int i;
    send_me_notice(sptr, ":SET Options");
    for (i = 0; slist[i].name; i++) 
	send_me_notice(sptr, ":%s", slist[i].usage);
}

int m_set(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    if (parc > 1) {
    int i;
	logevent_call(LogSys.operevent, MSG_SET, sptr, &parv, parc);
        for (i = 0; slist[i].name; i++) {
            if (irc_strcmp(parv[1], slist[i].name) == 0) {
                slist[i].function(cptr, sptr, parc, parv);
		return 0;
	    }
        }
    }
    return 0;
}

int m_netset(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    if (parc < 2)
	return 0;

    m_set(cptr, sptr, parc, parv);

    if (parc == 3)
	sendto_serv_butone(cptr, sptr, TOK1_NETSET, "%s :%s", parv[1], parv[2]);
    else
	sendto_serv_butone(cptr, sptr, TOK1_NETSET, "%s", parv[1]);

    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1