/*
 * =================================================================
 * Filename:      remotenick.c
 * =================================================================
 * Description:	  Snomask +N: allows you to see remote nickchanges.
 * =================================================================
 * Requested by:  FreakyComputer
 * Author:        AngryWolf <angrywolf@flashmail.com>
 * =================================================================
 *
 * I accept bugreports, ideas and opinions, and if you have any
 * questions, just send an email to me!
 *
 * Thank you for using my module!
 *
 * =================================================================
 * Requirements:
 * =================================================================
 *
 * o Unreal >=3.2-beta19
 *   (or devel CVS with ReleaseID >= 1.1.1.1.2.1.2.1.2.1959)
 * o One of the supported operating systems (see unreal32docs.html)
 *
 * =================================================================
 * Installation:
 * =================================================================
 *
 * See http://angrywolf.linktipp.org/compiling.php?lang=en
 *
 * =================================================================
 * Mirror files:
 * =================================================================
 *
 * http://angrywolf.linktipp.org/remotenick.c [Germany]
 * http://angrywolf.uw.hu/remotenick.c [Hungary]
 * http://angrywolf.fw.hu/remotenick.c [Hungary]
 *
 * =================================================================
 * Changes:
 * =================================================================
 *
 * $Log: remotenick.c,v $
 * Revision 1.7  2004/03/08 21:27:22  angrywolf
 * - Fixed some bugs that could cause crash if you compile the module
 *   statically (for example, under Windows).
 *
 * Revision 1.6  2004/02/04 08:54:20  angrywolf
 * - Fixed a win32 crash bug, reported by Zell.
 *
 * Revision 1.5  2004/02/03 11:25:34  angrywolf
 * - Windows compilation fixes.
 *
 * Revision 1.4  2004/01/16 19:43:46  angrywolf
 * - The module is now permanent.
 *
 * Revision 1.3  2003/11/29 19:31:12  angrywolf
 * - Fixed a win32 crash bug regarding ModuleGetError, reported by TuX.
 *
 * Revision 1.2  2003/11/27 10:58:13  angrywolf
 * - Fixed a crash bug reported by gauntlet.
 *
 * Revision 1.1  2003/11/20 21:57:07  angrywolf
 * Initial revision
 *
 * =================================================================
 */

#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

#define RN_Flag		'N'
#define DelSnomask(x)	if (x) SnomaskDel(x); x = NULL
#define DelHook(x)	if (x) HookDel(x); x = NULL

static Snomask		*AddSnomask(Module *module, char flag, iFP allowed, long *mode);
static int		cb_remote_nickchange(aClient *, aClient *, char *);

Snomask			*RN_Snomask = NULL;
long			SNO_REMOTENICK = 0;
static Hook		*HookRemoteNick = NULL;

ModuleHeader MOD_HEADER(remotenick)
  = {
	"remotenick",
	"$Id: remotenick.c,v 1.7 2004/03/08 21:27:22 angrywolf Exp $",
	"Snomask +N: notification for remote nickchanges",
	"3.2-b8-1",
	NULL 
    };

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

	HookRemoteNick	= HookAddEx(modinfo->handle, HOOKTYPE_REMOTE_NICKCHANGE, cb_remote_nickchange);
        RN_Snomask	= AddSnomask(modinfo->handle, RN_Flag, umode_allow_opers, &SNO_REMOTENICK);

	if (!RN_Snomask)
		return MOD_FAILED;

	return MOD_SUCCESS;
}

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

DLLFUNC int MOD_UNLOAD(remotenick)(int module_unload)
{
	DelHook(HookRemoteNick);
	DelSnomask(RN_Snomask);

	return MOD_SUCCESS;
}

static Snomask *AddSnomask(Module *module, char flag, iFP allowed, long *mode)
{
	Snomask *s;

	*mode = 0;
        s = SnomaskAdd(module, flag, allowed, mode);

#ifndef STATIC_LINKING
        if ((ModuleGetError(module) != MODERR_NOERROR) || !s)
#else
        if (!s)
#endif
        {
#ifndef STATIC_LINKING
                sendto_realops("[\2remotenick\2] Error adding snomask %c: %s",
                        flag, ModuleGetErrorStr(module));
#else
                sendto_realops("[\2remotenick\2] Error adding snomask %c",
                        flag);
#endif
		return NULL;
        }

	return s;
}

static int cb_remote_nickchange(aClient *cptr, aClient *sptr, char *nick)
{
	sendto_snomask(SNO_REMOTENICK, "*** Notice -- %s (%s@%s) has changed his/her nickname to %s",
		sptr->name, sptr->user->username, sptr->user->realhost, nick);

	return 0;
}

