Compare commits

...

4 commits

Author SHA1 Message Date
sinavir
63208ed6a6 feat: package 2024-05-22 16:59:36 +02:00
sinavir
435a85732c chore: clippy 2024-05-22 16:59:36 +02:00
sinavir
a98da9d47c feat: Add retry for signal socket connection 2024-05-18 20:46:29 +02:00
sinavir
a13272684d feat: Refactor config system 2024-05-18 20:46:29 +02:00
11 changed files with 607 additions and 69 deletions

2
.gitignore vendored
View file

@ -1,6 +1,8 @@
.config .config
/www /www
config.toml
# Added by cargo # Added by cargo

394
Cargo.lock generated
View file

@ -79,6 +79,12 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "base64"
version = "0.21.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
[[package]] [[package]]
name = "beef" name = "beef"
version = "0.5.2" version = "0.5.2"
@ -88,6 +94,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "bimap"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "230c5f1ca6a325a32553f8640d31ac9b49f2411e901e427570154868b46da4f7"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -99,6 +114,18 @@ name = "bitflags"
version = "2.5.0" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
dependencies = [
"serde",
]
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
@ -136,6 +163,55 @@ dependencies = [
"windows-targets 0.52.5", "windows-targets 0.52.5",
] ]
[[package]]
name = "config"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7328b20597b53c2454f0b1919720c25c7339051c02b72b7e05409e00b14132be"
dependencies = [
"async-trait",
"convert_case",
"json5",
"lazy_static",
"nom",
"pathdiff",
"ron",
"rust-ini",
"serde",
"serde_json",
"toml 0.8.12",
"yaml-rust",
]
[[package]]
name = "const-random"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359"
dependencies = [
"const-random-macro",
]
[[package]]
name = "const-random-macro"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e"
dependencies = [
"getrandom",
"once_cell",
"tiny-keccak",
]
[[package]]
name = "convert_case"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca"
dependencies = [
"unicode-segmentation",
]
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.4" version = "0.9.4"
@ -152,6 +228,50 @@ version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "cpufeatures"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504"
dependencies = [
"libc",
]
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "dlv-list"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f"
dependencies = [
"const-random",
]
[[package]] [[package]]
name = "encoding" name = "encoding"
version = "0.2.33" version = "0.2.33"
@ -361,12 +481,39 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.28.1" version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "hashbrown"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.3" version = "0.14.3"
@ -421,7 +568,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown 0.14.3",
] ]
[[package]] [[package]]
@ -445,7 +592,7 @@ dependencies = [
"tokio-native-tls", "tokio-native-tls",
"tokio-stream", "tokio-stream",
"tokio-util", "tokio-util",
"toml", "toml 0.7.8",
] ]
[[package]] [[package]]
@ -487,6 +634,17 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "json5"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1"
dependencies = [
"pest",
"pest_derive",
"serde",
]
[[package]] [[package]]
name = "jsonrpsee" name = "jsonrpsee"
version = "0.22.4" version = "0.22.4"
@ -559,6 +717,12 @@ version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.13" version = "0.4.13"
@ -587,6 +751,12 @@ version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.7.2" version = "0.7.2"
@ -625,6 +795,16 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.18" version = "0.2.18"
@ -703,6 +883,16 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "ordered-multimap"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ed8acf08e98e744e5384c8bc63ceb0364e68a6854187221c18df61c4797690e"
dependencies = [
"dlv-list",
"hashbrown 0.13.2",
]
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.1" version = "0.12.1"
@ -726,6 +916,57 @@ dependencies = [
"windows-targets 0.48.5", "windows-targets 0.48.5",
] ]
[[package]]
name = "pathdiff"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
[[package]]
name = "pest"
version = "2.7.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "560131c633294438da9f7c4b08189194b20946c8274c6b9e38881a7874dc8ee8"
dependencies = [
"memchr",
"thiserror",
"ucd-trie",
]
[[package]]
name = "pest_derive"
version = "2.7.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26293c9193fbca7b1a3bf9b79dc1e388e927e6cacaa78b4a3ab705a1d3d41459"
dependencies = [
"pest",
"pest_generator",
]
[[package]]
name = "pest_generator"
version = "2.7.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ec22af7d3fb470a85dd2ca96b7c577a1eb4ef6f1683a9fe9a8c16e136c04687"
dependencies = [
"pest",
"pest_meta",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pest_meta"
version = "2.7.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7a240022f37c361ec1878d646fc5b7d7c4d28d5946e1a80ad5a7a4f4ca0bdcd"
dependencies = [
"once_cell",
"pest",
"sha2",
]
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "1.1.5" version = "1.1.5"
@ -839,6 +1080,28 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
[[package]]
name = "ron"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94"
dependencies = [
"base64",
"bitflags 2.5.0",
"serde",
"serde_derive",
]
[[package]]
name = "rust-ini"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e2a3bcec1f113553ef1c88aae6c020a369d03d55b58de9869a0908930385091"
dependencies = [
"cfg-if",
"ordered-multimap",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.23" version = "0.1.23"
@ -910,18 +1173,27 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.199" version = "1.0.202"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a" checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde-toml-merge"
version = "1.0.199" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc" checksum = "e75a6b6c577215161d30ba4e13fb493c435a8b4140c442e590b61b4b2a27a226"
dependencies = [
"toml 0.8.12",
]
[[package]]
name = "serde_derive"
version = "1.0.202"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -948,6 +1220,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "sha2"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.1" version = "1.4.1"
@ -961,7 +1244,9 @@ dependencies = [
name = "signal-irc-bridge" name = "signal-irc-bridge"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bimap",
"bytes", "bytes",
"config",
"futures", "futures",
"futures-util", "futures-util",
"irc", "irc",
@ -969,11 +1254,13 @@ dependencies = [
"log", "log",
"pretty_env_logger", "pretty_env_logger",
"serde", "serde",
"serde_derive",
"serde_json", "serde_json",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tokio-util", "tokio-util",
"toml-env",
] ]
[[package]] [[package]]
@ -1053,6 +1340,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.37.0" version = "1.37.0"
@ -1130,6 +1426,31 @@ dependencies = [
"toml_edit 0.19.15", "toml_edit 0.19.15",
] ]
[[package]]
name = "toml"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit 0.22.12",
]
[[package]]
name = "toml-env"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a740df0c7ded7632d527f5e4015c28c19803c9b8d6e87cc7ae72ae4d68d6f4b0"
dependencies = [
"log",
"serde",
"serde-toml-merge",
"thiserror",
"toml 0.8.12",
]
[[package]] [[package]]
name = "toml_datetime" name = "toml_datetime"
version = "0.6.5" version = "0.6.5"
@ -1149,7 +1470,7 @@ dependencies = [
"serde", "serde",
"serde_spanned", "serde_spanned",
"toml_datetime", "toml_datetime",
"winnow", "winnow 0.5.40",
] ]
[[package]] [[package]]
@ -1160,7 +1481,20 @@ checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"toml_datetime", "toml_datetime",
"winnow", "winnow 0.5.40",
]
[[package]]
name = "toml_edit"
version = "0.22.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3328d4f68a705b2a4498da1d580585d39a6510f98318a2cec3018a7ec61ddef"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow 0.6.8",
] ]
[[package]] [[package]]
@ -1194,18 +1528,42 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "ucd-trie"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-segmentation"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.15" version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
@ -1431,3 +1789,21 @@ checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "winnow"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3c52e9c97a68071b23e836c9380edae937f17b9c4667bd021973efc689f618d"
dependencies = [
"memchr",
]
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]

