3af2119d1013803e15abfa93158f07ed569582ac
[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 #include "synchronizer.h"
32
33 #include "ircserverhandler.h"
34 #include "userinputhandler.h"
35 #include "ctcphandler.h"
36
37 Server::Server(UserId uid, uint networkId, QString net)
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, coreSession()->signalProxy(), this))
44 {
45   networkInfo()->setNetworkName(net);
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(socketDisconnected()));
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
61   exec();
62 }
63
64 void Server::connectToIrc(QString net) {
65   if(net != networkName())
66     return; // not me!
67   
68   CoreSession *sess = coreSession();
69   networkSettings = sess->retrieveSessionData("Networks").toMap()[net].toMap();
70   identity = sess->retrieveSessionData("Identities").toMap()[networkSettings["Identity"].toString()].toMap();
71
72   //FIXME this will result in a pretty fuckup if there are no servers in the list
73   QList<QVariant> servers = networkSettings["Servers"].toList();
74   QString host = servers[0].toMap()["Address"].toString();
75   quint16 port = servers[0].toMap()["Port"].toUInt();
76   displayStatusMsg(QString("Connecting to %1:%2...").arg(host).arg(port));
77   socket.connectToHost(host, port);
78 }
79
80 void Server::disconnectFromIrc(QString net) {
81   if(net != networkName())
82     return; // not me!
83   socket.disconnectFromHost();
84 }
85
86 void Server::socketHasData() {
87   while(socket.canReadLine()) {
88     QByteArray s = socket.readLine().trimmed();
89     ircServerHandler()->handleServerMsg(s);
90   }
91 }
92
93 void Server::socketError( QAbstractSocket::SocketError err ) {
94   //qDebug() << "Socket Error!";
95 }
96
97 void Server::socketConnected( ) {
98   emit connected(networkId());
99   putRawLine(QString("NICK :%1").arg(identity["NickList"].toStringList()[0]));  // FIXME: try more nicks if error occurs
100   putRawLine(QString("USER %1 8 * :%2").arg(identity["Ident"].toString()).arg(identity["RealName"].toString()));
101 }
102
103 void Server::socketDisconnected( ) {
104   emit disconnected(networkId());
105   // propagate to networkInfo, so we can clear up
106 }
107
108 void Server::socketStateChanged(QAbstractSocket::SocketState state) {
109   //qDebug() << "Socket state changed: " << state;
110 }
111
112 void Server::userInput(uint netid, QString buf, QString msg) {
113   if(netid != networkId())
114     return; // not me!
115   userInputHandler()->handleUserInput(buf, msg);
116 }
117
118 void Server::putRawLine(QString s) {
119   s += "\r\n";
120   socket.write(s.toAscii());
121 }
122
123 void Server::putCmd(QString cmd, QStringList params, QString prefix) {
124   QString msg;
125   if(!prefix.isEmpty())
126     msg += ":" + prefix + " ";
127   msg += cmd.toUpper();
128   
129   for(int i = 0; i < params.size() - 1; i++) {
130     msg += " " + params[i];
131   }
132   if(!params.isEmpty())
133     msg += " :" + params.last();
134
135   putRawLine(msg);
136 }
137
138
139 uint Server::networkId() const {
140   return _networkId;
141 }
142
143 QString Server::networkName() {
144   return networkInfo()->networkName();
145 }
146
147 CoreSession *Server::coreSession() const {
148   return Core::session(userId());
149 }
150
151 /* Exception classes for message handling */
152 Server::ParseError::ParseError(QString cmd, QString prefix, QStringList params) {
153   _msg = QString("Command Parse Error: ") + cmd + params.join(" ");
154 }
155
156 Server::UnknownCmdError::UnknownCmdError(QString cmd, QString prefix, QStringList params) {
157   _msg = QString("Unknown Command: ") + cmd;
158 }