Skip to content
Extraits de code Groupes Projets
Vérifiée Valider ceb9bb7f rédigé par Kubat's avatar Kubat
Parcourir les fichiers

WIP: Almost there

parent ec076200
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -2,25 +2,26 @@ pub mod acl; ...@@ -2,25 +2,26 @@ pub mod acl;
pub mod package; pub mod package;
use log::error; use log::error;
use std::{process::abort, string::String}; use std::{process::abort, string::String, sync::Arc};
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Clone)]
enum CmdHandler { enum CmdHandler {
Simple(Box<dyn Fn(Vec<String>) -> bool>), Simple(Arc<dyn Fn(Vec<String>) -> bool>),
Echo(Box<dyn Fn(Vec<String>) -> String>), Echo(Arc<dyn Fn(Vec<String>) -> String>),
MultiEcho(Box<dyn Fn(Vec<String>) -> Vec<String>>), MultiEcho(Arc<dyn Fn(Vec<String>) -> Vec<String>>),
} }
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Clone)]
pub struct Cmd { pub struct Cmd {
name: String, name: String,
argc: u32, argc: u32,
handler: CmdHandler, handler: CmdHandler,
} }
// unsafe impl Sync for CmdHandler {}
unsafe impl Sync for Cmd {} unsafe impl Sync for Cmd {}
// unsafe impl Send for Cmd {} unsafe impl Send for Cmd {}
impl Cmd { impl Cmd {
#[allow(dead_code)] #[allow(dead_code)]
...@@ -51,7 +52,7 @@ impl Cmd { ...@@ -51,7 +52,7 @@ impl Cmd {
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn new_simple(name: String, argc: u32, f: Box<dyn Fn(Vec<String>) -> bool>) -> Cmd { pub fn new_simple(name: String, argc: u32, f: Arc<dyn Fn(Vec<String>) -> bool>) -> Cmd {
Cmd { Cmd {
name: name, name: name,
argc: argc, argc: argc,
...@@ -60,7 +61,7 @@ impl Cmd { ...@@ -60,7 +61,7 @@ impl Cmd {
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn new_echo(name: String, argc: u32, f: Box<dyn Fn(Vec<String>) -> String>) -> Cmd { pub fn new_echo(name: String, argc: u32, f: Arc<dyn Fn(Vec<String>) -> String>) -> Cmd {
Cmd { Cmd {
name: name, name: name,
argc: argc, argc: argc,
...@@ -72,7 +73,7 @@ impl Cmd { ...@@ -72,7 +73,7 @@ impl Cmd {
pub fn new_multi_echo( pub fn new_multi_echo(
name: String, name: String,
argc: u32, argc: u32,
f: Box<dyn Fn(Vec<String>) -> Vec<String>>, f: Arc<dyn Fn(Vec<String>) -> Vec<String>>,
) -> Cmd { ) -> Cmd {
Cmd { Cmd {
name: name, name: name,
......
...@@ -6,6 +6,7 @@ use ini::Properties as IniSection; ...@@ -6,6 +6,7 @@ use ini::Properties as IniSection;
use matrix_sdk::{room::Joined as JoinedRoom, ruma::UserId}; use matrix_sdk::{room::Joined as JoinedRoom, ruma::UserId};
use std::{convert::TryFrom, fmt}; use std::{convert::TryFrom, fmt};
#[derive(Clone)]
pub struct CmdPackage { pub struct CmdPackage {
name: String, name: String,
acl: CmdAcl, acl: CmdAcl,
......
#![feature(clone_closures)]
mod cmd; mod cmd;
mod config; mod config;
mod matrix; mod matrix;
......
...@@ -16,7 +16,7 @@ use matrix_sdk::{ ...@@ -16,7 +16,7 @@ use matrix_sdk::{
}, },
Client, Result, SyncSettings, Client, Result, SyncSettings,
}; };
use std::{convert::TryFrom, process::abort}; use std::{convert::TryFrom, process::abort, sync::Arc};
use tokio::time::{sleep, Duration}; use tokio::time::{sleep, Duration};
async fn join_room(room: &Invited) { async fn join_room(room: &Invited) {
...@@ -82,17 +82,16 @@ async fn startup_and_log_rooms(client: &Client) { ...@@ -82,17 +82,16 @@ async fn startup_and_log_rooms(client: &Client) {
pub async fn connect_and_handle(config: Config) -> Result<()> { pub async fn connect_and_handle(config: Config) -> Result<()> {
// TODO: Refactor // TODO: Refactor
let username_copy = config.user_name.clone();
let pkg_commands_vec = vec![ let pkg_commands_vec = vec![
Cmd::new_simple(format!("ping"), 0, Box::new(|_x| true)), Cmd::new_simple(format!("ping"), 0, Arc::new(|_x| true)),
Cmd::new_echo( Cmd::new_echo(
format!("bot_name"), format!("bot_name"),
0, 0,
Box::new(move |_x| username_copy.clone()), Arc::new(move |_x| format!("toto")),
), ),
]; ];
let pkg = match config.section("basic") { let pkg = match config.section("basic") {
Some(section) => Box::new(CmdPackage::from_config(&section, pkg_commands_vec)), Some(section) => CmdPackage::from_config(&section, pkg_commands_vec),
None => { None => {
abort(); abort();
} }
...@@ -123,7 +122,7 @@ pub async fn connect_and_handle(config: Config) -> Result<()> { ...@@ -123,7 +122,7 @@ pub async fn connect_and_handle(config: Config) -> Result<()> {
client client
.register_event_handler( .register_event_handler(
|ev: SyncMessageEvent<MessageEventContent>, move |ev: SyncMessageEvent<MessageEventContent>,
room: Room, room: Room,
_encryption_info: Option<EncryptionInfo>| async { _encryption_info: Option<EncryptionInfo>| async {
let sender_id = ev.sender; let sender_id = ev.sender;
...@@ -145,7 +144,7 @@ pub async fn connect_and_handle(config: Config) -> Result<()> { ...@@ -145,7 +144,7 @@ pub async fn connect_and_handle(config: Config) -> Result<()> {
if body.starts_with("%") { if body.starts_with("%") {
info!("Got message from {sender_id} in {room_id} a.k.a. {room_name}"); info!("Got message from {sender_id} in {room_id} a.k.a. {room_name}");
let args = vec![]; let args = vec![];
let ret = &pkg.handle(&joined, &sender_id, args); let locked_pkg = pkg.handle(&joined, &sender_id, args);
} }
} }
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter