view sources/connection.cpp @ 4:a7051ac7118b

added primary parser implementation renamed some functions to well-formed format bool => false, pointers => NULL removed useless eTime()
author Vlad Glagolev <enqlave@gmail.com>
date Thu, 24 Jan 2008 19:38:34 +0300
parents 19227b0b7cc1
children 0faceb076254
line wrap: on
line source

/**
 * connection.cpp (2007-04-12)
 *
 * -- CABAL -- connection base (servers usage)
 *
 * Copyright (c) 2007-2008 Vlad Glagolev <enqlave@gmail.com>
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

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

Connection::Connection():
	nick(NICK),
	user(USER),
	name(NAME),
	chan(CHANNEL),
	key(CHANNEL_KEY)
{
	hosts = current = 0;

	now = CABAL->now;

	uptime = 0;

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

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

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

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

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

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

			return;
		}
	}

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

	if (!connected)
		return;

	uptime = now;

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

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

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

		return;
	}

	string buffer = receiveMsg();

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

		DATA->Parse(this);

		delete DATA;
	}
}

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

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

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

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

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

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

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

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

	return list;
}