aboutsummaryrefslogtreecommitdiff
path: root/examples/01.rs
blob: 5021aac76e11745f997e66cec9500e39516e435f (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
48
49
#![feature(plugin)]
#![plugin(regex_macros)]

extern crate irsc;
extern crate env_logger;

use std::borrow::ToOwned;
use std::borrow::Cow::*;

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

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

fn callback(server: &mut Client, msg: &Message) {
    match Command::from_message(msg) {
        Some(PRIVMSG(to, content)) => {
            let from = msg.prefix().and_then(Ident::parse).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()..]))
            };
            server.send(PRIVMSG(to, Owned(response))).unwrap();
        },
        _ => ()
    }

    match Reply::from_message(msg) {
        Some(RPL_WELCOME(_)) => {
            server.send(JOIN(vec![Borrowed("#botzoo")], vec![])).unwrap();
        },
        _ => ()
    }
}

fn main() {
    env_logger::init().unwrap();
    let mut s = Client::new();
    s.connect("irc.mozilla.org".to_owned(), 6667).unwrap();
    s.send(NICK(Borrowed(NAME))).unwrap();
    s.send(USER(Borrowed(NAME), Borrowed("*"), Borrowed("*"), Borrowed(DESC))).unwrap();

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