From ddbdde5ae6fb3d1de0e139d36167dcfd3572567e Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 16 Mar 2024 14:04:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0=20matrix?= =?UTF-8?q?=20bot=20=E8=BF=98=E6=B2=A1=E5=AE=9E=E7=8E=B0=20Python=20?= =?UTF-8?q?=E4=BE=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 681 +++--------------- ica-rs/Cargo.toml | 4 +- ica-rs/src/config.rs | 11 +- ica-rs/src/data_struct/ica/messages.rs | 2 +- .../src/data_struct/ica/messages/msg_trait.rs | 2 +- ica-rs/src/error.rs | 61 ++ ica-rs/src/ica.rs | 52 +- ica-rs/src/main.rs | 48 +- ica-rs/src/matrix.rs | 151 ++++ ica-rs/src/matrix/events.rs | 1 + 10 files changed, 398 insertions(+), 615 deletions(-) create mode 100644 ica-rs/src/matrix/events.rs diff --git a/Cargo.lock b/Cargo.lock index 5a90133..6c6fea6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,18 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "accessory" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "850bb534b9dc04744fbbb71d30ad6d25a7e4cf6dc33e223c81ef3a92ebab4e0b" -dependencies = [ - "macroific", - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "addr2line" version = "0.21.0" @@ -45,29 +33,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "aes" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" -dependencies = [ - "cfg-if", - "cipher", - "cpufeatures", -] - -[[package]] -name = "ahash" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" -dependencies = [ - "cfg-if", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "aho-corasick" version = "1.1.2" @@ -77,12 +42,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "allocator-api2" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" - [[package]] name = "android-tzdata" version = "0.1.1" @@ -120,7 +79,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21cc1548309245035eb18aa7f0967da6bc65587005170c56e6ef2788a4cf3f4e" dependencies = [ "include_dir", - "itertools 0.10.5", + "itertools", "proc-macro-error", "proc-macro2", "quote", @@ -138,9 +97,6 @@ name = "arrayvec" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" -dependencies = [ - "serde", -] [[package]] name = "as_variant" @@ -287,24 +243,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "block-padding" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" -dependencies = [ - "generic-array", -] - -[[package]] -name = "bs58" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5353f36341f7451062466f0b755b96ac3a9547e4d7f6b70d603fc721a7d7896" -dependencies = [ - "tinyvec", -] - [[package]] name = "bumpalo" version = "3.15.4" @@ -329,15 +267,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e368af43e418a04d52505cf3dbc23dda4e3407ae2fa99fd0e4f308ce546acc" -[[package]] -name = "cbc" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" -dependencies = [ - "cipher", -] - [[package]] name = "cc" version = "1.0.90" @@ -490,15 +419,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "ctr" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" -dependencies = [ - "cipher", -] - [[package]] name = "curve25519-dalek" version = "4.1.2" @@ -512,7 +432,6 @@ dependencies = [ "fiat-crypto", "platforms", "rustc_version", - "serde", "subtle", "zeroize", ] @@ -534,59 +453,6 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" -[[package]] -name = "deadpool" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb84100978c1c7b37f09ed3ce3e5f843af02c2a2c431bae5b19230dad2c1b490" -dependencies = [ - "async-trait", - "deadpool-runtime", - "num_cpus", - "tokio", -] - -[[package]] -name = "deadpool-runtime" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63dfa964fe2a66f3fde91fc70b267fe193d822c7e603e2a675a49a7f46ad3f49" -dependencies = [ - "tokio", -] - -[[package]] -name = "deadpool-sqlite" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8010e36e12f3be22543a5e478b4af20aeead9a700dd69581a5e050a070fc22c" -dependencies = [ - "deadpool", - "deadpool-sync", - "rusqlite", -] - -[[package]] -name = "deadpool-sync" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8db70494c13cae4ce67b4b4dafdaf828cf0df7237ab5b9e2fcabee4965d0a0a" -dependencies = [ - "deadpool-runtime", -] - -[[package]] -name = "delegate-display" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98a85201f233142ac819bbf6226e36d0b5e129a47bd325084674261c82d4cd66" -dependencies = [ - "macroific", - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "der" version = "0.7.8" @@ -594,22 +460,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fffa369a668c8af7dbf8b5e56c9f744fbd399949ed171606040001947de40b1c" dependencies = [ "const-oid", - "der_derive", - "flagset", "zeroize", ] -[[package]] -name = "der_derive" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fe87ce4529967e0ba1dcf8450bab64d97dfd5010a6256187ffe2e43e6f0e049" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "deranged" version = "0.3.11" @@ -648,7 +501,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" dependencies = [ "pkcs8", - "serde", "signature", ] @@ -660,7 +512,6 @@ checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ "curve25519-dalek", "ed25519", - "rand_core", "serde", "sha2", "subtle", @@ -754,30 +605,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "fallible-iterator" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" - -[[package]] -name = "fallible-streaming-iterator" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" - -[[package]] -name = "fancy_constructor" -version = "1.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f71f317e4af73b2f8f608fac190c52eac4b1879d2145df1db2fe48881ca69435" -dependencies = [ - "macroific", - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "fastrand" version = "2.0.1" @@ -790,12 +617,6 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1676f435fc1dadde4d03e43f5d62b259e1ce5f40bd4ffb21db2b42ebe59c1382" -[[package]] -name = "flagset" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdeb3aa5e95cf9aabc17f060cfa0ced7b83f042390760ca53bf09df9968acaa1" - [[package]] name = "fnv" version = "1.0.7" @@ -905,10 +726,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ "cfg-if", - "js-sys", "libc", "wasi", - "wasm-bindgen", ] [[package]] @@ -929,19 +748,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "gloo-utils" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b5555354113b18c547c1d3a98fbf7fb32a9ff4f6fa112ce823a21641a0ba3aa" -dependencies = [ - "js-sys", - "serde", - "serde_json", - "wasm-bindgen", - "web-sys", -] - [[package]] name = "h2" version = "0.3.25" @@ -966,19 +772,6 @@ name = "hashbrown" version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" -dependencies = [ - "ahash", - "allocator-api2", -] - -[[package]] -name = "hashlink" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" -dependencies = [ - "hashbrown", -] [[package]] name = "heck" @@ -998,15 +791,6 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" -[[package]] -name = "hkdf" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" -dependencies = [ - "hmac", -] - [[package]] name = "hmac" version = "0.12.1" @@ -1074,6 +858,20 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-rustls" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" +dependencies = [ + "futures-util", + "http", + "hyper", + "rustls", + "tokio", + "tokio-rustls", +] + [[package]] name = "hyper-tls" version = "0.5.0" @@ -1185,23 +983,6 @@ dependencies = [ "quote", ] -[[package]] -name = "indexed_db_futures" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6cc2083760572ee02385ab8b7c02c20925d2dd1f97a1a25a8737a238608f1152" -dependencies = [ - "accessory", - "cfg-if", - "delegate-display", - "fancy_constructor", - "js-sys", - "uuid", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", -] - [[package]] name = "indexmap" version = "2.2.5" @@ -1225,7 +1006,6 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" dependencies = [ - "block-padding", "generic-array", ] @@ -1236,9 +1016,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", ] [[package]] @@ -1256,24 +1033,6 @@ dependencies = [ "either", ] -[[package]] -name = "itertools" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" -dependencies = [ - "either", -] - -[[package]] -name = "itertools" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" -dependencies = [ - "either", -] - [[package]] name = "itoa" version = "1.0.10" @@ -1339,16 +1098,6 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" -[[package]] -name = "libsqlite3-sys" -version = "0.27.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4e226dcd58b4be396f7bd3c20da8fdee2911400705297ba7d2d7cc2c30f716" -dependencies = [ - "pkg-config", - "vcpkg", -] - [[package]] name = "linux-raw-sys" version = "0.4.13" @@ -1371,82 +1120,12 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" -[[package]] -name = "macroific" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f05c00ac596022625d01047c421a0d97d7f09a18e429187b341c201cb631b9dd" -dependencies = [ - "macroific_attr_parse", - "macroific_core", - "macroific_macro", -] - -[[package]] -name = "macroific_attr_parse" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd94d5da95b30ae6e10621ad02340909346ad91661f3f8c0f2b62345e46a2f67" -dependencies = [ - "cfg-if", - "proc-macro2", - "quote", - "syn 2.0.52", -] - -[[package]] -name = "macroific_core" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13198c120864097a565ccb3ff947672d969932b7975ebd4085732c9f09435e55" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.52", -] - -[[package]] -name = "macroific_macro" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c9853143cbed7f1e41dc39fee95f9b361bec65c8dc2a01bf609be01b61f5ae" -dependencies = [ - "macroific_attr_parse", - "macroific_core", - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "maplit" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" -[[package]] -name = "matrix-pickle" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7fd26463ce5d86b8d9bb9c4142d453198ba22fb91bd46d3c9f144ae699d821d" -dependencies = [ - "matrix-pickle-derive", - "thiserror", -] - -[[package]] -name = "matrix-pickle-derive" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93779aa78d39c2fe34746287b10a866192cf8af1b81767fff76bd64099acc0f5" -dependencies = [ - "proc-macro-crate 2.0.0", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "matrix-sdk" version = "0.7.1" @@ -1474,8 +1153,6 @@ dependencies = [ "indexmap", "matrix-sdk-base", "matrix-sdk-common", - "matrix-sdk-indexeddb", - "matrix-sdk-sqlite", "mime", "mime2ext", "reqwest", @@ -1507,7 +1184,6 @@ dependencies = [ "eyeball-im", "futures-util", "matrix-sdk-common", - "matrix-sdk-crypto", "matrix-sdk-store-encryption", "once_cell", "ruma", @@ -1537,98 +1213,9 @@ dependencies = [ "tracing", "tracing-subscriber", "wasm-bindgen", - "wasm-bindgen-futures", "web-sys", ] -[[package]] -name = "matrix-sdk-crypto" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72aaeca3deb1387a63cd8c689270bd499b9eac3a594c2aaec72d7441ff00cd09" -dependencies = [ - "aes", - "as_variant", - "async-trait", - "bs58", - "byteorder", - "cbc", - "cfg-if", - "ctr", - "eyeball", - "futures-core", - "futures-util", - "hkdf", - "hmac", - "itertools 0.12.1", - "matrix-sdk-common", - "pbkdf2", - "rand", - "rmp-serde", - "ruma", - "serde", - "serde_json", - "sha2", - "subtle", - "thiserror", - "tokio", - "tokio-stream", - "tracing", - "ulid", - "vodozemac", - "zeroize", -] - -[[package]] -name = "matrix-sdk-indexeddb" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad388005c5d4ed2ff38f405d52aa7fa606f4e1ab51baf5f2504721124ed4a58b" -dependencies = [ - "anyhow", - "async-trait", - "base64", - "getrandom", - "gloo-utils", - "indexed_db_futures", - "js-sys", - "matrix-sdk-base", - "matrix-sdk-crypto", - "matrix-sdk-store-encryption", - "ruma", - "serde", - "serde-wasm-bindgen", - "serde_json", - "thiserror", - "tokio", - "tracing", - "wasm-bindgen", - "web-sys", -] - -[[package]] -name = "matrix-sdk-sqlite" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20bd36bc5fa7ecd93516b242ba27466196d52b4a8743d85dd883a67bd6db11dc" -dependencies = [ - "async-trait", - "deadpool-sqlite", - "itertools 0.12.1", - "matrix-sdk-base", - "matrix-sdk-crypto", - "matrix-sdk-store-encryption", - "rmp-serde", - "ruma", - "rusqlite", - "serde", - "serde_json", - "thiserror", - "tokio", - "tracing", - "vodozemac", -] - [[package]] name = "matrix-sdk-store-encryption" version = "0.7.0" @@ -1638,7 +1225,6 @@ dependencies = [ "blake3", "chacha20poly1305", "displaydoc", - "getrandom", "hmac", "pbkdf2", "rand", @@ -1884,17 +1470,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "pkcs7" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d79178be066405e0602bf3035946edef6b11b3f9dde46dfe5f8bfd7dea4b77e7" -dependencies = [ - "der", - "spki", - "x509-cert", -] - [[package]] name = "pkcs8" version = "0.10.2" @@ -1998,29 +1573,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "prost" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" -dependencies = [ - "bytes", - "prost-derive", -] - -[[package]] -name = "prost-derive" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" -dependencies = [ - "anyhow", - "itertools 0.11.0", - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "pyo3" version = "0.20.3" @@ -2191,6 +1743,7 @@ dependencies = [ "http", "http-body", "hyper", + "hyper-rustls", "hyper-tls", "ipnet", "js-sys", @@ -2200,6 +1753,7 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", + "rustls", "rustls-pemfile", "serde", "serde_json", @@ -2208,6 +1762,7 @@ dependencies = [ "system-configuration", "tokio", "tokio-native-tls", + "tokio-rustls", "tokio-util", "tower-service", "url", @@ -2215,9 +1770,25 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams", "web-sys", + "webpki-roots", "winreg", ] +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.52.0", +] + [[package]] name = "rmp" version = "0.8.12" @@ -2284,10 +1855,8 @@ dependencies = [ "base64", "bytes", "form_urlencoded", - "getrandom", "http", "indexmap", - "js-sys", "js_int", "konst", "percent-encoding", @@ -2367,20 +1936,6 @@ dependencies = [ "toml", ] -[[package]] -name = "rusqlite" -version = "0.30.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a78046161564f5e7cd9008aff3b2990b3850dc8e0349119b98e8f251e099f24d" -dependencies = [ - "bitflags 2.4.2", - "fallible-iterator", - "fallible-streaming-iterator", - "hashlink", - "libsqlite3-sys", - "smallvec", -] - [[package]] name = "rust_engineio" version = "0.4.4" @@ -2454,6 +2009,18 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustls" +version = "0.21.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba" +dependencies = [ + "log", + "ring", + "rustls-webpki", + "sct", +] + [[package]] name = "rustls-pemfile" version = "1.0.4" @@ -2463,6 +2030,16 @@ dependencies = [ "base64", ] +[[package]] +name = "rustls-webpki" +version = "0.101.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "ryu" version = "1.0.17" @@ -2484,6 +2061,16 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "sct" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "security-framework" version = "2.9.2" @@ -2522,26 +2109,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-wasm-bindgen" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8302e169f0eddcc139c70f139d19d6467353af16f9fce27e8c30158036a1e16b" -dependencies = [ - "js-sys", - "serde", - "wasm-bindgen", -] - -[[package]] -name = "serde_bytes" -version = "0.11.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b8497c313fd43ab992087548117643f6fcd935cbf36f176ffda0aacf9591734" -dependencies = [ - "serde", -] - [[package]] name = "serde_derive" version = "1.0.197" @@ -2672,6 +2239,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + [[package]] name = "spki" version = "0.7.3" @@ -2871,6 +2444,16 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-rustls" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" +dependencies = [ + "rustls", + "tokio", +] + [[package]] name = "tokio-stream" version = "0.1.15" @@ -3078,17 +2661,6 @@ version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e36a83ea2b3c704935a01b4642946aadd445cea40b10935e3f8bd8052b8193d6" -[[package]] -name = "ulid" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34778c17965aa2a08913b57e1f34db9b4a63f5de31768b55bf20d2795f921259" -dependencies = [ - "getrandom", - "rand", - "web-time", -] - [[package]] name = "unicode-bidi" version = "0.3.15" @@ -3126,6 +2698,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "url" version = "2.5.0" @@ -3156,7 +2734,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" dependencies = [ "getrandom", - "wasm-bindgen", ] [[package]] @@ -3177,35 +2754,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "vodozemac" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2790dffeecc522299d72d9a855c43adb0c23ba1dc1112d79a651fdf3beb2a356" -dependencies = [ - "aes", - "arrayvec", - "base64", - "cbc", - "curve25519-dalek", - "ed25519-dalek", - "getrandom", - "hkdf", - "hmac", - "matrix-pickle", - "pkcs7", - "prost", - "rand", - "serde", - "serde_bytes", - "serde_json", - "sha2", - "subtle", - "thiserror", - "x25519-dalek", - "zeroize", -] - [[package]] name = "want" version = "0.3.1" @@ -3311,14 +2859,10 @@ dependencies = [ ] [[package]] -name = "web-time" -version = "1.1.0" +name = "webpki-roots" +version = "0.25.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" -dependencies = [ - "js-sys", - "wasm-bindgen", -] +checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" [[package]] name = "wildmatch" @@ -3517,49 +3061,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "x25519-dalek" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7e468321c81fb07fa7f4c636c3972b9100f0346e5b6a9f2bd0603a52f7ed277" -dependencies = [ - "curve25519-dalek", - "rand_core", - "serde", - "zeroize", -] - -[[package]] -name = "x509-cert" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1301e935010a701ae5f8655edc0ad17c44bad3ac5ce8c39185f75453b720ae94" -dependencies = [ - "const-oid", - "der", - "spki", -] - -[[package]] -name = "zerocopy" -version = "0.7.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.7.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.52", -] - [[package]] name = "zeroize" version = "1.7.0" diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index c6fa6c9..f16fb44 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["ica"] +default = ["ica", "matrix"] ica = ["dep:ed25519", "dep:ed25519-dalek", "dep:hex", "dep:rust_socketio"] matrix = ["dep:matrix-sdk", "dep:url"] @@ -14,7 +14,7 @@ matrix = ["dep:matrix-sdk", "dep:url"] # matrix url = { version = "2.5.0", optional = true } -matrix-sdk = { version = "0.7.1", optional = true } +matrix-sdk = { version = "0.7.1", optional = true, default-features = false, features = ["rustls-tls"] } # ica ed25519 = { version = "2.2.3", optional = true } diff --git a/ica-rs/src/config.rs b/ica-rs/src/config.rs index 8327172..391f113 100644 --- a/ica-rs/src/config.rs +++ b/ica-rs/src/config.rs @@ -73,7 +73,16 @@ impl BotConfig { ret } pub fn new_from_cli() -> Self { - let config_file_path = env::args().nth(1).expect("No config path given"); + // let config_file_path = env::args().nth(1).expect("No config path given"); + // -c + let mut config_file_path = String::new(); + let mut args = env::args(); + while let Some(arg) = args.next() { + if arg == "-c" { + config_file_path = args.next().expect("No config path given"); + break; + } + } Self::new_from_path(config_file_path) } diff --git a/ica-rs/src/data_struct/ica/messages.rs b/ica-rs/src/data_struct/ica/messages.rs index 83e3cb8..2207338 100644 --- a/ica-rs/src/data_struct/ica/messages.rs +++ b/ica-rs/src/data_struct/ica/messages.rs @@ -1,7 +1,7 @@ use crate::data_struct::ica::files::MessageFile; use crate::data_struct::ica::{MessageId, RoomId, UserId}; -use chrono::{DateTime, NaiveDateTime}; +use chrono::DateTime; use serde::{Deserialize, Serialize}; use serde_json::{json, Value as JsonValue}; use tracing::warn; diff --git a/ica-rs/src/data_struct/ica/messages/msg_trait.rs b/ica-rs/src/data_struct/ica/messages/msg_trait.rs index 00a1cfa..c658441 100644 --- a/ica-rs/src/data_struct/ica/messages/msg_trait.rs +++ b/ica-rs/src/data_struct/ica/messages/msg_trait.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -use chrono::{DateTime, NaiveDateTime}; +use chrono::DateTime; use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; diff --git a/ica-rs/src/error.rs b/ica-rs/src/error.rs index e69de29..702de7d 100644 --- a/ica-rs/src/error.rs +++ b/ica-rs/src/error.rs @@ -0,0 +1,61 @@ +pub type ClientResult = Result; + +#[derive(Debug)] +pub enum IcaError { + /// Socket IO 链接错误 + SocketIoError(rust_socketio::Error), +} + +#[derive(Debug)] +pub enum MatrixError { + /// Homeserver Url 错误 + HomeserverUrlError(url::ParseError), + /// Http 请求错误 + HttpError(matrix_sdk::HttpError), + /// Matrix Error + MatrixError(matrix_sdk::Error), +} + +impl From for IcaError { + fn from(e: rust_socketio::Error) -> Self { IcaError::SocketIoError(e) } +} + +impl From for MatrixError { + fn from(e: matrix_sdk::Error) -> Self { MatrixError::MatrixError(e) } +} + +impl std::fmt::Display for IcaError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + IcaError::SocketIoError(e) => write!(f, "Socket IO 链接错误: {}", e), + } + } +} + +impl std::error::Error for IcaError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + IcaError::SocketIoError(e) => Some(e), + } + } +} + +impl std::fmt::Display for MatrixError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + MatrixError::HomeserverUrlError(e) => write!(f, "Homeserver Url 错误: {}", e), + MatrixError::HttpError(e) => write!(f, "Http 请求错误: {}", e), + MatrixError::MatrixError(e) => write!(f, "Matrix Error: {}", e), + } + } +} + +impl std::error::Error for MatrixError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + MatrixError::HomeserverUrlError(e) => Some(e), + MatrixError::HttpError(e) => Some(e), + MatrixError::MatrixError(e) => Some(e), + } + } +} diff --git a/ica-rs/src/ica.rs b/ica-rs/src/ica.rs index a77f2b4..3939a07 100644 --- a/ica-rs/src/ica.rs +++ b/ica-rs/src/ica.rs @@ -4,14 +4,19 @@ pub mod events; use futures_util::FutureExt; use rust_socketio::asynchronous::{Client, ClientBuilder}; use rust_socketio::{Event, Payload, TransportType}; -use tracing::{event, info, Level}; +use tracing::{event, span, Level}; use crate::config::IcaConfig; -use crate::{wrap_any_callback, wrap_callback}; +use crate::error::{ClientResult, IcaError}; +use crate::{wrap_any_callback, wrap_callback, StopGetter}; -pub async fn start_ica(config: &IcaConfig, stop_reciver: tokio::sync::oneshot::Receiver<()>) { - event!(Level::INFO, "ica-async-rs v{} start ica", crate::ICA_VERSION); - let socket = ClientBuilder::new(config.host.clone()) +pub async fn start_ica(config: &IcaConfig, stop_reciver: StopGetter) -> ClientResult<(), IcaError> { + let span = span!(Level::INFO, "Icalingua Client"); + let _enter = span.enter(); + + event!(Level::INFO, "ica-async-rs v{} initing", crate::ICA_VERSION); + + let socket = match ClientBuilder::new(config.host.clone()) .transport_type(TransportType::Websocket) .on_any(wrap_any_callback!(events::any_event)) .on("requireAuth", wrap_callback!(client::sign_callback)) @@ -27,27 +32,50 @@ pub async fn start_ica(config: &IcaConfig, stop_reciver: tokio::sync::oneshot::R .on("deleteMessage", wrap_callback!(events::delete_message)) .connect() .await - .expect("Connection failed"); - - info!("Connected"); + { + Ok(client) => { + event!(Level::INFO, "socketio connected"); + client + } + Err(e) => { + event!(Level::ERROR, "socketio connect failed: {}", e); + return Err(IcaError::SocketIoError(e)); + } + }; if config.notice_start { for room in config.notice_room.iter() { let startup_msg = crate::data_struct::ica::messages::SendMessage::new( - format!("shenbot v {}\nica-async-rs bot v{}", crate::VERSION, crate::ICA_VERSION), + format!("shenbot v {}\nica-async-rs v{}", crate::VERSION, crate::ICA_VERSION), *room, None, ); tokio::time::sleep(std::time::Duration::from_secs(1)).await; - info!("发送启动消息到房间: {}", room); + + event!(Level::INFO, "发送启动消息到房间: {}", room); + if let Err(e) = socket.emit("sendMessage", serde_json::to_value(startup_msg).unwrap()).await { - info!("启动信息发送失败 房间:{}|e:{}", room, e); + event!(Level::INFO, "启动信息发送失败 房间:{}|e:{}", room, e); } } } // 等待停止信号 stop_reciver.await.ok(); - socket.disconnect().await.expect("Disconnect failed"); + event!(Level::INFO, "socketio client stopping"); + let disconnect = socket.disconnect().await; + match disconnect { + Ok(_) => { + event!(Level::INFO, "socketio client stopped"); + Ok(()) + } + Err(e) => { + event!(Level::ERROR, "socketio client stopped with error: {}", e); + Err(IcaError::SocketIoError(e)) + } + } + + // event!(Level::INFO, "socketio client stopped"); + // disconnect.into() } diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index e8d85f1..103ae32 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -2,6 +2,7 @@ use std::time::Duration; mod config; mod data_struct; +mod error; #[cfg(feature = "ica")] mod ica; #[cfg(feature = "matrix")] @@ -10,7 +11,7 @@ mod py; mod status; use config::BotConfig; -use tracing::{event, info, Level}; +use tracing::{event, info, span, Level}; pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus { config: None, @@ -20,6 +21,8 @@ pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus { pub type MainStatus = status::BotStatus; +pub type StopGetter = tokio::sync::oneshot::Receiver<()>; + pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const ICA_VERSION: &str = "1.4.0"; pub const MATRIX_VERSION: &str = "0.1.0"; @@ -40,8 +43,22 @@ macro_rules! wrap_any_callback { #[tokio::main] async fn main() { - tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG).init(); - event!(Level::INFO, "shenbot-async-rs v{} main", VERSION); + // -d -> debug + // none -> info + let level = { + let args = std::env::args(); + if args.collect::>().contains(&"-d".to_string()) { + Level::DEBUG + } else { + Level::INFO + } + }; + + tracing_subscriber::fmt().with_max_level(level).init(); + let span = span!(Level::INFO, "Shenbot Main"); + let _enter = span.enter(); + + event!(Level::INFO, "shenbot-async-rs v{} starting", VERSION); let bot_config = BotConfig::new_from_cli(); MainStatus::static_init(bot_config); @@ -50,16 +67,30 @@ async fn main() { py::init_py(); // 准备一个用于停止 socket 的变量 - let (send, recv) = tokio::sync::oneshot::channel::<()>(); + event!(Level::INFO, "启动 ICA"); + let (ica_send, ica_recv) = tokio::sync::oneshot::channel::<()>(); if bot_config.check_ica() { - info!("启动 ica"); + event!(Level::INFO, "启动 ica"); let config = bot_config.ica(); tokio::spawn(async move { - ica::start_ica(&config, recv).await; + ica::start_ica(&config, ica_recv).await.unwrap(); }); } else { - info!("未启用 ica"); + event!(Level::INFO, "未启用 ica"); + } + + event!(Level::INFO, "启动 Matrix"); + let (matrix_send, matrix_recv) = tokio::sync::oneshot::channel::<()>(); + + if bot_config.check_matrix() { + event!(Level::INFO, "启动 Matrix"); + let config = bot_config.matrix(); + tokio::spawn(async move { + matrix::start_matrix(&config, matrix_recv).await.unwrap(); + }); + } else { + event!(Level::INFO, "未启用 Matrix"); } tokio::time::sleep(Duration::from_secs(2)).await; @@ -68,7 +99,8 @@ async fn main() { let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); - send.send(()).ok(); + ica_send.send(()).ok(); + matrix_send.send(()).ok(); info!("Disconnected"); } diff --git a/ica-rs/src/matrix.rs b/ica-rs/src/matrix.rs index 8b13789..e66d26b 100644 --- a/ica-rs/src/matrix.rs +++ b/ica-rs/src/matrix.rs @@ -1 +1,152 @@ +pub mod events; +use std::str::FromStr; + +use matrix_sdk::{ + config::SyncSettings, + ruma::{ + api::client::message::send_message_event, + events::room::message::{ + AddMentions, ForwardThread, MessageType, OriginalSyncRoomMessageEvent, + RoomMessageEventContent, + }, + OwnedRoomId, TransactionId, + }, + Client, Room, RoomState, +}; +use tracing::{event, span, Level}; +use url::Url; + +use crate::config::MatrixConfig; +use crate::error::{ClientResult, MatrixError}; +use crate::StopGetter; + +pub async fn start_matrix( + config: &MatrixConfig, + stop_reciver: StopGetter, +) -> ClientResult<(), MatrixError> { + let span = span!(Level::INFO, "Matrix Client"); + let _enter = span.enter(); + + let homeserver_url = match Url::parse(&config.home_server) { + Ok(url) => url, + Err(e) => { + event!(Level::ERROR, "Homeserver Url 错误: {}", e); + return Err(MatrixError::HomeserverUrlError(e)); + } + }; + let password = &config.bot_password; + let username = &config.bot_id; + + let client = match Client::new(homeserver_url).await { + Ok(client) => { + event!(Level::INFO, "Logged in as {}", username); + client + } + Err(error) => { + event!(Level::ERROR, "Failed to log in as {}: {}", username, error); + return Err(MatrixError::HttpError(error)); + } + }; + + let display_name = format!("shenbot-matrix v{}", crate::MATRIX_VERSION); + + match client + .matrix_auth() + .login_username(&username, &password) + .initial_device_display_name(&display_name) + .await + { + Ok(_) => { + event!(Level::INFO, "Logged in as {}", username); + } + Err(error) => { + event!(Level::ERROR, "Failed to log in as {}: {}", username, error); + return Err(MatrixError::MatrixError(error)); + } + } + + // 发送启动消息 + if config.notice_start { + for room in config.notice_room.iter() { + let startup_msg = RoomMessageEventContent::text_plain(format!( + "shenbot v {}\nmatrix-rs v{} started!", + crate::VERSION, + crate::MATRIX_VERSION + )); + let startup_req: send_message_event::v3::Request = + send_message_event::v3::Request::new( + OwnedRoomId::from_str(&room).unwrap(), + TransactionId::new(), + &startup_msg, + ) + .unwrap(); + + event!(Level::INFO, "发送启动消息到房间: {}", room); + + if let Err(e) = client.send::(startup_req, None).await + { + event!(Level::INFO, "启动信息发送失败 房间:{}|e:{}", room, e); + } + } + } else { + event!(Level::INFO, "未启用启动消息"); + } + + client.add_event_handler(on_room_message); + + match client.sync_once(SyncSettings::new()).await { + Ok(_) => { + event!(Level::INFO, "Synced"); + } + Err(error) => { + event!(Level::ERROR, "Failed to sync: {}", error); + return Err(MatrixError::MatrixError(error)); + } + } + + client.sync(SyncSettings::default()).await?; + + // while stop_reciver.await.is_err() { + // event!(Level::INFO, "Matrix client is running"); + // tokio::time::sleep(std::time::Duration::from_secs(1)).await; + // } + + event!(Level::INFO, "Matrix is not implemented yet"); + stop_reciver.await.ok(); + event!(Level::INFO, "Matrix client stopping"); + // some stop + event!(Level::INFO, "Matrix client stopped"); + Ok(()) +} + +pub async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { + // We only want to listen to joined rooms. + if room.state() != RoomState::Joined { + return; + } + + // We only want to log text messages. + let MessageType::Text(msgtype) = &event.content.msgtype else { + return; + }; + + // 匹配消息 + + // /bot + if msgtype.body == "/bot" { + let pong = format!("shenbot v {}\nmatrix-rs v{}", crate::VERSION, crate::MATRIX_VERSION); + + let reply = RoomMessageEventContent::text_plain(pong); + let reply = reply.make_reply_to( + &event.into_full_event(room.room_id().to_owned()), + ForwardThread::No, + AddMentions::No, + ); + + room.send(reply).await.expect("Failed to send message"); + return; + } + + // 发给 Python 处理剩下的 +} diff --git a/ica-rs/src/matrix/events.rs b/ica-rs/src/matrix/events.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ica-rs/src/matrix/events.rs @@ -0,0 +1 @@ +