1 /***************************************************************************
2 * Copyright (C) 2005-2010 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "eventstringifier.h"
23 #include "coresession.h"
24 #include "messageevent.h"
26 EventStringifier::EventStringifier(CoreSession *parent) : QObject(parent),
33 void EventStringifier::displayMsg(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
34 const QString &target, Message::Flags msgFlags) {
35 if(event->flags().testFlag(EventManager::Silent))
38 MessageEvent *msgEvent = createMessageEvent(event, msgType, msg, sender, target, msgFlags);
39 sendMessageEvent(msgEvent);
42 MessageEvent *EventStringifier::createMessageEvent(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
43 const QString &target, Message::Flags msgFlags) {
44 MessageEvent *msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
45 msgEvent->setTimestamp(event->timestamp());
49 void EventStringifier::sendMessageEvent(MessageEvent *event) {
50 coreSession()->eventManager()->sendEvent(event);
53 bool EventStringifier::checkParamCount(IrcEvent *e, int minParams) {
54 if(e->params().count() < minParams) {
55 if(e->type() == EventManager::IrcEventNumeric) {
56 qWarning() << "Command " << static_cast<IrcEventNumeric *>(e)->number() << " requires " << minParams << "params, got: " << e->params();
58 QString name = coreSession()->eventManager()->enumName(e->type());
59 qWarning() << qPrintable(name) << "requires" << minParams << "params, got:" << e->params();
67 /* These are only for legacy reasons; remove as soon as we handle NetworkSplitEvents properly */
68 void EventStringifier::processNetworkSplitJoin(NetworkSplitEvent *e) {
69 QString msg = e->users().join("#:#") + "#:#" + e->quitMessage();
70 displayMsg(e, Message::NetsplitJoin, msg, QString(), e->channel());
73 void EventStringifier::processNetworkSplitQuit(NetworkSplitEvent *e) {
74 QString msg = e->users().join("#:#") + "#:#" + e->quitMessage();
75 displayMsg(e, Message::NetsplitQuit, msg, QString(), e->channel());
80 void EventStringifier::processIrcEventNumeric(IrcEventNumeric *e) {
81 //qDebug() << e->number();
83 // Welcome, status, info messages. Just display these.
84 case 1: case 2: case 3: case 4: case 5:
85 case 221: case 250: case 251: case 252: case 253: case 254: case 255: case 265: case 266:
87 displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
90 // Server error messages without param, just display them
91 case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
92 case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
93 case 431: // ERR_NONICKNAMEGIVEN
94 displayMsg(e, Message::Error, e->params().join(" "), e->prefix());
97 // Server error messages, display them in red. First param will be appended.
99 QString target = e->params().takeFirst();
100 displayMsg(e, Message::Error, e->params().join(" ") + " " + target, e->prefix(), target, Message::Redirected);
104 case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442: {
105 QString channelName = e->params().takeFirst();
106 displayMsg(e, Message::Error, e->params().join(" ") + " " + channelName, e->prefix());
110 // Server error messages which will be displayed with a colon between the first param and the rest
111 case 413: case 414: case 423: case 441: case 444: case 461: // FIXME see below for the 47x codes
112 case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
113 case 436: // ERR_NICKCOLLISION
115 QString p = e->params().takeFirst();
116 displayMsg(e, Message::Error, p + ": " + e->params().join(" "));
120 // Ignore these commands.
121 case 321: case 353: case 366: case 376:
125 case 900: case 903: case 904: case 905: case 906: case 907:
127 displayMsg(e, Message::Info, "CAP: " + e->params().join(""));
131 // Everything else will be marked in red, so we can add them somewhere.
134 // many nets define their own WHOIS fields. we fetch those not in need of special attention here:
135 displayMsg(e, Message::Server, tr("[Whois] ") + e->params().join(" "), e->prefix());
137 // FIXME figure out how/where to do this in the future
138 //if(coreSession()->ircListHelper()->requestInProgress(network()->networkId()))
139 // coreSession()->ircListHelper()->reportError(params.join(" "));
141 displayMsg(e, Message::Error, QString("%1 %2").arg(e->number(), 3, 10, QLatin1Char('0')).arg(e->params().join(" ")), e->prefix());
146 void EventStringifier::processIrcEventInvite(IrcEvent *e) {
147 displayMsg(e, Message::Invite, tr("%1 invited you to channel %2").arg(e->nick(), e->params().at(1)));
150 void EventStringifier::processIrcEventJoin(IrcEvent *e) {
151 if(e->testFlag(EventManager::Netsplit))
154 displayMsg(e, Message::Join, e->params()[0], e->prefix(), e->params()[0]);
157 void EventStringifier::earlyProcessIrcEventKick(IrcEvent *e) {
158 IrcUser *victim = e->network()->ircUser(e->params().at(1));
160 QString channel = e->params().at(0);
161 QString msg = victim->nick();
162 if(e->params().count() > 2)
163 msg += " " + e->params().at(2);
165 displayMsg(e, Message::Kick, msg, e->prefix(), channel);
169 void EventStringifier::processIrcEventMode(IrcEvent *e) {
170 if(e->network()->isChannelName(e->params().first())) {
172 displayMsg(e, Message::Mode, e->params().join(" "), e->prefix(), e->params().first());
176 displayMsg(e, Message::Mode, e->params().join(" "), e->prefix());
180 // this needs to be called before the ircuser is renamed!
181 void EventStringifier::earlyProcessIrcEventNick(IrcEvent *e) {
182 if(!checkParamCount(e, 1))
185 IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
187 qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
191 QString newnick = e->params().at(0);
192 QString oldnick = ircuser->nick();
194 QString sender = e->network()->isMyNick(oldnick) ? newnick : e->prefix();
195 foreach(const QString &channel, ircuser->channels())
196 displayMsg(e, Message::Nick, newnick, sender, channel);
199 void EventStringifier::earlyProcessIrcEventPart(IrcEvent *e) {
200 if(!checkParamCount(e, 1))
203 QString channel = e->params().at(0);
204 QString msg = e->params().count() > 1? e->params().at(1) : QString();
206 displayMsg(e, Message::Part, msg, e->prefix(), channel);
209 void EventStringifier::processIrcEventPong(IrcEvent *e) {
210 QString timestamp = e->params().at(1);
211 QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
212 if(!sendTime.isValid())
213 displayMsg(e, Message::Server, "PONG " + e->params().join(" "), e->prefix());
216 void EventStringifier::processIrcEventQuit(IrcEvent *e) {
217 if(e->testFlag(EventManager::Netsplit))
220 IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
224 foreach(const QString &channel, ircuser->channels())
225 displayMsg(e, Message::Quit, e->params().count()? e->params().first() : QString(), e->prefix(), channel);
228 void EventStringifier::processIrcEventTopic(IrcEvent *e) {
229 displayMsg(e, Message::Topic, tr("%1 has changed topic for %2 to: \"%3\"")
230 .arg(e->nick(), e->params().at(0), e->params().at(1)), QString(), e->params().at(0));
234 void EventStringifier::processIrcEvent005(IrcEvent *e) {
235 if(!e->params().last().contains(QRegExp("are supported (by|on) this server")))
236 displayMsg(e, Message::Error, tr("Received non-RFC-compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), e->prefix());
237 displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
240 /* RPL_AWAY - "<nick> :<away message>" */
241 void EventStringifier::processIrcEvent301(IrcEvent *e) {
242 QString nick = e->params().at(0);
243 QString awayMsg = e->params().at(1);
247 // FIXME: proper redirection needed
249 msg = tr("[Whois] ");
252 IrcUser *ircuser = e->network()->ircUser(nick);
254 int now = QDateTime::currentDateTime().toTime_t();
255 const int silenceTime = 60;
256 if(ircuser->lastAwayMessage() + silenceTime >= now)
258 ircuser->setLastAwayMessage(now);
262 displayMsg(e, Message::Server, msg + tr("%1 is away: \"%2\"").arg(nick, awayMsg), QString(), target);
265 /* RPL_UNAWAY - ":You are no longer marked as being away" */
266 void EventStringifier::processIrcEvent305(IrcEvent *e) {
267 displayMsg(e, Message::Server, tr("You are no longer marked as being away"));
270 /* RPL_NOWAWAY - ":You have been marked as being away" */
271 void EventStringifier::processIrcEvent306(IrcEvent *e) {
272 if(!e->network()->autoAwayActive())
273 displayMsg(e, Message::Server, tr("You have been marked as being away"));
278 Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
280 "<nick> :<away message>"
282 Replies 352 and 315 paired are used to answer a WHO message.
285 Replies 314 and 369 are responses to a WHOWAS message.
289 /* RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
290 void EventStringifier::processIrcEvent311(IrcEvent *e) {
293 const QString whoisUserString = tr("[Whois] %1 is %2 (%3)");
295 IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
297 displayMsg(e, Message::Server, whoisUserString.arg(ircuser->nick(), ircuser->hostmask(), ircuser->realName()));
299 QString host = QString("%1!%2@%3").arg(e->params().at(0), e->params().at(1), e->params().at(2));
300 displayMsg(e, Message::Server, whoisUserString.arg(e->params().at(0), host, e->params().last()));
304 /* RPL_WHOISSERVER - "<nick> <server> :<server info>" */
305 void EventStringifier::processIrcEvent312(IrcEvent *e) {
307 displayMsg(e, Message::Server, tr("[Whois] %1 is online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
309 displayMsg(e, Message::Server, tr("[Whowas] %1 was online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
312 /* RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
313 void EventStringifier::processIrcEvent314(IrcEvent *e) {
314 if(!checkParamCount(e, 3))
317 displayMsg(e, Message::Server, tr("[Whowas] %1 was %2@%3 (%4)").arg(e->params()[0], e->params()[1], e->params()[2], e->params().last()));
320 /* RPL_ENDOFWHO: "<name> :End of WHO list" */
321 void EventStringifier::processIrcEvent315(IrcEvent *e) {
322 QStringList p = e->params();
323 p.takeLast(); // should be "End of WHO list"
324 displayMsg(e, Message::Server, tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
327 /* RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
328 (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
329 void EventStringifier::processIrcEvent317(IrcEvent *e) {
330 int idleSecs = e->params()[1].toInt();
332 if(e->params().count() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
333 QDateTime loginTime = QDateTime::fromTime_t(e->params()[2].toInt());
334 displayMsg(e, Message::Server, tr("[Whois] %1 is logged in since %2").arg(e->params()[0], loginTime.toString()));
336 displayMsg(e, Message::Server, tr("[Whois] %1 is idling for %2 (since %3)")
337 .arg(e->params()[0], secondsToString(idleSecs), e->timestamp().toLocalTime().addSecs(-idleSecs).toString()));
340 /* RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
341 void EventStringifier::processIrcEvent318(IrcEvent *e) {
343 displayMsg(e, Message::Server, tr("[Whois] End of /WHOIS list"));
346 /* RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
347 void EventStringifier::processIrcEvent319(IrcEvent *e) {
348 if(!checkParamCount(e, 2))
351 QString nick = e->params().first();
355 foreach(QString channel, e->params().last().split(" ")) {
356 if(channel.startsWith("@"))
357 op.append(channel.remove(0,1));
358 else if(channel.startsWith("+"))
359 voice.append(channel.remove(0,1));
361 user.append(channel);
364 displayMsg(e, Message::Server, tr("[Whois] %1 is a user on channels: %2").arg(nick, user.join(" ")));
366 displayMsg(e, Message::Server, tr("[Whois] %1 has voice on channels: %2").arg(nick, voice.join(" ")));
368 displayMsg(e, Message::Server, tr("[Whois] %1 is an operator on channels: %2").arg(nick, op.join(" ")));
371 /* RPL_LIST - "<channel> <# visible> :<topic>" */
372 void EventStringifier::processIrcEvent322(IrcEvent *e) {
374 quint32 userCount = 0;
377 switch(e->params().count()) {
379 topic = e->params()[2];
381 userCount = e->params()[1].toUInt();
383 channelName = e->params()[0];
387 displayMsg(e, Message::Server, tr("Channel %1 has %2 users. Topic is: \"%3\"")
388 .arg(channelName).arg(userCount).arg(topic));
391 /* RPL_LISTEND ":End of LIST" */
392 void EventStringifier::processIrcEvent323(IrcEvent *e) {
393 displayMsg(e, Message::Server, tr("End of channel list"));
396 /* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
397 void EventStringifier::processIrcEvent324(IrcEvent *e) {
398 processIrcEventMode(e);
401 /* RPL_??? - "<channel> <homepage> */
402 void EventStringifier::processIrcEvent328(IrcEvent *e) {
403 if(!checkParamCount(e, 2))
406 QString channel = e->params()[0];
407 displayMsg(e, Message::Topic, tr("Homepage for %1 is %2").arg(channel, e->params()[1]), QString(), channel);
410 /* RPL_??? - "<channel> <creation time (unix)>" */
411 void EventStringifier::processIrcEvent329(IrcEvent *e) {
412 if(!checkParamCount(e, 2))
415 QString channel = e->params()[0];
416 uint unixtime = e->params()[1].toUInt();
418 qWarning() << Q_FUNC_INFO << "received invalid timestamp:" << e->params()[1];
421 QDateTime time = QDateTime::fromTime_t(unixtime);
422 displayMsg(e, Message::Topic, tr("Channel %1 created on %2").arg(channel, time.toString()), QString(), channel);
425 /* RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
426 void EventStringifier::processIrcEvent330(IrcEvent *e) {
427 if(e->params().count() < 3)
430 displayMsg(e, Message::Server, tr("[Whois] %1 is authed as %2").arg(e->params()[0], e->params()[1]));
434 void EventStringifier::processIrcEvent331(IrcEvent *e) {
435 QString channel = e->params().first();
436 displayMsg(e, Message::Topic, tr("No topic is set for %1.").arg(channel), QString(), channel);
440 void EventStringifier::processIrcEvent332(IrcEvent *e) {
441 QString channel = e->params().first();
442 displayMsg(e, Message::Topic, tr("Topic for %1 is \"%2\"").arg(channel, e->params()[1]), QString(), channel);
445 /* Topic set by... */
446 void EventStringifier::processIrcEvent333(IrcEvent *e) {
447 if(!checkParamCount(e, 3))
450 QString channel = e->params().first();
451 displayMsg(e, Message::Topic, tr("Topic set by %1 on %2")
452 .arg(e->params()[1], QDateTime::fromTime_t(e->params()[2].toInt()).toString()), QString(), channel);
455 /* RPL_INVITING - "<nick> <channel>*/
456 void EventStringifier::processIrcEvent341(IrcEvent *e) {
457 if(!checkParamCount(e, 2))
460 QString channel = e->params()[1];
461 displayMsg(e, Message::Server, tr("%1 has been invited to %2").arg(e->params().first(), channel), QString(), channel);
464 /* RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
465 ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
466 void EventStringifier::processIrcEvent352(IrcEvent *e) {
467 displayMsg(e, Message::Server, tr("[Who] %1").arg(e->params().join(" ")));
470 /* RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
471 void EventStringifier::processIrcEvent369(IrcEvent *e) {
472 displayMsg(e, Message::Server, tr("End of /WHOWAS"));
475 /* ERR_ERRONEUSNICKNAME */
476 void EventStringifier::processIrcEvent432(IrcEvent *e) {
477 displayMsg(e, Message::Error, tr("Nick %1 contains illegal characters").arg(e->params()[0]));
480 /* ERR_NICKNAMEINUSE */
481 void EventStringifier::processIrcEvent433(IrcEvent *e) {
482 displayMsg(e, Message::Error, tr("Nick already in use: %1").arg(e->params()[0]));
485 /* ERR_UNAVAILRESOURCE */
486 void EventStringifier::processIrcEvent437(IrcEvent *e) {
487 displayMsg(e, Message::Error, tr("Nick/channel is temporarily unavailable: %1").arg(e->params()[0]));
493 void EventStringifier::processIrcEvent(IrcEvent *e) {