use std::sync::Mutex;
pub const CLAIM_CODE_KEY: &str = "owner_claim_code";
const CLAIM_COMMAND: &str = "/claim";
const MAX_WRONG_ATTEMPTS: u32 = 5;
pub fn new_claim_code() -> String {
let raw = uuid::Uuid::new_v4()
.simple()
.to_string()
.to_ascii_uppercase();
let raw = &raw[..12];
format!("{}-{}-{}", &raw[..4], &raw[4..8], &raw[8..12])
}
pub fn claim_code_from_extra(extra_config: &str) -> Option<String> {
serde_json::from_str::<serde_json::Value>(extra_config)
.ok()?
.get(CLAIM_CODE_KEY)?
.as_str()
.map(str::trim)
.filter(|code| !code.is_empty())
.map(str::to_string)
}
pub fn extra_with_claim_code(extra_config: &str, code: &str) -> String {
let mut value = serde_json::from_str::<serde_json::Value>(extra_config)
.ok()
.filter(serde_json::Value::is_object)
.unwrap_or_else(|| serde_json::json!({}));
value[CLAIM_CODE_KEY] = serde_json::Value::String(code.to_string());
value.to_string()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClaimAttempt {
Claimed,
Refused,
}
pub struct OwnerClaim {
platform: &'static str,
state: Mutex<ClaimState>,
}
struct ClaimState {
code: String,
wrong: u32,
}
impl OwnerClaim {
pub fn new(platform: &'static str, code: Option<String>) -> Self {
Self {
platform,
state: Mutex::new(ClaimState {
code: code.unwrap_or_else(new_claim_code),
wrong: 0,
}),
}
}
pub fn code(&self) -> String {
self.lock().code.clone()
}
pub fn set_code(&self, code: &str) {
let mut state = self.lock();
state.code = code.trim().to_string();
state.wrong = 0;
}
pub fn announce(&self) {
tracing::warn!(
platform = self.platform,
"This {} bot has no owner yet. To claim it, send it a direct message: {} {}",
self.platform,
CLAIM_COMMAND,
self.code()
);
}
pub fn attempt(&self, text: &str) -> ClaimAttempt {
let presented = presented_code(text);
let mut state = self.lock();
if presented.is_some_and(|presented| same_code(presented, &state.code)) {
return ClaimAttempt::Claimed;
}
state.wrong += 1;
if state.wrong >= MAX_WRONG_ATTEMPTS {
state.code = new_claim_code();
state.wrong = 0;
drop(state);
self.announce();
}
ClaimAttempt::Refused
}
pub fn refusal(&self) -> String {
format!(
"This bot has no owner yet. If you connected it, send the claim code \
shown where you connected it: {CLAIM_COMMAND} <code>"
)
}
fn lock(&self) -> std::sync::MutexGuard<'_, ClaimState> {
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
}
fn presented_code(text: &str) -> Option<&str> {
let text = text.trim();
let code = match text.strip_prefix(CLAIM_COMMAND) {
Some(rest) if rest.is_empty() || rest.starts_with(char::is_whitespace) => rest.trim(),
Some(_) => return None,
None => text,
};
(!code.is_empty()).then_some(code)
}
fn same_code(presented: &str, code: &str) -> bool {
let (a, b) = (presented.as_bytes(), code.as_bytes());
let mut diff = a.len() ^ b.len();
for (i, byte) in b.iter().enumerate() {
let other = a.get(i).copied().unwrap_or(0).to_ascii_uppercase();
diff |= usize::from(other ^ byte.to_ascii_uppercase());
}
diff == 0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_the_code_claims() {
let claim = OwnerClaim::new("telegram", Some("ABCD-EF12-3456".into()));
assert_eq!(claim.attempt("hello"), ClaimAttempt::Refused);
assert_eq!(
claim.attempt("/claim abcd-ef12-3456"),
ClaimAttempt::Claimed
);
assert_eq!(claim.attempt(" ABCD-EF12-3456 "), ClaimAttempt::Claimed);
assert_eq!(claim.attempt("/claimABCD-EF12-3456"), ClaimAttempt::Refused);
assert_eq!(claim.attempt("ABCD-EF12-345"), ClaimAttempt::Refused);
assert!(!claim.refusal().contains("ABCD"));
}
#[test]
fn wrong_guesses_replace_the_code() {
let claim = OwnerClaim::new("slack", Some("ABCD-EF12-3456".into()));
for _ in 0..MAX_WRONG_ATTEMPTS {
assert_eq!(
claim.attempt("/claim 0000-0000-0000"),
ClaimAttempt::Refused
);
}
assert_ne!(claim.code(), "ABCD-EF12-3456");
assert_eq!(
claim.attempt("/claim ABCD-EF12-3456"),
ClaimAttempt::Refused
);
let fresh = claim.code();
assert_eq!(claim.attempt(&fresh), ClaimAttempt::Claimed);
}
#[test]
fn codes_round_trip_through_extra_config() {
let code = new_claim_code();
assert_eq!(code.len(), 14);
let extra = extra_with_claim_code(r#"{"username":"synthetic_bot"}"#, &code);
assert_eq!(claim_code_from_extra(&extra), Some(code));
assert!(extra.contains("synthetic_bot"));
assert_eq!(claim_code_from_extra("{}"), None);
assert_eq!(claim_code_from_extra("not json"), None);
}
}