qa: Remove lots of superfluous semicolons
[quassel.git] / src / core / ctcpparser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "ctcpevent.h"
26 #include "messageevent.h"
27 #include "coreuserinputhandler.h"
28
29 const QByteArray XDELIM = "\001";
30
31 CtcpParser::CtcpParser(CoreSession *coreSession, QObject *parent)
32     : QObject(parent),
33     _coreSession(coreSession)
34 {
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;
40
41     setStandardCtcp(_coreSession->networkConfig()->standardCtcp());
42
43     connect(_coreSession->networkConfig(), SIGNAL(standardCtcpSet(bool)), this, SLOT(setStandardCtcp(bool)));
44     connect(this, SIGNAL(newEvent(Event *)), _coreSession->eventManager(), SLOT(postEvent(Event *)));
45 }
46
47
48 void CtcpParser::setStandardCtcp(bool enabled)
49 {
50     QByteArray XQUOTE = QByteArray("\134");
51     if (enabled)
52         _ctcpXDelimDequoteHash[XQUOTE + XQUOTE] = XQUOTE;
53     else
54         _ctcpXDelimDequoteHash.remove(XQUOTE + XQUOTE);
55     _ctcpXDelimDequoteHash[XQUOTE + QByteArray("a")] = XDELIM;
56 }
57
58
59 void CtcpParser::displayMsg(NetworkEvent *event, Message::Type msgType, const QString &msg, const QString &sender,
60     const QString &target, Message::Flags msgFlags)
61 {
62     if (event->testFlag(EventManager::Silent))
63         return;
64
65     MessageEvent *msgEvent = new MessageEvent(msgType, event->network(), msg, sender, target, msgFlags);
66     msgEvent->setTimestamp(event->timestamp());
67
68     emit newEvent(msgEvent);
69 }
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
90 QByteArray CtcpParser::lowLevelDequote(const QByteArray &message)
91 {
92     QByteArray dequotedMessage;
93     QByteArray messagepart;
94     QHash<QByteArray, QByteArray>::iterator ctcpquote;
95
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();
103                     ++i;
104                     break;
105                 }
106             }
107         }
108         dequotedMessage += messagepart;
109     }
110     return dequotedMessage;
111 }
112
113
114 QByteArray CtcpParser::xdelimQuote(const QByteArray &message)
115 {
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());
120         ++quoteIter;
121     }
122     return quotedMessage;
123 }
124
125
126 QByteArray CtcpParser::xdelimDequote(const QByteArray &message)
127 {
128     QByteArray dequotedMessage;
129     QByteArray messagepart;
130     QHash<QByteArray, QByteArray>::iterator xdelimquote;
131
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();
138                     i++;
139                     break;
140                 }
141             }
142         }
143         dequotedMessage += messagepart;
144     }
145     return dequotedMessage;
146 }
147
148
149 void CtcpParser::processIrcEventRawNotice(IrcEventRawMessage *event)
150 {
151     parse(event, Message::Notice);
152 }
153
154
155 void CtcpParser::processIrcEventRawPrivmsg(IrcEventRawMessage *event)
156 {
157     parse(event, Message::Plain);
158 }
159
160
161 void CtcpParser::parse(IrcEventRawMessage *e, Message::Type messagetype)
162 {
163     //lowlevel message dequote
164     QByteArray dequotedMessage = lowLevelDequote(e->rawMessage());
165
166     CtcpEvent::CtcpType ctcptype = e->type() == EventManager::IrcEventRawNotice
167                                    ? CtcpEvent::Reply
168                                    : CtcpEvent::Query;
169
170     Message::Flags flags = (ctcptype == CtcpEvent::Reply && !e->network()->isChannelName(e->target()))
171                            ? Message::Redirected
172                            : Message::None;
173
174     bool isStatusMsg = false;
175
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())) {
178         isStatusMsg = true;
179         e->setTarget(e->target().remove(0, 1));
180     }
181
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))) {
186         isStatusMsg = true;
187         e->setTarget(e->target().remove(0, 1));
188     }
189
190     // If any statusmsg characters were removed, Flag the message as a StatusMsg.
191     if (isStatusMsg) {
192         flags |= Message::StatusMsg;
193     }
194
195     // For self-messages, pass the flag on to the message, too
196     if (e->testFlag(EventManager::Self)) {
197         flags |= Message::Self;
198     }
199
200     if (coreSession()->networkConfig()->standardCtcp())
201         parseStandard(e, messagetype, dequotedMessage, ctcptype, flags);
202     else
203         parseSimple(e, messagetype, dequotedMessage, ctcptype, flags);
204 }
205
206
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)
210 {
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);
213     } else {
214         int spacePos;
215         QString ctcpcmd, ctcpparam;
216
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));
222         } else {
223             ctcpcmd = targetDecode(e, ctcp);
224             ctcpparam = QString();
225         }
226         ctcpcmd = ctcpcmd.toUpper();
227
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);
238         }
239     }
240 }
241
242
243 void CtcpParser::parseStandard(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
244 {
245     QByteArray ctcp;
246
247     QList<CtcpEvent *> ctcpEvents;
248     QUuid uuid; // needed to group all replies together
249
250     // extract tagged / extended data
251     int xdelimPos = -1;
252     int xdelimEndPos = -1;
253     int spacePos = -1;
254     while ((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
255         if (xdelimPos > 0)
256             displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
257
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();
262         }
263         ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
264         dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
265
266         //dispatch the ctcp command
267         QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
268         QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
269
270         spacePos = ctcp.indexOf(' ');
271         if (spacePos != -1) {
272             ctcpcmd = targetDecode(e, ctcp.left(spacePos));
273             ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
274         }
275         else {
276             ctcpcmd = targetDecode(e, ctcp);
277             ctcpparam = QString();
278         }
279
280         ctcpcmd = ctcpcmd.toUpper();
281
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)) {
284             if (uuid.isNull())
285                 uuid = QUuid::createUuid();
286
287             CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
288                 ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
289             ctcpEvents << event;
290         }
291     }
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);
299         }
300     }
301
302     if (!dequotedMessage.isEmpty())
303         displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
304 }
305
306
307 void CtcpParser::sendCtcpEvent(CtcpEvent *e)
308 {
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())));
317             else
318                 // reply not caused by a request processed in here, so send it off immediately
319                 reply(net, bufname, e->ctcpCmd(), e->reply());
320         }
321     }
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);
326     }
327 }
328
329
330 QByteArray CtcpParser::pack(const QByteArray &ctcpTag, const QByteArray &message)
331 {
332     if (message.isEmpty())
333         return XDELIM + ctcpTag + XDELIM;
334
335     return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
336 }
337
338
339 void CtcpParser::query(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
340 {
341     QString cmd("PRIVMSG");
342
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)));
345     };
346
347     net->putCmd(cmd, net->splitMessage(cmd, message, cmdGenerator));
348 }
349
350
351 void CtcpParser::reply(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
352 {
353     QList<QByteArray> params;
354     params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
355     net->putCmd("NOTICE", params);
356 }
357
358
359 void CtcpParser::packedReply(CoreNetwork *net, const QString &bufname, const QList<QByteArray> &replies)
360 {
361     QList<QByteArray> params;
362
363     int answerSize = 0;
364     for (int i = 0; i < replies.count(); i++) {
365         answerSize += replies.at(i).size();
366     }
367
368     QByteArray quotedReply;
369     quotedReply.reserve(answerSize);
370     for (int i = 0; i < replies.count(); i++) {
371         quotedReply.append(replies.at(i));
372     }
373
374     params << net->serverEncode(bufname) << quotedReply;
375     // FIXME user proper event
376     net->putCmd("NOTICE", params);
377 }