diff --git a/modules/reportserv/src/close.rs b/modules/reportserv/src/close.rs new file mode 100644 index 0000000..f26f24e --- /dev/null +++ b/modules/reportserv/src/close.rs @@ -0,0 +1,17 @@ +use fedserv_api::{Sender, ServiceCtx, Store}; + +// CLOSE (aka RESOLVE): mark a report resolved. +pub fn handle(me: &str, from: &Sender, id: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) { + if !super::require_oper(me, from, ctx) { + return; + } + let Some(n) = id.and_then(|n| n.parse::().ok()) else { + ctx.notice(me, from.uid, "Syntax: CLOSE "); + return; + }; + if db.report_close(n) { + ctx.notice(me, from.uid, format!("Report \x02#{n}\x02 closed.")); + } else { + ctx.notice(me, from.uid, format!("Report \x02#{n}\x02 isn't open (or doesn't exist).")); + } +} diff --git a/modules/reportserv/src/del.rs b/modules/reportserv/src/del.rs new file mode 100644 index 0000000..a06c320 --- /dev/null +++ b/modules/reportserv/src/del.rs @@ -0,0 +1,17 @@ +use fedserv_api::{Sender, ServiceCtx, Store}; + +// DEL (aka REMOVE): delete a report outright. +pub fn handle(me: &str, from: &Sender, id: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) { + if !super::require_oper(me, from, ctx) { + return; + } + let Some(n) = id.and_then(|n| n.parse::().ok()) else { + ctx.notice(me, from.uid, "Syntax: DEL "); + return; + }; + if db.report_del(n) { + ctx.notice(me, from.uid, format!("Report \x02#{n}\x02 deleted.")); + } else { + ctx.notice(me, from.uid, format!("There's no report \x02#{n}\x02.")); + } +} diff --git a/modules/reportserv/src/lib.rs b/modules/reportserv/src/lib.rs index 14bb8ed..364a101 100644 --- a/modules/reportserv/src/lib.rs +++ b/modules/reportserv/src/lib.rs @@ -4,8 +4,22 @@ //! records it in the searchable action log (OperServ LOGSEARCH), so a report is //! tied into the same trail as everything else. Operators review the queue with //! LIST / VIEW / CLOSE / DEL. +//! +//! `lib.rs` holds the dispatcher and the shared oper guard; each command lives +//! in its own file. -use fedserv_api::{human_time, NetView, Sender, Service, ServiceCtx, Store}; +use fedserv_api::{NetView, Sender, Service, ServiceCtx, Store}; + +#[path = "report.rs"] +mod report; +#[path = "list.rs"] +mod list; +#[path = "view.rs"] +mod view; +#[path = "close.rs"] +mod close; +#[path = "del.rs"] +mod del; pub struct ReportServ { pub uid: String, @@ -25,38 +39,17 @@ impl Service for ReportServ { fn on_command(&mut self, from: &Sender, args: &[&str], ctx: &mut ServiceCtx, _net: &dyn NetView, db: &mut dyn Store) { let me = self.uid.as_str(); match args.first().map(|s| s.to_ascii_uppercase()).as_deref() { - Some("REPORT") => report(me, from, &args[1..], ctx, db), - Some("LIST") => list(me, from, args.get(1).copied(), ctx, db), - Some("VIEW") | Some("READ") => view(me, from, args.get(1).copied(), ctx, db), - Some("CLOSE") | Some("RESOLVE") => close(me, from, args.get(1).copied(), ctx, db), - Some("DEL") | Some("REMOVE") => del(me, from, args.get(1).copied(), ctx, db), - Some("HELP") | None => help(me, from, ctx), + Some("REPORT") => report::handle(me, from, &args[1..], ctx, db), + Some("LIST") => list::handle(me, from, args.get(1).copied(), ctx, db), + Some("VIEW") | Some("READ") => view::handle(me, from, args.get(1).copied(), ctx, db), + Some("CLOSE") | Some("RESOLVE") => close::handle(me, from, args.get(1).copied(), ctx, db), + Some("DEL") | Some("REMOVE") => del::handle(me, from, args.get(1).copied(), ctx, db), + Some("HELP") | None => ctx.notice(me, from.uid, "ReportServ takes abuse reports. \x02REPORT\x02 tells the staff about a problem. Operators review with \x02LIST\x02 [ALL], \x02VIEW\x02 , \x02CLOSE\x02 , \x02DEL\x02 ."), Some(other) => ctx.notice(me, from.uid, format!("I don't know \x02{other}\x02. Try \x02REPORT\x02 or \x02HELP\x02.")), } } } -fn help(me: &str, from: &Sender, ctx: &mut ServiceCtx) { - ctx.notice(me, from.uid, "ReportServ takes abuse reports. \x02REPORT\x02 tells the staff about a problem. Operators review with \x02LIST\x02 [ALL], \x02VIEW\x02 , \x02CLOSE\x02 , \x02DEL\x02 ."); -} - -fn report(me: &str, from: &Sender, rest: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) { - let Some((&target, reason_words)) = rest.split_first() else { - ctx.notice(me, from.uid, "Syntax: REPORT "); - return; - }; - if reason_words.is_empty() { - ctx.notice(me, from.uid, "Please say what the problem is: REPORT "); - return; - } - let reason = reason_words.join(" "); - let reporter = from.account.unwrap_or(from.nick); - match db.report_file(reporter, target, &reason) { - Some(id) => ctx.notice(me, from.uid, format!("Thanks — your report (\x02#{id}\x02) about \x02{target}\x02 has been sent to the staff.")), - None => ctx.notice(me, from.uid, "You just filed a report — please wait a moment before filing another."), - } -} - // Reviewing the queue is for operators only. fn require_oper(me: &str, from: &Sender, ctx: &mut ServiceCtx) -> bool { if from.privs.any() { @@ -65,66 +58,3 @@ fn require_oper(me: &str, from: &Sender, ctx: &mut ServiceCtx) -> bool { ctx.notice(me, from.uid, "Access denied — reviewing reports is for services operators."); false } - -fn list(me: &str, from: &Sender, arg: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) { - if !require_oper(me, from, ctx) { - return; - } - let open_only = !arg.is_some_and(|a| a.eq_ignore_ascii_case("ALL")); - let reports = db.reports(open_only); - if reports.is_empty() { - ctx.notice(me, from.uid, if open_only { "No open reports." } else { "No reports." }); - return; - } - for r in &reports { - let flag = if r.open { "" } else { " (closed)" }; - let short: String = r.reason.chars().take(60).collect(); - ctx.notice(me, from.uid, format!("\x02#{}\x02 {} → \x02{}\x02: {}{}", r.id, r.reporter, r.target, short, flag)); - } - ctx.notice(me, from.uid, format!("{} report(s). \x02VIEW\x02 for detail.", reports.len())); -} - -fn view(me: &str, from: &Sender, id: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) { - if !require_oper(me, from, ctx) { - return; - } - let Some(r) = id.and_then(|n| n.parse::().ok()).and_then(|n| db.report(n)) else { - ctx.notice(me, from.uid, "No such report. Syntax: VIEW "); - return; - }; - ctx.notice(me, from.uid, format!("Report \x02#{}\x02 ({}):", r.id, if r.open { "open" } else { "closed" })); - ctx.notice(me, from.uid, format!(" By : \x02{}\x02", r.reporter)); - ctx.notice(me, from.uid, format!(" About : \x02{}\x02", r.target)); - ctx.notice(me, from.uid, format!(" Filed : {}", human_time(r.ts))); - ctx.notice(me, from.uid, format!(" Reason : {}", r.reason)); -} - -fn close(me: &str, from: &Sender, id: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) { - if !require_oper(me, from, ctx) { - return; - } - let Some(n) = id.and_then(|n| n.parse::().ok()) else { - ctx.notice(me, from.uid, "Syntax: CLOSE "); - return; - }; - if db.report_close(n) { - ctx.notice(me, from.uid, format!("Report \x02#{n}\x02 closed.")); - } else { - ctx.notice(me, from.uid, format!("Report \x02#{n}\x02 isn't open (or doesn't exist).")); - } -} - -fn del(me: &str, from: &Sender, id: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) { - if !require_oper(me, from, ctx) { - return; - } - let Some(n) = id.and_then(|n| n.parse::().ok()) else { - ctx.notice(me, from.uid, "Syntax: DEL "); - return; - }; - if db.report_del(n) { - ctx.notice(me, from.uid, format!("Report \x02#{n}\x02 deleted.")); - } else { - ctx.notice(me, from.uid, format!("There's no report \x02#{n}\x02.")); - } -} diff --git a/modules/reportserv/src/list.rs b/modules/reportserv/src/list.rs new file mode 100644 index 0000000..982d9e2 --- /dev/null +++ b/modules/reportserv/src/list.rs @@ -0,0 +1,20 @@ +use fedserv_api::{Sender, ServiceCtx, Store}; + +// LIST [ALL]: operators list the open reports (or every report with ALL). +pub fn handle(me: &str, from: &Sender, arg: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) { + if !super::require_oper(me, from, ctx) { + return; + } + let open_only = !arg.is_some_and(|a| a.eq_ignore_ascii_case("ALL")); + let reports = db.reports(open_only); + if reports.is_empty() { + ctx.notice(me, from.uid, if open_only { "No open reports." } else { "No reports." }); + return; + } + for r in &reports { + let flag = if r.open { "" } else { " (closed)" }; + let short: String = r.reason.chars().take(60).collect(); + ctx.notice(me, from.uid, format!("\x02#{}\x02 {} → \x02{}\x02: {}{}", r.id, r.reporter, r.target, short, flag)); + } + ctx.notice(me, from.uid, format!("{} report(s). \x02VIEW\x02 for detail.", reports.len())); +} diff --git a/modules/reportserv/src/report.rs b/modules/reportserv/src/report.rs new file mode 100644 index 0000000..ae10837 --- /dev/null +++ b/modules/reportserv/src/report.rs @@ -0,0 +1,19 @@ +use fedserv_api::{Sender, ServiceCtx, Store}; + +// REPORT : file an abuse report. Rate-limited. +pub fn handle(me: &str, from: &Sender, rest: &[&str], ctx: &mut ServiceCtx, db: &mut dyn Store) { + let Some((&target, reason_words)) = rest.split_first() else { + ctx.notice(me, from.uid, "Syntax: REPORT "); + return; + }; + if reason_words.is_empty() { + ctx.notice(me, from.uid, "Please say what the problem is: REPORT "); + return; + } + let reason = reason_words.join(" "); + let reporter = from.account.unwrap_or(from.nick); + match db.report_file(reporter, target, &reason) { + Some(id) => ctx.notice(me, from.uid, format!("Thanks — your report (\x02#{id}\x02) about \x02{target}\x02 has been sent to the staff.")), + None => ctx.notice(me, from.uid, "You just filed a report — please wait a moment before filing another."), + } +} diff --git a/modules/reportserv/src/view.rs b/modules/reportserv/src/view.rs new file mode 100644 index 0000000..3b515e7 --- /dev/null +++ b/modules/reportserv/src/view.rs @@ -0,0 +1,17 @@ +use fedserv_api::{human_time, Sender, ServiceCtx, Store}; + +// VIEW (aka READ): operators read a report in full. +pub fn handle(me: &str, from: &Sender, id: Option<&str>, ctx: &mut ServiceCtx, db: &mut dyn Store) { + if !super::require_oper(me, from, ctx) { + return; + } + let Some(r) = id.and_then(|n| n.parse::().ok()).and_then(|n| db.report(n)) else { + ctx.notice(me, from.uid, "No such report. Syntax: VIEW "); + return; + }; + ctx.notice(me, from.uid, format!("Report \x02#{}\x02 ({}):", r.id, if r.open { "open" } else { "closed" })); + ctx.notice(me, from.uid, format!(" By : \x02{}\x02", r.reporter)); + ctx.notice(me, from.uid, format!(" About : \x02{}\x02", r.target)); + ctx.notice(me, from.uid, format!(" Filed : {}", human_time(r.ts))); + ctx.notice(me, from.uid, format!(" Reason : {}", r.reason)); +}