96cb0546d65fdecd1ae159b22b3451f43d156120
[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 void EventStringifier::processIrcEventNumeric(IrcEventNumeric *e) {
54   //qDebug() << e->number();
55   switch(e->number()) {
56   // Welcome, status, info messages. Just display these.
57   case 1: case 2: case 3: case 4: case 5:
58   case 221: case 250: case 251: case 252: case 253: case 254: case 255: case 265: case 266:
59   case 372: case 375:
60     displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
61     break;
62
63   // Server error messages without param, just display them
64   case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
65   case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
66   case 431: // ERR_NONICKNAMEGIVEN
67     displayMsg(e, Message::Error, e->params().join(" "), e->prefix());
68     break;
69
70   // Server error messages, display them in red. First param will be appended.
71   case 401: {
72     QString target = e->params().takeFirst();
73     displayMsg(e, Message::Error, e->params().join(" ") + " " + target, e->prefix(), target, Message::Redirected);
74     break;
75   }
76
77   case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442: {
78     QString channelName = e->params().takeFirst();
79     displayMsg(e, Message::Error, e->params().join(" ") + " " + channelName, e->prefix());
80     break;
81   }
82
83   // Server error messages which will be displayed with a colon between the first param and the rest
84   case 413: case 414: case 423: case 441: case 444: case 461:  // FIXME see below for the 47x codes
85   case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
86   case 436: // ERR_NICKCOLLISION
87   {
88     QString p = e->params().takeFirst();
89     displayMsg(e, Message::Error, p + ": " + e->params().join(" "));
90     break;
91   }
92
93   // Ignore these commands.
94   case 321: case 366: case 376:
95     break;
96
97   // CAP stuff
98   case 903: case 904: case 905: case 906: case 907:
99   {
100     displayMsg(e, Message::Info, "CAP: " + e->params().join(""));
101     break;
102   }
103
104   // Everything else will be marked in red, so we can add them somewhere.
105   default:
106     if(_whois) {
107       // many nets define their own WHOIS fields. we fetch those not in need of special attention here:
108       displayMsg(e, Message::Server, tr("[Whois] ") + e->params().join(" "), e->prefix());
109     } else {
110       // FIXME figure out how/where to do this in the future
111       //if(coreSession()->ircListHelper()->requestInProgress(network()->networkId()))
112       //  coreSession()->ircListHelper()->reportError(params.join(" "));
113       //else
114         displayMsg(e, Message::Error, QString("%1 %2").arg(e->number(), 3, 10, QLatin1Char('0')).arg(e->params().join(" ")), e->prefix());
115     }
116   }
117 }
118
119 void EventStringifier::processIrcEventInvite(IrcEvent *e) {
120   displayMsg(e, Message::Invite, tr("%1 invited you to channel %2").arg(e->nick(), e->params().at(1)));
121 }
122
123 void EventStringifier::earlyProcessIrcEventKick(IrcEvent *e) {
124   IrcUser *victim = e->network()->ircUser(e->params().at(1));
125   if(victim) {
126     QString channel = e->params().at(0);
127     QString msg = victim->nick();
128     if(e->params().count() > 2)
129       msg += " " + e->params().at(2);
130
131     displayMsg(e, Message::Kick, msg, e->prefix(), channel);
132   }
133 }
134
135 // this needs to be called before the ircuser is renamed!
136 void EventStringifier::earlyProcessIrcEventNick(IrcEvent *e) {
137   if(e->params().count() < 1)
138     return;
139
140   IrcUser *ircuser = e->network()->updateNickFromMask(e->prefix());
141   if(!ircuser) {
142     qWarning() << Q_FUNC_INFO << "Unknown IrcUser!";
143     return;
144   }
145
146   QString newnick = e->params().at(0);
147   QString oldnick = ircuser->nick();
148
149   QString sender = e->network()->isMyNick(oldnick) ? newnick : e->prefix();
150   foreach(const QString &channel, ircuser->channels())
151     displayMsg(e, Message::Nick, newnick, sender, channel);
152 }
153
154 void EventStringifier::earlyProcessIrcEventPart(IrcEvent *e) {
155   if(e->params().count() < 1)
156     return;
157
158   QString channel = e->params().at(0);
159   QString msg = e->params().count() > 1? e->params().at(1) : QString();
160
161   displayMsg(e, Message::Part, msg, e->prefix(), channel);
162 }
163
164 void EventStringifier::processIrcEventPong(IrcEvent *e) {
165   QString timestamp = e->params().at(1);
166   QTime sendTime = QTime::fromString(timestamp, "hh:mm:ss.zzz");
167   if(!sendTime.isValid())
168     displayMsg(e, Message::Server, "PONG " + e->params().join(" "), e->prefix());
169 }
170
171 void EventStringifier::processIrcEventTopic(IrcEvent *e) {
172   displayMsg(e, Message::Topic, tr("%1 has changed topic for %2 to: \"%3\"")
173              .arg(e->nick(), e->params().at(0), e->params().at(1)), QString(), e->params().at(0));
174 }
175
176 void EventStringifier::processIrcEvent301(IrcEvent *e) {
177   QString nick = e->params().at(0);
178   QString awayMsg = e->params().at(1);
179   QString msg, target;
180   bool send = true;
181
182   // FIXME: proper redirection needed
183   if(_whois) {
184     msg = tr("[Whois] ");
185   } else {
186     target = nick;
187     IrcUser *ircuser = e->network()->ircUser(nick);
188     if(ircuser) {
189       int now = QDateTime::currentDateTime().toTime_t();
190       const int silenceTime = 60;
191       if(ircuser->lastAwayMessage() + silenceTime >= now)
192         send = false;
193       ircuser->setLastAwayMessage(now);
194     }
195   }
196   if(send)
197     displayMsg(e, Message::Server, msg + tr("%1 is away: \"%2\"").arg(nick, awayMsg), QString(), target);
198 }
199
200 /* RPL_UNAWAY */
201 void EventStringifier::processIrcEvent305(IrcEvent *e) {
202   displayMsg(e, Message::Server, tr("You are no longer marked as being away"));
203 }
204
205 /* RPL_NOWAWAY */
206 void EventStringifier::processIrcEvent306(IrcEvent *e) {
207   if(!e->network()->autoAwayActive())
208     displayMsg(e, Message::Server, tr("You have been marked as being away"));
209 }
210
211 /*
212 WHOIS-Message:
213    Replies 311 - 313, 317 - 319 are all replies generated in response to a WHOIS message.
214   and 301 (RPL_AWAY)
215               "<nick> :<away message>"
216 WHO-Message:
217    Replies 352 and 315 paired are used to answer a WHO message.
218
219 WHOWAS-Message:
220    Replies 314 and 369 are responses to a WHOWAS message.
221
222 */
223
224 /*  RPL_WHOISUSER - "<nick> <user> <host> * :<real name>" */
225 void EventStringifier::processIrcEvent311(IrcEvent *e) {
226   _whois = true;
227
228   const QString whoisUserString = tr("[Whois] %1 is %2 (%3)");
229
230   IrcUser *ircuser = e->network()->ircUser(e->params().at(0));
231   if(ircuser)
232     displayMsg(e, Message::Server, whoisUserString.arg(ircuser->nick(), ircuser->hostmask(), ircuser->realName()));
233   else {
234     QString host = QString("%1!%2@%3").arg(e->params().at(0), e->params().at(1), e->params().at(2));
235     displayMsg(e, Message::Server, whoisUserString.arg(e->params().at(0), host, e->params().last()));
236   }
237 }
238
239 /*  RPL_WHOISSERVER -  "<nick> <server> :<server info>" */
240 void EventStringifier::processIrcEvent312(IrcEvent *e) {
241   if(_whois)
242     displayMsg(e, Message::Server, tr("[Whois] %1 is online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
243   else
244     displayMsg(e, Message::Server, tr("[Whowas] %1 was online via %2 (%3)").arg(e->params().at(0), e->params().at(1), e->params().last()));
245 }
246
247 /*  RPL_WHOWASUSER - "<nick> <user> <host> * :<real name>" */
248 void EventStringifier::processIrcEvent314(IrcEvent *e) {
249   if(e->params().count() < 3)
250     return;
251
252   displayMsg(e, Message::Server, tr("[Whowas] %1 was %2@%3 (%4)").arg(e->params()[0], e->params()[1], e->params()[2], e->params().last()));
253 }
254
255 /*  RPL_ENDOFWHO: "<name> :End of WHO list" */
256 void EventStringifier::processIrcEvent315(IrcEvent *e) {
257   QStringList p = e->params();
258   p.takeLast(); // should be "End of WHO list"
259   displayMsg(e, Message::Server, tr("[Who] End of /WHO list for %1").arg(p.join(" ")));
260 }
261
262 /*  RPL_WHOISIDLE - "<nick> <integer> :seconds idle"
263    (real life: "<nick> <integer> <integer> :seconds idle, signon time) */
264 void EventStringifier::processIrcEvent317(IrcEvent *e) {
265   int idleSecs = e->params()[1].toInt();
266
267   if(e->params().count() > 3) { // if we have more then 3 params we have the above mentioned "real life" situation
268     QDateTime loginTime = QDateTime::fromTime_t(e->params()[2].toInt());
269     displayMsg(e, Message::Server, tr("[Whois] %1 is logged in since %2").arg(e->params()[0], loginTime.toString()));
270   }
271   displayMsg(e, Message::Server, tr("[Whois] %1 is idling for %2 (since %3)")
272              .arg(e->params()[0], secondsToString(idleSecs), e->timestamp().toLocalTime().addSecs(-idleSecs).toString()));
273 }
274
275 /*  RPL_ENDOFWHOIS - "<nick> :End of WHOIS list" */
276 void EventStringifier::processIrcEvent318(IrcEvent *e) {
277   _whois = false;
278   displayMsg(e, Message::Server, tr("[Whois] End of /WHOIS list"));
279 }
280
281 /*  RPL_WHOISCHANNELS - "<nick> :*( ( "@" / "+" ) <channel> " " )" */
282 void EventStringifier::processIrcEvent319(IrcEvent *e) {
283   if(e->params().count() < 2)
284     return;
285
286   QString nick = e->params().first();
287   QStringList op;
288   QStringList voice;
289   QStringList user;
290   foreach(QString channel, e->params().last().split(" ")) {
291     if(channel.startsWith("@"))
292        op.append(channel.remove(0,1));
293     else if(channel.startsWith("+"))
294       voice.append(channel.remove(0,1));
295     else
296       user.append(channel);
297   }
298   if(!user.isEmpty())
299     displayMsg(e, Message::Server, tr("[Whois] %1 is a user on channels: %2").arg(nick, user.join(" ")));
300   if(!voice.isEmpty())
301     displayMsg(e, Message::Server, tr("[Whois] %1 has voice on channels: %2").arg(nick, voice.join(" ")));
302   if(!op.isEmpty())
303     displayMsg(e, Message::Server, tr("[Whois] %1 is an operator on channels: %2").arg(nick, op.join(" ")));
304 }
305
306 /*  RPL_WHOISACCOUNT: "<nick> <account> :is authed as */
307 void EventStringifier::processIrcEvent330(IrcEvent *e) {
308   if(e->params().count() < 3)
309     return;
310
311   displayMsg(e, Message::Server, tr("[Whois] %1 is authed as %2").arg(e->params()[0], e->params()[1]));
312 }
313
314 // template
315 /*
316
317 void EventStringifier::processIrcEvent(IrcEvent *e) {
318
319 }
320
321 */