2017-02-16 01:05:33 -06:00
|
|
|
use modules::Module;
|
2017-02-13 00:22:06 -06:00
|
|
|
use toml::Table;
|
|
|
|
|
2017-02-19 02:55:30 -06:00
|
|
|
use std::sync::Arc;
|
2017-02-13 00:55:30 -06:00
|
|
|
use std::sync::mpsc::Receiver;
|
2017-02-13 00:22:06 -06:00
|
|
|
use event::Event;
|
|
|
|
|
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 {
|
|
|
|
pub fn new (configuration: &Table) -> Box<Module> {
|
2017-02-13 00:22:06 -06:00
|
|
|
let prefix = configuration.get("prefix")
|
|
|
|
.and_then(|value| value.as_str())
|
|
|
|
.unwrap_or("!echo ");
|
|
|
|
|
2017-02-16 01:05:33 -06:00
|
|
|
Box::new(EchoModule {
|
2017-02-13 00:22:06 -06:00
|
|
|
prefix: String::from(prefix)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 01:05:33 -06:00
|
|
|
impl Module for EchoModule {
|
2017-02-19 02:55:30 -06:00
|
|
|
fn consume_events (&self, receiver: Receiver<Arc<Event>>) {
|
2017-02-13 00:55:30 -06:00
|
|
|
loop {
|
|
|
|
match receiver.recv() {
|
|
|
|
Ok(event) => {
|
2017-02-19 02:55:30 -06:00
|
|
|
match *event {
|
|
|
|
Event::Message { content: ref message, ref channel, ref sender } => {
|
2017-02-17 02:38:15 -06:00
|
|
|
debug!("Received message... {:?}", message);
|
2017-02-13 00:55:30 -06:00
|
|
|
if message.starts_with(self.prefix.as_str()) {
|
2017-02-13 21:44:37 -06:00
|
|
|
let substring = &message[self.prefix.chars().count()..];
|
2017-02-19 02:55:30 -06:00
|
|
|
match *channel {
|
|
|
|
Some(ref channel) => channel.send(substring),
|
2017-02-13 21:44:37 -06:00
|
|
|
None => sender.send(substring)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|