1 /***************************************************************************
2 * Copyright (C) 2005-2012 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "ctcpparser.h"
23 #include "coresession.h"
24 #include "ctcpevent.h"
25 #include "messageevent.h"
27 const QByteArray XDELIM = "\001";
29 CtcpParser::CtcpParser(CoreSession *coreSession, QObject *parent)
31 _coreSession(coreSession)
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;
39 QByteArray XQUOTE = QByteArray("\134");
40 _ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
41 _ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
43 connect(this, SIGNAL(newEvent(Event *)), _coreSession->eventManager(), SLOT(postEvent(Event *)));
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))
51 MessageEvent *msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
52 msgEvent->setTimestamp(event->timestamp());
54 emit newEvent(msgEvent);
57 QByteArray CtcpParser::lowLevelQuote(const QByteArray &message) {
58 QByteArray quotedMessage = message;
60 QHash<QByteArray, QByteArray> quoteHash = _ctcpMDequoteHash;
61 QByteArray MQUOTE = QByteArray("\020");
62 quoteHash.remove(MQUOTE + MQUOTE);
63 quotedMessage.replace(MQUOTE, MQUOTE + MQUOTE);
65 QHash<QByteArray, QByteArray>::const_iterator quoteIter = quoteHash.constBegin();
66 while(quoteIter != quoteHash.constEnd()) {
67 quotedMessage.replace(quoteIter.value(), quoteIter.key());
73 QByteArray CtcpParser::lowLevelDequote(const QByteArray &message) {
74 QByteArray dequotedMessage;
75 QByteArray messagepart;
76 QHash<QByteArray, QByteArray>::iterator ctcpquote;
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();
90 dequotedMessage += messagepart;
92 return dequotedMessage;
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());
102 return quotedMessage;
105 QByteArray CtcpParser::xdelimDequote(const QByteArray &message) {
106 QByteArray dequotedMessage;
107 QByteArray messagepart;
108 QHash<QByteArray, QByteArray>::iterator xdelimquote;
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();
121 dequotedMessage += messagepart;
123 return dequotedMessage;
126 void CtcpParser::processIrcEventRawNotice(IrcEventRawMessage *event) {
127 parse(event, Message::Notice);
130 void CtcpParser::processIrcEventRawPrivmsg(IrcEventRawMessage *event) {
131 parse(event, Message::Plain);
134 void CtcpParser::parse(IrcEventRawMessage *e, Message::Type messagetype) {
137 //lowlevel message dequote
138 QByteArray dequotedMessage = lowLevelDequote(e->rawMessage());
140 CtcpEvent::CtcpType ctcptype = e->type() == EventManager::IrcEventRawNotice
144 Message::Flags flags = (ctcptype == CtcpEvent::Reply && !e->network()->isChannelName(e->target()))
145 ? Message::Redirected
148 QList<CtcpEvent *> ctcpEvents;
149 QUuid uuid; // needed to group all replies together
151 // extract tagged / extended data
153 int xdelimEndPos = -1;
155 while((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
157 displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
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();
164 ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
165 dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
167 //dispatch the ctcp command
168 QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
169 QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
171 spacePos = ctcp.indexOf(' ');
173 ctcpcmd = targetDecode(e, ctcp.left(spacePos));
174 ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
176 ctcpcmd = targetDecode(e, ctcp);
177 ctcpparam = QString();
180 ctcpcmd = ctcpcmd.toUpper();
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)) {
185 uuid = QUuid::createUuid();
187 CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
188 ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
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);
202 if(!dequotedMessage.isEmpty())
203 displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
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())));
216 // reply not caused by a request processed in here, so send it off immediately
217 reply(net, bufname, e->ctcpCmd(), e->reply());
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);
226 QByteArray CtcpParser::pack(const QByteArray &ctcpTag, const QByteArray &message) {
227 if(message.isEmpty())
228 return XDELIM + ctcpTag + XDELIM;
230 return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
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);
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);
245 void CtcpParser::packedReply(CoreNetwork *net, const QString &bufname, const QList<QByteArray> &replies) {
246 QList<QByteArray> params;
249 for(int i = 0; i < replies.count(); i++) {
250 answerSize += replies.at(i).size();
253 QByteArray quotedReply;
254 quotedReply.reserve(answerSize);
255 for(int i = 0; i < replies.count(); i++) {
256 quotedReply.append(replies.at(i));
259 params << net->serverEncode(bufname) << quotedReply;
260 // FIXME user proper event
261 net->putCmd("NOTICE", params);