Update dependencies and cleanup the code

This commit is contained in:
lemon-sh 2021-08-27 14:30:39 +02:00
parent 20eab3e3b0
commit 6e092b2093
6 changed files with 264 additions and 133 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
.idea
/target

310
Cargo.lock generated
View file

@ -1,19 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "addr2line"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
version = 3
[[package]]
name = "aho-corasick"
@ -24,33 +11,67 @@ dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]]
name = "anyhow"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28ae2b3dec75a406790005a200b1bd89785afc02517a00ca99ecfe093ee9e6cf"
[[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.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "backtrace"
version = "0.3.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88fb5a785d6b44fd9d6700935608639af1b8356de1e55d5f7c2740f4faa15d82"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "bindgen"
version = "0.56.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2da379dbebc0b76ef63ca68d8fc6e71c0f13e59432e0987e508c1820e6ab5239"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"clap",
"env_logger",
"lazy_static",
"lazycell",
"log",
"peeking_take_while",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"which",
]
[[package]]
name = "bitflags"
version = "1.2.1"
@ -75,6 +96,15 @@ version = "1.0.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd"
[[package]]
name = "cexpr"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27"
dependencies = [
"nom",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -94,6 +124,32 @@ dependencies = [
"winapi",
]
[[package]]
name = "clang-sys"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cf2cc85830eae84823884db23c5306442a6c3d5bfd3beb2f2a2c829faa1816"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "core-foundation"
version = "0.9.1"
@ -184,25 +240,16 @@ dependencies = [
]
[[package]]
name = "failure"
version = "0.1.8"
name = "env_logger"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
dependencies = [
"backtrace",
"failure_derive",
]
[[package]]
name = "failure_derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
@ -340,10 +387,10 @@ dependencies = [
]
[[package]]
name = "gimli"
version = "0.23.0"
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]]
name = "h2"
@ -419,6 +466,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05842d0d43232b23ccb7060ecb0f0626922c21f30012e97b767b30afd4a5d4b9"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.14.7"
@ -550,12 +603,28 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
[[package]]
name = "libloading"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f84d96438c15fcd6c3f244c8fce01d1e2b9c6b5623e9c711dc9286d8fc92d6a"
dependencies = [
"cfg-if",
"winapi",
]
[[package]]
name = "lock_api"
version = "0.4.4"
@ -592,16 +661,6 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "miniz_oxide"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
dependencies = [
"adler",
"autocfg",
]
[[package]]
name = "mio"
version = "0.7.11"
@ -642,6 +701,16 @@ dependencies = [
"tempfile",
]
[[package]]
name = "nom"
version = "5.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af"
dependencies = [
"memchr",
"version_check",
]
[[package]]
name = "ntapi"
version = "0.3.6"
@ -680,18 +749,35 @@ dependencies = [
"libc",
]
[[package]]
name = "object"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
[[package]]
name = "once_cell"
version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
[[package]]
name = "onig"
version = "6.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b16fd3c0e73b516af509c13c4ba76ec0c987ce20d78b38cff356b8d01fc6a6c0"
dependencies = [
"bitflags",
"lazy_static",
"libc",
"onig_sys",
]
[[package]]
name = "onig_sys"
version = "69.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fd9442a09e4fbd08d196ddf419b2c79a43c3a46c800320cc841d45c2449a240"
dependencies = [
"bindgen",
"cc",
"pkg-config",
]
[[package]]
name = "openssl"
version = "0.10.34"
@ -750,6 +836,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]]
name = "percent-encoding"
version = "2.1.0"
@ -941,10 +1033,10 @@ dependencies = [
]
[[package]]
name = "rustc-demangle"
version = "0.1.18"
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "ryu"
@ -1034,6 +1126,12 @@ dependencies = [
"serde",
]
[[package]]
name = "shlex"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2"
[[package]]
name = "signal-hook-registry"
version = "1.3.0"
@ -1065,6 +1163,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.71"
@ -1076,18 +1180,6 @@ 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.2.0"
@ -1102,6 +1194,24 @@ dependencies = [
"winapi",
]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "thiserror"
version = "1.0.24"
@ -1151,11 +1261,11 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
name = "titlebot"
version = "0.1.0"
dependencies = [
"failure",
"anyhow",
"futures",
"htmlescape",
"irc",
"regex",
"onig",
"reqwest",
"tokio",
]
@ -1285,6 +1395,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.2"
@ -1309,6 +1425,18 @@ version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbdbff6266a24120518560b5dc983096efb98462e51d0d68169895b237be3e5d"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "want"
version = "0.3.0"
@ -1403,6 +1531,15 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "which"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
dependencies = [
"libc",
]
[[package]]
name = "winapi"
version = "0.3.9"
@ -1419,6 +1556,15 @@ 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"

