1 /***************************************************************************
2 * Copyright (C) 2005-2015 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "ctcpparser.h"
23 #include "corenetworkconfig.h"
24 #include "coresession.h"
25 #include "ctcpevent.h"
26 #include "messageevent.h"
27 #include "coreuserinputhandler.h"
29 const QByteArray XDELIM = "\001";
31 CtcpParser::CtcpParser(CoreSession *coreSession, QObject *parent)
33 _coreSession(coreSession)
35 QByteArray MQUOTE = QByteArray("\020");
36 _ctcpMDequoteHash[MQUOTE + '0'] = QByteArray(1, '\000');
37 _ctcpMDequoteHash[MQUOTE + 'n'] = QByteArray(1, '\n');
38 _ctcpMDequoteHash[MQUOTE + 'r'] = QByteArray(1, '\r');
39 _ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
41 setStandardCtcp(_coreSession->networkConfig()->standardCtcp());
43 connect(_coreSession->networkConfig(), SIGNAL(standardCtcpSet(bool)), this, SLOT(setStandardCtcp(bool)));
44 connect(this, SIGNAL(newEvent(Event *)), _coreSession->eventManager(), SLOT(postEvent(Event *)));
48 void CtcpParser::setStandardCtcp(bool enabled)
50 QByteArray XQUOTE = QByteArray("\134");
52 _ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
54 _ctcpXDelimDequoteHash.remove(XQUOTE + XQUOTE);
55 _ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
59 void CtcpParser::displayMsg(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
60 const QString &target, Message::Flags msgFlags)
62 if (event->testFlag(EventManager::Silent))
65 MessageEvent *msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
66 msgEvent->setTimestamp(event->timestamp());
68 emit newEvent(msgEvent);
72 QByteArray CtcpParser::lowLevelQuote(const QByteArray &message)
74 QByteArray quotedMessage = message;
76 QHash<QByteArray, QByteArray> quoteHash = _ctcpMDequoteHash;
77 QByteArray MQUOTE = QByteArray("\020");
78 quoteHash.remove(MQUOTE + MQUOTE);
79 quotedMessage.replace(MQUOTE, MQUOTE + MQUOTE);
81 QHash<QByteArray, QByteArray>::const_iterator quoteIter = quoteHash.constBegin();
82 while (quoteIter != quoteHash.constEnd()) {
83 quotedMessage.replace(quoteIter.value(), quoteIter.key());
90 QByteArray CtcpParser::lowLevelDequote(const QByteArray &message)
92 QByteArray dequotedMessage;
93 QByteArray messagepart;
94 QHash<QByteArray, QByteArray>::iterator ctcpquote;
96 // copy dequote Message
97 for (int i = 0; i < message.size(); i++) {
98 messagepart = message.mid(i, 1);
99 if (i+1 < message.size()) {
100 for (ctcpquote = _ctcpMDequoteHash.begin(); ctcpquote != _ctcpMDequoteHash.end(); ++ctcpquote) {
101 if (message.mid(i, 2) == ctcpquote.key()) {
102 messagepart = ctcpquote.value();
108 dequotedMessage += messagepart;
110 return dequotedMessage;
114 QByteArray CtcpParser::xdelimQuote(const QByteArray &message)
116 QByteArray quotedMessage = message;
117 QHash<QByteArray, QByteArray>::const_iterator quoteIter = _ctcpXDelimDequoteHash.constBegin();
118 while (quoteIter != _ctcpXDelimDequoteHash.constEnd()) {
119 quotedMessage.replace(quoteIter.value(), quoteIter.key());
122 return quotedMessage;
126 QByteArray CtcpParser::xdelimDequote(const QByteArray &message)
128 QByteArray dequotedMessage;
129 QByteArray messagepart;
130 QHash<QByteArray, QByteArray>::iterator xdelimquote;
132 for (int i = 0; i < message.size(); i++) {
133 messagepart = message.mid(i, 1);
134 if (i+1 < message.size()) {
135 for (xdelimquote = _ctcpXDelimDequoteHash.begin(); xdelimquote != _ctcpXDelimDequoteHash.end(); ++xdelimquote) {
136 if (message.mid(i, 2) == xdelimquote.key()) {
137 messagepart = xdelimquote.value();
143 dequotedMessage += messagepart;
145 return dequotedMessage;
149 void CtcpParser::processIrcEventRawNotice(IrcEventRawMessage *event)
151 parse(event, Message::Notice);
155 void CtcpParser::processIrcEventRawPrivmsg(IrcEventRawMessage *event)
157 parse(event, Message::Plain);
161 void CtcpParser::parse(IrcEventRawMessage *e, Message::Type messagetype)
163 //lowlevel message dequote
164 QByteArray dequotedMessage = lowLevelDequote(e->rawMessage());
166 CtcpEvent::CtcpType ctcptype = e->type() == EventManager::IrcEventRawNotice
170 Message::Flags flags = (ctcptype == CtcpEvent::Reply && !e->network()->isChannelName(e->target()))
171 ? Message::Redirected
174 bool isStatusMsg = false;
176 // First remove all statusmsg prefix characters that are not also channel prefix characters.
177 while (e->network()->isStatusMsg(e->target()) && !e->network()->isChannelName(e->target())) {
179 e->setTarget(e->target().remove(0, 1));
182 // Then continue removing statusmsg characters as long as removing the character will still result in a
183 // valid channel name. This prevents removing the channel prefix character if said character is in the
184 // overlap between the statusmsg characters and the channel prefix characters.
185 while (e->network()->isStatusMsg(e->target()) && e->network()->isChannelName(e->target().remove(0, 1))) {
187 e->setTarget(e->target().remove(0, 1));
190 // If any statusmsg characters were removed, Flag the message as a StatusMsg.
192 flags |= Message::StatusMsg;
195 if (coreSession()->networkConfig()->standardCtcp())
196 parseStandard(e, messagetype, dequotedMessage, ctcptype, flags);
198 parseSimple(e, messagetype, dequotedMessage, ctcptype, flags);
202 // only accept CTCPs in their simplest form, i.e. one ctcp, from start to
203 // end, no text around it; not as per the 'specs', but makes people happier
204 void CtcpParser::parseSimple(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
206 if (dequotedMessage.count(XDELIM) != 2 || dequotedMessage[0] != '\001' || dequotedMessage[dequotedMessage.count() -1] != '\001') {
207 displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
210 QString ctcpcmd, ctcpparam;
212 QByteArray ctcp = xdelimDequote(dequotedMessage.mid(1, dequotedMessage.count() - 2));
213 spacePos = ctcp.indexOf(' ');
214 if (spacePos != -1) {
215 ctcpcmd = targetDecode(e, ctcp.left(spacePos));
216 ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
218 ctcpcmd = targetDecode(e, ctcp);
219 ctcpparam = QString();
221 ctcpcmd = ctcpcmd.toUpper();
223 // we don't want to block /me messages by the CTCP ignore list
224 if (ctcpcmd == QLatin1String("ACTION") || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
225 QUuid uuid = QUuid::createUuid();
226 _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
227 CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
228 ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
229 emit newEvent(event);
230 CtcpEvent *flushEvent = new CtcpEvent(EventManager::CtcpEventFlush, e->network(), e->prefix(), e->target(),
231 ctcptype, "INVALID", QString(), e->timestamp(), uuid);
232 emit newEvent(flushEvent);
238 void CtcpParser::parseStandard(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
242 QList<CtcpEvent *> ctcpEvents;
243 QUuid uuid; // needed to group all replies together
245 // extract tagged / extended data
247 int xdelimEndPos = -1;
249 while ((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
251 displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
253 xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
254 if (xdelimEndPos == -1) {
255 // no matching end delimiter found... treat rest of the message as ctcp
256 xdelimEndPos = dequotedMessage.count();
258 ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
259 dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
261 //dispatch the ctcp command
262 QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
263 QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
265 spacePos = ctcp.indexOf(' ');
266 if (spacePos != -1) {
267 ctcpcmd = targetDecode(e, ctcp.left(spacePos));
268 ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
271 ctcpcmd = targetDecode(e, ctcp);
272 ctcpparam = QString();
275 ctcpcmd = ctcpcmd.toUpper();
277 // we don't want to block /me messages by the CTCP ignore list
278 if (ctcpcmd == QLatin1String("ACTION") || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
280 uuid = QUuid::createUuid();
282 CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
283 ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
287 if (!ctcpEvents.isEmpty()) {
288 _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
289 CtcpEvent *flushEvent = new CtcpEvent(EventManager::CtcpEventFlush, e->network(), e->prefix(), e->target(),
290 ctcptype, "INVALID", QString(), e->timestamp(), uuid);
291 ctcpEvents << flushEvent;
292 foreach(CtcpEvent *event, ctcpEvents) {
293 emit newEvent(event);
297 if (!dequotedMessage.isEmpty())
298 displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
302 void CtcpParser::sendCtcpEvent(CtcpEvent *e)
304 CoreNetwork *net = coreNetwork(e);
305 if (e->type() == EventManager::CtcpEvent) {
306 QByteArray quotedReply;
307 QString bufname = nickFromMask(e->prefix());
308 if (e->ctcpType() == CtcpEvent::Query && !e->reply().isNull()) {
309 if (_replies.contains(e->uuid()))
310 _replies[e->uuid()].replies << lowLevelQuote(pack(net->serverEncode(e->ctcpCmd()),
311 net->userEncode(bufname, e->reply())));
313 // reply not caused by a request processed in here, so send it off immediately
314 reply(net, bufname, e->ctcpCmd(), e->reply());
317 else if (e->type() == EventManager::CtcpEventFlush && _replies.contains(e->uuid())) {
318 CtcpReply reply = _replies.take(e->uuid());
319 if (reply.replies.count())
320 packedReply(net, reply.bufferName, reply.replies);
325 QByteArray CtcpParser::pack(const QByteArray &ctcpTag, const QByteArray &message)
327 if (message.isEmpty())
328 return XDELIM + ctcpTag + XDELIM;
330 return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
334 void CtcpParser::query(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
336 QString cmd("PRIVMSG");
338 std::function<QList<QByteArray>(QString &)> cmdGenerator = [&] (QString &splitMsg) -> QList<QByteArray> {
339 return QList<QByteArray>() << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, splitMsg)));
342 net->putCmd(cmd, net->splitMessage(cmd, message, cmdGenerator));
346 void CtcpParser::reply(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
348 QList<QByteArray> params;
349 params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
350 net->putCmd("NOTICE", params);
354 void CtcpParser::packedReply(CoreNetwork *net, const QString &bufname, const QList<QByteArray> &replies)
356 QList<QByteArray> params;
359 for (int i = 0; i < replies.count(); i++) {
360 answerSize += replies.at(i).size();
363 QByteArray quotedReply;
364 quotedReply.reserve(answerSize);
365 for (int i = 0; i < replies.count(); i++) {
366 quotedReply.append(replies.at(i));
369 params << net->serverEncode(bufname) << quotedReply;
370 // FIXME user proper event
371 net->putCmd("NOTICE", params);