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