Allocate guest nicks that skip online and registered names
This commit is contained in:
parent
2c81af108b
commit
118782a4bf
5 changed files with 40 additions and 7 deletions
|
|
@ -2277,6 +2277,25 @@ pub fn valid_email(addr: &str) -> bool {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The next free guest nick: `base` + an incrementing counter, skipping any nick
|
||||||
|
/// that is currently online or registered. An enforced/logged-out rename must not
|
||||||
|
/// land on a real user or a registered nick — that would either defeat the rename
|
||||||
|
/// (the target stays put) or collide with and kill an innocent occupant. Advances
|
||||||
|
/// `seq` past whatever it returns.
|
||||||
|
pub fn next_guest_nick(base: &str, seq: &mut u32, net: &dyn NetView, store: &dyn Store) -> String {
|
||||||
|
for _ in 0..100_000 {
|
||||||
|
let candidate = format!("{base}{seq}");
|
||||||
|
*seq = seq.wrapping_add(1);
|
||||||
|
if net.uid_by_nick(&candidate).is_none() && !store.exists(&candidate) {
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Pathological: the whole range is taken. Fall back rather than loop forever.
|
||||||
|
let candidate = format!("{base}{seq}");
|
||||||
|
*seq = seq.wrapping_add(1);
|
||||||
|
candidate
|
||||||
|
}
|
||||||
|
|
||||||
// Reject a look-alike / mixed-script registration name (nick or channel): the
|
// Reject a look-alike / mixed-script registration name (nick or channel): the
|
||||||
// impersonation trick that a message-content filter never sees at the registration
|
// impersonation trick that a message-content filter never sees at the registration
|
||||||
// seam. Returns a rejection reason, or None if the name is fine. Genuine
|
// seam. Returns a rejection reason, or None if the name is fine. Genuine
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,7 @@ pub fn handle(me: &str, guest_nick: &str, guest_seq: &mut u32, from: &Sender, ar
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Some(ghost) => {
|
Some(ghost) => {
|
||||||
let guest = format!("{guest_nick}{guest_seq}");
|
let guest = echo_api::next_guest_nick(guest_nick, guest_seq, net, &*db);
|
||||||
*guest_seq = guest_seq.wrapping_add(1);
|
|
||||||
ctx.force_nick(&ghost, &guest);
|
ctx.force_nick(&ghost, &guest);
|
||||||
ctx.notice(me, from.uid, format!("\x02{target}\x02 has been freed."));
|
ctx.notice(me, from.uid, format!("\x02{target}\x02 has been freed."));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -112,7 +112,7 @@ impl Service for NickServ {
|
||||||
match cmd.as_deref() {
|
match cmd.as_deref() {
|
||||||
Some("REGISTER") => register::handle(me, from, args, ctx, db),
|
Some("REGISTER") => register::handle(me, from, args, ctx, db),
|
||||||
Some("IDENTIFY") | Some("ID") => identify::handle(me, from, args, ctx, db),
|
Some("IDENTIFY") | Some("ID") => identify::handle(me, from, args, ctx, db),
|
||||||
Some("LOGOUT") | Some("LOGOFF") => logout::handle(me, &self.guest_nick, &mut self.guest_seq, from, ctx),
|
Some("LOGOUT") | Some("LOGOFF") => logout::handle(me, &self.guest_nick, &mut self.guest_seq, from, ctx, net, db),
|
||||||
Some("CERT") => cert::handle(me, from, args, ctx, db),
|
Some("CERT") => cert::handle(me, from, args, ctx, db),
|
||||||
Some("INFO") => info::handle(me, from, args, ctx, net, db),
|
Some("INFO") => info::handle(me, from, args, ctx, net, db),
|
||||||
Some("ALIST") => alist::handle(me, from, ctx, db),
|
Some("ALIST") => alist::handle(me, from, ctx, db),
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
use echo_api::{Sender, ServiceCtx};
|
use echo_api::{NetView, Sender, ServiceCtx, Store};
|
||||||
|
|
||||||
// LOGOUT: log out and rename to a guest nick (prefix + a per-logout sequence).
|
// LOGOUT: log out and rename to a guest nick (prefix + a per-logout sequence).
|
||||||
pub fn handle(me: &str, guest_nick: &str, guest_seq: &mut u32, from: &Sender, ctx: &mut ServiceCtx) {
|
pub fn handle(me: &str, guest_nick: &str, guest_seq: &mut u32, from: &Sender, ctx: &mut ServiceCtx, net: &dyn NetView, db: &dyn Store) {
|
||||||
if from.account.is_none() {
|
if from.account.is_none() {
|
||||||
ctx.notice(me, from.uid, "You're not logged in.");
|
ctx.notice(me, from.uid, "You're not logged in.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let guest = format!("{guest_nick}{guest_seq}");
|
let guest = echo_api::next_guest_nick(guest_nick, guest_seq, net, db);
|
||||||
*guest_seq = guest_seq.wrapping_add(1);
|
|
||||||
ctx.logout(from.uid);
|
ctx.logout(from.uid);
|
||||||
ctx.force_nick(from.uid, &guest);
|
ctx.force_nick(from.uid, &guest);
|
||||||
ctx.notice(me, from.uid, format!("You're now logged out. Your nick is now \x02{}\x02.", guest));
|
ctx.notice(me, from.uid, format!("You're now logged out. Your nick is now \x02{}\x02.", guest));
|
||||||
|
|
|
||||||
|
|
@ -418,6 +418,22 @@
|
||||||
e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAA".into(), text: "LOGOUT".into() })
|
e.handle(NetEvent::Privmsg { from: uid.into(), to: "42SAAAAAA".into(), text: "LOGOUT".into() })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A guest rename must skip a nick that's already online, or it would collide
|
||||||
|
// with (and get) an innocent user instead of freeing the name.
|
||||||
|
#[test]
|
||||||
|
fn logout_skips_an_occupied_guest_nick() {
|
||||||
|
let mut e = engine_with("guestskip", "foo", "sesame");
|
||||||
|
e.handle(NetEvent::UserConnect { uid: "000AAAAAB".into(), nick: "foo".into(), host: "h".into() , ip: "0.0.0.0".into() });
|
||||||
|
e.handle(NetEvent::Privmsg { from: "000AAAAAB".into(), to: "42SAAAAAA".into(), text: "IDENTIFY sesame".into() });
|
||||||
|
// Someone already holds Guest12345 (the first nick the counter would pick).
|
||||||
|
e.handle(NetEvent::UserConnect { uid: "000AAAAAC".into(), nick: "Guest12345".into(), host: "h".into() , ip: "0.0.0.0".into() });
|
||||||
|
let out = logout(&mut e, "000AAAAAB");
|
||||||
|
assert!(
|
||||||
|
out.iter().any(|a| matches!(a, NetAction::ForceNick { uid, nick } if uid == "000AAAAAB" && nick == "Guest12346")),
|
||||||
|
"logout skips the taken guest nick: {out:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// LOGOUT while not identified must not rename you or clear anything.
|
// LOGOUT while not identified must not rename you or clear anything.
|
||||||
#[test]
|
#[test]
|
||||||
fn logout_without_login_is_noop() {
|
fn logout_without_login_is_noop() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue