2018-04-22 01:20:58 +02:00
|
|
|
# the irc crate [![Build Status][ci-badge]][ci] [![Crates.io][cr-badge]][cr] ![Downloads][dl-badge] [![Docs][doc-badge]][doc]
|
2017-12-13 20:31:28 +01:00
|
|
|
|
2018-01-09 05:42:03 +01:00
|
|
|
[ci-badge]: https://travis-ci.org/aatxe/irc.svg?branch=stable
|
2017-12-13 20:31:28 +01:00
|
|
|
[ci]: https://travis-ci.org/aatxe/irc
|
|
|
|
[cr-badge]: https://img.shields.io/crates/v/irc.svg
|
|
|
|
[cr]: https://crates.io/crates/irc
|
2018-03-07 19:43:56 +01:00
|
|
|
[dl-badge]: https://img.shields.io/crates/d/irc.svg
|
2017-12-13 20:31:28 +01:00
|
|
|
[doc-badge]: https://docs.rs/irc/badge.svg
|
|
|
|
[doc]: https://docs.rs/irc
|
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
[rfc2812]: http://tools.ietf.org/html/rfc2812
|
2018-08-09 02:13:50 +02:00
|
|
|
[ircv3.1]: http://ircv3.net/irc/3.1.html
|
2018-01-30 03:10:05 +01:00
|
|
|
[ircv3.2]: http://ircv3.net/irc/3.2.html
|
|
|
|
|
|
|
|
"the irc crate" is a thread-safe and async-friendly IRC client library written in Rust. It's
|
|
|
|
compliant with [RFC 2812][rfc2812], [IRCv3.1][ircv3.1], [IRCv3.2][ircv3.2], and includes some
|
|
|
|
additional, common features from popular IRCds. You can find up-to-date, ready-to-use documentation
|
|
|
|
online [on docs.rs][doc].
|
2014-12-02 18:26:50 +01:00
|
|
|
|
2018-03-31 16:03:19 +02:00
|
|
|
## Built with the irc crate
|
2018-01-30 03:41:36 +01:00
|
|
|
|
|
|
|
the irc crate is being used to build new IRC software in Rust. Here are some of our favorite
|
|
|
|
projects:
|
|
|
|
|
|
|
|
- [alectro][alectro] — a terminal IRC client
|
|
|
|
- [spilo][spilo] — a minimalistic IRC bouncer
|
|
|
|
- [irc-bot.rs][ircbot] — a library for writing IRC bots
|
2018-03-26 04:01:56 +02:00
|
|
|
- [playbot_ng][playbot_ng] — a Rust-evaluating IRC bot in Rust
|
2018-01-30 03:41:36 +01:00
|
|
|
- [bunnybutt-rs][bunnybutt] — an IRC bot for the [Feed The Beast Wiki][ftb-wiki]
|
|
|
|
|
|
|
|
[alectro]: https://github.com/aatxe/alectro
|
|
|
|
[spilo]: https://github.com/aatxe/spilo
|
|
|
|
[ircbot]: https://github.com/8573/irc-bot.rs
|
|
|
|
[bunnybutt]: https://github.com/FTB-Gamepedia/bunnybutt-rs
|
2018-03-26 04:01:56 +02:00
|
|
|
[playbot_ng]: https://github.com/panicbit/playbot_ng
|
2018-01-30 03:41:36 +01:00
|
|
|
[ftb-wiki]: https://ftb.gamepedia.com/FTB_Wiki
|
|
|
|
|
|
|
|
Making your own project? [Submit a pull request](https://github.com/aatxe/irc/pulls) to add it!
|
|
|
|
|
2018-03-31 16:03:19 +02:00
|
|
|
## Getting Started
|
2014-12-02 18:26:50 +01:00
|
|
|
|
2018-08-09 02:13:50 +02:00
|
|
|
To start using the irc crate with cargo, you can add `irc = "0.13"` to your dependencies in
|
2018-03-31 16:03:19 +02:00
|
|
|
your Cargo.toml file. The high-level API can be found in [`irc::client::prelude`][irc-prelude].
|
|
|
|
You'll find a number of examples to help you get started in `examples/`, throughout the
|
|
|
|
documentation, and below.
|
2018-01-30 03:10:05 +01:00
|
|
|
|
2018-03-31 16:03:19 +02:00
|
|
|
[irc-prelude]: https://docs.rs/irc/*/irc/client/prelude/index.html
|
2018-01-30 03:10:05 +01:00
|
|
|
|
2018-03-31 16:03:19 +02:00
|
|
|
## A Tale of Two APIs
|
|
|
|
|
|
|
|
### Reactors (The "New" API)
|
2015-01-13 08:57:40 +01:00
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
The release of v0.13 brought with it a new API called `IrcReactor` that enables easier multiserver
|
|
|
|
support and more graceful error handling. The general model is that you use the reactor to create
|
|
|
|
new `IrcClients`, register message handler functions, and finally block the thread to run the
|
|
|
|
clients with their respective handlers. Here's an example:
|
2014-12-02 18:26:50 +01:00
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
```rust,no_run
|
2014-12-05 16:27:58 +01:00
|
|
|
extern crate irc;
|
|
|
|
|
2015-02-23 03:22:33 +01:00
|
|
|
use irc::client::prelude::*;
|
2014-12-05 16:27:58 +01:00
|
|
|
|
2014-12-02 18:26:50 +01:00
|
|
|
fn main() {
|
2018-01-30 03:10:05 +01:00
|
|
|
// We can also load the Config at runtime via Config::load("path/to/config.toml")
|
|
|
|
let config = Config {
|
|
|
|
nickname: Some("the-irc-crate".to_owned()),
|
|
|
|
server: Some("irc.pdgn.co".to_owned()),
|
|
|
|
channels: Some(vec!["#test".to_owned()]),
|
|
|
|
..Config::default()
|
2016-06-30 20:09:25 +02:00
|
|
|
};
|
2018-01-30 03:10:05 +01:00
|
|
|
|
|
|
|
let mut reactor = IrcReactor::new().unwrap();
|
|
|
|
let client = reactor.prepare_client_and_connect(&config).unwrap();
|
2018-01-28 04:41:04 +01:00
|
|
|
client.identify().unwrap();
|
2018-08-09 02:13:50 +02:00
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
reactor.register_client_with_handler(client, |client, message| {
|
|
|
|
print!("{}", message);
|
|
|
|
// And here we can do whatever we want with the messages.
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
reactor.run().unwrap();
|
2014-12-02 18:26:50 +01:00
|
|
|
}
|
|
|
|
```
|
2014-11-05 08:11:33 +01:00
|
|
|
|
2016-01-18 07:17:32 +01:00
|
|
|
|
2018-03-31 16:03:19 +02:00
|
|
|
### Direct Style (The "Old" API)
|
2016-06-30 20:09:25 +02:00
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
The old API for connecting to an IRC server is still supported through the `IrcClient` type. It's
|
|
|
|
simpler for the most basic use case, but will panic upon encountering any sort of connection issues.
|
|
|
|
In general, it's recommended that users switch to the new API if possible. Nevertheless, here is an
|
|
|
|
example:
|
|
|
|
|
|
|
|
```rust,no_run
|
2016-06-30 20:09:25 +02:00
|
|
|
extern crate irc;
|
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
use std::default::Default;
|
2016-06-30 20:09:25 +02:00
|
|
|
use irc::client::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
2018-01-30 03:10:05 +01:00
|
|
|
// We can also load the Config at runtime via Config::load("path/to/config.toml")
|
|
|
|
let cfg = Config {
|
|
|
|
nickname: Some(format!("the-irc-crate")),
|
|
|
|
server: Some(format!("irc.example.com")),
|
|
|
|
channels: Some(vec![format!("#test")]),
|
|
|
|
.. Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let client = IrcClient::from_config(cfg).unwrap();
|
2018-01-28 04:41:04 +01:00
|
|
|
client.identify().unwrap();
|
2018-01-30 03:10:05 +01:00
|
|
|
|
2018-01-28 04:41:04 +01:00
|
|
|
client.for_each_incoming(|message| {
|
2018-01-30 03:10:05 +01:00
|
|
|
print!("{}", message);
|
|
|
|
// And here we can do whatever we want with the messages.
|
2017-06-25 11:00:44 +02:00
|
|
|
}).unwrap()
|
2016-06-30 20:09:25 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-03-31 16:03:19 +02:00
|
|
|
## Configuring IRC Clients
|
2016-01-18 07:17:32 +01:00
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
As seen above, there are two techniques for configuring the irc crate: runtime loading and
|
|
|
|
programmatic configuration. Runtime loading is done via the function `Config::load`, and is likely
|
|
|
|
sufficient for most IRC bots. Programmatic configuration is convenient for writing tests, but can
|
|
|
|
also be useful when defining your own custom configuration format that can be converted to `Config`.
|
|
|
|
The primary configuration format is TOML, but if you are so inclined, you can use JSON and/or YAML
|
|
|
|
via the optional `json` and `yaml` features respectively. At the minimum, a configuration requires
|
|
|
|
`nickname` and `server` to be defined, and all other fields are optional. You can find detailed
|
2018-03-31 16:03:19 +02:00
|
|
|
explanations of the various fields on [docs.rs][config-fields].
|
|
|
|
|
|
|
|
[config-fields]: https://docs.rs/irc/*/irc/client/data/config/struct.Config.html#fields
|
2017-08-05 02:31:45 +02:00
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
Alternatively, you can look at the example below of a TOML configuration with all the fields:
|
2017-08-05 02:31:45 +02:00
|
|
|
|
|
|
|
```toml
|
|
|
|
owners = []
|
|
|
|
nickname = "user"
|
|
|
|
nick_password = "password"
|
|
|
|
alt_nicks = ["user_", "user__"]
|
|
|
|
username = "user"
|
|
|
|
realname = "Test User"
|
|
|
|
server = "chat.freenode.net"
|
|
|
|
port = 6697
|
|
|
|
password = ""
|
|
|
|
use_ssl = true
|
|
|
|
cert_path = "cert.der"
|
2018-03-31 21:09:42 +02:00
|
|
|
client_cert_path = "client.der"
|
|
|
|
client_cert_pass = "password"
|
2017-08-05 02:31:45 +02:00
|
|
|
encoding = "UTF-8"
|
|
|
|
channels = ["#rust", "#haskell", "#fake"]
|
|
|
|
umodes = "+RB-x"
|
2018-01-30 01:27:23 +01:00
|
|
|
user_info = "I'm a test user for the irc crate."
|
2017-08-05 02:31:45 +02:00
|
|
|
version = "irc:git:Rust"
|
|
|
|
source = "https://github.com/aatxe/irc"
|
|
|
|
ping_time = 180
|
|
|
|
ping_timeout = 10
|
|
|
|
burst_window_length = 8
|
|
|
|
max_messages_in_burst = 15
|
|
|
|
should_ghost = false
|
|
|
|
ghost_sequence = []
|
|
|
|
|
|
|
|
[channel_keys]
|
|
|
|
"#fake" = "password"
|
|
|
|
|
|
|
|
[options]
|
|
|
|
note = "anything you want can be in here!"
|
|
|
|
and = "you can use it to build your own additional configuration options."
|
|
|
|
key = "value"
|
2016-01-18 07:17:32 +01:00
|
|
|
```
|
|
|
|
|
2017-12-13 20:31:28 +01:00
|
|
|
You can convert between different configuration formats with `convertconf` like so:
|
|
|
|
|
2018-01-30 03:10:05 +01:00
|
|
|
```shell
|
2017-12-13 20:31:28 +01:00
|
|
|
cargo run --example convertconf -- -i client_config.json -o client_config.toml
|
|
|
|
```
|
|
|
|
|
2018-08-09 02:13:50 +02:00
|
|
|
Note that the formats are automatically determined based on the selected file extensions. This
|
|
|
|
tool should make it easier for users to migrate their old configurations to TOML.
|
2017-12-13 20:31:28 +01:00
|
|
|
|
2018-03-31 16:03:19 +02:00
|
|
|
## Contributing
|
2018-02-14 14:13:44 +01:00
|
|
|
the irc crate is a free, open source library that relies on contributions from its maintainers,
|
2018-01-30 03:12:19 +01:00
|
|
|
Aaron Weiss ([@aatxe][awe]) and Peter Atashian ([@retep998][bun]), as well as the broader Rust
|
|
|
|
community. It's licensed under the Mozilla Public License 2.0 whose text can be found in
|
|
|
|
`LICENSE.md`. To foster an inclusive community around the irc crate, we have adopted a Code of
|
2018-02-14 14:11:43 +01:00
|
|
|
Conduct whose text can be found in `CODE_OF_CONDUCT.md`. You can find details about how to
|
|
|
|
contribute in `CONTRIBUTING.md`.
|
2018-01-30 03:12:19 +01:00
|
|
|
|
|
|
|
[awe]: https://github.com/aatxe/
|
|
|
|
[bun]: https://github.com/retep998/
|