View file

@ -6,16 +6,20 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bimap = { version = "0.6.3", features = ["serde"] }
bytes = "1.6.0" bytes = "1.6.0"
config = { version = "0.14.0", features = ["toml"] }
futures = "0.3.30" futures = "0.3.30"
futures-util = "0.3.30" futures-util = "0.3.30"
irc = "1.0.0" irc = "1.0.0"
jsonrpsee = { version = "0.22.4", features = ["macros", "async-client"] } jsonrpsee = { version = "0.22.4", features = ["macros", "async-client"] }
log = "0.4.21" log = "0.4.21"
pretty_env_logger = "0.5.0" pretty_env_logger = "0.5.0"
serde = "1.0.199" serde = { version = "1.0.199", features = ["std", "alloc", "derive", "serde_derive"] }
serde_derive = "1.0.202"
serde_json = "1.0.116" serde_json = "1.0.116"
thiserror = "1.0.59" thiserror = "1.0.59"
tokio = { version = "1.37.0", features = ["full"] } tokio = { version = "1.37.0", features = ["full"] }
tokio-stream = "0.1.15" tokio-stream = "0.1.15"
tokio-util = "0.7.10" tokio-util = "0.7.10"
toml-env = { version = "1.2.0", features = ["log"] }

4
default.nix Normal file
View file

