1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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 ***************************************************************************/
20 #include "networkconnection.h"
22 #include <QMetaObject>
23 #include <QMetaMethod>
28 #include "coresession.h"
34 #include "ircserverhandler.h"
35 #include "userinputhandler.h"
36 #include "ctcphandler.h"
38 NetworkConnection::NetworkConnection(Network *network, CoreSession *session, const QVariant &state) : QObject(network),
40 _coreSession(session),
41 _ircServerHandler(new IrcServerHandler(this)),
42 _userInputHandler(new UserInputHandler(this)),
43 _ctcpHandler(new CtcpHandler(this)),
46 connect(network, SIGNAL(currentServerSet(const QString &)), this, SLOT(sendPerform()));
48 connect(&socket, SIGNAL(connected()), this, SLOT(socketConnected()));
49 //connect(&socket, SIGNAL(disconnected()), this, SLOT(quit())); FIXME
50 connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
51 connect(&socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
52 connect(&socket, SIGNAL(readyRead()), this, SLOT(socketHasData()));
56 NetworkConnection::~NetworkConnection() {
57 delete _ircServerHandler;
58 delete _userInputHandler;
62 bool NetworkConnection::isConnected() const {
63 return socket.state() == QAbstractSocket::ConnectedState;
66 NetworkId NetworkConnection::networkId() const {
67 return network()->networkId();
70 QString NetworkConnection::networkName() const {
71 return network()->networkName();
74 Network *NetworkConnection::network() const {
78 CoreSession *NetworkConnection::coreSession() const {
82 IrcServerHandler *NetworkConnection::ircServerHandler() const {
83 return _ircServerHandler;
86 UserInputHandler *NetworkConnection::userInputHandler() const {
87 return _userInputHandler;
90 CtcpHandler *NetworkConnection::ctcpHandler() const {
94 QString NetworkConnection::serverDecode(const QByteArray &string) const {
95 return network()->decodeString(string);
98 QString NetworkConnection::bufferDecode(const QString &bufferName, const QByteArray &string) const {
100 // TODO: Implement buffer-specific encodings
101 return network()->decodeString(string);
104 QString NetworkConnection::userDecode(const QString &userNick, const QByteArray &string) const {
105 IrcUser *user = network()->ircUser(userNick);
106 if(user) return user->decodeString(string);
107 return network()->decodeString(string);
110 QByteArray NetworkConnection::serverEncode(const QString &string) const {
111 return network()->encodeString(string);
114 QByteArray NetworkConnection::bufferEncode(const QString &bufferName, const QString &string) const {
115 Q_UNUSED(bufferName);
116 // TODO: Implement buffer-specific encodings
117 return network()->encodeString(string);
120 QByteArray NetworkConnection::userEncode(const QString &userNick, const QString &string) const {
121 IrcUser *user = network()->ircUser(userNick);
122 if(user) return user->encodeString(string);
123 return network()->encodeString(string);
127 void NetworkConnection::connectToIrc() {
128 QList<QVariantMap> serverList = network()->serverList();
129 Identity *identity = coreSession()->identity(network()->identity());
130 if(!serverList.count()) {
131 qWarning() << "Server list empty, ignoring connect request!";
135 qWarning() << "Invalid identity configures, ignoring connect request!";
139 // TODO implement cycling / random servers
140 QString host = serverList[0]["Address"].toString();
141 quint16 port = serverList[0]["Port"].toUInt();
142 displayStatusMsg(QString("Connecting to %1:%2...").arg(host).arg(port));
143 socket.connectToHost(host, port);
146 void NetworkConnection::sendPerform() {
147 // TODO: reimplement perform List!
148 //// send performlist
149 //QStringList performList = networkSettings["Perform"].toString().split( "\n" );
150 //int count = performList.count();
151 //for(int a = 0; a < count; a++) {
152 // if(!performList[a].isEmpty() ) {
153 // userInput(network, "", performList[a]);
157 // rejoin channels we've been in
158 QStringList chans = _previousState.toStringList();
159 if(chans.count() > 0) {
160 qDebug() << "autojoining" << chans;
161 QString list = chans.join(",");
162 putCmd("join", QStringList(list));
164 // delete _previousState, we won't need it again
165 _previousState = QVariant();
168 QVariant NetworkConnection::state() const {
169 IrcUser *me = network()->ircUser(network()->myNick());
170 if(!me) return QVariant(); // this shouldn't really happen, I guess
171 return me->channels();
174 void NetworkConnection::disconnectFromIrc() {
175 socket.disconnectFromHost();
178 void NetworkConnection::socketHasData() {
179 while(socket.canReadLine()) {
180 QByteArray s = socket.readLine().trimmed();
181 ircServerHandler()->handleServerMsg(s);
185 void NetworkConnection::socketError( QAbstractSocket::SocketError err ) {
187 qDebug() << "Socket Error!";
190 void NetworkConnection::socketConnected() {
191 emit connected(networkId());
192 Identity *identity = coreSession()->identity(network()->identity());
194 qWarning() << "Identity invalid!";
198 putRawLine(QString("NICK :%1").arg(identity->nicks()[0])); // FIXME: try more nicks if error occurs
199 putRawLine(QString("USER %1 8 * :%2").arg(identity->ident(), identity->realName()));
202 void NetworkConnection::socketStateChanged(QAbstractSocket::SocketState state) {
204 //qDebug() << "Socket state changed: " << state;
207 // FIXME switch to BufferId
208 void NetworkConnection::userInput(QString buf, QString msg) {
209 userInputHandler()->handleUserInput(buf, msg);
212 void NetworkConnection::putRawLine(QString s) {
214 socket.write(s.toAscii());
217 void NetworkConnection::putCmd(QString cmd, QStringList params, QString prefix) {
219 if(!prefix.isEmpty())
220 msg += ":" + prefix + " ";
221 msg += cmd.toUpper();
223 for(int i = 0; i < params.size() - 1; i++) {
224 msg += " " + params[i];
226 if(!params.isEmpty())
227 msg += " :" + params.last();
232 /* Exception classes for message handling */
233 NetworkConnection::ParseError::ParseError(QString cmd, QString prefix, QStringList params) {
235 _msg = QString("Command Parse Error: ") + cmd + params.join(" ");
238 NetworkConnection::UnknownCmdError::UnknownCmdError(QString cmd, QString prefix, QStringList params) {
240 _msg = QString("Unknown Command: ") + cmd + params.join(" ");