initial working implementation of autolink module
This commit is contained in:
parent
5551ebd552
commit
8c57bd6eb7
@ -16,4 +16,4 @@ time = "0.1"
|
||||
regex = "0.2"
|
||||
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" }
|
||||
stc = { git = "http://gitlab.monarch-pass.net/malacoda/stc.git" }
|
||||
|
@ -5,7 +5,7 @@ extern crate rand;
|
||||
extern crate pvn;
|
||||
extern crate echobox;
|
||||
extern crate transformable_channels;
|
||||
//extern crate stc;
|
||||
extern crate stc;
|
||||
extern crate regex;
|
||||
|
||||
#[macro_use]
|
||||
|
50
src/modules/autolink.rs
Normal file
50
src/modules/autolink.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use modules::Module;
|
||||
use toml::Table;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use transformable_channels::mpsc::ExtSender;
|
||||
|
||||
use event::{Event, Envelope};
|
||||
|
||||
use stc::searchers::{Searcher, AggregateSearcher};
|
||||
use stc::searchers::yugioh::{YugiohCard, YugiohSearcher};
|
||||
use stc::searchers::mtg::{MtgCard, MtgSearcher};
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
pub struct AutolinkModule {}
|
||||
|
||||
impl AutolinkModule {
|
||||
pub fn new (_: &Table, _: &Table) -> Box<Module> {
|
||||
Box::new(AutolinkModule {})
|
||||
}
|
||||
}
|
||||
|
||||
impl Module for AutolinkModule {
|
||||
fn run (&self, _: Box<ExtSender<Envelope>>, receiver: Receiver<Arc<Envelope>>) {
|
||||
let link_regex = Regex::new(r"\[\[([^\[\]]*)\]\]").expect("Invalid regex...");
|
||||
let mut searchers = AggregateSearcher::new();
|
||||
searchers.add_searcher("mtg", Box::new(MtgSearcher::new()));
|
||||
searchers.add_searcher("ygo", Box::new(YugiohSearcher::new()));
|
||||
|
||||
loop {
|
||||
match receiver.recv() {
|
||||
Ok(envelope) => {
|
||||
match envelope.event {
|
||||
Event::Message { ref message } => {
|
||||
debug!("Received message from module {:?}... {:?}", envelope.from, message.content);
|
||||
for cap in link_regex.captures_iter(&message.content) {
|
||||
if let Some(item) = searchers.exact_search(&cap[1]) {
|
||||
message.reply(&format!("**Autolink:** {} -> {}", item.label(), item.url()));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
Err(error) => { error!("Error {:?}", error) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user