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 ***************************************************************************/
25 #include <QMetaObject>
36 //Message::init(&dispatchServerMsg, &dispatchUserMsg);
40 connect(&socket, SIGNAL(connected()), this, SLOT(socketConnected()));
41 connect(&socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
42 connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
43 connect(&socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
44 connect(&socket, SIGNAL(readyRead()), this, SLOT(socketHasData()));
49 void Server::connectToIrc( const QString & host, quint16 port ) {
50 qDebug() << "Connecting...";
51 socket.connectToHost(host, port);
54 void Server::disconnectFromIrc( ) {
55 socket.disconnectFromHost();
58 void Server::socketHasData() {
59 while(socket.canReadLine()) {
60 QString s = socket.readLine().trimmed();
61 qDebug() << "Read: " << s;
62 emit recvRawServerMsg(s);
63 //Message *msg = Message::createFromServerString(this, s);
68 void Server::socketError( QAbstractSocket::SocketError err ) {
69 qDebug() << "Socket Error!";
73 void Server::socketConnected( ) {
74 qDebug() << "Socket connected!";
75 putRawLine("NICK :QuasselDev");
76 putRawLine("USER Sputnick 8 * :Using Quassel IRC (WiP Version)");
79 void Server::socketDisconnected( ) {
80 qDebug() << "Socket disconnected!";
81 //emit disconnected();
84 void Server::socketStateChanged(QAbstractSocket::SocketState state) {
85 qDebug() << "Socket state changed: " << state;
88 void Server::putRawLine(QString s) {
89 qDebug() << "SentRaw: " << s;
91 socket.write(s.toAscii());
94 void Server::putCmd(QString cmd, QStringList params, QString prefix) {
96 if(!prefix.isEmpty()) m += ":" + prefix + " ";
98 for(int i = 0; i < params.size() - 1; i++) {
101 if(!params.isEmpty()) m += " :" + params.last();
102 qDebug() << "SentCmd: " << m;
104 socket.write(m.toAscii());
107 /** Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
108 void Server::handleServerMsg(QString msg) {
111 qWarning() << "Received empty string from server!";
114 // OK, first we split the raw message into its various parts...
120 prefix = msg.section(' ', 0, 0);
121 msg = msg.section(' ', 1);
123 cmd = msg.section(' ', 0, 0).toUpper();
124 msg = msg.section(' ', 1);
125 QString left = msg.section(':', 0, 0);
126 QString trailing = msg.section(':', 1);
127 if(!left.isEmpty()) {
128 params << left.split(' ', QString::SkipEmptyParts);
130 if(!trailing.isEmpty()) {
133 // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
134 QString hname = cmd.toLower();
135 hname[0] = hname[0].toUpper();
136 hname = "handle" + hname + "FromServer";
137 if(!QMetaObject::invokeMethod(this, hname.toAscii(), Q_ARG(QString, prefix), Q_ARG(QStringList, params))) {
138 // Ok. Default handler it is.
139 defaultHandlerForServer(cmd, prefix, params);
141 } catch(Exception e) {
142 emit recvLine(e.msg());
146 void Server::defaultHandlerForServer(QString cmd, QString prefix, QStringList params) {
147 uint num = cmd.toUInt();
149 recvLine(cmd + " " + params.join(" "));
151 recvLine(QString("Unknown: ") + cmd + " " + params.join(" "));
155 void Server::handleUserMsg(QString usrMsg) {
160 void Server::handleServerMsg(Message *msg) {
161 int cmdCode = msg->getCmdCode();
162 QString prefix = msg->getPrefix();
163 QStringList params = msg->getParams();
167 // PING <server1> [<server2>]
168 if(params.size() < 1 || params.size() > 2) throw ParseError(msg);
169 putCmd("PONG", params);
173 throw Exception(QString("No handler installed for command: ") + msg->getCmd() + " " + msg->getParams().join(" "));
175 } else if(msg->getCmdCode() > 0) {
176 switch(msg->getCmdCode()) {
180 throw Exception(msg->getCmd() + " " + msg->getParams().join(" "));
184 throw UnknownCmdError(msg);
189 void Server::handleNoticeFromServer(QString prefix, QStringList params) {
190 recvLine(params.join(" "));
195 void Server::handlePingFromServer(QString prefix, QStringList params) {
196 putCmd("PONG", params);
199 /* Exception classes for message handling */
200 Server::ParseError::ParseError(QString cmd, QString prefix, QStringList params) {
201 _msg = QString("Command Parse Error: ") + cmd + params.join(" ");
205 Server::UnknownCmdError::UnknownCmdError(QString cmd, QString prefix, QStringList params) {
206 _msg = QString("Unknown Command: ") + cmd;