/*
 * Turkish :
 * - Bu Modul Sky-Dancer Tarafindan Yazilmis/Editlenmistir.
 * - Bu Modulu Kullaniyorsaniz Telif Hakki Yazilarini Silemezsiniz.
 * - Bu Modulu Kullanarak LICENSE.UNOFF.TR adli okuma dosyasi icindeki Lisansi
 *   Kabul Etmis Sayiliyorsunuz.
 * - Dagitim Tarihi : 13 / 02 / 2007
 */

/*
 * English :
 * - This Module Codded/Edited By Sky-Dancer.
 * - if you are using this module, you can not delete copyright texts on this module.
 * - if you are using this module, You Are Accepting The License in LICENSE.ENG named file.
 * - Release Date : 13 / 02 / 2007
 */


#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

// For English Version (ingilizce Versiyon icin)
#define MODENG
// For Turkish Version (Turkce Versiyon icin)
//#undef MODENG 

// English : if you define this, all netadmins will see who used allsend command.
// Turkish : Eger Bunu Define ederseniz, Tum netadminler allsend komutunun uygulandigini gorecektir..
// Note (English) : For Disable, #undef SEND_COMMAND_USES
// Note (Turkish) : iptal etmek icin, #undef SEND_COMMAND_USES
#define SEND_COMMAND_USES

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

/* Prototypes */
void Delete_Command(Command *Dlt);
static int m_allsend(aClient *cptr, aClient *sptr, int parc, char *parv[]);
static Command *AddCommand(Module *module, char *msg, char *token, iFP func, int params);

/* Defines & Variables */
#define MSG_ALLSEND	"ALLSEND"
#define TOK_ALLSEND	"4LLS3ND"
Command *C_AllSend=NULL;

/* Lang Defines; Yes, This is an ugly language switcher but, need a basic language switcher :) */
#ifdef MODENG
	#define ALLSEND_LANG_MODHEAD "Send Messages To All Users v1.0.4 (By Sky-Dancer)"
	#define ALLSEND_LANG_SYNTAX "Syntax : /allsend users/opers/all notice/private local/global <[Fake]Sender> <Message>"
	#define ALLSEND_LANG_SYNTAX2 "\2users\2 : users; \2opers\2 : opers (& admins); \2all\2 : all users"
	#define ALLSEND_LANG_SYNTAX3 "\2notice\2 : notice; \2private\2 : private"
	#define ALLSEND_LANG_SYNTAX4 "\2local\2 : local; \2global\2 : All Linked Servers"
	#define ALLSEND_LANG_SYNTAX5 "-"
	#define ALLSEND_LANG_SYNTAX6 "Exmpl : /allsend users notice global Nick Hi All Users."
//	#define ALLSEND_LANG_NOT_OPER "Only Opers Can Use This Command.."
	#define ALLSEND_LANG_NOT_OPER "Only NetAdmins Can Use This Command.."
	#define ALLSEND_LANG_SENT_TEXT "Count For Sent Message(s)"
	#define ALLSEND_LANG_USERS "User(s)"
	#define ALLSEND_LANG_ADMINS "Oper(s)"
	#define ALLSEND_LANG_TOTAL "Total :"
	#define ALLSEND_LANG_NICK_USED "Used /AllSend Command."
#else
	#define ALLSEND_LANG_MODHEAD "Toplu Mesaj Gonderme v1.0.4 (Yapimci : Sky-Dancer) - 4RodixBilisim Shell Hosting"
	#define ALLSEND_LANG_SYNTAX "Kullanimi : /allsend users/opers/all notice/private local/global <[Sahte]Gonderen> <Mesaj>"
	#define ALLSEND_LANG_SYNTAX2 "\2users\2 : Kullanicilar; \2opers\2 : operlar (& adminler); \2all\2 : Tum Herkes"
	#define ALLSEND_LANG_SYNTAX3 "\2notice\2 : notice; \2private\2 : private"
	#define ALLSEND_LANG_SYNTAX4 "\2local\2 : yerel; \2global\2 : Tum Linkli Serverlar"
	#define ALLSEND_LANG_SYNTAX5 "-"
	#define ALLSEND_LANG_SYNTAX6 "Ornek : /allsend users notice global Nick Merhaba Kullanicilar."
