cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / core / eventstringifier.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
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) version 3.                                           *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "eventstringifier.h"
22
23 #include "coresession.h"
24 #include "ctcpevent.h"
25 #include "irctags.h"
26 #include "messageevent.h"
27
28 EventStringifier::EventStringifier(CoreSession* parent)
29     : BasicHandler("handleCtcp", parent)
30     , _coreSession(parent)
31     , _whois(false)
32 {
33     connect(this, &EventStringifier::newMessageEvent, coreSession()->eventManager(), &EventManager::postEvent);
34 }
35
36 void EventStringifier::displayMsg(NetworkEvent* event,
37                                   Message::Type msgType,
38                                   QString msg,
39                                   QString sender,
40                                   QString target,
41                                   Message::Flags msgFlags)
42 {
43     if (event->flags().testFlag(EventManager::Silent))
44         return;
45
46     MessageEvent* msgEvent = createMessageEvent(event, msgType, std::move(msg), std::move(sender), std::move(target), msgFlags);
47     // sendMessageEvent(msgEvent);
48     emit newMessageEvent(msgEvent);
49 }
50
51 MessageEvent* EventStringifier::createMessageEvent(NetworkEvent* event,
52                                                    Message::Type msgType,
53                                                    QString msg,
54                                                    QString sender,
55                                                    QString target,
56                                                    Message::Flags msgFlags)
57 {
58     MessageEvent* msgEvent = new MessageEvent(msgType, event->network(), std::move(msg), std::move(sender), std::move(target), msgFlags, event->timestamp());
59     return msgEvent;
60 }
61
62 bool EventStringifier::checkParamCount(IrcEvent* e, int minParams)
63 {
64     if (e->params().count() < minParams) {
65         if (e->type() == EventManager::IrcEventNumeric) {
66             qWarning() << "Command " << static_cast<IrcEventNumeric*>(e)->number() << " requires " << minParams
67                        << "params, got: " << e->params();
68         }
69         else {
70             QString name = EventManager::enumName(e->type());
71             qWarning() << qPrintable(name) << "requires" << minParams << "params, got:" << e->params();
72         }
73         e->stop();
74         return false;
75     }
76     return true;
77 }
78
79 /* These are only for legacy reasons; remove as soon as we handle NetworkSplitEvents properly */
80 void EventStringifier::processNetworkSplitJoin(NetworkSplitEvent* e)
81 {
82     QString msg = e->users().join("#:#") + "#:#" + e->quitMessage();
83     displayMsg(e, Message::NetsplitJoin, msg, QString(), e->channel());
84 }
85
86 void EventStringifier::processNetworkSplitQuit(NetworkSplitEvent* e)
87 {
88     QString msg = e->users().join("#:#") + "#:#" + e->quitMessage();
89     displayMsg(e, Message::NetsplitQuit, msg, QString(), e->channel());
90 }
91
92 /* End legacy */
93
94 void EventStringifier::processIrcEventNumeric(IrcEventNumeric* e)
95 {
96     // qDebug() << e->number();
97     switch (e->number()) {
98     // Welcome, status, info messages. Just display these.
99     case 1:
100     case 2:
101     case 3:
102     case 4:
103     case 5:
104     case 221:
105     case 250:
106     case 251:
107     case 252:
108     case 253:
109     case 254:
110     case 255:
111     case 256:
112     case 257:
113     case 258:
114     case 259:
115     case 265:
116     case 266:
117     case 372:
118     case 375:
119         displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
120         break;
121
122     // Server error messages without param, just display them
123     case 263:
124     case 409:
125     case 411:
126     case 412:
127     case 422:
128     case 424:
129     case 445:
130     case 446:
131     case 451:
132     case 462:
133     case 463:
134     case 464:
135     case 465:
136     case 466:
137     case 472:
138     case 481:
139     case 483:
140     case 485:
141     case 491:
142     case 501:
143     case 502:
144     case 431:  // ERR_NONICKNAMEGIVEN
145         displayMsg(e, Message::Error, e->params().join(" "), e->prefix());
146         break;
147
148     // Server error messages, display them in red. Colon between first param and rest.
149     case 401: {
150         if (!checkParamCount(e, 1))
151             return;
152
153         QStringList params = e->params();
154         QString target = params.takeFirst();
155         displayMsg(e, Message::Error, target + ": " + params.join(" "), e->prefix(), target, Message::Redirected);
156         break;
157     }
158
159     case 402:
160     case 403:
161     case 404:
162     case 406:
163     case 408:
164     case 415:
165     case 421:
166     case 442: {
167         if (!checkParamCount(e, 1))
168             return;
169
170         QStringList params = e->params();
171         QString channelName = params.takeFirst();
172         displayMsg(e, Message::Error, channelName + ": " + params.join(" "), e->prefix());
173         break;
174     }
175
176     // Server error messages which will be displayed with a colon between the first param and the rest
177     case 413:
178     case 414:
179     case 423:
180     case 441:
181     case 444:
182     case 461:  // FIXME see below for the 47x codes
183     case 467:
184     case 471:
185     case 473:
186     case 474:
187     case 475:
188     case 476:
189     case 477:
190     case 478:
191     case 482:
192     case 436:  // ERR_NICKCOLLISION
193     {
194         if (!checkParamCount(e, 1))
195             return;
196
197         QStringList params = e->params();
198         QString p = params.takeFirst();
199         displayMsg(e, Message::Error, p + ": " + params.join(" "));
200         break;
201     }
202
203     // Ignore these commands.
204     case 321:
205     case 353:
206     case 366:
207     case 376:
208         break;
209
210     // SASL authentication stuff
211     // See: http://ircv3.net/specs/extensions/sasl-3.1.html
212     case 900:  // RPL_LOGGEDIN
213     case 901:  // RPL_LOGGEDOUT
214     {
215         // :server 900 <nick> <nick>!<ident>@<host> <account> :You are now logged in as <user>
216         // :server 901 <nick> <nick>!<ident>@<host> :You are now logged out
217         if (!checkParamCount(e, 3))
218             return;
219         displayMsg(e, Message::Server, "SASL: " + e->params().at(2));
220         break;
221     }
222     // Ignore SASL success, partially redundant with RPL_LOGGEDIN and RPL_LOGGEDOUT
223     case 903:  // RPL_SASLSUCCESS  :server 903 <nick> :SASL authentication successful
224         break;
225     case 902:  // ERR_NICKLOCKED   :server 902 <nick> :You must use a nick assigned to you
226     case 904:  // ERR_SASLFAIL     :server 904 <nick> :SASL authentication failed
227     case 905:  // ERR_SASLTOOLONG  :server 905 <nick> :SASL message too long
228     case 906:  // ERR_SASLABORTED  :server 906 <nick> :SASL authentication aborted
229     case 907:  // ERR_SASLALREADY  :server 907 <nick> :You have already authenticated using SASL
230     case 908:  // RPL_SASLMECHS    :server 908 <nick> <mechanisms> :are available SASL mechanisms
231     {
232         displayMsg(e, Message::Server, "SASL: " + e->params().join(""));
233         break;
234     }
235
236     // Everything else will be marked in red, so we can add them somewhere.
237     default:
238         if (_whois) {
239             // many nets define their own WHOIS fields. we fetch those not in need of special attention here:
240             displayMsg(e, Message::Server, tr("[Whois] ") + e->params().join(" "), e->prefix());
241         }
242         else {
243             // FIXME figure out how/where to do this in the future
244             // if(coreSession()->ircListHelper()->requestInProgress(network()->networkId()))
245             //  coreSession()->ircListHelper()->reportError(params.join(" "));
246             // else
247             displayMsg(e, Message::Error, QString("%1 %2").arg(e->number(), 3, 10, QLatin1Char('0')).arg(e->params().join(" ")), e->prefix());
248         }
249     }
250 }
251
252 void EventStringifier::processIrcEventInvite(IrcEvent* e)
253 {
254     displayMsg(e, Message::Invite, tr("%1 invited you to channel %2").arg(e->nick(), e->params().at(1)));
255 }
256
257 void EventStringifier::processIrcEventJoin(IrcEvent* e)
258 {
259     if (e->testFlag(EventManager::Netsplit))
260         return;
261
262     Message::Flag msgFlags = Message::Flag::None;
263     if (e->testFlag(EventManager::Self)) {
264         // Mark the message as Self
265         msgFlags = Message::Self;
266     }
267
268     displayMsg(e, Message::Join, e->params()[0], e->prefix(), e->params()[0], msgFlags);
269 }
270
271 void EventStringifier::processIrcEventKick(IrcEvent* e)
272 {
273     if (!checkParamCount(e, 2))
274         return;
275
276     IrcUser* victim = e->network()->ircUser(e->params().at(1));
277     if (victim) {
278         QString channel = e->params().at(0);
279         QString msg = victim->nick();
280         if (e->params().count() > 2)
281             msg += " " + e->params().at(2);
282
283         Message::Flag msgFlags = Message::Flag::None;
284         if (e->testFlag(EventManager::Self)) {
285             // Mark the message as Self
286             msgFlags = Message::Self;
287         }
288
289         displayMsg(e, Message::Kick, msg, e->prefix(), channel, msgFlags);
290     }
291 }
292
293 void EventStringifier::processIrcEventMode(IrcEvent* e)
294 {
295     if (e->network()->isChannelName(e->params().first())) {
296         // Channel Modes
297         displayMsg(e, Message::Mode, e->params().join(" "), e->prefix(), e->params().first());
298     }
299     else {
300         // User Modes
301         // FIXME: redirect
302
303         Message::Flag msgFlags = Message::Flag::None;
304         if (e->testFlag(EventManager::Self)) {
305             // Mark the message as Self
306             msgFlags = Message::Self;
307         }
308         displayMsg(e, Message::Mode, e->params().join(" "), e->prefix(), QString(), msgFlags);
309     }
310 }
311
312 // this needs to be called before the ircuser is renamed!
313 void EventStringifier::processIrcEventNick(IrcEvent* e)
314 {
315     if (!checkParamCount(e, 1))
316         return;
317
318     IrcUser* ircuser = e->network()->updateNickFromMask(e->prefix());
319     if (!ircuser) {
320         qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
321         return;
322     }
323
324     QString newnick = e->params().at(0);
325
326     QString sender;
327     Message::Flag msgFlags = Message::Flag::None;
328     if (e->testFlag(EventManager::Self)) {
329         // Treat the sender as the new nickname, mark the message as Self
330         sender = newnick;
331         msgFlags = Message::Self;
332     }
333     else {
334         // Take the sender from the event prefix, don't mark the message
335         sender = e->prefix();
336     }
337
338     // Announce to all channels the IrcUser is in
339     for (const QString& channel : ircuser->channels()) {
340         displayMsg(e, Message::Nick, newnick, sender, channel, msgFlags);
341     }
342 }
343
344 void EventStringifier::processIrcEventPart(IrcEvent* e)
345 {
346     if (!checkParamCount(e, 1))
347         return;
348
349     QString channel = e->params().at(0);
350     QString msg = e->params().count() > 1 ? e->params().at(1) : QString();
351
352     Message::Flag msgFlags = Message::Flag::None;
353     if (e->testFlag(EventManager::Self)) {
354         // Mark the message as Self
355         msgFlags = Message::Self;
356     }
357
358     displayMsg(e, Message::Part, msg, e->prefix(), channel, msgFlags);
359 }
360
361 void EventStringifier::processIrcEventPong(IrcEvent* e)
362 {
363     // CoreSessionEventProcessor will flag automated PONG replies as EventManager::Silent.  There's
364     // no need to handle that specially here.
365
366     // Format the PONG reply for display
367     displayMsg(e, Message::Server, "PONG " + e->params().join(" "), e->prefix());
368 }
369
370 void EventStringifier::processIrcEventQuit(IrcEvent* e)
371 {
372     if (e->testFlag(EventManager::Netsplit))
373         return;
374
375     IrcUser* ircuser = e->network()->updateNickFromMask(e->prefix());
376     if (!ircuser)
377         return;
378
379     Message::Flag msgFlags = Message::Flag::None;
380     if (e->testFlag(EventManager::Self)) {
381         // Mark the message as Self
382         msgFlags = Message::Self;
383     }
384
385     // Announce to all channels the IrcUser is in
386     for (const QString& channel : ircuser->channels()) {
387         displayMsg(e, Message::Quit, e->params().count() ? e->params().first() : QString(), e->prefix(), channel, msgFlags);
388     }
389 }
390
391 void EventStringifier::processIrcEventTopic(IrcEvent* e)
392 {
393     Message::Flag msgFlags = Message::Flag::None;
394     if (e->testFlag(EventManager::Self)) {
395         // Mark the message as Self
396         msgFlags = Message::Self;
397     }
398
399     displayMsg(e,
400                Message::Topic,
401                tr("%1 has changed topic for %2 to: \"%3\"").arg(e->nick(), e->params().at(0), e->params().at(1)),
402                QString(),
403                e->params().at(0),
404                msgFlags);
405 }
406
407 void EventStringifier::processIrcEventError(IrcEvent* e)
408 {
409     // Need an error reason
410     if (!checkParamCount(e, 1))
411         return;
412
413     displayMsg(e, Message::Server, tr("Error from server: ") + e->params().join(""));
414 }
415
416 void EventStringifier::processIrcEventWallops(IrcEvent* e)
417 {
418     displayMsg(e, Message::Server, tr("[Operwall] %1: %2").arg(e->nick(), e->params().join(" ")));
419 }
420
421 /* RPL_ISUPPORT */
422 void EventStringifier::processIrcEvent005(IrcEvent* e)
423 {
424     if (!e->params().last().contains(QRegExp("are supported (by|on) this server")))
425         displayMsg(e, Message::Error, tr("Received non-RFC-compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), e->prefix());
426     displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
427 }
428
429 /* RPL_AWAY - "<nick> :<away message>" */
430 void EventStringifier::processIrcEvent301(IrcEvent* e)
431 {
432     QString nick = e->params().at(0);
433     QString awayMsg = e->params().at(1);
434     QString msg, target;
435     bool send = true;
436
437     // FIXME: proper redirection needed
438     if (_whois) {
439         msg = tr("[Whois] ");
440     }
441     else {
442         target = nick;
443         IrcUser* ircuser = e->network()->ircUser(nick);
444         if (ircuser) {
445             QDateTime now = QDateTime::currentDateTime();
446             now.setTimeSpec(Qt::UTC);
447             // Don't print "user is away" messages more often than this
448             // 1 hour = 60 min * 60 sec
449             const int silenceTime = 60 * 60;
450             // Check if away state has NOT changed and silence time hasn't yet elapsed
451             if (!ircuser->hasAwayChanged() && ircuser->lastAwayMessageTime().addSecs(silenceTime) >= now) {
452                 // Away message hasn't changed and we're still within the period of silence; don't
453                 // repeat the message
454                 send = false;
455             }
456             ircuser->setLastAwayMessageTime(now);
457             // Mark any changes in away as acknowledged
458             ircuser->acknowledgeAwayChanged();
459         }
460     }
461     if (send)
462         displayMsg(e, Message::Server, msg + tr("%1 is away: \"%2\"").arg(nick, awayMsg), QString(), target);
463 }
464
465 /* RPL_UNAWAY - ":You are no longer marked as being away" */
466 void EventStringifier::processIrcEvent305(IrcEvent* e)
467 {
468     displayMsg(e, Message::Server, tr("You are no longer marked as being away"));
469 }
470
471 /* RPL_NOWAWAY - ":You have been marked as being away" */
472 void EventStringifier::processIrcEvent306(IrcEvent* e)
473 {
474     if (!e->network()->autoAwayActive())
475         displayMsg(e, Message::Server, tr("You have been marked as being away"));
476 }
477
478 /*
479 WHOIS-Message:
480    Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
481   and 301 (RPL_AWAY)
482               "<nick> :<away message>"
483 WHO-Message:
484    Replies 352 and 315 paired are used to answer a WHO message.
485
486 WHOWAS-Message:
487    Replies 314 and 369 are responses to a WHOWAS message.
488
489 */
490
491 /*  RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
492 void EventStringifier::processIrcEvent311(IrcEvent* e)
493 {
494     _whois = true;
495
496     const QString whoisUserString = tr("[Whois] %1 is %2 (%3)");
497
498     IrcUser* ircuser = e->network()->ircUser(e->params().at(0));
499     if (ircuser)
500         displayMsg(e, Message::Server, whoisUserString.arg(ircuser->nick(), ircuser->hostmask(), ircuser->realName()));
501     else {
502         QString host = QString("%1!%2@%3").arg(e->params().at(0), e->params().at(1), e->params().at(2));
503         displayMsg(e, Message::Server, whoisUserString.arg(e->params().at(0), host, e->params().last()));
504     }
505 }
506
507 /*  RPL_WHOISSERVER -  "<nick> <server> :<server info>" */
508 void EventStringifier::processIrcEvent312(IrcEvent* e)
509 {
510     if (_whois)
511         displayMsg(e, Message::Server, tr("[Whois] %1 is online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
512     else
513         displayMsg(e, Message::Server, tr("[Whowas] %1 was online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
514 }
515
516 /*  RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
517 void EventStringifier::processIrcEvent314(IrcEvent* e)
518 {
519     if (!checkParamCount(e, 3))
520         return;
521
522     displayMsg(e, Message::Server, tr("[Whowas] %1 was %2@%3 (%4)").arg(e->params()[0], e->params()[1], e->params()[2], e->params().last()));
523 }
524
525 /*  RPL_ENDOFWHO: "<name> :End of WHO list" */
526 void EventStringifier::processIrcEvent315(IrcEvent* e)
527 {
528     QStringList p = e->params();
529     p.takeLast();  // should be "End of WHO list"
530     displayMsg(e, Message::Server, tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
531 }
532
533 /*  RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
534    (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
535 void EventStringifier::processIrcEvent317(IrcEvent* e)
536 {
537     int idleSecs = e->params()[1].toInt();
538
539     if (e->params().count() > 3) {
540         // if we have more then 3 params we have the above mentioned "real life" situation
541         // Time in IRC protocol is defined as seconds.  Convert from seconds instead.
542         // See https://doc.qt.io/qt-5/qdatetime.html#fromSecsSinceEpoch
543 #if QT_VERSION >= 0x050800
544         QDateTime loginTime = QDateTime::fromSecsSinceEpoch(e->params()[2].toLongLong()).toUTC();
545 #else
546         // fromSecsSinceEpoch() was added in Qt 5.8.  Manually downconvert to seconds for now.
547         // See https://doc.qt.io/qt-5/qdatetime.html#fromMSecsSinceEpoch
548         QDateTime loginTime = QDateTime::fromMSecsSinceEpoch((qint64)(e->params()[2].toLongLong() * 1000)).toUTC();
549 #endif
550         displayMsg(e,
551                    Message::Server,
552                    tr("[Whois] %1 is logged in since %2").arg(e->params()[0], loginTime.toString("yyyy-MM-dd hh:mm:ss UTC")));
553     }
554     QDateTime idlingSince = e->timestamp().toLocalTime().addSecs(-idleSecs).toUTC();
555     displayMsg(e,
556                Message::Server,
557                tr("[Whois] %1 is idling for %2 (since %3)")
558                    .arg(e->params()[0], secondsToString(idleSecs), idlingSince.toString("yyyy-MM-dd hh:mm:ss UTC")));
559 }
560
561 /*  RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
562 void EventStringifier::processIrcEvent318(IrcEvent* e)
563 {
564     _whois = false;
565     displayMsg(e, Message::Server, tr("[Whois] End of /WHOIS list"));
566 }
567
568 /*  RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
569 void EventStringifier::processIrcEvent319(IrcEvent* e)
570 {
571     if (!checkParamCount(e, 2))
572         return;
573
574     QString nick = e->params().first();
575     QStringList op;
576     QStringList voice;
577     QStringList user;
578     for (QString channel : e->params().last().split(" ")) {
579         if (channel.startsWith("@"))
580             op.append(channel.remove(0, 1));
581         else if (channel.startsWith("+"))
582             voice.append(channel.remove(0, 1));
583         else
584             user.append(channel);
585     }
586     if (!user.isEmpty())
587         displayMsg(e, Message::Server, tr("[Whois] %1 is a user on channels: %2").arg(nick, user.join(" ")));
588     if (!voice.isEmpty())
589         displayMsg(e, Message::Server, tr("[Whois] %1 has voice on channels: %2").arg(nick, voice.join(" ")));
590     if (!op.isEmpty())
591         displayMsg(e, Message::Server, tr("[Whois] %1 is an operator on channels: %2").arg(nick, op.join(" ")));
592 }
593
594 /* RPL_LIST -  "<channel> <# visible> :<topic>" */
595 void EventStringifier::processIrcEvent322(IrcEvent* e)
596 {
597     QString channelName;
598     quint32 userCount = 0;
599     QString topic;
600
601     switch (e->params().count()) {
602     case 3:
603         topic = e->params()[2];
604         // fallthrough
605     case 2:
606         userCount = e->params()[1].toUInt();
607         /* fallthrough */
608     case 1:
609         channelName = e->params()[0];
610         // blubb
611     default:
612         break;
613     }
614     displayMsg(e, Message::Server, tr("Channel %1 has %2 users. Topic is: \"%3\"").arg(channelName).arg(userCount).arg(topic));
615 }
616
617 /* RPL_LISTEND ":End of LIST" */
618 void EventStringifier::processIrcEvent323(IrcEvent* e)
619 {
620     displayMsg(e, Message::Server, tr("End of channel list"));
621 }
622
623 /* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
624 void EventStringifier::processIrcEvent324(IrcEvent* e)
625 {
626     processIrcEventMode(e);
627 }
628
629 /* RPL_??? - "<channel> <homepage> */
630 void EventStringifier::processIrcEvent328(IrcEvent* e)
631 {
632     if (!checkParamCount(e, 2))
633         return;
634
635     QString channel = e->params()[0];
636     displayMsg(e, Message::Topic, tr("Homepage for %1 is %2").arg(channel, e->params()[1]), QString(), channel);
637 }
638
639 /* RPL_??? - "<channel> <creation time (unix)>" */
640 void EventStringifier::processIrcEvent329(IrcEvent* e)
641 {
642     if (!checkParamCount(e, 2))
643         return;
644
645     QString channel = e->params()[0];
646     // Allow for 64-bit time
647     qint64 unixtime = e->params()[1].toLongLong();
648     if (!unixtime) {
649         qWarning() << Q_FUNC_INFO << "received invalid timestamp:" << e->params()[1];
650         return;
651     }
652     // Time in IRC protocol is defined as seconds.  Convert from seconds instead.
653     // See https://doc.qt.io/qt-5/qdatetime.html#fromSecsSinceEpoch
654 #if QT_VERSION >= 0x050800
655     QDateTime time = QDateTime::fromSecsSinceEpoch(unixtime).toUTC();
656 #else
657     // fromSecsSinceEpoch() was added in Qt 5.8.  Manually downconvert to seconds for now.
658     // See https://doc.qt.io/qt-5/qdatetime.html#fromMSecsSinceEpoch
659     QDateTime time = QDateTime::fromMSecsSinceEpoch((qint64)(unixtime * 1000)).toUTC();
660 #endif
661     displayMsg(e, Message::Topic, tr("Channel %1 created on %2").arg(channel, time.toString("yyyy-MM-dd hh:mm:ss UTC")), QString(), channel);
662 }
663
664 /*  RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
665 void EventStringifier::processIrcEvent330(IrcEvent* e)
666 {
667     if (e->params().count() < 3)
668         return;
669
670     // check for whois or whowas
671     if (_whois) {
672         displayMsg(e, Message::Server, tr("[Whois] %1 is authed as %2").arg(e->params()[0], e->params()[1]));
673     }
674     else {
675         displayMsg(e, Message::Server, tr("[Whowas] %1 was authed as %2").arg(e->params()[0], e->params()[1]));
676     }
677 }
678
679 /* RPL_NOTOPIC */
680 void EventStringifier::processIrcEvent331(IrcEvent* e)
681 {
682     QString channel = e->params().first();
683     displayMsg(e, Message::Topic, tr("No topic is set for %1.").arg(channel), QString(), channel);
684 }
685
686 /* RPL_TOPIC */
687 void EventStringifier::processIrcEvent332(IrcEvent* e)
688 {
689     QString channel = e->params().first();
690     displayMsg(e, Message::Topic, tr("Topic for %1 is \"%2\"").arg(channel, e->params()[1]), QString(), channel);
691 }
692
693 /* Topic set by... */
694 void EventStringifier::processIrcEvent333(IrcEvent* e)
695 {
696     if (!checkParamCount(e, 3))
697         return;
698
699     QString channel = e->params().first();
700     // Time in IRC protocol is defined as seconds.  Convert from seconds instead.
701     // See https://doc.qt.io/qt-5/qdatetime.html#fromSecsSinceEpoch
702 #if QT_VERSION >= 0x050800
703     QDateTime topicSetTime = QDateTime::fromSecsSinceEpoch(e->params()[2].toLongLong()).toUTC();
704 #else
705     // fromSecsSinceEpoch() was added in Qt 5.8.  Manually downconvert to seconds for now.
706     // See https://doc.qt.io/qt-5/qdatetime.html#fromMSecsSinceEpoch
707     QDateTime topicSetTime = QDateTime::fromMSecsSinceEpoch((qint64)(e->params()[2].toLongLong() * 1000)).toUTC();
708 #endif
709     displayMsg(e,
710                Message::Topic,
711                tr("Topic set by %1 on %2").arg(e->params()[1], topicSetTime.toString("yyyy-MM-dd hh:mm:ss UTC")),
712                QString(),
713                channel);
714 }
715
716 /* RPL_INVITING - "<nick> <channel>*/
717 void EventStringifier::processIrcEvent341(IrcEvent* e)
718 {
719     if (!checkParamCount(e, 2))
720         return;
721
722     QString channel = e->params()[1];
723     displayMsg(e, Message::Server, tr("%1 has been invited to %2").arg(e->params().first(), channel), QString(), channel);
724 }
725
726 /*  RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
727               ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
728 void EventStringifier::processIrcEvent352(IrcEvent* e)
729 {
730     displayMsg(e, Message::Server, tr("[Who] %1").arg(e->params().join(" ")));
731 }
732
733 /*  RPL_WHOSPCRPL: "<yournick> <num> #<channel> ~<ident> <host> <servname> <nick>
734                     ("H"/ "G") <account> :<realname>"
735 Could be anything else, though.  User-specified fields.
736 See http://faerion.sourceforge.net/doc/irc/whox.var */
737 void EventStringifier::processIrcEvent354(IrcEvent* e)
738 {
739     displayMsg(e, Message::Server, tr("[WhoX] %1").arg(e->params().join(" ")));
740 }
741
742 /*  RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
743 void EventStringifier::processIrcEvent369(IrcEvent* e)
744 {
745     displayMsg(e, Message::Server, tr("End of /WHOWAS"));
746 }
747
748 /* ERR_ERRONEUSNICKNAME */
749 void EventStringifier::processIrcEvent432(IrcEvent* e)
750 {
751     if (!checkParamCount(e, 1))
752         return;
753
754     displayMsg(e, Message::Error, tr("Nick %1 contains illegal characters").arg(e->params()[0]));
755 }
756
757 /* ERR_NICKNAMEINUSE */
758 void EventStringifier::processIrcEvent433(IrcEvent* e)
759 {
760     if (!checkParamCount(e, 1))
761         return;
762
763     displayMsg(e, Message::Error, tr("Nick already in use: %1").arg(e->params()[0]));
764 }
765
766 /* ERR_UNAVAILRESOURCE */
767 void EventStringifier::processIrcEvent437(IrcEvent* e)
768 {
769     if (!checkParamCount(e, 1))
770         return;
771
772     displayMsg(e, Message::Error, tr("Nick/channel is temporarily unavailable: %1").arg(e->params()[0]));
773 }
774
775 // template
776 /*
777
778 void EventStringifier::processIrcEvent(IrcEvent *e) {
779
780 }
781
782 */
783
784 /*******************************/
785 /******** CTCP HANDLING ********/
786 /*******************************/
787
788 void EventStringifier::processCtcpEvent(CtcpEvent* e)
789 {
790     if (e->type() != EventManager::CtcpEvent)
791         return;
792
793     if (e->testFlag(EventManager::Self)) {
794         displayMsg(e,
795                    Message::Action,
796                    tr("sending CTCP-%1 request to %2").arg(e->ctcpCmd(), e->target()),
797                    e->network()->myNick(),
798                    QString(),
799                    Message::Flag::Self);
800         return;
801     }
802
803     handle(e->ctcpCmd(), Q_ARG(CtcpEvent*, e));
804 }
805
806 void EventStringifier::defaultHandler(const QString& ctcpCmd, CtcpEvent* e)
807 {
808     Q_UNUSED(ctcpCmd);
809     if (e->ctcpType() == CtcpEvent::Query) {
810         QString unknown;
811         if (e->reply().isNull())  // all known core-side handlers (except for ACTION) set a reply!
812             //: Optional "unknown" in "Received unknown CTCP-FOO request by bar"
813             unknown = tr("unknown") + ' ';
814         displayMsg(e, Message::Server, tr("Received %1CTCP-%2 request by %3").arg(unknown, e->ctcpCmd(), e->prefix()));
815         return;
816     }
817     displayMsg(e, Message::Server, tr("Received CTCP-%1 answer from %2: %3").arg(e->ctcpCmd(), nickFromMask(e->prefix()), e->param()));
818 }
819
820 void EventStringifier::handleCtcpAction(CtcpEvent* e)
821 {
822     displayMsg(e, Message::Action, e->param(), e->prefix(), e->target());
823 }
824
825 void EventStringifier::handleCtcpPing(CtcpEvent* e)
826 {
827     if (e->ctcpType() == CtcpEvent::Query)
828         defaultHandler(e->ctcpCmd(), e);
829     else {
830         displayMsg(e,
831                    Message::Server,
832                    tr("Received CTCP-PING answer from %1 with %2 milliseconds round trip time")
833                        .arg(nickFromMask(e->prefix()))
834                        .arg(QDateTime::fromMSecsSinceEpoch(e->param().toULongLong()).msecsTo(e->timestamp())));
835     }
836 }