Finishing the switch to types.h and the resulting cleanup.
[quassel.git] / src / core / server.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel 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) any later version.                                   *
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 "networkinfo.h"
31
32 #include "ircserverhandler.h"
33 #include "userinputhandler.h"
34 #include "ctcphandler.h"
35
36 Server::Server(UserId uid, uint networkId, QString net)
37   : _userId(uid),
38     _networkId(networkId),
39     _ircServerHandler(new IrcServerHandler(this)),
40     _userInputHandler(new UserInputHandler(this)),
41     _ctcpHandler(new CtcpHandler(this)),
42     _networkInfo(new NetworkInfo(networkId, this))
43 {
44   networkInfo()->setNetworkName(net);
45   networkInfo()->setProxy(coreSession()->signalProxy());
46 }
47
48 Server::~Server() {
49   delete _ircServerHandler;
50   delete _userInputHandler;
51   delete _ctcpHandler;
52 }
53
54 void Server::run() {
55   connect(&socket, SIGNAL(connected()), this, SLOT(socketConnected()));
56   connect(&socket, SIGNAL(disconnected()), this, SLOT(quit()));
57   connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
58   connect(&socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
59   connect(&socket, SIGNAL(readyRead()), this, SLOT(socketHasData()));
60   connect(this, SIGNAL(finished()), this, SLOT(threadFinished()));
61
62   exec();
63 }
64
65 void Server::connectToIrc(QString net) {
66   if(net != networkName())
67     return; // not me!
68   
69   CoreSession *sess = coreSession();
70   networkSettings = sess->retrieveSessionData("Networks").toMap()[net].toMap();
71   identity = sess->retrieveSessionData("Identities").toMap()[networkSettings["Identity"].toString()].toMap();
72
73   //FIXME this will result in a pretty fuckup if there are no servers in the list
74   QList<QVariant> servers = networkSettings["Servers"].toList();
75   QString host = servers[0].toMap()["Address"].toString();
76   quint16 port = servers[0].toMap()["Port"].toUInt();
77   displayStatusMsg(QString("Connecting to %1:%2...").arg(host).arg(port));
78   socket.connectToHost(host, port);
79 }
80
81 void Server::disconnectFromIrc(QString net) {
82   if(net != networkName())
83     return; // not me!
84   socket.disconnectFromHost();
85 }
86
87 void Server::socketHasData() {
88   while(socket.canReadLine()) {
89     QByteArray s = socket.readLine().trimmed();
90     ircServerHandler()->handleServerMsg(s);
91   }
92 }
93
94 void Server::socketError( QAbstractSocket::SocketError err ) {
95   //qDebug() << "Socket Error!";
96 }
97
98 void Server::socketConnected() {
99   emit connected(networkId());
100   putRawLine(QString("NICK :%1").arg(identity["NickList"].toStringList()[0]));  // FIXME: try more nicks if error occurs
101   putRawLine(QString("USER %1 8 * :%2").arg(identity["Ident"].toString()).arg(identity["RealName"].toString()));
102 }
103
104 void Server::threadFinished() {
105   // the Socket::disconnected() is connect to this::quit()
106   // so after the event loop is finished we're beeing called
107   // and propagate the disconnect
108   emit disconnected(networkId());
109 }
110
111 void Server::socketStateChanged(QAbstractSocket::SocketState state) {
112   //qDebug() << "Socket state changed: " << state;
113 }
114
115 void Server::userInput(uint netid, QString buf, QString msg) {
116   if(netid != networkId())
117     return; // not me!
118   userInputHandler()->handleUserInput(buf, msg);
119 }
120
121 void Server::putRawLine(QString s) {
122   s += "\r\n";
123   socket.write(s.toAscii());
124 }
125
126 void Server::putCmd(QString cmd, QStringList params, QString prefix) {
127   QString msg;
128   if(!prefix.isEmpty())
129     msg += ":" + prefix + " ";
130   msg += cmd.toUpper();
131   
132   for(int i = 0; i < params.size() - 1; i++) {
133     msg += " " + params[i];
134   }
135   if(!params.isEmpty())
136     msg += " :" + params.last();
137
138   putRawLine(msg);
139 }
140
141
142 uint Server::networkId() const {
143   return _networkId;
144 }
145
146 QString Server::networkName() {
147   return networkInfo()->networkName();
148 }
149
150 CoreSession *Server::coreSession() const {
151   return Core::session(userId());
152 }
153
154 /* Exception classes for message handling */
155 Server::ParseError::ParseError(QString cmd, QString prefix, QStringList params) {
156   _msg = QString("Command Parse Error: ") + cmd + params.join(" ");
157 }
158
159 Server::UnknownCmdError::UnknownCmdError(QString cmd, QString prefix, QStringList params) {
160   _msg = QString("Unknown Command: ") + cmd;
161 }