//	#define ALLSEND_LANG_NOT_OPER "Bu Komutu Sadece Operlar Kullanabilirler.."
	#define ALLSEND_LANG_NOT_OPER "Bu Komutu Sadece Network Adminler Kullanabilirler.."
	#define ALLSEND_LANG_SENT_TEXT "Mesaj Gonderilen Sayi"
	#define ALLSEND_LANG_USERS "Kullanici"
	#define ALLSEND_LANG_ADMINS "Admin"
	#define ALLSEND_LANG_TOTAL "Toplam :"
	#define ALLSEND_LANG_NICK_USED "/AllSend Komutunu Kullandi."
#endif

#ifndef STATIC_LINKING
static ModuleInfo	*Mod_AllSend;
#endif

/* Main Module Header For UnrealIRCd */
ModuleHeader MOD_HEADER(m_allsend) = {
	"m_allsend",
	ALLSEND_LANG_MODHEAD,
	"64 bit edit OmerAti",
	"3.2-b8-1",
	NULL 
};

/* MOD_TEST Function for our dll Binary */
DLLFUNC int MOD_TEST(m_allsend)(ModuleInfo *modinfo) {
#ifndef STATIC_LINKING
	// For Later Uses.. Maybe Later :P
	Mod_AllSend = modinfo;
#endif
	return MOD_SUCCESS;
}

/* MOD_INIT Function for our dll Binary */
DLLFUNC int MOD_INIT(m_allsend)(ModuleInfo *modinfo) {
	int ret = MOD_SUCCESS;
	C_AllSend = AddCommand(modinfo->handle, MSG_ALLSEND, TOK_ALLSEND, m_allsend, 5);
	if (!C_AllSend)
		ret = MOD_FAILED;
return ret;
}

/* MOD_LOAD Function for our dll Binary */
DLLFUNC int MOD_LOAD(m_allsend)(int module_load) {
return MOD_SUCCESS;
}

/* MOD_UNLOAD Function for our dll Binary */
DLLFUNC int MOD_UNLOAD(m_allsend)(int module_unload) {
	Delete_Command(C_AllSend);
return MOD_SUCCESS;
}

void Delete_Command(Command *Dlt) {
	if (Dlt) {
		CommandDel(Dlt);
		Dlt = NULL;
	}
}