@ -0,0 +1,4 @@
{
pkgs ? import (import ./npins).nixpkgs { },
}:
pkgs.callPackage ./package.nix {}

0
launch-signal-cli.sh Normal file → Executable file
View file

82
module.nix Normal file
View file

@ -0,0 +1,82 @@
{ pkgs, config, lib, ... }:
let
cfg = config.services.signal-irc-bridge;
mkSystemdRunOptions = opts: lib.escapeShellArgs (lib.mapAttrsToList (k: v: "-p${k}=${builtins.toString v}") opts);
commonServiceOptions = {
DynamicUser = true;
User = "signal-irc-client";
StateDirectory = "signal-cli";
RuntimeDirectory = "signal-cli";
PrivateDevices=true;
PrivateTmp=true;
ProtectControlGroups=true;
ProtectKernelTunables=true;
RestrictSUIDSGID=true;
ProtectSystem="strict";
ProtectKernelLogs=true;
ProtectProc="invisible";
PrivateUsers=true;
ProtectHome=true;
UMask="0077";
RuntimeDirectoryMode="0750";
StateDirectoryMode="0750";
};
signal-cli-bridge-wrapper = pkgs.writeShellApplication {
name = "signal-cli-bridge-wrapper";
text = ''
systemd-run ${mkSystemdRunOptions commonServiceOptions} --pty --pipe --unit="signal-cli-bridge" ${lib.getExe pkgs.signal-cli} --config "\''${STATE_DIRECTORY}"/signal-cli-config/ "$@"
'';
};
in {
options = {
services.signal-irc-bridge = {
enable = lib.mkEnableOption "signal-irc bridge";
package = lib.mkOption {
type = lib.types.package;
default = pkgs.signal-irc-bridge;
};
configFile = lib.mkOption {
type = lib.types.path;
description = "Path to the toml config file";
};
};
};
config = {
nixpkgs.overlays = [
(import ./overlay.nix)
];
systemd.services = lib.mkIf cfg.enable {
signal-irc-bridge = {
environment = {
CONFIG_PATH = cfg.configFile;
};
unitConfig = {
BindsTo = [ "signal-irc-bridge-signal-cli.service" ];
After = [ "signal-irc-bridge-signal-cli.service" ];
};
serviceConfig = commonServiceOptions // {
ExecStart = "${lib.getExe cfg.package}";
Restart = "always";
RestartSec= "5s";
StateDirectory = "signal-irc";
RuntimeDirectory = "signal-irc";
};
};
signal-irc-bridge-signal-cli = {
serviceConfig = commonServiceOptions // {
ExecStart = "${lib.getExe pkgs.signal-cli} --config \"\${STATE_DIRECTORY}\"/signal-cli-config/ daemon --socket \"\${RUNTIME_DIRECTORY}\"/socket --receive-mode=manual";
Restart = "always";
RestartSec= "5s";
};
};
};
environment.systemPackages = lib.mkIf cfg.enable [
signal-cli-bridge-wrapper
];
};
}

3
overlay.nix Normal file
View file

@ -0,0 +1,3 @@
final: prev: {
signal-irc-bridge = final.callPackage ./package.nix {};
}

25
package.nix Normal file
View file

