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 = { type t = {
addr : sockaddr; addr : sockaddr;
user : User.t; user : User.t;
mutable pending_nick : string option;
outbox : Irc.Msg.t Lwt_stream.t; outbox : Irc.Msg.t Lwt_stream.t;
push_outbox : (Irc.Msg.t option -> unit); push_outbox : (Irc.Msg.t option -> unit);
} }
@ -15,7 +16,7 @@ let make ~(router : Router.t) ~(addr : sockaddr) : t =
in in
let user = User.make router ~hostname in let user = User.make router ~hostname in
let outbox, push_outbox = Lwt_stream.create () 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 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 -> () 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 *) (* > user registration *)
let attempt_to_register t = 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 -> | Some nick, Some _userinfo ->
t.pending_nick <- None;
begin match User.set_nick t.user nick with begin match User.set_nick t.user nick with
| `nick_in_use -> `nicknameinuse nick | `nick_in_use -> `nicknameinuse nick
| `nick_set -> | `nick_set ->
@ -54,7 +56,7 @@ let on_msg_nick t nick =
| `nick_in_use -> `nicknameinuse nick | `nick_in_use -> `nicknameinuse nick
| `nick_set -> `ok | `nick_set -> `ok
else begin else begin
t.user.nick <- Some nick; t.pending_nick <- Some nick;
attempt_to_register t attempt_to_register t
end 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_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_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_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_notregistered t = rpl t "451" ["You have not registered"]
let err_unknowncommand t cmd = rpl t "421" [cmd; "Unknown command"] let err_unknowncommand t cmd = rpl t "421" [cmd; "Unknown command"]

View File

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