1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 // For self-messages, pass the flag on to the message, too
196 if (e->testFlag(EventManager::Self)) {
197 flags |= Message::Self;
200 if (coreSession()->networkConfig()->standardCtcp())
201 parseStandard(e, messagetype, dequotedMessage, ctcptype, flags);
203 parseSimple(e, messagetype, dequotedMessage, ctcptype, flags);
207 // only accept CTCPs in their simplest form, i.e. one ctcp, from start to
208 // end, no text around it; not as per the 'specs', but makes people happier
209 void CtcpParser::parseSimple(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
211 if (dequotedMessage.count(XDELIM) != 2 || dequotedMessage[0] != '\001' || dequotedMessage[dequotedMessage.count() -1] != '\001') {
212 displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
215 QString ctcpcmd, ctcpparam;
217 QByteArray ctcp = xdelimDequote(dequotedMessage.mid(1, dequotedMessage.count() - 2));
218 spacePos = ctcp.indexOf(' ');
219 if (spacePos != -1) {
220 ctcpcmd = targetDecode(e, ctcp.left(spacePos));
221 ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
223 ctcpcmd = targetDecode(e, ctcp);
224 ctcpparam = QString();
226 ctcpcmd = ctcpcmd.toUpper();
228 // we don't want to block /me messages by the CTCP ignore list
229 if (ctcpcmd == QLatin1String("ACTION") || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
230 QUuid uuid = QUuid::createUuid();
231 _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
232 CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
233 ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
234 emit newEvent(event);
235 CtcpEvent *flushEvent = new CtcpEvent(EventManager::CtcpEventFlush, e->network(), e->prefix(), e->target(),
236 ctcptype, "INVALID", QString(), e->timestamp(), uuid);
237 emit newEvent(flushEvent);
243 void CtcpParser::parseStandard(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
247 QList<CtcpEvent *> ctcpEvents;
248 QUuid uuid; // needed to group all replies together
250 // extract tagged / extended data
252 int xdelimEndPos = -1;
254 while ((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
256 displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
258 xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
259 if (xdelimEndPos == -1) {
260 // no matching end delimiter found... treat rest of the message as ctcp
261 xdelimEndPos = dequotedMessage.count();
263 ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
264 dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
266 //dispatch the ctcp command
267 QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
268 QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
270 spacePos = ctcp.indexOf(' ');
271 if (spacePos != -1) {
272 ctcpcmd = targetDecode(e, ctcp.left(spacePos));
273 ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
276 ctcpcmd = targetDecode(e, ctcp);
277 ctcpparam = QString();
280 ctcpcmd = ctcpcmd.toUpper();
282 // we don't want to block /me messages by the CTCP ignore list
283 if (ctcpcmd == QLatin1String("ACTION") || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
285 uuid = QUuid::createUuid();
287 CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
288 ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
292 if (!ctcpEvents.isEmpty()) {
293 _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
294 CtcpEvent *flushEvent = new CtcpEvent(EventManager::CtcpEventFlush, e->network(), e->prefix(), e->target(),
295 ctcptype, "INVALID", QString(), e->timestamp(), uuid);
296 ctcpEvents << flushEvent;
297 foreach(CtcpEvent *event, ctcpEvents) {
298 emit newEvent(event);
302 if (!dequotedMessage.isEmpty())
303 displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
307 void CtcpParser::sendCtcpEvent(CtcpEvent *e)
309 CoreNetwork *net = coreNetwork(e);
310 if (e->type() == EventManager::CtcpEvent) {
311 QByteArray quotedReply;
312 QString bufname = nickFromMask(e->prefix());
313 if (e->ctcpType() == CtcpEvent::Query && !e->reply().isNull()) {
314 if (_replies.contains(e->uuid()))
315 _replies[e->uuid()].replies << lowLevelQuote(pack(net->serverEncode(e->ctcpCmd()),
316 net->userEncode(bufname, e->reply())));
318 // reply not caused by a request processed in here, so send it off immediately
319 reply(net, bufname, e->ctcpCmd(), e->reply());
322 else if (e->type() == EventManager::CtcpEventFlush && _replies.contains(e->uuid())) {
323 CtcpReply reply = _replies.take(e->uuid());
324 if (reply.replies.count())
325 packedReply(net, reply.bufferName, reply.replies);
330 QByteArray CtcpParser::pack(const QByteArray &ctcpTag, const QByteArray &message)
332 if (message.isEmpty())
333 return XDELIM + ctcpTag + XDELIM;
335 return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
339 void CtcpParser::query(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
341 QString cmd("PRIVMSG");
343 std::function<QList<QByteArray>(QString &)> cmdGenerator = [&] (QString &splitMsg) -> QList<QByteArray> {
344 return QList<QByteArray>() << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, splitMsg)));
347 net->putCmd(cmd, net->splitMessage(cmd, message, cmdGenerator));
351 void CtcpParser::reply(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
353 QList<QByteArray> params;
354 params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
355 net->putCmd("NOTICE", params);
359 void CtcpParser::packedReply(CoreNetwork *net, const QString &bufname, const QList<QByteArray> &replies)
361 QList<QByteArray> params;
364 for (int i = 0; i < replies.count(); i++) {
365 answerSize += replies.at(i).size();
368 QByteArray quotedReply;
369 quotedReply.reserve(answerSize);
370 for (int i = 0; i < replies.count(); i++) {
371 quotedReply.append(replies.at(i));
374 params << net->serverEncode(bufname) << quotedReply;
375 // FIXME user proper event
376 net->putCmd("NOTICE", params);