2019-08-27 15:05:51 +02:00
|
|
|
use futures::prelude::*;
|
|
|
|
use irc::{client::prelude::*, error};
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> irc::error::Result<()> {
|
|
|
|
env_logger::init();
|
2017-06-25 11:24:10 +02:00
|
|
|
|
|
|
|
let cfg1 = Config {
|
|
|
|
nickname: Some("pickles".to_owned()),
|
2020-07-23 19:40:10 +02:00
|
|
|
server: Some("chat.freenode.net".to_owned()),
|
2019-09-29 00:54:07 +02:00
|
|
|
channels: vec!["#rust-spam".to_owned()],
|
2017-06-25 11:24:10 +02:00
|
|
|
..Default::default()
|
|
|
|
};
|
2018-01-28 01:03:49 +01:00
|
|
|
|
2017-06-25 11:24:10 +02:00
|
|
|
let cfg2 = Config {
|
2018-01-28 01:03:49 +01:00
|
|
|
nickname: Some("bananas".to_owned()),
|
2020-07-23 19:40:10 +02:00
|
|
|
server: Some("chat.freenode.net".to_owned()),
|
2019-09-29 00:54:07 +02:00
|
|
|
channels: vec!["#rust-spam".to_owned()],
|
2017-06-25 11:24:10 +02:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2018-01-28 01:03:49 +01:00
|
|
|
let configs = vec![cfg1, cfg2];
|
2019-08-27 15:05:51 +02:00
|
|
|
let mut streams = Vec::new();
|
2020-03-07 06:17:35 +01:00
|
|
|
let mut senders = Vec::new();
|
2018-01-28 01:03:49 +01:00
|
|
|
|
|
|
|
for config in configs {
|
2018-01-28 03:19:05 +01:00
|
|
|
// Immediate errors like failure to resolve the server's domain or to establish any connection will
|
2018-01-28 02:16:50 +01:00
|
|
|
// manifest here in the result of prepare_client_and_connect.
|
2019-08-27 15:05:51 +02:00
|
|
|
let mut client = Client::from_config(config).await?;
|
|
|
|
client.identify()?;
|
|
|
|
|
|
|
|
streams.push(client.stream()?);
|
2020-03-07 06:17:35 +01:00
|
|
|
senders.push(client.sender());
|
2018-01-28 01:03:49 +01:00
|
|
|
}
|
|
|
|
|
2019-08-27 15:05:51 +02:00
|
|
|
loop {
|
|
|
|
let (message, index, _) =
|
|
|
|
futures::future::select_all(streams.iter_mut().map(|s| s.select_next_some())).await;
|
|
|
|
let message = message?;
|
|
|
|
let sender = &senders[index];
|
|
|
|
process_msg(sender, message)?;
|
|
|
|
}
|
2017-06-25 11:24:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 15:05:51 +02:00
|
|
|
fn process_msg(sender: &Sender, message: Message) -> error::Result<()> {
|
2020-03-07 06:17:35 +01:00
|
|
|
print!("{}", message);
|
2019-08-27 15:05:51 +02:00
|
|
|
|
2021-11-14 21:03:16 +01:00
|
|
|
if let Command::PRIVMSG(ref target, ref msg) = message.command {
|
|
|
|
if msg.contains("pickles") {
|
|
|
|
sender.send_privmsg(target, "Hi!")?;
|
2017-06-25 11:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-27 15:05:51 +02:00
|
|
|
|
2017-06-25 11:24:10 +02:00
|
|
|
Ok(())
|
|
|
|
}
|