aboutsummaryrefslogtreecommitdiffstats
path: root/lib.rs
blob: a6dfb7b5a1a3b106b27f93b2d2553e4975cea6cc (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//#![crate_id = "irc#0.1"]
//#![crate_type = "lib"]

use std::io::net::tcp::TcpStream;
use std::io::IoResult;
use std::io::BufferedReader;
use std::fmt;
use std::from_str::FromStr;

pub struct IrcConnection {
	callbacks: IrcCallbacks,
	stream: TcpStream
}

#[deriving(Clone)]
pub enum Command {
	Nick,
	User,
	Quit,
	Join,
	Part,
	PrivMsg,
	Notice,
	Motd,
	Ping,
	Pong,
	Error,
	Away,
	Numeric(u16),
	UnknownStr(String)
}

impl fmt::Show for Command {
	fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
		write!(formatter, "{}",
			match *self {
				Nick => "NICK".into_maybe_owned(),
				User => "USER".into_maybe_owned(),
				Quit => "QUIT".into_maybe_owned(),
				Join => "JOIN".into_maybe_owned(),
				Part => "PART".into_maybe_owned(),
				PrivMsg => "PRIVMSG".into_maybe_owned(),
				Notice => "NOTICE".into_maybe_owned(),
				Motd => "MOTD".into_maybe_owned(),
				Ping => "PING".into_maybe_owned(),
				Pong => "PONG".into_maybe_owned(),
				Error => "ERROR".into_maybe_owned(),
				Away => "AWAY".into_maybe_owned(),
				Numeric(i) => i.to_str().into_maybe_owned(),
				UnknownStr(ref s) => s.as_slice().into_maybe_owned(),
			}
		)
	}
}

impl FromStr for Command {
	fn from_str(s: &str) -> Option<Command> {
		Some(match s {
			"NICK" => Nick,
			"USER" => User,
			"QUIT" => Quit,
			"JOIN" => Join,
			"PART" => Part,
			"PRIVMSG" => PrivMsg,
			"NOTICE" => Notice,
			"MOTD" => Motd,
			"PING" => Ping,
			"PONG" => Pong,
			"ERROR" => Error,
			"AWAY" => Away,
			other => match from_str::<u16>(other) {
				Some(i) => Numeric(i),
				None => UnknownStr(other.to_string())
			}
		})
	}
}

#[deriving(Clone)]
pub struct Message {
	pub prefix: Option<String>,
	pub command: Command,
	pub arguments: Vec<String>,
}

impl<'a> fmt::Show for Message {
	fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
		if self.prefix.is_some() {
			try!(write!(formatter, ":{} ", self.prefix.get_ref()));
		}
		try!(write!(formatter, "{}", self.command));
		for argument in self.arguments.iter() {
			try!(write!(formatter, " {}", *argument));
		}
		Ok(())
	}
}

impl FromStr for Message {
	fn from_str(s: &str) -> Option<Message> {
		//TODO: Parse string
		unimplemented!();
	}
}



impl IrcConnection {
	pub fn connect(
		callbacks: IrcCallbacks, host: &str, port: u16,
		nick: &str,  username: &str, real_name: &str) -> IoResult<IrcConnection> {
		let mut stream = try!(TcpStream::connect(host, port));

		Ok(IrcConnection {
			callbacks: callbacks,
			stream: stream
		})
	}

	pub fn start_loop(&mut self) {
		let stream = self.stream.clone();
//		spawn(proc() {
			let mut reader = BufferedReader::new(stream);
			loop {
				match reader.read_line() {
					Ok(line) => {
						let mut words: Vec<&str> = line.as_slice().words().collect();

						println!("{}", words);
						if words.is_empty() { continue; }
						if (*words.get(0)).starts_with(":") {
							words.shift();
						}
						match *words.get(0) {
							"PING" => println!("PING"),
							other => println!("other: {}", other),
						}
					}
					Err(e) => {
						println!("An Error occured: {}", e);
						break;
					}
				}
				break;
			}
//		});
	}
}

pub struct IrcCallbacks {
	pub on_connect: fn(&mut IrcConnection)->(),
	pub on_numeric: fn(&mut IrcConnection, uint, &str, &[&str])->(),
	//TODO: Add the rest
}