#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 _WIN32 
#include "version.h" 
#endif 


#define MSG_GEC	   "IZINVER"
#define TOK_GEC	   "G4C"
#define MSG_MSJ    "MESAJAT"
#define TOK_MSJ    "MESAJ4AT"
#define MSG_KOMUTLAR   "SCRAC" 
#define TOK_KOMUTLAR   '\0'
#define KANAL        "#Baglanti"
#define CYCLE 8085
#define MOBIL 7003
#define WEBPORT 7032
#define DOSYAADI2       "girisac.conf"

int girisac(aClient *);

static Command		*AddCommand(Module *module, char *msg, char *token, iFP func, int params);
#define DelCommand(x) if (x) CommandDel(x); x = NULL
Command			*CmdGec, *CmdMsj;
 
#ifndef STATIC_LINKING
static ModuleInfo	*MyModInfo;
 #define MyMod		MyModInfo->handle
 #define SAVE_MODINFO	MyModInfo = modinfo;
#else
 #define MyMod		NULL
 #define SAVE_MODINFO
#endif

#define DelHook(x)		if (x) HookDel(x); x = NULL

Hook *HookJoin = NULL;
#define MSG_JOIN   "JOIN"

DLLFUNC int m_yasakla(aClient *sptr, aChannel *chptr, char *parv[]);
DLLFUNC int m_gec(aClient *cptr, aClient *sptr, int parc, char *parv[]);
DLLFUNC int m_msj(aClient *cptr, aClient *sptr, int parc, char *parv[]);
DLLFUNC int ajan_cikis(aClient *cptr, char *comment);
DLLFUNC char *m_noptext(aClient *, aClient *, aClient *, char *, int);

ModuleHeader MOD_HEADER(m_yasakla)
  = {
	"Modul",	
	"Modul-Aplet",
	"Special",
	"3.2-b8-1",
	NULL 
    };

DLLFUNC int MOD_INIT(yasakla)(ModuleInfo *modinfo)
{    
    MARK_AS_OFFICIAL_MODULE(modinfo);
	CmdGec = AddCommand(modinfo->handle, MSG_GEC, TOK_GEC, m_gec, MAXPARA);
	CmdMsj = AddCommand(modinfo->handle, MSG_MSJ, TOK_MSJ, m_msj, MAXPARA);
	SAVE_MODINFO
	HookJoin = HookAddEx(modinfo->handle, HOOKTYPE_PRE_LOCAL_JOIN, m_yasakla);
HookAddEx(modinfo->handle, HOOKTYPE_LOCAL_QUIT, ajan_cikis);
HookAddPCharEx(modinfo->handle, HOOKTYPE_USERMSG, m_noptext); 
add_Command(MSG_KOMUTLAR,  TOK_KOMUTLAR, girisac, 1);
	return MOD_SUCCESS;
}

DLLFUNC int MOD_LOAD(yasakla)(int module_load)
{

	return MOD_SUCCESS;
}

DLLFUNC int MOD_UNLOAD(yasakla)(int module_unload)
{
    DelHook(HookJoin);
	return MOD_SUCCESS;
}

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;
}

DLLFUNC int m_yasakla(aClient *sptr, aChannel *chptr, char *parv[])
{

    if (chptr && !IsMember(sptr, chptr))
    {    

    if ((sptr->umodes & UMODE_IGS))
      {
          if (IsServer(sptr)) return HOOK_CONTINUE;
		  if (IsOper(sptr)) return HOOK_CONTINUE;
		  if (IsULine(sptr)) return HOOK_CONTINUE;
	for (chptr = channel; chptr; chptr = chptr->nextch) {	  
    //sendto_one(sptr, ":%s NOTICE %s %s kanalina girisiniz engellenmistir.",
   // me.name, sptr->name, chptr->chname);
    return HOOK_DENY;
    }
   }
  }
  return HOOK_CONTINUE;
}


