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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use crate::{from_cstring, ChannelRef, IrcIdent, IrcIdentRef, UserString};
use std::os::raw::c_char;
pub trait ServerEvent {
const NAME: &'static str;
#[doc(hidden)]
unsafe fn create(word: *mut *mut c_char, word_eol: *mut *mut c_char) -> Self;
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct PRIVMSG {
user: UserString,
target: PrivmsgTarget,
message: String,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum PrivmsgTarget {
User(IrcIdent),
Channel {
channel_name: IrcIdent,
channel: ChannelRef,
},
HostMask(IrcIdent),
ServerMask(IrcIdent),
}
impl PRIVMSG {
pub fn get_user(&self) -> &UserString {
&self.user
}
pub fn get_target(&self) -> &PrivmsgTarget {
&self.target
}
pub fn get_message(&self) -> &str {
&self.message
}
}
impl ServerEvent for PRIVMSG {
const NAME: &'static str = "PRIVMSG";
unsafe fn create(word: *mut *mut c_char, word_eol: *mut *mut c_char) -> Self {
let arg1 = *word.offset(1);
let user_string = from_cstring(arg1.offset(1));
let user = UserString::new(user_string).unwrap();
let arg3 = *word.offset(3);
let target = match *arg3 as u8 {
b'#' => {
let mut target_string = IrcIdent(from_cstring(arg3));
if target_string.contains('*') {
target_string.0.remove(1);
PrivmsgTarget::HostMask(target_string)
} else {
let channel = crate::get_server_name()
.and_then(|s| crate::get_channel(&s, &target_string))
.unwrap_or_else(|| crate::get_first_channel(&target_string).unwrap());
PrivmsgTarget::Channel {
channel,
channel_name: target_string,
}
}
}
b'$' => PrivmsgTarget::ServerMask(IrcIdent(from_cstring(arg3.offset(1)))),
_ => PrivmsgTarget::User(IrcIdent(from_cstring(arg3))),
};
let arg4_eol = *word_eol.offset(4);
let message = from_cstring(arg4_eol.offset(1));
Self {
user,
target,
message,
}
}
}
pub struct JOIN {
user: UserString,
channel_string: IrcIdent,
channel: ChannelRef,
}
impl JOIN {
pub fn get_user(&self) -> &UserString {
&self.user
}
pub fn get_channel_name(&self) -> IrcIdentRef {
self.channel_string.as_ref()
}
pub fn get_channel(&self) -> &ChannelRef {
&self.channel
}
}
impl ServerEvent for JOIN {
const NAME: &'static str = "JOIN";
unsafe fn create(word: *mut *mut c_char, _word_eol: *mut *mut c_char) -> Self {
let arg1 = *word.offset(1);
let user_string = from_cstring(arg1.offset(1));
let user = UserString::new(user_string).unwrap();
let arg3 = *word.offset(3);
let channel_string = IrcIdent(from_cstring(arg3.offset(1)));
let channel = crate::get_server_name()
.and_then(|s| crate::get_channel(&s, &channel_string))
.unwrap_or_else(|| crate::get_first_channel(&channel_string).unwrap());
Self {
user,
channel_string,
channel,
}
}
}
pub struct QUIT {
user: UserString,
message: Option<String>,
}
impl QUIT {
pub fn get_user(&self) -> &UserString {
&self.user
}
pub fn get_message(&self) -> Option<&str> {
self.message.as_ref().map(|s| &**s)
}
}
impl ServerEvent for QUIT {
const NAME: &'static str = "QUIT";
unsafe fn create(word: *mut *mut c_char, word_eol: *mut *mut c_char) -> Self {
let arg1 = *word.offset(1);
let user_string = from_cstring(arg1.offset(1));
let user = UserString::new(user_string).unwrap();
let arg3_eol_ptr = word_eol.offset(3);
let message = if arg3_eol_ptr.is_null() {
None
} else {
let arg3_eol = *arg3_eol_ptr;
if arg3_eol.is_null() || *arg3_eol == b'\0' as _ || *arg3_eol.offset(1) == b'\0' as _ {
None
} else {
Some(from_cstring(arg3_eol.offset(1)))
}
};
Self { user, message }
}
}
pub struct PART {
user: UserString,
channel_names: Vec<IrcIdent>,
channels: Vec<ChannelRef>,
message: Option<String>,
}
impl PART {
pub fn get_user(&self) -> &UserString {
&self.user
}
pub fn get_channel_names(&self) -> &[IrcIdent] {
&self.channel_names
}
pub fn get_channels(&self) -> &[ChannelRef] {
&self.channels
}
pub fn get_message(&self) -> Option<&str> {
self.message.as_ref().map(|s| &**s)
}
}
impl ServerEvent for PART {
const NAME: &'static str = "PART";
unsafe fn create(word: *mut *mut c_char, _word_eol: *mut *mut c_char) -> Self {
let arg1 = *word.offset(1);
let user_string = from_cstring(arg1.offset(1));
let user = UserString::new(user_string).unwrap();
let arg3 = *word.offset(3);
let channel_string = from_cstring(arg3);
let channel_names: Vec<_> = channel_string
.split(',')
.map(|t| IrcIdent(t.to_string()))
.collect();
let server = crate::get_server_name();
let channels = channel_names
.iter()
.map(|c| {
server
.as_ref()
.and_then(|s| crate::get_channel(s, c))
.unwrap_or_else(|| crate::get_first_channel(c).unwrap())
})
.collect();
let arg4_eol_ptr = word.offset(4);
let message = if arg4_eol_ptr.is_null() {
None
} else {
let arg4_eol = *arg4_eol_ptr;
if arg4_eol.is_null() || *arg4_eol == b'\0' as _ || *arg4_eol.offset(1) == b'\0' as _ {
None
} else {
Some(from_cstring(arg4_eol.offset(1)))
}
};
Self {
user,
channel_names,
channels,
message,
}
}
}
pub struct TOPIC {
user: UserString,
channel_string: IrcIdent,
channel: ChannelRef,
message: Option<String>,
}
impl TOPIC {
pub fn get_user(&self) -> &UserString {
&self.user
}
pub fn get_channel_name(&self) -> IrcIdentRef {
self.channel_string.as_ref()
}
pub fn get_channel(&self) -> &ChannelRef {
&self.channel
}
pub fn get_message(&self) -> Option<&str> {
self.message.as_ref().map(|s| &**s)
}
}
impl ServerEvent for TOPIC {
const NAME: &'static str = "TOPIC";
unsafe fn create(word: *mut *mut c_char, word_eol: *mut *mut c_char) -> Self {
let arg1 = *word.offset(1);
let user_string = from_cstring(arg1.offset(1));
let user = UserString::new(user_string).unwrap();
let arg3 = *word.offset(3);
let channel_string = IrcIdent(from_cstring(arg3));
let channel = crate::get_server_name()
.and_then(|s| crate::get_channel(&s, &channel_string))
.unwrap_or_else(|| crate::get_first_channel(&channel_string).unwrap());
let arg4_eol_ptr = word_eol.offset(4);
let message = if arg4_eol_ptr.is_null() {
None
} else {
let arg4_eol = *arg4_eol_ptr;
if arg4_eol.is_null() || *arg4_eol == b'\0' as _ || *arg4_eol.offset(1) == b'\0' as _ {
None
} else {
Some(from_cstring(arg4_eol.offset(1)))
}
};
Self {
user,
channel_string,
channel,
message,
}
}
}
pub struct INVITE {
sender: UserString,
recipient: IrcIdent,
channel_string: IrcIdent,
channel: ChannelRef,
}
impl INVITE {
pub fn get_sender(&self) -> &UserString {
&self.sender
}
pub fn get_recipient(&self) -> IrcIdentRef {
self.recipient.as_ref()
}
pub fn get_channel_name(&self) -> IrcIdentRef {
self.channel_string.as_ref()
}
pub fn get_channel(&self) -> &ChannelRef {
&self.channel
}
}
impl ServerEvent for INVITE {
const NAME: &'static str = "INVITE";
unsafe fn create(word: *mut *mut c_char, _word_eol: *mut *mut c_char) -> Self {
let arg1 = *word.offset(1);
let sender_string = from_cstring(arg1.offset(1));
let sender = UserString::new(sender_string).unwrap();
let arg3 = *word.offset(3);
let recipient = IrcIdent(from_cstring(arg3));
let arg4 = *word.offset(4);
let channel_string = IrcIdent(from_cstring(arg4));
let channel = crate::get_server_name()
.and_then(|s| crate::get_channel(&s, &channel_string))
.unwrap_or_else(|| crate::get_first_channel(&channel_string).unwrap());
Self {
sender,
recipient,
channel_string,
channel,
}
}
}
pub struct KICK {
sender: UserString,
channel: ChannelRef,
channel_string: IrcIdent,
kicked: IrcIdent,
comment: Option<String>,
}
impl KICK {
pub fn get_sender(&self) -> &UserString {
&self.sender
}
pub fn get_channel(&self) -> &ChannelRef {
&self.channel
}
pub fn get_channel_name(&self) -> IrcIdentRef {
self.channel_string.as_ref()
}
pub fn get_kicked(&self) -> IrcIdentRef {
self.kicked.as_ref()
}
pub fn get_comment(&self) -> Option<&str> {
self.comment.as_ref().map(|s| &**s)
}
}
impl ServerEvent for KICK {
const NAME: &'static str = "KICK";
unsafe fn create(word: *mut *mut c_char, word_eol: *mut *mut c_char) -> Self {
let arg1 = *word.offset(1);
let sender_string = from_cstring(arg1.offset(1));
let sender = UserString::new(sender_string).unwrap();
let arg3 = *word.offset(3);
let channel_string = IrcIdent(from_cstring(arg3));
let channel = crate::get_server_name()
.and_then(|s| crate::get_channel(&s, &channel_string))
.unwrap_or_else(|| crate::get_first_channel(&channel_string).unwrap());
let arg4 = *word.offset(4);
let kicked = IrcIdent(from_cstring(arg4));
let arg5_eol_ptr = word_eol.offset(5);
let comment = if arg5_eol_ptr.is_null() {
None
} else {
let arg5_eol = *arg5_eol_ptr;
if arg5_eol.is_null() || *arg5_eol == b'\0' as _ || *arg5_eol.offset(1) == b'\0' as _ {
None
} else {
Some(from_cstring(arg5_eol.offset(1)))
}
};
Self {
sender,
channel,
channel_string,
comment,
kicked,
}
}
}
pub struct NOTICE {
privmsg: PRIVMSG,
}
impl NOTICE {
pub fn get_user(&self) -> &UserString {
self.privmsg.get_user()
}
pub fn get_target(&self) -> &PrivmsgTarget {
self.privmsg.get_target()
}
pub fn get_message(&self) -> &str {
self.privmsg.get_message()
}
}
impl ServerEvent for NOTICE {
const NAME: &'static str = "NOTICE";
unsafe fn create(word: *mut *mut c_char, word_eol: *mut *mut c_char) -> Self {
Self {
privmsg: PRIVMSG::create(word, word_eol),
}
}
}
pub struct WALLOPS {
server_name: IrcIdent,
message: String,
}
impl WALLOPS {
pub fn get_server_name(&self) -> IrcIdentRef {
self.server_name.as_ref()
}
pub fn get_message(&self) -> &str {
&self.message
}
}
impl ServerEvent for WALLOPS {
const NAME: &'static str = "WALLOPS";
unsafe fn create(word: *mut *mut c_char, word_eol: *mut *mut c_char) -> Self {
let arg1 = *word.offset(1);
let server_name = IrcIdent(from_cstring(arg1.offset(1)));
let arg3 = *word_eol.offset(3);
let message = from_cstring(arg3.offset(1));
Self {
server_name,
message,
}
}
}