Handle STATUSMSG messages
[quassel.git] / src / core / ctcpparser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2015 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     if (coreSession()->networkConfig()->standardCtcp())
196         parseStandard(e, messagetype, dequotedMessage, ctcptype, flags);
197     else
198         parseSimple(e, messagetype, dequotedMessage, ctcptype, flags);
199 }
200
201
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)
205 {
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);
208     } else {
209         int spacePos;
210         QString ctcpcmd, ctcpparam;
211
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));
217         } else {
218             ctcpcmd = targetDecode(e, ctcp);
219             ctcpparam = QString();
220         }
221         ctcpcmd = ctcpcmd.toUpper();
222
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);
233         }
234     }
235 }
236
237
238 void CtcpParser::parseStandard(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags)
239 {
240     QByteArray ctcp;
241
242     QList<CtcpEvent *> ctcpEvents;
243     QUuid uuid; // needed to group all replies together
244
245     // extract tagged / extended data
246     int xdelimPos = -1;
247     int xdelimEndPos = -1;
248     int spacePos = -1;
249     while ((xdelimPos = dequotedMessage.indexOf(XDELIM)) != -1) {
250         if (xdelimPos > 0)
251             displayMsg(e, messagetype, targetDecode(e, dequotedMessage.left(xdelimPos)), e->prefix(), e->target(), flags);
252
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();
257         }
258         ctcp = xdelimDequote(dequotedMessage.mid(xdelimPos + 1, xdelimEndPos - xdelimPos - 1));
259         dequotedMessage = dequotedMessage.mid(xdelimEndPos + 1);
260
261         //dispatch the ctcp command
262         QString ctcpcmd = targetDecode(e, ctcp.left(spacePos));
263         QString ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
264
265         spacePos = ctcp.indexOf(' ');
266         if (spacePos != -1) {
267             ctcpcmd = targetDecode(e, ctcp.left(spacePos));
268             ctcpparam = targetDecode(e, ctcp.mid(spacePos + 1));
269         }
270         else {
271             ctcpcmd = targetDecode(e, ctcp);
272             ctcpparam = QString();
273         }
274
275         ctcpcmd = ctcpcmd.toUpper();
276
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)) {
279             if (uuid.isNull())
280                 uuid = QUuid::createUuid();
281
282             CtcpEvent *event = new CtcpEvent(EventManager::CtcpEvent, e->network(), e->prefix(), e->target(),
283                 ctcptype, ctcpcmd, ctcpparam, e->timestamp(), uuid);
284             ctcpEvents << event;
285         }
286     }
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);
294         }
295     }
296
297     if (!dequotedMessage.isEmpty())
298         displayMsg(e, messagetype, targetDecode(e, dequotedMessage), e->prefix(), e->target(), flags);
299 }
300
301
302 void CtcpParser::sendCtcpEvent(CtcpEvent *e)
303 {
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())));
312             else
313                 // reply not caused by a request processed in here, so send it off immediately
314                 reply(net, bufname, e->ctcpCmd(), e->reply());
315         }
316     }
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);
321     }
322 }
323
324
325 QByteArray CtcpParser::pack(const QByteArray &ctcpTag, const QByteArray &message)
326 {
327     if (message.isEmpty())
328         return XDELIM + ctcpTag + XDELIM;
329
330     return XDELIM + ctcpTag + ' ' + xdelimQuote(message) + XDELIM;
331 }
332
333
334 void CtcpParser::query(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
335 {
336     QString cmd("PRIVMSG");
337
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)));
340     };
341
342     net->putCmd(cmd, net->splitMessage(cmd, message, cmdGenerator));
343 }
344
345
346 void CtcpParser::reply(CoreNetwork *net, const QString &bufname, const QString &ctcpTag, const QString &message)
347 {
348     QList<QByteArray> params;
349     params << net->serverEncode(bufname) << lowLevelQuote(pack(net->serverEncode(ctcpTag), net->userEncode(bufname, message)));
350     net->putCmd("NOTICE", params);
351 }
352
353
354 void CtcpParser::packedReply(CoreNetwork *net, const QString &bufname, const QList<QByteArray> &replies)
355 {
356     QList<QByteArray> params;
357
358     int answerSize = 0;
359     for (int i = 0; i < replies.count(); i++) {
360         answerSize += replies.at(i).size();
361     }
362
363     QByteArray quotedReply;
364     quotedReply.reserve(answerSize);
365     for (int i = 0; i < replies.count(); i++) {
366         quotedReply.append(replies.at(i));
367     }
368
369     params << net->serverEncode(bufname) << quotedReply;
370     // FIXME user proper event
371     net->putCmd("NOTICE", params);
372 }