/*
 * =================================================================
 * Filename:		m_iscmd.c
 * =================================================================
 * Description:         A little command called /iscmd. It tells you
 *			which of the given commands are supported by
 *			the server. It works for alias commands too.
 *			The output is designed for client-side
 *			scripting.
 * =================================================================
 * Author:		AngryWolf
 * Email:		angrywolf@flashmail.com
 * =================================================================
 *
 * I accept bugreports, ideas and opinions, and if you have
 * questions, just send an email for me!
 *
 * Thank you for using my module!
 *
 * =================================================================
 * Requirements:
 * =================================================================
 *
 * o Unreal >=3.2-beta15
 * 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/m_iscmd.c [Germany]
 * http://angrywolf.uw.hu/m_iscmd.c [Hungary]
 * http://angrywolf.fw.hu/m_iscmd.c [Hungary]
 *
 * =================================================================
 * Changes:
 * =================================================================
 *
 * $Log: m_iscmd.c,v $
 * Revision 2.3  2004/03/08 21:25:29  angrywolf
 * - Fixed some bugs that could cause crash if you compile the module
 *   statically (for example, under Windows).
 *
 * Revision 2.2  2003/12/01 11:53:41  angrywolf
 * - Replaced add_Command and del_Command with CommandAdd and CommandDel.
 *
 * Revision 2.1  2003/08/31 20:29:56  angrywolf
 * From now on I'm using RCS
 *
 * 2003-06-04: Fixed a stupid bug caused by forgetting a colon
 *             before the prefix of the reply message (reported
 *             by ChRiS)
 * 2003-04-27: Fixed compile warnings for win32
 * 2003-04-27: Coded m_iscmd
 *
 * =================================================================
 */

#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

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

#define MSG_ISCMD 	"ISCMD"
#define TOK_ISCMD 	"IS"
#define DelCommand(x)	if (x) CommandDel(x); x = NULL

static Command		*AddCommand(Module *module, char *msg, char *token, iFP func);
DLLFUNC int		m_iscmd(aClient *cptr, aClient *sptr, int parc, char *parv[]);

Command			*CmdIscmd;

#ifndef DYNAMIC_LINKING
ModuleHeader m_iscmd_Header
#else
#define m_iscmd_Header Mod_Header
ModuleHeader Mod_Header
#endif
  = {
	"iscmd",
	"$Id: m_iscmd.c,v 2.3 2004/03/08 21:25:29 angrywolf Exp $",
	"command /iscmd",
	"3.2-b8-1",
	NULL 
    };

#ifdef DYNAMIC_LINKING
DLLFUNC int	Mod_Init(ModuleInfo *modinfo)
#else
int    m_iscmd_Init(ModuleInfo *modinfo)
#endif
{
	CmdIscmd = AddCommand(modinfo->handle, MSG_ISCMD, TOK_ISCMD, m_iscmd);

	if (!CmdIscmd)
		return MOD_FAILED;

	return MOD_SUCCESS;
}

#ifdef DYNAMIC_LINKING
DLLFUNC int	Mod_Load(int module_load)
#else
int    m_iscmd_Load(int module_load)
#endif
{
	return MOD_SUCCESS;
}

#ifdef DYNAMIC_LINKING
DLLFUNC int	Mod_Unload(int module_unload)
#else
int	m_iscmd_Unload(int module_unload)
#endif
{
	DelCommand(CmdIscmd);

	return MOD_SUCCESS;
}

static Command *AddCommand(Module *module, char *msg, char *token, iFP func)
{
	Command *cmd;

	if (CommandExists(msg))
    	{
		config_error("Command %s already exists", msg);
		return NULL;
    	}
    	if (CommandExists(token))
	{
		config_error("Token %s already exists", token);
		return NULL;
    	}

	cmd = CommandAdd(module, msg, token, func, MAXPARA, 0);

#ifndef STATIC_LINKING
	if (ModuleGetError(module) != MODERR_NOERROR || !cmd)
#else
	if (!cmd)
#endif
	{
#ifndef STATIC_LINKING
		config_error("Error adding command %s: %s", msg,
			ModuleGetErrorStr(module));
#else
		config_error("Error adding command %s", msg);
#endif
		return NULL;
	}

	return cmd;
}

/*
** m_iscmd
**      parv[0] = sender prefix
**      parv[*] = list of command names
*/

DLLFUNC int m_iscmd(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	aCommand	*p;
        char		buf[BUFSIZE];
	int		i;

	if (!IsPerson(sptr))
		return 0;

	if (parc < 2)
	{
		sendto_one(sptr, err_str(ERR_NEEDMOREPARAMS),
			me.name, parv[0], "ISCMD");
		return 0;
	}

	memset(&buf, 0, sizeof buf);

	for (i = 1; i < parc; i++)
		if ((p = find_Command(parv[i], 0, M_USER | M_SERVER)))
		{
			strncat(buf, p->cmd, sizeof buf);
			strncat(buf, " ", sizeof buf);
		}
	
	sendto_one(sptr, ":%s 340 %s :%s",
		me.name, sptr->name, buf);

	return 0;
}
