ssl: Use QSslSocket directly to avoid redundant qobject_casts
[quassel.git] / src / core / ctcpparser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "ctcpparser.h"
22
23 #include "corenetworkconfig.h"
24 #include "coresession.h"
25 #include "coreuserinputhandler.h"
26 #include "ctcpevent.h"
27 #include "irctags.h"
28 #include "messageevent.h"
29
30 const QByteArray XDELIM = "\001";
31
32 CtcpParser::CtcpParser(CoreSession* coreSession, QObject* parent)
33     : QObject(parent)
34     , _coreSession(coreSession)
35 {
36     QByteArray MQUOTE = QByteArray("\020");
37     _ctcpMDequoteHash[MQUOTE + '0'] = QByteArray(1, '\000');
38     _ctcpMDequoteHash[MQUOTE + 'n'] = QByteArray(1, '\n');
39     _ctcpMDequoteHash[MQUOTE + 'r'] = QByteArray(1, '\r');
40     _ctcpMDequoteHash[MQUOTE + MQUOTE] = MQUOTE;
41
42     setStandardCtcp(_coreSession->networkConfig()->standardCtcp());
43
44     connect(_coreSession->networkConfig(), &NetworkConfig::standardCtcpSet, this, &CtcpParser::setStandardCtcp);
45     connect(this, &CtcpParser::newEvent, _coreSession->eventManager(), &EventManager::postEvent);
46 }
47
48 void CtcpParser::setStandardCtcp(bool enabled)
49 {
50     QByteArray XQUOTE = QByteArray(R"(\)");
51     if (enabled)
52         _ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
53     else
54         _ctcpXDelimDequoteHash.remove(XQUOTE + XQUOTE);
55     _ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
56 }
57
58 void CtcpParser::displayMsg(NetworkEvent* event,
59                             Message::Type msgType,
60                             QString msg,
61                             QString sender,
62                             QString target,
63                             Message::Flags msgFlags)
64 {
65     if (event->testFlag(EventManager::Silent))
66         return;
67
68     MessageEvent* msgEvent = new MessageEvent(msgType, event->network(), std::move(msg), std::move(sender), std::move(target), msgFlags, event->timestamp());
69     emit newEvent(msgEvent);
70 }
71
72 QByteArray CtcpParser::lowLevelQuote(const QByteArray& message)
73 {
74     QByteArray quotedMessage = message;
75
76     QHash<QByteArray, QByteArray> quoteHash = _ctcpMDequoteHash;
77     QByteArray MQUOTE = QByteArray("\020");
78     quoteHash.remove(MQUOTE + MQUOTE);
79     quotedMessage.replace(MQUOTE, MQUOTE + MQUOTE);
80
81     QHash<QByteArray, QByteArray>::const_iterator quoteIter = quoteHash.constBegin();
82     while (quoteIter != quoteHash.constEnd()) {
83         quotedMessage.replace(quoteIter.value(), quoteIter.key());
84         ++quoteIter;
85     }
86     return quotedMessage;
87 }
88
89 QByteArray CtcpParser::lowLevelDequote(const QByteArray& message)
90 {
91     QByteArray dequotedMessage;
92     QByteArray messagepart;
93     QHash<QByteArray, QByteArray>::iterator ctcpquote;
94
95     // copy dequote Message
96     for (int i = 0; i < message.size(); i++) {
97         messagepart = message.mid(i, 1);
98         if (i + 1 < message.size()) {
99             for (ctcpquote = _ctcpMDequoteHash.begin(); ctcpquote != _ctcpMDequoteHash.end(); ++ctcpquote) {
100                 if (message.mid(i, 2) == ctcpquote.key()) {
101                     messagepart = ctcpquote.value();
102                     ++i;
103                     break;
104                 }
105             }
106         }
107         dequotedMessage += messagepart;
108     }
109     return dequotedMessage;
110 }
111
112 QByteArray CtcpParser::xdelimQuote(const QByteArray& message)
113 {
114     QByteArray quotedMessage = message;
115     QHash<QByteArray, QByteArray>::const_iterator quoteIter = _ctcpXDelimDequoteHash.constBegin();
116     while (quoteIter != _ctcpXDelimDequoteHash.constEnd()) {
117         quotedMessage.replace(quoteIter.value(), quoteIter.key());
118         ++quoteIter;
119     }
120     return quotedMessage;
121 }
122
123 QByteArray CtcpParser::xdelimDequote(const QByteArray& message)
124 {
125     QByteArray dequotedMessage;
126     QByteArray messagepart;
127     QHash<QByteArray, QByteArray>::iterator xdelimquote;
128
129     for (int i = 0; i < message.size(); i++) {
130         messagepart = message.mid(i, 1);
131         if (i + 1 < message.size()) {
132             for (xdelimquote = _ctcpXDelimDequoteHash.begin(); xdelimquote != _ctcpXDelimDequoteHash.end(); ++xdelimquote) {
133                 if (message.mid(i, 2) == xdelimquote.key()) {
134                     messagepart = xdelimquote.value();
135                     i++;
136                     break;
137                 }
138             }
139         }
140         dequotedMessage += messagepart;
141     }
142     return dequotedMessage;
143 }
144
145 void CtcpParser::processIrcEventRawNotice(IrcEventRawMessage* event)
146 {
147     parse(event, Message::Notice);
148 }
149
150 void CtcpParser::processIrcEventRawPrivmsg(IrcEventRawMessage* event)
151 {
152     parse(event, Message::Plain);
153 }
154
155 void CtcpParser::parse(IrcEventRawMessage* e, Message::Type messagetype)
156 {
157     // lowlevel message dequote
158     QByteArray dequotedMessage = lowLevelDequote(e->rawMessage());
159
160     CtcpEvent::CtcpType ctcptype = e->type() == EventManager::IrcEventRawNotice ? CtcpEvent::Reply : CtcpEvent::Query;
161
162     Message::Flags flags = (ctcptype == CtcpEvent::Reply && !e->network()->isChannelName(e->target())) ? Message::Redirected : Message::None;
163
164     bool isStatusMsg = false;
165
166     // First remove all statusmsg prefix characters that are not also channel prefix characters.
167     while (e->network()->isStatusMsg(e->target()) && !e->network()->isChannelName(e->target())) {
168         isStatusMsg = true;
169         e->setTarget(e->target().remove(0, 1));
170     }
171
172     // Then continue removing statusmsg characters as long as removing the character will still result in a
173     // valid channel name.  This prevents removing the channel prefix character if said character is in the
174     // overlap between the statusmsg characters and the channel prefix characters.
175     while (e->network()->isStatusMsg(e->target()) && e->network()->isChannelName(e->target().remove(0, 1))) {
176         isStatusMsg = true;
177         e->setTarget(e->target().remove(0, 1));
178     }
179
180     // If any statusmsg characters were removed, Flag the message as a StatusMsg.
181     if (isStatusMsg) {
182         flags |= Message::StatusMsg;
183     }
184
185     // For self-messages, pass the flag on to the message, too
186     if (e->testFlag(EventManager::Self)) {
187         flags |= Message::Self;
188     }
189
190     if (coreSession()->networkConfig()->standardCtcp())
191         parseStandard(e, messagetype, dequotedMessage, ctcptype, flags);
192     else
193         parseSimple(e, messagetype, dequotedMessage, ctcptype, flags);
194 }
195
196 // only accept CTCPs in their simplest form, i.e. one ctcp, from start to
197 // end, no text around it; not as per the 'specs', but makes people happier
198 void CtcpParser::parseSimple(IrcEventRawMessage* e,
199                              Message::Type messagetype,
200                              const QByteArray& dequotedMessage,
201                              CtcpEvent::CtcpType ctcptype,
202                              Message::Flags flags)
203 {
204     if (dequotedMessage.count(XDELIM) != 2 || dequotedMessage[0] != '\001' || dequotedMessage[dequotedMessage.count() - 1] != '\001') {
205         displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
206     }
207     else {
208         int spacePos;
209         QString ctcpcmd, ctcpparam;
210
211         QByteArray ctcp = xdelimDequote(dequotedMessage.mid(1, dequotedMessage.count() - 2));
212         spacePos = ctcp.indexOf(' ');
213         if (spacePos != -1) {
214             ctcpcmd = targetDecode(e, ctcp.left(spacePos));
215             ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
216         }
217         else {
218             ctcpcmd = targetDecode(e, ctcp);
219             ctcpparam = QString();
220         }
221         ctcpcmd = ctcpcmd.toUpper();
222
223         bool isAction = ctcpcmd == QLatin1String("ACTION");
224         // we don't want to block /me messages by the CTCP ignore list
225         if (isAction
226             || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
227             QUuid uuid = QUuid::createUuid();
228             _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
229             CtcpEvent* event = new CtcpEvent(EventManager::CtcpEvent,
230                                              e->network(),
231                                              isAction ? QHash<IrcTagKey, QString>() : e->tags(),
232                                              e->prefix(),
233                                              e->target(),
234                                              ctcptype,
235                                              ctcpcmd,
236                                              ctcpparam,
237                                              e->timestamp(),
238                                              uuid);
239             emit newEvent(event);
240             CtcpEvent* flushEvent = new CtcpEvent(EventManager::CtcpEventFlush,
241                                                   e->network(),
242                                                   {},
243                                                   e->prefix(),
244                                                   e->target(),
245                                                   ctcptype,
246                                                   "INVALID",
247                                                   QString(),
248                                                   e->timestamp(),
249                                                   uuid);
250             emit newEvent(flushEvent);
251         }
252     }
253 }
254
255 void CtcpParser::parseStandard(IrcEventRawMessage* e,
256                                Message::Type messagetype,
257                                const QByteArray& dequotedMessage_,
258                                CtcpEvent::CtcpType ctcptype,
259                                Message::Flags flags)
260 {
261     auto dequotedMessage = dequotedMessage_;
262     QByteArray ctcp;
263
264     QList<CtcpEvent*> ctcpEvents;
265     QUuid uuid;  // needed to group all replies together
266
267     // extract tagged / extended data
268     int xdelimPos = -1;
269     int xdelimEndPos = -1;
270     int spacePos = -1;
271     while ((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
272         if (xdelimPos > 0)
273             displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
274
275         xdelimEndPos = dequotedMessage.indexOf(XDELIM, xdelimPos + 1);
276         if (xdelimEndPos == -1) {
277             // no matching end delimiter found... treat rest of the message as ctcp
278             xdelimEndPos = dequotedMessage.count();
279         }
280         ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
281         dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
282
283         // dispatch the ctcp command
284         QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
285         QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
286
287         spacePos = ctcp.indexOf(' ');
288         if (spacePos != -1) {
289             ctcpcmd = targetDecode(e, ctcp.left(spacePos));
290             ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
291         }
292         else {
293             ctcpcmd = targetDecode(e, ctcp);
294             ctcpparam = QString();
295         }
296
297         ctcpcmd = ctcpcmd.toUpper();
298
299         bool isAction = ctcpcmd == QLatin1String("ACTION");
300         // we don't want to block /me messages by the CTCP ignore list
301         if (isAction
302             || !coreSession()->ignoreListManager()->ctcpMatch(e->prefix(), e->network()->networkName(), ctcpcmd)) {
303             if (uuid.isNull())
304                 uuid = QUuid::createUuid();
305
306             CtcpEvent* event = new CtcpEvent(EventManager::CtcpEvent,
307                                              e->network(),
308                                              isAction ? QHash<IrcTagKey, QString>() : e->tags(),
309                                              e->prefix(),
310                                              e->target(),
311                                              ctcptype,
312                                              ctcpcmd,
313                                              ctcpparam,
314                                              e->timestamp(),
315                                              uuid);
316             ctcpEvents << event;
317         }
318     }
319     if (!ctcpEvents.isEmpty()) {
320         _replies.insert(uuid, CtcpReply(coreNetwork(e), nickFromMask(e->prefix())));
321         CtcpEvent* flushEvent = new CtcpEvent(EventManager::CtcpEventFlush,
322                                               e->network(),
323                                               {},
324                                               e->prefix(),
325                                               e->target(),
326                                               ctcptype,
327                                               "INVALID",
328                                               QString(),
329                                               e->timestamp(),
330                                               uuid);
331         ctcpEvents << flushEvent;
332         for (CtcpEvent* event : ctcpEvents) {
333             emit newEvent(event);
334         }
335     }
336
337     if (!dequotedMessage.isEmpty())
338         displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
339 }
340
341 void CtcpParser::sendCtcpEvent(CtcpEvent* e)
342 {
343     CoreNetwork* net = coreNetwork(e);
344     if (e->type() == EventManager::CtcpEvent) {
345         QByteArray quotedReply;
346         QString bufname = nickFromMask(e->prefix());
347         if (e->ctcpType() == CtcpEvent::Query && !e->reply().isNull()) {
348             if (_replies.contains(e->uuid()))
349                 _replies[e->uuid()].replies << lowLevelQuote(pack(net->serverEncode(e->ctcpCmd()), net->userEncode(bufname, e->reply())));
350             else
351                 // reply not caused by a request processed in here, so send it off immediately
352                 reply(net, bufname, e->ctcpCmd(), e->reply());
353         }
354     }
355     else if (e->type() == EventManager::CtcpEventFlush && _replies.contains(e->uuid())) {
356         CtcpReply reply = _replies.take(e->uuid());
357         if (reply.replies.count())
358             packedReply(net, reply.bufferName, reply.replies);
359     }
360 }
361
362 QByteArray CtcpParser::pack(const QByteArray& ctcpTag, const QByteArray& message)
363 {
364     if (message.isEmpty())
365         return XDELIM + ctcpTag + XDELIM;
366
367     return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
368 }
369
370 void CtcpParser::query(CoreNetwork* net, const QString& bufname, const QString& ctcpTag, const QString& message)
371 {
372     QString cmd("PRIVMSG");
373
374     std::function<QList<QByteArray>(QString&)> cmdGenerator = [&](QString& splitMsg) -> QList<QByteArray> {
375         return QList<QByteArray>() << net->serverEncode(bufname)
376                                    << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, splitMsg)));
377     };
378
379     net->putCmd(cmd, net->splitMessage(cmd, message, cmdGenerator));
380 }
381
382 void CtcpParser::reply(CoreNetwork* net, const QString& bufname, const QString& ctcpTag, const QString& message)
383 {
384     QList<QByteArray> params;
385     params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
386     net->putCmd("NOTICE", params);
387 }
388
389 void CtcpParser::packedReply(CoreNetwork* net, const QString& bufname, const QList<QByteArray>& replies)
390 {
391     QList<QByteArray> params;
392
393     int answerSize = 0;
394     for (int i = 0; i < replies.count(); i++) {
395         answerSize += replies.at(i).size();
396     }
397
398     QByteArray quotedReply;
399     quotedReply.reserve(answerSize);
400     for (int i = 0; i < replies.count(); i++) {
401         quotedReply.append(replies.at(i));
402     }
403
404     params << net->serverEncode(bufname) << quotedReply;
405     // FIXME user proper event
406     net->putCmd("NOTICE", params);
407 }