- implemented on request a chat monitor: a simple buffer which shows
[quassel.git] / src / core / networkconnection.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
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  *   (at your option) version 3.                                           *
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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include "networkconnection.h"
21
22 #include <QMetaObject>
23 #include <QMetaMethod>
24 #include <QDateTime>
25
26 #include "util.h"
27 #include "core.h"
28 #include "coresession.h"
29
30 #include "ircuser.h"
31 #include "network.h"
32 #include "identity.h"
33
34 #include "ircserverhandler.h"
35 #include "userinputhandler.h"
36 #include "ctcphandler.h"
37
38 NetworkConnection::NetworkConnection(Network *network, CoreSession *session, const QVariant &state) : QObject(network),
39     _network(network),
40     _coreSession(session),
41     _ircServerHandler(new IrcServerHandler(this)),
42     _userInputHandler(new UserInputHandler(this)),
43     _ctcpHandler(new CtcpHandler(this)),
44     _previousState(state)
45 {
46   connect(network, SIGNAL(currentServerSet(const QString &)), this, SLOT(sendPerform()));
47
48   connect(&socket, SIGNAL(connected()), this, SLOT(socketConnected()));
49   connect(&socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
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()));
53
54 }
55
56 NetworkConnection::~NetworkConnection() {
57   delete _ircServerHandler;
58   delete _userInputHandler;
59   delete _ctcpHandler;
60 }
61
62 bool NetworkConnection::isConnected() const {
63   return socket.state() == QAbstractSocket::ConnectedState;
64 }
65
66 NetworkId NetworkConnection::networkId() const {
67   return network()->networkId();
68 }
69
70 QString NetworkConnection::networkName() const {
71   return network()->networkName();
72 }
73
74 Network *NetworkConnection::network() const {
75   return _network;
76 }
77
78 CoreSession *NetworkConnection::coreSession() const {
79   return _coreSession;
80 }
81
82 IrcServerHandler *NetworkConnection::ircServerHandler() const {
83   return _ircServerHandler;
84 }
85
86 UserInputHandler *NetworkConnection::userInputHandler() const {
87   return _userInputHandler;
88 }
89
90 CtcpHandler *NetworkConnection::ctcpHandler() const {
91   return _ctcpHandler;
92 }
93
94 QString NetworkConnection::serverDecode(const QByteArray &string) const {
95   return network()->decodeString(string);
96 }
97
98 QString NetworkConnection::bufferDecode(const QString &bufferName, const QByteArray &string) const {
99   Q_UNUSED(bufferName);
100   // TODO: Implement buffer-specific encodings
101   return network()->decodeString(string);
102 }
103
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);
108 }
109
110 QByteArray NetworkConnection::serverEncode(const QString &string) const {
111   return network()->encodeString(string);
112 }
113
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);
118 }
119
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);
124 }
125
126
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!";
132     return;
133   }
134   if(!identity) {
135     qWarning() << "Invalid identity configures, ignoring connect request!";
136     return;
137   }
138
139   // TODO implement cycling / random servers
140   QString host = serverList[0]["Host"].toString();
141   quint16 port = serverList[0]["Port"].toUInt();
142   displayStatusMsg(QString("Connecting to %1:%2...").arg(host).arg(port));
143   socket.connectToHost(host, port);
144 }
145
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]);
154   //  }
155   //}
156
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));
163   }
164   // delete _previousState, we won't need it again
165   _previousState = QVariant();
166 }
167
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();
172 }
173
174 void NetworkConnection::disconnectFromIrc() {
175   socket.disconnectFromHost();
176 }
177
178 void NetworkConnection::socketHasData() {
179   while(socket.canReadLine()) {
180     QByteArray s = socket.readLine().trimmed();
181     ircServerHandler()->handleServerMsg(s);
182   }
183 }
184
185 void NetworkConnection::socketError( QAbstractSocket::SocketError err ) {
186   Q_UNUSED(err);
187   qDebug() << "Socket Error!";
188 }
189
190 void NetworkConnection::socketConnected() {
191   emit connected(networkId());
192   Identity *identity = coreSession()->identity(network()->identity());
193   if(!identity) {
194     qWarning() << "Identity invalid!";
195     disconnectFromIrc();
196     return;
197   }
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()));
200 }
201
202 void NetworkConnection::socketStateChanged(QAbstractSocket::SocketState state) {
203   Q_UNUSED(state);
204   //qDebug() << "Socket state changed: " << state;
205 }
206
207 void NetworkConnection::socketDisconnected() {
208   emit disconnected(networkId());
209 }
210
211 // FIXME switch to BufferId
212 void NetworkConnection::userInput(QString buf, QString msg) {
213   userInputHandler()->handleUserInput(buf, msg);
214 }
215
216 void NetworkConnection::putRawLine(QString s) {
217   s += "\r\n";
218   socket.write(s.toAscii());
219 }
220
221 void NetworkConnection::putCmd(QString cmd, QStringList params, QString prefix) {
222   QString msg;
223   if(!prefix.isEmpty())
224     msg += ":" + prefix + " ";
225   msg += cmd.toUpper();
226   
227   for(int i = 0; i < params.size() - 1; i++) {
228     msg += " " + params[i];
229   }
230   if(!params.isEmpty())
231     msg += " :" + params.last();
232
233   putRawLine(msg);
234 }
235
236 /* Exception classes for message handling */
237 NetworkConnection::ParseError::ParseError(QString cmd, QString prefix, QStringList params) {
238   Q_UNUSED(prefix);
239   _msg = QString("Command Parse Error: ") + cmd + params.join(" ");
240 }
241
242 NetworkConnection::UnknownCmdError::UnknownCmdError(QString cmd, QString prefix, QStringList params) {
243   Q_UNUSED(prefix);
244   _msg = QString("Unknown Command: ") + cmd + params.join(" ");
245 }