@ -0,0 +1,25 @@
{ lib, openssl, pkg-config, rustPlatform }:
rustPlatform.buildRustPackage rec {
pname = "signal-irc-bridge";
version = "0.1";
src =
with lib.fileset;
toSource {
root = ./.;
fileset =
intersection (gitTracked ./.) (fileFilter (file: !file.hasExt "nix") ./.);
};
nativeBuildInputs = [
pkg-config
];
buildInputs = [
openssl
];
cargoHash = "sha256-J7+o6krHuK3CwwOIcDfm0s6F0cmviFQhSHpdKpXsa/g=";
}

39
src/config.rs Normal file
View file

@ -0,0 +1,39 @@
use std::env;
use std::path::Path;
use bimap::BiMap;
use irc::client::prelude::Config as IrcConfig;
use serde::{Deserialize, Serialize};
use toml_env::{initialize, Args, AutoMapEnvArgs, Logging};
#[derive(Debug, Deserialize, Serialize)]
pub struct BridgeConfig {
pub irc: IrcConfig,
pub signal: SignalConfig,
pub mapping: BiMap<String, String>,
}
impl BridgeConfig {
pub fn load() -> Result<BridgeConfig, String> {
initialize(Args {
config_path: env::var("CONFIG_PATH").ok().as_ref().map(Path::new),
logging: Logging::StdOut,
auto_map_env: Some(AutoMapEnvArgs {
divider: "__",
prefix: Some("BRIDGE"),
transform: Box::new(|name| name.to_lowercase()),
}),
..Args::default()
})
.map_err(|e| format!("failed to deserialize config: {e}"))
.map(|e| e.unwrap())
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SignalConfig {
pub media_dir: Box<Path>,
pub signal_cli_dir: Box<Path>,
pub socket: Box<Path>,
pub url_root: String,
}

View file

@ -1,7 +1,7 @@
mod jsonrpc; mod jsonrpc;
mod transform; mod transform;
mod transports; mod transports;
use std::path::Path; mod config;
use futures::future::try_join; use futures::future::try_join;
use futures::prelude::*; use futures::prelude::*;
@ -9,28 +9,33 @@ use irc::client::prelude::*;
use irc::client::ClientStream; use irc::client::ClientStream;
use jsonrpsee::core::client::{Subscription, SubscriptionClientT}; use jsonrpsee::core::client::{Subscription, SubscriptionClientT};
use serde_json::Value; use serde_json::Value;
use tokio::time::sleep;
use core::time::Duration;
use transform::Bridge; use transform::Bridge;
use {log, pretty_env_logger};
use crate::jsonrpc::RpcClient; use crate::jsonrpc::RpcClient;
use crate::config::BridgeConfig;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), String> { async fn main() -> Result<(), String> {
pretty_env_logger::init(); pretty_env_logger::init();
log::info!("Starting..."); log::info!("Starting...");
let config = Config::load("./config.toml").map_err(|e| format!("Error while loading config: {e}"))?; let config = BridgeConfig::load()?;
check_config(&config);
// Initialisation of components. TODO separate module // Initialisation of components. TODO separate module
let init_irc = async { let init_irc = async {
let mut client = Client::from_config(config.clone()).await.unwrap(); let mut client = Client::from_config(config.irc.clone()).await.unwrap();
client.identify().unwrap(); client
.identify()
.map_err(|e| format!("Error while initializing irc client: {e}"))?;
let sender = client.sender(); let sender = client.sender();
let mut stream = client.stream().unwrap(); let mut stream = client
.stream()
.map_err(|e| format!("Error while initializing irc stream: {e}"))?;
log::debug!("Initializing IRC"); log::debug!("Initializing IRC");
while client.list_channels().is_some_and(|x| x.len() == 0) { while client.list_channels().is_some_and(|x| x.is_empty()) {
stream stream
.next() .next()
.await .await
@ -42,22 +47,34 @@ async fn main() -> Result<(), String> {
}; };
let init_signal = async { let init_signal = async {
log::debug!("Initializing Signal"); log::debug!("Initializing Signal");
let signal_client = (async {
for i in 0..10 {
match jsonrpc::connect_unix(&config.signal.socket).await {
Ok(client) => {
return Ok(client);
}, Err(e) => {
log::info!("failed attempt {} connect to signal-cli socket: {:?}. Retrying", i, e);
},
}
sleep(Duration::from_secs(1)).await;
};
Err("Trying too many times to open socket")
let signal_client = jsonrpc::connect_unix(config.options.get("socket").unwrap()) }).await.map_err(|e| format!("failed to connect to signal socket: {e}"))?;
let signal_stream = signal_client
.subscribe_receive(None)
.await .await
.map_err(|e| format!("failed to connect to signal socket: {e}"))?; .map_err(|e| format!("failed to subscribe to signal msg: {e}"))?;
let signal_stream = signal_client.subscribe_receive(None).await.unwrap();
log::debug!("Initialized Signal"); log::debug!("Initialized Signal");
Ok((signal_stream, signal_client)) Ok((signal_stream, signal_client))
}; };
let ((sender, mut stream), (mut signal_stream, signal_client)) = let ((sender, mut stream), (mut signal_stream, signal_client)) =
try_join(init_irc, init_signal).await?; try_join(init_irc, init_signal).await?;
let bridge = Bridge::new( let bridge = Bridge::new(
&Path::new(config.options.get("media_dir").unwrap()), &config.signal.media_dir,
&Path::new(config.options.get("signal_cli_dir").unwrap()), &config.signal.signal_cli_dir,
&config.options.get("url_root").unwrap(), &config.signal.url_root,
&config.options.get("groupid").unwrap(), &config.mapping,
&config.channels[0],
); );
// Run !! // Run !!
@ -144,14 +161,3 @@ async fn handle_signal(
} }
} }
} }
fn check_config(config: &Config) -> bool {
let keys = vec![
"groupid",
"media_dir",
"signal_cli_dir",
"socket",
"url_root",
];
keys.into_iter().fold(config.channels.len() >= 1, |b, k| b && config.options.contains_key(k))
}

View file

@ -1,40 +1,39 @@
use std::path::Path; use std::path::Path;
use bimap::BiMap;
use irc::client::prelude::*; use irc::client::prelude::*;
use serde_json::{Map, Value}; use serde_json::{Map, Value};
use tokio::fs::copy; use tokio::fs::copy;
#[derive(Default)] #[derive(Default)]
struct SignalMessageBuilder { struct SignalMessageBuilder<'a> {
from: Option<String>, from: Option<String>,
message: Option<String>, message: Option<String>,
attachments_urls: Vec<String>, attachments_urls: Vec<String>,
valid: bool, target: Option<&'a String>,
} }
impl SignalMessageBuilder { impl<'b> SignalMessageBuilder<'b> {
pub fn message(self: &mut Self, message: String) { pub fn message(&mut self, message: String) {
self.message = Some(message); self.message = Some(message);
} }
pub fn from(self: &mut Self, from: String) { pub fn from(&mut self, from: String) {
self.from = Some(from); self.from = Some(from);
} }
pub fn attach(self: &mut Self, attach: Vec<String>) { pub fn attach(&mut self, attach: Vec<String>) {
self.attachments_urls.extend_from_slice(&attach); self.attachments_urls.extend_from_slice(&attach);
} }
pub fn valid(self: &mut Self, v: bool) { pub fn target(&mut self, channel: Option<&'b String>) {
self.valid = v; self.target = channel;
} }
pub fn build(self: &Self) -> Option<String> { pub fn build(self) -> Option<(&'b String, String)>
if !self.valid { {
return None;
}
let attachments_text = self.attachments_urls.join(", "); let attachments_text = self.attachments_urls.join(", ");
match (&self.message, attachments_text.len()) { (match (&self.message, attachments_text.len()) {
(None, 0) => None, (None, 0) => None,
(None, _) => self (None, _) => self
.from .from
@ -45,7 +44,7 @@ impl SignalMessageBuilder {
.from .from
.as_ref() .as_ref()
.map(|from| format!("<{from}>: {m} ({attachments_text})")), .map(|from| format!("<{from}>: {m} ({attachments_text})")),
} }).and_then(|m| self.target.map(|e| (e, m)))
} }
} }
@ -53,8 +52,7 @@ pub struct Bridge<'a> {
media_dir: &'a Path, media_dir: &'a Path,
signalcli_dir: &'a Path, signalcli_dir: &'a Path,
url_root: &'a str, url_root: &'a str,
groupid: &'a str, mapping: &'a BiMap<String, String>,
irc_channel: &'a str,
} }
impl<'a> Bridge<'a> { impl<'a> Bridge<'a> {
@ -62,19 +60,17 @@ impl<'a> Bridge<'a> {
media_dir: &'a Path, media_dir: &'a Path,
signalcli_dir: &'a Path, signalcli_dir: &'a Path,
url_root: &'a str, url_root: &'a str,
groupid: &'a str, mapping: &'a BiMap<String, String>,
irc_channel: &'a str,
) -> Self { ) -> Self {
Self { Self {
media_dir, media_dir,
signalcli_dir, signalcli_dir,
url_root, url_root,
groupid, mapping,
irc_channel,
} }
} }
async fn handle_attachment(self: &Self, attachment: &Value) -> Option<String> { async fn handle_attachment(&self, attachment: &Value) -> Option<String> {
if let Value::Object(a) = attachment { if let Value::Object(a) = attachment {
if let Some(Value::String(fname)) = a.get("id") { if let Some(Value::String(fname)) = a.get("id") {
match copy( match copy(
@ -98,9 +94,9 @@ impl<'a> Bridge<'a> {
} }
async fn process_signal( async fn process_signal(
self: &Self, &self,
message: &Map<String, Value>, message: &Map<String, Value>,
result: &mut SignalMessageBuilder, result: &mut SignalMessageBuilder<'a>,
) { ) {
if let Some(Value::Array(attachments)) = message.get("attachments") { if let Some(Value::Array(attachments)) = message.get("attachments") {
let mut attachments_urls = Vec::with_capacity(attachments.len()); let mut attachments_urls = Vec::with_capacity(attachments.len());
@ -118,12 +114,12 @@ impl<'a> Bridge<'a> {
} }
if let Some(Value::Object(group_info)) = message.get("groupInfo") { if let Some(Value::Object(group_info)) = message.get("groupInfo") {
if let Some(Value::String(groupid)) = group_info.get("groupId") { if let Some(Value::String(groupid)) = group_info.get("groupId") {
result.valid(groupid == self.groupid); result.target(self.mapping.get_by_right(groupid));
} }
} }
} }
pub async fn signal2irc(self: &Self, signal: Value) -> Option<(&str, String)> { pub async fn signal2irc(&self, signal: Value) -> Option<(&String, String)> {
let mut result: SignalMessageBuilder = SignalMessageBuilder::default(); let mut result: SignalMessageBuilder = SignalMessageBuilder::default();
if let Value::Object(map) = signal { if let Value::Object(map) = signal {
if let Some(Value::Object(envelope)) = map.get("envelope") { if let Some(Value::Object(envelope)) = map.get("envelope") {
@ -140,18 +136,19 @@ impl<'a> Bridge<'a> {
} }
} }
} }
result.build().map(|e| (self.irc_channel, e)) result.build()
} }
pub fn irc2signal(self: &Self, message: Message) -> Option<(&str, String)> { pub fn irc2signal(&self, message: Message) -> Option<(&String, String)> {
match (message.prefix, message.command) { match (message.prefix, message.command) {
(Some(Prefix::Nickname(from, _, _)), Command::PRIVMSG(_, message)) => { (Some(Prefix::Nickname(from, _, _)), Command::PRIVMSG(channel, message)) => {
Some(format!("<{from}>: {message}")) Some((channel, format!("<{from}>: {message}")))
} }
(Some(Prefix::Nickname(from, _, _)), Command::NOTICE(_, message)) => { (Some(Prefix::Nickname(from, _, _)), Command::NOTICE(channel, message)) => {
Some(format!("📜 {from} {message}")) Some((channel, format!("📜 {from} {message}")))
} }
_ => None, _ => None,
}.map(|e| (self.groupid, e)) }
.and_then(|(channel, e)| self.mapping.get_by_left(&channel).map(|c| (c, e)))
} }
} }