Mercurial > cabal
diff sources/connection.cpp @ 0:bafff9de2a76
Initial commit since summer'07
| author | Vlad Glagolev <enqlave@gmail.com> |
|---|---|
| date | Sun, 20 Jan 2008 19:25:25 +0300 |
| parents | |
| children | 19227b0b7cc1 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sources/connection.cpp Sun Jan 20 19:25:25 2008 +0300 @@ -0,0 +1,148 @@ +/** + * connection.cpp (2007-04-12) + * + * -- CABAL -- connection base (servers usage) + * + * Copyright (c) 2007 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.h" // 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 = 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; +}
