/**
 * connection.cpp (2007-04-12)
 *
 * -- CABAL -- connection base (servers usage)
 *
 * Copyright (c) 2007, NOD Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *	* Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 *	* 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.
 *	* Neither the name of the NOD Team nor the names of its contributors
 *	  may be used to endorse or promote products derived from this
 *	  software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
 */

#include "cabal.h"	// required: core.h

Connection::Connection() : nick (NICK), user (USER), name (NAME), \
	chan (CHAN), ckey (CKEY)
{
	hosts = current = 0;

	now = CABAL->now;

	uptime = 0;

	cout << "CONNECTION: opening...\n";
}

Connection::~Connection()
{
	cout << "CONNECTION: closed\n";
}

void Connection::ircConnect()
{
	if (current == 0)
		shut ("no hosts defined");

	if (!connected && !connecting)
	{
		cout << "CONNECTION: connecting to server: " << current->host << endl;

		if (!hostResolve (current->host, &address))
		{
			cout << "Error: unable to resolve host " << current->host << endl;

			current = current->next;
		}
		else if (!tcpActivate (current->port, virtualhost))
		{
			cout << "Error: unable to open socket\n";

			return;
		}
	}

	if (!hostOpen())
		current = current->next;

	if (!connected)
		return;

	uptime = now;

	if (current->password)
		ircPass (current->password);

	ircNick (nick);
	ircUser (user, name);
	ircJoin (chan, ckey);
}

void Connection::establish()
{
	if (!connected)
	{
		ircConnect();

		return;
	}

	string buffer = msgReceive();

	if (buffer != "")
	{
		Parser *DATA = new Parser (this, buffer);

		DATA->Parse (this);

		delete DATA;
	}
}

void Connection::ircPass (string pass)
{
	msgSend ("PASS " + pass);
}

void Connection::ircNick (string nnick)
{
	msgSend ("NICK " + (nnick != "" ? nnick : NICK));
}

void Connection::ircUser (string nuser, string nname)
{
	msgSend ("USER " + nuser + (virtualhost != 0 ? " " + (string)virtualhost + \
		" " : " 0 ") + (string)current->host + " :" + nname);
}

void Connection::ircAway (string away)
{
	msgSend ("AWAY :" + away);
}

void Connection::ircJoin (string nchan, string nckey)
{
	msgSend ("JOIN " + nchan + (nckey != "" ? " " + nckey : ""));
}

void Connection::ircPart (string nchan)
{
	msgSend ("PART " + nchan);
}

void Connection::ircCycle (string nchan)
{
	msgSend ("PART " + nchan);
	msgSend ("JOIN " + nchan);
}

Connection::host_type *
Connection::hostAdd (host_type *list, pchar host, int port, pchar password)
{
	if (list == 0)
		current = list = new host_type (host, port, password);
	else if (list->next == current)
		list->next = new host_type (host, port, password, current);
	else
		return hostAdd (list->next, host, port, password);

	return list;
}
