modernize: Pass arguments by value and move in constructors
[quassel.git] / src / core / ctcpparser.h
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 #ifndef CTCPPARSER_H
22 #define CTCPPARSER_H
23
24 #include <QUuid>
25 #include <utility>
26
27 #include "corenetwork.h"
28 #include "eventmanager.h"
29 #include "ircevent.h"
30 #include "ctcpevent.h"
31
32 class CoreSession;
33 class CtcpEvent;
34
35 class CtcpParser : public QObject
36 {
37     Q_OBJECT
38
39 public:
40     CtcpParser(CoreSession *coreSession, QObject *parent = nullptr);
41
42     inline CoreSession *coreSession() const { return _coreSession; }
43
44     void query(CoreNetwork *network, const QString &bufname, const QString &ctcpTag, const QString &message);
45     void reply(CoreNetwork *network, const QString &bufname, const QString &ctcpTag, const QString &message);
46
47     Q_INVOKABLE void processIrcEventRawNotice(IrcEventRawMessage *event);
48     Q_INVOKABLE void processIrcEventRawPrivmsg(IrcEventRawMessage *event);
49
50     Q_INVOKABLE void sendCtcpEvent(CtcpEvent *event);
51
52 signals:
53     void newEvent(Event *event);
54
55 protected:
56     inline CoreNetwork *coreNetwork(NetworkEvent *e) const { return qobject_cast<CoreNetwork *>(e->network()); }
57
58     // FIXME duplicates functionality in EventStringifier, maybe want to put that in something common
59     //! Creates and sends a MessageEvent
60     void displayMsg(NetworkEvent *event,
61         Message::Type msgType,
62         const QString &msg,
63         const QString &sender = QString(),
64         const QString &target = QString(),
65         Message::Flags msgFlags = Message::None);
66
67     void parse(IrcEventRawMessage *event, Message::Type msgType);
68     void parseSimple(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags);
69     void parseStandard(IrcEventRawMessage *e, Message::Type messagetype, QByteArray dequotedMessage, CtcpEvent::CtcpType ctcptype, Message::Flags flags);
70
71     QByteArray lowLevelQuote(const QByteArray &);
72     QByteArray lowLevelDequote(const QByteArray &);
73     QByteArray xdelimQuote(const QByteArray &);
74     QByteArray xdelimDequote(const QByteArray &);
75
76     QByteArray pack(const QByteArray &ctcpTag, const QByteArray &message);
77     void packedReply(CoreNetwork *network, const QString &bufname, const QList<QByteArray> &replies);
78
79 private slots:
80     void setStandardCtcp(bool enabled);
81
82 private:
83     inline QString targetDecode(IrcEventRawMessage *e, const QByteArray &msg) { return coreNetwork(e)->userDecode(e->target(), msg); }
84
85     CoreSession *_coreSession;
86
87     struct CtcpReply {
88         CoreNetwork *network;
89         QString bufferName;
90         QList<QByteArray> replies;
91
92         CtcpReply() : network(nullptr) {}
93         CtcpReply(CoreNetwork *net, QString buf) : network(net), bufferName(std::move(buf)) {}
94     };
95
96     QHash<QUuid, CtcpReply> _replies;
97
98     QHash<QByteArray, QByteArray> _ctcpMDequoteHash;
99     QHash<QByteArray, QByteArray> _ctcpXDelimDequoteHash;
100 };
101
102
103 #endif