1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
use failure::Error;
use std::result::Result;
use crate::error::ProtocolError;
use crate::primitive::{StringList, Variant, VariantList};
mod types;
use crate::primitive::VariantMap;
use crate::{HandshakeDeserialize, HandshakeSerialize};
use crate::match_variant;
/// Data received right after initializing the connection
///
/// ConnAck is serialized sequentially
#[derive(Debug)]
pub struct ConnAck {
/// The Flag 0x01 for TLS
/// and 0x02 for Deflate Compression
flags: u8,
/// Some extra protocol version specific data
/// So far unused
extra: i16,
/// The version of the protocol
/// 0x00000001 for the legacy protocol
/// 0x00000002 for the datastream protocol
///
/// Only the datastream protocol is supported by this crate
version: i8,
}
impl Default for ConnAck {
fn default() -> Self {
Self {
flags: 0x00,
extra: 0x00,
version: 0x00000002,
}
}
}
impl crate::Serialize for ConnAck {
fn serialize(&self) -> Result<Vec<std::primitive::u8>, Error> {
let mut bytes: Vec<u8> = Vec::new();
bytes.append(&mut self.flags.serialize()?);
bytes.append(&mut self.extra.serialize()?);
bytes.append(&mut self.version.serialize()?);
Ok(bytes)
}
}
impl crate::Deserialize for ConnAck {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (flen, flags) = u8::parse(b)?;
let (elen, extra) = i16::parse(&b[flen..])?;
let (vlen, version) = i8::parse(&b[(flen + elen)..])?;
return Ok((
flen + elen + vlen,
Self {
flags,
extra,
version,
},
));
}
}
/// ClientInit is the Initial message send to the core after establishing a base layer comunication.
///
/// Features
///
/// | Flag | Name | Description |
/// | ---- | ---- | ----------- |
/// | 0x00000001 | SynchronizedMarkerLine | -- |
/// | 0x00000002 | SaslAuthentication | -- |
/// | 0x00000004 | SaslExternal | -- |
/// | 0x00000008 | HideInactiveNetworks | -- |
/// | 0x00000010 | PasswordChange | -- |
/// | 0x00000020 | CapNegotiation | IRCv3 capability negotiation, account tracking |
/// | 0x00000040 | VerifyServerSSL | IRC server SSL validation |
/// | 0x00000080 | CustomRateLimits | IRC server custom message rate limits |
/// | 0x00000100 | DccFileTransfer | Currently not supported |
/// | 0x00000200 | AwayFormatTimestamp | Timestamp formatting in away (e.g. %%hh:mm%%) |
/// | 0x00000400 | Authenticators | Support for exchangeable auth backends |
/// | 0x00000800 | BufferActivitySync | Sync buffer activity status |
/// | 0x00001000 | CoreSideHighlights | Core-Side highlight configuration and matching |
/// | 0x00002000 | SenderPrefixes | Show prefixes for senders in backlog |
/// | 0x00004000 | RemoteDisconnect | Supports RPC call disconnectFromCore to remotely disconnect a client |
/// | 0x00008000 | ExtendedFeatures | Transmit features as list of strings |
/// | -- | LongTime | Serialize message time as 64-bit |
/// | -- | RichMessages | Real Name and Avatar URL in backlog |
/// | -- | BacklogFilterType | Backlogmanager supports filtering backlog by messagetype |
/// | -- | EcdsaCertfpKeys | ECDSA keys for CertFP in identities |
/// | -- | LongMessageId | 64-bit IDs for messages |
/// | -- | SyncedCoreInfo | CoreInfo dynamically updated using signals |
#[derive(Debug)]
pub struct ClientInit {
/// Version of the client
pub client_version: String,
/// Build date of the client
pub client_date: String,
/// supported features as bitflags
pub client_features: u32,
/// List of supported extended features
pub feature_list: StringList,
}
impl HandshakeSerialize for ClientInit {
fn serialize(&self) -> Result<Vec<u8>, Error> {
let mut values: VariantMap = VariantMap::with_capacity(5);
values.insert(
"MsgType".to_string(),
Variant::String("ClientInit".to_string()),
);
values.insert(
"ClientVersion".to_string(),
Variant::String(self.client_version.clone()),
);
values.insert(
"ClientDate".to_string(),
Variant::String(self.client_date.clone()),
);
values.insert("Features".to_string(), Variant::u32(self.client_features));
values.insert(
"FeatureList".to_string(),
Variant::StringList(self.feature_list.clone()),
);
return HandshakeSerialize::serialize(&values);
}
}
impl HandshakeDeserialize for ClientInit {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8);
if msgtype == "ClientInit" {
return Ok((
len,
Self {
client_version: match_variant!(values["ClientVersion"], Variant::String),
client_date: match_variant!(values["ClientDate"], Variant::String),
feature_list: match_variant!(values["FeatureList"], Variant::StringList),
client_features: match_variant!(values["Features"], Variant::u32),
},
));
} else {
bail!(ProtocolError::WrongMsgType);
}
}
}
/// ClientInitReject is received when the initialization fails
#[derive(Debug)]
pub struct ClientInitReject {
/// String with an error message of what went wrong
pub error_string: String,
}
impl HandshakeSerialize for ClientInitReject {
fn serialize(&self) -> Result<Vec<u8>, Error> {
let mut values: VariantMap = VariantMap::with_capacity(2);
values.insert(
"MsgType".to_string(),
Variant::String("ClientInitReject".to_string()),
);
values.insert(
"ErrorString".to_string(),
Variant::String(self.error_string.clone()),
);
return HandshakeSerialize::serialize(&values);
}
}
impl HandshakeDeserialize for ClientInitReject {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8);
if msgtype == "ClientInitReject" {
return Ok((
len,
Self {
error_string: match_variant!(values["ErrorString"], Variant::String),
},
));
} else {
bail!(ProtocolError::WrongMsgType);
}
}
}
/// ClientInitAck is received when the initialization was successfull
#[derive(Debug)]
pub struct ClientInitAck {
/// Flags of supported legacy features
pub core_features: u32,
/// If the core has already been configured
pub core_configured: bool,
/// List of VariantMaps of info on available backends
pub storage_backends: VariantList,
/// List of VariantMaps of info on available authenticators
pub authenticators: VariantList,
/// List of supported extended features
pub feature_list: StringList,
}
impl HandshakeSerialize for ClientInitAck {
fn serialize(&self) -> Result<Vec<u8>, Error> {
let mut values: VariantMap = VariantMap::with_capacity(6);
values.insert(
"MsgType".to_string(),
Variant::String("ClientInitAck".to_string()),
);
values.insert("CoreFeatures".to_string(), Variant::u32(self.core_features));
values.insert(
"Configured".to_string(),
Variant::bool(self.core_configured),
);
values.insert(
"StorageBackends".to_string(),
Variant::VariantList(self.storage_backends.clone()),
);
values.insert(
"Authenticators".to_string(),
Variant::VariantList(self.authenticators.clone()),
);
values.insert(
"FeatureList".to_string(),
Variant::StringList(self.feature_list.clone()),
);
return HandshakeSerialize::serialize(&values);
}
}
impl HandshakeDeserialize for ClientInitAck {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8);
if msgtype == "ClientInitAck" {
return Ok((
len,
Self {
core_features: 0x00008000,
core_configured: match_variant!(values["Configured"], Variant::bool),
storage_backends: match_variant!(
values["StorageBackends"],
Variant::VariantList
),
authenticators: match_variant!(values["Authenticators"], Variant::VariantList),
feature_list: match_variant!(values["FeatureList"], Variant::StringList),
},
));
} else {
bail!(ProtocolError::WrongMsgType);
}
}
}
/// Login to the core with user data
/// username and password are transmitted in plain text
#[derive(Debug)]
pub struct ClientLogin {
pub user: String,
pub password: String,
}
impl HandshakeSerialize for ClientLogin {
fn serialize(&self) -> Result<Vec<u8>, Error> {
let mut values: VariantMap = VariantMap::new();
values.insert(
"MsgType".to_string(),
Variant::String("ClientLogin".to_string()),
);
values.insert("User".to_string(), Variant::String(self.user.clone()));
values.insert(
"Password".to_string(),
Variant::String(self.password.clone()),
);
return HandshakeSerialize::serialize(&values);
}
}
impl HandshakeDeserialize for ClientLogin {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8);
if msgtype == "ClientLogin" {
return Ok((
len,
Self {
user: match_variant!(values["User"], Variant::String),
password: match_variant!(values["Password"], Variant::String),
},
));
} else {
bail!(ProtocolError::WrongMsgType);
}
}
}
/// ClientLoginAck is received after the client has successfully logged in
/// it has no fields
#[derive(Debug)]
pub struct ClientLoginAck;
impl HandshakeSerialize for ClientLoginAck {
fn serialize(&self) -> Result<Vec<u8>, Error> {
let mut values: VariantMap = VariantMap::with_capacity(1);
values.insert(
"MsgType".to_string(),
Variant::String("ClientLoginAck".to_string()),
);
return HandshakeSerialize::serialize(&values);
}
}
impl HandshakeDeserialize for ClientLoginAck {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8);
if msgtype == "ClientLogin" {
return Ok((len, Self {}));
} else {
bail!(ProtocolError::WrongMsgType);
}
}
}
/// ClientLoginReject is received after the client failed to login
/// It contains an error message as String
#[derive(Debug)]
pub struct ClientLoginReject {
error: String,
}
impl HandshakeSerialize for ClientLoginReject {
fn serialize(&self) -> Result<Vec<u8>, Error> {
let mut values: VariantMap = VariantMap::with_capacity(1);
values.insert(
"MsgType".to_string(),
Variant::String("ClientLoginReject".to_string()),
);
values.insert(
"ErrorString".to_string(),
Variant::String(self.error.clone()),
);
return HandshakeSerialize::serialize(&values);
}
}
impl HandshakeDeserialize for ClientLoginReject {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8);
if msgtype == "ClientLogin" {
return Ok((
len,
Self {
error: match_variant!(values["ErrorString"], Variant::String),
},
));
} else {
bail!(ProtocolError::WrongMsgType);
}
}
}
/// SessionInit is received along with ClientLoginAck to initialize that user Session
// TODO Replace with proper types
#[derive(Debug)]
pub struct SessionInit {
/// List of all configured identities
identities: VariantList,
/// List of all existing buffers
buffers: VariantList,
/// Ids of all networks
network_ids: VariantList,
}
impl HandshakeSerialize for SessionInit {
fn serialize(&self) -> Result<Vec<u8>, Error> {
let mut values: VariantMap = VariantMap::with_capacity(1);
values.insert(
"MsgType".to_string(),
Variant::String("SessionInit".to_string()),
);
values.insert(
"Identities".to_string(),
Variant::VariantList(self.identities.clone()),
);
values.insert(
"BufferInfos".to_string(),
Variant::VariantList(self.buffers.clone()),
);
values.insert(
"NetworkIds".to_string(),
Variant::VariantList(self.network_ids.clone()),
);
return HandshakeSerialize::serialize(&values);
}
}
impl HandshakeDeserialize for SessionInit {
fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8);
if msgtype == "ClientLogin" {
return Ok((
len,
Self {
identities: match_variant!(values["Identities"], Variant::VariantList),
buffers: match_variant!(values["BufferInfos"], Variant::VariantList),
network_ids: match_variant!(values["NetworkIds"], Variant::VariantList),
},
));
} else {
bail!(ProtocolError::WrongMsgType);
}
}
}
|