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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
/*
* This file is part of CowIRC.
*
* CowIRC is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CowIRC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with CowIRC. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::Message;
use std::fmt;
/// Represents different IRC events from the server.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum IncomingIrcEvent {
Join(String, String),
Part(String, String, Option<String>),
Privmsg(String, String, String),
Notice(String, String, String),
Kick(String, String, String, String),
Mode(String, String, String),
Ping(String),
Welcome(String),
Isupport(Vec<(String, Option<String>)>),
Cap(String, Vec<String>),
Topic(String, String, String),
Names(String, Vec<String>),
EndOfNames(String),
WhoisUser(String, String, String, String),
WhoisChannels(String, Vec<String>),
WhoisServer(String, String, String),
WhoisLoggedInAs(String, String),
WhoisIdle(String, String),
EndOfWhois(String),
WhoisSecure(String, String),
Unknown(String),
}
/// Represents errors that might occur parsing the server response.
#[derive(Debug)]
pub enum IncomingIrcEventError {
ParserError(crate::parser::Error),
MissingParameter(String),
MissingSource,
MissingKey,
}
impl fmt::Display for IncomingIrcEventError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::ParserError(e) => write!(f, "Parser error: {e}"),
Self::MissingParameter(s) => write!(f, "Missing parameter: {s}"),
Self::MissingSource => write!(f, "Missing source"),
Self::MissingKey => write!(f, "Missing key"),
}
}
}
impl IncomingIrcEvent {
/// Handles a `Join` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if a source for the Join is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from(":nick!ident@host JOIN #channel");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Join(channel, nick) => {
/// println!("Channel: {channel}");
/// println!("Nick: {nick}");
/// },
/// _ => eprintln!("error: not a `Join` event!"),
/// }
/// ```
pub fn handle_join(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let channel = parsed_message.get_param(0)?;
let nickname = &parsed_message
.source
.as_ref()
.ok_or(IncomingIrcEventError::MissingSource)?
.nickname;
Ok((
Self::Join(channel.to_string(), nickname.to_string()),
parsed_message,
))
}
/// Handles a `Part` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if a source for the Part is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from(":nick!ident@host PART #channel :Goodbye, world!");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Part(channel, nick, part_message) => {
/// println!("Channel: {channel}");
/// println!("Nick: {nick}");
/// println!("Part Message: {:?}", part_message)
/// },
/// _ => eprintln!("error: not a `Part` event!"),
/// }
/// ```
pub fn handle_part(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let channel = parsed_message.get_param(0)?;
let part_message = parsed_message
.get_param(1)
.map_or(None, |part_message| Some(part_message.to_string()));
let nickname = &parsed_message
.source
.as_ref()
.ok_or(IncomingIrcEventError::MissingSource)?
.nickname;
Ok((
Self::Part(channel.to_string(), nickname.to_string(), part_message),
parsed_message,
))
}
/// Handles a `Privmsg` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if a source or target for the Privmsg is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from(":nick!ident@host PRIVMSG #channel :Hello, world!");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Privmsg(channel, nick, message) => {
/// println!("Channel: {channel}");
/// println!("Nick: {nick}");
/// println!("Message: {message}")
/// },
/// _ => eprintln!("error: not a `Privmsg` event!"),
/// }
/// ```
pub fn handle_privmsg(
parsed_message: Message,
) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(0)?;
let message = parsed_message.get_param(1)?;
let nickname = &parsed_message
.source
.as_ref()
.ok_or(IncomingIrcEventError::MissingSource)?
.nickname;
Ok((
Self::Privmsg(
target.to_string(),
nickname.to_string(),
message.to_string(),
),
parsed_message,
))
}
/// Handles a `Notice` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if a source or target for the Notice is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from(":nick!ident@host NOTICE #channel :Hello, world!");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Notice(channel, nick, message) => {
/// println!("Channel: {channel}");
/// println!("Nick: {nick}");
/// println!("Message: {message}")
/// },
/// _ => eprintln!("error: not a `Notice` event!"),
/// }
/// ```
pub fn handle_notice(
parsed_message: Message,
) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(0)?;
let nickname = &parsed_message
.source
.as_ref()
.ok_or(IncomingIrcEventError::MissingSource)?
.nickname;
let message = parsed_message.get_param(1)?;
Ok((
Self::Notice(
target.to_string(),
nickname.to_string(),
message.to_string(),
),
parsed_message,
))
}
/// Handles a `Kick` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if a source, channel, nickname or reason for the Kick is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from(":kicker!ident@host KICK #channel nickname :You have been kicked");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Kick(channel, nickname, kicker, reason) => {
/// println!("Channel: {channel}");
/// println!("Nickname: {nickname}");
/// println!("Kicker: {kicker}");
/// println!("Reason: {reason}");
/// },
/// _ => eprintln!("error: not a `Kick` event!"),
/// }
/// ```
pub fn handle_kick(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let channel = parsed_message.get_param(0)?;
let nickname = parsed_message.get_param(1)?;
let kicker = &parsed_message
.source
.as_ref()
.ok_or(IncomingIrcEventError::MissingSource)?
.nickname;
let reason = parsed_message.get_param(2)?;
Ok((
Self::Kick(
channel.to_string(),
nickname.to_string(),
kicker.to_string(),
reason.to_string(),
),
parsed_message,
))
}
/// Handles a `Mode` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if a target or mode change for the Mode is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from(":irc.server.com MODE #channel +o nickname");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Mode(target, mode_change, mode_args) => {
/// println!("Target: {target}");
/// println!("Mode Change: {mode_change}");
/// println!("Mode Arguments: {mode_args}");
/// },
/// _ => eprintln!("error: not a `Mode` event!"),
/// }
/// ```
pub fn handle_mode(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(0)?;
let mode_change = parsed_message.get_param(1)?;
let params = parsed_message.params();
let mode_args = params.get(2..).unwrap_or(&[]);
Ok((
Self::Mode(
target.to_string(),
mode_change.to_string(),
mode_args.join(" "),
),
parsed_message,
))
}
/// Handles a `Ping` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if a server for the PING is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from("PING :irc.server.com");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Ping(server) => {
/// println!("Server: {server}");
/// },
/// _ => eprintln!("error: not a `Ping` event!"),
/// }
/// ```
pub fn handle_ping(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let server = parsed_message.get_param(0)?;
Ok((Self::Ping(server.to_string()), parsed_message))
}
/// Handles a `Cap` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the subcommand or capabilities are missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from(":irc.server.com CAP * ACK :sasl multi-prefix");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Cap(subcommand, capabilities) => {
/// println!("Subcommand: {subcommand}");
/// println!("Capabilities: {}", capabilities.join(" "));
/// },
/// _ => eprintln!("error: not a `Cap` event!"),
/// }
/// ```
pub fn handle_cap(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let subcommand = parsed_message.get_param(1)?;
let capabilities = parsed_message.get_param(2)?;
let tokens: Vec<String> = capabilities
.split_whitespace()
.map(std::string::ToString::to_string)
.collect();
Ok((Self::Cap(subcommand.to_string(), tokens), parsed_message))
}
/// Handles a `Welcome` event,
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the source for the welcome message is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = String::from(":irc.server.com 001 nickname :Welcome to the IRC Server");
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Welcome(message) => {
/// println!("Welcome Message: {message}");
/// },
/// _ => eprintln!("error: not a `Welcome` event!"),
/// }
/// ```
pub fn handle_001(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let message = parsed_message.get_param(1)?;
let nickname = &parsed_message
.source
.as_ref()
.ok_or(IncomingIrcEventError::MissingSource)?
.nickname;
Ok((
Self::Welcome(format!("{nickname}: {message}")),
parsed_message,
))
}
/// Handles an `Isupport` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if a key for the parts is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 005 nickname CHANMODES=eIbq,k,flj,CFLMPQRSTcgimnprstuz CHANLIMIT=#:250 PREFIX=(ov)@+ MAXLIST=bqeI:100 MODES=4 NETWORK=Irc.Server STATUSMSG=@+ CASEMAPPING=rfc1459 NICKLEN=16 MAXNICKLEN=16 CHANNELLEN=50 TOPICLEN=390 :are supported by this server";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Isupport(capabilities) => {
/// for (key, value) in capabilities {
/// match value {
/// Some(val) => println!("{}: {}", key, val),
/// None => println!("{}", key),
/// }
/// }
/// },
/// _ => eprintln!("error: not an `Isupport` event!"),
/// }
/// ```
pub fn handle_005(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let isupports = parsed_message.params()[1..].to_vec();
let mut parsed_isupports = Vec::new();
for isupport in isupports {
let mut parts = isupport.splitn(2, '=');
let key = match parts.next() {
Some(key) => key.to_string(),
None => return Err(IncomingIrcEventError::MissingKey),
};
let value = parts.next().map(std::string::ToString::to_string);
parsed_isupports.push((key, value));
}
Ok((Self::Isupport(parsed_isupports), parsed_message))
}
/// Handles a `WhoisUser` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the target, username, host or realname is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 311 nickname target username host * :real name";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::WhoisUser(target, username, host, realname) => {
/// println!("Target: {target}");
/// println!("Username: {username}");
/// println!("Host: {host}");
/// println!("Real Name: {realname}");
/// },
/// _ => eprintln!("error: not a `WhoisUser` event!"),
/// }
/// ```
pub fn handle_311(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(1)?;
let username = parsed_message.get_param(2)?;
let host = parsed_message.get_param(3)?;
let realname = parsed_message.get_param(5)?;
Ok((
Self::WhoisUser(
target.to_string(),
username.to_string(),
host.to_string(),
realname.to_string(),
),
parsed_message,
))
}
/// Handles a `WhoisServer` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the target, server or server info is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 312 nick anothernick irc.server.com :Earth";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::WhoisServer(target, server, server_info) => {
/// println!("Target: {target}");
/// println!("Server: {server}");
/// println!("Server Info: {server_info}");
/// },
/// _ => eprintln!("error: not a `WhoisServer` event!"),
/// }
/// ```
pub fn handle_312(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(1)?;
let server = parsed_message.get_param(2)?;
let server_info = parsed_message.get_param(3)?;
Ok((
Self::WhoisServer(
target.to_string(),
server.to_string(),
server_info.to_string(),
),
parsed_message,
))
}
/// Handles a `WhoisIdle` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the `target` or `idle_time` is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 317 nick anothernick 1800";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::WhoisIdle(target, idle_time) => {
/// println!("Target: {target}");
/// println!("Idle Time: {idle_time} seconds");
/// },
/// _ => eprintln!("error: not a `317` event!"),
/// }
/// ```
pub fn handle_317(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(1)?;
let idle_time = parsed_message.get_param(2)?;
Ok((
Self::WhoisIdle(target.to_string(), idle_time.to_string()),
parsed_message,
))
}
/// Handles an `EndOfWhois` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the target is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 318 nick anothernick :End of /WHOIS list.";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::EndOfWhois(target) => {
/// println!("Target: {target}");
/// },
/// _ => eprintln!("error: not a `EndOfWhois` event!"),
/// }
/// ```
pub fn handle_318(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(1)?;
Ok((Self::EndOfWhois(target.to_string()), parsed_message))
}
/// Handles a `WhoisChannels` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the target or channels are missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 319 mynick anothernick :#channel";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::WhoisChannels(target, channels) => {
/// println!("Target: {target}");
/// println!("Channels: {}", channels.join(", "));
/// },
/// _ => eprintln!("error: not a `WhoisChannels` event!"),
/// }
/// ```
pub fn handle_319(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(1)?;
let channels = parsed_message
.get_param(2)?
.split(' ')
.map(std::string::ToString::to_string)
.collect();
Ok((
Self::WhoisChannels(target.to_string(), channels),
parsed_message,
))
}
/// Handles a `WhoisLoggedInAs` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the target or nickname are missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 330 mynick anothernick anothernick :is logged in as";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::WhoisLoggedInAs(target, nickname) => {
/// println!("Target: {target}");
/// println!("Logged in as: {nickname}");
/// },
/// _ => eprintln!("error: not a `WhoisLoggedInAs` event!"),
/// }
/// ```
pub fn handle_330(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(1)?;
let nickname = parsed_message.get_param(2)?;
Ok((
Self::WhoisLoggedInAs(target.to_string(), nickname.to_string()),
parsed_message,
))
}
/// Handles a `Topic` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the channel, topic, or source are missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 332 nick #channel :Welcome to the channel!";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Topic(channel, nickname, topic) => {
/// println!("Channel: {channel}");
/// println!("Set by: {nickname}");
/// println!("Topic: {topic}");
/// },
/// _ => eprintln!("error: not a `Topic` event!"),
/// }
/// ```
pub fn handle_332(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let channel = parsed_message.get_param(1)?;
let topic = parsed_message.get_param(2)?;
let nickname = &parsed_message
.source
.as_ref()
.ok_or(IncomingIrcEventError::MissingSource)?
.nickname;
Ok((
Self::Topic(channel.to_string(), nickname.to_string(), topic.to_string()),
parsed_message,
))
}
/// Handles a `Names` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the channel or list of `names` is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 353 nick @ #chanel :nick @anothernick";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::Names(channel, names) => {
/// println!("Channel: {channel}");
/// println!("Names: {}", names.join(", "));
/// },
/// _ => eprintln!("error: not a `Names` event!"),
/// }
/// ```
pub fn handle_353(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let channel = parsed_message.get_param(2)?;
let names = parsed_message.get_param(3)?;
let names: Vec<String> = names
.split(' ')
.map(std::string::ToString::to_string)
.collect();
Ok((Self::Names(channel.to_string(), names), parsed_message))
}
/// Handles an `EndOfNames` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the channel is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 366 nick #channel :End of /NAMES list.";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::EndOfNames(channel) => {
/// println!("Channel: {channel}");
/// },
/// _ => eprintln!("error: not a `EndOfNames` event!"),
/// }
/// ```
pub fn handle_366(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let channel = parsed_message.get_param(1)?;
Ok((Self::EndOfNames(channel.to_string()), parsed_message))
}
/// Handles a `WhoisSecure` event.
///
/// # Arguments
///
/// * `parsed_message` - `Message` struct containing the parsed version of the server response.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return `IncomingIrcEventError` if the target or the message is missing.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let response = ":irc.server.com 671 nick anothernick :is using a secure connection";
/// let (event, parsed_response) = IncomingIrcEvent::new(&response).unwrap();
/// println!("Parsed response: {:?}", parsed_response);
/// match event {
/// IncomingIrcEvent::WhoisSecure(target, message) => {
/// println!("Target: {target}");
/// println!("Secure Connection: {message}");
/// },
/// _ => eprintln!("error: not a `WhoisSecure` event!"),
/// }
/// ```
pub fn handle_671(parsed_message: Message) -> Result<(Self, Message), IncomingIrcEventError> {
let target = parsed_message.get_param(1)?;
let message = parsed_message.get_param(2)?;
Ok((
Self::WhoisSecure(target.to_string(), message.to_string()),
parsed_message,
))
}
/// Parses the input string and handles the corresponding IRC event.
///
/// # Arguments
///
/// * `input` - A string containing the raw IRC message.
///
/// # Returns
///
/// * `<Result(Self, Message), IncomingIrcEventError>` - A result containing either a tuple of
/// an instantiated Enum `IncomingIrcEvent` with the handled event and an instantiated `Message`
/// struct containing the parsed version of the server response, or on failure an error of type
/// `IncomingIrcEventError` with more details.
///
/// # Errors
///
/// Will return an `IncomingIrcEventError` if the parsing or handling of the IRC event encounters an error.
///
/// # Examples
///
/// ```rust
/// use cowirc::event_handler::IncomingIrcEvent;
///
/// let input = ":nick!user@host JOIN #channel";
/// let (event, parsed_message) = IncomingIrcEvent::new(&input).unwrap();
/// println!("Parsed message: {:?}", parsed_message);
/// match event {
/// // Handle desired IRC events
/// _ => eprintln!("error: unrecognized event!"),
/// }
/// ```
pub fn new(input: &str) -> Result<(Self, Message), IncomingIrcEventError> {
let parsed_message =
Message::from(input.to_string()).map_err(IncomingIrcEventError::ParserError)?;
match parsed_message.command.to_uppercase().as_str() {
"JOIN" => Self::handle_join(parsed_message),
"PART" => Self::handle_part(parsed_message),
"PRIVMSG" => Self::handle_privmsg(parsed_message),
"NOTICE" => Self::handle_notice(parsed_message),
"KICK" => Self::handle_kick(parsed_message),
"MODE" => Self::handle_mode(parsed_message),
"PING" => Self::handle_ping(parsed_message),
"CAP" => Self::handle_cap(parsed_message),
"001" => Self::handle_001(parsed_message),
"005" => Self::handle_005(parsed_message),
"311" => Self::handle_311(parsed_message),
"312" => Self::handle_312(parsed_message),
"317" => Self::handle_317(parsed_message),
"318" => Self::handle_318(parsed_message),
"319" => Self::handle_319(parsed_message),
"330" => Self::handle_330(parsed_message),
"332" => Self::handle_332(parsed_message),
"353" => Self::handle_353(parsed_message),
"366" => Self::handle_366(parsed_message),
"671" => Self::handle_671(parsed_message),
_ => Ok((Self::Unknown(input.to_string()), parsed_message)),
}
}
}