pub struct Writer { /* private fields */ }Expand description
Used to send messages to the IRC server.
This object is thread safe. You can clone it and send the clones to other
threads. You can write from multiple threads without any issue. Internally,
it uses Arc and Mutex.
Implementations§
Source§impl Writer
impl Writer
Sourcepub fn disconnect(&self) -> Result<(), Error>
pub fn disconnect(&self) -> Result<(), Error>
Drop the connection and trigger the reconnection process.
There might be a reconnection attempt, based on your settings.
This should be used if you want the connection to be re-created.
This is not the preferred way of shutting down the connection
for good. Use close for this.
Sourcepub fn close(&self) -> Result<(), Error>
pub fn close(&self) -> Result<(), Error>
Close the connection and stop listening for messages.
There will not be any reconnection attempt. An error will be returned if the connection is already closed.
Sourcepub fn raw<S: AsRef<str>>(&self, data: S) -> Result<(), Error>
pub fn raw<S: AsRef<str>>(&self, data: S) -> Result<(), Error>
Send a raw string to the IRC server.
A new line will be not be added, so make sure that you include it. An error will be returned if the client is disconnected.
Examples found in repository?
11fn main() {
12 let args: Vec<String> = env::args().collect();
13 let channel = args.get(1).expect("Channel must be given as an argument.");
14
15 // Connect to freenode and use no not reconnect.
16 let (writer, reader) = connect("irc.freenode.net:6667",
17 ReconnectionSettings::DoNotReconnect,
18 UTF_8).unwrap();
19 writer.raw(format!("USER {} 8 * :{}\n", "peekaboo", "peekaboo"));
20 writer.raw(format!("NICK {}\n", "peekaboo"));
21
22 // Receive events.
23 for event in reader.iter() {
24 println!("{:?}", event);
25 match event {
26 Event::Message(msg) => {
27 if msg.code == Code::RplWelcome {
28 // join channel, no password
29 writer.raw(format!("JOIN {}\n", channel));
30 }
31 // JOIN is sent when you join a channel.
32 if msg.code == Code::Join {
33 // If there is a prefix...
34 if let Some(prefix) = msg.prefix {
35 match prefix {
36 // And the prefix is a user...
37 Prefix::User(user) => {
38 // And that user's nick is peekaboo, we've joined the channel!
39 if user.nickname == "peekaboo" {
40 writer.raw(format!("PRIVMSG {} :{}\n", channel, "peekaboo"));
41 // Note that if the reconnection settings said to reconnect,
42 // it would. Close would "really" stop it.
43 writer.raw(format!("QUIT :{}\n", "peekaboo"));
44 // writer.close();
45 }
46 }
47 _ => {}
48 }
49 }
50 }
51 }
52 _ => {}
53 }
54 }
55}