ellidri-tokens
A minimal library that provides helpers to:
- correctly and efficiently parse IRC messages and modes
- correctly and efficiently build IRC messages
To the documentation!
Usage
[dependencies]
ellidri-tokens = "0.1.0"
Let's start with a simple problem...
use ellidri_tokens as irc;
let msg = irc::Message::parse("TOPIC #ircdocs\r\n").unwrap();
assert_eq!(msg.num_params, 1);
assert_eq!(msg.params[0], "#ircdocs");
match msg.command {
Ok(Command::Topic) => {
let mut buffer = irc::Buffer::new();
buffer.message("my.server.com", irc::rpl::TOPIC)
.param("their_nick")
.param("#ircdocs")
.trailing_param("Praise the IRC!");
let string = buffer.build();
println!("{}", string);
}
_ => unreachable!(),
}
And what if I want to use modern IRCv3?
use ellidri_tokens as irc;
let msg = irc::Message::parse("@label=mylabel JOIN #ircdocs\r\n").unwrap();
let mut tags = msg.tags(); assert_eq!(tags.next(), Some(Tag { key: "label", value: Some("mylabel") }));
assert_eq!(tags.next(), None);
match msg.command {
Ok(Command::Join) => {
let mut reply_buffer = irc::ReplyBuffer::new(
"my.server.com", "their_nick", msg.tags().find(|tag| tag.key == "label").and_then(|tag| tag.value),
);
reply_buffer.start_lr_batch(); reply_buffer.message("their_nick!~user@host", Command::Join)
.param("#ircdocs");
reply_buffer.reply(irc::rpl::TOPIC)
.param("#ircdocs")
.trailing_param("Praise the IRC!");
reply_buffer.reply(irc::rpl::NAMREPLY)
.param("=")
.param("#ircdocs")
.trailing_param("@dan- their_nick");
reply.end_lr();
let string = reply_buffer.build();
println!("{}", string);
}
_ => unreachable!(),
}
Parse mode strings like a boss!
use ellidri_tokens::mode;
let mut modes = mode::user_query("+i-oi");
assert_eq!(modes.next().unwrap(), Ok(mode::UserChange::Invisible(true)));
assert_eq!(modes.next().unwrap(), Ok(mode::UserChange::Deoperator));
assert_eq!(modes.next().unwrap(), Ok(mode::UserChange::Invisible(false)));
assert!(modes.next().is_none());
let mut modes = mode::channel_query("+ol-m", &["chief", "42"]);
assert_eq!(modes.next().unwrap(), Ok(mode::ChannelChange::Operator(true, "chief")));
assert_eq!(modes.next().unwrap(), Ok(mode::ChannelChange::UserLimit(Some("42")));
assert_eq!(modes.next().unwrap(), Ok(mode::ChannelChange::Moderated(false));
assert!(modes.next().is_none());
License
This library is under the ISC license.