44528e53e1bd42aff2a5edbfdc3f2f279457e2da
[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   MessageEvent *msgEvent = createMessageEvent(event, msgType, msg, sender, target, msgFlags);
36   sendMessageEvent(msgEvent);
37 }
38
39 MessageEvent *EventStringifier::createMessageEvent(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
40                         const QString &target, Message::Flags msgFlags) {
41   MessageEvent *msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
42   return msgEvent;
43 }
44
45 void EventStringifier::sendMessageEvent(MessageEvent *event) {
46   coreSession()->eventManager()->sendEvent(event);
47 }
48
49 void EventStringifier::processIrcEventNumeric(IrcEventNumeric *e) {
50   //qDebug() << e->number();
51   switch(e->number()) {
52   // Welcome, status, info messages. Just display these.
53   case 2: case 3: case 4: case 5: case 251: case 252: case 253: case 254: case 255: case 372: case 375:
54     displayMsg(e, Message::Server, e->params().join(" "), e->prefix());
55     break;
56
57   // Server error messages without param, just display them
58   case 409: case 411: case 412: case 422: case 424: case 445: case 446: case 451: case 462:
59   case 463: case 464: case 465: case 466: case 472: case 481: case 483: case 485: case 491: case 501: case 502:
60   case 431: // ERR_NONICKNAMEGIVEN
61     displayMsg(e, Message::Error, e->params().join(" "), e->prefix());
62     break;
63
64   // Server error messages, display them in red. First param will be appended.
65   case 401: {
66     QString target = e->params().takeFirst();
67     displayMsg(e, Message::Error, e->params().join(" ") + " " + target, e->prefix(), target, Message::Redirected);
68     break;
69   }
70
71   case 402: case 403: case 404: case 406: case 408: case 415: case 421: case 442: {
72     QString channelName = e->params().takeFirst();
73     displayMsg(e, Message::Error, e->params().join(" ") + " " + channelName, e->prefix());
74     break;
75   }
76
77   // Server error messages which will be displayed with a colon between the first param and the rest
78   case 413: case 414: case 423: case 441: case 444: case 461:  // FIXME see below for the 47x codes
79   case 467: case 471: case 473: case 474: case 475: case 476: case 477: case 478: case 482:
80   case 436: // ERR_NICKCOLLISION
81   {
82     QString p = e->params().takeFirst();
83     displayMsg(e, Message::Error, p + ": " + e->params().join(" "));
84     break;
85   }
86
87   // Ignore these commands.
88   case 321: case 366: case 376:
89     break;
90
91   // CAP stuff
92   case 903: case 904: case 905: case 906: case 907:
93   {
94     displayMsg(e, Message::Info, "CAP: " + e->params().join(""));
95     break;
96   }
97
98   // Everything else will be marked in red, so we can add them somewhere.
99   default:
100     if(_whois) {
101       // many nets define their own WHOIS fields. we fetch those not in need of special attention here:
102       displayMsg(e, Message::Server, "[Whois] " + e->params().join(" "), e->prefix());
103     } else {
104       // FIXME figure out how/where to do this in the future
105       //if(coreSession()->ircListHelper()->requestInProgress(network()->networkId()))
106       //  coreSession()->ircListHelper()->reportError(params.join(" "));
107       //else
108         displayMsg(e, Message::Error, QString("%1 %2").arg(e->number(), 3, 10, QLatin1Char('0')).arg(e->params().join(" ")), e->prefix());
109     }
110   }
111 }