View file

@ -4,13 +4,14 @@ version = "0.1.0"
authors = ["Yash Karandikar <nerdstep710@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.release]
lto = true
[dependencies]
irc = "0.15.0"
tokio = { version = "1.5.0", features = ["full"] }
irc = "0.15"
tokio = { version = "1", features = ["full"] }
futures = "0.3"
failure = "0.1.8"
regex = "1"
anyhow = "1.0"
onig = "6.2"
reqwest = { version = "0.11", features = ["json"] }
htmlescape = "0.3.1"
htmlescape = "0.3"

View file

@ -1,7 +1,7 @@
nickname = "titlebot"
username = "titlebot"
server = "192.168.254.28"
port = 6667
channels = ["#main", "#no-normies", "#aksmp", "#aksmp-mc"]
use_tls = false
server = "karx.xyz"
port = 6697
use_tls = true
umodes = "+B"

View file

@ -2,25 +2,24 @@ use std::collections::HashMap;
use futures::prelude::*;
use irc::client::prelude::*;
use regex::Regex;
use onig::*;
use htmlescape::decode_html;
#[tokio::main]
async fn main() -> Result<(), failure::Error> {
async fn main() -> anyhow::Result<()> {
if let Some(filename) = std::env::args().nth(1) {
let config = Config::load(filename)?;
let mut client = Client::from_config(config).await?;
client.identify()?;
println!("Connected to IRC");
println!("Connected to IRC!");
let mut stream = client.stream()?;
let ure = Regex::new(r"https?://[www\.]*\w+\.\w+[/\S+]*").unwrap();
let titlefetch = Regex::new(r"<title>[\s\S]+</title>").unwrap();
let treplace = Regex::new(r"</?title>").unwrap();
let url_regex = Regex::new(r"https?://\w+\.\w+[/\S+]*").unwrap();
let title_regex = Regex::new(r"(?<=<title>)(.*)(?=</title>)").unwrap();
let mut cache: HashMap<String, String> = HashMap::new();
@ -28,47 +27,31 @@ async fn main() -> Result<(), failure::Error> {
print!("{}", message);
if let Command::PRIVMSG(channel, message) = message.command {
if message.contains(client.current_nickname()) {
client
.send_privmsg(&channel, "beep boop, i'm titlebot!")
.unwrap();
}
if ure.is_match(&message) {
if let Some(mat) = ure.find(&message) {
let slice = &message[mat.start()..mat.end()];
if let Some(entry) = cache.get(&slice.to_string()) {
let fin = format!("Title: {}", entry);
client.send_privmsg(&channel, fin)?;
} else {
let response = reqwest::get(slice).await;
if let Err(_e) = response {
client.send_privmsg(&channel, "Error: could not get title")?;
} else {
let body = response?.text().await?;
if let Some(title_mat) = titlefetch.find(&body) {
let title_raw = &body[title_mat.start()..title_mat.end()];
let result = treplace.replace_all(title_raw, "");
if let Ok(decoded) = decode_html(&result) {
let fin = format!("Title: {}", decoded.to_string());
client.send_privmsg(&channel, fin)?;
cache.insert(slice.to_string(), decoded.to_string());
} else {
// Send the raw result instead of decoding
let fin = format!("Title: {}", result.to_string());
client.send_privmsg(&channel, fin)?;
cache.insert(slice.to_string(), result.to_string());
}
}
if let Some(m) = url_regex.find(&message) {
let url = &message[m.0..m.1];
if let Some(entry) = cache.get(&url.to_string()) {
client.send_privmsg(&channel, format!("Title: {}", entry))?;
continue
}
match reqwest::get(url).await {
Err(_) => {
client.send_privmsg(&channel, "Error: Could not get title")?;
}
Ok(o) => {
let body = o.text().await?;
if let Some(tm) = title_regex.find(&body) {
let title_match = &body[tm.0..tm.1];
let result = decode_html(&title_match).unwrap_or(title_match.to_string());
client.send_privmsg(&channel, &result)?;
cache.insert(url.to_string(), result.to_string());
}
}
}
}
}
}
} else {
println!("Usage: titlebot <path to config.toml>")
}
Ok(())
}

View file

@ -1,5 +1,5 @@
[Unit]
Description=irc title bot
Description=IRC title bot
[Service]
Type=simple