use std::collections::HashMap;
use std::sync::Mutex;
pub fn is_channel_permission_relay_enabled() -> bool {
false
}
#[derive(Debug, Clone)]
pub struct ChannelPermissionResponse {
pub behavior: PermissionBehavior,
pub from_server: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum PermissionBehavior {
Allow,
Deny,
}
pub struct ChannelPermissionCallbacks {
handlers: Mutex<HashMap<String, Vec<Box<dyn Fn(ChannelPermissionResponse) + Send + Sync>>>>,
pending: Mutex<HashMap<String, (PermissionBehavior, String)>>,
}
impl ChannelPermissionCallbacks {
pub fn new() -> Self {
Self {
handlers: Mutex::new(HashMap::new()),
pending: Mutex::new(HashMap::new()),
}
}
pub fn on_response<F>(&self, request_id: &str, handler: F)
where
F: Fn(ChannelPermissionResponse) + Send + Sync + 'static,
{
let mut handlers = self.handlers.lock().unwrap();
handlers
.entry(request_id.to_string())
.or_insert_with(Vec::new)
.push(Box::new(handler));
}
pub fn resolve(
&self,
request_id: &str,
behavior: PermissionBehavior,
from_server: &str,
) -> bool {
let mut pending = self.pending.lock().unwrap();
if let Some((stored_behavior, stored_server)) = pending.remove(request_id) {
if stored_behavior == behavior && stored_server == from_server {
let handlers = self.handlers.lock().unwrap();
if let Some(h) = handlers.get(request_id) {
let response = ChannelPermissionResponse {
behavior,
from_server: from_server.to_string(),
};
for handler in h {
handler(response.clone());
}
}
return true;
}
}
false
}
}
impl Default for ChannelPermissionCallbacks {
fn default() -> Self {
Self::new()
}
}
const PERMISSION_REPLY_RE: &str = r"^\s*(y|yes|n|no)\s+([a-km-z]{5})\s*$";
const ID_ALPHABET: &str = "abcdefghijkmnopqrstuvwxyz";
pub fn generate_permission_id() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.subsec_nanos();
let mut id = String::new();
let alphabet: Vec<char> = ID_ALPHABET.chars().collect();
for i in 0..5 {
let idx = ((nanos >> (i * 3)) % (alphabet.len() as u32)) as usize;
id.push(alphabet[idx]);
}
id
}