view sources/connection.cpp @ 7:8d54d9fdeca3

fixed comments removed daemonization verbose strings' size() -> length()
author Vlad Glagolev <enqlave@gmail.com>
date Sun, 03 Feb 2008 21:29:41 +0300
parents 9be05a31b7f9
children
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.hpp

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

	now = CABAL->now;

	uptime = 0;

	if (CABAL->debug)
		cout << "CONNECTION: opening...\n";
}

Connection::~Connection()
{
	if (CABAL->debug)
		cout << "CONNECTION: closed\n";
}

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

	if (!connected && !connecting) {
		if (CABAL->debug)
			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 p)
{
	sendMsg("PASS " + p);
}

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

void Connection::ircUser(string u, string n)
{
	sendMsg("USER " + u + (virtualhost ? " " + (string)virtualhost + " ": \
			" 0 ") + (string)current->host + " :" + n);
}

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

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

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

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

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;
}