/* AddCommand Function for New Command Adding.. */
static Command *AddCommand(Module *module, char *msg, char *token, iFP func, int params) {
	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, params, 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_allsend function (for our command) */
static int m_allsend(aClient *cptr, aClient *sptr, int parc, char *parv[]) {
	aClient		*acptr;
	int i=0,t=0;
	char tipi[256];
	int SendMode, NoticeMode, LocalMode;

// This is for Opers Only.
/*	if (!IsOper(sptr)) {
		sendto_one(sptr, ":%s %d %s :%s", me.name, RPL_INFO, sptr->name, ALLSEND_LANG_NOT_OPER);
	return 0;
	}
*/

	if (!IsNetAdmin(sptr)) {
		sendto_one(sptr, ":%s %d %s :%s", me.name, RPL_INFO, sptr->name, ALLSEND_LANG_NOT_OPER);
	return 0;
	}

	if (parc<6) goto Syntax;

	if (!strcmp(parv[1],"users")) SendMode = 0;
	else if (!strcmp(parv[1],"opers")) SendMode = 1;
	else if (!strcmp(parv[1],"all")) SendMode = 2;
	else goto Syntax;

	if (!strcmp(parv[2],"private")) NoticeMode = 0;
	else if (!strcmp(parv[2],"notice")) NoticeMode = 1;
	else goto Syntax;

	if (!strcmp(parv[3],"global")) LocalMode = 0;
	else if (!strcmp(parv[3],"local")) LocalMode = 1;
	else goto Syntax;

	for (acptr = client; acptr; acptr = acptr->next) {

		if (acptr->name==me.name) {
		continue;
		}

		if (IsServer(acptr)) {
			if (LocalMode==0) {
				sendto_serv_butone(cptr, ":%s "MSG_ALLSEND" %s %s %s %s :%s",
					sptr->name, parv[1], parv[2], parv[3], parv[4], parv[5]);
			}
		continue;
		}

/*
		// With This, Sender won't Recieve The Text.
		if (acptr==sptr) {
		continue;
		}
*/

		if (!IsULine(acptr)) {
			if (MyClient(acptr)) {
				if (IsOper(acptr)) {
					// Opers
					if (SendMode==1 || SendMode==2) {
						if (NoticeMode==0)
							sendto_one(acptr, ":%s PRIVMSG %s :%s", parv[4], acptr->name, parv[5]);
						else
							sendto_one(acptr, ":%s NOTICE %s :%s", parv[4], acptr->name, parv[5]);
					t++;
					}
				}
				else {
					// users
					if (SendMode==0 || SendMode==2) {
						if (NoticeMode==0)
							sendto_one(acptr, ":%s PRIVMSG %s :%s", parv[4], acptr->name, parv[5]);
						else
							sendto_one(acptr, ":%s NOTICE %s :%s", parv[4], acptr->name, parv[5]);
					i++;
					}
				}
				#ifdef SEND_COMMAND_USES
				if (IsNetAdmin(acptr)) {
					// This is an ugly hook.. But, maybe, it can be best for don't warn the sender :)
					if (acptr!=sptr) {
						if (LocalMode==0)
							sendto_one(acptr, ":%s NOTICE %s :\2%s\2 %s (G) (%s) (%s) (%s) [Text : %s]",
							me.name, acptr->name, sptr->name, ALLSEND_LANG_NICK_USED, parv[1], parv[2],
							parv[3], parv[5]);
						else
							sendto_one(acptr, ":%s NOTICE %s :\2%s\2 %s (L) (%s) (%s) (%s) [Text : %s]",
							me.name, acptr->name, sptr->name, ALLSEND_LANG_NICK_USED, parv[1], parv[2],
							parv[3], parv[5]);
					}
				}
				#endif
			}
			/*
			else { 
				// unknown
			}
			*/
		}
	}

	if (SendMode==2) {
		sendto_one(sptr, ":%s NOTICE %s :%s : %d %s %d %s, %s %d",
			me.name, sptr->name, ALLSEND_LANG_SENT_TEXT, i, ALLSEND_LANG_USERS, t,
			ALLSEND_LANG_ADMINS, ALLSEND_LANG_TOTAL, i+t);
	}

	if (SendMode==1 || SendMode==0) {
		sendto_one(sptr, ":%s NOTICE %s :%s : %d %s",
			me.name, sptr->name, ALLSEND_LANG_SENT_TEXT, SendMode?t:i,
			SendMode?ALLSEND_LANG_ADMINS:ALLSEND_LANG_USERS);
	}

return 0;

Syntax:
	sendto_one(sptr, ":%s NOTICE %s :%s", me.name, sptr->name, ALLSEND_LANG_SYNTAX);
	sendto_one(sptr, ":%s NOTICE %s :%s", me.name, sptr->name, ALLSEND_LANG_SYNTAX2);
	sendto_one(sptr, ":%s NOTICE %s :%s", me.name, sptr->name, ALLSEND_LANG_SYNTAX3);
	sendto_one(sptr, ":%s NOTICE %s :%s", me.name, sptr->name, ALLSEND_LANG_SYNTAX4);
	sendto_one(sptr, ":%s NOTICE %s :%s", me.name, sptr->name, ALLSEND_LANG_SYNTAX5);
	sendto_one(sptr, ":%s NOTICE %s :%s", me.name, sptr->name, ALLSEND_LANG_SYNTAX6);
return 0;
}

