Skip to content

Commit

Permalink
begin moving things out of librustbot
Browse files Browse the repository at this point in the history
  • Loading branch information
GinjaNinja32 committed Dec 22, 2019
1 parent 54b8289 commit c546766
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 61 deletions.
6 changes: 1 addition & 5 deletions mod_admin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ mod db;
mod raw;

#[no_mangle]
pub fn get_meta() -> Meta {
let mut meta = Meta::new();

pub fn get_meta(meta: &mut dyn Meta) {
meta.cmd("raw", Command::new(raw::raw).req_perms(Perms::Raw));
meta.cmd("join", Command::new(raw::join).req_perms(Perms::Raw));
meta.cmd("part", Command::new(raw::part).req_perms(Perms::Raw));
Expand All @@ -24,8 +22,6 @@ pub fn get_meta() -> Meta {

meta.cmd("bash", Command::new(bash::bash).req_perms(Perms::Eval));
meta.cmd("bashl", Command::new(bash::bashl).req_perms(Perms::Eval));

meta
}

fn whoami(ctx: &Context, _: &str) -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions mod_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use std::process::Command as ProcessCommand;
use std::str;

#[no_mangle]
pub fn get_meta() -> Meta {
let mut meta = Meta::new();
pub fn get_meta(meta: &mut dyn Meta) {
meta.cmd("drop", Command::new(drop).req_perms(Perms::Modules));
meta.cmd("load", Command::new(load).req_perms(Perms::Modules));
meta.cmd("reload", Command::new(reload).req_perms(Perms::Modules));
Expand All @@ -19,7 +18,6 @@ pub fn get_meta() -> Meta {
"disable",
Command::new(move |ctx, args| set_enabled(ctx, args, false)).req_perms(Perms::Modules),
);
meta
}

fn exec(ctx: &Context, args: &str, what: fn(&Context, &str) -> Result<()>) -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions mod_dice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ mod dice;
use rustbot::prelude::*;

#[no_mangle]
pub fn get_meta() -> Meta {
let mut meta = Meta::new();
pub fn get_meta(meta: &mut dyn Meta) {
meta.cmd("dice", Command::new(cmd_dice));
meta
}

fn cmd_dice(ctx: &Context, args: &str) -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions mod_dm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use std::io::prelude::*;
use std::process::Command as ProcessCommand;

#[no_mangle]
pub fn get_meta() -> Meta {
let mut meta = Meta::new();
pub fn get_meta(meta: &mut dyn Meta) {
meta.cmd("dm", Command::new(|ctx, args| dm(ctx, args, false, false)));
meta.cmd("dml", Command::new(|ctx, args| dm(ctx, args, false, true)));
meta.cmd(
Expand All @@ -20,7 +19,6 @@ pub fn get_meta() -> Meta {
"dmsl",
Command::new(|ctx, args| dm(ctx, args, true, true)).req_perms(Perms::Eval),
);
meta
}

fn dm(ctx: &Context, args: &str, secure: bool, multiline: bool) -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions mod_randomlist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ extern crate rustbot;
use rustbot::prelude::*;

#[no_mangle]
pub fn get_meta() -> Meta {
let mut meta = Meta::new();
pub fn get_meta(meta: &mut dyn Meta) {
meta.cmd("8ball", Command::new(|ctx, args| randomlist("eightball", ctx, args)));
meta.cmd("kitty", Command::new(|ctx, args| randomlist("kitty", ctx, args)));
meta.cmd("fox", Command::new(|ctx, args| randomlist("fox", ctx, args)));
meta.cmd("snek", Command::new(|ctx, args| randomlist("snek", ctx, args)));
meta.cmd("otter", Command::new(|ctx, args| randomlist("otter", ctx, args)));
meta.cmd("doggo", Command::new(|ctx, args| randomlist("doggo", ctx, args)));
meta.cmd("delrand", Command::new(delrand).req_perms(Perms::Admin));
meta
}

fn delrand(ctx: &Context, args: &str) -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions mod_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ extern crate rustbot;
use rustbot::prelude::*;

#[no_mangle]
pub fn get_meta() -> Meta {
let mut meta = Meta::new();
pub fn get_meta(meta: &mut dyn Meta) {
meta.cmd(
"test",
Command::new(|ctx, args| {
ctx.say(&format!("beep boop {}", ctx.perms()?))?;
ctx.say(&format!("you passed: {}", args))
}),
);
meta
}
5 changes: 2 additions & 3 deletions mod_weather/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ struct Module {
}

#[no_mangle]
pub fn get_meta_conf(config: toml::Value) -> Result<Meta> {
pub fn get_meta_conf(meta: &mut dyn Meta, config: toml::Value) -> Result<()> {
let m: Module = config.try_into()?;
let mut meta = Meta::new();
meta.cmd("weather", Command::arc(Arc::new(move |ctx, args| m.weather(ctx, args))));
Ok(meta)
Ok(())
}

impl Module {
Expand Down
45 changes: 37 additions & 8 deletions rustbot/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use std::time::{Duration, Instant};

use config;
use db;
use prelude::*;
use types;
use rustbot::prelude::*;
use rustbot::types;

struct Rustbot {
clients: RwLock<BTreeMap<String, Arc<irc::IrcClient>>>,
Expand Down Expand Up @@ -597,24 +597,30 @@ impl dis::EventHandler for DiscordBot {
use bot::rent_module::Module;
rental! {
mod rent_module {
use types;
use crate::bot;

#[rental]
pub struct Module {
lib: Box<libloading::Library>,
meta: types::Meta,
meta: bot::Meta,
}
}
}

fn load_module(name: &str, lib: Library) -> Result<rent_module::Module> {
let m = rent_module::Module::try_new_or_drop(Box::new(lib), |lib| unsafe {
match lib.get::<unsafe fn() -> Meta>(b"get_meta") {
Ok(f) => Ok(f()),
Err(e) => match lib.get::<unsafe fn(toml::Value) -> Result<Meta>>(b"get_meta_conf") {
match lib.get::<unsafe fn(&mut dyn types::Meta)>(b"get_meta") {
Ok(f) => {
let mut m = Meta::new();
f(&mut m);
Ok(m)
}
Err(e) => match lib.get::<unsafe fn(&mut dyn types::Meta, toml::Value) -> Result<()>>(b"get_meta_conf") {
Ok(f) => {
if let Some(c) = config::load()?.module.remove(name) {
Ok(f(c)?)
let mut m = Meta::new();
f(&mut m, c)?;
Ok(m)
} else {
Err(Error::new("required config not passed"))
}
Expand All @@ -626,3 +632,26 @@ fn load_module(name: &str, lib: Library) -> Result<rent_module::Module> {

Ok(m)
}

pub struct Meta {
commands: BTreeMap<String, Command>,
deinit: Option<Box<DeinitFn>>,
}

impl Meta {
fn new() -> Self {
Self {
commands: BTreeMap::new(),
deinit: None,
}
}
}

impl types::Meta for Meta {
fn cmd(&mut self, name: &str, cmd: Command) {
self.commands.insert(name.to_string(), cmd);
}
fn deinit(&mut self, f: Box<DeinitFn>) {
self.deinit = Some(f)
}
}
2 changes: 1 addition & 1 deletion rustbot/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Deserialize;
use std::collections::BTreeMap;
use std::fs;

use prelude::*;
use rustbot::prelude::*;

#[derive(Deserialize)]
pub struct Config {
Expand Down
11 changes: 1 addition & 10 deletions rustbot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@
extern crate bitflags;
extern crate csv;
extern crate irc;
extern crate libloading;
extern crate migrant_lib;
extern crate parking_lot;
extern crate postgres;
extern crate regex;
extern crate reqwest;
extern crate serde;
extern crate serde_json;
extern crate serenity;
#[macro_use]
extern crate rental;
extern crate toml;

pub mod error;
pub mod types;

pub mod bot;
mod config;
mod db;

pub mod prelude {
pub use error::*;
pub use types::Prefix::*;
Expand Down
16 changes: 15 additions & 1 deletion rustbot/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
extern crate irc;
extern crate libloading;
extern crate migrant_lib;
extern crate parking_lot;
extern crate postgres;
extern crate regex;
extern crate rustbot;
extern crate serde;
extern crate serde_json;
extern crate serenity;
#[macro_use]
extern crate rental;

mod bot;
mod config;
mod db;

fn main() {
match rustbot::bot::start() {
match bot::start() {
Ok(()) => start_deadlock_monitor(),
Err(e) => Err(e).unwrap(),
}
Expand Down
21 changes: 3 additions & 18 deletions rustbot/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,9 @@ impl Command {

pub type DeinitFn = dyn FnMut(&dyn Bot) -> Result<()> + Send + Sync;

pub struct Meta {
pub(crate) commands: BTreeMap<String, Command>,
pub(crate) deinit: Option<Box<DeinitFn>>,
}

impl Meta {
pub fn new() -> Meta {
Meta {
commands: BTreeMap::new(),
deinit: None,
}
}
pub fn cmd(&mut self, name: &str, cmd: Command) {
self.commands.insert(name.to_string(), cmd);
}
pub fn deinit(&mut self, f: Box<DeinitFn>) {
self.deinit = Some(f)
}
pub trait Meta {
fn cmd(&mut self, name: &str, cmd: Command);
fn deinit(&mut self, f: Box<DeinitFn>);
}

pub trait Bot {
Expand Down

0 comments on commit c546766

Please sign in to comment.