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"
16tokio = "0.2.0-alpha.6" 16tokio = "0.2.0-alpha.6"
17async-std = { version = "1.0.1", features = [ "unstable" ] } 17async-std = { version = "1.0.1", features = [ "unstable" ] }
18pipe-channel = "1.2.2" 18pipe-channel = "1.2.2"
19clap = "2.33.0"
19 20
20matrix-nio = { path = "/home/poljar/werk/priv/nio-rust/" } 21matrix-nio = { path = "/home/poljar/werk/priv/nio-rust/" }
21 22
Makefile +1 −1
@@ -3,7 +3,7 @@ PREFIX ?= $(WEECHAT_HOME)
3 3
4.PHONY: install install-dir phony lint 4.PHONY: install install-dir phony lint
5 5
6target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/executor.rs 6target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/executor.rs src/commands.rs
7 cargo build 7 cargo build
8 8
9install: install-dir target/debug/libmatrix.so 9install: install-dir target/debug/libmatrix.so
src/commands.rs added +60
@@ -0,0 +1,60 @@
1use clap::App as Argparse;
2use clap::AppSettings as ArgParseSettings;
3use clap::SubCommand;
4
5use crate::plugin;
6use weechat::Weechat;
7use weechat::{ArgsWeechat, Buffer, CommandDescription, CommandHook};
8
9pub struct Commands {
10 matrix: CommandHook<()>,
11}
12
13impl 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 @@
1mod commands;
1mod executor; 2mod executor;
2mod room_buffer; 3mod room_buffer;
3mod server; 4mod server;
@@ -9,6 +10,7 @@ use weechat::{
9 weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult, 10 weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult,
10}; 11};
11 12
13use crate::commands::Commands;
12use crate::executor::{cleanup_executor, spawn_weechat}; 14use crate::executor::{cleanup_executor, spawn_weechat};
13use crate::server::MatrixServer; 15use crate::server::MatrixServer;
14 16
@@ -16,6 +18,7 @@ const PLUGIN_NAME: &str = "matrix";
16 18
17struct Matrix { 19struct Matrix {
18 servers: HashMap<String, MatrixServer>, 20 servers: HashMap<String, MatrixServer>,
21 commands: Commands,
19} 22}
20 23
21impl Matrix { 24impl Matrix {
@@ -40,8 +43,9 @@ impl WeechatPlugin for Matrix {
40 let matrix = plugin(); 43 let matrix = plugin();
41 matrix.autoconnect(); 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