1 /***************************************************************************
2 * Copyright (C) 2005-10 by the Quassel Project *
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) version 3. *
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 ***************************************************************************/
20 #include "ircserverhandler.h"
24 #include "coresession.h"
25 #include "coreirclisthelper.h"
26 #include "coreidentity.h"
27 #include "ctcphandler.h"
30 #include "coreircchannel.h"
39 IrcServerHandler::IrcServerHandler(CoreNetwork *parent)
40 : CoreBasicHandler(parent),
43 connect(parent, SIGNAL(disconnected(NetworkId)), this, SLOT(destroyNetsplits()));
46 IrcServerHandler::~IrcServerHandler() {
50 /*! Handle a raw message string sent by the server. We try to find a suitable handler, otherwise we call a default handler. */
51 void IrcServerHandler::handleServerMsg(QByteArray msg) {
53 qWarning() << "Received empty string from server!";
57 // Now we split the raw message into its various parts...
62 // First, check for a trailing parameter introduced by " :", since this might screw up splitting the msg
63 // NOTE: This assumes that this is true in raw encoding, but well, hopefully there are no servers running in japanese on protocol level...
64 int idx = msg.indexOf(" :");
66 if(msg.length() > idx + 2)
67 trailing = msg.mid(idx + 2);
70 // OK, now it is safe to split...
71 QList<QByteArray> params = msg.split(' ');
73 // This could still contain empty elements due to (faulty?) ircds sending multiple spaces in a row
74 // Also, QByteArray is not nearly as convenient to work with as QString for such things :)
75 QList<QByteArray>::iterator iter = params.begin();
76 while(iter != params.end()) {
78 iter = params.erase(iter);
83 if(!trailing.isEmpty()) params << trailing;
84 if(params.count() < 1) {
85 qWarning() << "Received invalid string from server!";
89 QString foo = serverDecode(params.takeFirst());
91 // with SASL, the command is 'AUTHENTICATE +' and we should check for this here.
92 /* obsolete because of events
93 if(foo == QString("AUTHENTICATE +")) {
98 // a colon as the first chars indicates the existence of a prefix
102 if(params.count() < 1) {
103 qWarning() << "Received invalid string from server!";
106 foo = serverDecode(params.takeFirst());
109 // next string without a whitespace is the command
110 cmd = foo.trimmed().toUpper();
112 // numeric replies have the target as first param (RFC 2812 - 2.4). this is usually our own nick. Remove this!
113 uint num = cmd.toUInt();
115 if(params.count() == 0) {
116 qWarning() << "Message received from server violates RFC and is ignored!" << msg;
119 _target = serverDecode(params.takeFirst());
124 // note that the IRC server is still alive
125 network()->resetPingTimeout();
127 // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
128 handle(cmd, Q_ARG(QString, prefix), Q_ARG(QList<QByteArray>, params));
132 void IrcServerHandler::defaultHandler(QString cmd, const QString &prefix, const QList<QByteArray> &rawparams) {
133 // many commands are handled by the event system now
139 //******************************/
140 // IRC SERVER HANDLER
141 //******************************/
143 void IrcServerHandler::handleJoin(const QString &prefix, const QList<QByteArray> ¶ms) {
144 if(!checkParamCount("IrcServerHandler::handleJoin()", params, 1))
147 QString channel = serverDecode(params[0]);
148 IrcUser *ircuser = network()->updateNickFromMask(prefix);
150 bool handledByNetsplit = false;
151 if(!_netsplits.empty()) {
152 foreach(Netsplit* n, _netsplits) {
153 handledByNetsplit = n->userJoined(prefix, channel);
154 if(handledByNetsplit)
160 if(!handledByNetsplit) {
161 emit displayMsg(Message::Join, BufferInfo::ChannelBuffer, channel, channel, prefix);
162 ircuser->joinChannel(channel);
164 //qDebug() << "IrcServerHandler::handleJoin()" << prefix << params;
166 if(network()->isMe(ircuser)) {
167 network()->setChannelJoined(channel);
168 putCmd("MODE", params[0]); // we want to know the modes of the channel we just joined, so we ask politely
172 void IrcServerHandler::handleMode(const QString &prefix, const QList<QByteArray> ¶ms) {
173 if(!checkParamCount("IrcServerHandler::handleMode()", params, 2))
176 if(network()->isChannelName(serverDecode(params[0]))) {
178 emit displayMsg(Message::Mode, BufferInfo::ChannelBuffer, serverDecode(params[0]), serverDecode(params).join(" "), prefix);
180 IrcChannel *channel = network()->ircChannel(params[0]);
182 // we received mode information for a channel we're not in. that means probably we've just been kicked out or something like that
183 // anyways: we don't have a place to store the data --> discard the info.
187 QString modes = params[1];
190 for(int c = 0; c < modes.length(); c++) {
191 if(modes[c] == '+') {
195 if(modes[c] == '-') {
200 if(network()->prefixModes().contains(modes[c])) {
201 // user channel modes (op, voice, etc...)
202 if(paramOffset < params.count()) {
203 IrcUser *ircUser = network()->ircUser(params[paramOffset]);
205 qWarning() << Q_FUNC_INFO << "Unknown IrcUser:" << params[paramOffset];
208 bool handledByNetsplit = false;
209 if(!_netsplits.empty()) {
210 foreach(Netsplit* n, _netsplits) {
211 handledByNetsplit = n->userAlreadyJoined(ircUser->hostmask(), channel->name());
212 if(handledByNetsplit) {
213 n->addMode(ircUser->hostmask(), channel->name(), QString(modes[c]));
218 if(!handledByNetsplit)
219 channel->addUserMode(ircUser, QString(modes[c]));
222 channel->removeUserMode(ircUser, QString(modes[c]));
225 qWarning() << "Received MODE with too few parameters:" << serverDecode(params);
229 // regular channel modes
231 Network::ChannelModeType modeType = network()->channelModeType(modes[c]);
232 if(modeType == Network::A_CHANMODE || modeType == Network::B_CHANMODE || (modeType == Network::C_CHANMODE && add)) {
233 if(paramOffset < params.count()) {
234 value = params[paramOffset];
236 qWarning() << "Received MODE with too few parameters:" << serverDecode(params);
242 channel->addChannelMode(modes[c], value);
244 channel->removeChannelMode(modes[c], value);
250 IrcUser *ircUser = network()->newIrcUser(params[0]);
251 QString modeString(serverDecode(params[1]));
255 for(int c = 0; c < modeString.count(); c++) {
256 if(modeString[c] == '+') {
260 if(modeString[c] == '-') {
265 addModes += modeString[c];
267 removeModes += modeString[c];
269 if(!addModes.isEmpty())
270 ircUser->addUserModes(addModes);
271 if(!removeModes.isEmpty())
272 ircUser->removeUserModes(removeModes);
274 if(network()->isMe(ircUser)) {
275 network()->updatePersistentModes(addModes, removeModes);
279 emit displayMsg(Message::Mode, BufferInfo::StatusBuffer, "", serverDecode(params).join(" "), prefix);
283 void IrcServerHandler::handleNotice(const QString &prefix, const QList<QByteArray> ¶ms) {
284 if(!checkParamCount("IrcServerHandler::handleNotice()", params, 2))
288 QStringList targets = serverDecode(params[0]).split(',', QString::SkipEmptyParts);
289 QStringList::const_iterator targetIter;
290 for(targetIter = targets.constBegin(); targetIter != targets.constEnd(); targetIter++) {
291 QString target = *targetIter;
293 // special treatment for welcome messages like:
294 // :ChanServ!ChanServ@services. NOTICE egst :[#apache] Welcome, this is #apache. Please read the in-channel topic message. This channel is being logged by IRSeekBot. If you have any question please see http://blog.freenode.net/?p=68
295 if(!network()->isChannelName(target)) {
296 QString msg = serverDecode(params[1]);
297 QRegExp welcomeRegExp("^\\[([^\\]]+)\\] ");
298 if(welcomeRegExp.indexIn(msg) != -1) {
299 QString channelname = welcomeRegExp.cap(1);
300 msg = msg.mid(welcomeRegExp.matchedLength());
301 CoreIrcChannel *chan = static_cast<CoreIrcChannel *>(network()->ircChannel(channelname)); // we only have CoreIrcChannels in the core, so this cast is safe
302 if(chan && !chan->receivedWelcomeMsg()) {
303 chan->setReceivedWelcomeMsg();
304 emit displayMsg(Message::Notice, BufferInfo::ChannelBuffer, channelname, msg, prefix);
310 if(prefix.isEmpty() || target == "AUTH") {
313 if(!target.isEmpty() && network()->prefixes().contains(target[0]))
314 target = target.mid(1);
315 if(!network()->isChannelName(target))
316 target = nickFromMask(prefix);
319 network()->ctcpHandler()->parse(Message::Notice, prefix, target, params[1]);
324 void IrcServerHandler::handlePing(const QString &prefix, const QList<QByteArray> ¶ms) {
326 putCmd("PONG", params);
329 void IrcServerHandler::handlePrivmsg(const QString &prefix, const QList<QByteArray> ¶ms) {
330 if(!checkParamCount("IrcServerHandler::handlePrivmsg()", params, 1))
333 IrcUser *ircuser = network()->updateNickFromMask(prefix);
335 qWarning() << "IrcServerHandler::handlePrivmsg(): Unknown IrcUser!";
339 if(params.isEmpty()) {
340 qWarning() << "IrcServerHandler::handlePrivmsg(): received PRIVMSG without target or message from:" << prefix;
344 QString senderNick = nickFromMask(prefix);
346 QByteArray msg = params.count() < 2
350 QStringList targets = serverDecode(params[0]).split(',', QString::SkipEmptyParts);
351 QStringList::const_iterator targetIter;
352 for(targetIter = targets.constBegin(); targetIter != targets.constEnd(); targetIter++) {
353 const QString &target = network()->isChannelName(*targetIter)
358 msg = decrypt(target, msg);
360 // it's possible to pack multiple privmsgs into one param using ctcp
361 // - > we let the ctcpHandler do the work
362 network()->ctcpHandler()->parse(Message::Plain, prefix, target, msg);
366 void IrcServerHandler::handleQuit(const QString &prefix, const QList<QByteArray> ¶ms) {
367 IrcUser *ircuser = network()->updateNickFromMask(prefix);
371 if(params.count() > 0)
372 msg = userDecode(ircuser->nick(), params[0]);
375 if(Netsplit::isNetsplit(msg)) {
377 if(!_netsplits.contains(msg)) {
379 connect(n, SIGNAL(finished()), this, SLOT(handleNetsplitFinished()));
380 connect(n, SIGNAL(netsplitJoin(const QString&, const QStringList&, const QStringList&, const QString&)),
381 this, SLOT(handleNetsplitJoin(const QString&, const QStringList&, const QStringList&, const QString&)));
382 connect(n, SIGNAL(netsplitQuit(const QString&, const QStringList&, const QString&)),
383 this, SLOT(handleNetsplitQuit(const QString&, const QStringList&, const QString&)));
384 connect(n, SIGNAL(earlyJoin(const QString&, const QStringList&, const QStringList&)),
385 this, SLOT(handleEarlyNetsplitJoin(const QString&, const QStringList&, const QStringList&)));
386 _netsplits.insert(msg, n);
391 // add this user to the netsplit
392 n->userQuit(prefix, ircuser->channels(),msg);
396 foreach(QString channel, ircuser->channels())
397 emit displayMsg(Message::Quit, BufferInfo::ChannelBuffer, channel, msg, prefix);
403 // TODO Complete 005 handling, also use sensible defaults for non-sent stuff
404 void IrcServerHandler::handle005(const QString &prefix, const QList<QByteArray> ¶ms) {
406 const int numParams = params.size();
408 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received RPL_ISUPPORT (005) without parameters!"), prefix);
412 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params).join(" "), prefix);
414 QString rpl_isupport_suffix = serverDecode(params.last());
415 if(!rpl_isupport_suffix.toLower().contains("are supported by this server")) {
416 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Received non RFC compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), prefix);
421 for(int i = 0; i < numParams - 1; i++) {
422 QString rawSupport = serverDecode(params[i]);
423 QString key = rawSupport.section("=", 0, 0);
424 QString value = rawSupport.section("=", 1);
425 network()->addSupport(key, value);
428 /* determine our prefixes here to get an accurate result */
429 network()->determinePrefixes();
434 Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
436 "<nick> :<away message>"
438 Replies 352 and 315 paired are used to answer a WHO message.
441 Replies 314 and 369 are responses to a WHOWAS message.
446 /* RPL_AWAY - "<nick> :<away message>" */
447 void IrcServerHandler::handle301(const QString &prefix, const QList<QByteArray> ¶ms) {
449 if(!checkParamCount("IrcServerHandler::handle301()", params, 2))
453 QString nickName = serverDecode(params[0]);
454 QString awayMessage = userDecode(nickName, params[1]);
456 IrcUser *ircuser = network()->ircUser(nickName);
458 ircuser->setAwayMessage(awayMessage);
459 ircuser->setAway(true);
462 // FIXME: proper redirection needed
464 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is away: \"%2\"").arg(nickName).arg(awayMessage));
467 int now = QDateTime::currentDateTime().toTime_t();
468 int silenceTime = 60;
469 if(ircuser->lastAwayMessage() + silenceTime < now) {
470 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, params[0], tr("%1 is away: \"%2\"").arg(nickName).arg(awayMessage));
472 ircuser->setLastAwayMessage(now);
474 // probably should not happen
475 emit displayMsg(Message::Server, BufferInfo::QueryBuffer, params[0], tr("%1 is away: \"%2\"").arg(nickName).arg(awayMessage));
481 // ":You are no longer marked as being away"
482 void IrcServerHandler::handle305(const QString &prefix, const QList<QByteArray> ¶ms) {
484 IrcUser *me = network()->me();
488 if(!network()->autoAwayActive()) {
489 if(!params.isEmpty())
490 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params[0]));
492 network()->setAutoAwayActive(false);
497 // ":You have been marked as being away"
498 void IrcServerHandler::handle306(const QString &prefix, const QList<QByteArray> ¶ms) {
500 IrcUser *me = network()->me();
504 if(!params.isEmpty() && !network()->autoAwayActive())
505 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", serverDecode(params[0]));
508 /* RPL_WHOISSERVICE - "<user> is registered nick" */
509 void IrcServerHandler::handle307(const QString &prefix, const QList<QByteArray> ¶ms) {
511 if(!checkParamCount("IrcServerHandler::handle307()", params, 1))
514 QString whoisServiceReply = serverDecode(params).join(" ");
515 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
517 ircuser->setWhoisServiceReply(whoisServiceReply);
519 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(whoisServiceReply));
522 /* RPL_SUSERHOST - "<user> is available for help." */
523 void IrcServerHandler::handle310(const QString &prefix, const QList<QByteArray> ¶ms) {
525 if(!checkParamCount("IrcServerHandler::handle310()", params, 1))
528 QString suserHost = serverDecode(params).join(" ");
529 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
531 ircuser->setSuserHost(suserHost);
533 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(suserHost));
536 /* RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
537 void IrcServerHandler::handle311(const QString &prefix, const QList<QByteArray> ¶ms) {
539 if(!checkParamCount("IrcServerHandler::handle311()", params, 3))
543 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
545 ircuser->setUser(serverDecode(params[1]));
546 ircuser->setHost(serverDecode(params[2]));
547 ircuser->setRealName(serverDecode(params.last()));
548 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is %2 (%3)") .arg(ircuser->nick()).arg(ircuser->hostmask()).arg(ircuser->realName()));
550 QString host = QString("%1!%2@%3").arg(serverDecode(params[0])).arg(serverDecode(params[1])).arg(serverDecode(params[2]));
551 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is %2 (%3)") .arg(serverDecode(params[0])).arg(host).arg(serverDecode(params.last())));
555 /* RPL_WHOISSERVER - "<nick> <server> :<server info>" */
556 void IrcServerHandler::handle312(const QString &prefix, const QList<QByteArray> ¶ms) {
558 if(!checkParamCount("IrcServerHandler::handle312()", params, 2))
561 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
563 ircuser->setServer(serverDecode(params[1]));
566 QString returnString = tr("%1 is online via %2 (%3)").arg(serverDecode(params[0])).arg(serverDecode(params[1])).arg(serverDecode(params.last()));
568 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(returnString));
570 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whowas] %1").arg(returnString));
574 /* RPL_WHOISOPERATOR - "<nick> :is an IRC operator" */
575 void IrcServerHandler::handle313(const QString &prefix, const QList<QByteArray> ¶ms) {
577 if(!checkParamCount("IrcServerHandler::handle313()", params, 1))
580 IrcUser *ircuser = network()->ircUser(serverDecode(params[0]));
582 ircuser->setIrcOperator(params.last());
584 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(serverDecode(params).join(" ")));
587 /* RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
588 void IrcServerHandler::handle314(const QString &prefix, const QList<QByteArray> ¶ms) {
590 if(!checkParamCount("IrcServerHandler::handle314()", params, 3))
593 QString nick = serverDecode(params[0]);
594 QString hostmask = QString("%1@%2").arg(serverDecode(params[1])).arg(serverDecode(params[2]));
595 QString realName = serverDecode(params.last());
596 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whowas] %1 was %2 (%3)").arg(nick).arg(hostmask).arg(realName));
599 /* RPL_ENDOFWHO: "<name> :End of WHO list" */
600 void IrcServerHandler::handle315(const QString &prefix, const QList<QByteArray> ¶ms) {
602 if(!checkParamCount("IrcServerHandler::handle315()", params, 1))
605 QStringList p = serverDecode(params);
606 if(network()->setAutoWhoDone(p[0])) {
607 return; // stay silent
609 p.takeLast(); // should be "End of WHO list"
610 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
613 /* RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
614 (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
615 void IrcServerHandler::handle317(const QString &prefix, const QList<QByteArray> ¶ms) {
617 if(!checkParamCount("IrcServerHandler::handle317()", params, 2))
620 QString nick = serverDecode(params[0]);
621 IrcUser *ircuser = network()->ircUser(nick);
623 QDateTime now = QDateTime::currentDateTime();
624 int idleSecs = serverDecode(params[1]).toInt();
628 ircuser->setIdleTime(now.addSecs(idleSecs));
629 if(params.size() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
630 int loginTime = serverDecode(params[2]).toInt();
631 ircuser->setLoginTime(QDateTime::fromTime_t(loginTime));
632 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is logged in since %2").arg(ircuser->nick()).arg(ircuser->loginTime().toString()));
634 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is idling for %2 (%3)").arg(ircuser->nick()).arg(secondsToString(ircuser->idleTime().secsTo(now))).arg(ircuser->idleTime().toString()));
636 QDateTime idleSince = now.addSecs(idleSecs);
637 if (params.size() > 3) { // we have a signon time
638 int loginTime = serverDecode(params[2]).toInt();
639 QDateTime datetime = QDateTime::fromTime_t(loginTime);
640 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is logged in since %2").arg(nick).arg(datetime.toString()));
642 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is idling for %2 (%3)").arg(nick).arg(secondsToString(idleSince.secsTo(now))).arg(idleSince.toString()));
646 /* RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
647 void IrcServerHandler::handle318(const QString &prefix, const QList<QByteArray> ¶ms) {
650 QStringList parameter = serverDecode(params);
651 parameter.removeFirst();
652 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(parameter.join(" ")));
655 /* RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
656 void IrcServerHandler::handle319(const QString &prefix, const QList<QByteArray> ¶ms) {
658 if(!checkParamCount("IrcServerHandler::handle319()", params, 2))
661 QString nick = serverDecode(params.first());
665 foreach (QString channel, serverDecode(params.last()).split(" ")) {
666 if(channel.startsWith("@"))
667 op.append(channel.remove(0,1));
668 else if(channel.startsWith("+"))
669 voice.append(channel.remove(0,1));
671 user.append(channel);
674 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is a user on channels: %2").arg(nick).arg(user.join(" ")));
676 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 has voice on channels: %2").arg(nick).arg(voice.join(" ")));
678 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is an operator on channels: %2").arg(nick).arg(op.join(" ")));
681 /* RPL_WHOISVIRT - "<nick> is identified to services" */
682 void IrcServerHandler::handle320(const QString &prefix, const QList<QByteArray> ¶ms) {
684 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1").arg(serverDecode(params).join(" ")));
687 /* RPL_LIST - "<channel> <# visible> :<topic>" */
688 void IrcServerHandler::handle322(const QString &prefix, const QList<QByteArray> ¶ms) {
691 quint32 userCount = 0;
694 int paramCount = params.count();
697 topic = serverDecode(params[2]);
699 userCount = serverDecode(params[1]).toUInt();
701 channelName = serverDecode(params[0]);
705 if(!coreSession()->ircListHelper()->addChannel(network()->networkId(), channelName, userCount, topic))
706 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("Channel %1 has %2 users. Topic is: %3").arg(channelName).arg(userCount).arg(topic));
709 /* RPL_LISTEND ":End of LIST" */
710 void IrcServerHandler::handle323(const QString &prefix, const QList<QByteArray> ¶ms) {
714 if(!coreSession()->ircListHelper()->endOfChannelList(network()->networkId()))
715 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("End of channel list"));
718 /* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
719 void IrcServerHandler::handle324(const QString &prefix, const QList<QByteArray> ¶ms) {
721 handleMode(prefix, params);
724 /* RPL_??? - "<channel> <homepage> */
725 void IrcServerHandler::handle328(const QString &prefix, const QList<QByteArray> ¶ms) {
727 if(!checkParamCount("IrcServerHandler::handle328()", params, 2))
730 QString channel = serverDecode(params[0]);
731 QString homepage = serverDecode(params[1]);
733 emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("Homepage for %1 is %2").arg(channel, homepage));
737 /* RPL_??? - "<channel> <creation time (unix)>" */
738 void IrcServerHandler::handle329(const QString &prefix, const QList<QByteArray> ¶ms) {
740 if(!checkParamCount("IrcServerHandler::handle329()", params, 2))
743 QString channel = serverDecode(params[0]);
744 uint unixtime = params[1].toUInt();
746 qWarning() << Q_FUNC_INFO << "received invalid timestamp:" << params[1];
749 QDateTime time = QDateTime::fromTime_t(unixtime);
751 emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel, tr("Channel %1 created on %2").arg(channel, time.toString()));
754 /* RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
755 void IrcServerHandler::handle330(const QString &prefix, const QList<QByteArray> ¶ms) {
757 if(!checkParamCount("IrcServerHandler::handle330()", params, 3))
760 QString nick = serverDecode(params[0]);
761 QString account = serverDecode(params[1]);
763 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whois] %1 is authed as %2").arg(nick).arg(account));
767 void IrcServerHandler::handle331(const QString &prefix, const QList<QByteArray> ¶ms) {
769 if(!checkParamCount("IrcServerHandler::handle331()", params, 1))
772 QString channel = serverDecode(params[0]);
773 IrcChannel *chan = network()->ircChannel(channel);
775 chan->setTopic(QString());
777 emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel, tr("No topic is set for %1.").arg(channel));
781 void IrcServerHandler::handle332(const QString &prefix, const QList<QByteArray> ¶ms) {
783 if(!checkParamCount("IrcServerHandler::handle332()", params, 2))
786 QString channel = serverDecode(params[0]);
787 QByteArray rawTopic = params[1];
789 rawTopic = decrypt(channel, rawTopic, true);
791 QString topic = channelDecode(channel, rawTopic);
793 IrcChannel *chan = network()->ircChannel(channel);
795 chan->setTopic(topic);
797 emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel, tr("Topic for %1 is \"%2\"").arg(channel, topic));
800 /* Topic set by... */
801 void IrcServerHandler::handle333(const QString &prefix, const QList<QByteArray> ¶ms) {
803 if(!checkParamCount("IrcServerHandler::handle333()", params, 3))
806 QString channel = serverDecode(params[0]);
807 emit displayMsg(Message::Topic, BufferInfo::ChannelBuffer, channel,
808 tr("Topic set by %1 on %2") .arg(serverDecode(params[1]), QDateTime::fromTime_t(channelDecode(channel, params[2]).toUInt()).toString()));
811 /* RPL_INVITING - "<nick> <channel>*/
812 void IrcServerHandler::handle341(const QString &prefix, const QList<QByteArray> ¶ms) {
814 if(!checkParamCount("IrcServerHandler::handle341()", params, 2))
817 QString nick = serverDecode(params[0]);
819 IrcChannel *channel = network()->ircChannel(serverDecode(params[1]));
821 qWarning() << "IrcServerHandler::handle341(): unknown channel:" << params[1];
825 emit displayMsg(Message::Server, BufferInfo::ChannelBuffer, channel->name(), tr("%1 has been invited to %2").arg(nick).arg(channel->name()));
828 /* RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
829 ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
830 void IrcServerHandler::handle352(const QString &prefix, const QList<QByteArray> ¶ms) {
832 if(!checkParamCount("IrcServerHandler::handle352()", params, 6))
835 QString channel = serverDecode(params[0]);
836 IrcUser *ircuser = network()->ircUser(serverDecode(params[4]));
838 ircuser->setUser(serverDecode(params[1]));
839 ircuser->setHost(serverDecode(params[2]));
841 bool away = serverDecode(params[5]).startsWith("G") ? true : false;
842 ircuser->setAway(away);
843 ircuser->setServer(serverDecode(params[3]));
844 ircuser->setRealName(serverDecode(params.last()).section(" ", 1));
847 if(!network()->isAutoWhoInProgress(channel)) {
848 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Who] %1").arg(serverDecode(params).join(" ")));
853 void IrcServerHandler::handle353(const QString &prefix, const QList<QByteArray> ¶ms) {
855 if(!checkParamCount("IrcServerHandler::handle353()", params, 3))
858 // param[0] is either "=", "*" or "@" indicating a public, private or secret channel
859 // we don't use this information at the time beeing
860 QString channelname = serverDecode(params[1]);
862 IrcChannel *channel = network()->ircChannel(channelname);
864 qWarning() << "IrcServerHandler::handle353(): received unknown target channel:" << channelname;
871 foreach(QString nick, serverDecode(params[2]).split(' ')) {
872 QString mode = QString();
874 if(network()->prefixes().contains(nick[0])) {
875 mode = network()->prefixToMode(nick[0]);
883 channel->joinIrcUsers(nicks, modes);
886 /* RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
887 void IrcServerHandler::handle369(const QString &prefix, const QList<QByteArray> ¶ms) {
889 emit displayMsg(Message::Server, BufferInfo::StatusBuffer, "", tr("[Whowas] %1").arg(serverDecode(params).join(" ")));
892 /* ERR_ERRONEUSNICKNAME */
893 void IrcServerHandler::handle432(const QString &prefix, const QList<QByteArray> ¶ms) {
897 if(params.size() < 2) {
898 // handle unreal-ircd bug, where unreal ircd doesnt supply a TARGET in ERR_ERRONEUSNICKNAME during registration phase:
900 // :irc.scortum.moep.net 432 @@@ :Erroneous Nickname: Illegal characters
901 // correct server reply:
902 // :irc.scortum.moep.net 432 * @@@ :Erroneous Nickname: Illegal characters
907 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick %1 contains illegal characters").arg(errnick));
908 tryNextNick(errnick, true /* erroneus */);
911 /* ERR_NICKNAMEINUSE */
912 void IrcServerHandler::handle433(const QString &prefix, const QList<QByteArray> ¶ms) {
914 if(!checkParamCount("IrcServerHandler::handle433()", params, 1))
917 QString errnick = serverDecode(params[0]);
918 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick already in use: %1").arg(errnick));
920 // if there is a problem while connecting to the server -> we handle it
921 // but only if our connection has not been finished yet...
922 if(!network()->currentServer().isEmpty())
925 tryNextNick(errnick);
928 /* ERR_UNAVAILRESOURCE */
929 void IrcServerHandler::handle437(const QString &prefix, const QList<QByteArray> ¶ms) {
931 if(!checkParamCount("IrcServerHandler::handle437()", params, 1))
934 QString errnick = serverDecode(params[0]);
935 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("Nick/channel is temporarily unavailable: %1").arg(errnick));
937 // if there is a problem while connecting to the server -> we handle it
938 // but only if our connection has not been finished yet...
939 if(!network()->currentServer().isEmpty())
942 if(!network()->isChannelName(errnick))
943 tryNextNick(errnick);
946 /* Handle signals from Netsplit objects */
948 void IrcServerHandler::handleNetsplitJoin(const QString &channel, const QStringList &users, const QStringList &modes, const QString& quitMessage)
950 IrcChannel *ircChannel = network()->ircChannel(channel);
954 QList<IrcUser *> ircUsers;
955 QStringList newModes = modes;
956 QStringList newUsers = users;
958 foreach(QString user, users) {
959 IrcUser *iu = network()->ircUser(nickFromMask(user));
962 else { // the user already quit
963 int idx = users.indexOf(user);
964 newUsers.removeAt(idx);
965 newModes.removeAt(idx);
969 QString msg = newUsers.join("#:#").append("#:#").append(quitMessage);
970 emit displayMsg(Message::NetsplitJoin, BufferInfo::ChannelBuffer, channel, msg);
971 ircChannel->joinIrcUsers(ircUsers, newModes);
974 void IrcServerHandler::handleNetsplitQuit(const QString &channel, const QStringList &users, const QString& quitMessage)
976 QString msg = users.join("#:#").append("#:#").append(quitMessage);
977 emit displayMsg(Message::NetsplitQuit, BufferInfo::ChannelBuffer, channel, msg);
978 foreach(QString user, users) {
979 IrcUser *iu = network()->ircUser(nickFromMask(user));
985 void IrcServerHandler::handleEarlyNetsplitJoin(const QString &channel, const QStringList &users, const QStringList &modes) {
986 IrcChannel *ircChannel = network()->ircChannel(channel);
988 qDebug() << "handleEarlyNetsplitJoin(): channel " << channel << " invalid";
991 QList<IrcUser *> ircUsers;
992 QStringList newModes = modes;
994 foreach(QString user, users) {
995 IrcUser *iu = network()->updateNickFromMask(user);
998 emit displayMsg(Message::Join, BufferInfo::ChannelBuffer, channel, channel, user);
1001 newModes.removeAt(users.indexOf(user));
1004 ircChannel->joinIrcUsers(ircUsers, newModes);
1006 void IrcServerHandler::handleNetsplitFinished()
1008 Netsplit* n = qobject_cast<Netsplit*>(sender());
1009 _netsplits.remove(_netsplits.key(n));
1015 // FIXME networkConnection()->setChannelKey("") for all ERR replies indicating that a JOIN went wrong
1016 // mostly, these are codes in the 47x range
1020 void IrcServerHandler::tryNextNick(const QString &errnick, bool erroneus) {
1021 QStringList desiredNicks = coreSession()->identity(network()->identity())->nicks();
1022 int nextNickIdx = desiredNicks.indexOf(errnick) + 1;
1024 if(nextNickIdx > 0 && desiredNicks.size() > nextNickIdx) {
1025 nextNick = desiredNicks[nextNickIdx];
1028 emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", tr("No free and valid nicks in nicklist found. use: /nick <othernick> to continue"));
1031 nextNick = errnick + "_";
1034 putCmd("NICK", serverEncode(nextNick));
1037 bool IrcServerHandler::checkParamCount(const QString &methodName, const QList<QByteArray> ¶ms, int minParams) {
1038 if(params.count() < minParams) {
1039 qWarning() << qPrintable(methodName) << "requires" << minParams << "parameters but received only" << params.count() << serverDecode(params);
1046 void IrcServerHandler::destroyNetsplits() {
1047 qDeleteAll(_netsplits);
1052 QByteArray IrcServerHandler::decrypt(const QString &bufferName, const QByteArray &message_, bool isTopic) {
1053 if(message_.isEmpty())
1056 Cipher *cipher = network()->cipher(bufferName);
1060 QByteArray message = message_;
1061 message = isTopic? cipher->decryptTopic(message) : cipher->decrypt(message);