Commit aa8af19c64
aa8af19c64de303120b7c0b56abd48204998b2f5
parent: fd5653ac1d
Unsigned
Damir Jelić <poljar@termina.org.uk> · 2019-11-16 20:08 UTC
commands: Add initial matrix command.
Layout: unified · split
Cargo.toml
+1
| @@ -16,6 +16,7 @@ url = "2.1.0" |
| 16 | 16 | tokio = "0.2.0-alpha.6" |
| 17 | 17 | async-std = { version = "1.0.1", features = [ "unstable" ] } |
| 18 | 18 | pipe-channel = "1.2.2" |
| 19 | clap = "2.33.0" |
| 19 | 20 | |
| 20 | 21 | matrix-nio = { path = "/home/poljar/werk/priv/nio-rust/" } |
| 21 | 22 | |
Makefile
+1 −1
| @@ -3,7 +3,7 @@ PREFIX ?= $(WEECHAT_HOME) |
| 3 | 3 | |
| 4 | 4 | .PHONY: install install-dir phony lint |
| 5 | 5 | |
| 6 | | target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/executor.rs |
| 6 | target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/executor.rs src/commands.rs |
| 7 | 7 | cargo build |
| 8 | 8 | |
| 9 | 9 | install: install-dir target/debug/libmatrix.so |
src/commands.rs
added
+60
| @@ -0,0 +1,60 @@ |
| 1 | use clap::App as Argparse; |
| 2 | use clap::AppSettings as ArgParseSettings; |
| 3 | use clap::SubCommand; |
| 4 | |
| 5 | use crate::plugin; |
| 6 | use weechat::Weechat; |
| 7 | use weechat::{ArgsWeechat, Buffer, CommandDescription, CommandHook}; |
| 8 | |
| 9 | pub struct Commands { |
| 10 | matrix: CommandHook<()>, |
| 11 | } |
| 12 | |
| 13 | impl Commands { |
| 14 | pub fn hook_all(weechat: &Weechat) -> Commands { |
| 15 | let matrix_desc = CommandDescription { |
| 16 | name: "matrix", |
| 17 | ..Default::default() |
| 18 | }; |
| 19 | |
| 20 | let matrix = weechat.hook_command( |
| 21 | matrix_desc, |
| 22 | Commands::matrix_command_cb, |
| 23 | None, |
| 24 | ); |
| 25 | |
| 26 | Commands { matrix } |
| 27 | } |
| 28 | |
| 29 | fn matrix_command_cb(_data: &(), buffer: Buffer, args: ArgsWeechat) { |
| 30 | let weechat = unsafe { Weechat::weechat() }; |
| 31 | let argparse = Argparse::new("matrix") |
| 32 | .setting(ArgParseSettings::ColorNever) |
| 33 | .subcommand(SubCommand::with_name("connect")) |
| 34 | .subcommand(SubCommand::with_name("disconnect")); |
| 35 | |
| 36 | let matches = match argparse.get_matches_from_safe(args) { |
| 37 | Ok(m) => m, |
| 38 | Err(e) => { |
| 39 | weechat |
| 40 | .print(&format!("Error parsing command arguments {}", e)); |
| 41 | return; |
| 42 | } |
| 43 | }; |
| 44 | let mut plugin = plugin(); |
| 45 | |
| 46 | if let Some(matches) = matches.subcommand_matches("connect") { |
| 47 | weechat.print("Connecting"); |
| 48 | for server in plugin.servers.values_mut() { |
| 49 | server.connect(); |
| 50 | } |
| 51 | } else if let Some(matches) = matches.subcommand_matches("disconnect") { |
| 52 | weechat.print("Disconnecting"); |
| 53 | for server in plugin.servers.values_mut() { |
| 54 | server.disconnect(); |
| 55 | } |
| 56 | } else { |
| 57 | weechat.print("Unknown subcommand"); |
| 58 | } |
| 59 | } |
| 60 | } |
src/lib.rs
+5 −1
| @@ -1,3 +1,4 @@ |
| 1 | mod commands; |
| 1 | 2 | mod executor; |
| 2 | 3 | mod room_buffer; |
| 3 | 4 | mod server; |
| @@ -9,6 +10,7 @@ use weechat::{ |
| 9 | 10 | weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult, |
| 10 | 11 | }; |
| 11 | 12 | |
| 13 | use crate::commands::Commands; |
| 12 | 14 | use crate::executor::{cleanup_executor, spawn_weechat}; |
| 13 | 15 | use crate::server::MatrixServer; |
| 14 | 16 | |
| @@ -16,6 +18,7 @@ const PLUGIN_NAME: &str = "matrix"; |
| 16 | 18 | |
| 17 | 19 | struct Matrix { |
| 18 | 20 | servers: HashMap<String, MatrixServer>, |
| 21 | commands: Commands, |
| 19 | 22 | } |
| 20 | 23 | |
| 21 | 24 | impl Matrix { |
| @@ -40,8 +43,9 @@ impl WeechatPlugin for Matrix { |
| 40 | 43 | let matrix = plugin(); |
| 41 | 44 | matrix.autoconnect(); |
| 42 | 45 | }); |
| 46 | let commands = Commands::hook_all(weechat); |
| 43 | 47 | |
| 44 | | Ok(Matrix { servers }) |
| 48 | Ok(Matrix { servers, commands }) |
| 45 | 49 | } |
| 46 | 50 | } |
| 47 | 51 | |