fix nickname assignment logic for the 1000th time

This commit is contained in:
tali 2024-01-08 00:39:39 -05:00
parent bd5afe7ad1
commit 7b72410498
2 changed files with 16 additions and 16 deletions

View File

@ -4,6 +4,7 @@ module User = Router.User
type t = {
addr : sockaddr;
user : User.t;
mutable pending_nick : string option;
outbox : Irc.Msg.t Lwt_stream.t;
push_outbox : (Irc.Msg.t option -> unit);
}
@ -15,7 +16,7 @@ let make ~(router : Router.t) ~(addr : sockaddr) : t =
in
let user = User.make router ~hostname in
let outbox, push_outbox = Lwt_stream.create () in
{ addr; user; outbox; push_outbox }
{ addr; user; pending_nick = None; outbox; push_outbox }
let outbox t = Lwt_stream.choose [t.outbox; User.inbox t.user]
let send t msg = try t.push_outbox (Some msg) with Lwt_stream.Closed -> ()
@ -36,8 +37,9 @@ let require_registered t f =
(* > user registration *)
let attempt_to_register t =
match t.user.nick, t.user.userinfo with
match t.pending_nick, t.user.userinfo with
| Some nick, Some _userinfo ->
t.pending_nick <- None;
begin match User.set_nick t.user nick with
| `nick_in_use -> `nicknameinuse nick
| `nick_set ->
@ -54,7 +56,7 @@ let on_msg_nick t nick =
| `nick_in_use -> `nicknameinuse nick
| `nick_set -> `ok
else begin
t.user.nick <- Some nick;
t.pending_nick <- Some nick;
attempt_to_register t
end
@ -136,7 +138,7 @@ let err_needmoreparams t cmd = rpl t "461" [cmd; "Not enough parameters"]
let err_nicknameinuse t nick = rpl t "433" [nick; "Nickname is already in use"]
let err_norecipient t cmd = rpl t "411" [Fmt.str "No recipient given (%s)" cmd]
let err_nosuchnick t tgt = rpl t "401" [tgt; "No such nick/channel"]
let err_notexttosend t = rpl t "412" [Fmt.str "No text to send"]
let err_notexttosend t = rpl t "412" ["No text to send"]
let err_notregistered t = rpl t "451" ["You have not registered"]
let err_unknowncommand t cmd = rpl t "421" [cmd; "Unknown command"]

View File

@ -11,7 +11,7 @@ and user = {
router : t;
hostname : string;
mutable key : nick_key;
mutable nick : Irc.nick option;
mutable nick : Irc.nick;
mutable userinfo : Irc.userinfo option;
mutable mode : Irc.Mode.t;
inbox : Irc.Msg.t Lwt_stream.t;
@ -33,25 +33,22 @@ module User = struct
router;
hostname;
key = unset;
nick = None;
nick = "*";
userinfo = None;
mode = Irc.Mode.of_string "iw";
inbox; push_inbox;
}
let nick t = t.nick
let prefix t = Irc.Msg.User_prefix (t.nick, t.userinfo, Some t.hostname)
let inbox t = t.inbox
let send t msg = try t.push_inbox (Some msg) with Lwt_stream.Closed -> ()
let close t = try t.push_inbox None with Lwt_stream.Closed -> ()
let nick t = Option.value t.nick ~default:"*"
let prefix t : Irc.Msg.prefix =
match t.nick with
| None -> No_prefix
| Some nick -> User_prefix (nick, t.userinfo, Some t.hostname)
let is_registered t = t.key <> unset
let unset_nick t =
let unregister t =
Hashtbl.remove t.router.users t.key;
t.key <- unset
@ -61,16 +58,17 @@ module User = struct
`nick_in_use
else begin
((* TODO: relay NICK message *));
unset_nick t;
unregister t;
Hashtbl.add t.router.users key t;
t.key <- key;
t.nick <- new_nick;
`nick_set
end
let cleanup t =
(* TODO: relay QUIT message *)
close t;
unset_nick t
unregister t;
close t
end
let privmsg src dst txt =