My X-Mas present to you: partially working encodings! \o/
[quassel.git] / src / core / server.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
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 "server.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 "networkinfo.h"
32
33 #include "ircserverhandler.h"
34 #include "userinputhandler.h"
35 #include "ctcphandler.h"
36
37 Server::Server(UserId uid, NetworkId networkId, QString net, const QVariant &state)
38   : _userId(uid),
39     _networkId(networkId),
40     _ircServerHandler(new IrcServerHandler(this)),
41     _userInputHandler(new UserInputHandler(this)),
42     _ctcpHandler(new CtcpHandler(this)),
43     _networkInfo(new NetworkInfo(networkId, this)),
44     _previousState(state)
45 {
46   connect(networkInfo(), SIGNAL(currentServerSet(const QString &)), this, SLOT(sendPerform()));
47   networkInfo()->setCodecForEncoding("ISO-8859-15"); // FIXME
48   networkInfo()->setCodecForDecoding("ISO-8859-15"); // FIXME
49   networkInfo()->setNetworkName(net);
50   networkInfo()->setProxy(coreSession()->signalProxy());
51 }
52
53 Server::~Server() {
54   delete _ircServerHandler;
55   delete _userInputHandler;
56   delete _ctcpHandler;
57 }
58
59 void Server::run() {
60   connect(&socket, SIGNAL(connected()), this, SLOT(socketConnected()));
61   connect(&socket, SIGNAL(disconnected()), this, SLOT(quit()));
62   connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
63   connect(&socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
64   connect(&socket, SIGNAL(readyRead()), this, SLOT(socketHasData()));
65   connect(this, SIGNAL(finished()), this, SLOT(threadFinished()));
66
67   exec();
68 }
69
70 QString Server::serverDecode(const QByteArray &string) const {
71   return networkInfo()->decodeString(string);
72 }
73
74 QString Server::bufferDecode(const QString &bufferName, const QByteArray &string) const {
75   Q_UNUSED(bufferName);
76   // TODO: Implement buffer-specific encodings
77   return networkInfo()->decodeString(string);
78 }
79
80 QString Server::userDecode(const QString &userNick, const QByteArray &string) const {
81   IrcUser *user = networkInfo()->ircUser(userNick);
82   if(user) return user->decodeString(string);
83   return networkInfo()->decodeString(string);
84 }
85
86 QByteArray Server::serverEncode(const QString &string) const {
87   return networkInfo()->encodeString(string);
88 }
89
90 QByteArray Server::bufferEncode(const QString &bufferName, const QString &string) const {
91   Q_UNUSED(bufferName);
92   // TODO: Implement buffer-specific encodings
93   return networkInfo()->encodeString(string);
94 }
95
96 QByteArray Server::userEncode(const QString &userNick, const QString &string) const {
97   IrcUser *user = networkInfo()->ircUser(userNick);
98   if(user) return user->encodeString(string);
99   return networkInfo()->encodeString(string);
100 }
101
102
103 void Server::connectToIrc(QString net) {
104   if(net != networkName())
105     return; // not me!
106   
107   CoreSession *sess = coreSession();
108   networkSettings = sess->retrieveSessionData("Networks").toMap()[net].toMap();
109   identity = sess->retrieveSessionData("Identities").toMap()[networkSettings["Identity"].toString()].toMap();
110
111   //FIXME this will result in a pretty fuckup if there are no servers in the list
112   QList<QVariant> servers = networkSettings["Servers"].toList();
113   QString host = servers[0].toMap()["Address"].toString();
114   quint16 port = servers[0].toMap()["Port"].toUInt();
115   displayStatusMsg(QString("Connecting to %1:%2...").arg(host).arg(port));
116   socket.connectToHost(host, port);
117 }
118
119 void Server::sendPerform() {
120   // TODO: reimplement perform List!
121   //// send performlist
122   //QStringList performList = networkSettings["Perform"].toString().split( "\n" );
123   //int count = performList.count();
124   //for(int a = 0; a < count; a++) {
125   //  if(!performList[a].isEmpty() ) {
126   //    userInput(network, "", performList[a]);
127   //  }
128   //}
129
130   // rejoin channels we've been in
131   QStringList chans = _previousState.toStringList();
132   if(chans.count() > 0) {
133     qDebug() << "autojoining" << chans;
134     QString list = chans.join(",");
135     putCmd("join", QStringList(list));
136   }
137   // delete _previousState, we won't need it again
138   _previousState = QVariant();
139 }
140
141 QVariant Server::state() {
142   IrcUser *me = networkInfo()->ircUser(networkInfo()->myNick());
143   if(!me) return QVariant();  // this shouldn't really happen, I guess
144   return me->channels();
145 }
146
147 void Server::disconnectFromIrc(QString net) {
148   if(net != networkName())
149     return; // not me!
150   socket.disconnectFromHost();
151 }
152
153 void Server::socketHasData() {
154   while(socket.canReadLine()) {
155     QByteArray s = socket.readLine().trimmed();
156     ircServerHandler()->handleServerMsg(s);
157   }
158 }
159
160 void Server::socketError( QAbstractSocket::SocketError err ) {
161   //qDebug() << "Socket Error!";
162 }
163
164 void Server::socketConnected() {
165   emit connected(networkId());
166   putRawLine(QString("NICK :%1").arg(identity["NickList"].toStringList()[0]));  // FIXME: try more nicks if error occurs
167   putRawLine(QString("USER %1 8 * :%2").arg(identity["Ident"].toString()).arg(identity["RealName"].toString()));
168 }
169
170 void Server::threadFinished() {
171   // the Socket::disconnected() is connect to this::quit()
172   // so after the event loop is finished we're beeing called
173   // and propagate the disconnect
174   emit disconnected(networkId());
175 }
176
177 void Server::socketStateChanged(QAbstractSocket::SocketState state) {
178   //qDebug() << "Socket state changed: " << state;
179 }
180
181 void Server::userInput(uint netid, QString buf, QString msg) {
182   if(netid != networkId())
183     return; // not me!
184   userInputHandler()->handleUserInput(buf, msg);
185 }
186
187 void Server::putRawLine(QString s) {
188   s += "\r\n";
189   socket.write(s.toAscii());
190 }
191
192 void Server::putCmd(QString cmd, QStringList params, QString prefix) {
193   QString msg;
194   if(!prefix.isEmpty())
195     msg += ":" + prefix + " ";
196   msg += cmd.toUpper();
197   
198   for(int i = 0; i < params.size() - 1; i++) {
199     msg += " " + params[i];
200   }
201   if(!params.isEmpty())
202     msg += " :" + params.last();
203
204   putRawLine(msg);
205 }
206
207
208 uint Server::networkId() const {
209   return _networkId;
210 }
211
212 QString Server::networkName() const {
213   return networkInfo()->networkName();
214 }
215
216 CoreSession *Server::coreSession() const {
217   return Core::session(userId());
218 }
219
220 /* Exception classes for message handling */
221 Server::ParseError::ParseError(QString cmd, QString prefix, QStringList params) {
222   _msg = QString("Command Parse Error: ") + cmd + params.join(" ");
223 }
224
225 Server::UnknownCmdError::UnknownCmdError(QString cmd, QString prefix, QStringList params) {
226   _msg = QString("Unknown Command: ") + cmd;
227 }