initial commit

This commit is contained in:
2026-03-18 00:11:49 +03:00
commit 7566b2d1c6
4 changed files with 921 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

575
Cargo.lock generated Normal file
View File

@@ -0,0 +1,575 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "ahash"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bitflags"
version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
[[package]]
name = "bumpalo"
version = "3.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb"
[[package]]
name = "cc"
version = "1.2.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a0dd1ca384932ff3641c8718a02769f1698e7563dc6974ffd03346116310423"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chrono"
version = "0.4.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
dependencies = [
"iana-time-zone",
"js-sys",
"num-traits",
"serde",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "clap"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1110bd8a634a1ab8cb04345d8d878267d57c3cf1b38d91b71af6686408bbca6a"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "fallible-iterator"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
dependencies = [
"ahash",
]
[[package]]
name = "hashlink"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af"
dependencies = [
"hashbrown",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "iana-time-zone"
version = "0.1.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "js-sys"
version = "0.3.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b49715b7073f385ba4bc528e5747d02e66cb39c6146efb66b781f131f0fb399c"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.183"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
[[package]]
name = "libsqlite3-sys"
version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c10584274047cb335c23d3e61bcef8e323adae7c5c8c760540f73610177fc3f"
dependencies = [
"cc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "log"
version = "0.4.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "proc-macro2"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quassel-log-extractor"
version = "0.1.0"
dependencies = [
"anyhow",
"chrono",
"clap",
"rusqlite",
]
[[package]]
name = "quote"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rusqlite"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b838eba278d213a8beaf485bd313fd580ca4505a00d5871caeb1457c55322cae"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"smallvec",
]
[[package]]
name = "rustversion"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasm-bindgen"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6532f9a5c1ece3798cb1c2cfdba640b9b3ba884f5db45973a6f442510a87d38e"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18a2d50fcf105fb33bb15f00e7a77b772945a2ee45dcf454961fd843e74c18e6"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03ce4caeaac547cdf713d280eda22a730824dd11e6b8c3ca9e42247b25c631e3"
dependencies = [
"bumpalo",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75a326b8c223ee17883a4251907455a2431acc2791c98c26279376490c378c16"
dependencies = [
"unicode-ident",
]
[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]]
name = "zerocopy"
version = "0.8.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2578b716f8a7a858b7f02d5bd870c14bf4ddbbcf3a4c05414ba6503640505e3"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e6cc098ea4d3bd6246687de65af3f920c430e236bee1e3bf2e441463f08a02f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

15
Cargo.toml Normal file
View File

@@ -0,0 +1,15 @@
[package]
name = "quassel-log-extractor"
version = "0.1.0"
edition = "2021"
description = "Extract chat logs from a Quassel Core SQLite database"
[[bin]]
name = "quassel-log-extractor"
path = "src/main.rs"
[dependencies]
rusqlite = { version = "0.31", features = ["bundled"] }
clap = { version = "4", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
anyhow = "1"

330
src/main.rs Normal file
View File

@@ -0,0 +1,330 @@
use anyhow::{Context, Result, bail};
use chrono::{DateTime, Local, TimeZone, Utc};
use clap::Parser;
use rusqlite::{Connection, params};
use std::collections::HashSet;
use std::fs;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
const MSG_PLAIN: u32 = 0x0001;
const MSG_ACTION: u32 = 0x0004;
const MSG_NICK: u32 = 0x0008;
const MSG_MODE: u32 = 0x0010;
const MSG_JOIN: u32 = 0x0020;
const MSG_PART: u32 = 0x0040;
const MSG_QUIT: u32 = 0x0080;
const MSG_KICK: u32 = 0x0100;
/// Extract chat logs from a Quassel Core SQLite database.
///
/// Always included: plain messages and /me actions.
/// Optionally included: join/part/quit/kick/nick-change/mode events (--include-events).
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path to the Quassel SQLite database file
#[arg(short = 'd', long)]
database: PathBuf,
/// Quassel user name
#[arg(short = 'u', long)]
user: String,
/// IRC network name (e.g. "Libera.Chat")
#[arg(short = 'n', long)]
network: String,
/// Channel / buffer name (e.g. "#rust")
#[arg(short = 'c', long)]
channel: String,
/// Start of time range, inclusive.
///
/// Accepted ISO 8601 formats:
/// 2024-01-15T20:00:00+02:00 (explicit UTC offset)
/// 2024-01-15T20:00:00Z (UTC)
/// 2024-01-15T20:00:00 (no offset → local time assumed)
/// 2024-01-15T20:00 (no offset, no seconds → local time assumed)
/// 2024-01-15 (date only → local midnight)
#[arg(long)]
start: String,
/// End of time range, inclusive (same formats as --start)
#[arg(long)]
end: String,
/// Output file path. Omit to write to stdout.
#[arg(short = 'o', long)]
output: Option<PathBuf>,
/// strftime-compatible format string for timestamps in the output.
/// Default: "%Y-%m-%d %H:%M:%S"
#[arg(long, default_value = "%Y-%m-%d %H:%M:%S")]
time_format: String,
/// Print timestamps in local time instead of UTC
#[arg(long)]
local_time: bool,
/// Prefix sender nicks with their channel status characters (e.g. "@nick", "+nick")
#[arg(long)]
sender_prefixes: bool,
/// Path to a plain-text file of nicks to exclude, one per line.
/// Blank lines and lines starting with '#' are ignored.
/// Matching is case-insensitive against the nick portion of nick!user@host.
#[arg(long)]
blacklist_file: Option<PathBuf>,
/// Also include join/part/quit/kick/nick-change/mode event lines
#[arg(long)]
include_events: bool,
}
fn parse_datetime(s: &str) -> Result<DateTime<Utc>> {
if let Ok(dt) = DateTime::parse_from_rfc3339(s) {
return Ok(dt.with_timezone(&Utc));
}
if let Ok(dt) = DateTime::parse_from_str(s, "%Y-%m-%dT%H:%M%z") {
return Ok(dt.with_timezone(&Utc));
}
let naive = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S")
.or_else(|_| chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M"))
.or_else(|_| {
chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d")
.map(|d| d.and_hms_opt(0, 0, 0).unwrap())
})
.with_context(|| {
format!(
"Cannot parse '{s}' as ISO 8601.\n\
Accepted formats: 2024-01-15T20:00:00, 2024-01-15T20:00:00+02:00, \
2024-01-15T20:00:00Z, 2024-01-15"
)
})?;
let local = Local
.from_local_datetime(&naive)
.single()
.with_context(|| format!("Ambiguous or invalid local time: '{s}'"))?;
Ok(local.with_timezone(&Utc))
}
fn nick_from_mask(mask: &str) -> &str {
mask.split('!').next().unwrap_or(mask)
}
fn build_blacklist(args: &Args) -> Result<HashSet<String>> {
let mut set = HashSet::new();
if let Some(ref path) = args.blacklist_file {
let content = fs::read_to_string(path)
.with_context(|| format!("Cannot read blacklist file: {}", path.display()))?;
for line in content.lines() {
let nick = line.trim();
if !nick.is_empty() && !nick.starts_with('#') {
set.insert(nick.to_lowercase());
}
}
eprintln!("Loaded {} nicks from blacklist file.", set.len());
}
Ok(set)
}
struct Message {
time: DateTime<Utc>,
msg_type: u32,
sender: String,
sender_prefixes: String,
message: String,
}
fn fetch_messages(
conn: &Connection,
user: &str,
network: &str,
channel: &str,
start_ts: i64,
end_ts: i64,
) -> Result<Vec<Message>> {
let user_id: i64 = conn
.query_row(
"SELECT userid FROM quasseluser WHERE username = ?1",
params![user],
|r| r.get(0),
)
.with_context(|| format!("User '{user}' not found in database"))?;
let network_id: i64 = conn
.query_row(
"SELECT networkid FROM network \
WHERE userid = ?1 AND lower(networkname) = lower(?2)",
params![user_id, network],
|r| r.get(0),
)
.with_context(|| format!("Network '{network}' not found for user '{user}'"))?;
let channel_lower = channel.to_lowercase();
let buffer_id: i64 = conn
.query_row(
"SELECT bufferid FROM buffer \
WHERE userid = ?1 AND networkid = ?2 AND buffercname = ?3",
params![user_id, network_id, channel_lower],
|r| r.get(0),
)
.with_context(|| {
format!(
"Channel '{channel}' not found on network '{network}' for user '{user}'"
)
})?;
// Fetch messages ordered by messageid (insertion order).
//
// backlog.time is seconds since Unix epoch in older schema versions and
// milliseconds in newer ones. The WHERE clause uses the seconds-scale bounds;
// this is safe because ms-epoch values for dates after 2001 always exceed any
// plausible seconds-scale value for the same era, so rows from a ms-schema DB
// will simply not match (the caller must pass ms bounds for such DBs — see
// note in run()).
let mut stmt = conn.prepare(
"SELECT b.time, b.type, b.senderprefixes, s.sender, b.message \
FROM backlog b \
JOIN sender s ON s.senderid = b.senderid \
WHERE b.bufferid = ?1 \
AND b.time >= ?2 \
AND b.time <= ?3 \
ORDER BY b.messageid ASC",
)?;
let rows = stmt.query_map(params![buffer_id, start_ts, end_ts], |r| {
let raw_time: i64 = r.get(0)?;
// Distinguish seconds vs milliseconds by magnitude.
// Year-2001 in ms ≈ 1_000_000_000_000; no plausible Unix-seconds value
// for modern dates reaches that threshold.
let time = if raw_time > 1_000_000_000_000 {
Utc.timestamp_millis_opt(raw_time).single().unwrap_or_default()
} else {
Utc.timestamp_opt(raw_time, 0).single().unwrap_or_default()
};
Ok(Message {
time,
msg_type: r.get::<_, u32>(1)?,
sender_prefixes: r.get::<_, String>(2).unwrap_or_default(),
sender: r.get(3)?,
message: r.get(4).unwrap_or_default(),
})
})?;
let mut messages = Vec::new();
for row in rows {
messages.push(row?);
}
Ok(messages)
}
fn format_message(msg: &Message, args: &Args) -> Option<String> {
let event_mask = if args.include_events {
MSG_NICK | MSG_MODE | MSG_JOIN | MSG_PART | MSG_QUIT | MSG_KICK
} else {
0
};
let accepted = MSG_PLAIN | MSG_ACTION | event_mask;
if msg.msg_type & accepted == 0 {
return None;
}
// Timestamp
let timestamp = if args.local_time {
msg.time
.with_timezone(&Local)
.format(&args.time_format)
.to_string()
} else {
msg.time.format(&args.time_format).to_string()
};
let nick = nick_from_mask(&msg.sender);
let sender_field = if args.sender_prefixes && !msg.sender_prefixes.is_empty() {
format!("{}{}", msg.sender_prefixes, nick)
} else {
nick.to_string()
};
let line = if msg.msg_type & MSG_ACTION != 0 {
format!("[{}] * {} {}", timestamp, sender_field, msg.message)
} else if msg.msg_type & (MSG_JOIN | MSG_PART | MSG_QUIT | MSG_KICK | MSG_NICK | MSG_MODE) != 0 {
format!("[{}] *** {}", timestamp, msg.message)
} else {
format!("[{}] <{}> {}", timestamp, sender_field, msg.message)
};
Some(line)
}
fn run(args: &Args) -> Result<()> {
let start_dt = parse_datetime(&args.start)
.with_context(|| format!("Invalid --start value: '{}'", args.start))?;
let end_dt = parse_datetime(&args.end)
.with_context(|| format!("Invalid --end value: '{}'", args.end))?;
if start_dt > end_dt {
bail!("--start must be earlier than or equal to --end");
}
let start_ts = start_dt.timestamp_millis();
let end_ts = end_dt.timestamp_millis();
let blacklist = build_blacklist(args)?;
let conn = Connection::open_with_flags(
&args.database,
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
)
.with_context(|| format!("Cannot open database: {}", args.database.display()))?;
let messages = fetch_messages(
&conn,
&args.user,
&args.network,
&args.channel,
start_ts,
end_ts,
)?;
eprintln!("Fetched {} raw messages from database.", messages.len());
let stdout = std::io::stdout();
let mut writer: Box<dyn Write> = match &args.output {
Some(path) => Box::new(BufWriter::new(
fs::File::create(path)
.with_context(|| format!("Cannot create output file: {}", path.display()))?,
)),
None => Box::new(BufWriter::new(stdout.lock())),
};
let mut written = 0usize;
for msg in &messages {
let nick_lc = nick_from_mask(&msg.sender).to_lowercase();
if blacklist.contains(&nick_lc) {
continue;
}
if let Some(line) = format_message(msg, args) {
writeln!(writer, "{}", line)?;
written += 1;
}
}
eprintln!("Wrote {} lines.", written);
Ok(())
}
fn main() {
let args = Args::parse();
if let Err(e) = run(&args) {
eprintln!("Error: {e:#}");
std::process::exit(1);
}
}