7988437a3efc982cda2fed2254f981804bd1d61b
[quassel.git] / src / common / ircevent.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 #pragma once
22
23 #include "common-export.h"
24
25 #include <utility>
26
27 #include "networkevent.h"
28 #include "util.h"
29
30 class COMMON_EXPORT IrcEvent : public NetworkEvent
31 {
32 public:
33     explicit IrcEvent(EventManager::EventType type, Network* network, QString prefix, QStringList params = QStringList())
34         : NetworkEvent(type, network)
35         , _prefix(std::move(prefix))
36         , _params(std::move(params))
37     {}
38
39     inline QString prefix() const { return _prefix; }
40     inline void setPrefix(const QString& prefix) { _prefix = prefix; }
41
42     inline QString nick() const { return nickFromMask(prefix()); }
43
44     inline QStringList params() const { return _params; }
45     inline void setParams(const QStringList& params) { _params = params; }
46
47     static Event* create(EventManager::EventType type, QVariantMap& map, Network* network);
48
49 protected:
50     explicit IrcEvent(EventManager::EventType type, QVariantMap& map, Network* network);
51     void toVariantMap(QVariantMap& map) const override;
52
53     inline QString className() const override { return "IrcEvent"; }
54     inline void debugInfo(QDebug& dbg) const override
55     {
56         NetworkEvent::debugInfo(dbg);
57         dbg << ", prefix = " << qPrintable(prefix()) << ", params = " << params();
58     }
59
60 private:
61     QString _prefix;
62     QStringList _params;
63 };
64
65 class COMMON_EXPORT IrcEventNumeric : public IrcEvent
66 {
67 public:
68     explicit IrcEventNumeric(uint number, Network* network, const QString& prefix, QString target, const QStringList& params = QStringList())
69         : IrcEvent(EventManager::IrcEventNumeric, network, prefix, params)
70         , _number(number)
71         , _target(std::move(target))
72     {}
73
74     inline uint number() const { return _number; }
75
76     inline QString target() const { return _target; }
77     inline void setTarget(const QString& target) { _target = target; }
78
79 protected:
80     explicit IrcEventNumeric(EventManager::EventType type, QVariantMap& map, Network* network);
81     void toVariantMap(QVariantMap& map) const override;
82
83     inline QString className() const override { return "IrcEventNumeric"; }
84     inline void debugInfo(QDebug& dbg) const override
85     {
86         dbg << ", num = " << number();
87         NetworkEvent::debugInfo(dbg);
88         dbg << ", target = " << qPrintable(target()) << ", prefix = " << qPrintable(prefix()) << ", params = " << params();
89     }
90
91 private:
92     uint _number;
93     QString _target;
94
95     friend class IrcEvent;
96 };
97
98 class COMMON_EXPORT IrcEventRawMessage : public IrcEvent
99 {
100 public:
101     explicit inline IrcEventRawMessage(EventManager::EventType type,
102                                        Network* network,
103                                        QByteArray rawMessage,
104                                        const QString& prefix,
105                                        const QString& target,
106                                        const QDateTime& timestamp = QDateTime())
107         : IrcEvent(type, network, prefix, QStringList() << target)
108         , _rawMessage(std::move(rawMessage))
109     {
110         setTimestamp(timestamp);
111     }
112
113     inline QString target() const { return params().at(0); }
114     inline void setTarget(const QString& target) { setParams(QStringList() << target); }
115
116     inline QByteArray rawMessage() const { return _rawMessage; }
117     inline void setRawMessage(const QByteArray& rawMessage) { _rawMessage = rawMessage; }
118
119 protected:
120     explicit IrcEventRawMessage(EventManager::EventType type, QVariantMap& map, Network* network);
121     void toVariantMap(QVariantMap& map) const override;
122
123     inline QString className() const override { return "IrcEventRawMessage"; }
124     inline void debugInfo(QDebug& dbg) const override
125     {
126         NetworkEvent::debugInfo(dbg);
127         dbg << ", target = " << qPrintable(target()) << ", prefix = " << qPrintable(prefix()) << ", msg = " << rawMessage();
128     }
129
130 private:
131     QByteArray _rawMessage;
132
133     friend class IrcEvent;
134 };