[project @ 2005-06-24 22:10:09 by unknown_lamer]
[clinton/bobotpp.git] / source / ServerConnection.C
CommitLineData
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
19#include <ctime>
20
21#include "ServerConnection.H"
22#include "Parser.H"
23#include "Bot.H"
24
25ServerConnection::ServerConnection(Bot *b, Server *s, String localIP)
26 : Connection(s->getHostName(),
27 s->getPort(), localIP),
28 server(s),
29 bot(b),
30 queue(new ServerQueue(&socket, b->debug)),
31 pingTime(0), lag(0), serverLastSpoken(time(0)),
32 debug(b->debug)
33{
34 b->connected = false;
35}
36
37ServerConnection::~ServerConnection()
38{
39 delete queue;
40}
41
42bool
43ServerConnection::connect()
44{
45 if (!socket.connect())
46 return false;
47
48 socket.setNonBlocking();
49
50 if (server->getPassword().length() != 0)
51 queue->sendPass(server->getPassword());
52 queue->sendNick(bot->wantedNickName);
53 queue->sendUser(bot->userName, bot->ircName);
54
55 queue->flush();
56
57 return true;
58}
59
60bool
61ServerConnection::handleInput()
62{
63 String line = socket.readLine();
64
65 if (line.length() == 0)
66 return true;
67
68 if (bot->debug)
69 std::cout << line << "\n";
70
71 serverLastSpoken = time(0);
72
73 Parser::parseLine(this, line);
74
75 return false;
76}