76 lines
1.7 KiB
OCaml
76 lines
1.7 KiB
OCaml
|
type nick_key = Nick_key of string [@@unboxed]
|
||
|
let nick_key n = Nick_key (String.lowercase_ascii n) (* TODO: "scandinavian" lowercase *)
|
||
|
let unset = Nick_key ""
|
||
|
|
||
|
(* TODO: notifications *)
|
||
|
(* type notif = *)
|
||
|
(* | Mode of Irc.nick * Irc.nick * Irc.Mode.t *)
|
||
|
(* | Nick of Irc.Msg.prefix * Irc.nick *)
|
||
|
|
||
|
type t = {
|
||
|
users : (nick_key, user) Hashtbl.t
|
||
|
(* TODO: channels *)
|
||
|
}
|
||
|
|
||
|
and user = {
|
||
|
router : t;
|
||
|
hostname : string;
|
||
|
mutable key : nick_key;
|
||
|
mutable nick : Irc.nick option;
|
||
|
mutable userinfo : Irc.userinfo option;
|
||
|
mutable mode : Irc.Mode.t;
|
||
|
(* inbox : notif Lwt_stream.t; *)
|
||
|
(* push_inbox : (notif option -> unit); *)
|
||
|
}
|
||
|
|
||
|
let make () =
|
||
|
{ users = Hashtbl.create 4096 }
|
||
|
|
||
|
let find_user t nick =
|
||
|
Hashtbl.find_opt t.users (nick_key nick)
|
||
|
|
||
|
module User = struct
|
||
|
type t = user
|
||
|
|
||
|
let make router ~hostname =
|
||
|
(* let inbox, push_inbox = Lwt_stream.create () in *)
|
||
|
{
|
||
|
router;
|
||
|
hostname;
|
||
|
key = unset;
|
||
|
nick = None;
|
||
|
userinfo = None;
|
||
|
mode = Irc.Mode.of_string "iw";
|
||
|
(* inbox; push_inbox; *)
|
||
|
}
|
||
|
|
||
|
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 =
|
||
|
Hashtbl.remove t.router.users t.key;
|
||
|
t.key <- unset
|
||
|
|
||
|
let set_nick t new_nick =
|
||
|
let key = nick_key new_nick in
|
||
|
if Hashtbl.mem t.router.users key then
|
||
|
`nick_in_use
|
||
|
else begin
|
||
|
((* TODO: notify others of nick change *));
|
||
|
unset_nick t;
|
||
|
Hashtbl.add t.router.users key t;
|
||
|
t.key <- key;
|
||
|
`nick_set
|
||
|
end
|
||
|
|
||
|
let cleanup user =
|
||
|
unset_nick user
|
||
|
(* user.push_inbox None *)
|
||
|
end
|