1 /***************************************************************************
2 * Copyright (C) 2005/06 by The Quassel Team *
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) any later version. *
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 ***************************************************************************/
26 #include <QMetaObject>
29 Server::Server(QString net) : network(net) {
38 connect(&socket, SIGNAL(connected()), this, SLOT(socketConnected()));
39 connect(&socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
40 connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
41 connect(&socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
42 connect(&socket, SIGNAL(readyRead()), this, SLOT(socketHasData()));
47 void Server::connectToIrc(QString net) {
48 if(net != network) return; // not me!
49 networkSettings = global->getData("Networks").toMap()[net].toMap();
50 identity = global->getData("Identities").toMap()[networkSettings["Identity"].toString()].toMap();
51 QList<QVariant> servers = networkSettings["Servers"].toList();
52 QString host = servers[0].toMap()["Address"].toString();
53 quint16 port = servers[0].toMap()["Port"].toUInt();
54 displayStatusMsg(QString("Connecting to %1:%2...").arg(host).arg(port));
55 socket.connectToHost(host, port);
58 void Server::disconnectFromIrc(QString net) {
59 if(net != network) return; // not me!
60 socket.disconnectFromHost();
63 void Server::socketHasData() {
64 while(socket.canReadLine()) {
65 QString s = socket.readLine().trimmed();
66 qDebug() << "Read: " << s;
67 emit recvRawServerMsg(s);
68 //Message *msg = Message::createFromServerString(this, s);
73 void Server::socketError( QAbstractSocket::SocketError err ) {
74 //qDebug() << "Socket Error!";
78 void Server::socketConnected( ) {
79 putRawLine(QString("NICK :%1").arg(identity["NickList"].toStringList()[0]));
80 putRawLine(QString("USER %1 8 * :%2").arg(identity["Ident"].toString()).arg(identity["RealName"].toString()));
83 void Server::socketDisconnected( ) {
84 //qDebug() << "Socket disconnected!";
88 void Server::socketStateChanged(QAbstractSocket::SocketState state) {
89 //qDebug() << "Socket state changed: " << state;
92 QString Server::nickFromMask(QString mask) {
93 return mask.section('!', 0, 0);
96 QString Server::userFromMask(QString mask) {
97 QString userhost = mask.section('!', 1);
98 if(userhost.isEmpty()) return QString();
99 return userhost.section('@', 0, 0);
102 QString Server::hostFromMask(QString mask) {
103 QString userhost = mask.section('!', 1);
104 if(userhost.isEmpty()) return QString();
105 return userhost.section('@', 1);
108 void Server::userInput(QString net, QString buf, QString msg) {
109 if(net != network) return; // not me!
110 msg = msg.trimmed(); // remove whitespace from start and end
111 if(msg.isEmpty()) return;
112 if(!msg.startsWith('/')) {
113 msg = QString("/SAY ") + msg;
115 handleUserMsg(buf, msg);
118 void Server::putRawLine(QString s) {
119 qDebug() << "SentRaw: " << s;
121 socket.write(s.toAscii());
124 void Server::putCmd(QString cmd, QStringList params, QString prefix) {
126 if(!prefix.isEmpty()) m += ":" + prefix + " ";
128 for(int i = 0; i < params.size() - 1; i++) {
129 m += " " + params[i];
131 if(!params.isEmpty()) m += " :" + params.last();
132 qDebug() << "SentCmd: " << m;
134 socket.write(m.toAscii());
137 /** Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
138 void Server::handleServerMsg(QString msg) {
141 qWarning() << "Received empty string from server!";
144 // OK, first we split the raw message into its various parts...
150 prefix = msg.section(' ', 0, 0);
151 msg = msg.section(' ', 1);
153 cmd = msg.section(' ', 0, 0).toUpper();
154 msg = msg.section(' ', 1);
155 QString left = msg.section(':', 0, 0);
156 QString trailing = msg.section(':', 1);
157 if(!left.isEmpty()) {
158 params << left.split(' ', QString::SkipEmptyParts);
160 if(!trailing.isEmpty()) {
163 // numeric replies usually have our own nick as first param. Remove this!
164 // BTW, this behavior is not in the RFC.
165 uint num = cmd.toUInt();
166 if(num > 1 && params.count() > 0) { // 001 sets our nick, so we shouldn't remove anything
167 if(params[0] == currentNick) params.removeFirst();
168 else qWarning((QString("First param NOT nick: %1:%2 %3").arg(prefix).arg(cmd).arg(params.join(" "))).toAscii());
170 // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
171 QString hname = cmd.toLower();
172 hname[0] = hname[0].toUpper();
173 hname = "handleServer" + hname;
174 if(!QMetaObject::invokeMethod(this, hname.toAscii(), Q_ARG(QString, prefix), Q_ARG(QStringList, params))) {
175 // Ok. Default handler it is.
176 defaultServerHandler(cmd, prefix, params);
178 } catch(Exception e) {
179 emit displayMsg("", Message(e.msg(), "", Message::Error));
183 void Server::defaultServerHandler(QString cmd, QString prefix, QStringList params) {
184 uint num = cmd.toUInt();
186 // A lot of server messages don't really need their own handler because they don't do much.
187 // Catch and handle these here.
189 // Welcome, status, info messages. Just display these.
190 case 2: case 3: case 4: case 5: case 251: case 252: case 253: case 254: case 255: case 372: case 375:
191 emit displayMsg("", Message(params.join(" "), prefix, Message::Server));
193 // Ignore these commands.
197 // Everything else will be marked in red, so we can add them somewhere.
199 emit displayMsg("", Message(cmd + " " + params.join(" "), prefix, Message::Error));
201 //qDebug() << prefix <<":"<<cmd<<params;
203 emit displayMsg("", Message(QString("Unknown: ") + cmd + " " + params.join(" "), prefix, Message::Error));
204 //qDebug() << prefix <<":"<<cmd<<params;
208 void Server::handleUserMsg(QString bufname, QString usrMsg) {
211 if(!bufname.isEmpty()) {
212 Q_ASSERT(buffers.contains(bufname));
213 buffer = buffers[bufname];
215 QString cmd = usrMsg.section(' ', 0, 0).remove(0, 1).toUpper();
216 QString msg = usrMsg.section(' ', 1).trimmed();
217 QString hname = cmd.toLower();
218 hname[0] = hname[0].toUpper();
219 hname = "handleUser" + hname;
220 if(!QMetaObject::invokeMethod(this, hname.toAscii(), Q_ARG(QString, msg), Q_ARG(Buffer*, buffer))) {
221 // Ok. Default handler it is.
222 defaultUserHandler(cmd, msg, buffer);
224 } catch(Exception e) {
225 emit displayMsg("", Message(e.msg(), "", Message::Error));
229 void Server::defaultUserHandler(QString cmd, QString msg, Buffer *buf) {
230 emit displayMsg("", Message(QString("Error: %1 %2").arg(cmd).arg(msg), "", Message::Error));
234 /**********************************************************************************/
237 void Server::handleUser(QString msg, Buffer *buf) {
243 void Server::handleUserJoin(QString msg, Buffer *buf) {
244 putCmd("JOIN", QStringList(msg));
248 void Server::handleUserQuote(QString msg, Buffer *buf) {
252 void Server::handleUserSay(QString msg, Buffer *buf) {
253 if(!buf) return; // server buffer
255 params << buf->name() << msg;
256 putCmd("PRIVMSG", params);
257 emit displayMsg(params[0], Message(msg, currentNick, Message::Msg, Message::Self));
260 /**********************************************************************************/
262 void Server::handleServerJoin(QString prefix, QStringList params) {
263 Q_ASSERT(params.count() == 1);
264 QString nick = nickFromMask(prefix);
265 if(nick == currentNick) {
266 Q_ASSERT(!buffers.contains(params[0])); // cannot join a buffer twice!
267 Buffer *buf = new Buffer(params[0]);
268 buffers[params[0]] = buf;
271 if(nicks.contains(nick)) {
272 n = nicks[nick].toMap();
273 VarMap chans = n["Channels"].toMap();
274 // Q_ASSERT(!chans.keys().contains(params[0])); TODO uncomment
275 chans[params[0]] = VarMap();
276 n["Channels"] = chans;
278 emit nickUpdated(network, nick, n);
281 chans[params[0]] = VarMap();
282 n["Channels"] = chans;
284 n["User"] = userFromMask(prefix);
285 n["Host"] = hostFromMask(prefix);
287 emit nickAdded(network, nick, n);
289 QString user = n["User"].toString(); QString host = n["Host"].toString();
290 if(user.isEmpty() || host.isEmpty()) emit displayMsg(params[0], Message(tr("%1 has joined %2").arg(nick).arg(params[0]), "", Message::Join));
291 else emit displayMsg(params[0], Message(tr("%1 (%2@%3) has joined %4").arg(nick).arg(user).arg(host).arg(params[0]), "", Message::Join));
297 void Server::handleServerNotice(QString prefix, QStringList params) {
298 Message msg(params[1], prefix, Message::Notice);
299 if(prefix == currentServer) emit displayMsg("", Message(params[1], prefix, Message::Server));
300 else emit displayMsg("", Message(params[1], prefix, Message::Notice));
303 void Server::handleServerPing(QString prefix, QStringList params) {
304 putCmd("PONG", params);
307 void Server::handleServerPrivmsg(QString prefix, QStringList params) {
308 emit displayMsg(params[0], Message(params[1], nickFromMask(prefix), Message::Msg));
313 void Server::handleServer001(QString prefix, QStringList params) {
314 currentServer = prefix;
315 currentNick = params[0];
316 emit ownNickSet(network, currentNick);
317 emit displayMsg("", Message(params[1], prefix, Message::Server));
321 void Server::handleServer331(QString prefix, QStringList params) {
322 emit topicSet(network, params[0], "");
326 void Server::handleServer332(QString prefix, QStringList params) {
327 emit topicSet(network, params[0], params[1]);
328 emit displayMsg(params[0], Message(tr("Topic for %1 is \"%2\"").arg(params[0]).arg(params[1]), "", Message::Server));
331 /* Topic set by... */
332 void Server::handleServer333(QString prefix, QStringList params) {
333 emit displayMsg(params[0], Message(tr("Topic set by %1 on %2").arg(params[1]).arg(QDateTime::fromTime_t(params[2].toUInt()).toString()), "", Message::Server));
337 void Server::handleServer353(QString prefix, QStringList params) {
338 params.removeFirst(); // = or *
339 QString buf = params.takeFirst();
340 foreach(QString nick, params[0].split(' ')) {
341 // TODO: parse more prefix characters! use 005?
343 if(nick.startsWith('@')) { mode = "o"; nick.remove(0,1); }
344 else if(nick.startsWith('+')) { mode = "v"; nick.remove(0,1); }
345 VarMap c; c["Mode"] = mode;
346 if(nicks.contains(nick)) {
347 VarMap n = nicks[nick].toMap();
348 VarMap chans = n["Channels"].toMap();
350 n["Channels"] = chans;
352 emit nickUpdated(network, nick, n);
354 VarMap n; VarMap c; VarMap chans;
357 n["Channels"] = chans;
360 emit nickAdded(network, nick, n);
364 /***********************************************************************************/
366 /* Exception classes for message handling */
367 Server::ParseError::ParseError(QString cmd, QString prefix, QStringList params) {
368 _msg = QString("Command Parse Error: ") + cmd + params.join(" ");
372 Server::UnknownCmdError::UnknownCmdError(QString cmd, QString prefix, QStringList params) {
373 _msg = QString("Unknown Command: ") + cmd;