/*
 *   IRC - Internet Relay Chat, modules/m_rehash.c
 *
 *   Copyright (C) 2000-2003 TR-IRCD Development
 *
 *   Copyright (C) 1990 Jarkko Oikarinen and
 *                      University of Oulu, Co Center
 *
 *   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, 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 "s_conf.h"
#include "h.h"
#include "hook.h"
#include "resnew.h"
#include "throttle.h"

static int hookid_rehash_more = 0;

static struct Message _msgtab[] = {
    {MSG_REHASH, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_permission, m_rehash, m_ignore, m_ignore}
};

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

static void read_all_motd(void);
static void read_links(void);
static void do_rehash_everything(void);
static void do_rehash_help(void);
static void do_rehash_gc(void);
static void do_rehash_akills(void);
static void do_rehash_excludes(void);
static void do_rehash_jupiters(void);
static void do_rehash_sqlines(void);
static void do_rehash_sglines(void);
static void do_rehash_szlines(void);
static void do_rehash_tklines(void);
static void do_rehash_tempmasks(void);
static void do_rehash_persmasks(void);
static void do_rehash_writeconf(void);

static RehashItem rlist[] = {
    {"DNS", restart_resolver},
    {"GC", do_rehash_gc},
    {"MOTD", read_all_motd},
    {"LINKS", read_links},
    {"THROTTLES", throttle_rehash},
    {"ALL", do_rehash_everything},
    {"AKILLS", do_rehash_akills}, 
    {"EXCLUDES", do_rehash_excludes},
    {"JUPITERS", do_rehash_jupiters},
    {"SQLINES", do_rehash_sqlines},
    {"SGLINES", do_rehash_sglines},
    {"SZLINES", do_rehash_szlines},
    {"TKLINES", do_rehash_tklines},
    {"TEMPMASKS", do_rehash_tempmasks},
    {"PERSMASKS", do_rehash_persmasks},
    {"WRITECONF", do_rehash_writeconf},
    {"HELP", do_rehash_help},
    {NULL},
};

static int log_rehash = -1;

#ifndef STATIC_MODULES

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

void _modinit(void)
{
    hookid_rehash_more = hook_add_event("doing rehash");
    mod_add_cmd(_msgtab);
    log_rehash = logevent_register("rehash", LOG_ALWAYS, LOG_LOGFILE | LOG_IRCLOG, LOG_NOTICE,                              
                                   "REHASH From %s\n");       
}

void _moddeinit(void)
{
    hook_del_event("doing rehash");
    mod_del_cmd(_msgtab);
    logevent_unregister(log_rehash);
}
#else
void m_rehash_init(void)
{
    hookid_rehash_more = hook_add_event("doing rehash");
    mod_add_cmd(_msgtab);
    log_rehash = logevent_register("rehash", LOG_ALWAYS, LOG_LOGFILE | LOG_IRCLOG, LOG_NOTICE,
                                   "REHASH From %s\n");  
}
#endif

static int help = 0;

static void do_rehash_gc(void)
{
    block_garbage_collect(NULL);
}

static void read_all_motd(void)
{
    read_message_file(&(GeneralOpts.motd));
    read_message_file(&(GeneralOpts.shortmotd));
}

static void read_links(void)
{
    read_message_file(&(GeneralOpts.linksfile));
}

static void do_rehash_everything(void)
{
    restart_resolver();
    read_all_motd();
    read_links();
    throttle_rehash();
}

static void do_rehash_help(void)
{
    help = 1;
}

static void do_rehash_akills(void)
{
    rehash_maskitems(MASKITEM_AUTOKILL);
}

static void do_rehash_excludes(void)
{
    rehash_maskitems(MASKITEM_EXCLUDE);
}

static void do_rehash_sqlines(void)
{
    rehash_maskitems(MASKITEM_QUARANTINE);
}

static void do_rehash_sglines(void)
{
    rehash_maskitems(MASKITEM_GECOS);
}

static void do_rehash_szlines(void)
{
    rehash_maskitems(MASKITEM_ZAPLINE);
}

static void do_rehash_jupiters(void)
{
    rehash_maskitems(MASKITEM_JUPITER);
}

static void do_rehash_tklines(void)
{
    rehash_maskitems(MASKITEM_KLINE);
}

static void do_rehash_tempmasks(void)
{
    rehash_maskitems(MASKITEM_AUTOKILL);
    rehash_maskitems(MASKITEM_EXCLUDE);
    rehash_maskitems(MASKITEM_QUARANTINE);
    rehash_maskitems(MASKITEM_GECOS);
    rehash_maskitems(MASKITEM_ZAPLINE);
    rehash_maskitems(MASKITEM_JUPITER);
    rehash_maskitems(MASKITEM_KLINE);
}

static void do_rehash_persmasks(void)
{
    rehash_maskitems(MASKITEM_KLINE_CONFIG);
    rehash_maskitems(MASKITEM_QUARANTINE_CONFIG);
    rehash_maskitems(MASKITEM_GECOS_CONFIG);
    rehash_maskitems(MASKITEM_ZAPLINE_CONFIG);
    rehash_maskitems(MASKITEM_JUPITER_CONFIG);
}

static void do_rehash_writeconf(void)
{
    generate_server_config_file(CONFIGFILE);
}

static void rehash_more(aClient *sptr, char *more)
{
    struct hook_data thisdata;

    thisdata.source_p = sptr;
    thisdata.data = more;

    hook_call_event(hookid_rehash_more, &thisdata);
}

int m_rehash(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    help = 0;
    if (parc > 1) {
    int i, j;
	rehash_more(sptr, parv[1]);
	for (i = 0; rlist[i].name; i++) {
	    if (irc_strcmp(parv[1], rlist[i].name) == 0) {
		send_me_numeric(sptr, RPL_REHASHING, rlist[i].name);
		rlist[i].function();
		if (!help)
		    sendto_ops("%s is rehashing %s", parv[0], parv[1]);
	    }
	}
	if (help) {
	    send_me_notice(sptr, ":Rehash elements");
            for (j = 0; rlist[j].name; j++) 
                send_me_notice(sptr, ":%s", rlist[j].name);
        }
	return 0;
    } else {
	send_me_numeric(sptr, RPL_REHASHING, "Server config file");
	sendto_ops("%s is rehashing Server config file while whistling innocently", parv[0]);
	logevent_call(log_rehash, get_client_name(sptr, FALSE));
	return rehash(cptr, sptr, 0);
    }
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1