From 6572e1d04e489533fca2a5787fb14a2d4ee02b4e Mon Sep 17 00:00:00 2001 From: jeff Date: Wed, 26 Aug 2020 21:09:50 +0000 Subject: [PATCH] Port dice bot to use matrix-rust-sdk. This gives us many things for free, like automated state management, no need to declare special API structs and use HTTP requests directly, and most importantly: ENCRYPTION! --- Cargo.lock | 1165 +++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 11 +- src/bin/dicebot.rs | 37 +- src/bot.rs | 315 +++++------- src/lib.rs | 1 - src/matrix.rs | 81 --- 6 files changed, 1319 insertions(+), 291 deletions(-) delete mode 100644 src/matrix.rs diff --git a/Cargo.lock b/Cargo.lock index c941393..eb7091f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,23 +1,121 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "ahash" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217" +dependencies = [ + "const-random", +] + +[[package]] +name = "aho-corasick" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" +dependencies = [ + "memchr", +] + +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "arc-swap" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + [[package]] name = "arrayvec" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" +[[package]] +name = "assign" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4af5687fe33aec5e70ef14caac5e0d363e335e5e5d6385fb75978d0c241b1d67" + +[[package]] +name = "async-native-tls" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9e7a929bd34c68a82d58a4de7f86fffdaf97fb2af850162a7bb19dd7269b33" +dependencies = [ + "native-tls", + "thiserror", + "tokio", + "url", +] + +[[package]] +name = "async-stream" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22068c0c19514942eefcfd4daf8976ef1aad84e61539f95cd200c35202f80af5" +dependencies = [ + "async-stream-impl", + "futures-core", +] + +[[package]] +name = "async-stream-impl" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25f9db3b38af870bf7e5cc649167533b493928e50744e2c30ae350230b414670" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "async-trait" +version = "0.1.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1a4a2f97ce50c9d0282c1468816208588441492b40d813b2e0419c22c05e7f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "atomic" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3410529e8288c463bedb5930f82833bc0c90e5d2fe639a56582a4d09220b281" +dependencies = [ + "autocfg", +] + [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "base64" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" + [[package]] name = "base64" version = "0.12.3" @@ -30,12 +128,29 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2b_simd" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + [[package]] name = "bumpalo" version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" +[[package]] +name = "byteorder" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" + [[package]] name = "bytes" version = "0.5.6" @@ -58,7 +173,13 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" name = "chronicle-dicebot" version = "0.2.0" dependencies = [ + "async-trait", + "dirs", "itertools", + "matrix-sdk", + "matrix-sdk-base 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0)", + "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0)", + "matrix-sdk-common-macros", "nom", "rand", "reqwest", @@ -67,8 +188,59 @@ dependencies = [ "thiserror", "tokio", "toml", + "tracing-subscriber", + "url", ] +[[package]] +name = "chrono" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b" +dependencies = [ + "num-integer", + "num-traits", + "time", +] + +[[package]] +name = "cjson" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2b601fb350e2fdbbd2ffb19aef2141fa90864d8fdca83d64466cb9bdb5694a7" +dependencies = [ + "itoa", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "const-random" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f1af9ac737b2dd2d577701e59fd09ba34822f6f2ebdb30a7647405d9e55e16a" +dependencies = [ + "const-random-macro", + "proc-macro-hack", +] + +[[package]] +name = "const-random-macro" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25e4c606eb459dd29f7c57b2e0879f2b6f14ee130918c2b78ccb58a9624e6c7a" +dependencies = [ + "getrandom", + "proc-macro-hack", +] + +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + [[package]] name = "core-foundation" version = "0.7.0" @@ -85,6 +257,65 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac" +[[package]] +name = "crossbeam-queue" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "dashmap" +version = "3.11.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f260e2fc850179ef410018660006951c1b55b79e8087e87111a2c388994b9b5" +dependencies = [ + "ahash", + "cfg-if", + "num_cpus", +] + +[[package]] +name = "dirs" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "142995ed02755914747cc6ca76fc7e4583cd18578746716d0508ea6ed558b9ff" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" +dependencies = [ + "libc", + "redox_users", + "winapi 0.3.9", +] + +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dtoa" version = "0.4.6" @@ -99,9 +330,9 @@ checksum = "cd56b59865bce947ac5958779cfa508f6c3b9497cc762b7e24a12d11ccde2c4f" [[package]] name = "encoding_rs" -version = "0.8.23" +version = "0.8.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8ac63f94732332f44fe654443c46f6375d1939684c17b0afb6cb56b0456e171" +checksum = "a51b8cf747471cb9499b6d59e59b0444f4c90eba8968c4e44874e92b5b64ace2" dependencies = [ "cfg-if", ] @@ -127,6 +358,16 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "form_urlencoded" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ece68d15c92e84fa4f19d3780f1294e5ca82a78a6d515f1efaabcc144688be00" +dependencies = [ + "matches", + "percent-encoding", +] + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -143,6 +384,27 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +[[package]] +name = "futures" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b980f2816d6ee8673b6517b52cb0e808a180efc92e5c19d02cdda79066703ef" + +[[package]] +name = "futures" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e05b85ec287aac0dc34db7d4a569323df697f9c55b99b15d6b4ef8cde49f613" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.5" @@ -150,6 +412,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f366ad74c28cca6ba456d95e6422883cfb4b252a83bed929c83abfdbbf2967d5" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -158,6 +421,52 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" +[[package]] +name = "futures-executor" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10d6bb888be1153d3abeb9006b11b02cf5e9b209fda28693c31ae1e4e012e314" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" + +[[package]] +name = "futures-locks" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4297dd1d9d6268237e4f93aeb9c90fc1bf0d8cec7e1cef22798939e4c43a251" +dependencies = [ + "futures 0.1.29", +] + +[[package]] +name = "futures-locks" +version = "0.5.1-pre" +source = "git+https://github.com/asomers/futures-locks#c380ff6ef44ee77f502b4feec2a83cb2dbae99cb" +dependencies = [ + "futures 0.3.5", +] + +[[package]] +name = "futures-macro" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0b5a30a4328ab5473878237c447333c093297bded83a4983d10f4deea240d39" +dependencies = [ + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.5" @@ -169,6 +478,19 @@ name = "futures-task" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bdb66b5f09e22019b1ab0830f7785bcea8e7a42148683f99214f73f8ec21a626" +dependencies = [ + "once_cell", +] + +[[package]] +name = "futures-timer" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" +dependencies = [ + "gloo-timers", + "send_wrapper", +] [[package]] name = "futures-util" @@ -176,10 +498,18 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project", "pin-utils", + "proc-macro-hack", + "proc-macro-nested", + "slab", ] [[package]] @@ -191,6 +521,20 @@ dependencies = [ "cfg-if", "libc", "wasi", + "wasm-bindgen", +] + +[[package]] +name = "gloo-timers" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", + "web-sys", ] [[package]] @@ -221,6 +565,30 @@ dependencies = [ "autocfg", ] +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9" +dependencies = [ + "libc", +] + +[[package]] +name = "hex" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" + [[package]] name = "http" version = "0.2.1" @@ -306,6 +674,18 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "instant" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b141fdc7836c525d4d594027d318c84161ca17aaf8113ab1f81ab93ae897485" +dependencies = [ + "js-sys", + "time", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "iovec" version = "0.1.4" @@ -345,6 +725,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "js_int" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b96797f53235a1d6dc985f244a69de54b04c45b7e0e357a35c85a45a847d92f2" +dependencies = [ + "serde", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -380,6 +769,17 @@ version = "0.2.76" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" +[[package]] +name = "libsqlite3-sys" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56d90181c2904c287e5390186be820e5ef311a3c62edebb7d6ca3d6a48ce041d" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + [[package]] name = "log" version = "0.4.11" @@ -389,12 +789,145 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "maplit" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" + +[[package]] +name = "matchers" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" +dependencies = [ + "regex-automata", +] + [[package]] name = "matches" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" +[[package]] +name = "matrix-sdk" +version = "0.1.0" +source = "git+https://github.com/matrix-org/matrix-rust-sdk?rev=master#95c87089957b9a98303ba4f2f8d49e5eea35f285" +dependencies = [ + "async-trait", + "dashmap", + "futures-timer", + "http", + "matrix-sdk-base 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=master)", + "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=master)", + "matrix-sdk-common-macros", + "reqwest", + "serde_json", + "thiserror", + "tracing", + "tracing-futures", + "url", +] + +[[package]] +name = "matrix-sdk-base" +version = "0.1.0" +source = "git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0#fd3dc50ebe90d21520c5ff475dc6af8192d3ede3" +dependencies = [ + "async-trait", + "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0)", + "serde", + "serde_json", + "thiserror", + "tokio", + "zeroize", +] + +[[package]] +name = "matrix-sdk-base" +version = "0.1.0" +source = "git+https://github.com/matrix-org/matrix-rust-sdk?rev=master#95c87089957b9a98303ba4f2f8d49e5eea35f285" +dependencies = [ + "async-trait", + "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=master)", + "matrix-sdk-common-macros", + "matrix-sdk-crypto", + "serde", + "serde_json", + "thiserror", + "tokio", + "tracing", + "zeroize", +] + +[[package]] +name = "matrix-sdk-common" +version = "0.1.0" +source = "git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0#fd3dc50ebe90d21520c5ff475dc6af8192d3ede3" +dependencies = [ + "futures-locks 0.5.0", + "instant", + "js_int", + "ruma-api 0.16.1", + "ruma-client-api 0.9.0", + "ruma-events 0.21.3", + "ruma-identifiers 0.16.2", + "tokio", + "uuid", +] + +[[package]] +name = "matrix-sdk-common" +version = "0.1.0" +source = "git+https://github.com/matrix-org/matrix-rust-sdk?rev=master#95c87089957b9a98303ba4f2f8d49e5eea35f285" +dependencies = [ + "assign", + "futures-locks 0.5.1-pre", + "instant", + "js_int", + "ruma", + "tokio", + "uuid", +] + +[[package]] +name = "matrix-sdk-common-macros" +version = "0.1.0" +source = "git+https://github.com/matrix-org/matrix-rust-sdk?rev=master#95c87089957b9a98303ba4f2f8d49e5eea35f285" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "matrix-sdk-crypto" +version = "0.1.0" +source = "git+https://github.com/matrix-org/matrix-rust-sdk?rev=master#95c87089957b9a98303ba4f2f8d49e5eea35f285" +dependencies = [ + "async-trait", + "atomic", + "cjson", + "dashmap", + "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=master)", + "matrix-sdk-common-macros", + "olm-rs", + "serde", + "serde_json", + "sqlx", + "thiserror", + "tracing", + "tracing-futures", + "url", + "zeroize", +] + +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + [[package]] name = "memchr" version = "2.3.3" @@ -499,6 +1032,59 @@ dependencies = [ "version_check", ] +[[package]] +name = "num-integer" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "olm-rs" +version = "0.6.0" +source = "git+https://gitlab.gnome.org/jhaye/olm-rs/#af483ef1715c3071fcd6da4958ee2216288b1e14" +dependencies = [ + "getrandom", + "olm-sys", + "serde", + "serde_json", + "zeroize", +] + +[[package]] +name = "olm-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38da14ca5e2ca81f3c0a91f0bf7cade30ec256f162f8be6929a26bbfca065bb1" + +[[package]] +name = "once_cell" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" + [[package]] name = "openssl" version = "0.10.30" @@ -582,6 +1168,27 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20" +[[package]] +name = "proc-macro-crate" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" +dependencies = [ + "toml", +] + +[[package]] +name = "proc-macro-hack" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598" + +[[package]] +name = "proc-macro-nested" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" + [[package]] name = "proc-macro2" version = "1.0.19" @@ -647,6 +1254,45 @@ version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +[[package]] +name = "redox_users" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431" +dependencies = [ + "getrandom", + "redox_syscall", + "rust-argon2", +] + +[[package]] +name = "regex" +version = "1.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-automata" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" +dependencies = [ + "byteorder", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" + [[package]] name = "remove_dir_all" version = "0.5.3" @@ -659,10 +1305,9 @@ dependencies = [ [[package]] name = "reqwest" version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12427a5577082c24419c9c417db35cfeb65962efc7675bb6b0d5f1f9d315bfe6" +source = "git+https://github.com/seanmonstar/reqwest?rev=cff487ff58630cf0ac59f3e46cbf20cf50a28b3f#cff487ff58630cf0ac59f3e46cbf20cf50a28b3f" dependencies = [ - "base64", + "base64 0.12.3", "bytes", "encoding_rs", "futures-core", @@ -691,6 +1336,266 @@ dependencies = [ "winreg", ] +[[package]] +name = "ruma" +version = "0.0.1" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "ruma-api 0.17.0-alpha.1", + "ruma-client-api 0.10.0-alpha.1", + "ruma-common 0.2.0", + "ruma-events 0.22.0-alpha.1", + "ruma-identifiers 0.17.4", + "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434)", +] + +[[package]] +name = "ruma-api" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82ffdb7fb9cf6af2b1d0d8254d922560ecb70081d7e70931c9b996b6b4839db5" +dependencies = [ + "http", + "percent-encoding", + "ruma-api-macros 0.16.1", + "ruma-identifiers 0.16.2", + "ruma-serde 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", + "strum 0.18.0", +] + +[[package]] +name = "ruma-api" +version = "0.17.0-alpha.1" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "http", + "percent-encoding", + "ruma-api-macros 0.17.0-alpha.1", + "ruma-identifiers 0.17.4", + "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434)", + "serde", + "serde_json", + "strum 0.19.2", +] + +[[package]] +name = "ruma-api-macros" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52b82b4567b9af9b40a86f7778821c016ea961f55e4fee255f8f24bb28ee7452" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ruma-api-macros" +version = "0.17.0-alpha.1" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ruma-client-api" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "082913ad135ca55ee06a55d295bea954982f2ac5e0150adc09024f5cbb8cb6cf" +dependencies = [ + "http", + "js_int", + "ruma-api 0.16.1", + "ruma-common 0.1.3", + "ruma-events 0.21.3", + "ruma-identifiers 0.16.2", + "ruma-serde 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", + "strum 0.18.0", +] + +[[package]] +name = "ruma-client-api" +version = "0.10.0-alpha.1" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "assign", + "http", + "js_int", + "percent-encoding", + "ruma-api 0.17.0-alpha.1", + "ruma-common 0.2.0", + "ruma-events 0.22.0-alpha.1", + "ruma-identifiers 0.17.4", + "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434)", + "serde", + "serde_json", + "strum 0.19.2", +] + +[[package]] +name = "ruma-common" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb49e83277e82c69cc258cedc7e68b3d72ba378f1cb6105cbfcc8831e422b4d" +dependencies = [ + "matches", + "ruma-serde 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", + "strum 0.18.0", +] + +[[package]] +name = "ruma-common" +version = "0.2.0" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "js_int", + "ruma-identifiers 0.17.4", + "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434)", + "serde", + "serde_json", + "strum 0.19.2", +] + +[[package]] +name = "ruma-events" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ddf82c2231e4c53443424df34e868e4b09c20de7a76780d47a133a3b3f8ad9c" +dependencies = [ + "js_int", + "ruma-common 0.1.3", + "ruma-events-macros 0.21.3", + "ruma-identifiers 0.16.2", + "ruma-serde 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_json", +] + +[[package]] +name = "ruma-events" +version = "0.22.0-alpha.1" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "js_int", + "ruma-common 0.2.0", + "ruma-events-macros 0.22.0-alpha.1", + "ruma-identifiers 0.17.4", + "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434)", + "serde", + "serde_json", + "strum 0.19.2", +] + +[[package]] +name = "ruma-events-macros" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88e5c5b242fe4ee0cc56879057353621196d0988dd359579cad8f43471e483b7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ruma-events-macros" +version = "0.22.0-alpha.1" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ruma-identifiers" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6316cb248e3e0323a5a269b8eaed571404fb4f65c81848549e9ba99fd9b8e9de" +dependencies = [ + "serde", +] + +[[package]] +name = "ruma-identifiers" +version = "0.17.4" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "ruma-identifiers-macros", + "ruma-identifiers-validation", + "serde", + "strum 0.19.2", +] + +[[package]] +name = "ruma-identifiers-macros" +version = "0.17.4" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "proc-macro2", + "quote", + "ruma-identifiers-validation", + "syn", +] + +[[package]] +name = "ruma-identifiers-validation" +version = "0.1.1" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "ruma-serde 0.2.3 (git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434)", + "serde", + "serde_json", + "strum 0.19.2", +] + +[[package]] +name = "ruma-serde" +version = "0.2.3" +source = "git+https://github.com/ruma/ruma?rev=e74158b2626186ce23d4d3c07782e60b3be39434#e74158b2626186ce23d4d3c07782e60b3be39434" +dependencies = [ + "form_urlencoded", + "itoa", + "js_int", + "serde", + "serde_json", +] + +[[package]] +name = "ruma-serde" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a50045b7e93329085488c88aee95e109c32d53c89d0f4882791e2cc81eb10ce" +dependencies = [ + "form_urlencoded", + "itoa", + "js_int", + "serde", + "serde_json", +] + +[[package]] +name = "rust-argon2" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017" +dependencies = [ + "base64 0.11.0", + "blake2b_simd", + "constant_time_eq", + "crossbeam-utils", +] + [[package]] name = "ryu" version = "1.0.5" @@ -730,6 +1635,12 @@ dependencies = [ "libc", ] +[[package]] +name = "send_wrapper" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f638d531eccd6e23b980caf34876660d38e265409d8e99b397ab71eb3612fad0" + [[package]] name = "serde" version = "1.0.115" @@ -773,6 +1684,15 @@ dependencies = [ "url", ] +[[package]] +name = "sharded-slab" +version = "0.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06d5a3f5166fb5b42a5439f2eee8b9de149e235961e3eb21c5808fc3ea17ff3e" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.2.1" @@ -789,6 +1709,12 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +[[package]] +name = "smallvec" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" + [[package]] name = "socket2" version = "0.3.12" @@ -801,12 +1727,119 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "sqlformat" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ce64a4576e1720a2e511bf3ccdb8c0f6cfed0fc265bcbaa0bd369485e02c631" +dependencies = [ + "lazy_static", + "maplit", + "regex", +] + +[[package]] +name = "sqlx" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8974cacd80085fbe49e778708d660dec6fb351604dc34c3905b26efb2803b038" +dependencies = [ + "sqlx-core", + "sqlx-macros", +] + +[[package]] +name = "sqlx-core" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ac5a436f941c42eac509471a730df5c3c58e1450e68cd39afedbd948206273" +dependencies = [ + "async-native-tls", + "async-stream", + "bitflags", + "byteorder", + "crossbeam-queue", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-util", + "hex", + "libc", + "libsqlite3-sys", + "log", + "memchr", + "percent-encoding", + "sqlformat", + "tokio", + "url", +] + +[[package]] +name = "sqlx-macros" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de2ae78b783af5922d811b14665a5a3755e531c3087bb805cf24cf71f15e6780" +dependencies = [ + "dotenv", + "futures 0.3.5", + "heck", + "lazy_static", + "proc-macro2", + "quote", + "sqlx-core", + "syn", + "tokio", + "url", +] + [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strum" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57bd81eb48f4c437cadc685403cad539345bf703d78e63707418431cecd4522b" +dependencies = [ + "strum_macros 0.18.0", +] + +[[package]] +name = "strum" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3924a58d165da3b7b2922c667ab0673c7b5fd52b5c19ea3442747bcb3cd15abe" +dependencies = [ + "strum_macros 0.19.2", +] + +[[package]] +name = "strum_macros" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87c85aa3f8ea653bfd3ddf25f7ee357ee4d204731f6aa9ad04002306f6e2774c" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strum_macros" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2ab682ecdcae7f5f45ae85cd7c1e6c8e68ea42c8a612d47fedf831c037146a" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "1.0.39" @@ -818,6 +1851,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "tempfile" version = "3.1.0" @@ -852,6 +1897,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + [[package]] name = "time" version = "0.1.43" @@ -883,6 +1937,7 @@ dependencies = [ "memchr", "mio", "mio-uds", + "num_cpus", "pin-project-lite", "signal-hook-registry", "slab", @@ -948,9 +2003,21 @@ checksum = "6d79ca061b032d6ce30c660fded31189ca0b9922bf483cd70759f13a2d86786c" dependencies = [ "cfg-if", "log", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.15" @@ -960,6 +2027,58 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "tracing-futures" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab7bb6f14721aa00656086e9335d363c5c8747bae02ebe32ea2c7dece5689b4c" +dependencies = [ + "pin-project", + "tracing", +] + +[[package]] +name = "tracing-log" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e0f8c7178e13481ff6765bd169b33e8d554c5d2bbede5e32c356194be02b9b9" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-serde" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6ccba2f8f16e0ed268fc765d9b7ff22e965e7185d32f8f1ec8294fe17d86e79" +dependencies = [ + "serde", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abd165311cc4d7a555ad11cc77a37756df836182db0d81aac908c8184c584f40" +dependencies = [ + "ansi_term", + "chrono", + "lazy_static", + "matchers", + "regex", + "serde", + "serde_json", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", + "tracing-serde", +] + [[package]] name = "try-lock" version = "0.2.3" @@ -993,6 +2112,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" + [[package]] name = "unicode-xid" version = "0.2.1" @@ -1010,6 +2135,15 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "uuid" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" +dependencies = [ + "rand", +] + [[package]] name = "vcpkg" version = "0.2.10" @@ -1168,3 +2302,24 @@ dependencies = [ "winapi 0.2.8", "winapi-build", ] + +[[package]] +name = "zeroize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbac2ed2ba24cc90f5e06485ac8c7c1e5449fe8911aef4d8877218af021a5b8" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de251eec69fc7c1bc3923403d18ececb929380e016afe103da75f396704f8ca2" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] diff --git a/Cargo.toml b/Cargo.toml index 8a05d7a..08c91b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,13 +11,22 @@ keywords = ["games", "dice", "matrix", "bot"] categories = ["games"] [dependencies] -reqwest = "0.10" +reqwest = { git = "https://github.com/seanmonstar/reqwest", rev = "cff487ff58630cf0ac59f3e46cbf20cf50a28b3f", default_features = false } serde_json = "1" toml = "0.5" nom = "5" rand = "0.7" thiserror = "1.0" itertools = "0.9" +matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "master" } +matrix-sdk-common-macros = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "master" } +matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "0.1.0" } +matrix-sdk-base = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "0.1.0", default_features = false } +async-trait = "0.1.38" +# these aren't really necessary for matrix. just used in support of example +tracing-subscriber = "0.2.11" +url = "2.1.1" +dirs = "3.0.1" [dependencies.serde] version = "1" diff --git a/src/bin/dicebot.rs b/src/bin/dicebot.rs index 40967a4..51875ec 100644 --- a/src/bin/dicebot.rs +++ b/src/bin/dicebot.rs @@ -1,6 +1,17 @@ -use chronicle_dicebot::bot::DiceBot; -use tokio::select; -use tokio::signal::unix::{signal, SignalKind}; +use chronicle_dicebot::bot::run_bot; +use chronicle_dicebot::bot::Config; +use std::fs; +use std::path::PathBuf; + +fn read_config>(config_path: P) -> Result> { + let config_path = config_path.into(); + let config = { + let contents = fs::read_to_string(&config_path)?; + toml::from_str(&contents)? + }; + + Ok(config) +} #[tokio::main] async fn main() -> Result<(), Box> { @@ -8,23 +19,9 @@ async fn main() -> Result<(), Box> { .skip(1) .next() .expect("Need a config as an argument"); - println!("Logging in"); - let mut bot = DiceBot::from_path(config_path).await?; - println!("Logged in"); - let mut sigint = signal(SignalKind::interrupt())?; + let cfg = read_config(config_path)?; - loop { - select! { - _ = sigint.recv() => { - break; - } - result = bot.sync() => { - result?; - } - } - } - - println!("Logging out"); - bot.logout().await + run_bot(cfg.matrix).await?; + Ok(()) } diff --git a/src/bot.rs b/src/bot.rs index 9430fb2..8cc4024 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,12 +1,19 @@ -use crate::matrix::{Event, MessageContent, RoomEvent, SyncCommand, NoticeMessage}; use crate::commands::parse_command; -use reqwest::{Client, Url}; +use dirs; +use matrix_sdk::{ + self, + events::{ + room::message::{MessageEventContent, NoticeMessageEventContent, TextMessageEventContent}, + SyncMessageEvent, + }, + Client, ClientConfig, EventEmitter, JsonStore, SyncRoom, SyncSettings, +}; +use matrix_sdk_common_macros::async_trait; use serde::{self, Deserialize, Serialize}; -use std::fs; -use std::path::PathBuf; +use thiserror::Error; +use url::Url; -const USER_AGENT: &str = - "AxFive Matrix DiceBot/0.1.0 (+https://gitlab.com/Taywee/axfive-matrix-dicebot)"; +//TODO move the config structs and read_config into their own file. /// The "matrix" section of the config, which gives home server, login information, and etc. #[derive(Serialize, Deserialize, Debug)] @@ -14,195 +21,137 @@ pub struct MatrixConfig { /// Your homeserver of choice, as an FQDN without scheme or path pub home_server: String, - /// The next batch to grab. This should be set automatically - #[serde(default)] - pub next_batch: Option, + /// Username to login as. Only the localpart. + pub username: String, - /// The transaction ID. This should be set automatically - #[serde(default)] - pub txn_id: u64, - - /// The login table. This may be set to whatever you wish, depending on your login method, - /// though multi-step logins (like challenge-based) won't work here. - pub login: toml::Value, + /// Bot account password. + pub password: String, } -/// The base config, which is read from and written to by the bot +/// Represents the toml config file for the dicebot. #[derive(Serialize, Deserialize, Debug)] pub struct Config { pub matrix: MatrixConfig, } -/// The actual dicebot structure, which drives the entire operation. -/// -/// This is the core of the dicebot program. +/// The DiceBot struct itself is the core of the program, essentially the entrypoint +/// to the bot. pub struct DiceBot { - config_path: Option, - config: Config, - access_token: String, - next_batch: Option, client: Client, - home_server: Url, - txn_id: u64, -} - -#[derive(Deserialize, Debug)] -struct LoginResponse { - access_token: String, } impl DiceBot { - /// Create a new dicebot from the given config path and config - pub async fn new( - config_path: Option, - config: Config, - ) -> Result> { - let home_server: Url = format!("https://{}", config.matrix.home_server).parse()?; - let client = Client::new(); - let request = serde_json::to_string(&config.matrix.login)?; - let mut login_url = home_server.clone(); - login_url.set_path("/_matrix/client/r0/login"); - let response = client - .post(login_url) - .header("user-agent", USER_AGENT) - .body(request) - .send() - .await?; - let body: LoginResponse = serde_json::from_str(&response.text().await?)?; - let next_batch = config.matrix.next_batch.clone(); - let txn_id = config.matrix.txn_id; - Ok(DiceBot { - home_server, - config_path, - client, - config, - access_token: body.access_token, - next_batch, - txn_id, - }) - } - - /// Create a new dicebot, storing the config path to write it out - pub async fn from_path>( - config_path: P, - ) -> Result> { - let config_path = config_path.into(); - let config = { - let contents = fs::read_to_string(&config_path)?; - toml::from_str(&contents)? - }; - DiceBot::new(Some(config_path), config).await - } - - /// Build a url using the current home server and the given path, as well as appending the - /// access token - fn url>(&self, path: S, query: &[(&str, &str)]) -> Url { - let mut url = self.home_server.clone(); - url.set_path(path.as_ref()); - { - let mut query_pairs = url.query_pairs_mut(); - query_pairs.append_pair("access_token", &self.access_token); - - for pair in query.iter() { - query_pairs.append_pair(pair.0, pair.1); - } - } - - url - } - - /// Sync to the matrix homeserver, acting on events as necessary - pub async fn sync(&mut self) -> Result<(), Box> { - let mut sync_url = self.url("/_matrix/client/r0/sync", &[("timeout", "30000")]); - - // TODO: handle http 429 - if let Some(since) = &self.next_batch { - sync_url.query_pairs_mut().append_pair("since", since); - } - let body = self - .client - .get(sync_url) - .header("user-agent", USER_AGENT) - .send() - .await? - .text() - .await?; - let sync: SyncCommand = serde_json::from_str(&body).unwrap(); - // First join invited rooms - for room in sync.rooms.invite.keys() { - let join_url = self.url(format!("/_matrix/client/r0/rooms/{}/join", room), &[]); - self.client - .post(join_url) - .header("user-agent", USER_AGENT) - .send() - .await?; - } - - for (room_id, room) in sync.rooms.join.iter() { - for event in &room.timeline.events { - if let Event::Room(RoomEvent { - sender, - event_id: _, - content: MessageContent::Text(message), - .. - }) = event - { - let (plain, html): (String, String) = match parse_command(message.body()) { - Ok(Some(command)) => { - let command = command.execute(); - (command.plain().into(), command.html().into()) - }, - Ok(None) => continue, - Err(e) => { - let message = format!("Error parsing command: {}", e); - let html_message = format!("

{}

", message); - (message, html_message) - }, - }; - - let plain = format!("{}\n{}", sender, plain); - let html = format!("

{}

\n{}", sender, html); - - let message = NoticeMessage { - body: plain, - format: Some("org.matrix.custom.html".into()), - formatted_body: Some(html), - }; - - self.txn_id += 1; - let send_url = self.url(format!("/_matrix/client/r0/rooms/{}/send/m.room.message/{}", room_id, self.txn_id), &[]); - self.client - .put(send_url) - .header("user-agent", USER_AGENT) - .body(serde_json::to_string(&message)?) - .send() - .await?; - } - } - } - self.next_batch = Some(sync.next_batch); - Ok(()) - } - - /// Log off of the matrix server, also writing out the config file if one was given in - /// construction - pub async fn logout(mut self) -> Result<(), Box> { - let logout_url = self.url("/_matrix/client/r0/logout", &[]); - self.client - .post(logout_url) - .header("user-agent", USER_AGENT) - .body("{}") - .send() - .await?; - - self.config.matrix.next_batch = self.next_batch; - self.config.matrix.txn_id = self.txn_id; - - if let Some(config_path) = self.config_path { - let config = toml::to_string_pretty(&self.config)?; - fs::write(config_path, config)?; - } - - Ok(()) + /// Create a new dicebot with the given Matrix client. + pub fn new(client: Client) -> Self { + DiceBot { client } } } + +/// This event emitter listens for messages with dice rolling commands. +/// Originally adapted from the matrix-rust-sdk command bot example. +#[async_trait] +impl EventEmitter for DiceBot { + async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent) { + if let SyncRoom::Joined(room) = room { + let (msg_body, sender_username) = if let SyncMessageEvent { + content: MessageEventContent::Text(TextMessageEventContent { body, .. }), + sender, + .. + } = event + { + ( + body.clone(), + format!("@{}:{}", sender.localpart(), sender.server_name()), + ) + } else { + (String::new(), String::new()) + }; + + let (plain, html) = match parse_command(&msg_body) { + Ok(Some(command)) => { + let command = command.execute(); + (command.plain().into(), command.html().into()) + } + Ok(None) => return, + Err(e) => { + let message = format!("Error parsing command: {}", e); + let html_message = format!("

{}

", message); + (message, html_message) + } + }; + + let plain = format!("{}\n{}", sender_username, plain); + let html = format!("

{}

\n{}", sender_username, html); + let content = MessageEventContent::Notice(NoticeMessageEventContent::html(plain, html)); + + //we clone here to hold the lock for as little time as possible. + let room_id = room.read().await.room_id.clone(); + let result = self.client.room_send(&room_id, content, None).await; + + match result { + Err(e) => println!("Error sending message: {}", e.to_string()), + Ok(_) => (), + } + } + } +} + +#[derive(Error, Debug)] +pub enum BotError { + /// Sync token couldn't be found. + #[error("the sync token could not be retrieved")] + SyncTokenRequired, +} + +/// Run the matrix dice bot until program terminated, or a panic occurs. +/// Originally adapted from the matrix-rust-sdk command bot example. +pub async fn run_bot(config: MatrixConfig) -> Result<(), Box> { + let homeserver_url = config.home_server; + let username = config.username; + let password = config.password; + + let mut cache_dir = dirs::cache_dir().expect("no cache directory found"); + cache_dir.push("matrix-dicebot"); + + //If the local json store has not been created yet, we need to do a single initial sync. + //It stores data under username's localpart. + let should_sync = { + let mut cache = cache_dir.clone(); + cache.push(username.clone()); + !cache.exists() + }; + + let store = JsonStore::open(&cache_dir)?; + let client_config = ClientConfig::new().state_store(Box::new(store)); + + let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); + let mut client = Client::new_with_config(homeserver_url, client_config).unwrap(); + + client + .login(&username, &password, None, Some("matrix dice bot")) + .await?; + + println!("Logged in as {}", username); + + if should_sync { + println!("Performing initial sync"); + client.sync(SyncSettings::default()).await?; + } + + //Attach event handler. + client + .add_event_emitter(Box::new(DiceBot::new(client.clone()))) + .await; + + let token = client + .sync_token() + .await + .ok_or(BotError::SyncTokenRequired)?; + let settings = SyncSettings::default().token(token); + + //this keeps state from the server streaming in to the dice bot via the EventEmitter trait + println!("Listening for commands"); + client.sync_forever(settings, |_| async {}).await; + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs index ad467d1..5d6b456 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,5 @@ pub mod bot; pub mod cofd; pub mod commands; pub mod dice; -pub mod matrix; mod parser; pub mod roll; diff --git a/src/matrix.rs b/src/matrix.rs deleted file mode 100644 index 9aaa410..0000000 --- a/src/matrix.rs +++ /dev/null @@ -1,81 +0,0 @@ -use serde::{self, Deserialize, Serialize}; -use std::collections::HashMap; - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "msgtype")] -#[serde(rename = "m.notice")] -pub struct NoticeMessage { - pub body: String, - - #[serde(default)] - pub format: Option, - - #[serde(default)] - pub formatted_body: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "msgtype")] -#[serde(rename = "m.text")] -pub struct TextMessage { - body: String, - - #[serde(default)] - format: Option, - - #[serde(default)] - formatted_body: Option, -} - -impl TextMessage { - pub fn body(&self) -> &str { - &self.body - } -} - -// Need untagged because redactions are blank -#[derive(Serialize, Deserialize, Debug)] -#[serde(untagged)] -pub enum MessageContent { - Text(TextMessage), - Other(serde_json::Value), -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct RoomEvent { - pub content: MessageContent, - pub event_id: String, - pub sender: String, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum Event { - #[serde(rename = "m.room.message")] - Room(RoomEvent), - - #[serde(other)] - Other, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Timeline { - pub events: Vec, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Room { - pub timeline: Timeline, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct Rooms { - pub invite: HashMap, - pub join: HashMap, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct SyncCommand { - pub next_batch: String, - pub rooms: Rooms, -}