modernize: Replace most remaining old-style connects by PMF ones
[quassel.git] / src / common / protocol.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 #pragma once
22
23 #include <QByteArray>
24 #include <QDateTime>
25 #include <QVariantList>
26 #include <utility>
27
28 #include "quassel.h"
29
30 namespace Protocol {
31
32 const quint32 magic = 0x42b33f00;
33
34 enum Type {
35     InternalProtocol = 0x00,
36     LegacyProtocol = 0x01,
37     DataStreamProtocol = 0x02
38 };
39
40
41 enum Feature {
42     Encryption = 0x01,
43     Compression = 0x02
44 };
45
46
47 enum class Handler {
48     SignalProxy,
49     AuthHandler
50 };
51
52
53 /*** Handshake, handled by AuthHandler ***/
54
55 struct HandshakeMessage {
56     inline Handler handler() const { return Handler::AuthHandler; }
57 };
58
59
60 struct RegisterClient : public HandshakeMessage
61 {
62     inline RegisterClient(Quassel::Features clientFeatures, QString clientVersion, QString buildDate, bool sslSupported = false)
63         : features(std::move(clientFeatures))
64         , clientVersion(std::move(clientVersion))
65         , buildDate(std::move(buildDate))
66         , sslSupported(sslSupported)
67     {}
68
69     Quassel::Features features;
70     QString clientVersion;
71     QString buildDate;
72
73     // this is only used by the LegacyProtocol in compat mode
74     bool sslSupported;
75 };
76
77
78 struct ClientDenied : public HandshakeMessage
79 {
80     inline ClientDenied(QString errorString)
81     : errorString(std::move(errorString)) {}
82
83     QString errorString;
84 };
85
86
87 struct ClientRegistered : public HandshakeMessage
88 {
89     inline ClientRegistered(Quassel::Features coreFeatures, bool coreConfigured, QVariantList backendInfo, QVariantList authenticatorInfo, bool sslSupported)
90         : features(std::move(coreFeatures))
91         , coreConfigured(coreConfigured)
92         , backendInfo(std::move(backendInfo))
93         , authenticatorInfo(std::move(authenticatorInfo))
94         , sslSupported(sslSupported)
95     {}
96
97     Quassel::Features features;
98     bool coreConfigured;
99     QVariantList backendInfo; // TODO: abstract this better
100
101     // The authenticatorInfo should be optional!
102     QVariantList authenticatorInfo;
103
104     // this is only used by the LegacyProtocol in compat mode
105     bool sslSupported;
106 };
107
108
109 struct SetupData : public HandshakeMessage
110 {
111     inline SetupData(QString adminUser, QString adminPassword, QString backend,
112                      QVariantMap setupData, QString authenticator = QString(),
113                      QVariantMap authSetupData = QVariantMap())
114     : adminUser(std::move(adminUser))
115     , adminPassword(std::move(adminPassword))
116     , backend(std::move(backend))
117     , setupData(std::move(setupData))
118     , authenticator(std::move(authenticator))
119     , authSetupData(std::move(authSetupData))
120     {}
121
122     QString adminUser;
123     QString adminPassword;
124     QString backend;
125     QVariantMap setupData;
126     QString authenticator;
127     QVariantMap authSetupData;
128 };
129
130
131 struct SetupFailed : public HandshakeMessage
132 {
133     inline SetupFailed(QString errorString)
134     : errorString(std::move(errorString)) {}
135
136     QString errorString;
137 };
138
139
140 struct SetupDone : public HandshakeMessage
141 {
142 };
143
144
145 struct Login : public HandshakeMessage
146 {
147     inline Login(QString user, QString password)
148     : user(std::move(user)), password(std::move(password)) {}
149
150     QString user;
151     QString password;
152 };
153
154
155 struct LoginFailed : public HandshakeMessage
156 {
157     inline LoginFailed(QString errorString)
158     : errorString(std::move(errorString)) {}
159
160     QString errorString;
161 };
162
163
164 struct LoginSuccess : public HandshakeMessage
165 {
166 };
167
168
169 // TODO: more generic format
170 struct SessionState : public HandshakeMessage
171 {
172     inline SessionState() = default; // needed for QMetaType (for the mono client)
173     inline SessionState(QVariantList identities, QVariantList bufferInfos, QVariantList networkIds)
174     : identities(std::move(identities)), bufferInfos(std::move(bufferInfos)), networkIds(std::move(networkIds)) {}
175
176     QVariantList identities;
177     QVariantList bufferInfos;
178     QVariantList networkIds;
179 };
180
181 /*** handled by SignalProxy ***/
182
183 struct SignalProxyMessage
184 {
185     inline Handler handler() const { return Handler::SignalProxy; }
186 };
187
188
189 struct SyncMessage : public SignalProxyMessage
190 {
191     SyncMessage() = default;
192     SyncMessage(QByteArray className, QString objectName, QByteArray slotName, QVariantList params)
193         : className(std::move(className)), objectName(std::move(objectName)), slotName(std::move(slotName)), params(std::move(params)) {}
194
195     QByteArray className;
196     QString objectName;
197     QByteArray slotName;
198     QVariantList params;
199 };
200
201
202 struct RpcCall : public SignalProxyMessage
203 {
204     RpcCall() = default;
205     RpcCall(QByteArray slotName, QVariantList params)
206         : slotName(std::move(slotName)), params(std::move(params)) {}
207
208     QByteArray slotName;
209     QVariantList params;
210 };
211
212
213 struct InitRequest : public SignalProxyMessage
214 {
215     InitRequest() = default;
216     InitRequest(QByteArray className, QString objectName)
217         : className(std::move(className)), objectName(std::move(objectName)) {}
218
219     QByteArray className;
220     QString objectName;
221 };
222
223
224 struct InitData : public SignalProxyMessage
225 {
226     InitData() = default;
227     InitData(QByteArray className, QString objectName, QVariantMap initData)
228         : className(std::move(className)), objectName(std::move(objectName)), initData(std::move(initData)) {}
229
230     QByteArray className;
231     QString objectName;
232     QVariantMap initData;
233 };
234
235
236 /*** handled by RemoteConnection ***/
237
238 struct HeartBeat
239 {
240     inline HeartBeat(QDateTime timestamp) : timestamp(std::move(timestamp)) {}
241
242     QDateTime timestamp;
243 };
244
245
246 struct HeartBeatReply
247 {
248     inline HeartBeatReply(QDateTime timestamp) : timestamp(std::move(timestamp)) {}
249
250     QDateTime timestamp;
251 };
252
253
254 }
255
256 // Required for InternalPeer
257 Q_DECLARE_METATYPE(Protocol::SyncMessage)
258 Q_DECLARE_METATYPE(Protocol::RpcCall)
259 Q_DECLARE_METATYPE(Protocol::InitRequest)
260 Q_DECLARE_METATYPE(Protocol::InitData)