Port to tokio 0.2

This commit is contained in:
John-John Tedro 2019-08-27 15:05:51 +02:00
parent 9bb7fa8ba2
commit 549e2e8722
42 changed files with 2361 additions and 2767 deletions

View file

@ -1,10 +1,12 @@
extern crate irc;
use std::default::Default;
use irc::error;
use irc::client::prelude::*;
use futures::prelude::*;
use irc::{client::prelude::*, error};
#[tokio::main]
async fn main() -> irc::error::Result<()> {
env_logger::init();
fn main() {
let cfg1 = Config {
nickname: Some("pickles".to_owned()),
server: Some("irc.mozilla.org".to_owned()),
@ -20,32 +22,39 @@ fn main() {
};
let configs = vec![cfg1, cfg2];
let mut reactor = IrcReactor::new().unwrap();
let mut senders = Vec::new();
let mut streams = Vec::new();
for config in configs {
// Immediate errors like failure to resolve the server's domain or to establish any connection will
// manifest here in the result of prepare_client_and_connect.
let client = reactor.prepare_client_and_connect(config).unwrap();
client.identify().unwrap();
// Here, we tell the reactor to setup this client for future handling (in run) using the specified
// handler function process_msg.
reactor.register_client_with_handler(client, process_msg);
let mut client = Client::from_config(config).await?;
client.identify()?;
senders.push(client.sender());
streams.push(client.stream()?);
}
// Runtime errors like a dropped connection will manifest here in the result of run.
reactor.run().unwrap();
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)?;
}
}
fn process_msg(client: &IrcClient, message: Message) -> error::Result<()> {
print!("{}", message);
fn process_msg(sender: &Sender, message: Message) -> error::Result<()> {
// print!("{}", message);
match message.command {
Command::PRIVMSG(ref target, ref msg) => {
if msg.contains("pickles") {
client.send_privmsg(target, "Hi!")?;
sender.send_privmsg(target, "Hi!")?;
}
}
_ => (),
}
Ok(())
}