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
EventStringifier(CoreSession * parent)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
displayMsg(NetworkEvent * event,Message::Type msgType,QString msg,QString sender,QString target,Message::Flags msgFlags)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
createMessageEvent(NetworkEvent * event,Message::Type msgType,QString msg,QString sender,QString target,Message::Flags msgFlags)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
checkParamCount(IrcEvent * e,int minParams)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 */
processNetworkSplitJoin(NetworkSplitEvent * e)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
processNetworkSplitQuit(NetworkSplitEvent * e)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
processIrcEventNumeric(IrcEventNumeric * e)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
processIrcEventInvite(IrcEvent * e)252 void EventStringifier::processIrcEventInvite(IrcEvent* e)
253 {
254 if (!checkParamCount(e, 2))
255 return;
256
257 // TODO: provide a nicer UI for invite notifications
258 QString target = e->params().at(0);
259 QString channel = e->params().at(1);
260 if (e->network()->isMyNick(target)) {
261 displayMsg(e, Message::Invite, tr("%1 invited you to channel %2").arg(e->nick(), channel));
262 }
263 else {
264 displayMsg(e, Message::Invite, tr("%1 invited %2 to channel %3").arg(e->nick(), target, channel));
265 }
266 }
267
processIrcEventJoin(IrcEvent * e)268 void EventStringifier::processIrcEventJoin(IrcEvent* e)
269 {
270 if (e->testFlag(EventManager::Netsplit))
271 return;
272
273 Message::Flag msgFlags = Message::Flag::None;
274 if (e->testFlag(EventManager::Self)) {
275 // Mark the message as Self
276 msgFlags = Message::Self;
277 }
278
279 displayMsg(e, Message::Join, e->params()[0], e->prefix(), e->params()[0], msgFlags);
280 }
281
processIrcEventKick(IrcEvent * e)282 void EventStringifier::processIrcEventKick(IrcEvent* e)
283 {
284 if (!checkParamCount(e, 2))
285 return;
286
287 IrcUser* victim = e->network()->ircUser(e->params().at(1));
288 if (victim) {
289 QString channel = e->params().at(0);
290 QString msg = victim->nick();
291 if (e->params().count() > 2)
292 msg += " " + e->params().at(2);
293
294 Message::Flag msgFlags = Message::Flag::None;
295 if (e->testFlag(EventManager::Self)) {
296 // Mark the message as Self
297 msgFlags = Message::Self;
298 }
299
300 displayMsg(e, Message::Kick, msg, e->prefix(), channel, msgFlags);
301 }
302 }
303
processIrcEventMode(IrcEvent * e)304 void EventStringifier::processIrcEventMode(IrcEvent* e)
305 {
306 if (e->network()->isChannelName(e->params().first())) {
307 // Channel Modes
308 displayMsg(e, Message::Mode, e->params().join(" "), e->prefix(), e->params().first());
309 }
310 else {
311 // User Modes
312 // FIXME: redirect
313
314 Message::Flag msgFlags = Message::Flag::None;
315 if (e->testFlag(EventManager::Self)) {
316 // Mark the message as Self
317 msgFlags = Message::Self;
318 }
319 displayMsg(e, Message::Mode, e->params().join(" "), e->prefix(), QString(), msgFlags);
320 }
321 }
322
323 // this needs to be called before the ircuser is renamed!
processIrcEventNick(IrcEvent * e)324 void EventStringifier::processIrcEventNick(IrcEvent* e)
325 {
326 if (!checkParamCount(e, 1))
327 return;
328
329 IrcUser* ircuser = e->network()->updateNickFromMask(e->prefix());
330 if (!ircuser) {
331 qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
332 return;
333 }
334
335 QString newnick = e->params().at(0);
336
337 QString sender;
338 Message::Flag msgFlags = Message::Flag::None;
339 if (e->testFlag(EventManager::Self)) {
340 // Treat the sender as the new nickname, mark the message as Self
341 sender = newnick;
342 msgFlags = Message::Self;
343 }
344 else {
345 // Take the sender from the event prefix, don't mark the message
346 sender = e->prefix();
347 }
348
349 // Announce to all channels the IrcUser is in
350 for (const QString& channel : ircuser->channels()) {
351 displayMsg(e, Message::Nick, newnick, sender, channel, msgFlags);
352 }
353 }
354
processIrcEventPart(IrcEvent * e)355 void EventStringifier::processIrcEventPart(IrcEvent* e)
356 {
357 if (!checkParamCount(e, 1))
358 return;
359
360 QString channel = e->params().at(0);
361 QString msg = e->params().count() > 1 ? e->params().at(1) : QString();
362
363 Message::Flag msgFlags = Message::Flag::None;
364 if (e->testFlag(EventManager::Self)) {
365 // Mark the message as Self
366 msgFlags = Message::Self;
367 }
368
369 displayMsg(e, Message::Part, msg, e->prefix(), channel, msgFlags);
370 }
371
processIrcEventPong(IrcEvent * e)372 void EventStringifier::processIrcEventPong(IrcEvent* e)
373 {
374 // CoreSessionEventProcessor will flag automated PONG replies as EventManager::Silent. There's
375 // no need to handle that specially here.
376
377 // Format the PONG reply for display
378 displayMsg(e, Message::Server, "PONG " + e->params().join(" "), e->prefix());
379 }
380
processIrcEventQuit(IrcEvent * e)381 void EventStringifier::processIrcEventQuit(IrcEvent* e)
382 {
383 if (e->testFlag(EventManager::Netsplit))
384 return;
385
386 IrcUser* ircuser = e->network()->updateNickFromMask(e->prefix());
387 if (!ircuser)
388 return;
389
390 Message::Flag msgFlags = Message::Flag::None;
391 if (e->testFlag(EventManager::Self)) {
392 // Mark the message as Self
393 msgFlags = Message::Self;
394 }
395
396 // Announce to all channels the IrcUser is in
397 for (const QString& channel : ircuser->channels()) {
398 displayMsg(e, Message::Quit, e->params().count() ? e->params().first() : QString(), e->prefix(), channel, msgFlags);
399 }
400 }
401
processIrcEventTopic(IrcEvent * e)402 void EventStringifier::processIrcEventTopic(IrcEvent* e)
403 {
404 Message::Flag msgFlags = Message::Flag::None;
405 if (e->testFlag(EventManager::Self)) {
406 // Mark the message as Self
407 msgFlags = Message::Self;
408 }
409
410 displayMsg(e,
411 Message::Topic,
412 tr("%1 has changed topic for %2 to: \"%3\"").arg(e->nick(), e->params().at(0), e->params().at(1)),
413 QString(),
414 e->params().at(0),
415 msgFlags);
416 }
417
processIrcEventError(IrcEvent * e)418 void EventStringifier::processIrcEventError(IrcEvent* e)
419 {
420 // Need an error reason
421 if (!checkParamCount(e, 1))
422 return;
423
424 displayMsg(e, Message::Server, tr("Error from server: ") + e->params().join(""));
425 }
426
processIrcEventWallops(IrcEvent * e)427 void EventStringifier::processIrcEventWallops(IrcEvent* e)
428 {
429 displayMsg(e, Message::Server, tr("[Operwall] %1: %2").arg(e->nick(), e->params().join(" ")));
430 }
431
432 /* RPL_ISUPPORT */
processIrcEvent005(IrcEvent * e)433 void EventStringifier::processIrcEvent005(IrcEvent* e)
434 {
435 if (!e->params().last().contains(QRegExp("are supported (by|on) this server")))
436 displayMsg(e, Message::Error, tr("Received non-RFC-compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), e->prefix());
437 displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
438 }
439
440 /* RPL_AWAY - "<nick> :<away message>" */
processIrcEvent301(IrcEvent * e)441 void EventStringifier::processIrcEvent301(IrcEvent* e)
442 {
443 QString nick = e->params().at(0);
444 QString awayMsg = e->params().at(1);
445 QString msg, target;
446 bool send = true;
447
448 // FIXME: proper redirection needed
449 if (_whois) {
450 msg = tr("[Whois] ");
451 }
452 else {
453 target = nick;
454 IrcUser* ircuser = e->network()->ircUser(nick);
455 if (ircuser) {
456 QDateTime now = QDateTime::currentDateTime();
457 now.setTimeSpec(Qt::UTC);
458 // Don't print "user is away" messages more often than this
459 // 1 hour = 60 min * 60 sec
460 const int silenceTime = 60 * 60;
461 // Check if away state has NOT changed and silence time hasn't yet elapsed
462 if (!ircuser->hasAwayChanged() && ircuser->lastAwayMessageTime().addSecs(silenceTime) >= now) {
463 // Away message hasn't changed and we're still within the period of silence; don't
464 // repeat the message
465 send = false;
466 }
467 ircuser->setLastAwayMessageTime(now);
468 // Mark any changes in away as acknowledged
469 ircuser->acknowledgeAwayChanged();
470 }
471 }
472 if (send)
473 displayMsg(e, Message::Server, msg + tr("%1 is away: \"%2\"").arg(nick, awayMsg), QString(), target);
474 }
475
476 /* RPL_UNAWAY - ":You are no longer marked as being away" */
processIrcEvent305(IrcEvent * e)477 void EventStringifier::processIrcEvent305(IrcEvent* e)
478 {
479 displayMsg(e, Message::Server, tr("You are no longer marked as being away"));
480 }
481
482 /* RPL_NOWAWAY - ":You have been marked as being away" */
processIrcEvent306(IrcEvent * e)483 void EventStringifier::processIrcEvent306(IrcEvent* e)
484 {
485 if (!e->network()->autoAwayActive())
486 displayMsg(e, Message::Server, tr("You have been marked as being away"));
487 }
488
489 /*
490 WHOIS-Message:
491 Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
492 and 301 (RPL_AWAY)
493 "<nick> :<away message>"
494 WHO-Message:
495 Replies 352 and 315 paired are used to answer a WHO message.
496
497 WHOWAS-Message:
498 Replies 314 and 369 are responses to a WHOWAS message.
499
500 */
501
502 /* RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
processIrcEvent311(IrcEvent * e)503 void EventStringifier::processIrcEvent311(IrcEvent* e)
504 {
505 _whois = true;
506
507 const QString whoisUserString = tr("[Whois] %1 is %2 (%3)");
508
509 IrcUser* ircuser = e->network()->ircUser(e->params().at(0));
510 if (ircuser)
511 displayMsg(e, Message::Server, whoisUserString.arg(ircuser->nick(), ircuser->hostmask(), ircuser->realName()));
512 else {
513 QString host = QString("%1!%2@%3").arg(e->params().at(0), e->params().at(1), e->params().at(2));
514 displayMsg(e, Message::Server, whoisUserString.arg(e->params().at(0), host, e->params().last()));
515 }
516 }
517
518 /* RPL_WHOISSERVER - "<nick> <server> :<server info>" */
processIrcEvent312(IrcEvent * e)519 void EventStringifier::processIrcEvent312(IrcEvent* e)
520 {
521 if (_whois)
522 displayMsg(e, Message::Server, tr("[Whois] %1 is online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
523 else
524 displayMsg(e, Message::Server, tr("[Whowas] %1 was online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
525 }
526
527 /* RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
processIrcEvent314(IrcEvent * e)528 void EventStringifier::processIrcEvent314(IrcEvent* e)
529 {
530 if (!checkParamCount(e, 3))
531 return;
532
533 displayMsg(e, Message::Server, tr("[Whowas] %1 was %2@%3 (%4)").arg(e->params()[0], e->params()[1], e->params()[2], e->params().last()));
534 }
535
536 /* RPL_ENDOFWHO: "<name> :End of WHO list" */
processIrcEvent315(IrcEvent * e)537 void EventStringifier::processIrcEvent315(IrcEvent* e)
538 {
539 QStringList p = e->params();
540 p.takeLast(); // should be "End of WHO list"
541 displayMsg(e, Message::Server, tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
542 }
543
544 /* RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
545 (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
processIrcEvent317(IrcEvent * e)546 void EventStringifier::processIrcEvent317(IrcEvent* e)
547 {
548 int idleSecs = e->params()[1].toInt();
549
550 if (e->params().count() > 3) {
551 // if we have more then 3 params we have the above mentioned "real life" situation
552 // Time in IRC protocol is defined as seconds. Convert from seconds instead.
553 // See https://doc.qt.io/qt-5/qdatetime.html#fromSecsSinceEpoch
554 #if QT_VERSION >= 0x050800
555 QDateTime loginTime = QDateTime::fromSecsSinceEpoch(e->params()[2].toLongLong()).toUTC();
556 #else
557 // fromSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now.
558 // See https://doc.qt.io/qt-5/qdatetime.html#fromMSecsSinceEpoch
559 QDateTime loginTime = QDateTime::fromMSecsSinceEpoch((qint64)(e->params()[2].toLongLong() * 1000)).toUTC();
560 #endif
561 displayMsg(e,
562 Message::Server,
563 tr("[Whois] %1 is logged in since %2").arg(e->params()[0], loginTime.toString("yyyy-MM-dd hh:mm:ss UTC")));
564 }
565 QDateTime idlingSince = e->timestamp().toLocalTime().addSecs(-idleSecs).toUTC();
566 displayMsg(e,
567 Message::Server,
568 tr("[Whois] %1 is idling for %2 (since %3)")
569 .arg(e->params()[0], secondsToString(idleSecs), idlingSince.toString("yyyy-MM-dd hh:mm:ss UTC")));
570 }
571
572 /* RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
processIrcEvent318(IrcEvent * e)573 void EventStringifier::processIrcEvent318(IrcEvent* e)
574 {
575 _whois = false;
576 displayMsg(e, Message::Server, tr("[Whois] End of /WHOIS list"));
577 }
578
579 /* RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
processIrcEvent319(IrcEvent * e)580 void EventStringifier::processIrcEvent319(IrcEvent* e)
581 {
582 if (!checkParamCount(e, 2))
583 return;
584
585 QString nick = e->params().first();
586 QStringList op;
587 QStringList voice;
588 QStringList user;
589 for (QString channel : e->params().last().split(" ")) {
590 if (channel.startsWith("@"))
591 op.append(channel.remove(0, 1));
592 else if (channel.startsWith("+"))
593 voice.append(channel.remove(0, 1));
594 else
595 user.append(channel);
596 }
597 if (!user.isEmpty())
598 displayMsg(e, Message::Server, tr("[Whois] %1 is a user on channels: %2").arg(nick, user.join(" ")));
599 if (!voice.isEmpty())
600 displayMsg(e, Message::Server, tr("[Whois] %1 has voice on channels: %2").arg(nick, voice.join(" ")));
601 if (!op.isEmpty())
602 displayMsg(e, Message::Server, tr("[Whois] %1 is an operator on channels: %2").arg(nick, op.join(" ")));
603 }
604
605 /* RPL_LIST - "<channel> <# visible> :<topic>" */
processIrcEvent322(IrcEvent * e)606 void EventStringifier::processIrcEvent322(IrcEvent* e)
607 {
608 QString channelName;
609 quint32 userCount = 0;
610 QString topic;
611
612 switch (e->params().count()) {
613 case 3:
614 topic = e->params()[2];
615 // fallthrough
616 case 2:
617 userCount = e->params()[1].toUInt();
618 /* fallthrough */
619 case 1:
620 channelName = e->params()[0];
621 // blubb
622 default:
623 break;
624 }
625 displayMsg(e, Message::Server, tr("Channel %1 has %2 users. Topic is: \"%3\"").arg(channelName).arg(userCount).arg(topic));
626 }
627
628 /* RPL_LISTEND ":End of LIST" */
processIrcEvent323(IrcEvent * e)629 void EventStringifier::processIrcEvent323(IrcEvent* e)
630 {
631 displayMsg(e, Message::Server, tr("End of channel list"));
632 }
633
634 /* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
processIrcEvent324(IrcEvent * e)635 void EventStringifier::processIrcEvent324(IrcEvent* e)
636 {
637 processIrcEventMode(e);
638 }
639
640 /* RPL_??? - "<channel> <homepage> */
processIrcEvent328(IrcEvent * e)641 void EventStringifier::processIrcEvent328(IrcEvent* e)
642 {
643 if (!checkParamCount(e, 2))
644 return;
645
646 QString channel = e->params()[0];
647 displayMsg(e, Message::Topic, tr("Homepage for %1 is %2").arg(channel, e->params()[1]), QString(), channel);
648 }
649
650 /* RPL_??? - "<channel> <creation time (unix)>" */
processIrcEvent329(IrcEvent * e)651 void EventStringifier::processIrcEvent329(IrcEvent* e)
652 {
653 if (!checkParamCount(e, 2))
654 return;
655
656 QString channel = e->params()[0];
657 // Allow for 64-bit time
658 qint64 unixtime = e->params()[1].toLongLong();
659 if (!unixtime) {
660 qWarning() << Q_FUNC_INFO << "received invalid timestamp:" << e->params()[1];
661 return;
662 }
663 // Time in IRC protocol is defined as seconds. Convert from seconds instead.
664 // See https://doc.qt.io/qt-5/qdatetime.html#fromSecsSinceEpoch
665 #if QT_VERSION >= 0x050800
666 QDateTime time = QDateTime::fromSecsSinceEpoch(unixtime).toUTC();
667 #else
668 // fromSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now.
669 // See https://doc.qt.io/qt-5/qdatetime.html#fromMSecsSinceEpoch
670 QDateTime time = QDateTime::fromMSecsSinceEpoch((qint64)(unixtime * 1000)).toUTC();
671 #endif
672 displayMsg(e, Message::Topic, tr("Channel %1 created on %2").arg(channel, time.toString("yyyy-MM-dd hh:mm:ss UTC")), QString(), channel);
673 }
674
675 /* RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
processIrcEvent330(IrcEvent * e)676 void EventStringifier::processIrcEvent330(IrcEvent* e)
677 {
678 if (e->params().count() < 3)
679 return;
680
681 // check for whois or whowas
682 if (_whois) {
683 displayMsg(e, Message::Server, tr("[Whois] %1 is authed as %2").arg(e->params()[0], e->params()[1]));
684 }
685 else {
686 displayMsg(e, Message::Server, tr("[Whowas] %1 was authed as %2").arg(e->params()[0], e->params()[1]));
687 }
688 }
689
690 /* RPL_NOTOPIC */
processIrcEvent331(IrcEvent * e)691 void EventStringifier::processIrcEvent331(IrcEvent* e)
692 {
693 QString channel = e->params().first();
694 displayMsg(e, Message::Topic, tr("No topic is set for %1.").arg(channel), QString(), channel);
695 }
696
697 /* RPL_TOPIC */
processIrcEvent332(IrcEvent * e)698 void EventStringifier::processIrcEvent332(IrcEvent* e)
699 {
700 QString channel = e->params().first();
701 displayMsg(e, Message::Topic, tr("Topic for %1 is \"%2\"").arg(channel, e->params()[1]), QString(), channel);
702 }
703
704 /* Topic set by... */
processIrcEvent333(IrcEvent * e)705 void EventStringifier::processIrcEvent333(IrcEvent* e)
706 {
707 if (!checkParamCount(e, 3))
708 return;
709
710 QString channel = e->params().first();
711 // Time in IRC protocol is defined as seconds. Convert from seconds instead.
712 // See https://doc.qt.io/qt-5/qdatetime.html#fromSecsSinceEpoch
713 #if QT_VERSION >= 0x050800
714 QDateTime topicSetTime = QDateTime::fromSecsSinceEpoch(e->params()[2].toLongLong()).toUTC();
715 #else
716 // fromSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now.
717 // See https://doc.qt.io/qt-5/qdatetime.html#fromMSecsSinceEpoch
718 QDateTime topicSetTime = QDateTime::fromMSecsSinceEpoch((qint64)(e->params()[2].toLongLong() * 1000)).toUTC();
719 #endif
720 displayMsg(e,
721 Message::Topic,
722 tr("Topic set by %1 on %2").arg(e->params()[1], topicSetTime.toString("yyyy-MM-dd hh:mm:ss UTC")),
723 QString(),
724 channel);
725 }
726
727 /* RPL_INVITING - "<nick> <channel>*/
processIrcEvent341(IrcEvent * e)728 void EventStringifier::processIrcEvent341(IrcEvent* e)
729 {
730 if (!checkParamCount(e, 2))
731 return;
732
733 QString channel = e->params()[1];
734 displayMsg(e, Message::Server, tr("%1 has been invited to %2").arg(e->params().first(), channel), QString(), channel);
735 }
736
737 /* RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
738 ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
processIrcEvent352(IrcEvent * e)739 void EventStringifier::processIrcEvent352(IrcEvent* e)
740 {
741 displayMsg(e, Message::Server, tr("[Who] %1").arg(e->params().join(" ")));
742 }
743
744 /* RPL_WHOSPCRPL: "<yournick> <num> #<channel> ~<ident> <host> <servname> <nick>
745 ("H"/ "G") <account> :<realname>"
746 Could be anything else, though. User-specified fields.
747 See http://faerion.sourceforge.net/doc/irc/whox.var */
processIrcEvent354(IrcEvent * e)748 void EventStringifier::processIrcEvent354(IrcEvent* e)
749 {
750 displayMsg(e, Message::Server, tr("[WhoX] %1").arg(e->params().join(" ")));
751 }
752
753 /* RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
processIrcEvent369(IrcEvent * e)754 void EventStringifier::processIrcEvent369(IrcEvent* e)
755 {
756 displayMsg(e, Message::Server, tr("End of /WHOWAS"));
757 }
758
759 /* ERR_ERRONEUSNICKNAME */
processIrcEvent432(IrcEvent * e)760 void EventStringifier::processIrcEvent432(IrcEvent* e)
761 {
762 if (!checkParamCount(e, 1))
763 return;
764
765 displayMsg(e, Message::Error, tr("Nick %1 contains illegal characters").arg(e->params()[0]));
766 }
767
768 /* ERR_NICKNAMEINUSE */
processIrcEvent433(IrcEvent * e)769 void EventStringifier::processIrcEvent433(IrcEvent* e)
770 {
771 if (!checkParamCount(e, 1))
772 return;
773
774 displayMsg(e, Message::Error, tr("Nick already in use: %1").arg(e->params()[0]));
775 }
776
777 /* ERR_UNAVAILRESOURCE */
processIrcEvent437(IrcEvent * e)778 void EventStringifier::processIrcEvent437(IrcEvent* e)
779 {
780 if (!checkParamCount(e, 1))
781 return;
782
783 displayMsg(e, Message::Error, tr("Nick/channel is temporarily unavailable: %1").arg(e->params()[0]));
784 }
785
786 // template
787 /*
788
789 void EventStringifier::processIrcEvent(IrcEvent *e) {
790
791 }
792
793 */
794
795 /*******************************/
796 /******** CTCP HANDLING ********/
797 /*******************************/
798
processCtcpEvent(CtcpEvent * e)799 void EventStringifier::processCtcpEvent(CtcpEvent* e)
800 {
801 if (e->type() != EventManager::CtcpEvent)
802 return;
803
804 // Only stringify outgoing CTCP messages this way
805 if (e->testFlag(EventManager::Self) &&
806 // Only stringify CTCP queries this way
807 e->ctcpType() == CtcpEvent::CtcpType::Query &&
808 // Always handle ACTIONs as if they were sent from network, to properly handle echo-message
809 e->ctcpCmd() != "ACTION") {
810 displayMsg(e,
811 Message::Action,
812 tr("sending CTCP-%1 request to %2").arg(e->ctcpCmd(), e->target()),
813 e->network()->myNick(),
814 QString(),
815 Message::Flag::Self);
816 return;
817 }
818
819 handle(e->ctcpCmd(), Q_ARG(CtcpEvent*, e));
820 }
821
defaultHandler(const QString & ctcpCmd,CtcpEvent * e)822 void EventStringifier::defaultHandler(const QString& ctcpCmd, CtcpEvent* e)
823 {
824 Q_UNUSED(ctcpCmd);
825 if (e->ctcpType() == CtcpEvent::Query) {
826 QString unknown;
827 if (e->reply().isNull()) // all known core-side handlers (except for ACTION) set a reply!
828 //: Optional "unknown" in "Received unknown CTCP-FOO request by bar"
829 unknown = tr("unknown") + ' ';
830 displayMsg(e, Message::Server, tr("Received %1CTCP-%2 request by %3").arg(unknown, e->ctcpCmd(), e->prefix()));
831 } else {
832 // Ignore echo messages for our own answers
833 if (!e->testFlag(EventManager::Self)) {
834 displayMsg(e, Message::Server,
835 tr("Received CTCP-%1 answer from %2: %3").arg(e->ctcpCmd(), nickFromMask(e->prefix()),e->param()));
836 }
837 }
838 }
839
handleCtcpAction(CtcpEvent * e)840 void EventStringifier::handleCtcpAction(CtcpEvent* e)
841 {
842 Message::Flag msgFlags = Message::Flag::None;
843 if (e->testFlag(EventManager::Self)) {
844 // Mark the message as Self
845 msgFlags = Message::Self;
846 }
847
848 displayMsg(e, Message::Action, e->param(), e->prefix(), e->target(), msgFlags);
849 }
850
handleCtcpPing(CtcpEvent * e)851 void EventStringifier::handleCtcpPing(CtcpEvent* e)
852 {
853 if (e->ctcpType() == CtcpEvent::Query)
854 defaultHandler(e->ctcpCmd(), e);
855 else {
856 displayMsg(e,
857 Message::Server,
858 tr("Received CTCP-PING answer from %1 with %2 milliseconds round trip time")
859 .arg(nickFromMask(e->prefix()))
860 .arg(QDateTime::fromMSecsSinceEpoch(e->param().toULongLong()).msecsTo(e->timestamp())));
861 }
862 }
863