Fix compiler warning (case value not in enum)
[quassel.git] / src / core / ctcpparser.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 "ctcpparser.h"
22
23 #include "coresession.h"
24 #include "ctcpevent.h"
25 #include "messageevent.h"
26
27 const QByteArray XDELIM = "\001";
28
29 CtcpParser::CtcpParser(CoreSession *coreSession, QObject *parent)
30   : QObject(parent),
31     _coreSession(coreSession)
32 {
33   QByteArray MQUOTE = QByteArray("\020");
34   _ctcpMDequoteHash[MQUOTE + '0'] = QByteArray(1, '\000');
35   _ctcpMDequoteHash[MQUOTE + 'n'] = QByteArray(1, '\n');
36   _ctcpMDequoteHash[MQUOTE + 'r'] = QByteArray(1, '\r');
37   _ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
38
39   QByteArray XQUOTE = QByteArray("\134");
40   _ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
41   _ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
42
43   connect(this, SIGNAL(newEvent(Event *)), _coreSession->eventManager(), SLOT(postEvent(Event *)));
44 }
45
46 void CtcpParser::displayMsg(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
47                             const QString &target, Message::Flags msgFlags) {
48   if(event->testFlag(EventManager::Silent))
49     return;
50
51   MessageEvent *msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
52   msgEvent->setTimestamp(event->timestamp());
53
54   emit newEvent(msgEvent);
55 }
56
57 QByteArray CtcpParser::lowLevelQuote(const QByteArray &message) {
58   QByteArray quotedMessage = message;
59
60   QHash<QByteArray, QByteArray> quoteHash = _ctcpMDequoteHash;
61   QByteArray MQUOTE = QByteArray("\020");
62   quoteHash.remove(MQUOTE + MQUOTE);
63   quotedMessage.replace(MQUOTE, MQUOTE + MQUOTE);
64
65   QHash<QByteArray, QByteArray>::const_iterator quoteIter = quoteHash.constBegin();
66   while(quoteIter != quoteHash.constEnd()) {
67     quotedMessage.replace(quoteIter.value(), quoteIter.key());
68     quoteIter++;
69   }
70   return quotedMessage;
71 }
72
73 QByteArray CtcpParser::lowLevelDequote(const QByteArray &message) {
74   QByteArray dequotedMessage;
75   QByteArray messagepart;
76   QHash<QByteArray, QByteArray>::iterator ctcpquote;
77
78   // copy dequote Message
79   for(int i = 0; i < message.size(); i++) {
80     messagepart = message.mid(i,1);
81     if(i+1 < message.size()) {
82       for(ctcpquote = _ctcpMDequoteHash.begin(); ctcpquote != _ctcpMDequoteHash.end(); ++ctcpquote) {
83         if(message.mid(i,2) == ctcpquote.key()) {
84           messagepart = ctcpquote.value();
85           ++i;
86           break;
87         }
88       }
89     }
90     dequotedMessage += messagepart;
91   }
92   return dequotedMessage;
93 }
94
95 QByteArray CtcpParser::xdelimQuote(const QByteArray &message) {
96   QByteArray quotedMessage = message;
97   QHash<QByteArray, QByteArray>::const_iterator quoteIter = _ctcpXDelimDequoteHash.constBegin();
98   while(quoteIter != _ctcpXDelimDequoteHash.constEnd()) {
99     quotedMessage.replace(quoteIter.value(), quoteIter.key());
100     quoteIter++;
101   }
102   return quotedMessage;
103 }
104
105 QByteArray CtcpParser::xdelimDequote(const QByteArray &message) {
106   QByteArray dequotedMessage;
107   QByteArray messagepart;
108   QHash<QByteArray, QByteArray>::iterator xdelimquote;
109
110   for(int i = 0; i < message.size(); i++) {
111     messagepart = message.mid(i,1);
112     if(i+1 < message.size()) {
113       for(xdelimquote = _ctcpXDelimDequoteHash.begin(); xdelimquote != _ctcpXDelimDequoteHash.end(); ++xdelimquote) {
114         if(message.mid(i,2) == xdelimquote.key()) {
115           messagepart = xdelimquote.value();
116           i++;
117           break;
118         }
119       }
120     }
121     dequotedMessage += messagepart;
122   }
123   return dequotedMessage;
124 }
125
126 void CtcpParser::processIrcEventRawNotice(IrcEventRawMessage *event) {
127   parse(event, Message::Notice);
128 }
129
130 void CtcpParser::processIrcEventRawPrivmsg(IrcEventRawMessage *event) {
131   parse(event, Message::Plain);
132 }
133
134 void CtcpParser::parse(IrcEventRawMessage *e, Message::Type messagetype) {
135   QByteArray ctcp;
136
137   //lowlevel message dequote
138   QByteArray dequotedMessage = lowLevelDequote(e->rawMessage());
139
140   CtcpEvent::CtcpType ctcptype = e->type() == EventManager::IrcEventRawNotice
141       ? CtcpEvent::Reply
142       : CtcpEvent::Query;
143
144   Message::Flags flags = (ctcptype == CtcpEvent::Reply && !e->network()->isChannelName(e->target()))
145                           ? Message::Redirected
146                           : Message::None;
147
148   QList<CtcpEvent *> ctcpEvents;
149   QUuid uuid; // needed to group all replies together
150
151   // extract tagged / extended data
152   int xdelimPos = -1;
153   int xdelimEndPos = -1;
154   int spacePos = -1;
155   while((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
156     if(xdelimPos > 0)
157       displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
158
159     xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
160     if(xdelimEndPos == -1) {
161       // no matching end delimiter found... treat rest of the message as ctcp
162       xdelimEndPos = dequotedMessage.count();
163     }
164     ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
165     dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
166
167     //dispatch the ctcp command
168     QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
169     QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
170
171     spacePos = ctcp.indexOf(' ');
172     if(spacePos != -1) {
173       ctcpcmd = targetDecode(e, ctcp.left(spacePos));
174       ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
175     } else {
176       ctcpcmd = targetDecode(e, ctcp);
177       ctcpparam = QString();
178     }
179
180     ctcpcmd = ctcpcmd.toUpper();
181
182     // we don't want to block /me messages by the CTCP ignore list
183     if(ctcpcmd == QLatin1String("ACTION") || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
184       if(uuid.isNull())
185         uuid = QUuid::createUuid();
186
187       CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
188                                        ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
189       ctcpEvents << event;
190     }
191   }
192   if(!ctcpEvents.isEmpty()) {
193     _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
194     CtcpEvent *flushEvent = new CtcpEvent(EventManager::CtcpEventFlush, e->network(), e->prefix(), e->target(),
195                                           ctcptype, "INVALID", QString(), e->timestamp(), uuid);
196     ctcpEvents << flushEvent;
197     foreach(CtcpEvent *event, ctcpEvents) {
198       emit newEvent(event);
199     }
200   }
201
202   if(!dequotedMessage.isEmpty())
203     displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
204 }
205
206 void CtcpParser::sendCtcpEvent(CtcpEvent *e) {
207   CoreNetwork *net = coreNetwork(e);
208   if(e->type() == EventManager::CtcpEvent) {
209     QByteArray quotedReply;
210     QString bufname = nickFromMask(e->prefix());
211     if(e->ctcpType() == CtcpEvent::Query && !e->reply().isNull()) {
212       if(_replies.contains(e->uuid()))
213         _replies[e->uuid()].replies << lowLevelQuote(pack(net->serverEncode(e->ctcpCmd()),
214                                                           net->userEncode(bufname, e->reply())));
215       else
216         // reply not caused by a request processed in here, so send it off immediately
217         reply(net, bufname, e->ctcpCmd(), e->reply());
218     }
219   } else if(e->type() == EventManager::CtcpEventFlush && _replies.contains(e->uuid())) {
220     CtcpReply reply = _replies.take(e->uuid());
221     if(reply.replies.count())
222       packedReply(net, reply.bufferName, reply.replies);
223   }
224 }
225
226 QByteArray CtcpParser::pack(const QByteArray &ctcpTag, const QByteArray &message) {
227   if(message.isEmpty())
228     return XDELIM + ctcpTag + XDELIM;
229
230   return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
231 }
232
233 void CtcpParser::query(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message) {
234   QList<QByteArray> params;
235   params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
236   net->putCmd("PRIVMSG", params);
237 }
238
239 void CtcpParser::reply(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message) {
240   QList<QByteArray> params;
241   params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
242   net->putCmd("NOTICE", params);
243 }
244
245 void CtcpParser::packedReply(CoreNetwork *net, const QString &bufname, const QList<QByteArray> &replies) {
246   QList<QByteArray> params;
247
248   int answerSize = 0;
249   for(int i = 0; i < replies.count(); i++) {
250     answerSize += replies.at(i).size();
251   }
252
253   QByteArray quotedReply(answerSize, 0);
254   int nextPos = 0;
255   QByteArray &reply = quotedReply;
256   for(int i = 0; i < replies.count(); i++) {
257     reply = replies.at(i);
258     quotedReply.replace(nextPos, reply.size(), reply);
259     nextPos += reply.size();
260   }
261
262   params << net->serverEncode(bufname) << quotedReply;
263   // FIXME user proper event
264   net->putCmd("NOTICE", params);
265 }