Initital commit of some factoids stufff

This commit is contained in:
Graham Christensen 2017-12-04 18:54:21 -05:00
parent b9d892ca1b
commit d2cfece806
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
9 changed files with 64 additions and 14 deletions

3
factoids.toml Normal file
View file

@ -0,0 +1,3 @@
[factoids]
"!cloudfront" = "https://gist.github.com/grahamc/df1bb806eb3552650d03eef7036a72ba"
"!which channel" = "https://gist.github.com/grahamc/c60578c6e6928043d29a427361634df6#which-channel-is-right-for-me"

17
ircbot/Cargo.lock generated
View file

@ -9,6 +9,7 @@ dependencies = [
"serde 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -52,7 +53,7 @@ dependencies = [
[[package]]
name = "amqp"
version = "0.1.0"
source = "git+https://github.com/grahamc/rust-amqp.git#1216885c84f7c94a205a8e41519684e7df0e0f35"
source = "git+https://github.com/grahamc/rust-amqp.git#7aedd23b36a44bf8820e708639273f19989ef688"
dependencies = [
"amq-proto 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -368,6 +369,11 @@ name = "lazy_static"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazycell"
version = "0.5.1"
@ -439,7 +445,7 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"openssl 0.9.22 (registry+https://github.com/rust-lang/crates.io-index)",
"schannel 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"schannel 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"security-framework 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"security-framework-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -582,13 +588,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "schannel"
version = "0.1.8"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"crypt32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"secur32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -918,6 +924,7 @@ dependencies = [
"checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
"checksum lazycell 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3b585b7a6811fb03aa10e74b278a0f00f8dd9b45dc681f148bb29fa5cb61859b"
"checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0"
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
@ -944,7 +951,7 @@ dependencies = [
"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db"
"checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e"
"checksum schannel 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7554288337c1110e34d7a2433518d889374c1de1a45f856b7bcddb03702131fc"
"checksum schannel 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "4330c2e874379fbd28fa67ba43239dbe8c7fb00662ceb1078bd37474f08bf5ce"
"checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d"
"checksum secur32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3f412dfa83308d893101dd59c10d6fda8283465976c28c287c5c855bf8d216bc"
"checksum security-framework 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "dfa44ee9c54ce5eecc9de7d5acbad112ee58755239381f687e564004ba4a2332"

View file

@ -11,3 +11,4 @@ amqp = { git = "https://github.com/grahamc/rust-amqp.git" }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
toml = "0.4"

View file

@ -0,0 +1,3 @@
pub mod ircbot {
}

View file

@ -38,6 +38,8 @@ fn main() {
}
let cfg = config::load(env::args().nth(1).unwrap().as_ref());
let factoids = cfg.factoids().factoids;
println!("{:?}", factoids.get("str1"));
let mut session = Session::open_url(&cfg.rabbitmq.as_uri()).unwrap();
println!("Connected to rabbitmq");
@ -52,15 +54,16 @@ fn main() {
"".as_ref(), false, Table::new()).unwrap();
let consumer_name = channel.basic_consume(
|chan: &mut Channel, _deliver: Deliver, _headers: BasicProperties, body: Vec<u8>| {
move |chan: &mut Channel, _deliver: Deliver, _headers: BasicProperties, body: Vec<u8>| {
debug!("Got a message");
let msg: Result<Message, serde_json::Error> = serde_json::from_slice(&body);
if let Ok(msg) = msg {
if msg.body.trim() == "!cloudfront" {
let msg = serde_json::to_string(&Message{
let trigger = msg.body.trim();
if let Some(response) = factoids.get(trigger) {
let resp = Some(Message{
target: msg.target.clone(),
body: "https://gist.github.com/grahamc/df1bb806eb3552650d03eef7036a72ba".to_owned(),
}).unwrap();
body: response.clone(),
});
chan.basic_publish(
"".to_owned(),
@ -71,7 +74,7 @@ fn main() {
content_type: Some("application/json".to_owned()),
..Default::default()
},
msg.into_bytes()
serde_json::to_string(&resp).unwrap().into_bytes()
).expect("Failed to publish message");
} else {
debug!("Message didn't match: {:?}", msg);

View file

@ -89,9 +89,11 @@ fn main() {
reader.for_each_incoming(|message| {
match message.command {
Command::PRIVMSG(ref target, ref msg) => {
Command::PRIVMSG(ref _target, ref msg) => {
let msg = serde_json::to_string(&Message{
target: target.clone(),
target: message.response_target()
.expect("a response target for a privmsg")
.to_owned(),
body: msg.clone(),
}).unwrap();

View file

@ -3,6 +3,7 @@ use std::path::Path;
use std::fs::File;
use std::io::Read;
use ircbot::factoids::Factoids;
use irc::client::prelude::Config as IrcConfig;
#[derive(Serialize, Deserialize, Debug)]
@ -11,7 +12,8 @@ pub struct Config {
alternate_nicknames: Vec<String>,
password: String,
channels: Vec<String>,
pub rabbitmq: RabbitMQConfig
pub rabbitmq: RabbitMQConfig,
factoid_toml: String,
}
#[derive(Serialize, Deserialize, Debug)]
@ -35,6 +37,10 @@ impl RabbitMQConfig {
}
impl Config {
pub fn factoids(&self) -> Factoids {
Factoids::load(&Path::new(&self.factoid_toml))
}
pub fn irc_config(&self) -> IrcConfig {
IrcConfig {
nickname: Some(self.nickname.clone()),

21
ircbot/src/factoids.rs Normal file
View file

@ -0,0 +1,21 @@
use std::collections::HashMap;
use std::path::Path;
use std::fs::File;
use std::io::Read;
use toml;
#[derive(Serialize, Deserialize, Debug)]
pub struct Factoids {
pub factoids: HashMap<String, String>
}
impl Factoids {
pub fn load(src: &Path) -> Factoids {
let mut file = File::open(src).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
return toml::from_str(&contents).unwrap();
}
}

View file

@ -5,8 +5,12 @@ extern crate serde_json;
extern crate irc;
extern crate amqp;
extern crate env_logger;
extern crate toml;
pub mod factoids;
pub mod config;
pub mod ircbot {
pub use config;
pub use factoids;
}