From ceeb329fd1d1d690c2118bc4c7b4649b2acab117 Mon Sep 17 00:00:00 2001 From: lalanza808 Date: Mon, 23 Mar 2020 23:58:50 -0700 Subject: [PATCH] use updated irc api --- src/main.rs | 72 +++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index a8b0848..e0e89a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,47 +1,36 @@ extern crate irc; extern crate tokio_postgres; -use irc::client::prelude::{Client, ClientExt, Command, IrcClient}; -use tokio_postgres::{NoTls, Error}; +use irc::client::prelude::{ClientExt, Command, Config, IrcReactor}; +use tokio_postgres::{NoTls, Error, Client}; #[tokio::main] async fn main() -> Result<(), Error> { println!("[DEBUG] Connecting to IRC server..."); - let client = IrcClient::new("config.toml").unwrap(); - - // Connect to the database. + let irc_config = Config::load("config.toml").unwrap(); let (pg_client, connection) = tokio_postgres::connect("host=localhost user=postgres password=postgres", NoTls) .await?; + let mut reactor = IrcReactor::new().unwrap(); + let irc_client = reactor.prepare_client_and_connect(&irc_config).unwrap(); + irc_client.identify().unwrap(); - // The connection object performs the actual communication with the database, - // so spawn it off to run on its own. tokio::spawn(async move { if let Err(e) = connection.await { eprintln!("connection error: {}", e); } }); - // Now we can execute a simple statement that just returns its parameter. - let rows = pg_client - .query("SELECT $1::TEXT", &[&"hello world"]) - .await?; - - // And then check that we got back the same string we sent over. - let value: &str = rows[0].get(0); - assert_eq!(value, "hello world"); - - client.identify().unwrap(); - client.for_each_incoming(|im| { - - if let Command::NOTICE(channel, message) = &im.command { - println!("[{:?}][{}]: {}", &im.response_target(), &channel, &message) + reactor.register_client_with_handler(irc_client, |c, m| { + if let Command::NOTICE(channel, message) = &m.command { + println!("[{:?}][{}]: {}", &m.response_target(), &channel, &message); } - if let Command::PRIVMSG(channel, message) = &im.command { - let src_nick = &im.source_nickname().unwrap(); + if let Command::PRIVMSG(channel, message) = &m.command { + let src_nick = &m.source_nickname().unwrap(); + let res_target = &m.response_target().unwrap(); let help_msg = vec![ - format!("Hey there {}, here's what you can do.", &im.source_nickname().unwrap()), + format!("Hey there {}, here's what you can do.", &m.source_nickname().unwrap()), "Say '!create' followed by the title of your listing to create a new one.".to_string(), "Example: '!create I am offering virtual guitar lessons/sessions'.".to_string(), "Say '!delete' followed by the name or ID of an existing listing.".to_string(), @@ -49,33 +38,34 @@ async fn main() -> Result<(), Error> { ]; let help_msg = help_msg.join(" "); - // Print all messages - println!( - "[{}][{}]: {}", - &im.response_target().unwrap(), - src_nick, - message - ); + // Print user messages to channel + println!("[{}][{}]: {}", res_target, src_nick, message); if message.starts_with("!create") { let user_msg = message.split(" "); let mut user_msg: Vec<&str> = user_msg.collect(); user_msg.remove(0); let user_msg = user_msg.join(" "); - println!("it looks like you said: '{:?}' - Trying to save to the DB", user_msg); - // pg_client.query( - // "INSERT INTO posts (nick, post_title) VALUES ('$1', '$2')", - // &[src_nick, &user_msg] - // ); - // let value: &str = rows2[0].get(0); - // println!("{}", value); + println!("it looks like you said: {:?} - Trying to save to the DB", user_msg); + // save to db } else if message.starts_with("!delete") { - let _ = client.send_privmsg(&channel, "This feature is still being worked on."); + let _ = c.send_privmsg(&channel, "This feature is still being worked on."); } else if message.starts_with("!help") { - let _ = client.send_privmsg(&channel, &help_msg); + let _ = c.send_privmsg(&channel, &help_msg); } } - }).unwrap(); + Ok(()) + }); + reactor.run().unwrap(); Ok(()) } + +// async fn insert_post(client: &Client, nick: &str, user_msg: &str) -> String { +// let rows2 = pg_client.query( +// "INSERT INTO posts (nick, post_title) VALUES ('$1', '$2')", +// &[&src_nick, &user_msg] +// ).await?; +// let value: &str = rows2[0].get(0); +// println!("{}", value); +// }