Commit 6aa326c390

6aa326c3909b82286ff0c5877e58f35272e2f782

parent: 071dd1ec35

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-09-05 16:15 UTC

weechat-matrix: Add a /devices command.

Layout: unified · split

Cargo.lock +31 −14
@@ -743,6 +743,14 @@ dependencies = [
743 "hashbrown 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", 743 "hashbrown 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
744] 744]
745 745
746[[package]]
747name = "indoc"
748version = "1.0.2"
749source = "registry+https://github.com/rust-lang/crates.io-index"
750dependencies = [
751 "unindent 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
752]
753
746[[package]] 754[[package]]
747name = "instant" 755name = "instant"
748version = "0.1.6" 756version = "0.1.6"
@@ -868,7 +876,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
868[[package]] 876[[package]]
869name = "matrix-sdk" 877name = "matrix-sdk"
870version = "0.1.0" 878version = "0.1.0"
871source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" 879source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62"
872dependencies = [ 880dependencies = [
873 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 881 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
874 "dashmap 3.11.10 (registry+https://github.com/rust-lang/crates.io-index)", 882 "dashmap 3.11.10 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -888,7 +896,7 @@ dependencies = [
888[[package]] 896[[package]]
889name = "matrix-sdk-base" 897name = "matrix-sdk-base"
890version = "0.1.0" 898version = "0.1.0"
891source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" 899source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62"
892dependencies = [ 900dependencies = [
893 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 901 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
894 "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk/)", 902 "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk/)",
@@ -905,7 +913,7 @@ dependencies = [
905[[package]] 913[[package]]
906name = "matrix-sdk-common" 914name = "matrix-sdk-common"
907version = "0.1.0" 915version = "0.1.0"
908source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" 916source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62"
909dependencies = [ 917dependencies = [
910 "assign 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 918 "assign 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
911 "futures-locks 0.5.1-pre (git+https://github.com/asomers/futures-locks)", 919 "futures-locks 0.5.1-pre (git+https://github.com/asomers/futures-locks)",
@@ -919,7 +927,7 @@ dependencies = [
919[[package]] 927[[package]]
920name = "matrix-sdk-common-macros" 928name = "matrix-sdk-common-macros"
921version = "0.1.0" 929version = "0.1.0"
922source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" 930source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62"
923dependencies = [ 931dependencies = [
924 "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", 932 "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
925 "syn 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", 933 "syn 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -928,7 +936,7 @@ dependencies = [
928[[package]] 936[[package]]
929name = "matrix-sdk-crypto" 937name = "matrix-sdk-crypto"
930version = "0.1.0" 938version = "0.1.0"
931source = "git+https://github.com/matrix-org/matrix-rust-sdk/#89efcee337443cd446e99f40b9ccf69edb16715e" 939source = "git+https://github.com/matrix-org/matrix-rust-sdk/#6c7dbb814bcec79b9b3292ac2743adf355d41c62"
932dependencies = [ 940dependencies = [
933 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 941 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
934 "atomic 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 942 "atomic 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1981,6 +1989,11 @@ name = "unicode-xid"
1981version = "0.2.1" 1989version = "0.2.1"
1982source = "registry+https://github.com/rust-lang/crates.io-index" 1990source = "registry+https://github.com/rust-lang/crates.io-index"
1983 1991
1992[[package]]
1993name = "unindent"
1994version = "0.1.6"
1995source = "registry+https://github.com/rust-lang/crates.io-index"
1996
1984[[package]] 1997[[package]]
1985name = "url" 1998name = "url"
1986version = "2.1.1" 1999version = "2.1.1"
@@ -2122,7 +2135,7 @@ dependencies = [
2122[[package]] 2135[[package]]
2123name = "weechat" 2136name = "weechat"
2124version = "0.3.1" 2137version = "0.3.1"
2125source = "registry+https://github.com/rust-lang/crates.io-index" 2138source = "git+https://github.com/poljar/rust-weechat#dc47d110249ca823f44c835c3a210fd8a048220e"
2126dependencies = [ 2139dependencies = [
2127 "async-task 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 2140 "async-task 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
2128 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 2141 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2132,14 +2145,14 @@ dependencies = [
2132 "paste 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 2145 "paste 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
2133 "pipe-channel 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 2146 "pipe-channel 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
2134 "strum 0.19.2 (registry+https://github.com/rust-lang/crates.io-index)", 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)", 2148 "weechat-macro 0.3.1 (git+https://github.com/poljar/rust-weechat)",
2136 "weechat-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 2149 "weechat-sys 0.3.1 (git+https://github.com/poljar/rust-weechat)",
2137] 2150]
2138 2151
2139[[package]] 2152[[package]]
2140name = "weechat-macro" 2153name = "weechat-macro"
2141version = "0.3.1" 2154version = "0.3.1"
2142source = "registry+https://github.com/rust-lang/crates.io-index" 2155source = "git+https://github.com/poljar/rust-weechat#dc47d110249ca823f44c835c3a210fd8a048220e"
2143dependencies = [ 2156dependencies = [
2144 "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)", 2157 "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)",
2145 "proc-macro2 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)", 2158 "proc-macro2 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2153,20 +2166,22 @@ version = "0.1.0"
2153dependencies = [ 2166dependencies = [
2154 "async-std 1.6.3 (registry+https://github.com/rust-lang/crates.io-index)", 2167 "async-std 1.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
2155 "async-trait 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", 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 "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)", 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 "matrix-sdk 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk/)", 2172 "matrix-sdk 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk/)",
2158 "tokio 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", 2173 "tokio 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
2159 "tracing 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", 2174 "tracing 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)",
2160 "tracing-subscriber 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", 2175 "tracing-subscriber 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
2161 "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 2176 "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
2162 "uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", 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[[package]] 2181[[package]]
2167name = "weechat-sys" 2182name = "weechat-sys"
2168version = "0.3.1" 2183version = "0.3.1"
2169source = "registry+https://github.com/rust-lang/crates.io-index" 2184source = "git+https://github.com/poljar/rust-weechat#dc47d110249ca823f44c835c3a210fd8a048220e"
2170dependencies = [ 2185dependencies = [
2171 "bindgen 0.55.1 (registry+https://github.com/rust-lang/crates.io-index)", 2186 "bindgen 0.55.1 (registry+https://github.com/rust-lang/crates.io-index)",
2172 "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)", 2187 "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2344,6 +2359,7 @@ dependencies = [
2344"checksum hyper-tls 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed" 2359"checksum hyper-tls 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d979acc56dcb5b8dddba3917601745e877576475aa046df3226eabdecef78eed"
2345"checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" 2360"checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9"
2346"checksum indexmap 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4e47a3566dd4fd4eec714ae6ceabdee0caec795be835c223d92c2d40f1e8cf1c" 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"checksum instant 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485" 2363"checksum instant 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485"
2348"checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" 2364"checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
2349"checksum ipnet 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135" 2365"checksum ipnet 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135"
@@ -2478,6 +2494,7 @@ dependencies = [
2478"checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" 2494"checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
2479"checksum unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" 2495"checksum unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
2480"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 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"checksum url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" 2498"checksum url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb"
2482"checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" 2499"checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11"
2483"checksum vcpkg 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" 2500"checksum vcpkg 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c"
@@ -2496,9 +2513,9 @@ dependencies = [
2496"checksum wasm-bindgen-macro-support 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "841a6d1c35c6f596ccea1f82504a192a60378f64b3bb0261904ad8f2f5657556" 2513"checksum wasm-bindgen-macro-support 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "841a6d1c35c6f596ccea1f82504a192a60378f64b3bb0261904ad8f2f5657556"
2497"checksum wasm-bindgen-shared 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "93b162580e34310e5931c4b792560108b10fd14d64915d7fff8ff00180e70092" 2514"checksum wasm-bindgen-shared 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "93b162580e34310e5931c4b792560108b10fd14d64915d7fff8ff00180e70092"
2498"checksum web-sys 0.3.44 (registry+https://github.com/rust-lang/crates.io-index)" = "dda38f4e5ca63eda02c059d243aa25b5f35ab98451e518c51612cd0f1bd19a47" 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" 2516"checksum weechat 0.3.1 (git+https://github.com/poljar/rust-weechat)" = "<none>"
2500"checksum weechat-macro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a939b44ce807e5e0dfffdd5df9535e890cc85de95f5c40bd5871575d61397f46" 2517"checksum weechat-macro 0.3.1 (git+https://github.com/poljar/rust-weechat)" = "<none>"
2501"checksum weechat-sys 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "432d9ec63b71ca962fa9df527d5eabd175bf1b484960fc090e341116fb4db133" 2518"checksum weechat-sys 0.3.1 (git+https://github.com/poljar/rust-weechat)" = "<none>"
2502"checksum wepoll-sys-stjepang 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd319e971980166b53e17b1026812ad66c6b54063be879eb182342b55284694" 2519"checksum wepoll-sys-stjepang 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd319e971980166b53e17b1026812ad66c6b54063be879eb182342b55284694"
2503"checksum which 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" 2520"checksum which 3.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
2504"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 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" ] }
18clap = "2.33.3" 18clap = "2.33.3"
19async-trait = "0.1.40" 19async-trait = "0.1.40"
20uuid = { version = "0.8.1", features = ["v4"] } 20uuid = { version = "0.8.1", features = ["v4"] }
21indoc = "1.0.2"
22chrono = "0.4.15"
21 23
22[dependencies.weechat] 24[dependencies.weechat]
23version = "0.3.1" 25git = "https://github.com/poljar/rust-weechat"
24features = ["async", "config_macro"] 26features = ["async", "config_macro"]
25 27
26[dependencies.matrix-sdk] 28[dependencies.matrix-sdk]
Makefile +3 −1
@@ -1,9 +1,11 @@
1WEECHAT_HOME ?= $(HOME)/.weechat 1WEECHAT_HOME ?= $(HOME)/.weechat
2PREFIX ?= $(WEECHAT_HOME) 2PREFIX ?= $(WEECHAT_HOME)
3 3
4SOURCES := $(wildcard src/*.rs src/commands/*.rs)
5
4.PHONY: install install-dir lint 6.PHONY: install install-dir lint
5 7
6target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/commands/mod.rs src/config.rs 8target/debug/libmatrix.so: $(SOURCES)
7 cargo build 9 cargo build
8 10
9install: install-dir target/debug/libmatrix.so 11install: install-dir target/debug/libmatrix.so
src/commands/devices.rs added +105
@@ -0,0 +1,105 @@
1use clap::{
2 App as Argparse, AppSettings as ArgParseSettings, Arg,
3 SubCommand,
4};
5
6use weechat::{
7 buffer::Buffer,
8 hooks::{Command, CommandCallback, CommandSettings},
9 Args, Weechat,
10};
11
12use crate::Servers;
13
14pub struct DevicesCommand {
15 servers: Servers,
16}
17
18impl 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
57impl 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};
5use url::Url; 5use url::Url;
6 6
7use crate::PLUGIN_NAME;
8
9use crate::{config::ConfigHandle, MatrixServer, Servers};
10use weechat::{ 7use weechat::{
11 buffer::Buffer, 8 buffer::Buffer,
12 hooks::{Command, CommandCallback, CommandSettings}, 9 hooks::{Command, CommandCallback, CommandSettings},
13 Args, Weechat, 10 Args, Weechat,
14}; 11};
15 12
13use crate::{PLUGIN_NAME, config::ConfigHandle, MatrixServer, Servers};
14
16pub struct MatrixCommand { 15pub struct MatrixCommand {
17 servers: Servers, 16 servers: Servers,
18 config: ConfigHandle, 17 config: ConfigHandle,
@@ -250,7 +249,6 @@ impl CommandCallback for MatrixCommand {
250 249
251 let argparse = Argparse::new("matrix") 250 let argparse = Argparse::new("matrix")
252 .about("Matrix chat protocol command.") 251 .about("Matrix chat protocol command.")
253 // .global_setting(ArgParseSettings::ColorNever)
254 .global_setting(ArgParseSettings::DisableHelpFlags) 252 .global_setting(ArgParseSettings::DisableHelpFlags)
255 .global_setting(ArgParseSettings::DisableVersion) 253 .global_setting(ArgParseSettings::DisableVersion)
256 .global_setting(ArgParseSettings::VersionlessSubcommands) 254 .global_setting(ArgParseSettings::VersionlessSubcommands)
src/commands/mod.rs +4
@@ -1,12 +1,15 @@
1use crate::{config::ConfigHandle, Servers}; 1use crate::{config::ConfigHandle, Servers};
2use weechat::hooks::Command; 2use weechat::hooks::Command;
3 3
4mod devices;
4mod matrix; 5mod matrix;
5 6
7use devices::DevicesCommand;
6use matrix::MatrixCommand; 8use matrix::MatrixCommand;
7 9
8pub struct Commands { 10pub struct Commands {
9 _matrix: Command, 11 _matrix: Command,
12 _devices: Command,
10} 13}
11 14
12impl Commands { 15impl Commands {
@@ -16,6 +19,7 @@ impl Commands {
16 ) -> Result<Commands, ()> { 19 ) -> Result<Commands, ()> {
17 Ok(Commands { 20 Ok(Commands {
18 _matrix: MatrixCommand::create(servers, config)?, 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 plugin, Args, Plugin, ReturnCode, Weechat, 19 plugin, Args, Plugin, ReturnCode, Weechat,
20}; 20};
21 21
22use crate::{commands::Commands, config::ConfigHandle, server::MatrixServer, room_buffer::RoomBuffer}; 22use crate::{
23 commands::Commands, config::ConfigHandle, room_buffer::RoomBuffer,
24 server::MatrixServer,
25};
23 26
24const PLUGIN_NAME: &str = "matrix"; 27const PLUGIN_NAME: &str = "matrix";
25 28
@@ -47,9 +50,15 @@ impl Servers {
47 let servers = self.borrow(); 50 let servers = self.borrow();
48 51
49 for server in servers.values() { 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 for room_buffer in server.inner().room_buffers.values() { 59 for room_buffer in server.inner().room_buffers.values() {
51 if buffer == &room_buffer.weechat_buffer() { 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 async fn callback(&mut self, buffer: BufferHandle, input: String) { 106 async fn callback(&mut self, buffer: BufferHandle, input: String) {
107 let connection = &self.connection; 107 let connection = &self.connection;
108 108
109 let buffer = buffer
110 .upgrade()
111 .expect("Running input cb but buffer is closed");
112
113 if let Some(c) = &*connection.borrow() { 109 if let Some(c) = &*connection.borrow() {
114 // TODO check for errors and print them out. 110 // TODO check for errors and print them out.
115 match c.send_message(&self.room_id, input).await { 111 match c.send_message(&self.room_id, input).await {
@@ -117,6 +113,10 @@ impl BufferInputCallbackAsync for MatrixRoom {
117 Err(_e) => (), 113 Err(_e) => (),
118 } 114 }
119 } else { 115 } else {
116 let buffer = buffer
117 .upgrade()
118 .expect("Running input cb but buffer is closed");
119
120 buffer.print("Error not connected"); 120 buffer.print("Error not connected");
121 } 121 }
122 } 122 }
src/server.rs +102 −3
@@ -55,6 +55,8 @@
55//! that processing events will not block the Weechat mainloop for too long. 55//! that processing events will not block the Weechat mainloop for too long.
56 56
57use async_std::sync::{channel as async_channel, Receiver, Sender}; 57use async_std::sync::{channel as async_channel, Receiver, Sender};
58use chrono::{offset::Utc, DateTime};
59use indoc::indoc;
58use std::{ 60use std::{
59 cell::{Ref, RefCell, RefMut}, 61 cell::{Ref, RefCell, RefMut},
60 collections::HashMap, 62 collections::HashMap,
@@ -72,6 +74,7 @@ use matrix_sdk::api::r0::session::login::Response as LoginResponse;
72use matrix_sdk::{ 74use matrix_sdk::{
73 self, 75 self,
74 api::r0::{ 76 api::r0::{
77 device::get_devices::Response as DevicesResponse,
75 message::send_message_event::Response as RoomSendResponse, 78 message::send_message_event::Response as RoomSendResponse,
76 typing::create_typing_event::{Response as TypingResponse, Typing}, 79 typing::create_typing_event::{Response as TypingResponse, Typing},
77 }, 80 },
@@ -167,6 +170,20 @@ impl Connection {
167 Err(e) => panic!("Tokio error while sending a message {:?}", e), 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 pub async fn send_typing_notice( 187 pub async fn send_typing_notice(
171 &self, 188 &self,
172 room_id: &RoomId, 189 room_id: &RoomId,
@@ -257,6 +274,80 @@ impl MatrixServer {
257 self.inner.borrow() 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 /// Parse an URL returning a None if the string is empty. 351 /// Parse an URL returning a None if the string is empty.
261 /// 352 ///
262 /// # Panics 353 /// # Panics
@@ -868,7 +959,12 @@ impl InnerServer {
868 buffer_handle 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 &self, 968 &self,
873 server_buffer: &'a mut RefMut<Option<BufferHandle>>, 969 server_buffer: &'a mut RefMut<Option<BufferHandle>>,
874 ) -> &'a BufferHandle { 970 ) -> &'a BufferHandle {
@@ -888,13 +984,16 @@ impl InnerServer {
888 /// Print a neutral message to the server buffer. 984 /// Print a neutral message to the server buffer.
889 pub fn print(&self, message: &str) { 985 pub fn print(&self, message: &str) {
890 let mut server_buffer = self.server_buffer.borrow_mut(); 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 buffer.print(message); 991 buffer.print(message);
893 } 992 }
894 993
895 /// Print a message with a given prefix to the server buffer. 994 /// Print a message with a given prefix to the server buffer.
896 pub fn print_with_prefix(&self, prefix: &str, message: &str) { 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 /// Print an network message to the server buffer. 999 /// Print an network message to the server buffer.