Commit 6aa326c390
Unsigned
Layout: unified · split
Cargo.lock +31 −14
| @@ -743,6 +743,14 @@ dependencies = [ | ||
| 743 | 743 | "hashbrown 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", |
| 744 | 744 | ] |
| 745 | 745 | |
| 746 | [[package]] | |
| 747 | name = "indoc" | |
| 748 | version = "1.0.2" | |
| 749 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 750 | dependencies = [ | |
| 751 | "unindent 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", | |
| 752 | ] | |
| 753 | ||
| 746 | 754 | [[package]] |
| 747 | 755 | name = "instant" |
| 748 | 756 | version = "0.1.6" |
| @@ -868,7 +876,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 868 | 876 | [[package]] |
| 869 | 877 | name = "matrix-sdk" |
| 870 | 878 | version = "0.1.0" |
| 871 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" | |
| 879 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62" | |
| 872 | 880 | dependencies = [ |
| 873 | 881 | "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", |
| 874 | 882 | "dashmap 3.11.10 (registry+https://github.com/rust-lang/crates.io-index)", |
| @@ -888,7 +896,7 @@ dependencies = [ | ||
| 888 | 896 | [[package]] |
| 889 | 897 | name = "matrix-sdk-base" |
| 890 | 898 | version = "0.1.0" |
| 891 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" | |
| 899 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62" | |
| 892 | 900 | dependencies = [ |
| 893 | 901 | "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", |
| 894 | 902 | "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk/)", |
| @@ -905,7 +913,7 @@ dependencies = [ | ||
| 905 | 913 | [[package]] |
| 906 | 914 | name = "matrix-sdk-common" |
| 907 | 915 | version = "0.1.0" |
| 908 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" | |
| 916 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62" | |
| 909 | 917 | dependencies = [ |
| 910 | 918 | "assign 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", |
| 911 | 919 | "futures-locks 0.5.1-pre (git+https://github.com/asomers/futures-locks)", |
| @@ -919,7 +927,7 @@ dependencies = [ | ||
| 919 | 927 | [[package]] |
| 920 | 928 | name = "matrix-sdk-common-macros" |
| 921 | 929 | version = "0.1.0" |
| 922 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" | |
| 930 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62" | |
| 923 | 931 | dependencies = [ |
| 924 | 932 | "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", |
| 925 | 933 | "syn 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", |
| @@ -928,7 +936,7 @@ dependencies = [ | ||
| 928 | 936 | [[package]] |
| 929 | 937 | name = "matrix-sdk-crypto" |
| 930 | 938 | version = "0.1.0" |
| 931 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" | |
| 939 | source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62" | |
| 932 | 940 | dependencies = [ |
| 933 | 941 | "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", |
| 934 | 942 | "atomic 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", |
| @@ -1981,6 +1989,11 @@ name = "unicode-xid" | ||
| 1981 | 1989 | version = "0.2.1" |
| 1982 | 1990 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1983 | 1991 | |
| 1992 | [[package]] | |
| 1993 | name = "unindent" | |
| 1994 | version = "0.1.6" | |
| 1995 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1996 | ||
| 1984 | 1997 | [[package]] |
| 1985 | 1998 | name = "url" |
| 1986 | 1999 | version = "2.1.1" |
| @@ -2122,7 +2135,7 @@ dependencies = [ | ||
| 2122 | 2135 | [[package]] |
| 2123 | 2136 | name = "weechat" |
| 2124 | 2137 | version = "0.3.1" |
| 2125 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 2138 | source = "git+https://github.com/poljar/rust-weechat#dc47d110249ca823f44c835c3a210fd8a048220e" | |
| 2126 | 2139 | dependencies = [ |
| 2127 | 2140 | "async-task 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2128 | 2141 | "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", |
| @@ -2132,14 +2145,14 @@ dependencies = [ | ||
| 2132 | 2145 | "paste 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2133 | 2146 | "pipe-channel 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2134 | 2147 | "strum 0.19.2 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2135 | "weechat-macro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", | |
| 2136 | "weechat-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", | |
| 2148 | "weechat-macro 0.3.1 (git+https://github.com/poljar/rust-weechat)", | |
| 2149 | "weechat-sys 0.3.1 (git+https://github.com/poljar/rust-weechat)", | |
| 2137 | 2150 | ] |
| 2138 | 2151 | |
| 2139 | 2152 | [[package]] |
| 2140 | 2153 | name = "weechat-macro" |
| 2141 | 2154 | version = "0.3.1" |
| 2142 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 2155 | source = "git+https://github.com/poljar/rust-weechat#dc47d110249ca823f44c835c3a210fd8a048220e" | |
| 2143 | 2156 | dependencies = [ |
| 2144 | 2157 | "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2145 | 2158 | "proc-macro2 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", |
| @@ -2153,20 +2166,22 @@ version = "0.1.0" | ||
| 2153 | 2166 | dependencies = [ |
| 2154 | 2167 | "async-std 1.6.3 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2155 | 2168 | "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2169 | "chrono 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)", | |
| 2156 | 2170 | "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2171 | "indoc 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", | |
| 2157 | 2172 | "matrix-sdk 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk/)", |
| 2158 | 2173 | "tokio 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2159 | 2174 | "tracing 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2160 | 2175 | "tracing-subscriber 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2161 | 2176 | "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2162 | 2177 | "uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2163 | "weechat 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", | |
| 2178 | "weechat 0.3.1 (git+https://github.com/poljar/rust-weechat)", | |
| 2164 | 2179 | ] |
| 2165 | 2180 | |
| 2166 | 2181 | [[package]] |
| 2167 | 2182 | name = "weechat-sys" |
| 2168 | 2183 | version = "0.3.1" |
| 2169 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 2184 | source = "git+https://github.com/poljar/rust-weechat#dc47d110249ca823f44c835c3a210fd8a048220e" | |
| 2170 | 2185 | dependencies = [ |
| 2171 | 2186 | "bindgen 0.55.1 (registry+https://github.com/rust-lang/crates.io-index)", |
| 2172 | 2187 | "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)", |
| @@ -2344,6 +2359,7 @@ dependencies = [ | ||
| 2344 | 2359 | "checksum hyper-tls 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed" |
| 2345 | 2360 | "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" |
| 2346 | 2361 | "checksum indexmap 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4e47a3566dd4fd4eec714ae6ceabdee0caec795be835c223d92c2d40f1e8cf1c" |
| 2362 | "checksum indoc 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "644defcefee68d7805653a682e99a2e2a5014a1fc3cc9be7059a215844eeea6f" | |
| 2347 | 2363 | "checksum instant 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485" |
| 2348 | 2364 | "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" |
| 2349 | 2365 | "checksum ipnet 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135" |
| @@ -2478,6 +2494,7 @@ dependencies = [ | ||
| 2478 | 2494 | "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" |
| 2479 | 2495 | "checksum unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" |
| 2480 | 2496 | "checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" |
| 2497 | "checksum unindent 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "af41d708427f8fd0e915dcebb2cae0f0e6acb2a939b2d399c265c39a38a18942" | |
| 2481 | 2498 | "checksum url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" |
| 2482 | 2499 | "checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" |
| 2483 | 2500 | "checksum vcpkg 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" |
| @@ -2496,9 +2513,9 @@ dependencies = [ | ||
| 2496 | 2513 | "checksum wasm-bindgen-macro-support 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "841a6d1c35c6f596ccea1f82504a192a60378f64b3bb0261904ad8f2f5657556" |
| 2497 | 2514 | "checksum wasm-bindgen-shared 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "93b162580e34310e5931c4b792560108b10fd14d64915d7fff8ff00180e70092" |
| 2498 | 2515 | "checksum web-sys 0.3.44 (registry+https://github.com/rust-lang/crates.io-index)" = "dda38f4e5ca63eda02c059d243aa25b5f35ab98451e518c51612cd0f1bd19a47" |
| 2499 | "checksum weechat 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9347ef4aec950d94e5e2628cd9a7f5359c7ebb5c488f5a820a3e20bf7d114bcd" | |
| 2500 | "checksum weechat-macro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a939b44ce807e5e0dfffdd5df9535e890cc85de95f5c40bd5871575d61397f46" | |
| 2501 | "checksum weechat-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "432d9ec63b71ca962fa9df527d5eabd175bf1b484960fc090e341116fb4db133" | |
| 2516 | "checksum weechat 0.3.1 (git+https://github.com/poljar/rust-weechat)" = "<none>" | |
| 2517 | "checksum weechat-macro 0.3.1 (git+https://github.com/poljar/rust-weechat)" = "<none>" | |
| 2518 | "checksum weechat-sys 0.3.1 (git+https://github.com/poljar/rust-weechat)" = "<none>" | |
| 2502 | 2519 | "checksum wepoll-sys-stjepang 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd319e971980166b53e17b1026812ad66c6b54063be879eb182342b55284694" |
| 2503 | 2520 | "checksum which 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" |
| 2504 | 2521 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" |
Cargo.toml +3 −1
| @@ -18,9 +18,11 @@ async-std = { version = "1.6.3", features = [ "unstable" ] } | ||
| 18 | 18 | clap = "2.33.3" |
| 19 | 19 | async-trait = "0.1.40" |
| 20 | 20 | uuid = { version = "0.8.1", features = ["v4"] } |
| 21 | indoc = "1.0.2" | |
| 22 | chrono = "0.4.15" | |
| 21 | 23 | |
| 22 | 24 | [dependencies.weechat] |
| 23 | version = "0.3.1" | |
| 25 | git = "https://github.com/poljar/rust-weechat" | |
| 24 | 26 | features = ["async", "config_macro"] |
| 25 | 27 | |
| 26 | 28 | [dependencies.matrix-sdk] |
Makefile +3 −1
| @@ -1,9 +1,11 @@ | ||
| 1 | 1 | WEECHAT_HOME ?= $(HOME)/.weechat |
| 2 | 2 | PREFIX ?= $(WEECHAT_HOME) |
| 3 | 3 | |
| 4 | SOURCES := $(wildcard src/*.rs src/commands/*.rs) | |
| 5 | ||
| 4 | 6 | .PHONY: install install-dir lint |
| 5 | 7 | |
| 6 | target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/commands/mod.rs src/config.rs | |
| 8 | target/debug/libmatrix.so: $(SOURCES) | |
| 7 | 9 | cargo build |
| 8 | 10 | |
| 9 | 11 | install: install-dir target/debug/libmatrix.so |
src/commands/devices.rs added +105
| @@ -0,0 +1,105 @@ | ||
| 1 | use clap::{ | |
| 2 | App as Argparse, AppSettings as ArgParseSettings, Arg, | |
| 3 | SubCommand, | |
| 4 | }; | |
| 5 | ||
| 6 | use weechat::{ | |
| 7 | buffer::Buffer, | |
| 8 | hooks::{Command, CommandCallback, CommandSettings}, | |
| 9 | Args, Weechat, | |
| 10 | }; | |
| 11 | ||
| 12 | use crate::Servers; | |
| 13 | ||
| 14 | pub struct DevicesCommand { | |
| 15 | servers: Servers, | |
| 16 | } | |
| 17 | ||
| 18 | impl DevicesCommand { | |
| 19 | pub fn create(servers: &Servers) -> Result<Command, ()> { | |
| 20 | let settings = CommandSettings::new("devices") | |
| 21 | .description("List, delete or rename Matrix devices.") | |
| 22 | .add_argument("list") | |
| 23 | .add_argument("delete <device-id>") | |
| 24 | .add_argument("set-name <device-id> <name>") | |
| 25 | .arguments_description( | |
| 26 | "device-id: The unique id of the device that should be deleted. | |
| 27 | name: | |
| 28 | ", | |
| 29 | ) | |
| 30 | .add_completion("list") | |
| 31 | .add_completion("delete %(matrix-own-devices)") | |
| 32 | .add_completion("set-name %(matrix-own-devices)") | |
| 33 | .add_completion("help list|delete|set-name"); | |
| 34 | ||
| 35 | Command::new( | |
| 36 | settings, | |
| 37 | DevicesCommand { | |
| 38 | servers: servers.clone(), | |
| 39 | }, | |
| 40 | ) | |
| 41 | } | |
| 42 | ||
| 43 | fn list(&self, buffer: &Buffer) { | |
| 44 | let server = self.servers.find_server(buffer); | |
| 45 | ||
| 46 | if let Some(s) = server { | |
| 47 | let devices = || async move { | |
| 48 | s.devices().await; | |
| 49 | }; | |
| 50 | Weechat::spawn(devices()); | |
| 51 | } else { | |
| 52 | Weechat::print("Must be executed on Matrix buffer") | |
| 53 | } | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | impl CommandCallback for DevicesCommand { | |
| 58 | fn callback( | |
| 59 | &mut self, | |
| 60 | _: &Weechat, | |
| 61 | buffer: &Buffer, | |
| 62 | arguments: Args, | |
| 63 | ) { | |
| 64 | let argparse = Argparse::new("devices") | |
| 65 | .global_setting(ArgParseSettings::DisableHelpFlags) | |
| 66 | .global_setting(ArgParseSettings::DisableVersion) | |
| 67 | .global_setting(ArgParseSettings::VersionlessSubcommands) | |
| 68 | .setting(ArgParseSettings::SubcommandRequiredElseHelp) | |
| 69 | .subcommand( | |
| 70 | SubCommand::with_name("list") | |
| 71 | .about("List your own Matrix devices on the server."), | |
| 72 | ) | |
| 73 | .subcommand( | |
| 74 | SubCommand::with_name("delete") | |
| 75 | .about("Delete the given device") | |
| 76 | .arg(Arg::with_name("device-id").required(true)), | |
| 77 | ) | |
| 78 | .subcommand( | |
| 79 | SubCommand::with_name("set-name") | |
| 80 | .about("Set the human readable name of the given device") | |
| 81 | .arg(Arg::with_name("device-id").required(true)) | |
| 82 | .arg(Arg::with_name("name").required(true)) | |
| 83 | ); | |
| 84 | ||
| 85 | let matches = match argparse.get_matches_from_safe(arguments) { | |
| 86 | Ok(m) => m, | |
| 87 | Err(e) => { | |
| 88 | Weechat::print( | |
| 89 | &Weechat::execute_modifier( | |
| 90 | "color_decode_ansi", | |
| 91 | "1", | |
| 92 | &e.to_string(), | |
| 93 | ) | |
| 94 | .unwrap(), | |
| 95 | ); | |
| 96 | return; | |
| 97 | } | |
| 98 | }; | |
| 99 | ||
| 100 | match matches.subcommand() { | |
| 101 | ("list", _) => self.list(buffer), | |
| 102 | _ => Weechat::print(&format!("{}Subcommand isn't implemented", Weechat::prefix("error"))), | |
| 103 | } | |
| 104 | } | |
| 105 | } | |
src/commands/matrix.rs +2 −4
| @@ -4,15 +4,14 @@ use clap::{ | ||
| 4 | 4 | }; |
| 5 | 5 | use url::Url; |
| 6 | 6 | |
| 7 | use crate::PLUGIN_NAME; | |
| 8 | ||
| 9 | use crate::{config::ConfigHandle, MatrixServer, Servers}; | |
| 10 | 7 | use weechat::{ |
| 11 | 8 | buffer::Buffer, |
| 12 | 9 | hooks::{Command, CommandCallback, CommandSettings}, |
| 13 | 10 | Args, Weechat, |
| 14 | 11 | }; |
| 15 | 12 | |
| 13 | use crate::{PLUGIN_NAME, config::ConfigHandle, MatrixServer, Servers}; | |
| 14 | ||
| 16 | 15 | pub struct MatrixCommand { |
| 17 | 16 | servers: Servers, |
| 18 | 17 | config: ConfigHandle, |
| @@ -250,7 +249,6 @@ impl CommandCallback for MatrixCommand { | ||
| 250 | 249 | |
| 251 | 250 | let argparse = Argparse::new("matrix") |
| 252 | 251 | .about("Matrix chat protocol command.") |
| 253 | // .global_setting(ArgParseSettings::ColorNever) | |
| 254 | 252 | .global_setting(ArgParseSettings::DisableHelpFlags) |
| 255 | 253 | .global_setting(ArgParseSettings::DisableVersion) |
| 256 | 254 | .global_setting(ArgParseSettings::VersionlessSubcommands) |
src/commands/mod.rs +4
| @@ -1,12 +1,15 @@ | ||
| 1 | 1 | use crate::{config::ConfigHandle, Servers}; |
| 2 | 2 | use weechat::hooks::Command; |
| 3 | 3 | |
| 4 | mod devices; | |
| 4 | 5 | mod matrix; |
| 5 | 6 | |
| 7 | use devices::DevicesCommand; | |
| 6 | 8 | use matrix::MatrixCommand; |
| 7 | 9 | |
| 8 | 10 | pub struct Commands { |
| 9 | 11 | _matrix: Command, |
| 12 | _devices: Command, | |
| 10 | 13 | } |
| 11 | 14 | |
| 12 | 15 | impl Commands { |
| @@ -16,6 +19,7 @@ impl Commands { | ||
| 16 | 19 | ) -> Result<Commands, ()> { |
| 17 | 20 | Ok(Commands { |
| 18 | 21 | _matrix: MatrixCommand::create(servers, config)?, |
| 22 | _devices: DevicesCommand::create(servers)?, | |
| 19 | 23 | }) |
| 20 | 24 | } |
| 21 | 25 | } |
src/lib.rs +11 −2
| @@ -19,7 +19,10 @@ use weechat::{ | ||
| 19 | 19 | plugin, Args, Plugin, ReturnCode, Weechat, |
| 20 | 20 | }; |
| 21 | 21 | |
| 22 | use crate::{commands::Commands, config::ConfigHandle, server::MatrixServer, room_buffer::RoomBuffer}; | |
| 22 | use crate::{ | |
| 23 | commands::Commands, config::ConfigHandle, room_buffer::RoomBuffer, | |
| 24 | server::MatrixServer, | |
| 25 | }; | |
| 23 | 26 | |
| 24 | 27 | const PLUGIN_NAME: &str = "matrix"; |
| 25 | 28 | |
| @@ -47,9 +50,15 @@ impl Servers { | ||
| 47 | 50 | let servers = self.borrow(); |
| 48 | 51 | |
| 49 | 52 | for server in servers.values() { |
| 53 | if let Some(b) = &*server.inner().server_buffer() { | |
| 54 | if b.upgrade().map_or(false, |b| &b == buffer) { | |
| 55 | return Some(server.clone()); | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 50 | 59 | for room_buffer in server.inner().room_buffers.values() { |
| 51 | 60 | if buffer == &room_buffer.weechat_buffer() { |
| 52 | return Some(server.clone()) | |
| 61 | return Some(server.clone()); | |
| 53 | 62 | } |
| 54 | 63 | } |
| 55 | 64 | } |
src/room_buffer.rs +4 −4
| @@ -106,10 +106,6 @@ impl BufferInputCallbackAsync for MatrixRoom { | ||
| 106 | 106 | async fn callback(&mut self, buffer: BufferHandle, input: String) { |
| 107 | 107 | let connection = &self.connection; |
| 108 | 108 | |
| 109 | let buffer = buffer | |
| 110 | .upgrade() | |
| 111 | .expect("Running input cb but buffer is closed"); | |
| 112 | ||
| 113 | 109 | if let Some(c) = &*connection.borrow() { |
| 114 | 110 | // TODO check for errors and print them out. |
| 115 | 111 | match c.send_message(&self.room_id, input).await { |
| @@ -117,6 +113,10 @@ impl BufferInputCallbackAsync for MatrixRoom { | ||
| 117 | 113 | Err(_e) => (), |
| 118 | 114 | } |
| 119 | 115 | } else { |
| 116 | let buffer = buffer | |
| 117 | .upgrade() | |
| 118 | .expect("Running input cb but buffer is closed"); | |
| 119 | ||
| 120 | 120 | buffer.print("Error not connected"); |
| 121 | 121 | } |
| 122 | 122 | } |
src/server.rs +102 −3
| @@ -55,6 +55,8 @@ | ||
| 55 | 55 | //! that processing events will not block the Weechat mainloop for too long. |
| 56 | 56 | |
| 57 | 57 | use async_std::sync::{channel as async_channel, Receiver, Sender}; |
| 58 | use chrono::{offset::Utc, DateTime}; | |
| 59 | use indoc::indoc; | |
| 58 | 60 | use std::{ |
| 59 | 61 | cell::{Ref, RefCell, RefMut}, |
| 60 | 62 | collections::HashMap, |
| @@ -72,6 +74,7 @@ use matrix_sdk::api::r0::session::login::Response as LoginResponse; | ||
| 72 | 74 | use matrix_sdk::{ |
| 73 | 75 | self, |
| 74 | 76 | api::r0::{ |
| 77 | device::get_devices::Response as DevicesResponse, | |
| 75 | 78 | message::send_message_event::Response as RoomSendResponse, |
| 76 | 79 | typing::create_typing_event::{Response as TypingResponse, Typing}, |
| 77 | 80 | }, |
| @@ -167,6 +170,20 @@ impl Connection { | ||
| 167 | 170 | Err(e) => panic!("Tokio error while sending a message {:?}", e), |
| 168 | 171 | } |
| 169 | 172 | } |
| 173 | ||
| 174 | pub async fn devices(&self) -> MatrixResult<DevicesResponse> { | |
| 175 | let client = self.client.clone(); | |
| 176 | let handle = self | |
| 177 | .runtime | |
| 178 | .spawn(async move { client.devices().await }) | |
| 179 | .await; | |
| 180 | ||
| 181 | match handle { | |
| 182 | Ok(response) => response, | |
| 183 | Err(e) => panic!("Tokio error while sending a message {:?}", e), | |
| 184 | } | |
| 185 | } | |
| 186 | ||
| 170 | 187 | pub async fn send_typing_notice( |
| 171 | 188 | &self, |
| 172 | 189 | room_id: &RoomId, |
| @@ -257,6 +274,80 @@ impl MatrixServer { | ||
| 257 | 274 | self.inner.borrow() |
| 258 | 275 | } |
| 259 | 276 | |
| 277 | pub fn connection(&self) -> Rc<RefCell<Option<Connection>>> { | |
| 278 | self.inner().connection.clone() | |
| 279 | } | |
| 280 | ||
| 281 | pub async fn devices(&self) { | |
| 282 | let connection = self.connection(); | |
| 283 | ||
| 284 | if let Some(c) = &*connection.borrow() { | |
| 285 | let response = match c.devices().await { | |
| 286 | Ok(r) => r, | |
| 287 | Err(e) => { | |
| 288 | self.print_error(&format!( | |
| 289 | "Error fetching devices {:?}", | |
| 290 | e | |
| 291 | )); | |
| 292 | return; | |
| 293 | } | |
| 294 | }; | |
| 295 | ||
| 296 | if response.devices.is_empty() { | |
| 297 | self.print_error("No devices were found for this server"); | |
| 298 | return; | |
| 299 | } | |
| 300 | ||
| 301 | self.print_network(&format!( | |
| 302 | indoc!( | |
| 303 | " | |
| 304 | Devices for server {}{}{}: | |
| 305 | Device ID Device name Last seen | |
| 306 | " | |
| 307 | ), | |
| 308 | Weechat::color("chat_server"), | |
| 309 | self.name(), | |
| 310 | Weechat::color("reset") | |
| 311 | )); | |
| 312 | ||
| 313 | let lines: Vec<String> = response | |
| 314 | .devices | |
| 315 | .iter() | |
| 316 | .map(|d| { | |
| 317 | let device_color = Weechat::info_get( | |
| 318 | "nick_color_name", | |
| 319 | d.device_id.as_str(), | |
| 320 | ) | |
| 321 | .expect("Can't get device color"); | |
| 322 | ||
| 323 | let last_seen_date = | |
| 324 | d.last_seen_ts.map_or("?".to_owned(), |d| { | |
| 325 | let date: DateTime<Utc> = d.into(); | |
| 326 | date.format("%Y/%m/%d %H:%M").to_string() | |
| 327 | }); | |
| 328 | ||
| 329 | let last_seen = format!( | |
| 330 | "{} @ {}", | |
| 331 | d.last_seen_ip.as_deref().unwrap_or("-"), | |
| 332 | last_seen_date | |
| 333 | ); | |
| 334 | ||
| 335 | format!( | |
| 336 | " {}{:<15}{}{:<30}{:<}", | |
| 337 | Weechat::color(&device_color), | |
| 338 | d.device_id.as_str(), | |
| 339 | Weechat::color("reset"), | |
| 340 | d.display_name.as_deref().unwrap_or(""), | |
| 341 | last_seen, | |
| 342 | ) | |
| 343 | }) | |
| 344 | .collect(); | |
| 345 | ||
| 346 | let line = lines.join("\n"); | |
| 347 | self.print_network(&line); | |
| 348 | }; | |
| 349 | } | |
| 350 | ||
| 260 | 351 | /// Parse an URL returning a None if the string is empty. |
| 261 | 352 | /// |
| 262 | 353 | /// # Panics |
| @@ -868,7 +959,12 @@ impl InnerServer { | ||
| 868 | 959 | buffer_handle |
| 869 | 960 | } |
| 870 | 961 | |
| 871 | pub fn server_buffer<'a>( | |
| 962 | /// Borrow the server buffer handle. | |
| 963 | pub fn server_buffer(&self) -> Ref<Option<BufferHandle>> { | |
| 964 | self.server_buffer.borrow() | |
| 965 | } | |
| 966 | ||
| 967 | fn get_or_create_buffer<'a>( | |
| 872 | 968 | &self, |
| 873 | 969 | server_buffer: &'a mut RefMut<Option<BufferHandle>>, |
| 874 | 970 | ) -> &'a BufferHandle { |
| @@ -888,13 +984,16 @@ impl InnerServer { | ||
| 888 | 984 | /// Print a neutral message to the server buffer. |
| 889 | 985 | pub fn print(&self, message: &str) { |
| 890 | 986 | let mut server_buffer = self.server_buffer.borrow_mut(); |
| 891 | let buffer = self.server_buffer(&mut server_buffer).upgrade().unwrap(); | |
| 987 | let buffer = self | |
| 988 | .get_or_create_buffer(&mut server_buffer) | |
| 989 | .upgrade() | |
| 990 | .unwrap(); | |
| 892 | 991 | buffer.print(message); |
| 893 | 992 | } |
| 894 | 993 | |
| 895 | 994 | /// Print a message with a given prefix to the server buffer. |
| 896 | 995 | pub fn print_with_prefix(&self, prefix: &str, message: &str) { |
| 897 | self.print(&format!("{}\t{}", prefix, message)); | |
| 996 | self.print(&format!("{}{}", prefix, message)); | |
| 898 | 997 | } |
| 899 | 998 | |
| 900 | 999 | /// Print an network message to the server buffer. |