Event backend porting
[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 void EventStringifier::processIrcEventNumeric(IrcEventNumeric *e) {
68   //qDebug() << e->number();
69   switch(e->number()) {
70   // Welcome, status, info messages. Just display these.
71   case 1: case 2: case 3: case 4: case 5:
72   case 221: case 250: case 251: case 252: case 253: case 254: case 255: case 265: case 266:
73   case 372: case 375:
74     displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
75     break;
76
77   // Server error messages without param, just display them
78   case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
79   case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
80   case 431: // ERR_NONICKNAMEGIVEN
81     displayMsg(e, Message::Error, e->params().join(" "), e->prefix());
82     break;
83
84   // Server error messages, display them in red. First param will be appended.
85   case 401: {
86     QString target = e->params().takeFirst();
87     displayMsg(e, Message::Error, e->params().join(" ") + " " + target, e->prefix(), target, Message::Redirected);
88     break;
89   }
90
91   case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442: {
92     QString channelName = e->params().takeFirst();
93     displayMsg(e, Message::Error, e->params().join(" ") + " " + channelName, e->prefix());
94     break;
95   }
96
97   // Server error messages which will be displayed with a colon between the first param and the rest
98   case 413: case 414: case 423: case 441: case 444: case 461:  // FIXME see below for the 47x codes
99   case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
100   case 436: // ERR_NICKCOLLISION
101   {
102     QString p = e->params().takeFirst();
103     displayMsg(e, Message::Error, p + ": " + e->params().join(" "));
104     break;
105   }
106
107   // Ignore these commands.
108   case 321: case 353: case 366: case 376:
109     break;
110
111   // CAP stuff
112   case 900: case 903: case 904: case 905: case 906: case 907:
113   {
114     displayMsg(e, Message::Info, "CAP: " + e->params().join(""));
115     break;
116   }
117
118   // Everything else will be marked in red, so we can add them somewhere.
119   default:
120     if(_whois) {
121       // many nets define their own WHOIS fields. we fetch those not in need of special attention here:
122       displayMsg(e, Message::Server, tr("[Whois] ") + e->params().join(" "), e->prefix());
123     } else {
124       // FIXME figure out how/where to do this in the future
125       //if(coreSession()->ircListHelper()->requestInProgress(network()->networkId()))
126       //  coreSession()->ircListHelper()->reportError(params.join(" "));
127       //else
128         displayMsg(e, Message::Error, QString("%1 %2").arg(e->number(), 3, 10, QLatin1Char('0')).arg(e->params().join(" ")), e->prefix());
129     }
130   }
131 }
132
133 void EventStringifier::processIrcEventInvite(IrcEvent *e) {
134   displayMsg(e, Message::Invite, tr("%1 invited you to channel %2").arg(e->nick(), e->params().at(1)));
135 }
136
137 void EventStringifier::earlyProcessIrcEventKick(IrcEvent *e) {
138   IrcUser *victim = e->network()->ircUser(e->params().at(1));
139   if(victim) {
140     QString channel = e->params().at(0);
141     QString msg = victim->nick();
142     if(e->params().count() > 2)
143       msg += " " + e->params().at(2);
144
145     displayMsg(e, Message::Kick, msg, e->prefix(), channel);
146   }
147 }
148
149 // this needs to be called before the ircuser is renamed!
150 void EventStringifier::earlyProcessIrcEventNick(IrcEvent *e) {
151   if(!checkParamCount(e, 1))
152     return;
153
154   IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
155   if(!ircuser) {
156     qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
157     return;
158   }
159
160   QString newnick = e->params().at(0);
161   QString oldnick = ircuser->nick();
162
163   QString sender = e->network()->isMyNick(oldnick) ? newnick : e->prefix();
164   foreach(const QString &channel, ircuser->channels())
165     displayMsg(e, Message::Nick, newnick, sender, channel);
166 }
167
168 void EventStringifier::earlyProcessIrcEventPart(IrcEvent *e) {
169   if(!checkParamCount(e, 1))
170     return;
171
172   QString channel = e->params().at(0);
173   QString msg = e->params().count() > 1? e->params().at(1) : QString();
174
175   displayMsg(e, Message::Part, msg, e->prefix(), channel);
176 }
177
178 void EventStringifier::processIrcEventPong(IrcEvent *e) {
179   QString timestamp = e->params().at(1);
180   QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
181   if(!sendTime.isValid())
182     displayMsg(e, Message::Server, "PONG " + e->params().join(" "), e->prefix());
183 }
184
185 void EventStringifier::processIrcEventTopic(IrcEvent *e) {
186   displayMsg(e, Message::Topic, tr("%1 has changed topic for %2 to: \"%3\"")
187              .arg(e->nick(), e->params().at(0), e->params().at(1)), QString(), e->params().at(0));
188 }
189
190 /* RPL_ISUPPORT */
191 void EventStringifier::processIrcEvent005(IrcEvent *e) {
192   if(!e->params().last().contains(QRegExp("are supported (by|on) this server")))
193     displayMsg(e, Message::Error, tr("Received non-RFC-compliant RPL_ISUPPORT: this can lead to unexpected behavior!"), e->prefix());
194   displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
195 }
196
197 /* RPL_AWAY - "<nick> :<away message>" */
198 void EventStringifier::processIrcEvent301(IrcEvent *e) {
199   QString nick = e->params().at(0);
200   QString awayMsg = e->params().at(1);
201   QString msg, target;
202   bool send = true;
203
204   // FIXME: proper redirection needed
205   if(_whois) {
206     msg = tr("[Whois] ");
207   } else {
208     target = nick;
209     IrcUser *ircuser = e->network()->ircUser(nick);
210     if(ircuser) {
211       int now = QDateTime::currentDateTime().toTime_t();
212       const int silenceTime = 60;
213       if(ircuser->lastAwayMessage() + silenceTime >= now)
214         send = false;
215       ircuser->setLastAwayMessage(now);
216     }
217   }
218   if(send)
219     displayMsg(e, Message::Server, msg + tr("%1 is away: \"%2\"").arg(nick, awayMsg), QString(), target);
220 }
221
222 /* RPL_UNAWAY - ":You are no longer marked as being away" */
223 void EventStringifier::processIrcEvent305(IrcEvent *e) {
224   displayMsg(e, Message::Server, tr("You are no longer marked as being away"));
225 }
226
227 /* RPL_NOWAWAY - ":You have been marked as being away" */
228 void EventStringifier::processIrcEvent306(IrcEvent *e) {
229   if(!e->network()->autoAwayActive())
230     displayMsg(e, Message::Server, tr("You have been marked as being away"));
231 }
232
233 /*
234 WHOIS-Message:
235    Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
236   and 301 (RPL_AWAY)
237               "<nick> :<away message>"
238 WHO-Message:
239    Replies 352 and 315 paired are used to answer a WHO message.
240
241 WHOWAS-Message:
242    Replies 314 and 369 are responses to a WHOWAS message.
243
244 */
245
246 /*  RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
247 void EventStringifier::processIrcEvent311(IrcEvent *e) {
248   _whois = true;
249
250   const QString whoisUserString = tr("[Whois] %1 is %2 (%3)");
251
252   IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
253   if(ircuser)
254     displayMsg(e, Message::Server, whoisUserString.arg(ircuser->nick(), ircuser->hostmask(), ircuser->realName()));
255   else {
256     QString host = QString("%1!%2@%3").arg(e->params().at(0), e->params().at(1), e->params().at(2));
257     displayMsg(e, Message::Server, whoisUserString.arg(e->params().at(0), host, e->params().last()));
258   }
259 }
260
261 /*  RPL_WHOISSERVER -  "<nick> <server> :<server info>" */
262 void EventStringifier::processIrcEvent312(IrcEvent *e) {
263   if(_whois)
264     displayMsg(e, Message::Server, tr("[Whois] %1 is online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
265   else
266     displayMsg(e, Message::Server, tr("[Whowas] %1 was online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
267 }
268
269 /*  RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
270 void EventStringifier::processIrcEvent314(IrcEvent *e) {
271   if(!checkParamCount(e, 3))
272     return;
273
274   displayMsg(e, Message::Server, tr("[Whowas] %1 was %2@%3 (%4)").arg(e->params()[0], e->params()[1], e->params()[2], e->params().last()));
275 }
276
277 /*  RPL_ENDOFWHO: "<name> :End of WHO list" */
278 void EventStringifier::processIrcEvent315(IrcEvent *e) {
279   QStringList p = e->params();
280   p.takeLast(); // should be "End of WHO list"
281   displayMsg(e, Message::Server, tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
282 }
283
284 /*  RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
285    (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
286 void EventStringifier::processIrcEvent317(IrcEvent *e) {
287   int idleSecs = e->params()[1].toInt();
288
289   if(e->params().count() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
290     QDateTime loginTime = QDateTime::fromTime_t(e->params()[2].toInt());
291     displayMsg(e, Message::Server, tr("[Whois] %1 is logged in since %2").arg(e->params()[0], loginTime.toString()));
292   }
293   displayMsg(e, Message::Server, tr("[Whois] %1 is idling for %2 (since %3)")
294              .arg(e->params()[0], secondsToString(idleSecs), e->timestamp().toLocalTime().addSecs(-idleSecs).toString()));
295 }
296
297 /*  RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
298 void EventStringifier::processIrcEvent318(IrcEvent *e) {
299   _whois = false;
300   displayMsg(e, Message::Server, tr("[Whois] End of /WHOIS list"));
301 }
302
303 /*  RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
304 void EventStringifier::processIrcEvent319(IrcEvent *e) {
305   if(!checkParamCount(e, 2))
306     return;
307
308   QString nick = e->params().first();
309   QStringList op;
310   QStringList voice;
311   QStringList user;
312   foreach(QString channel, e->params().last().split(" ")) {
313     if(channel.startsWith("@"))
314        op.append(channel.remove(0,1));
315     else if(channel.startsWith("+"))
316       voice.append(channel.remove(0,1));
317     else
318       user.append(channel);
319   }
320   if(!user.isEmpty())
321     displayMsg(e, Message::Server, tr("[Whois] %1 is a user on channels: %2").arg(nick, user.join(" ")));
322   if(!voice.isEmpty())
323     displayMsg(e, Message::Server, tr("[Whois] %1 has voice on channels: %2").arg(nick, voice.join(" ")));
324   if(!op.isEmpty())
325     displayMsg(e, Message::Server, tr("[Whois] %1 is an operator on channels: %2").arg(nick, op.join(" ")));
326 }
327
328 /* RPL_LIST -  "<channel> <# visible> :<topic>" */
329 void EventStringifier::processIrcEvent322(IrcEvent *e) {
330   QString channelName;
331   quint32 userCount = 0;
332   QString topic;
333
334   switch(e->params().count()) {
335   case 3:
336     topic = e->params()[2];
337   case 2:
338     userCount = e->params()[1].toUInt();
339   case 1:
340     channelName = e->params()[0];
341   default:
342     break;
343   }
344   displayMsg(e, Message::Server, tr("Channel %1 has %2 users. Topic is: \"%3\"")
345              .arg(channelName).arg(userCount).arg(topic));
346 }
347
348 /* RPL_LISTEND ":End of LIST" */
349 void EventStringifier::processIrcEvent323(IrcEvent *e) {
350   displayMsg(e, Message::Server, tr("End of channel list"));
351 }
352
353 /* RPL_??? - "<channel> <homepage> */
354 void EventStringifier::processIrcEvent328(IrcEvent *e) {
355   if(!checkParamCount(e, 2))
356     return;
357
358   QString channel = e->params()[0];
359   displayMsg(e, Message::Topic, tr("Homepage for %1 is %2").arg(channel, e->params()[1]), QString(), channel);
360 }
361
362 /* RPL_??? - "<channel> <creation time (unix)>" */
363 void EventStringifier::processIrcEvent329(IrcEvent *e) {
364   if(!checkParamCount(e, 2))
365     return;
366
367   QString channel = e->params()[0];
368   uint unixtime = e->params()[1].toUInt();
369   if(!unixtime) {
370     qWarning() << Q_FUNC_INFO << "received invalid timestamp:" << e->params()[1];
371     return;
372   }
373   QDateTime time = QDateTime::fromTime_t(unixtime);
374   displayMsg(e, Message::Topic, tr("Channel %1 created on %2").arg(channel, time.toString()), QString(), channel);
375 }
376
377 /*  RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
378 void EventStringifier::processIrcEvent330(IrcEvent *e) {
379   if(e->params().count() < 3)
380     return;
381
382   displayMsg(e, Message::Server, tr("[Whois] %1 is authed as %2").arg(e->params()[0], e->params()[1]));
383 }
384
385 /* RPL_NOTOPIC */
386 void EventStringifier::processIrcEvent331(IrcEvent *e) {
387   QString channel = e->params().first();
388   displayMsg(e, Message::Topic, tr("No topic is set for %1.").arg(channel), QString(), channel);
389 }
390
391 /* RPL_TOPIC */
392 void EventStringifier::processIrcEvent332(IrcEvent *e) {
393   QString channel = e->params().first();
394   displayMsg(e, Message::Topic, tr("Topic for %1 is \"%2\"").arg(channel, e->params()[1]), QString(), channel);
395 }
396
397 /* Topic set by... */
398 void EventStringifier::processIrcEvent333(IrcEvent *e) {
399   if(!checkParamCount(e, 3))
400     return;
401
402   QString channel = e->params().first();
403   displayMsg(e, Message::Topic, tr("Topic set by %1 on %2")
404              .arg(e->params()[1], QDateTime::fromTime_t(e->params()[2].toInt()).toString()), QString(), channel);
405 }
406
407 /* RPL_INVITING - "<nick> <channel>*/
408 void EventStringifier::processIrcEvent341(IrcEvent *e) {
409   if(!checkParamCount(e, 2))
410     return;
411
412   QString channel = e->params()[1];
413   displayMsg(e, Message::Server, tr("%1 has been invited to %2").arg(e->params().first(), channel), QString(), channel);
414 }
415
416 /*  RPL_WHOREPLY: "<channel> <user> <host> <server> <nick>
417               ( "H" / "G" > ["*"] [ ( "@" / "+" ) ] :<hopcount> <real name>" */
418 void EventStringifier::processIrcEvent352(IrcEvent *e) {
419   displayMsg(e, Message::Server, tr("[Who] %1").arg(e->params().join(" ")));
420 }
421
422 /*  RPL_ENDOFWHOWAS - "<nick> :End of WHOWAS" */
423 void EventStringifier::processIrcEvent369(IrcEvent *e) {
424   displayMsg(e, Message::Server, tr("End of /WHOWAS"));
425 }
426
427 /* ERR_ERRONEUSNICKNAME */
428 void EventStringifier::processIrcEvent432(IrcEvent *e) {
429   displayMsg(e, Message::Error, tr("Nick %1 contains illegal characters").arg(e->params()[0]));
430 }
431
432 /* ERR_NICKNAMEINUSE */
433 void EventStringifier::processIrcEvent433(IrcEvent *e) {
434   displayMsg(e, Message::Error, tr("Nick already in use: %1").arg(e->params()[0]));
435 }
436
437 /* ERR_UNAVAILRESOURCE */
438 void EventStringifier::processIrcEvent437(IrcEvent *e) {
439   displayMsg(e, Message::Error, tr("Nick/channel is temporarily unavailable: %1").arg(e->params()[0]));
440 }
441
442 // template
443 /*
444
445 void EventStringifier::processIrcEvent(IrcEvent *e) {
446
447 }
448
449 */