cb21075d |
1 | // ServerConnection.C -*- C++ -*- |
2 | // Copyright (c) 1997, 1998 Etienne BERNARD |
3 | // Copyright (C) 2002 Clinton Ebadi |
4 | |
5 | // This program is free software; you can redistribute it and/or modify |
6 | // it under the terms of the GNU General Public License as published by |
7 | // the Free Software Foundation; either version 2 of the License, or |
8 | // any later version. |
9 | |
10 | // This program is distributed in the hope that it will be useful, |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | // GNU General Public License for more details. |
14 | |
15 | // You should have received a copy of the GNU General Public License |
16 | // along with this program; if not, write to the Free Software |
39b022cb |
17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
cb21075d |
18 | |
cfa82921 |
19 | #include "ServerConnection.H" |
20 | |
cb21075d |
21 | #include <ctime> |
22 | |
cb21075d |
23 | #include "Bot.H" |
cfa82921 |
24 | #include "Channel.H" |
25 | #include "Commands.H" |
26 | #include "Parser.H" |
27 | #include "Server.H" |
28 | |
cb21075d |
29 | |
30 | ServerConnection::ServerConnection(Bot *b, Server *s, String localIP) |
31 | : Connection(s->getHostName(), |
32 | s->getPort(), localIP), |
33 | server(s), |
34 | bot(b), |
35 | queue(new ServerQueue(&socket, b->debug)), |
36 | pingTime(0), lag(0), serverLastSpoken(time(0)), |
37 | debug(b->debug) |
38 | { |
39 | b->connected = false; |
40 | } |
41 | |
42 | ServerConnection::~ServerConnection() |
43 | { |
44 | delete queue; |
45 | } |
46 | |
47 | bool |
48 | ServerConnection::connect() |
49 | { |
50 | if (!socket.connect()) |
51 | return false; |
52 | |
53 | socket.setNonBlocking(); |
54 | |
55 | if (server->getPassword().length() != 0) |
56 | queue->sendPass(server->getPassword()); |
57 | queue->sendNick(bot->wantedNickName); |
58 | queue->sendUser(bot->userName, bot->ircName); |
59 | |
60 | queue->flush(); |
61 | |
62 | return true; |
63 | } |
64 | |
65 | bool |
66 | ServerConnection::handleInput() |
67 | { |
68 | String line = socket.readLine(); |
69 | |
70 | if (line.length() == 0) |
71 | return true; |
72 | |
73 | if (bot->debug) |
74 | std::cout << line << "\n"; |
75 | |
76 | serverLastSpoken = time(0); |
77 | |
78 | Parser::parseLine(this, line); |
79 | |
80 | return false; |
81 | } |