1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 "coreuserinputhandler.h"
26 #include "ctcpevent.h"
27 #include "messageevent.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(), &NetworkConfig::standardCtcpSet, this, &CtcpParser::setStandardCtcp);
44 connect(this, &CtcpParser::newEvent, _coreSession->eventManager(), &EventManager::postEvent);
47 void CtcpParser::setStandardCtcp(bool enabled)
49 QByteArray XQUOTE = QByteArray(R"(\)");
51 _ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
53 _ctcpXDelimDequoteHash.remove(XQUOTE + XQUOTE);
54 _ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
57 void CtcpParser::displayMsg(
58 NetworkEvent* event, Message::Type msgType, const QString& msg, const QString& sender, const QString& target, Message::Flags msgFlags)
60 if (event->testFlag(EventManager::Silent))
63 MessageEvent* msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
64 msgEvent->setTimestamp(event->timestamp());
66 emit newEvent(msgEvent);
69 QByteArray CtcpParser::lowLevelQuote(const QByteArray& message)
71 QByteArray quotedMessage = message;
73 QHash<QByteArray, QByteArray> quoteHash = _ctcpMDequoteHash;
74 QByteArray MQUOTE = QByteArray("\020");
75 quoteHash.remove(MQUOTE + MQUOTE);
76 quotedMessage.replace(MQUOTE, MQUOTE + MQUOTE);
78 QHash<QByteArray, QByteArray>::const_iterator quoteIter = quoteHash.constBegin();
79 while (quoteIter != quoteHash.constEnd()) {
80 quotedMessage.replace(quoteIter.value(), quoteIter.key());
86 QByteArray CtcpParser::lowLevelDequote(const QByteArray& message)
88 QByteArray dequotedMessage;
89 QByteArray messagepart;
90 QHash<QByteArray, QByteArray>::iterator ctcpquote;
92 // copy dequote Message
93 for (int i = 0; i < message.size(); i++) {
94 messagepart = message.mid(i, 1);
95 if (i + 1 < message.size()) {
96 for (ctcpquote = _ctcpMDequoteHash.begin(); ctcpquote != _ctcpMDequoteHash.end(); ++ctcpquote) {
97 if (message.mid(i, 2) == ctcpquote.key()) {
98 messagepart = ctcpquote.value();
104 dequotedMessage += messagepart;
106 return dequotedMessage;
109 QByteArray CtcpParser::xdelimQuote(const QByteArray& message)
111 QByteArray quotedMessage = message;
112 QHash<QByteArray, QByteArray>::const_iterator quoteIter = _ctcpXDelimDequoteHash.constBegin();
113 while (quoteIter != _ctcpXDelimDequoteHash.constEnd()) {
114 quotedMessage.replace(quoteIter.value(), quoteIter.key());
117 return quotedMessage;
120 QByteArray CtcpParser::xdelimDequote(const QByteArray& message)
122 QByteArray dequotedMessage;
123 QByteArray messagepart;
124 QHash<QByteArray, QByteArray>::iterator xdelimquote;
126 for (int i = 0; i < message.size(); i++) {
127 messagepart = message.mid(i, 1);
128 if (i + 1 < message.size()) {
129 for (xdelimquote = _ctcpXDelimDequoteHash.begin(); xdelimquote != _ctcpXDelimDequoteHash.end(); ++xdelimquote) {
130 if (message.mid(i, 2) == xdelimquote.key()) {
131 messagepart = xdelimquote.value();
137 dequotedMessage += messagepart;
139 return dequotedMessage;
142 void CtcpParser::processIrcEventRawNotice(IrcEventRawMessage* event)
144 parse(event, Message::Notice);
147 void CtcpParser::processIrcEventRawPrivmsg(IrcEventRawMessage* event)
149 parse(event, Message::Plain);
152 void CtcpParser::parse(IrcEventRawMessage* e, Message::Type messagetype)
154 // lowlevel message dequote
155 QByteArray dequotedMessage = lowLevelDequote(e->rawMessage());
157 CtcpEvent::CtcpType ctcptype = e->type() == EventManager::IrcEventRawNotice ? CtcpEvent::Reply : CtcpEvent::Query;
159 Message::Flags flags = (ctcptype == CtcpEvent::Reply && !e->network()->isChannelName(e->target())) ? Message::Redirected : Message::None;
161 bool isStatusMsg = false;
163 // First remove all statusmsg prefix characters that are not also channel prefix characters.
164 while (e->network()->isStatusMsg(e->target()) && !e->network()->isChannelName(e->target())) {
166 e->setTarget(e->target().remove(0, 1));
169 // Then continue removing statusmsg characters as long as removing the character will still result in a
170 // valid channel name. This prevents removing the channel prefix character if said character is in the
171 // overlap between the statusmsg characters and the channel prefix characters.
172 while (e->network()->isStatusMsg(e->target()) && e->network()->isChannelName(e->target().remove(0, 1))) {
174 e->setTarget(e->target().remove(0, 1));
177 // If any statusmsg characters were removed, Flag the message as a StatusMsg.
179 flags |= Message::StatusMsg;
182 // For self-messages, pass the flag on to the message, too
183 if (e->testFlag(EventManager::Self)) {
184 flags |= Message::Self;
187 if (coreSession()->networkConfig()->standardCtcp())
188 parseStandard(e, messagetype, dequotedMessage, ctcptype, flags);
190 parseSimple(e, messagetype, dequotedMessage, ctcptype, flags);
193 // only accept CTCPs in their simplest form, i.e. one ctcp, from start to
194 // end, no text around it; not as per the 'specs', but makes people happier
195 void CtcpParser::parseSimple(
196 IrcEventRawMessage* e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
198 if (dequotedMessage.count(XDELIM) != 2 || dequotedMessage[0] != '\001' || dequotedMessage[dequotedMessage.count() - 1] != '\001') {
199 displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
203 QString ctcpcmd, ctcpparam;
205 QByteArray ctcp = xdelimDequote(dequotedMessage.mid(1, dequotedMessage.count() - 2));
206 spacePos = ctcp.indexOf(' ');
207 if (spacePos != -1) {
208 ctcpcmd = targetDecode(e, ctcp.left(spacePos));
209 ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
212 ctcpcmd = targetDecode(e, ctcp);
213 ctcpparam = QString();
215 ctcpcmd = ctcpcmd.toUpper();
217 // we don't want to block /me messages by the CTCP ignore list
218 if (ctcpcmd == QLatin1String("ACTION")
219 || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
220 QUuid uuid = QUuid::createUuid();
221 _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
222 CtcpEvent* event = new CtcpEvent(EventManager::CtcpEvent,
231 emit newEvent(event);
232 CtcpEvent* flushEvent = new CtcpEvent(EventManager::CtcpEventFlush,
241 emit newEvent(flushEvent);
246 void CtcpParser::parseStandard(
247 IrcEventRawMessage* e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
251 QList<CtcpEvent*> ctcpEvents;
252 QUuid uuid; // needed to group all replies together
254 // extract tagged / extended data
256 int xdelimEndPos = -1;
258 while ((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
260 displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
262 xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
263 if (xdelimEndPos == -1) {
264 // no matching end delimiter found... treat rest of the message as ctcp
265 xdelimEndPos = dequotedMessage.count();
267 ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
268 dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
270 // dispatch the ctcp command
271 QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
272 QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
274 spacePos = ctcp.indexOf(' ');
275 if (spacePos != -1) {
276 ctcpcmd = targetDecode(e, ctcp.left(spacePos));
277 ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
280 ctcpcmd = targetDecode(e, ctcp);
281 ctcpparam = QString();
284 ctcpcmd = ctcpcmd.toUpper();
286 // we don't want to block /me messages by the CTCP ignore list
287 if (ctcpcmd == QLatin1String("ACTION")
288 || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
290 uuid = QUuid::createUuid();
292 CtcpEvent* event = new CtcpEvent(EventManager::CtcpEvent,
304 if (!ctcpEvents.isEmpty()) {
305 _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
306 CtcpEvent* flushEvent = new CtcpEvent(EventManager::CtcpEventFlush,
315 ctcpEvents << flushEvent;
316 foreach (CtcpEvent* event, ctcpEvents) {
317 emit newEvent(event);
321 if (!dequotedMessage.isEmpty())
322 displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
325 void CtcpParser::sendCtcpEvent(CtcpEvent* e)
327 CoreNetwork* net = coreNetwork(e);
328 if (e->type() == EventManager::CtcpEvent) {
329 QByteArray quotedReply;
330 QString bufname = nickFromMask(e->prefix());
331 if (e->ctcpType() == CtcpEvent::Query && !e->reply().isNull()) {
332 if (_replies.contains(e->uuid()))
333 _replies[e->uuid()].replies << lowLevelQuote(pack(net->serverEncode(e->ctcpCmd()), net->userEncode(bufname, e->reply())));
335 // reply not caused by a request processed in here, so send it off immediately
336 reply(net, bufname, e->ctcpCmd(), e->reply());
339 else if (e->type() == EventManager::CtcpEventFlush && _replies.contains(e->uuid())) {
340 CtcpReply reply = _replies.take(e->uuid());
341 if (reply.replies.count())
342 packedReply(net, reply.bufferName, reply.replies);
346 QByteArray CtcpParser::pack(const QByteArray& ctcpTag, const QByteArray& message)
348 if (message.isEmpty())
349 return XDELIM + ctcpTag + XDELIM;
351 return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
354 void CtcpParser::query(CoreNetwork* net, const QString& bufname, const QString& ctcpTag, const QString& message)
356 QString cmd("PRIVMSG");
358 std::function<QList<QByteArray>(QString&)> cmdGenerator = [&](QString& splitMsg) -> QList<QByteArray> {
359 return QList<QByteArray>() << net->serverEncode(bufname)
360 << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, splitMsg)));
363 net->putCmd(cmd, net->splitMessage(cmd, message, cmdGenerator));
366 void CtcpParser::reply(CoreNetwork* net, const QString& bufname, const QString& ctcpTag, const QString& message)
368 QList<QByteArray> params;
369 params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
370 net->putCmd("NOTICE", params);
373 void CtcpParser::packedReply(CoreNetwork* net, const QString& bufname, const QList<QByteArray>& replies)
375 QList<QByteArray> params;
378 for (int i = 0; i < replies.count(); i++) {
379 answerSize += replies.at(i).size();
382 QByteArray quotedReply;
383 quotedReply.reserve(answerSize);
384 for (int i = 0; i < replies.count(); i++) {
385 quotedReply.append(replies.at(i));
388 params << net->serverEncode(bufname) << quotedReply;
389 // FIXME user proper event
390 net->putCmd("NOTICE", params);