add cat and dog modules

This commit is contained in:
Adrian Malacoda 2018-05-12 02:11:23 -05:00
parent 3a06d98247
commit ae9945403f
6 changed files with 137 additions and 3 deletions

View File

@ -18,6 +18,8 @@ multimap = "0.4.0"
notify = "4.0.0"
irc = "0.13.5"
thread_local = "0.3"
pvn = { git = "http://gitlab.monarch-pass.net/malacoda/pvn.git" }
echobox = { git = "http://gitlab.monarch-pass.net/malacoda/echobox.git" }
stc = { git = "http://gitlab.monarch-pass.net/malacoda/stc.git" }
pvn = { git = "https://gitlab.monarch-pass.net/malacoda/pvn.git" }
echobox = { git = "https://gitlab.monarch-pass.net/malacoda/echobox.git" }
stc = { git = "https://gitlab.monarch-pass.net/malacoda/stc.git" }
i-can-has-cat = { git = "https://gitlab.monarch-pass.net/malacoda/i-can-has-cat.git" }
wow-such-doge = { git = "https://gitlab.monarch-pass.net/malacoda/wow-such-doge.git" }

View File

@ -10,6 +10,8 @@ extern crate regex;
extern crate multimap;
extern crate irc;
extern crate thread_local;
extern crate i_can_has_cat;
extern crate wow_such_doge;
#[macro_use]
extern crate hlua;

62
src/modules/cat.rs Normal file
View File

@ -0,0 +1,62 @@
use modules::EventLoop;
use toml::value::Table;
use std::sync::Arc;
use std::sync::mpsc::Receiver;
use transformable_channels::mpsc::ExtSender;
use helpers::command::split_command;
use event::{Event, Envelope};
use i_can_has_cat::Cats;
pub struct CatModule {
prefix: String
}
impl CatModule {
pub fn new (_: &Table, configuration: &Table) -> Box<EventLoop> {
let prefix = configuration.get("prefix")
.and_then(|value| value.as_str())
.unwrap_or("?cat");
Box::new(CatModule {
prefix: String::from(prefix)
})
}
}
impl EventLoop for CatModule {
fn run(&self, _: Box<ExtSender<Event>>, receiver: Receiver<Arc<Envelope>>) {
let cats = Cats::new();
loop {
match receiver.recv() {
Ok(envelope) => {
match envelope.event {
Event::Message { ref message } => {
debug!("Received message from module {:?}... {:?}", envelope.from, message.content);
match split_command(&message.content) {
Some((command, argument)) => {
if command == self.prefix {
let cat = if !argument.is_empty() {
cats.random_image_of_category(argument)
} else {
cats.random_image()
};
if let Ok(cat) = cat {
message.reply(&cat.url);
}
}
},
_ => {}
}
}
_ => ()
}
}
Err(error) => { error!("Error {:?}", error) }
}
}
}
}

62
src/modules/dog.rs Normal file
View File

@ -0,0 +1,62 @@
use modules::EventLoop;
use toml::value::Table;
use std::sync::Arc;
use std::sync::mpsc::Receiver;
use transformable_channels::mpsc::ExtSender;
use helpers::command::split_command;
use event::{Event, Envelope};
use wow_such_doge::Dogs;
pub struct DogModule {
prefix: String
}
impl DogModule {
pub fn new (_: &Table, configuration: &Table) -> Box<EventLoop> {
let prefix = configuration.get("prefix")
.and_then(|value| value.as_str())
.unwrap_or("?dog");
Box::new(DogModule {
prefix: String::from(prefix)
})
}
}
impl EventLoop for DogModule {
fn run(&self, _: Box<ExtSender<Event>>, receiver: Receiver<Arc<Envelope>>) {
let dogs = Dogs::new();
loop {
match receiver.recv() {
Ok(envelope) => {
match envelope.event {
Event::Message { ref message } => {
debug!("Received message from module {:?}... {:?}", envelope.from, message.content);
match split_command(&message.content) {
Some((command, argument)) => {
if command == self.prefix {
let dog = if !argument.is_empty() {
dogs.random_image_by_breed(argument)
} else {
dogs.random_image()
};
if let Ok(dog) = dog {
message.reply(&dog);
}
}
},
_ => {}
}
}
_ => ()
}
}
Err(error) => { error!("Error {:?}", error) }
}
}
}
}

View File

@ -15,6 +15,8 @@ use modules::echobox::EchoboxModule;
use modules::autolink::AutolinkModule;
use modules::logger::LoggerModule;
use modules::irc::IrcHandler;
use modules::dog::DogModule;
use modules::cat::CatModule;
use std::sync::{Arc, Mutex};
@ -35,6 +37,8 @@ impl ModuleLoader {
types.insert("autolink", AutolinkModule::new as fn(&Table, &Table) -> Box<EventLoop>);
types.insert("logger", LoggerModule::new as fn(&Table, &Table) -> Box<EventLoop>);
types.insert("irc", IrcHandler::new as fn(&Table, &Table) -> Box<EventLoop>);
types.insert("cat", CatModule::new as fn(&Table, &Table) -> Box<EventLoop>);
types.insert("dog", DogModule::new as fn(&Table, &Table) -> Box<EventLoop>);
ModuleLoader {
types: types
}

View File

@ -8,6 +8,8 @@ pub mod echobox;
pub mod autolink;
pub mod logger;
pub mod irc;
pub mod cat;
pub mod dog;
pub mod loader;