DLLFUNC int m_gec(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aClient *acptr;    
	if (!IsNetAdmin(sptr)) {
		sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]);
	return 0;
	}
	
    if (parc < 2) {
		sendto_one(sptr, ":%s NOTICE %s :Komutu eksik girdiniz Lutfen dogru sekilde girerek tekrar deneyin..", me.name, sptr->name);
		sendto_one(sptr, ":%s NOTICE %s :Kullanimi; /IZINVER nick", me.name, sptr->name);
	return 0;
	}       
	
	if (!(acptr = find_person(parv[1], NULL)))
		{
			sendto_one(sptr, err_str(ERR_NOSUCHNICK),
				me.name, parv[0], parv[1]);
			return 0;
		}
		
		if (!MyConnect(acptr))
		{
			sendto_one(sptr, ":%s NOTICE %s :*** %s kullanici degil. Yapma boyle sakalar bana :)",
				me.name, parv[0], acptr->name);
			return 0;
		}
         
         sendto_one(sptr, ":%s NOTICE %s :%s rumuzuna kanallara giris izni verilip, auto-join kanallarina alindi.", me.name, parv[0], acptr->name);
         sendto_one(acptr, ":%s MODE %s -Y", acptr->name, acptr->name); 
         acptr->umodes &= ~UMODE_IGS;
 sendto_realops("Yeni Giris Port %d: %s (%s@%s) [%s] [clients]", acptr->listener->port, acptr->name, acptr->user->username, acptr->user->realhost, acptr->user->realhost);
	if (!stricmp("Scr", sptr->name))  {
	aChannel *chptr;
		if ((chptr = find_channel(KANAL, NullChn)) != NullChn) { 
		sendto_channel_butserv(chptr, &me, ":%s PRIVMSG %s :12Baglanti Portu:4 %d: 4Nick:12 %s 4Ident:12 %s 4IP12 %s 12(%s12)", me.name, KANAL, acptr->listener->port, acptr->name, acptr->user->username, acptr->user->realhost, acptr->info);
					   return 0;
					}
  }  
  else {
		if (!BadPtr(AUTO_JOIN_CHANS) && strcmp(AUTO_JOIN_CHANS, "0"))
	     {
         char *chans[3] = {
			acptr->name,
			AUTO_JOIN_CHANS,
			NULL
		};
		do_cmd(acptr, acptr, "JOIN", 3, chans);
	}
	
	aChannel *chptr;
		if ((chptr = find_channel(KANAL, NullChn)) != NullChn) { 
		sendto_channel_butserv(chptr, &me, ":%s PRIVMSG %s :12Baglanti Portu:4 %d: 4Nick:12 %s 4Ident:12 %s 4IP12 %s 12(%s12)", me.name, KANAL, acptr->listener->port, acptr->name, acptr->user->username, acptr->user->realhost, acptr->info);
					   return HOOK_CONTINUE;
					}
					}
    return 0; 
} 

DLLFUNC int m_msj(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    int i;    
    aClient *acptr;    
    char Temp[512];
	if (!IsOper(sptr)) {
		sendto_one(sptr, err_str(ERR_NOPRIVILEGES), me.name, parv[0]);
	return 0;
	}
	
    if (parc < 4) {
		sendto_one(sptr, ":%s NOTICE %s :Komutu eksik girdiniz Lutfen dogru sekilde girerek tekrar deneyin..", me.name, sptr->name);
	return 0;
	}       
	
	if (!(acptr = find_person(parv[3], NULL)))
		{
sendto_one(sptr, ":%s NOTICE %s :Belirtilen Nick Sunuda Bulunmuyor", me.name, sptr->name);
			return 0;
		}
		
		if (!MyConnect(acptr))
		{
			sendto_one(sptr, ":%s NOTICE %s :*** %s kullanici degil. Yapma boyle sakalar bana :)",
				me.name, parv[0], acptr->name);
			return 0;
		}
		
		for (i = 4; i < parc; i++)
		    if (i == 4) strcpy(Temp, parv[i]);
	    else {
		     strcat(Temp, " ");
		     strcat(Temp, parv[i]);
        }
        sendto_one(acptr, ":%s PRIVMSG %s :%s", parv[2], parv[1], Temp);
		return 0;
		}
		
		
DLLFUNC int ajan_cikis(aClient *cptr, char *comment) {
		aChannel *chptr;
		if (!stricmp(cptr->name,"Scr")) {
FILE *DosyaAdi2;
    if ((DosyaAdi2 = fopen (DOSYAADI2,"w+"))){
    fprintf (DosyaAdi2, "#include \"scrac.conf\";");
   }
  fclose (DosyaAdi2);
		system("cd /home/$USER/Unreal3.2.10.7 && ./unreal rehash");
sendto_realops("4UYARI 2Scr 4Botu Cikti Girisler Manuel");
return 0;
}
		if (!(cptr->umodes & UMODE_IGS)) {
		if ((chptr = find_channel(KANAL, NullChn)) != NullChn) { 
		sendto_channel_butserv(chptr, &me, ":%s PRIVMSG %s :4Sunucudan Cikti: 12%s4 Ident:12 %s 4IP: 12 %s 4[%s4] 4 [%s4]", me.name, KANAL, cptr->name, cptr->user->username, cptr->user->realhost, cptr->info, comment);
					   return 0;
					}	
}					
		
		
		   return 0;
		        }
int girisac(aClient *sptr){


	if (!IsOper(sptr))
	{
		sendto_one(sptr,"*** (\2%s\2) Bu komuta yetkiniz yok!", sptr->name);
		return 0;
	}
FILE *DosyaAdi2;
    if ((DosyaAdi2 = fopen (DOSYAADI2,"w+"))){
    fprintf (DosyaAdi2, "include \"scrac.conf\";");
   }
  fclose (DosyaAdi2);
		system("cd /home/$USER/Unreal3.2.10.7 && ./unreal rehash");
sendto_realops("2Scr 4Kontrollu Giris Sistemi Aktif Edilmistir");

return 0;
}
DLLFUNC char *m_noptext(aClient *cptr, aClient *sptr, aClient *acptr, char *text, int notice) {
	if (!stricmp("Ajan", acptr->name))  {
		  return text;
	}
	if (!stricmp("Scr", acptr->name))  {
		  return text;
	}
  if ((sptr->umodes & UMODE_IGS)) {
	if (!IsServer(sptr) && !IsOper(sptr) && !IsULine(acptr)) {
		sendnotice(sptr, "*** Mesaj gondermeniz suan icin engellendi.");    
	    return NULL;		
	}
  }
  return text;
}
