rust-irc/examples/repeater.rs
John-John Tedro 5189b69e7e Simplify Config structure
This simplifies some of the `Config` structure, in particular this
means:

Parameters which are meaningfully equivalent longer stored in
an `Option<T>`, an example of this is `channels`. If you don't
want to join any channels you simply leave it as empty instead.
In effect, `None` is behaviorally equivalent to `vec![]`.

We don't allocate when accessing certain configuration options.
For example, when accessing `channels` we used to allocate a
vector to handle the "empty case", we simply return the slice
corresponding to the list of channels instead.

We skip serializing empty or optional configuration fields.
From a deserialization perspective this is already something
that was mostly supported through use of `Option<T>` and
`#[serde(default)]`.
2019-12-27 17:12:46 +01:00

42 lines
1.4 KiB
Rust

use futures::prelude::*;
use irc::client::prelude::*;
#[tokio::main]
async fn main() -> irc::error::Result<()> {
let config = Config {
nickname: Some("repeater".to_owned()),
alt_nicks: vec!["blaster".to_owned(), "smg".to_owned()],
server: Some("irc.mozilla.org".to_owned()),
use_ssl: true,
channels: vec!["#rust-spam".to_owned()],
burst_window_length: Some(4),
max_messages_in_burst: Some(4),
..Default::default()
};
let mut client = Client::from_config(config).await?;
client.identify()?;
let mut stream = client.stream()?;
loop {
let message = stream.select_next_some().await?;
if let Command::PRIVMSG(ref target, ref msg) = message.command {
if msg.starts_with(&*client.current_nickname()) {
let tokens: Vec<_> = msg.split(' ').collect();
if tokens.len() > 2 {
let n = tokens[0].len() + tokens[1].len() + 2;
if let Ok(count) = tokens[1].parse::<u8>() {
for _ in 0..count {
client.send_privmsg(
message.response_target().unwrap_or(target),
&msg[n..],
)?;
}
}
}
}
}
}
}