c301dbf989fa3516ae18800b594a9c28da3c6402
[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 "messageevent.h"
25
26 EventStringifier::EventStringifier(CoreSession *parent) : QObject(parent),
27   _coreSession(parent),
28   _whois(false)
29 {
30
31 }
32
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))
36     return;
37
38   MessageEvent *msgEvent = createMessageEvent(event, msgType, msg, sender, target, msgFlags);
39   sendMessageEvent(msgEvent);
40 }
41
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());
46   return msgEvent;
47 }
48
49 void EventStringifier::sendMessageEvent(MessageEvent *event) {
50   coreSession()->eventManager()->sendEvent(event);
51 }
52
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();
57     } else {
58       QString name = coreSession()->eventManager()->enumName(e->type());
59       qWarning() << qPrintable(name) << "requires" << minParams << "params, got:" << e->params();
60     }
61     e->stop();
62     return false;
63   }
64   return true;
65 }
66
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());
71 }
72
73 void EventStringifier::processNetworkSplitQuit(NetworkSplitEvent *e) {
74   QString msg = e->users().join("#:#") + "#:#" + e->quitMessage();
75   displayMsg(e, Message::NetsplitQuit, msg, QString(), e->channel());
76 }
77
78 /* End legacy */
79
80 void EventStringifier::processIrcEventNumeric(IrcEventNumeric *e) {
81   //qDebug() << e->number();
82   switch(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:
86   case 372: case 375:
87     displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
88     break;
89
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());
95     break;
96
97   // Server error messages, display them in red. First param will be appended.
98   case 401: {
99     QString target = e->params().takeFirst();
100     displayMsg(e, Message::Error, e->params().join(" ") + " " + target, e->prefix(), target, Message::Redirected);
101     break;
102   }
103
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());
107     break;
108   }
109
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
114   {
115     QString p = e->params().takeFirst();
116     displayMsg(e, Message::Error, p + ": " + e->params().join(" "));
117     break;
118   }
119
120   // Ignore these commands.
121   case 321: case 353: case 366: case 376:
122     break;
123
124   // CAP stuff
125   case 900: case 903: case 904: case 905: case 906: case 907:
126   {
127     displayMsg(e, Message::Info, "CAP: " + e->params().join(""));
128     break;
129   }
130
131   // Everything else will be marked in red, so we can add them somewhere.
132   default:
133     if(_whois) {
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());
136     } else {
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(" "));
140       //else
141         displayMsg(e, Message::Error, QString("%1 %2").arg(e->number(), 3, 10, QLatin1Char('0')).arg(e->params().join(" ")), e->prefix());
142     }
143   }
144 }
145
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)));
148 }
149
150 void EventStringifier::processIrcEventJoin(IrcEvent *e) {
151   if(e->testFlag(EventManager::Netsplit))
152     return;
153
154   displayMsg(e, Message::Join, e->params()[0], e->prefix(), e->params()[0]);
155 }
156
157 void EventStringifier::processIrcEventKick(IrcEvent *e) {
158   if(!checkParamCount(e, 2))
159     return;
160
161   IrcUser *victim = e->network()->ircUser(e->params().at(1));
162   if(victim) {
163     QString channel = e->params().at(0);
164     QString msg = victim->nick();
165     if(e->params().count() > 2)
166       msg += " " + e->params().at(2);
167
168     displayMsg(e, Message::Kick, msg, e->prefix(), channel);
169   }
170 }
171
172 void EventStringifier::processIrcEventMode(IrcEvent *e) {
173   if(e->network()->isChannelName(e->params().first())) {
174     // Channel Modes
175     displayMsg(e, Message::Mode, e->params().join(" "), e->prefix(), e->params().first());
176   } else {
177     // User Modes
178     // FIXME: redirect
179     displayMsg(e, Message::Mode, e->params().join(" "), e->prefix());
180   }
181 }
182
183 // this needs to be called before the ircuser is renamed!
184 void EventStringifier::processIrcEventNick(IrcEvent *e) {
185   if(!checkParamCount(e, 1))
186     return;
187
188   IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
189   if(!ircuser) {
190     qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
191     return;
192   }
193
194   QString newnick = e->params().at(0);
195   QString oldnick = ircuser->nick();
196
197   QString sender = e->network()->isMyNick(oldnick) ? newnick : e->prefix();
198   foreach(const QString &channel, ircuser->channels())
199     displayMsg(e, Message::Nick, newnick, sender, channel);
200 }
201
202 void EventStringifier::processIrcEventPart(IrcEvent *e) {
203   if(!checkParamCount(e, 1))
204     return;
205
206   QString channel = e->params().at(0);
207   QString msg = e->params().count() > 1? e->params().at(1) : QString();
208
209   displayMsg(e, Message::Part, msg, e->prefix(), channel);
210 }
211
212 void EventStringifier::processIrcEventPong(IrcEvent *e) {
213   QString timestamp = e->params().at(1);
214   QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
215   if(!sendTime.isValid())
216     displayMsg(e, Message::Server, "PONG " + e->params().join(" "), e->prefix());
217 }
218
219 void EventStringifier::processIrcEventQuit(IrcEvent *e) {
220   if(e->testFlag(EventManager::Netsplit))
221     return;
222
223   IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
224   if(!ircuser)
225     return;
226
227   foreach(const QString &channel, ircuser->channels())
228     displayMsg(e, Message::Quit, e->params().count()? e->params().first() : QString(), e->prefix(), channel);
229 }
230
231 void EventStringifier::processIrcEventTopic(IrcEvent *e) {
232   displayMsg(e, Message::Topic, tr("%1 has changed topic for %2 to: \"%3\"")
233              .arg(e->nick(), e->params().at(0), e->params().at(1)), QString(), e->params().at(0));
234 }
235
236 /* RPL_ISUPPORT */
237 void EventStringifier::processIrcEvent005(IrcEvent *e) {
238   if(!e->params().last().contains(QRegExp("are supported (by|on) this server")))
239     displayMsg(e, Message::Error, tr("Received non-RFC-compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), e->prefix());
240   displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
241 }
242
243 /* RPL_AWAY - "<nick> :<away message>" */
244 void EventStringifier::processIrcEvent301(IrcEvent *e) {
245   QString nick = e->params().at(0);
246   QString awayMsg = e->params().at(1);
247   QString msg, target;
248   bool send = true;
249
250   // FIXME: proper redirection needed
251   if(_whois) {
252     msg = tr("[Whois] ");
253   } else {
254     target = nick;
255     IrcUser *ircuser = e->network()->ircUser(nick);
256     if(ircuser) {
257       int now = QDateTime::currentDateTime().toTime_t();
258       const int silenceTime = 60;
259       if(ircuser->lastAwayMessage() + silenceTime >= now)
260         send = false;
261       ircuser->setLastAwayMessage(now);
262     }
263   }
264   if(send)
265     displayMsg(e, Message::Server, msg + tr("%1 is away: \"%2\"").arg(nick, awayMsg), QString(), target);
266 }
267
268 /* RPL_UNAWAY - ":You are no longer marked as being away" */
269 void EventStringifier::processIrcEvent305(IrcEvent *e) {
270   displayMsg(e, Message::Server, tr("You are no longer marked as being away"));
271 }
272
273 /* RPL_NOWAWAY - ":You have been marked as being away" */
274 void EventStringifier::processIrcEvent306(IrcEvent *e) {
275   if(!e->network()->autoAwayActive())
276     displayMsg(e, Message::Server, tr("You have been marked as being away"));
277 }
278
279 /*
280 WHOIS-Message:
281    Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
282   and 301 (RPL_AWAY)
283               "<nick> :<away message>"
284 WHO-Message:
285    Replies 352 and 315 paired are used to answer a WHO message.
286
287 WHOWAS-Message:
288    Replies 314 and 369 are responses to a WHOWAS message.
289
290 */
291
292 /*  RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
293 void EventStringifier::processIrcEvent311(IrcEvent *e) {
294   _whois = true;
295
296   const QString whoisUserString = tr("[Whois] %1 is %2 (%3)");
297
298   IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
299   if(ircuser)
300     displayMsg(e, Message::Server, whoisUserString.arg(ircuser->nick(), ircuser->hostmask(), ircuser->realName()));
301   else {
302     QString host = QString("%1!%2@%3").arg(e->params().at(0), e->params().at(1), e->params().at(2));
303     displayMsg(e, Message::Server, whoisUserString.arg(e->params().at(0), host, e->params().last()));
304   }
305 }
306
307 /*  RPL_WHOISSERVER -  "<nick> <server> :<server info>" */
308 void EventStringifier::processIrcEvent312(IrcEvent *e) {
309   if(_whois)
310     displayMsg(e, Message::Server, tr("[Whois] %1 is online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
311   else
312     displayMsg(e, Message::Server, tr("[Whowas] %1 was online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
313 }
314
315 /*  RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
316 void EventStringifier::processIrcEvent314(IrcEvent *e) {
317   if(!checkParamCount(e, 3))
318     return;
319
320   displayMsg(e, Message::Server, tr("[Whowas] %1 was %2@%3 (%4)").arg(e->params()[0], e->params()[1], e->params()[2], e->params().last()));
321 }
322
323 /*  RPL_ENDOFWHO: "<name> :End of WHO list" */
324 void EventStringifier::processIrcEvent315(IrcEvent *e) {
325   QStringList p = e->params();
326   p.takeLast(); // should be "End of WHO list"
327   displayMsg(e, Message::Server, tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
328 }
329
330 /*  RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
331    (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
332 void EventStringifier::processIrcEvent317(IrcEvent *e) {
333   int idleSecs = e->params()[1].toInt();
334
335   if(e->params().count() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
336     QDateTime loginTime = QDateTime::fromTime_t(e->params()[2].toInt());
337     displayMsg(e, Message::Server, tr("[Whois] %1 is logged in since %2").arg(e->params()[0], loginTime.toString()));
338   }
339   displayMsg(e, Message::Server, tr("[Whois] %1 is idling for %2 (since %3)")
340              .arg(e->params()[0], secondsToString(idleSecs), e->timestamp().toLocalTime().addSecs(-idleSecs).toString()));
341 }
342
343 /*  RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
344 void EventStringifier::processIrcEvent318(IrcEvent *e) {
345   _whois = false;
346   displayMsg(e, Message::Server, tr("[Whois] End of /WHOIS list"));
347 }
348
349 /*  RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
350 void EventStringifier::processIrcEvent319(IrcEvent *e) {
351   if(!checkParamCount(e, 2))
352     return;
353
354   QString nick = e->params().first();
355   QStringList op;
356   QStringList voice;
357   QStringList user;
358   foreach(QString channel, e->params().last().split(" ")) {
359     if(channel.startsWith("@"))
360        op.append(channel.remove(0,1));
361     else if(channel.startsWith("+"))
362       voice.append(channel.remove(0,1));
363     else
364       user.append(channel);
365   }
366   if(!user.isEmpty())
367     displayMsg(e, Message::Server, tr("[Whois] %1 is a user on channels: %2").arg(nick, user.join(" ")));
368   if(!voice.isEmpty())
369     displayMsg(e, Message::Server, tr("[Whois] %1 has voice on channels: %2").arg(nick, voice.join(" ")));
370   if(!op.isEmpty())
371     displayMsg(e, Message::Server, tr("[Whois] %1 is an operator on channels: %2").arg(nick, op.join(" ")));
372 }
373
374 /* RPL_LIST -  "<channel> <# visible> :<topic>" */
375 void EventStringifier::processIrcEvent322(IrcEvent *e) {
376   QString channelName;
377   quint32 userCount = 0;
378   QString topic;
379
380   switch(e->params().count()) {
381   case 3:
382     topic = e->params()[2];
383   case 2:
384     userCount = e->params()[1].toUInt();
385   case 1:
386     channelName = e->params()[0];
387   default:
388     break;
389   }
390   displayMsg(e, Message::Server, tr("Channel %1 has %2 users. Topic is: \"%3\"")
391              .arg(channelName).arg(userCount).arg(topic));
392 }
393
394 /* RPL_LISTEND ":End of LIST" */
395 void EventStringifier::processIrcEvent323(IrcEvent *e) {
396   displayMsg(e, Message::Server, tr("End of channel list"));
397 }
398
399 /* RPL_CHANNELMODEIS - "<channel> <mode> <mode params>" */
400 void EventStringifier::processIrcEvent324(IrcEvent *e) {
401   processIrcEventMode(e);
402 }
403
404 /* RPL_??? - "<channel> <homepage> */
405 void EventStringifier::processIrcEvent328(IrcEvent *e) {
406   if(!checkParamCount(e, 2))
407     return;
408
409   QString channel = e->params()[0];
410   displayMsg(e, Message::Topic, tr("Homepage for %1 is %2").arg(channel, e->params()[1]), QString(), channel);
411 }
412
413 /* RPL_??? - "<channel> <creation time (unix)>" */
414 void EventStringifier::processIrcEvent329(IrcEvent *e) {
415   if(!checkParamCount(e, 2))
416     return;
417
418   QString channel = e->params()[0];
419   uint unixtime = e->params()[1].toUInt();
420   if(!unixtime) {
421     qWarning() << Q_FUNC_INFO << "received invalid timestamp:" << e->params()[1];
422     return;
423   }
424   QDateTime time = QDateTime::fromTime_t(unixtime);
425   displayMsg(e, Message::Topic, tr("Channel %1 created on %2").arg(channel, time.toString()), QString(), channel);
426 }
427
428 /*  RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
429 void EventStringifier::processIrcEvent330(IrcEvent *e) {
430   if(e->params().count() < 3)
431     return;
432
433   displayMsg(e, Message::Server, tr("[Whois] %1 is authed as %2").arg(e->params()[0], e->params()[1]));
434 }
435
436 /* RPL_NOTOPIC */
437 void EventStringifier::processIrcEvent331(IrcEvent *e) {
438   QString channel = e->params().first();
439   displayMsg(e, Message::Topic, tr("No topic is set for %1.").arg(channel), QString(), channel);
440 }
441
442 /* RPL_TOPIC */
443 void EventStringifier::processIrcEvent332(IrcEvent *e) {
444   QString channel = e->params().first();
445   displayMsg(e, Message::Topic, tr("Topic for %1 is \"%2\"").arg(channel, e->params()[1]), QString(), channel);
446 }
447
448 /* Topic set by... */
449 void EventStringifier::processIrcEvent333(IrcEvent *e) {
450   if(!checkParamCount(e, 3))
451     return;
452
453   QString channel = e->params().first();
454   displayMsg(e, Message::Topic, tr("Topic set by %1 on %2")
455              .arg(e->params()[1], QDateTime::fromTime_t(e->params()[2].toInt()).toString()), QString(), channel);
456 }
457
458 /* RPL_INVITING - "<nick> <channel>*/
459 void EventStringifier::processIrcEvent341(IrcEvent *e) {
460   if(!checkParamCount(e, 2))
461     return;
462
463   QString channel = e->params()[1];
464   displayMsg(e, Message::Server, tr("%1 has been invited to %2").arg(e->params().first(), channel), QString(), channel);
465 }
466
467 /*  RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
468               ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
469 void EventStringifier::processIrcEvent352(IrcEvent *e) {
470   displayMsg(e, Message::Server, tr("[Who] %1").arg(e->params().join(" ")));
471 }
472
473 /*  RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
474 void EventStringifier::processIrcEvent369(IrcEvent *e) {
475   displayMsg(e, Message::Server, tr("End of /WHOWAS"));
476 }
477
478 /* ERR_ERRONEUSNICKNAME */
479 void EventStringifier::processIrcEvent432(IrcEvent *e) {
480   displayMsg(e, Message::Error, tr("Nick %1 contains illegal characters").arg(e->params()[0]));
481 }
482
483 /* ERR_NICKNAMEINUSE */
484 void EventStringifier::processIrcEvent433(IrcEvent *e) {
485   displayMsg(e, Message::Error, tr("Nick already in use: %1").arg(e->params()[0]));
486 }
487
488 /* ERR_UNAVAILRESOURCE */
489 void EventStringifier::processIrcEvent437(IrcEvent *e) {
490   displayMsg(e, Message::Error, tr("Nick/channel is temporarily unavailable: %1").arg(e->params()[0]));
491 }
492
493 // template
494 /*
495
496 void EventStringifier::processIrcEvent(IrcEvent *e) {
497
498 }
499
500 */