 /* 
 *********************************************************************
 **  Written by      : srcmaster
 *********************************************************************
 */

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


DLLFUNC int m_cptime(Cmdoverride *cmdover, aClient *cptr, aClient *sptr, int parc, char *parv[]);
static int check_user_status(aClient *sptr, char *dest);

static Cmdoverride *pmover;
ModuleInfo Cpt;

#define RPMTIME 60 /* Time requirement for private messages */
//#define INFCHAN "#SpyingMonitor"

ModuleHeader MOD_HEADER(cptime)
  = {
	"cptime",
	"written by srcmaster",
	"Checking pm time for newly user connections",
	"3.2-b8-1",
	NULL 
    };

DLLFUNC int MOD_INIT(cptime)(ModuleInfo *modinfo)
{
        bcopy(modinfo,&Cpt,modinfo->size);  	
        return MOD_SUCCESS;
}

DLLFUNC int MOD_LOAD(cptime)(int module_load)
{
        pmover = CmdoverrideAdd(Cpt.handle, "PRIVMSG", m_cptime);
        if (!pmover)
		return MOD_FAILED;
        return MOD_SUCCESS;
}

DLLFUNC int MOD_UNLOAD(cptime)(int module_unload)
{
        CmdoverrideDel(pmover);
        return MOD_SUCCESS;
}

DLLFUNC int m_cptime(Cmdoverride *cmdover, aClient *cptr, aClient *sptr, int parc, char *parv[]) 
{
	aChannel *chptr;
	char *dest = parv[1];
	char *text = parv[2];
	int pm_ok;
 
	
	if (!(parc < 3 || *dest == '\0' || *text == '\0'))
	{
	 	pm_ok = check_user_status(sptr, dest);
		
		/* The '\001' parsing actually verbose.But if we not parse this, 
	         * the version check does not keep going properly.
		 */
		if (!pm_ok && (strstr(text, "\001") == NULL)) {
			
			/* This is kinda unethical, but you may wanna use it..
			if ((chptr = find_channel(INFCHAN, NullChn)) != NullChn)
				sendto_channel_butone(&me, &me, chptr, ":%s PRIVMSG %s :User: \2%s\2  Message: [\2%s\2] Time to approve: \2%d\2", me.name, INFCHAN, sptr->name, parv[2], RPMTIME - (TStime() - sptr->firsttime));
			 */
				sendnotice(sptr, "Özele mesaj yazmak için %d saniye daha beklemeniz gerekiyor", RPMTIME - (TStime() - sptr->firsttime));
			return 0;
		} 
		return CallCmdoverride(pmover, cptr, sptr, parc, parv);
	} 
	return CallCmdoverride(pmover, cptr, sptr, parc, parv);
}

static int check_user_status(aClient *sptr, char *dest) 
{
	aClient *cptr = find_person(dest, NULL);

	if (cptr && MyClient(sptr) && !(IsARegNick(sptr) || IsAnOper(sptr))) 
	{
		if (*dest != '#' && !IsULine(cptr))
		{
			if (sptr->firsttime + RPMTIME > TStime())
			{
				return 0;
			}
		}

	}
}



