#include <stdio.h>
#include "module.h"
#define AUTHOR "Certus"
#define VERSION "3.1"

/**
 * network-vhost on nickserv register
 * Date: 11/07/2003
 * Author: Raistlin <selcuk@turkislem.com>
 **/

 /* In v3.0 I recoded almost the whole module and I decided to change
  * the nethost system so you can now define a nethost with the result
  * <nickname>.<NETHOST>
  * This means: there is no longer an _identic_ host for _every_ user.
  */

  /* v3.0:
   * Fixed a memleak, you should restart services before updating this module.
   * Changed multiple strcat()s to a single snprintf()
   * Changed nethost system, see above information
   * non-valid vhosts characters in a nick will now be replaced by a dot (yay @ Larcen)
   * Rob is evil! (hidden comment)
   */

  /* v3.1:
   * Woozaa, fixed a really fucked up crashbug. Thanks to hexa <0x51@insiderz.org>.
   */

  /***************************************************************************/

/* PLEASE EDIT THIS LINE LIKE YOU WANT IT, WITH AN UNDEFINED
 * NETHOST, THE NETWORK VHOST WILL BE
 * <nickname>.<NetworkDomain>
 * SO MAKE SURE THAT NetworkDomain IS DEFINED
 * IN YOUR SERVICES CONFIG FILE
 */

#define NETHOST "eroin.org" 

/* DON'T CHANGE ANY OF THE LINES BELOW, UNLESS YOU REALLY KNOW WHAT YOU DO */

/***************************************************************************/


int set_netvhost(User *u);
int set_confirm_netvhost(User *u);
char dilim = ' ';
extern void addHostCore(char *nick, char *vIdent, char *vhost, char *creator, int32 tmp_time);
extern char *getvHost(char *nick);
void alnum_check(char *s);

void AnopeInit(void) {

	Command *c;
	c=createCommand("register",set_netvhost,NULL,-1,-1,-1,-1,-1);
	alog("Loading module hs_nethost.so [Status: %d]",moduleAddCommand(NICKSERV,c,MOD_TAIL));
	c=createCommand("group",set_netvhost,NULL,-1,-1,-1,-1,-1);
	moduleAddCommand(NICKSERV,c,MOD_TAIL);
	c=createCommand("confirm",set_confirm_netvhost,NULL,-1,-1,-1,-1,-1);
	moduleAddCommand(NICKSERV,c,MOD_TAIL);
#ifndef NETHOST
	if (NetworkDomains[0])
	alog("Network vHost: nickname.%s", NetworkDomains[0]);
#else
	alog("Network vHost: %s", NETHOST);
#endif
}

void AnopeFini(void) {
	alog("Unloading module hs_nethost.so");
}


int set_netvhost(User *u) {

	int32 tmp_time;
	char *vIdent = NULL;
	char *networkhost=NULL;
	int len = 0;
	tmp_time = time(NULL);

	if (!nick_identified(u) || (getvHost(u->nick) != NULL))
		return MOD_CONT;

#ifndef NETHOST
		if(NetworkDomains[0]) {
			len = strlen(NetworkDomains[0]);
			len += strlen(u->nick);
			if((networkhost = malloc(sizeof(char)*len+3))!=NULL) {
				snprintf(networkhost, (sizeof(char)*len+3), "%s.%s", u->nick, NetworkDomains[0]);
			} else {
				return MOD_CONT;
			}
		} else {
			return MOD_CONT;
		}
#else
	len = strlen(NETHOST);
	len += strlen(u->nick);
	if((networkhost = malloc(sizeof(char)*len+3))!=NULL) {
		snprintf(networkhost, (sizeof(char)*len+3), "%s.%s", u->nick, NETHOST);
	} else {
		return MOD_CONT;
	}
#endif

	alnum_check(networkhost);
	alog("Creating vhost for nick %s (%s)", u->nick, networkhost);

	addHostCore(u->nick, vIdent, networkhost, u->nick, tmp_time);
	notice_lang(s_HostServ, u, HOST_SET, u->nick, networkhost);
	do_on_id(u);
	free(networkhost);
	return MOD_CONT;
}


int set_confirm_netvhost(User *u) {

	int32 tmp_time;
	char *nick = NULL;
	char *vIdent = NULL;
	char *networkhost=NULL;
	char *command = NULL;
	int len = 0;
	tmp_time = time(NULL);
	
	command = moduleGetLastBuffer();

	if (!NSEmailReg) {
		notice(s_NickServ, u->nick, "NSEmailReg is not active");
		return MOD_CONT;
	}

	if (!nick_identified(u) || (getvHost(u->nick) != NULL))
		return MOD_CONT;

	if (is_services_admin(u)) {
		nick = myStrGetToken(command, dilim, 0); /* Don't forget to free() me!!!! */
		if (!nick) {
			return MOD_CONT;
		} if (!findnick(nick)) {
			free(nick);
			return MOD_CONT;
		}
	} else {
		nick = u->nick;
	}

#ifndef NETHOST
		if(NetworkDomains[0]) {
			len = strlen(NetworkDomains[0]);
			len += strlen(nick);
			if((networkhost = malloc(sizeof(char)*len+3))!=NULL) {
				snprintf(networkhost, (sizeof(char)*len+3), "%s.%s", nick, NetworkDomains[0]);
			} else {
				return MOD_CONT;
			}
		} else {
			if (is_services_admin(u))
				free(nick);
			return MOD_CONT;
		}
#else
	len = strlen(NETHOST);
	len += strlen(nick);
	if((networkhost = malloc(sizeof(char)*len+3))!=NULL) {
		snprintf(networkhost, (sizeof(char)*len+3), "%s.%s", nick, NETHOST);
	} else {
		return MOD_CONT;
	}
#endif

	alnum_check(networkhost);
	alog("Creating vhost for nick %s (%s)", nick, networkhost);

	addHostCore(nick, vIdent, networkhost, nick, tmp_time);
	notice_lang(s_HostServ, u, HOST_SET, nick, networkhost);
	if (!is_services_admin(u))
		do_on_id(u);
	free(networkhost);
	if (is_services_admin(u))
		free(nick);
	return MOD_CONT;
}

void alnum_check(char *s) {
	register int c;

    while ((c = *s)) {
		if (!isalnum(c)) {
			*s = '.';
		}
	s++;
	}
	return;
}


