comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:bafff9de2a76
1 /**
2 * connection.cpp (2007-04-12)
3 *
4 * -- CABAL -- connection base (servers usage)
5 *
6 * Copyright (c) 2007 Vlad Glagolev <enqlave@gmail.com>
7 * All rights reserved.
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22 #include "cabal.h" // required: core.h
23
24 Connection::Connection()
25 :
26 nick(NICK),
27 user(USER),
28 name(NAME),
29 chan(CHANNEL),
30 key(CHANNEL_KEY)
31 {
32 hosts = current = 0;
33
34 now = CABAL->now;
35
36 uptime = 0;
37
38 cout << "CONNECTION: opening...\n";
39 }
40
41 Connection::~Connection()
42 {
43 cout << "CONNECTION: closed\n";
44 }
45
46 void Connection::ircConnect()
47 {
48 if (!current)
49 shut ("no hosts defined");
50
51 if (!connected && !connecting) {
52 cout << "CONNECTION: connecting to server: " << current->host << endl;
53
54 if (!hostResolve (current->host, &address)) {
55 cout << "Error: unable to resolve host " << current->host << endl;
56
57 current = current->next;
58 } else if (!tcpActivate (current->port, virtualhost)) {
59 cout << "Error: unable to open socket\n";
60
61 return;
62 }
63 }
64
65 if (!hostOpen())
66 current = current->next;
67
68 if (!connected)
69 return;
70
71 uptime = now;
72
73 if (current->password)
74 ircPass(current->password);
75
76 ircNick(nick);
77 ircUser(user, name);
78 ircJoin(chan, key);
79 }
80
81 void Connection::establish()
82 {
83 if (!connected) {
84 ircConnect();
85
86 return;
87 }
88
89 string buffer = msgReceive();
90
91 if (buffer != "") {
92 Parser *DATA = new Parser (this, buffer);
93
94 DATA->Parse (this);
95
96 delete DATA;
97 }
98 }
99
100 void Connection::ircPass (string pass)
101 {
102 msgSend ("PASS " + pass);
103 }
104
105 void Connection::ircNick (string nnick)
106 {
107 msgSend ("NICK " + (nnick != "" ? nnick : NICK));
108 }
109
110 void Connection::ircUser (string nuser, string nname)
111 {
112 msgSend ("USER " + nuser + (virtualhost != 0 ? " " + (string)virtualhost + \
113 " " : " 0 ") + (string)current->host + " :" + nname);
114 }
115
116 void Connection::ircAway (string away)
117 {
118 msgSend ("AWAY :" + away);
119 }
120
121 void Connection::ircJoin (string nchan, string nckey)
122 {
123 msgSend ("JOIN " + nchan + (nckey != "" ? " " + nckey : ""));
124 }
125
126 void Connection::ircPart (string nchan)
127 {
128 msgSend ("PART " + nchan);
129 }
130
131 void Connection::ircCycle (string nchan)
132 {
133 msgSend ("PART " + nchan);
134 msgSend ("JOIN " + nchan);
135 }
136
137 Connection::host_type *
138 Connection::hostAdd (host_type *list, pchar host, int port, pchar password)
139 {
140 if (list == 0)
141 current = list = new host_type (host, port, password);
142 else if (list->next == current)
143 list->next = new host_type (host, port, password, current);
144 else
145 return hostAdd (list->next, host, port, password);
146
147 return list;
148 }