#include "module.h"

#define AUTHOR "Viper"
#define VERSION "0.50"

    /* -----------------------------------------------------------
     * Name  : bs_nickserv
     * Author: Viper <Viper@absurd-irc.net>
     * Date  : 07/09/05
     * -----------------------------------------------------------
     * Tested: Unreal(3.2.2)
     * -----------------------------------------------------------
     * This is my first module and i d like to thank anope support and all
     *  other coders for providing those handy help files, faqs and tutorials ^^
     * I used a few other modules to get this one together - mainly bs_fantasytemplate - , so thx to the other
     * coders.
     *
     * version 0.30 -> 0.50
     * cleaned up unnecessary code
     *
     * version 0.21
     * fixed small typo
     *
     * version  0.20
     * made it compatible with anope 1.7.10
     *
     * version: 0.10
     * Complete rewrite based on bs_fantasytemplate
     * Rename to bs_nickserv (org: ns_nickserv)
     *
     * version: 0.01
     * It works ^^
     *
     * -----------------------------------------------------------
     */

/*---------------------Configuration Block-------------------*/


#define MSG1 "Hello I am NickServ, a service that allows users to register their nicks."
#define MSG2 "To register your nick just reply to this message with the the following: 'REGISTER <pass> <email>' (without the 's)"
#define MSG3 "Replace <pass> with a password of your choice (don't make it too simple though)"
#define MSG4 "and <email> with your e-mail address"
#define MSG5 "To identify for an allready registered nick, reply to this message with 'IDENTIFY <pass>' (without the 's)"
#define MSG6 "All my replies to you will be shown in your status window (for normal mIRC)."
#define MSG7 "For mode info, type help (replies go to status window!)"
//#define MSG8 ""
//#define MSG9 ""


/*---------------End of Configuration Block-------------------*/


int my_privmsg(char *source, int ac, char **av);
int mynewmsg(User * u, ChannelInfo * ci, char *text);
//char *mod_current_buffer = NULL;
char *text=NULL;

void AnopeInit(void){
    Message *msg = NULL;
    int status;
    msg = createMessage("PRIVMSG", my_privmsg);
    status = moduleAddMessage(msg, MOD_HEAD);
    moduleAddAuthor(AUTHOR);
    moduleAddVersion(VERSION);
    alog("bs_nickserv: !nickserv command loaded, message status [%d]", status);
    alog("All hail Viper !!! Hail Viper !!! Hail Viper !!!"); // j/k :D
}

void AnopeFini(void)
{
	alog("Unloading bs_nickserv...");
}

int my_privmsg(char *source, int ac, char **av)
{
    User *u;
    ChannelInfo *ci;


    if (ac != 2)							/* First, some basic checks */
        return MOD_CONT;
    if (!(u = finduser(source))) {			/* make sure the source is a user*/
        return MOD_CONT;
    }
    if (*av[0] == '#') {				/* make sure it's being said in a channel*/
        if (s_BotServ && (ci = cs_findchan(av[0])))
            if (!(ci->flags & CI_VERBOTEN) && ci->c && ci->bi)
				if (av[1]) {   // these three lines optimize by skipping
				text = sstrdup(av[1]);  // more stuff if the line does not start with !
				if(text[0]=='!')         // If you want to make commands that don't, remove them.
				mynewmsg(u, ci, text);	/* This is the function we call on every channelmsg */
				Anope_Free(text);
    }}
        return MOD_CONT;
}

void ns_priv_help(User *u) {

#ifdef MSG1
    privmsg(s_NickServ,u->nick,MSG1);
#endif

#ifdef MSG2
    privmsg(s_NickServ,u->nick,MSG2);
#endif

#ifdef MSG3
    privmsg(s_NickServ,u->nick,MSG3);
#endif

#ifdef MSG4
    privmsg(s_NickServ,u->nick,MSG4);
#endif

#ifdef MSG5
    privmsg(s_NickServ,u->nick,MSG5);
#endif

#ifdef MSG6
    privmsg(s_NickServ,u->nick,MSG6);
#endif

#ifdef MSG7
    privmsg(s_NickServ,u->nick,MSG7);
#endif

#ifdef MSG8
    privmsg(s_NickServ,u->nick,MSG8);
#endif

#ifdef MSG9
    privmsg(s_NickServ,u->nick,MSG9);
#endif

}

int mynewmsg(User * u, ChannelInfo *ci, char *text) // called every channel message said in a channel where a bot is assigned
{
	char *cmd=NULL; // initialize everything
	char dilim = ' ';

	cmd = myStrGetToken(text, dilim, 0);

	if(!cmd) // if there is no first word (when the line starts with a space), stop to prevent crash.
	return MOD_CONT;

	if (!stricmp(cmd, "!nickserv")) {
		ns_priv_help(u);
	}


    if (!cmd)
        return MOD_CONT;

	return MOD_CONT;
}

