Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/spotnet/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,15 @@ pub struct Comment {
pub body: String,
}

/// What's been said about release `release_id`'s spot: its comments, oldest first, and how many spam reports it
/// has. `spot` is the message-id its row has; the spots of the same NZB that tagged it before count too.
/// What's been said about release `release_id`'s spot: its comments, oldest first (but those removed by a
/// moderator or their poster), and how many spam reports it has. `spot` is the message-id its row has; the spots of the same NZB that tagged it before count too.
pub fn about(conn: &Connection, release_id: i64, spot: &str) -> Result<(Vec<Comment>, i64)> {
let spots = "select ?2 union select message_id from main.spots_seen where release_id = ?1 and state = ?3";
let comments = conn
.prepare_cached(&format!(
"select at, rating, coalesce(poster, ''), spotter_id, state, coalesce(body, '')
from main.spot_comments where spot in ({spots}) order by at, message_id"
from main.spot_comments c where spot in ({spots}) and not {} order by at, message_id",
super::store::disposed_sql("d", "c", "spotter_id", None)
))?
.query_map(params![release_id, spot, SAVED], |r| {
Ok(Comment {
Expand Down
19 changes: 19 additions & 0 deletions src/spotnet/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ impl SpotHeader {
self.key_id == MODERATOR_KEY
}

/// The spot (or comment) a key-2 command removes: `DISPOSE <message-id> - <title>`, or `DELETE` or `REMOVE`.
pub fn removes(&self) -> Option<String> {
if !self.is_moderation() {
return None;
}
let mut words = self.title.split_whitespace();
let command = words.next()?;
if !["dispose", "delete", "remove"].iter().any(|c| command.eq_ignore_ascii_case(c)) {
return None;
}
let id = words.next()?.trim_matches(['<', '>']);
super::valid_message_id(id).then(|| id.to_string())
}

/// Whether this is a spot worth fetching whole: signed, not a moderator's command, movies or TV.
pub fn wanted(&self) -> bool {
self.signed && !self.is_moderation() && self.kind().is_some()
Expand Down Expand Up @@ -278,6 +292,11 @@ mod tests {
assert!(h.is_moderation());
assert_eq!(h.kind(), Some(Kind::Movie));
assert!(!h.wanted());
assert_eq!(h.removes().as_deref(), Some("hzqY4qMS5McHhOuQESoUdhzD@spot.net"));
assert!(parse("Movie (2026) | tag", FROM).unwrap().wanted());
assert_eq!(parse("DISPOSE a@spot.net", FROM).unwrap().removes(), None);
assert_eq!(parse("remove <a@spot.net>", from).unwrap().removes().as_deref(), Some("a@spot.net"));
assert_eq!(parse("Hello a@spot.net", from).unwrap().removes(), None);
assert_eq!(parse("DISPOSE nothing", from).unwrap().removes(), None);
}
}
33 changes: 31 additions & 2 deletions src/spotnet/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ async fn read_overview(
Err(e) => return Err(e.into()),
};
read += rows.len() as u64;
let now = chrono::Utc::now().timestamp();
let removals: Vec<store::Disposal> = rows.iter().filter_map(|r| removal(r, now)).collect();
let wanted: Vec<store::Noted> = rows
.iter()
.filter_map(|r| {
Expand All @@ -467,13 +469,40 @@ async fn read_overview(
})
.collect();
let key = host.clone();
let now = chrono::Utc::now().timestamp();
noted += on_db(db, move |conn| Ok(store::note_read(conn, &key, to, &wanted, now)?)).await?;
// the removals first: the cursor moves with the spots
noted += on_db(db, move |conn| {
let removed = store::note_disposals(conn, &removals)?;
if removed > 0 {
println!("spots: {removed} spots or comments removed by a moderator or their spotter");
}
Ok(store::note_read(conn, &key, to, &wanted, now)?)
})
.await?;
from = to + 1;
}
Ok((read, noted))
}

/// The removal an overview line is, when it's a moderator's (or a spotter's own) `DISPOSE` whose signatures hold.
/// A spotter's own needs their key to have signed its title and message-id: it removes only their posts.
fn removal(r: &crate::nntp::Overview, now: i64) -> Option<store::Disposal> {
let h = header::parse(&r.subject, &r.from)?;
let message_id = h.removes()?;
let verdict = sign::verify(&h.title, &r.from, &r.message_id);
if !verdict.verified {
return None;
}
// their key must sign both the title, which names the target, and the message-id: the header signature alone
// could be replayed onto another message-id, the message-id's alone onto another title
let spotter = match verdict.personal {
true if verdict.message_id_signed && verdict.title_signed => verdict.spotter_id?,
true => return None,
false => String::new(),
};
let at = crate::dates::posted_timestamp(&r.date).unwrap_or(now);
Some(store::Disposal { message_id, spotter, at })
}

/// What one pass read of what's said about spots, for the log.
#[derive(Debug, Default, PartialEq, Eq)]
struct Said {
Expand Down
14 changes: 12 additions & 2 deletions src/spotnet/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub struct Verdict {
/// the spotter's key signed the message-id itself. A spotter id from the header signature alone can be
/// replayed: that signature covers the title, header and nickname, not the message-id
pub message_id_signed: bool,
/// the header signature (over the title, header and nickname) holds: under Spotnet's key, or for a key-7 spot
/// or a spotter's own command under the spotter's key
pub title_signed: bool,
}

/// Checks the signatures of the spot (or moderator's command) whose overview gives `subject`, `from` and
Expand Down Expand Up @@ -87,17 +90,20 @@ pub fn verify(title: &str, from: &str, message_id: &str) -> Verdict {
verdict.verified = hashcash(&message_id);
if verdict.verified && by_user() {
verdict.spotter_id = spotter_id(&user_key);
verdict.title_signed = true;
}
} else if let Some((_, modulus)) = SPOTNET_KEYS.iter().find(|(k, _)| *k == key_id) {
if key_id == 2 && user_key.len() > PERSONAL_KEY_LEN {
verdict.personal = true;
verdict.verified = hashcash(&message_id);
if verdict.verified && by_user() {
verdict.spotter_id = spotter_id(&user_key);
verdict.title_signed = true;
}
} else {
let modulus = decode_base64(modulus).expect("Spotnet's keys are base64");
verdict.verified = rsa_sha1(&signed, &header_sig, &modulus);
verdict.title_signed = verdict.verified;
}
}
// a `From` with the spotter's key and signature: the message-id must be signed with that key too (a key that
Expand Down Expand Up @@ -192,7 +198,7 @@ mod tests {
#[test]
fn a_moderators_command() {
let v = verify(MOD_TITLE, MOD_FROM, MOD_ID);
assert_eq!(v, Verdict { verified: true, ..Verdict::default() });
assert_eq!(v, Verdict { verified: true, title_signed: true, ..Verdict::default() });
// the signed title names the spot: another spot's id breaks it
let other = MOD_TITLE.replace("vhcUXQS5", "vhcUXQS6");
assert_eq!(verify(&other, MOD_FROM, MOD_ID), Verdict::default());
Expand All @@ -207,7 +213,11 @@ mod tests {
#[test]
fn a_spotters_own_dispose() {
let v = verify(OWN_TITLE, OWN_FROM, OWN_ID);
assert!(v.verified && v.personal && v.message_id_signed);
assert!(v.verified && v.personal && v.message_id_signed && v.title_signed);
// another target in the title: the spotter's header signature no longer holds
let other = OWN_TITLE.replace("x9FBYiqq", "x9FBYiqr");
let v = verify(&other, OWN_FROM, OWN_ID);
assert!(v.verified && v.message_id_signed && !v.title_signed);
assert_eq!(v.spotter_id, spotter_id("4CGr2alVHshWG91Ajiozvi3D5D1MPD8Iwh5veSiUvGdrhFl5LBcyN5jV6GwHXqZB"));
assert!(v.spotter_id.is_some());
}
Expand Down
Loading