/*---------------------------------------------------------------------------

 Copyright (c) 2005
	by Dirk Meyer, All rights reserved.
	Im Grund 4, 34317 Habichtswald, Germany
	Email: dirk.meyer@dinoex.sub.org

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. Neither the name of the author nor the names of any co-contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

-----------------------------------------------------------------------------

	identify - a plugin for xchat2
	==============================

	$Id: identify.c,v 1.4 2006/07/22 03:27:46 dm Exp $

----------------------------------------------------------------------------*/

#include "xchat-plugin.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>


int xchat_plugin_init(xchat_plugin *plugin_handle,
	const char **plugin_name,
	const char **plugin_desc,
	const char **plugin_version,
	const char *arg);
void xchat_plugin_deinit(void);


#define MAX_BUF 1024

static xchat_plugin *ph; /* plugin handle */
static int enable = 1;
static char nbuf[MAX_BUF];

#if 0
	word	text

	1	:dinoex!~dinoex@euirc-c679c75c.dinoex.sub.de
	2	PRIVMSG
	3	dinoex
	4	:xdcc
	5	list | send
	6	group | pack
#endif

static void
extract_nick(const char *from)
{
	size_t len;
	char *end;

	nbuf[0] = 0;
	if (from == NULL)
		return;
	if (from[0] == 0)
		return;
	from ++;
	len = strlen(from);
	if ((len <= 0) || (len >= sizeof(nbuf))) {
		xchat_printf(ph, "oversized nick %s\n", from);
		return;
	}
	strncpy(nbuf, from, len);
	nbuf[sizeof(nbuf) - 1] = 0;

	end = strchr(nbuf, '!' );
	if ( end == NULL )
		return;

	*end = 0;
	return;
}

static int
identify_cb(char *word[], char *word_eol[], void *userdata)
{
	const char *cptr;

	cptr = xchat_get_info(ph, "nickserv");
	if (cptr == NULL) {
		xchat_print(ph, "No password found in config\n" );
		return XCHAT_EAT_ALL;
	}
	xchat_print(ph, "send: identify *****\n" );
#ifdef WITHOUT_IDENTIFY
	xchat_commandf(ph, "privmsg nickserv identify %s", cptr);
#else
	xchat_commandf(ph, "identify %s", cptr);
#endif
	return XCHAT_EAT_ALL; /* eat this command so xchat and other plugins can't process it */
}

static void
parse_nickserv(char *word[], char *word_eol[], void *userdata)
{
	extract_nick(word[1]);
	if (xchat_nickcmp(ph,nbuf,"nickserv") != 0)
		return;

	xchat_printf(ph, "debug: nickserv %s\n", word_eol[4]);

	if (strcasecmp(word_eol[4], ":Nickname is registered to someone else") == 0) {
		identify_cb(word,word_eol,userdata);
	}
	if (strcasecmp(word_eol[4], ":This nickname has been registered") == 0) {
		identify_cb(word,word_eol,userdata);
	}
	if (strcasecmp(word_eol[4], ":This nickname is registered and protected.") == 0) {
		identify_cb(word,word_eol,userdata);
	}
	if (strcasecmp(word_eol[4], ":please choose a different nick.") == 0) {
		identify_cb(word,word_eol,userdata);
	}
	if (strcasecmp(word_eol[4], ":This nickname is registered and protected.") == 0) {
		identify_cb(word,word_eol,userdata);
	}
}


static int
privmsg_cb(char *word[], char *word_eol[], void *userdata)
{
	const char *str;
	int i;

	if (enable == 0)
		return XCHAT_EAT_NONE; /* don't eat this event, let other plugins and xchat see it too */

	str = NULL;
	if (xchat_get_prefs (ph, "irc_nick1", &str, &i) == 1) {
		if (xchat_nickcmp(ph,word[3], str) != 0) {
			parse_nickserv(word, word_eol, userdata);
			return XCHAT_EAT_NONE; /* don't eat this event, let other plugins and xchat see it too */
		}
	}

	return XCHAT_EAT_NONE; /* don't eat this event, let other plugins and xchat see it too */
}

static int
notice_cb(char *word[], char *word_eol[], void *userdata)
{
	const char *str;
	int i;

	if (enable == 0)
		return XCHAT_EAT_NONE; /* don't eat this event, let other plugins and xchat see it too */

	str = NULL;
	if (xchat_get_prefs (ph, "irc_nick1", &str, &i) == 1) {
		if (xchat_nickcmp(ph,word[3], str) != 0) {
			parse_nickserv(word, word_eol, userdata);
			return XCHAT_EAT_NONE; /* don't eat this event, let other plugins and xchat see it too */
		}
	}

	return XCHAT_EAT_NONE; /* don't eat this event, let other plugins and xchat see it too */
}

static int
identify_toggle_cb(char *word[], char *word_eol[], void *userdata)
{
	if (enable == 0) {
		enable = 1;
		xchat_print(ph, "identify now enabled!\n");
	} else {
		enable = 0;
		xchat_print(ph, "identify now disabled!\n");
	}

	return XCHAT_EAT_ALL; /* eat this command so xchat and other plugins can't process it */
}

int
xchat_plugin_init(xchat_plugin *plugin_handle,
	const char **plugin_name,
	const char **plugin_desc,
	const char **plugin_version,
	const char *arg)
{
	/* we need to save this for use with any xchat_* functions */
	ph = plugin_handle;

	*plugin_name = "identify";
	*plugin_desc = "authenticate against nickserv";
	*plugin_version = "0.1";

	xchat_hook_command(ph, "autoidentify", XCHAT_PRI_NORM, identify_toggle_cb,
		"Usage: autoidentify, Turns OFF/ON automatic identify", 0);
	xchat_hook_command(ph, "reidentify", XCHAT_PRI_NORM, identify_cb,
		"Usage: reidentify, authenticate to nickserv", 0);

	xchat_hook_server(ph, "PRIVMSG", XCHAT_PRI_NORM, privmsg_cb, NULL);
	xchat_hook_server(ph, "NOTICE", XCHAT_PRI_NORM, notice_cb, NULL);

	xchat_printf(ph, "identify Plugin %s loaded successfully!\n", *plugin_version);

	return 1; /* return 1 for success */
}

void
xchat_plugin_deinit(void)
{
}

/* EOF */
