summaryrefslogtreecommitdiffstats
path: root/examples/client.rs
blob: eb3d31a9841c134a1ad94ac26686e7dceef5d9b1 (plain) (blame)
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
extern crate irc;

use irc::constants::{Ping, Pong};
use irc::Client;

use std::io;

struct ExampleClient {
    conn: io::BufferedStream<io::TcpStream>,
    socket_name: Option<String>,
}

impl irc::Client for ExampleClient {
    fn new (conn: io::BufferedStream<io::TcpStream>, socket_name: Option<String>) -> ExampleClient {
        ExampleClient { conn: conn, socket_name: socket_name }
    }
    fn conn (&mut self) -> &mut io::BufferedStream<io::TcpStream> {
        &mut self.conn
    }
    fn socket_name (&self) -> Option<&str> {
        match self.socket_name {
            Some(ref name) => Some(name.as_slice()),
            None => None,
        }
    }
}

fn main () {
    let builder = irc::ClientBuilder::new("doytest", "chat.freenode.net");
    let client: ExampleClient = builder.connect();
    client.run_loop_with(|client, m| {
        println!("{}", m);
        match *m.message_type() {
            Ping => {
                client.write(irc::Message::new(None, Pong, m.params().clone()));
            },
            _ => {},
        }
    });
}