1 /***************************************************************************
2 * Copyright (C) 2005/06 by The Quassel Team *
3 * devel@quassel-irc.org *
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 * (at your option) any later version. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
26 socket = new QTcpSocket();
35 Message::init(&dispatchServerMsg, &dispatchUserMsg);
39 connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
40 connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
41 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
42 connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
43 connect(socket, SIGNAL(readyRead()), this, SLOT(socketHasData()));
45 stream.setDevice(socket);
46 //connectToIrc("irc.quakenet.org", 6667);
51 QAbstractSocket::SocketState TcpConnection::state( ) const {
52 return socket.state();
56 void Server::connectToIrc( const QString & host, quint16 port ) {
57 qDebug() << "Connecting...";
58 socket->connectToHost(host, port);
61 void Server::disconnectFromIrc( ) {
62 socket->disconnectFromHost();
65 void Server::putRawLine( const QString &s ) {
66 qDebug() << "Sent: " << s;
67 stream << s << "\r\n" << flush;
68 //Message::createFromServerString(this, s);
71 void Server::socketHasData( ) {
72 while(socket->canReadLine()) {
73 QString s = stream.readLine();
74 qDebug() << "Read: " << s;
75 emit recvRawServerMsg(s);
76 Message *msg = Message::createFromServerString(this, s);
78 try { handleServerMsg(msg); } catch(Exception e) {
79 emit recvLine(e.msg() + "\n");
86 void Server::socketError( QAbstractSocket::SocketError err ) {
87 qDebug() << "Socket Error!";
91 void Server::socketConnected( ) {
92 qDebug() << "Socket connected!";
96 void Server::socketDisconnected( ) {
97 qDebug() << "Socket disconnected!";
98 //emit disconnected();
101 void Server::socketStateChanged(QAbstractSocket::SocketState state) {
102 qDebug() << "Socket state changed: " << state;
105 /** Handle a message sent by the IRC server that does not have a custom handler. */
106 void Server::handleServerMsg(Message *msg) {
107 int cmdCode = msg->getCmdCode();
108 QString prefix = msg->getPrefix();
109 QStringList params = msg->getParams();
113 // PING <server1> [<server2>]
114 if(params.size() == 1) {
115 putRawLine(QString("PONG :") + params[0]);
116 } else if(params.size() == 2) {
117 putRawLine(QString("PONG ") + params[0] + " :" + params[1]);
118 } else throw ParseError(msg);
122 throw Exception(QString("No handler installed for command: ") + msg->getCmd() + " " + msg->getParams().join(" "));
124 } else if(msg->getCmdCode() > 0) {
125 switch(msg->getCmdCode()) {
129 throw Exception(msg->getCmd() + " " + msg->getParams().join(" "));
133 throw UnknownCmdError(msg);
137 QString Server::handleUserMsg(Message *msg) {
142 /* Exception classes for message handling */
143 Server::ParseError::ParseError(Message *msg) {
144 _msg = QString("Command Parse Error: ") + msg->getCmd() + msg->getParams().join(" ");
148 Server::UnknownCmdError::UnknownCmdError(Message *msg) {
149 _msg = QString("Unknown Command: ") + msg->getCmd();