+void EventStringifier::processIrcEventNick(IrcEvent *e)
+{
+ if (!checkParamCount(e, 1))
+ return;
+
+ IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
+ if (!ircuser) {
+ qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
+ return;
+ }
+
+ QString newnick = e->params().at(0);
+
+ QString sender;
+ Message::Flag msgFlags = Message::Flag::None;
+ if (e->testFlag(EventManager::Self)) {
+ // Treat the sender as the new nickname, mark the message as Self
+ sender = newnick;
+ msgFlags = Message::Self;
+ } else {
+ // Take the sender from the event prefix, don't mark the message
+ sender = e->prefix();
+ }
+
+ // Announce to all channels the IrcUser is in
+ foreach(const QString &channel, ircuser->channels()) {
+ displayMsg(e, Message::Nick, newnick, sender, channel, msgFlags);
+ }
+}
+
+
+void EventStringifier::processIrcEventPart(IrcEvent *e)
+{
+ if (!checkParamCount(e, 1))
+ return;
+
+ QString channel = e->params().at(0);
+ QString msg = e->params().count() > 1 ? e->params().at(1) : QString();
+
+
+ Message::Flag msgFlags = Message::Flag::None;
+ if (e->testFlag(EventManager::Self)) {
+ // Mark the message as Self
+ msgFlags = Message::Self;
+ }
+
+ displayMsg(e, Message::Part, msg, e->prefix(), channel, msgFlags);
+}
+
+
+void EventStringifier::processIrcEventPong(IrcEvent *e)
+{
+ QString timestamp = e->params().at(1);
+ QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
+ if (!sendTime.isValid())
+ displayMsg(e, Message::Server, "PONG " + e->params().join(" "), e->prefix());
+}
+
+
+void EventStringifier::processIrcEventQuit(IrcEvent *e)
+{
+ if (e->testFlag(EventManager::Netsplit))
+ return;
+
+ IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
+ if (!ircuser)
+ return;
+
+
+ Message::Flag msgFlags = Message::Flag::None;
+ if (e->testFlag(EventManager::Self)) {
+ // Mark the message as Self
+ msgFlags = Message::Self;
+ }
+
+ // Announce to all channels the IrcUser is in
+ foreach(const QString &channel, ircuser->channels()) {
+ displayMsg(e, Message::Quit, e->params().count() ? e->params().first() : QString(),
+ e->prefix(), channel, msgFlags);
+ }
+}
+
+
+void EventStringifier::processIrcEventTopic(IrcEvent *e)
+{
+ Message::Flag msgFlags = Message::Flag::None;
+ if (e->testFlag(EventManager::Self)) {
+ // Mark the message as Self
+ msgFlags = Message::Self;
+ }
+
+ displayMsg(e, Message::Topic, tr("%1 has changed topic for %2 to: \"%3\"")
+ .arg(e->nick(), e->params().at(0), e->params().at(1)), QString(), e->params().at(0),
+ msgFlags);
+}
+
+void EventStringifier::processIrcEventError(IrcEvent *e)
+{
+ // Need an error reason
+ if (!checkParamCount(e, 1))
+ return;
+
+ displayMsg(e, Message::Server, tr("Error from server: ") + e->params().join(""));
+}
+
+void EventStringifier::processIrcEventWallops(IrcEvent *e)
+{
+ displayMsg(e, Message::Server, tr("[Operwall] %1: %2").arg(e->nick(), e->params().join(" ")));
+}
+
+
+/* RPL_ISUPPORT */
+void EventStringifier::processIrcEvent005(IrcEvent *e)
+{
+ if (!e->params().last().contains(QRegExp("are supported (by|on) this server")))
+ displayMsg(e, Message::Error, tr("Received non-RFC-compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), e->prefix());
+ displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
+}
+
+
+/* RPL_AWAY - "<nick> :<away message>" */
+void EventStringifier::processIrcEvent301(IrcEvent *e)
+{
+ QString nick = e->params().at(0);
+ QString awayMsg = e->params().at(1);
+ QString msg, target;
+ bool send = true;
+
+ // FIXME: proper redirection needed
+ if (_whois) {
+ msg = tr("[Whois] ");
+ }
+ else {
+ target = nick;
+ IrcUser *ircuser = e->network()->ircUser(nick);
+ if (ircuser) {
+ QDateTime now = QDateTime::currentDateTime();
+ now.setTimeSpec(Qt::UTC);
+ // Don't print "user is away" messages more often than this
+ const int silenceTime = 60;
+ if (ircuser->lastAwayMessageTime().addSecs(silenceTime) >= now)
+ send = false;
+ ircuser->setLastAwayMessageTime(now);
+ }
+ }
+ if (send)
+ displayMsg(e, Message::Server, msg + tr("%1 is away: \"%2\"").arg(nick, awayMsg), QString(), target);
+}
+
+
+/* RPL_UNAWAY - ":You are no longer marked as being away" */
+void EventStringifier::processIrcEvent305(IrcEvent *e)
+{
+ displayMsg(e, Message::Server, tr("You are no longer marked as being away"));
+}
+
+
+/* RPL_NOWAWAY - ":You have been marked as being away" */
+void EventStringifier::processIrcEvent306(IrcEvent *e)
+{
+ if (!e->network()->autoAwayActive())
+ displayMsg(e, Message::Server, tr("You have been marked as being away"));
+}
+
+
+/*
+WHOIS-Message:
+ Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
+ and 301 (RPL_AWAY)
+ "<nick> :<away message>"
+WHO-Message:
+ Replies 352 and 315 paired are used to answer a WHO message.
+
+WHOWAS-Message:
+ Replies 314 and 369 are responses to a WHOWAS message.
+
+*/
+
+/* RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
+void EventStringifier::processIrcEvent311(IrcEvent *e)
+{
+ _whois = true;
+
+ const QString whoisUserString = tr("[Whois] %1 is %2 (%3)");
+
+ IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
+ if (ircuser)
+ displayMsg(e, Message::Server, whoisUserString.arg(ircuser->nick(), ircuser->hostmask(), ircuser->realName()));
+ else {
+ QString host = QString("%1!%2@%3").arg(e->params().at(0), e->params().at(1), e->params().at(2));
+ displayMsg(e, Message::Server, whoisUserString.arg(e->params().at(0), host, e->params().last()));
+ }
+}
+
+
+/* RPL_WHOISSERVER - "<nick> <server> :<server info>" */
+void EventStringifier::processIrcEvent312(IrcEvent *e)
+{
+ if (_whois)
+ displayMsg(e, Message::Server, tr("[Whois] %1 is online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
+ else
+ displayMsg(e, Message::Server, tr("[Whowas] %1 was online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
+}
+
+
+/* RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
+void EventStringifier::processIrcEvent314(IrcEvent *e)
+{
+ if (!checkParamCount(e, 3))
+ return;
+
+ displayMsg(e, Message::Server, tr("[Whowas] %1 was %2@%3 (%4)").arg(e->params()[0], e->params()[1], e->params()[2], e->params().last()));
+}
+
+
+/* RPL_ENDOFWHO: "<name> :End of WHO list" */
+void EventStringifier::processIrcEvent315(IrcEvent *e)
+{
+ QStringList p = e->params();
+ p.takeLast(); // should be "End of WHO list"
+ displayMsg(e, Message::Server, tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
+}
+
+
+/* RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
+ (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
+void EventStringifier::processIrcEvent317(IrcEvent *e)
+{
+ int idleSecs = e->params()[1].toInt();
+
+ if (e->params().count() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
+ // Time in IRC protocol is defined as seconds. Convert from seconds instead.
+ // See https://doc.qt.io/qt-5/qdatetime.html#fromSecsSinceEpoch
+#if QT_VERSION >= 0x050800
+ QDateTime loginTime = QDateTime::fromSecsSinceEpoch(e->params()[2].toLongLong()).toUTC();
+#else
+ // fromSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now.
+ // See https://doc.qt.io/qt-5/qdatetime.html#fromMSecsSinceEpoch
+ QDateTime loginTime = QDateTime::fromMSecsSinceEpoch(
+ (qint64)(e->params()[2].toLongLong() * 1000)).toUTC();
+#endif
+ displayMsg(e, Message::Server, tr("[Whois] %1 is logged in since %2")
+ .arg(e->params()[0], loginTime.toString("yyyy-MM-dd hh:mm:ss UTC")));
+ }
+ QDateTime idlingSince = e->timestamp().toLocalTime().addSecs(-idleSecs).toUTC();
+ displayMsg(e, Message::Server, tr("[Whois] %1 is idling for %2 (since %3)")
+ .arg(e->params()[0], secondsToString(idleSecs),
+ idlingSince.toString("yyyy-MM-dd hh:mm:ss UTC")));
+}
+
+
+/* RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
+void EventStringifier::processIrcEvent318(IrcEvent *e)
+{
+ _whois = false;
+ displayMsg(e, Message::Server, tr("[Whois] End of /WHOIS list"));
+}
+
+
+/* RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
+void EventStringifier::processIrcEvent319(IrcEvent *e)
+{
+ if (!checkParamCount(e, 2))
+ return;
+
+ QString nick = e->params().first();
+ QStringList op;
+ QStringList voice;
+ QStringList user;
+ foreach(QString channel, e->params().last().split(" ")) {
+ if (channel.startsWith("@"))
+ op.append(channel.remove(0, 1));
+ else if (channel.startsWith("+"))
+ voice.append(channel.remove(0, 1));
+ else
+ user.append(channel);
+ }
+ if (!user.isEmpty())
+ displayMsg(e, Message::Server, tr("[Whois] %1 is a user on channels: %2").arg(nick, user.join(" ")));
+ if (!voice.isEmpty())
+ displayMsg(e, Message::Server, tr("[Whois] %1 has voice on channels: %2").arg(nick, voice.join(" ")));
+ if (!op.isEmpty())
+ displayMsg(e, Message::Server, tr("[Whois] %1 is an operator on channels: %2").arg(nick, op.join(" ")));
+}