diff --git a/Cargo.lock b/Cargo.lock index 16aaecf..10ddd41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,17 +17,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "ahash" -version = "0.7.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a824f2aa7e75a0c98c5a504fceb80649e9c35265d44525b5f94de4771a395cd" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - [[package]] name = "aho-corasick" version = "1.1.2" @@ -54,9 +43,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.8" +version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "628a8f9bd1e24b4e0db2b4bc2d000b001e7dd032d54afa60a68836aeec5aa54a" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" dependencies = [ "anstyle", "anstyle-parse", @@ -68,9 +57,9 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" +checksum = "2faccea4cc4ab4a667ce676a30e8ec13922a692c99bb8f5b11f1502c72e04220" [[package]] name = "anstyle-parse" @@ -132,12 +121,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - [[package]] name = "base64" version = "0.21.7" @@ -155,6 +138,9 @@ name = "bitflags" version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +dependencies = [ + "serde", +] [[package]] name = "block-buffer" @@ -200,16 +186,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.31" +version = "0.4.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" +checksum = "9f13690e35a5e4ace198e7beea2895d29f3a9cc55015fcebe6336bd2010af9eb" dependencies = [ "android-tzdata", "iana-time-zone", "js-sys", "num-traits", "wasm-bindgen", - "windows-targets 0.48.5", + "windows-targets 0.52.0", ] [[package]] @@ -247,11 +233,12 @@ checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" [[package]] name = "config" -version = "0.13.4" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23738e11972c7643e4ec947840fc463b6a571afcd3e735bdfce7d03c7a784aca" +checksum = "7328b20597b53c2454f0b1919720c25c7339051c02b72b7e05409e00b14132be" dependencies = [ "async-trait", + "convert_case", "json5", "lazy_static", "nom", @@ -264,6 +251,35 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "const-random" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aaf16c9c2c612020bcfd042e170f6e32de9b9d75adb5277cdbbd2e2c8c8299a" +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]] name = "core-foundation" version = "0.9.4" @@ -289,6 +305,12 @@ 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" @@ -321,9 +343,12 @@ dependencies = [ [[package]] name = "dlv-list" -version = "0.3.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" +checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" +dependencies = [ + "const-random", +] [[package]] name = "encoding_rs" @@ -481,9 +506,9 @@ checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "h2" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b553656127a00601c8ae5590fcfdc118e4083a7924b6cf4ffc1ea4b99dc429d7" +checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9" dependencies = [ "bytes", "fnv", @@ -500,12 +525,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.12.3" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -dependencies = [ - "ahash", -] +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" [[package]] name = "hashbrown" @@ -515,9 +537,9 @@ checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" [[package]] name = "hermit-abi" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" [[package]] name = "http" @@ -625,9 +647,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.1.0" +version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +checksum = "824b2ae422412366ba479e8111fd301f7b5faece8149317bb81925979a53f520" dependencies = [ "equivalent", "hashbrown 0.14.3", @@ -673,9 +695,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.152" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libsystemd-sys" @@ -818,9 +840,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "openssl" -version = "0.10.62" +version = "0.10.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cde4d2d9200ad5909f8dac647e29482e07c3a35de8a13fce7c9c7747ad9f671" +checksum = "15c9d69dd87a29568d4d017cfe8ec518706046a05184e5aea92d0af890b803c8" dependencies = [ "bitflags 2.4.2", "cfg-if", @@ -850,9 +872,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.98" +version = "0.9.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1665caf8ab2dc9aef43d1c0023bd904633a6a05cb30b0ad59bec2ae986e57a7" +checksum = "22e1bf214306098e4832460f797824c05d25aacdf896f64a985fb0fd992454ae" dependencies = [ "cc", "libc", @@ -862,12 +884,12 @@ dependencies = [ [[package]] name = "ordered-multimap" -version = "0.4.3" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" +checksum = "4ed8acf08e98e744e5384c8bc63ceb0364e68a6854187221c18df61c4797690e" dependencies = [ "dlv-list", - "hashbrown 0.12.3", + "hashbrown 0.13.2", ] [[package]] @@ -964,15 +986,15 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a" +checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb" [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] @@ -997,9 +1019,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.2" +version = "1.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" dependencies = [ "aho-corasick", "memchr", @@ -1009,9 +1031,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.3" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" dependencies = [ "aho-corasick", "memchr", @@ -1026,11 +1048,11 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "reqwest" -version = "0.11.23" +version = "0.11.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37b1ae8d9ac08420c66222fb9096fc5de435c3c48542bc5336c51892cffafb41" +checksum = "c6920094eb85afde5e4a138be3f2de8bbdf28000f0029e72c45025a56b042251" dependencies = [ - "base64 0.21.7", + "base64", "bytes", "encoding_rs", "futures-core", @@ -1048,9 +1070,11 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", + "rustls-pemfile", "serde", "serde_json", "serde_urlencoded", + "sync_wrapper", "system-configuration", "tokio", "tokio-native-tls", @@ -1064,20 +1088,21 @@ dependencies = [ [[package]] name = "ron" -version = "0.7.1" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" +checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" dependencies = [ - "base64 0.13.1", - "bitflags 1.3.2", + "base64", + "bitflags 2.4.2", "serde", + "serde_derive", ] [[package]] name = "rust-ini" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" +checksum = "7e2a3bcec1f113553ef1c88aae6c020a369d03d55b58de9869a0908930385091" dependencies = [ "cfg-if", "ordered-multimap", @@ -1091,9 +1116,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.30" +version = "0.38.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" +checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" dependencies = [ "bitflags 2.4.2", "errno", @@ -1102,6 +1127,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64", +] + [[package]] name = "ryu" version = "1.0.16" @@ -1159,18 +1193,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" +checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" +checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" dependencies = [ "proc-macro2", "quote", @@ -1179,15 +1213,24 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.111" +version = "1.0.113" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" +checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79" dependencies = [ "itoa", "ryu", "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1241,9 +1284,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.12.0" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2593d31f82ead8df961d8bd23a64c2ccf2eb5dd34b0a34bfb4dd54011c72009e" +checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" [[package]] name = "socket2" @@ -1272,6 +1315,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "system-configuration" version = "0.5.1" @@ -1310,7 +1359,7 @@ dependencies = [ [[package]] name = "telelog" -version = "0.1.7" +version = "0.1.8" dependencies = [ "chrono", "clap", @@ -1359,6 +1408,15 @@ dependencies = [ "syn", ] +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1430,13 +1488,38 @@ dependencies = [ [[package]] name = "toml" -version = "0.5.11" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6a4b9e8023eb94392d3dca65d717c53abc5dad49c07cb65bb8fcd87115fa325" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" dependencies = [ "serde", ] +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tower-service" version = "0.3.2" @@ -1482,9 +1565,9 @@ checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" [[package]] name = "unicode-bidi" -version = "0.3.14" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f2528f27a9eb2b21e69c95319b30bd0efd85d09c379741b0f78ea1d86be2416" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" [[package]] name = "unicode-ident" @@ -1501,6 +1584,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + [[package]] name = "url" version = "2.5.0" @@ -1768,6 +1857,15 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "winnow" +version = "0.5.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "818ce546a11a9986bc24f93d0cdf38a8a1a400f1473ea8c82e59f6e0ffab9249" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.50.0" diff --git a/Cargo.toml b/Cargo.toml index 73fc760..3b5fe30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "telelog" -version = "0.1.7" +version = "0.1.8" edition = "2021" [dependencies] chrono = "0.4.31" clap = { version = "4.4.18", features = [ "cargo" ] } -config = "0.13.4" +config = "0.14.0" lazy_static = "1.4.0" regex = "1.10.2" reqwest = "0.11.23" diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..549abc0 --- /dev/null +++ b/src/helpers.rs @@ -0,0 +1,77 @@ +use crate::journal::LogEntry; + +pub fn generate_messages(buffer: &Vec) -> Vec { + let mut message_list: Vec = vec![]; + let mut current_message = String::from("\n"); + + for entry in buffer { + let new_entry_string = format!("{}[{}] {}: {}\n", colour_translate(entry.priority), entry.timestamp.format("%b %d %H:%M:%S"), entry.identifier, entry.message); + + if current_message.len() + new_entry_string.len() >= 4088 { + current_message.push_str(""); + message_list.push(current_message); + current_message = String::from("\n"); + } + + current_message.push_str(&new_entry_string); + } + if current_message != "\n" { + current_message.push_str(""); + message_list.push(current_message); + } + return message_list +} + +/// Flatten multiple message lists of sparse length to a single list of messages at maximum length +pub fn flatten_messages(message_lists: [&Vec; 2]) -> Vec { + // if two or more messages can be concatenated and still be within the size limit, do it + let mut flattened_messages: Vec = Vec::new(); + + for list in message_lists { + for message in list { + if flattened_messages.len() == 0 { + flattened_messages.push(message.clone()); + continue + } + + if flattened_messages.last().unwrap().len() + message.len() < 4108 { + let mut previous_message: String = flattened_messages.pop().unwrap(); + + previous_message = previous_message + .strip_suffix("") + .expect("last message did not end with ") + .to_string(); + + previous_message.push_str(message.strip_prefix("").unwrap_or("")); + flattened_messages.push(previous_message); + } + } + } + + return flattened_messages +} + +pub fn colour_translate(priority: u8) -> String { + // match priority { + // 0 => "#FF3333".to_owned(), + // 1 => "#FF6600".to_owned(), + // 2 => "#800080".to_owned(), + // 3 => "#B22222".to_owned(), + // 4 => "#FFD700".to_owned(), + // 5 => "#87CEEB".to_owned(), + // 6 => "#4169E1".to_owned(), + // 7 => "#CDD1D3".to_owned(), + // _ => "#000000".to_owned(), + // } + match priority { + 0 => "☢️".to_owned(), + 1 => "‼️".to_owned(), + 2 => "🟣".to_owned(), + 3 => "⭕️".to_owned(), + 4 => "🟡".to_owned(), + 5 => "🔵".to_owned(), + 6 => "⚫️".to_owned(), + 7 => "⚪️".to_owned(), + _ => "⚪️".to_owned(), + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 277db49..60c85ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ use filter::filter_log_entry; use tokio::sync::mpsc; mod telegram; +mod helpers; async fn process_entry(entry: sysjournal::JournalRecord, telegram_tx: &mpsc::Sender) { match parse_message(entry) { diff --git a/src/telegram.rs b/src/telegram.rs index 5fde95e..aad346f 100644 --- a/src/telegram.rs +++ b/src/telegram.rs @@ -1,14 +1,14 @@ use lazy_static::lazy_static; use signal_hook::{consts::{SIGTERM,SIGINT}, iterator::Signals}; use tokio::time::sleep; -use tokio::sync::{Mutex as AsyncMutex, mpsc}; +use tokio::sync::{Mutex as AsyncMutex, mpsc, Notify}; use reqwest; use std::time::Duration; use serde::Deserialize; use serde_json::Error as JsonError; use std::sync::OnceLock; -use crate::journal::LogEntry; +use crate::{helpers::*, journal::LogEntry}; use crate::config::AppSettings; #[derive(Debug)] @@ -23,6 +23,8 @@ lazy_static!( static ref PROCESSED_MESSAGE_BUFFER: AsyncMutex> = AsyncMutex::new(Vec::new()); static ref SEND_LOCK: AsyncMutex<()> = AsyncMutex::new(()); static ref REQUEST_CLIENT: reqwest::Client = reqwest::Client::new(); + static ref RETRY_FLAG: Notify = Notify::new(); + static ref RETRY_COUNT: AsyncMutex = AsyncMutex::new(1); ); static TELEGRAM_CONTEXT: OnceLock = OnceLock::new(); @@ -40,12 +42,14 @@ struct RetryParameters { } pub fn init(settings: AppSettings, mut rx: mpsc::Receiver) { + let flush_seconds = settings.telegram.flush_seconds.unwrap_or(5); TELEGRAM_CONTEXT.set(TelegramContext { chat_id: settings.telegram.chat_id, api_key: settings.telegram.api_key.unwrap(), - flush_seconds: settings.telegram.flush_seconds.unwrap_or(5), + flush_seconds: flush_seconds, }).expect("Initialisation only occurs once"); + // task to handle incoming signals tokio::spawn(async move { let mut signals = Signals::new(&[SIGTERM, SIGINT]).unwrap(); for signal in signals.forever() { @@ -89,54 +93,22 @@ pub fn init(settings: AppSettings, mut rx: mpsc::Receiver) { } }); - println!("[telegram] initialised"); -} - -fn colour_translate(priority: u8) -> String { - // match priority { - // 0 => "#FF3333".to_owned(), - // 1 => "#FF6600".to_owned(), - // 2 => "#800080".to_owned(), - // 3 => "#B22222".to_owned(), - // 4 => "#FFD700".to_owned(), - // 5 => "#87CEEB".to_owned(), - // 6 => "#4169E1".to_owned(), - // 7 => "#CDD1D3".to_owned(), - // _ => "#000000".to_owned(), - // } - match priority { - 0 => "☢️".to_owned(), - 1 => "‼️".to_owned(), - 2 => "🟣".to_owned(), - 3 => "⭕️".to_owned(), - 4 => "🟡".to_owned(), - 5 => "🔵".to_owned(), - 6 => "⚫️".to_owned(), - 7 => "⚪️".to_owned(), - _ => "⚪️".to_owned(), - } -} - -fn generate_messages(buffer: &Vec) -> Vec { - let mut message_list: Vec = vec![]; - let mut current_message = String::from("\n"); - - for entry in buffer { - let new_entry_string = format!("{}[{}] {}: {}\n", colour_translate(entry.priority), entry.timestamp.format("%b %d %H:%M:%S"), entry.identifier, entry.message); - - if current_message.len() + new_entry_string.len() >= 4088 { - current_message.push_str(""); - message_list.push(current_message); - current_message = String::from("\n"); + // task to handle retrying failed flushes + tokio::spawn( async move { + loop { + RETRY_FLAG.notified().await; + let retry_count = RETRY_COUNT.lock().await; + if *retry_count > 5 { + // TODO: write to disk maybe? + }; + sleep(Duration::from_secs(*retry_count * 2 * flush_seconds as u64)).await; + tokio::spawn(async move { + flush_log_buffer().await; + }); } + }); - current_message.push_str(&new_entry_string); - } - if current_message != "\n" { - current_message.push_str(""); - message_list.push(current_message); - } - return message_list + println!("[telegram] initialised"); } async fn send_telegram_message(message: &String, api_key: &String, chat_id: &String) -> Result { @@ -174,58 +146,64 @@ async fn flush_log_buffer() { }; let mut old_unsent_messages = PROCESSED_MESSAGE_BUFFER.lock().await; - let mut new_unsent_messages: Vec = Vec::new(); + let mut failed_unsent_messages: Vec = Vec::new(); + + let all_unsent_messages = flatten_messages([&old_unsent_messages, &message_list]); // try sending all the messages - for buffer in [&old_unsent_messages, &message_list] { - for message in buffer { - let result = send_telegram_message(message, &api_key, &chat_id).await; + for message in all_unsent_messages { + let result = send_telegram_message(&message, &api_key, &chat_id).await; - if let Err(e) = result { - eprintln!("[telegram] Failed: {}", e); - new_unsent_messages.push(message.to_string()); - continue - } + if let Err(e) = result { + eprintln!("[telegram] Failed: {}", e); + failed_unsent_messages.push(message.to_string()); + continue + } + + let response = result.unwrap(); - let response = result.unwrap(); - - if !response.status().is_success() { - let status = response.status(); - let text = response.text().await.unwrap(); - - // Error handling specifics - match status.as_u16() { - 429 => { - match serde_json::from_str(&text) as Result { - Ok(error_response) => { - if let Some(parameters) = error_response.parameters { - if let Some(retry_after) = parameters.retry_after { - eprintln!("[telegram] API response 429: pausing messages for {} seconds", retry_after); - tokio::spawn(async move { - let _guard = SEND_LOCK.lock().await; - sleep(Duration::from_secs(retry_after)).await; - drop(_guard); - }); - } + if !response.status().is_success() { + let status = response.status(); + let text = response.text().await.unwrap(); + + // Error handling specifics + match status.as_u16() { + 429 => { + match serde_json::from_str(&text) as Result { + Ok(error_response) => { + if let Some(parameters) = error_response.parameters { + if let Some(retry_after) = parameters.retry_after { + eprintln!("[telegram] API response 429: pausing messages for {} seconds", retry_after); + tokio::spawn(async move { + let _guard = SEND_LOCK.lock().await; + sleep(Duration::from_secs(retry_after)).await; + drop(_guard); + }); } } - Err(e) => { - eprintln!("[telegram] Failed to parse 429 response: {}", e); - } } - - new_unsent_messages.push(message.to_string()); - }, - _ => { - println!("[telegram] API response {}: {:?}", status, text); - // new_unsent_messages.push(message.to_string()); + Err(e) => { + eprintln!("[telegram] Failed to parse 429 response: {}", e); + } } + + failed_unsent_messages.push(message.to_string()); + }, + _ => { + println!("[telegram] API response {}: {:?}", status, text); + failed_unsent_messages.push(message.to_string()); } } } } + + let mut retry_count = RETRY_COUNT.lock().await; + if failed_unsent_messages.len() > 0 { + *retry_count *= 2; + RETRY_FLAG.notify_one(); + } else { + *retry_count = 1; + } - *old_unsent_messages = new_unsent_messages; - - drop(old_unsent_messages); + *old_unsent_messages = failed_unsent_messages; }