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
|
use tokio::sync::mpsc;
use crate::client::event::Event;
use crate::client::handler::handle;
use crate::client::state::ClientState;
use crate::connection::{self, Sender};
use crate::proto::message::{Command, IrcMessage};
pub mod event;
pub mod handler;
pub mod state;
/// Configuration for the IRC client.
pub struct Config {
/// Server address, e.g. "irc.libera.chat:6667"
pub server: String,
/// Desired nick
pub nick: String,
/// IRC username (shown in /whois)
pub user: String,
/// Real name (shown in /whois)
pub realname: String,
/// Optional server password
pub password: Option<String>,
}
/// The main IRC client.
///
/// Call `Client::connect` to establish a connection, then drive the event
/// loop with `client.next_event().await` in your application loop.
pub struct Client {
state: ClientState,
sender: Sender,
inbox: mpsc::UnboundedReceiver<IrcMessage>,
config: Config,
}
impl Client {
/// Connect to the server and begin the registration handshake.
pub async fn connect(config: Config) -> Result<Self, std::io::Error> {
let (sender, inbox) = connection::connect(&config.server).await?;
let state = ClientState::new(&config.nick);
let client = Self {
state,
sender,
inbox,
config,
};
client.register();
Ok(client)
}
/// Offer a clone of the sender
pub fn sender(&self) -> Sender {
self.sender.clone()
}
/// Read-only view of current client state.
pub fn state(&self) -> &ClientState {
&self.state
}
/// Wait for the next event from the server.
/// Returns `None` if the connection has closed.
pub async fn next_event(&mut self) -> Option<Event> {
loop {
let msg = self.inbox.recv().await?;
let events = handle(msg, &mut self.state, &self.sender);
// Return the first event; re-queue the rest
// (simple approach: process one at a time via recursive buffering)
if let Some(first) = events.into_iter().next() {
return Some(first);
}
// If no events were produced (e.g. a PING), loop and wait for next message
}
}
/// Send the registration sequence to the server.
fn register(&self) {
// Optional server password
if let Some(pass) = &self.config.password {
self.sender
.send(IrcMessage::new(Command::Pass, vec![pass.clone()]));
}
// Begin CAP negotiation first — lets us request IRCv3 caps
// before NICK/USER so the server doesn't rush past registration
self.sender.send(IrcMessage::new(
Command::Cap,
vec!["LS".into(), "302".into()],
));
self.sender.send(IrcMessage::new(
Command::Nick,
vec![self.config.nick.clone()],
));
self.sender.send(IrcMessage::new(
Command::User,
vec![
self.config.user.clone(),
"0".into(),
"*".into(),
self.config.realname.clone(),
],
));
}
}
|