added new user input command 'showkey"
[quassel.git] / src / core / eventstringifier.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 void EventStringifier::displayMsg(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
35                                   const QString &target, Message::Flags msgFlags) {
36   if(event->flags().testFlag(EventManager::Silent))
37     return;
38
39   MessageEvent *msgEvent = createMessageEvent(event, msgType, msg, sender, target, msgFlags);
40   //sendMessageEvent(msgEvent);
41   emit newMessageEvent(msgEvent);
42 }
43
44 MessageEvent *EventStringifier::createMessageEvent(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
45                         const QString &target, Message::Flags msgFlags) {
46   MessageEvent *msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
47   msgEvent->setTimestamp(event->timestamp());
48   return msgEvent;
49 }
50
51 bool EventStringifier::checkParamCount(IrcEvent *e, int minParams) {
52   if(e->params().count() < minParams) {
53     if(e->type() == EventManager::IrcEventNumeric) {
54       qWarning() << "Command " << static_cast<IrcEventNumeric *>(e)->number() << " requires " << minParams << "params, got: " << e->params();
55     } else {
56       QString name = coreSession()->eventManager()->enumName(e->type());
57       qWarning() << qPrintable(name) << "requires" << minParams << "params, got:" << e->params();
58     }
59     e->stop();
60     return false;
61   }
62   return true;
63 }
64
65 /* These are only for legacy reasons; remove as soon as we handle NetworkSplitEvents properly */
66 void EventStringifier::processNetworkSplitJoin(NetworkSplitEvent *e) {
67   QString msg = e->users().join("#:#") + "#:#" + e->quitMessage();
68   displayMsg(e, Message::NetsplitJoin, msg, QString(), e->channel());
69 }
70
71 void EventStringifier::processNetworkSplitQuit(NetworkSplitEvent *e) {
72   QString msg = e->users().join("#:#") + "#:#" + e->quitMessage();
73   displayMsg(e, Message::NetsplitQuit, msg, QString(), e->channel());
74 }
75
76 /* End legacy */
77
78 void EventStringifier::processIrcEventNumeric(IrcEventNumeric *e) {
79   //qDebug() << e->number();
80   switch(e->number()) {
81   // Welcome, status, info messages. Just display these.
82   case 1: case 2: case 3: case 4: case 5:
83   case 221: case 250: case 251: case 252: case 253: case 254: case 255: case 265: case 266:
84   case 372: case 375:
85     displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
86     break;
87
88   // Server error messages without param, just display them
89   case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
90   case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
91   case 431: // ERR_NONICKNAMEGIVEN
92     displayMsg(e, Message::Error, e->params().join(" "), e->prefix());
93     break;
94
95   // Server error messages, display them in red. First param will be appended.
96   case 401: {
97     QString target = e->params().takeFirst();
98     displayMsg(e, Message::Error, e->params().join(" ") + " " + target, e->prefix(), target, Message::Redirected);
99     break;
100   }
101
102   case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442: {
103     QString channelName = e->params().takeFirst();
104     displayMsg(e, Message::Error, e->params().join(" ") + " " + channelName, e->prefix());
105     break;
106   }
107
108   // Server error messages which will be displayed with a colon between the first param and the rest
109   case 413: case 414: case 423: case 441: case 444: case 461:  // FIXME see below for the 47x codes
110   case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
111   case 436: // ERR_NICKCOLLISION
112   {
113     QString p = e->params().takeFirst();
114     displayMsg(e, Message::Error, p + ": " + e->params().join(" "));
115     break;
116   }
117
118   // Ignore these commands.
119   case 321: case 353: case 366: case 376:
120     break;
121
122   // CAP stuff
123   case 900: case 903: case 904: case 905: case 906: case 907:
124   {
125     displayMsg(e, Message::Info, "CAP: " + e->params().join(""));
126     break;
127   }
128
129   // Everything else will be marked in red, so we can add them somewhere.
130   default:
131     if(_whois) {
132       // many nets define their own WHOIS fields. we fetch those not in need of special attention here:
133       displayMsg(e, Message::Server, tr("[Whois] ") + e->params().join(" "), e->prefix());
134     } else {
135       // FIXME figure out how/where to do this in the future
136       //if(coreSession()->ircListHelper()->requestInProgress(network()->networkId()))
137       //  coreSession()->ircListHelper()->reportError(params.join(" "));
138       //else
139         displayMsg(e, Message::Error, QString("%1 %2").arg(e->number(), 3, 10, QLatin1Char('0')).arg(e->params().join(" ")), e->prefix());
140     }
141   }
142 }
143
144 void EventStringifier::processIrcEventInvite(IrcEvent *e) {
145   displayMsg(e, Message::Invite, tr("%1 invited you to channel %2").arg(e->nick(), e->params().at(1)));
146 }
147
148 void EventStringifier::processIrcEventJoin(IrcEvent *e) {
149   if(e->testFlag(EventManager::Netsplit))
150     return;
151
152   displayMsg(e, Message::Join, e->params()[0], e->prefix(), e->params()[0]);
153 }
154
155 void EventStringifier::processIrcEventKick(IrcEvent *e) {
156   if(!checkParamCount(e, 2))
157     return;
158
159   IrcUser *victim = e->network()->ircUser(e->params().at(1));
160   if(victim) {
161     QString channel = e->params().at(0);
162     QString msg = victim->nick();
163     if(e->params().count() > 2)
164       msg += " " + e->params().at(2);
165
166     displayMsg(e, Message::Kick, msg, e->prefix(), channel);
167   }
168 }
169
170 void EventStringifier::processIrcEventMode(IrcEvent *e) {
171   if(e->network()->isChannelName(e->params().first())) {
172     // Channel Modes
173     displayMsg(e, Message::Mode, e->params().join(" "), e->prefix(), e->params().first());
174   } else {
175     // User Modes
176     // FIXME: redirect
177     displayMsg(e, Message::Mode, e->params().join(" "), e->prefix());
178   }
179 }
180
181 // this needs to be called before the ircuser is renamed!
182 void EventStringifier::processIrcEventNick(IrcEvent *e) {
183   if(!checkParamCount(e, 1))
184     return;
185
186   IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
187   if(!ircuser) {
188     qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
189     return;
190   }
191
192   QString newnick = e->params().at(0);
193   QString oldnick = ircuser->nick();
194
195   QString sender = e->network()->isMyNick(oldnick) ? newnick : e->prefix();
196   foreach(const QString &channel, ircuser->channels())
197     displayMsg(e, Message::Nick, newnick, sender, channel);
198 }
199
200 void EventStringifier::processIrcEventPart(IrcEvent *e) {
201   if(!checkParamCount(e, 1))
202     return;
203
204   QString channel = e->params().at(0);
205   QString msg = e->params().count() > 1? e->params().at(1) : QString();
206
207   displayMsg(e, Message::Part, msg, e->prefix(), channel);
208 }
209
210 void EventStringifier::processIrcEventPong(IrcEvent *e) {
211   QString timestamp = e->params().at(1);
212   QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
213   if(!sendTime.isValid())
214     displayMsg(e, Message::Server, "PONG " + e->params().join(" "), e->prefix());
215 }
216
217 void EventStringifier::processIrcEventQuit(IrcEvent *e) {
218   if(e->testFlag(EventManager::Netsplit))
219     return;
220
221   IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
222   if(!ircuser)
223     return;
224
225   foreach(const QString &channel, ircuser->channels())
226     displayMsg(e, Message::Quit, e->params().count()? e->params().first() : QString(), e->prefix(), channel);
227 }
228
229 void EventStringifier::processIrcEventTopic(IrcEvent *e) {
230   displayMsg(e, Message::Topic, tr("%1 has changed topic for %2 to: \"%3\"")
231              .arg(e->nick(), e->params().at(0), e->params().at(1)), QString(), e->params().at(0));
232 }
233
234 /* RPL_ISUPPORT */
235 void EventStringifier::processIrcEvent005(IrcEvent *e) {
236   if(!e->params().last().contains(QRegExp("are supported (by|on) this server")))
237     displayMsg(e, Message::Error, tr("Received non-RFC-compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), e->prefix());
238   displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
239 }
240
241 /* RPL_AWAY - "<nick> :<away message>" */
242 void EventStringifier::processIrcEvent301(IrcEvent *e) {
243   QString nick = e->params().at(0);
244   QString awayMsg = e->params().at(1);
245   QString msg, target;
246   bool send = true;
247
248   // FIXME: proper redirection needed
249   if(_whois) {
250     msg = tr("[Whois] ");
251   } else {
252     target = nick;
253     IrcUser *ircuser = e->network()->ircUser(nick);
254     if(ircuser) {
255       int now = QDateTime::currentDateTime().toTime_t();
256       const int silenceTime = 60;
257       if(ircuser->lastAwayMessage() + silenceTime >= now)
258         send = false;
259       ircuser->setLastAwayMessage(now);
260     }
261   }
262   if(send)
263     displayMsg(e, Message::Server, msg + tr("%1 is away: \"%2\"").arg(nick, awayMsg), QString(), target);
264 }
265
266 /* RPL_UNAWAY - ":You are no longer marked as being away" */
267 void EventStringifier::processIrcEvent305(IrcEvent *e) {
268   displayMsg(e, Message::Server, tr("You are no longer marked as being away"));
269 }
270
271 /* RPL_NOWAWAY - ":You have been marked as being away" */
272 void EventStringifier::processIrcEvent306(IrcEvent *e) {
273   if(!e->network()->autoAwayActive())
274     displayMsg(e, Message::Server, tr("You have been marked as being away"));
275 }
276
277 /*
278 WHOIS-Message:
279    Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
280   and 301 (RPL_AWAY)
281               "<nick> :<away message>"
282 WHO-Message:
283    Replies 352 and 315 paired are used to answer a WHO message.
284
285 WHOWAS-Message:
286    Replies 314 and 369 are responses to a WHOWAS message.
287
288 */
289
290 /*  RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
291 void EventStringifier::processIrcEvent311(IrcEvent *e) {
292   _whois = true;
293
294   const QString whoisUserString = tr("[Whois] %1 is %2 (%3)");
295
296   IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
297   if(ircuser)
298     displayMsg(e, Message::Server, whoisUserString.arg(ircuser->nick(), ircuser->hostmask(), ircuser->realName()));
299   else {
300     QString host = QString("%1!%2@%3").arg(e->params().at(0), e->params().at(1), e->params().at(2));
301     displayMsg(e, Message::Server, whoisUserString.arg(e->params().at(0), host, e->params().last()));
302   }
303 }
304
305 /*  RPL_WHOISSERVER -  "<nick> <server> :<server info>" */
306 void EventStringifier::processIrcEvent312(IrcEvent *e) {
307   if(_whois)
308     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   else
310     displayMsg(e, Message::Server, tr("[Whowas] %1 was online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
311 }
312
313 /*  RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
314 void EventStringifier::processIrcEvent314(IrcEvent *e) {
315   if(!checkParamCount(e, 3))
316     return;
317
318   displayMsg(e, Message::Server, tr("[Whowas] %1 was %2@%3 (%4)").arg(e->params()[0], e->params()[1], e->params()[2], e->params().last()));
319 }
320
321 /*  RPL_ENDOFWHO: "<name> :End of WHO list" */
322 void EventStringifier::processIrcEvent315(IrcEvent *e) {
323   QStringList p = e->params();
324   p.takeLast(); // should be "End of WHO list"
325   displayMsg(e, Message::Server, tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
326 }
327
328 /*  RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
329    (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
330 void EventStringifier::processIrcEvent317(IrcEvent *e) {
331   int idleSecs = e->params()[1].toInt();
332
333   if(e->params().count() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
334     QDateTime loginTime = QDateTime::fromTime_t(e->params()[2].toInt());
335     displayMsg(e, Message::Server, tr("[Whois] %1 is logged in since %2").arg(e->params()[0], loginTime.toString()));
336   }
337   displayMsg(e, Message::Server, tr("[Whois] %1 is idling for %2 (since %3)")
338              .arg(e->params()[0], secondsToString(idleSecs), e->timestamp().toLocalTime().addSecs(-idleSecs).toString()));
339 }
340
341 /*  RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
342 void EventStringifier::processIrcEvent318(IrcEvent *e) {
343   _whois = false;
344   displayMsg(e, Message::Server, tr("[Whois] End of /WHOIS list"));
345 }
346
347 /*  RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
348 void EventStringifier::processIrcEvent319(IrcEvent *e) {
349   if(!checkParamCount(e, 2))
350     return;
351
352   QString nick = e->params().first();
353   QStringList op;
354   QStringList voice;
355   QStringList user;
356   foreach(QString channel, e->params().last().split(" ")) {
357     if(channel.startsWith("@"))
358        op.append(channel.remove(0,1));
359     else if(channel.startsWith("+"))
360       voice.append(channel.remove(0,1));
361     else
362       user.append(channel);
363   }
364   if(!user.isEmpty())
365     displayMsg(e, Message::Server, tr("[Whois] %1 is a user on channels: %2").arg(nick, user.join(" ")));
366   if(!voice.isEmpty())
367     displayMsg(e, Message::Server, tr("[Whois] %1 has voice on channels: %2").arg(nick, voice.join(" ")));
368   if(!op.isEmpty())
369     displayMsg(e, Message::Server, tr("[Whois] %1 is an operator on channels: %2").arg(nick, op.join(" ")));
370 }
371
372 /* RPL_LIST -  "<channel> <# visible> :<topic>" */
373 void EventStringifier::processIrcEvent322(IrcEvent *e) {
374   QString channelName;
375   quint32 userCount = 0;
376   QString topic;
377
378   switch(e->params().count()) {
379   case 3:
380     topic = e->params()[2];
381   case 2:
382     userCount = e->params()[1].toUInt();
383   case 1:
384     channelName = e->params()[0];
385   default:
386     break;
387   }
388   displayMsg(e, Message::Server, tr("Channel %1 has %2 users. Topic is: \"%3\"")
389              .arg(channelName).arg(userCount).arg(topic));
390 }
391
392 /* RPL_LISTEND ":End of LIST" */
393 void EventStringifier::processIrcEvent323(IrcEvent *e) {
394   displayMsg(e, Message::Server, tr("End of channel list"));
395 }
396
397 /* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
398 void EventStringifier::processIrcEvent324(IrcEvent *e) {
399   processIrcEventMode(e);
400 }
401
402 /* RPL_??? - "<channel> <homepage> */
403 void EventStringifier::processIrcEvent328(IrcEvent *e) {
404   if(!checkParamCount(e, 2))
405     return;
406
407   QString channel = e->params()[0];
408   displayMsg(e, Message::Topic, tr("Homepage for %1 is %2").arg(channel, e->params()[1]), QString(), channel);
409 }
410
411 /* RPL_??? - "<channel> <creation time (unix)>" */
412 void EventStringifier::processIrcEvent329(IrcEvent *e) {
413   if(!checkParamCount(e, 2))
414     return;
415
416   QString channel = e->params()[0];
417   uint unixtime = e->params()[1].toUInt();
418   if(!unixtime) {
419     qWarning() << Q_FUNC_INFO << "received invalid timestamp:" << e->params()[1];
420     return;
421   }
422   QDateTime time = QDateTime::fromTime_t(unixtime);
423   displayMsg(e, Message::Topic, tr("Channel %1 created on %2").arg(channel, time.toString()), QString(), channel);
424 }
425
426 /*  RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
427 void EventStringifier::processIrcEvent330(IrcEvent *e) {
428   if(e->params().count() < 3)
429     return;
430
431   displayMsg(e, Message::Server, tr("[Whois] %1 is authed as %2").arg(e->params()[0], e->params()[1]));
432 }
433
434 /* RPL_NOTOPIC */
435 void EventStringifier::processIrcEvent331(IrcEvent *e) {
436   QString channel = e->params().first();
437   displayMsg(e, Message::Topic, tr("No topic is set for %1.").arg(channel), QString(), channel);
438 }
439
440 /* RPL_TOPIC */
441 void EventStringifier::processIrcEvent332(IrcEvent *e) {
442   QString channel = e->params().first();
443   displayMsg(e, Message::Topic, tr("Topic for %1 is \"%2\"").arg(channel, e->params()[1]), QString(), channel);
444 }
445
446 /* Topic set by... */
447 void EventStringifier::processIrcEvent333(IrcEvent *e) {
448   if(!checkParamCount(e, 3))
449     return;
450
451   QString channel = e->params().first();
452   displayMsg(e, Message::Topic, tr("Topic set by %1 on %2")
453              .arg(e->params()[1], QDateTime::fromTime_t(e->params()[2].toInt()).toString()), QString(), channel);
454 }
455
456 /* RPL_INVITING - "<nick> <channel>*/
457 void EventStringifier::processIrcEvent341(IrcEvent *e) {
458   if(!checkParamCount(e, 2))
459     return;
460
461   QString channel = e->params()[1];
462   displayMsg(e, Message::Server, tr("%1 has been invited to %2").arg(e->params().first(), channel), QString(), channel);
463 }
464
465 /*  RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
466               ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
467 void EventStringifier::processIrcEvent352(IrcEvent *e) {
468   displayMsg(e, Message::Server, tr("[Who] %1").arg(e->params().join(" ")));
469 }
470
471 /*  RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
472 void EventStringifier::processIrcEvent369(IrcEvent *e) {
473   displayMsg(e, Message::Server, tr("End of /WHOWAS"));
474 }
475
476 /* ERR_ERRONEUSNICKNAME */
477 void EventStringifier::processIrcEvent432(IrcEvent *e) {
478   displayMsg(e, Message::Error, tr("Nick %1 contains illegal characters").arg(e->params()[0]));
479 }
480
481 /* ERR_NICKNAMEINUSE */
482 void EventStringifier::processIrcEvent433(IrcEvent *e) {
483   displayMsg(e, Message::Error, tr("Nick already in use: %1").arg(e->params()[0]));
484 }
485
486 /* ERR_UNAVAILRESOURCE */
487 void EventStringifier::processIrcEvent437(IrcEvent *e) {
488   displayMsg(e, Message::Error, tr("Nick/channel is temporarily unavailable: %1").arg(e->params()[0]));
489 }
490
491 // template
492 /*
493
494 void EventStringifier::processIrcEvent(IrcEvent *e) {
495
496 }
497
498 */
499
500 /*******************************/
501 /******** CTCP HANDLING ********/
502 /*******************************/
503
504 void EventStringifier::processCtcpEvent(CtcpEvent *e) {
505   if(e->type() != EventManager::CtcpEvent)
506     return;
507
508   if(e->testFlag(EventManager::Self)) {
509     displayMsg(e, Message::Action, tr("sending CTCP-%1 request to %2").arg(e->ctcpCmd(), e->target()), e->network()->myNick());
510     return;
511   }
512
513   handle(e->ctcpCmd(), Q_ARG(CtcpEvent *, e));
514 }
515
516 void EventStringifier::defaultHandler(const QString &ctcpCmd, CtcpEvent *e) {
517   Q_UNUSED(ctcpCmd);
518   if(e->ctcpType() == CtcpEvent::Query) {
519     QString unknown;
520     if(e->reply().isNull()) // all known core-side handlers (except for ACTION) set a reply!
521       //: Optional "unknown" in "Received unknown CTCP-FOO request by bar"
522       unknown = tr("unknown") + ' ';
523     displayMsg(e, Message::Server, tr("Received %1CTCP-%2 request by %3").arg(unknown, e->ctcpCmd(), e->prefix()));
524     return;
525   }
526   displayMsg(e, Message::Server, tr("Received CTCP-%1 answer from %2: %3").arg(e->ctcpCmd(), nickFromMask(e->prefix()), e->param()));
527 }
528
529 void EventStringifier::handleCtcpAction(CtcpEvent *e) {
530   displayMsg(e, Message::Action, e->param(), e->prefix(), e->target());
531 }
532
533 void EventStringifier::handleCtcpPing(CtcpEvent *e) {
534   if(e->ctcpType() == CtcpEvent::Query)
535     defaultHandler(e->ctcpCmd(), e);
536   else {
537     displayMsg(e, Message::Server, tr("Received CTCP-PING answer from %1 with %2 seconds round trip time")
538                .arg(nickFromMask(e->prefix())).arg(QDateTime::fromTime_t(e->param().toInt()).secsTo(e->timestamp())));
539   }
540 }