aboutsummaryrefslogtreecommitdiff
path: root/src/event.rs
blob: aef34f9d328bd61224bdb6540be239e4e6a4de11 (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
use ident::Ident;

#[deriving(Clone)]
pub struct Event {
    pub prefix: String,
    pub command: String,
    pub content: Vec<String>
}

pub trait ParseResult {
    fn parse(event: Event) -> Option<Self>;
}

pub const PING: &'static str = "PING";

pub const PRIVMSG: &'static str = "PRIVMSG";

fn join(v: Vec<String>, from: uint) -> String {
    let mut msg = if v[from].chars().next().unwrap() == ':' {
        v[from][][1..].into_string()
    } else { v[from].clone() };
    for m in v.iter().skip(from + 1) {
        msg.push_str(" ");
        msg.push_str(m.trim_right());
    }
    msg
}

pub struct PrivMsg {
    pub from: Ident,
    pub to: String,
    pub content: String
}

impl ParseResult for PrivMsg {
    fn parse(event: Event) -> Option<PrivMsg> {
        let from = Ident::parse(event.prefix[]);
        let to = event.content[0].clone();
        match from {
            Some(from) => Some(PrivMsg {
                from: from,
                to: to,
                content: join(event.content, 1)
            }),
            None => None
        }
    }
}