Skip to content

Instantly share code, notes, and snippets.

@antonva
Created May 23, 2014 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonva/fdfb672df8e6951d3be2 to your computer and use it in GitHub Desktop.
Save antonva/fdfb672df8e6951d3be2 to your computer and use it in GitHub Desktop.
#![crate_id = "rust-irc-lib #0.1"]
#![license = "BSD"]
#![crate_type = "rlib"]
/* Dependencies */
extern crate sync;
extern crate regex;
//use std::os;
//use std::path::BytesContainer;
//use std::vec::Vec;
//use std::io::net::tcp::TcpStream;
use regex::Regex;
/* Event object, should only be used internally.
* TCP strings should be parsed into an Event object.
*
* Members:
* -prefix
* -command
* -argument
* The only required member variable is the command.
*
* RFC 2812 2.3 Messages specifies:
* The presence of a prefix is indicated with a single leading ASCII
* colon character (':', 0x3b), which MUST be the first character of the
* message itself. There MUST be NO gap (whitespace) between the colon
* and the prefix. The prefix is used by servers to indicate the true
* origin of the message. If the prefix is missing from the message, it
* is assumed to have originated from the connection from which it was
* received from. Clients SHOULD NOT use a prefix when sending a
* message; if they use one, the only valid prefix is the registered
* nickname associated with the client.
*
* The command MUST either be a valid IRC command or a three (3)
* digit number represented in ASCII text.
*
* IRC messages are always lines of characters terminated with a CR-LF
* (Carriage Return - Line Feed) pair, and these messages SHALL NOT
* exceed 512 characters in length, counting all characters including
* the trailing CR-LF. Thus, there are 510 characters maximum allowed
* for the command and its parameters. There is no provision for
* continuation of message lines. See section 6 for more details about
* current implementations.
*/
struct Event {
prefix : ~str,
command : ~str,
argument : ~str,
}
fn event_parse(string: ~str) -> Event
{
//let re = match Regex::new(r"^:?(.* |)([A-Z]+|[0-9]{3}) ?:?(.*);?$")
let re = match Regex::new(r"^(?::(.*) )?([A-Z]+|[0-9]{3}) ?:?(.*);?$")
{
Ok(re) => re,
Err(err) => fail!("{}", err),
};
let cap = re.captures(string).unwrap();
let event = Event {
prefix : cap.at(1).to_owned(),
command : cap.at(2).to_owned(),
argument : cap.at(3).to_owned(),
};
return event;
}
fn event_to_string(event: Event) -> ~str
{
let mut string = "".to_owned();
return string;
}
#[cfg(test)]
mod tests
{
use super::event_parse;
/* Event Parser Tests*/
#[test]
fn parse_string_into_event_obj_full()
{
/* :<prefix> <command> :<argument> */
let s = ":syrk!kalt@millennium.stealth.net QUIT :Gone to have lunch".to_owned();
let res = event_parse(s);
assert_eq!(res.prefix, "syrk!kalt@millennium.stealth.net".to_owned())
assert_eq!(res.command, "QUIT".to_owned())
assert_eq!(res.argument, "Gone to have lunch".to_owned())
}
#[test]
fn parse_string_into_event_obj_without_prefix()
{
/* <command> :<argument> */
let s = "QUIT :Gone to have lunch".to_owned();
let res = event_parse(s);
assert_eq!(res.prefix, "".to_owned())
assert_eq!(res.command, "QUIT".to_owned())
assert_eq!(res.argument, "Gone to have lunch".to_owned())
}
#[test]
fn parse_string_into_event_obj_cmd_as_string()
{
/* <command> (as string) */
let s = "QUIT".to_owned();
let res = event_parse(s);
assert_eq!(res.prefix, "".to_owned())
assert_eq!(res.command, "QUIT".to_owned())
assert_eq!(res.argument, "".to_owned())
}
#[test]
fn parse_string_into_event_obj_cmd_as_number()
{
/* <command> (as numeric) */
let s = "123".to_owned();
let res = event_parse(s);
assert_eq!(res.prefix, "".to_owned())
assert_eq!(res.command, "123".to_owned())
assert_eq!(res.argument, "".to_owned())
}
#[test]
fn create_response_string()
{
let event = Event
{
prefix : "",
command : "NICK",
argument : "CrashOverride"
};
let s = event_to_string(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment