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