/*
 * =================================================================
 * Filename:          identonly.c
 * Description:       Channel mode +X (ident only channel)
 * Author:            AngryWolf <angrywolf@flashmail.com>
 * Documentation:     identonly.txt (comes with the package)
 * =================================================================
 */

#include "config.h"
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "channel.h"
#include <time.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <io.h>
#endif
#include <fcntl.h>
#include "h.h"
#ifdef STRIPBADWORDS
#include "badwords.h"
#endif
#ifdef _WIN32
#include "version.h"
#endif

#ifndef EXTCMODE
 #error "This module requires extended channel modes to be enabled."
 #error "See the EXTCMODE macro in include/config.h for more information."
 #error "Compilation aborted."
#endif

extern void			sendto_one(aClient *to, char *pattern, ...);

#define FLAG_IDENTONLY		'X'
#define ERR_IDENTONLY		498
#define IsIdentOnly(x)		((x)->mode.extmode & MODE_IDENTONLY)
#define GotId(x)		((x)->flags & FLAGS_GOTID)
#define DelCmode(x)		if (x) CmodeDel(x); x = NULL
#define DelHook(x)		if (x) HookDel(x); x = NULL

/* Backward compatibility */
#ifndef EX_DENY
 #define EX_DENY		0
 #define EX_ALLOW		1
 #define EX_ALWAYS_DENY		0
#endif

static Cmode			*AddCmode(Module *module, CmodeInfo *req, Cmode_t *mode);
static int			ModeIO_is_ok(aClient *, aChannel *, char *, int, int);
static int			cb_pre_local_join(aClient *, aChannel *, char *[]);

Cmode_t				MODE_IDENTONLY = 0L;
Cmode				*ModeIdentOnly;
Hook				*HookPreJoin;

#ifndef STATIC_LINKING
static ModuleInfo	*MyModInfo;
 #define MyMod		MyModInfo->handle
 #define SAVE_MODINFO	MyModInfo = modinfo;
#else
 #define MyMod		NULL
 #define SAVE_MODINFO
#endif

ModuleHeader MOD_HEADER(identonly)
  = {
	"ident Join",
	"v 2.1 ",
	"mode +X (ident only channel)",
	"3.2-b8-1",
	NULL 
    };

DLLFUNC int MOD_TEST(identonly)(ModuleInfo *modinfo)
{
	CmodeInfo ModeIO;

	memset(&ModeIO, 0, sizeof ModeIO);

	ModeIO.paracount	= 0;
	ModeIO.is_ok		= ModeIO_is_ok;
	ModeIO.flag		= FLAG_IDENTONLY;
	ModeIdentOnly		= AddCmode(modinfo->handle, &ModeIO, &MODE_IDENTONLY);

	if (!ModeIdentOnly)
		return MOD_FAILED;

	return MOD_SUCCESS;
}

DLLFUNC int MOD_INIT(identonly)(ModuleInfo *modinfo)
{
	SAVE_MODINFO
#ifndef STATIC_LINKING
	ModuleSetOptions(modinfo->handle, MOD_OPT_PERM);
#endif

	HookPreJoin = HookAddEx(modinfo->handle, HOOKTYPE_PRE_LOCAL_JOIN, cb_pre_local_join);

	return MOD_SUCCESS;
}

DLLFUNC int MOD_LOAD(identonly)(int module_load)
{
	return MOD_SUCCESS;
}

DLLFUNC int MOD_UNLOAD(identonly)(int module_unload)
{
	DelCmode(ModeIdentOnly);
	DelHook(HookPreJoin);

	return MOD_FAILED;
}

static Cmode *AddCmode(Module *module, CmodeInfo *req, Cmode_t *mode)
{
	Cmode *cmode;

	*mode = 0;
	cmode = CmodeAdd(module, *req, mode);

#ifndef STATIC_LINKING
        if (ModuleGetError(module) != MODERR_NOERROR || !cmode)
#else
        if (!cmode)
#endif
	{
#ifndef STATIC_LINKING
		config_error("Error adding channel mode +%c when loading module %s: %s",
			req->flag, MOD_HEADER(identonly).name, ModuleGetErrorStr(module));
#else
		config_error("Error adding channel mode +%c when loading module %s",
			req->flag, MOD_HEADER(identonly).name);
#endif
		return NULL;
	}

	return cmode;
}

static int ModeIO_is_ok(aClient *sptr, aChannel *chptr, char *para, int type, int what)
{
	if ((type == EXCHK_ACCESS) || (type == EXCHK_ACCESS_ERR))
	{
		if (!IsPerson(sptr) || IsULine(sptr))
			return EX_ALLOW;

		if (!is_chan_op(sptr, chptr))
		{
			if (type == EXCHK_ACCESS_ERR)
				sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED),
					me.name, sptr->name, chptr->chname);
			return EX_DENY;
		}

		if (!IDENT_CHECK && (what == MODE_ADD))
		{
			if (type == EXCHK_ACCESS_ERR)
				sendto_one(sptr, ":%s NOTICE %s :Channel mode %c is not allowed to be set while ident checking is disabled",
					me.name, sptr->name, FLAG_IDENTONLY);
			return EX_ALWAYS_DENY;
		}

		return EX_ALLOW;
	}

	return 0;
}

static int cb_pre_local_join(aClient *sptr, aChannel *chptr, char *parv[])
{
	if (!IsOper(sptr) && IsIdentOnly(chptr) && !GotId(sptr))
	{
		sendto_one(sptr, ":%s %d %s :Cannot join channel (+%c)",
			me.name, ERR_IDENTONLY, sptr->name, FLAG_IDENTONLY);

		return 1;
	}

	return 0;
}
