view sources/connection.cpp @ 2:19227b0b7cc1

.h => .hpp for the headers added year 2008 to copyright notes renamed some functions for better meaning prepared parser module for rewriting added showHelp() function some code clean up
author Vlad Glagolev <enqlave@gmail.com>
date Mon, 21 Jan 2008 01:14:10 +0300
parents bafff9de2a76
children a7051ac7118b
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 (!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, 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;
}