From b9d892ca1b662eff1cbdbcdc1dee2f78771b5ce1 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Sat, 2 Dec 2017 08:28:36 -0500 Subject: [PATCH] Create a stupid factoid bot --- ircbot/src/bin/factoids.rs | 86 ++++++++++++++++++++++++++ ircbot/src/{main.rs => bin/gateway.rs} | 5 +- ircbot/src/lib.rs | 12 ++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 ircbot/src/bin/factoids.rs rename ircbot/src/{main.rs => bin/gateway.rs} (98%) create mode 100644 ircbot/src/lib.rs diff --git a/ircbot/src/bin/factoids.rs b/ircbot/src/bin/factoids.rs new file mode 100644 index 0000000..07c5dad --- /dev/null +++ b/ircbot/src/bin/factoids.rs @@ -0,0 +1,86 @@ +#[macro_use] +extern crate serde_derive; +extern crate serde; +extern crate serde_json; +extern crate ircbot; +extern crate irc; +extern crate amqp; +extern crate env_logger; + +#[macro_use] +extern crate log; + +use amqp::protocol::basic::Deliver; +use amqp::protocol::basic::BasicProperties; +use amqp::Basic; +use amqp::Channel; +use amqp::Session; +use amqp::Table; + + +use ircbot::config; + +use std::env; + +#[derive(Serialize, Deserialize, Debug)] +struct Message { + target: String, + body: String +} + +fn main() { + if let Err(_) = env::var("RUST_LOG") { + env::set_var("RUST_LOG", "info"); + env_logger::init().unwrap(); + info!("Defaulting RUST_LOG environment variable to info"); + } else { + env_logger::init().unwrap(); + } + + let cfg = config::load(env::args().nth(1).unwrap().as_ref()); + + let mut session = Session::open_url(&cfg.rabbitmq.as_uri()).unwrap(); + println!("Connected to rabbitmq"); + println!("About to open channel #1"); + let mut channel = session.open_channel(1).unwrap(); + + + let read_queue = channel.queue_declare("", false, false, true, + false, false, Table::new()).unwrap(); + + channel.queue_bind(read_queue.queue.as_ref(), "exchange-messages", + "".as_ref(), false, Table::new()).unwrap(); + + let consumer_name = channel.basic_consume( + |chan: &mut Channel, _deliver: Deliver, _headers: BasicProperties, body: Vec| { + debug!("Got a message"); + let msg: Result = serde_json::from_slice(&body); + if let Ok(msg) = msg { + if msg.body.trim() == "!cloudfront" { + let msg = serde_json::to_string(&Message{ + target: msg.target.clone(), + body: "https://gist.github.com/grahamc/df1bb806eb3552650d03eef7036a72ba".to_owned(), + }).unwrap(); + + chan.basic_publish( + "".to_owned(), + "queue-publish".to_owned(), + false, + false, + BasicProperties { + content_type: Some("application/json".to_owned()), + ..Default::default() + }, + msg.into_bytes() + ).expect("Failed to publish message"); + } else { + debug!("Message didn't match: {:?}", msg); + } + } + }, + read_queue.queue.as_ref(), "", false, true, false, false, Table::new()); + println!("Starting consumer {:?}", consumer_name); + + channel.start_consuming(); + channel.close(200, "Bye").unwrap(); +} diff --git a/ircbot/src/main.rs b/ircbot/src/bin/gateway.rs similarity index 98% rename from ircbot/src/main.rs rename to ircbot/src/bin/gateway.rs index 0d14e5d..c6bc72f 100644 --- a/ircbot/src/main.rs +++ b/ircbot/src/bin/gateway.rs @@ -2,7 +2,7 @@ extern crate serde_derive; extern crate serde; extern crate serde_json; - +extern crate ircbot; extern crate irc; extern crate amqp; extern crate env_logger; @@ -21,7 +21,8 @@ use amqp::Channel; use amqp::Session; use amqp::Table; -mod config; + +use ircbot::config; use std::thread; use std::env; diff --git a/ircbot/src/lib.rs b/ircbot/src/lib.rs new file mode 100644 index 0000000..38f6a51 --- /dev/null +++ b/ircbot/src/lib.rs @@ -0,0 +1,12 @@ +#[macro_use] +extern crate serde_derive; +extern crate serde; +extern crate serde_json; +extern crate irc; +extern crate amqp; +extern crate env_logger; + +pub mod config; +pub mod ircbot { + pub use config; +}