aboutsummaryrefslogtreecommitdiff
path: root/examples/01.rs
blob: 33c2fcfb849cd9e45b12de59b9316cac3fb4f0b8 (plain)
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
extern crate irsc;
extern crate env_logger;
extern crate openssl;

use irsc::color::bold;
use irsc::*;
use irsc::Command::*;
use irsc::Reply::*;

use openssl::ssl::{ Ssl, SslContext, SslMethod };

static NAME: &'static str = "rusticbot";
static DESC: &'static str = "A bot, written in Rust.";

fn callback(server: &mut Client, msg: &Message, event: Option<Event>) {
    match event {
        Some(Event::Command(PRIVMSG(to, content))) => {
            let from = msg.ident().unwrap();
            let response = match msg.msg_type {
                MsgType::Irc => format!("{} wrote: {}", from.nickname, bold(&content)),
                MsgType::Ctcp => format!("{} emoted: {}", from.nickname,
                                         bold(&content["ACTION ".len()..]))
            };

            // only send to channels, to prevent recursion when we are pm'ed
            // technically, there are other prefixes than '#', but ignoring them is fine
            if to.starts_with("#") {
                server.msg(&to, &response);
            }
        },
        Some(Event::Reply(RPL_WELCOME(_))) => {
            server.join("#meep!", None);
        },
        _ => ()
    }
}

fn main() {
    env_logger::init().unwrap();
    let mut s = OwnedClient::new();
    let ssl = Ssl::new(&SslContext::new(SslMethod::Tlsv1).unwrap()).unwrap();
    s.connect_ssl("irc.mozilla.org", 6697, ssl);
    s.register(NAME, NAME, DESC, None);

    // Dedicate this thread to listening and event processing
    s.listen_with_callback(callback);
}