modules: port randquote, disable, restrictchans, channames

This commit is contained in:
Jean Chevronnet 2026-08-09 20:03:47 +00:00
parent bda7eecfc2
commit 140554719b
6 changed files with 172 additions and 0 deletions

44
src/modules/randquote.rs Normal file
View file

@ -0,0 +1,44 @@
//! randquote — greet each connecting user with a random line from a configured
//! set of quotes. Off unless one or more `randquote = <line>` are configured.
//!
//! Behaviour reference: InspIRCd's `m_randquote`. Original native Rust.
use openssl::rand::rand_bytes;
use crate::module::Module;
use crate::server::Server;
use crate::Uid;
/// A random index in `0..n` from the CSPRNG (0 if `n == 0` or on error).
fn rand_below(n: usize) -> usize {
if n == 0 {
return 0;
}
let mut b = [0u8; 8];
if rand_bytes(&mut b).is_err() {
return 0;
}
(u64::from_le_bytes(b) % n as u64) as usize
}
pub struct RandQuote;
impl Module for RandQuote {
fn name(&self) -> &'static str {
"randquote"
}
fn on_user_connect(&mut self, srv: &mut Server, uid: Uid) {
let quotes = srv.conf_all("randquote");
if quotes.is_empty() {
return;
}
let quote = quotes[rand_below(quotes.len())].clone();
let nick = srv
.users
.get(&uid)
.map(|u| u.nick.clone())
.unwrap_or_default();
srv.send(uid, format!(":{} NOTICE {nick} :{quote}", srv.name));
}
}