2017-05-09 22:44:33 -05:00
|
|
|
use modules::{Module, EventLoop};
|
2018-02-18 16:29:30 -06:00
|
|
|
use toml::value::Table;
|
2017-02-13 00:22:06 -06:00
|
|
|
|
2017-02-19 02:55:30 -06:00
|
|
|
use std::sync::Arc;
|
2017-02-25 21:11:25 -06:00
|
|
|
use std::sync::mpsc::Receiver;
|
|
|
|
use transformable_channels::mpsc::ExtSender;
|
2017-02-19 05:37:56 -06:00
|
|
|
|
|
|
|
use helpers::command::split_command;
|
2017-02-25 20:17:46 -06:00
|
|
|
use event::{Event, Envelope};
|
2017-02-13 00:22:06 -06:00
|
|
|
|
2017-02-16 01:05:33 -06:00
|
|
|
pub struct EchoModule {
|
2017-02-13 00:22:06 -06:00
|
|
|
prefix: String
|
|
|
|
}
|
|
|
|
|
2017-02-16 01:05:33 -06:00
|
|
|
impl EchoModule {
|
2017-05-09 22:44:33 -05:00
|
|
|
pub fn new (_: &Table, configuration: &Table) -> Module {
|
2017-02-13 00:22:06 -06:00
|
|
|
let prefix = configuration.get("prefix")
|
|
|
|
.and_then(|value| value.as_str())
|
2017-02-19 05:37:56 -06:00
|
|
|
.unwrap_or("!echo");
|
2017-02-13 00:22:06 -06:00
|
|
|
|
2017-05-09 22:44:33 -05:00
|
|
|
Module {
|
|
|
|
module_type: String::from("echo"),
|
|
|
|
event_loop: Box::new(EchoModule {
|
|
|
|
prefix: String::from(prefix)
|
|
|
|
}),
|
|
|
|
config: configuration.clone()
|
|
|
|
}
|
2017-02-13 00:22:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:44:33 -05:00
|
|
|
impl EventLoop for EchoModule {
|
2018-02-22 02:40:22 -06:00
|
|
|
fn run(&self, _: Box<ExtSender<Event>>, receiver: Receiver<Arc<Envelope>>) {
|
2017-02-13 00:55:30 -06:00
|
|
|
loop {
|
|
|
|
match receiver.recv() {
|
2017-02-25 20:17:46 -06:00
|
|
|
Ok(envelope) => {
|
2017-02-25 20:33:47 -06:00
|
|
|
match envelope.event {
|
2017-02-19 05:37:56 -06:00
|
|
|
Event::Message { ref message } => {
|
2017-02-25 21:11:25 -06:00
|
|
|
debug!("Received message from module {:?}... {:?}", envelope.from, message.content);
|
2017-02-19 05:37:56 -06:00
|
|
|
match split_command(&message.content) {
|
|
|
|
Some((command, argument)) => {
|
|
|
|
if command == self.prefix {
|
|
|
|
message.reply(argument);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {}
|
2017-02-13 00:55:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
2017-02-13 00:22:06 -06:00
|
|
|
}
|
2017-02-17 02:38:15 -06:00
|
|
|
Err(error) => { error!("Error {:?}", error) }
|
2017-02-13 00:22:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|