/*
 * ==================================================================
 * Module:			m_allwhois.c
 * Author:			w00t <surreal.w00t@gmail.com>
 * Version:			1.0.2
 * Written For:		angelic (of Anope)
 * Licence:			GPL
 * Description:		Allowing all users to be notified on /whois
 * ==================================================================
 */


/* ChangeLog:
 *    20/4/2005 - 1.0.2
 *		-
 *	27/3/2005 - 1.0.1
 *		-Removed sptr->virthost, replaced with GetHost on advice of aquanight
 *  09/3/2005
 *		-Added a few comments.
 *		-Changed some variable names so I don't clash with my other modules.
 *	20/2/2005 - 1.0.0a
 *		-*slap* it's acptr, not sptr...
 *		-Changed notice from realhost to virthost for non-eyes.
 *		-Fixed usermode deletion
 *		-Added usermode
 *		-Oops, fixed a core caused by incorrect signature :\
 *	19/2/2005 - 1.0.0
 *		-Initial version
 *
 *
 * TODO:
 *  -Check for UmodeDel etc's success.
 */

/* Thanks to:
 *  Stskeeps - m_dummy skeleton
 *  aquanight - Telling me to put overrides in MOD_LOAD, not MOD_INIT (!?)
 *		(that so needs to be documented)
 *  Whoever wrote m_whois, thanks for the loop ;)
 *  Whoever hardcoded +W :/
 */ 
#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

Cmdoverride *allwhois_override = NULL;
ModuleInfo *allwhois_modinfo = NULL;
Umode *my_umode = NULL;
long UMODE_ALLWHOIS;

DLLFUNC int m_allwhois(Cmdoverride *anoverride, aClient *cptr, aClient *sptr, int parc, char *parv[]);

#define MSG_WHOIS	"WHOIS"


ModuleHeader MOD_HEADER(m_allwhois)
  = {
	"m_allwhois",	/* Name */
	"1.0.2", /* Ver */
	"All-user WHOIS notification by w00t <surreal.w00t@gmail.com>", /* Desc. */
	"3.2-b8-1",
	NULL 
    };

DLLFUNC int MOD_INIT(m_allwhois)(ModuleInfo *modinfo)
{
	allwhois_modinfo = modinfo;
	my_umode = UmodeAdd(allwhois_modinfo->handle, 'E', UMODE_GLOBAL, NULL, &UMODE_ALLWHOIS);
	if (!my_umode)
	{
		sendto_realops("m_allwhois: Failed to allocate usermode 'E': %s", ModuleGetErrorStr(allwhois_modinfo->handle));
		return MOD_FAILED;
	}
	sendto_realops("m_allwhois: MOD_INIT completed sucessfully.");
	return MOD_SUCCESS;
}

DLLFUNC int MOD_LOAD(m_allwhois)(int module_load)
{
	allwhois_override = CmdoverrideAdd(allwhois_modinfo->handle, MSG_WHOIS, m_allwhois);
	if (!allwhois_override)
	{
		sendto_realops("m_allwhois: Failed to allocate override: %s", ModuleGetErrorStr(allwhois_modinfo->handle));
		return MOD_FAILED;
	}
	sendto_realops("m_allwhois: MOD_LOAD completed sucessfully.");
	return MOD_SUCCESS;
}

DLLFUNC int MOD_UNLOAD(m_allwhois)(int module_unload)
{
	CmdoverrideDel(allwhois_override);
	UmodeDel(my_umode);

	return MOD_SUCCESS;
}

DLLFUNC int m_allwhois(Cmdoverride *anoverride, aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	char *tmp;
	char *nick;
	char *p = NULL;
	
	int  found = 0;
	
	aClient *acptr;
	
	/* Start doing stuff here. */
	
	if (parc < 1)
	{
		/* Ignore, no nick specified. Also useful to note we always ignore parv[2] here.
		 * Question: How do we actually react on /whois nick nick syntax?
		 * will be sending two notices? one from cptr's server and one from target's
		 * server?
		 */
		return 0;
	}
	
	/* Loop pretty much shamelessly ripped from m_whois ;P */
	/* strtoken... is that safe?! well, it was in m_whois, so i'll assume so. */
	for (tmp = parv[1]; (nick = strtoken(&p, tmp, ",")); tmp = NULL)
	{
		unsigned char wilds; // <- these are all boolean-alike

		wilds = (index(nick, '?') || index(nick, '*'));
		if (wilds)
		{
			/* Ignore wildcards. */
			continue;
		}

		if ((acptr = find_client(nick, NULL)))
		{
			if (IsServer(acptr))
			{
				continue;
			}

			if (IsMe(acptr))
			{
				break;
			}

			if (!IsPerson(acptr))
			{
				continue;	
			}
				

			if ((acptr->umodes & UMODE_ALLWHOIS) && (sptr != acptr))
			{
				sendto_one(acptr, ":%s %s %s :*** %s (%s@%s) did a /whois on you.", me.name, IsWebTV(acptr) ? "PRIVMSG" : "NOTICE", acptr->name, sptr->name, sptr->user->username, IsEyes(acptr) ? sptr->user->realhost : GetHost(sptr));
			}

		if (p)
			p[-1] = ',';
		}
	}

	/* Go on our merry way ;) */
	return CallCmdoverride(allwhois_override, cptr, sptr, parc, parv);
}