/*
 * =================================================================
 * Filename:       m_sendraw.c
 * Description:	   Command /sendraw
 * Author:         AngryWolf <angrywolf@flashmail.com>
 * Documentation:  Comes with the package (m_sendraw.txt)
 * =================================================================
 */

#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, ...);
extern void		sendto_realops(char *pattern, ...);

#define MSG_SENDRAW 	"SENDRAW"
#define TOK_SENDRAW 	"SR"
#define DelCommand(x)	if (x) CommandDel(x); x = NULL

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

Command			*CmdSendraw;

ModuleHeader MOD_HEADER(m_sendraw)
  = {
	"sendraw",
	"$Id: m_sendraw.c,v 3.1 2004/05/17 20:55:18 angrywolf Exp $",
	"command /sendraw",
	"3.2-b8-1",
	NULL 
    };

DLLFUNC int MOD_INIT(m_sendraw)(ModuleInfo *modinfo)
{
	CmdSendraw = AddCommand(modinfo->handle, MSG_SENDRAW, TOK_SENDRAW, m_sendraw);

	if (!CmdSendraw)
		return MOD_FAILED;

	return MOD_SUCCESS;
}

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

DLLFUNC int MOD_UNLOAD(m_sendraw)(int module_unload)
{
	DelCommand(CmdSendraw);

	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_sendraw
**      parv[0] = sender prefix
**      parv[1] = targets separated by commas (nick/server/channel)
**      parv[2] = message
*/

static int m_sendraw(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	aClient		*acptr;
	aChannel	*chptr;
	char		*recipients, *to, *message, *p;

        if (!MyClient(sptr) || !IsNetAdmin(sptr))
	{
        	sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]);
		return -1;
	}

        if (parc < 3 || BadPtr(parv[2]))
	{
    		sendto_one(sptr, ":%s NOTICE %s :*** Usage: /sendraw <to1>[,<to2>,[,<to3>...]]> [:]<message>",
	                me.name, sptr->name);
    		sendto_one(sptr, ":%s NOTICE %s :*** <to>: <#channel>|<nick>|<server>|-servers",
	                me.name, sptr->name);
    		sendto_one(sptr, ":%s NOTICE %s :*** '-servers' sends a message to all servers connected to the local one",
	                me.name, sptr->name);
    		sendto_one(sptr, ":%s NOTICE %s :*** Example: /sendraw #opers ::Lamer PRIVMSG #opers :Hello!",
	                me.name, sptr->name);
		return -1;
	}

	recipients	= strdup(parv[1]);
	message		= parv[2];

	sendto_realops("%s (%s@%s) did a /sendraw [ \2to=\2%s \2message=\2%s ]",
		sptr->name, sptr->user->username,
		IsHidden(sptr) ? sptr->user->virthost : sptr->user->realhost,
		recipients, message);

	for (to = strtoken(&p, recipients, ","); to; to = strtoken(&p, NULL, ","))
	{

    		if (!strcasecmp(to, "-servers"))
			sendto_serv_butone(&me, message);
    		else if (*to == '#' && (chptr = find_channel(to, NullChn)) != NullChn)
			sendto_channel_butone(&me, &me, chptr, message);
    		else if ((acptr = find_person(to, NULL)) || (acptr = find_server_quick(to)))
			sendto_one(acptr, message);
		else
		{
			if (!IsServer(sptr))
	    			sendto_one(sptr, err_str(ERR_NOSUCHNICK),
					me.name, sptr->name, to);
			return -1;
		}

	}

	MyFree(recipients);
	
	return 0;

}
