diff --git a/.build.yml b/.build.yml index 5077f99..5ff5f34 100644 --- a/.build.yml +++ b/.build.yml @@ -1,12 +1,17 @@ -image: alpine/latest +image: debian/bullseye packages: - curl + - lld + - gcc + - upx sources: - https://git.sr.ht/~cofob/matrix-dnsbot triggers: - action: email condition: failure to: failure@cofob.ru +artifacts: + - matrix-dnsbot/target/release/dnsbot tasks: - install: | curl https://sh.rustup.rs -sSf | sh -s -- -y -q --default-toolchain nightly --profile minimal @@ -18,3 +23,8 @@ tasks: source $HOME/.cargo/env cd matrix-dnsbot cargo test + - build: | + source $HOME/.cargo/env + cd matrix-dnsbot + cargo build --release + upx -9 target/release/dnsbot diff --git a/Cargo.lock b/Cargo.lock index 3730801..468bd7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -272,17 +272,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -671,7 +660,6 @@ dependencies = [ "log", "matrix-sdk", "matrix-sdk-sled", - "pretty_env_logger", "regex", "tokio", "tracing-subscriber", @@ -735,19 +723,6 @@ dependencies = [ "syn", ] -[[package]] -name = "env_logger" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "event-listener" version = "2.5.2" @@ -1077,15 +1052,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" -[[package]] -name = "humantime" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" -dependencies = [ - "quick-error", -] - [[package]] name = "hyper" version = "0.14.19" @@ -1763,16 +1729,6 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" -[[package]] -name = "pretty_env_logger" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" -dependencies = [ - "env_logger", - "log", -] - [[package]] name = "proc-macro-crate" version = "1.1.3" @@ -2363,15 +2319,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "termcolor" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" -dependencies = [ - "winapi-util", -] - [[package]] name = "thiserror" version = "1.0.31" @@ -2882,15 +2829,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 90fcaae..d497816 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ anyhow = "^1" regex = "^1" lazy_static = "^1.4" tracing-subscriber = "^0.3" -pretty_env_logger = "^0.4" async-std-resolver = "^0.21" [profile.dev] diff --git a/src/main.rs b/src/main.rs index c31c659..5c3cfae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ extern crate dotenv; +#[macro_use] extern crate log; + use std::env; use std::time::Instant; use dotenv::dotenv; @@ -13,7 +15,6 @@ use matrix_sdk::{ }, Client, }; use tokio::time::{sleep, Duration}; -use log::{info, debug}; use regex::Regex; use lazy_static::lazy_static; use async_std_resolver::resolver_from_system_conf; @@ -30,79 +31,90 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) { }; msg_body = msg_body.to_lowercase(); + let mut i: u8 = 0; - if msg_body.contains("resolve help") { - let content = RoomMessageEventContent::text_html( - "Just send 'resolve (domain)'. 'resolve source' to get the code.", - "Just send resolve (domain). resolve source to get the code."); - room.send(content, None).await.unwrap(); - } else if msg_body.contains("resolve source") { - let content = RoomMessageEventContent::text_plain("https://git.sr.ht/~cofob/matrix-dnsbot"); - room.send(content, None).await.unwrap(); - } else if msg_body.contains("resolve") { - let caps = RE.captures(&msg_body); - match caps { - Some(cap) => { - let start = Instant::now(); - let domain = cap.get(1).unwrap().as_str(); - let resolver = resolver_from_system_conf().await.unwrap(); - let lookup = resolver.lookup_ip(domain).await; - if lookup.is_ok() { - let lookup = lookup.unwrap(); - if lookup.iter().count() == 0 { - let content = RoomMessageEventContent::text_plain("empty"); - room.send(content, None).await.unwrap(); - return; - } - - let mut plain_answer = String::from("IP record"); - let mut answer = String::from("IP record"); - - if lookup.iter().count() > 1 { - // lets goooo shitcode - plain_answer.push_str("s for "); - plain_answer.push_str(domain); - plain_answer.push(':'); - answer.push_str("s for "); - answer.push_str(domain); - answer.push_str(":"); - } else { - plain_answer.push_str(" for "); - plain_answer.push_str(domain); - plain_answer.push(':'); - answer.push_str(" for "); - answer.push_str(domain); - answer.push_str(":"); - } - - for ip in lookup { - plain_answer.push_str(" '"); - plain_answer.push_str(&ip.to_string()); - plain_answer.push_str("';"); - answer.push_str(" "); - answer.push_str(&ip.to_string()); - answer.push_str(";"); - } - let end = Instant::now(); - let delta = end - start; - plain_answer.push_str(" That took "); - plain_answer.push_str(&delta.as_millis().to_string()); - plain_answer.push_str(" ms."); - answer.push_str(" That took "); - answer.push_str(&delta.as_millis().to_string()); - answer.push_str(" ms."); - let content = RoomMessageEventContent::text_html(plain_answer, answer); - room.send(content, None).await.unwrap(); - } else { - let content = RoomMessageEventContent::text_plain("Unexpected error occured. Most likely, the domain simply does not have IP records."); - room.send(content, None).await.unwrap(); - } - } - _ => debug!("regex not found"), + for line in msg_body.rsplit('\n') { + if line.starts_with('>') { + continue + } + + if line.contains("resolve help") { + let content = RoomMessageEventContent::text_html( + "Just send 'resolve (domain)'. 'resolve source' to get the code.", + "Just send resolve (domain). resolve source to get the code."); + room.send(content, None).await.unwrap(); + } else if line.contains("resolve source") { + let content = RoomMessageEventContent::text_plain("https://git.sr.ht/~cofob/matrix-dnsbot"); + room.send(content, None).await.unwrap(); + } else if line.contains("resolve") { + i += 1; + if i >= 6 { + return; + } + let caps = RE.captures(&line); + match caps { + Some(cap) => { + let start = Instant::now(); + let domain = cap.get(1).unwrap().as_str(); + let resolver = resolver_from_system_conf().await.unwrap(); + let lookup = resolver.lookup_ip(domain).await; + if lookup.is_ok() { + let lookup = lookup.unwrap(); + if lookup.iter().count() == 0 { + let content = RoomMessageEventContent::text_plain("empty"); + room.send(content, None).await.unwrap(); + return; + } + + let mut plain_answer = String::from("IP record"); + let mut answer = String::from("IP record"); + + if lookup.iter().count() > 1 { + // lets goooo shitcode + plain_answer.push_str("s for "); + plain_answer.push_str(domain); + plain_answer.push(':'); + answer.push_str("s for "); + answer.push_str(domain); + answer.push_str(":"); + } else { + plain_answer.push_str(" for "); + plain_answer.push_str(domain); + plain_answer.push(':'); + answer.push_str(" for "); + answer.push_str(domain); + answer.push_str(":"); + } + + for ip in lookup { + plain_answer.push_str(" `"); + plain_answer.push_str(&ip.to_string()); + plain_answer.push_str("`;"); + answer.push_str(" "); + answer.push_str(&ip.to_string()); + answer.push_str(";"); + } + let end = Instant::now(); + let delta = end - start; + plain_answer.push_str(" That took "); + plain_answer.push_str(&delta.as_millis().to_string()); + plain_answer.push_str(" ms."); + answer.push_str(" That took "); + answer.push_str(&delta.as_millis().to_string()); + answer.push_str(" ms."); + let content = RoomMessageEventContent::text_html(plain_answer, answer); + room.send(content, None).await.unwrap(); + } else { + let content = RoomMessageEventContent::text_plain("Unexpected error occured. Most likely, the domain simply does not have IP records."); + room.send(content, None).await.unwrap(); + } + } + _ => debug!("regex not found"), + } + } else if room.is_direct() { + let content = RoomMessageEventContent::text_plain("I dont understand you! Send `resolve help` to get help."); + room.send(content, None).await.unwrap(); } - } else if room.is_direct() { - let content = RoomMessageEventContent::text_plain("I dont understand you! Send `resolve help` to get help."); - room.send(content, None).await.unwrap(); } } }