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