add WHOIS command

This commit is contained in:
tali 2024-01-31 10:39:48 -05:00
parent 65b3dc6672
commit bfa22c6dbd
1 changed files with 46 additions and 2 deletions

View File

@ -61,7 +61,7 @@ let reply t (num, params) =
| None -> "*" | None -> "*"
in in
let always_trailing = match num with let always_trailing = match num with
| "301" | "332" | "353" -> true | "301" | "311" | "312" | "319" | "332" | "353" -> true
| _ -> false | _ -> false
in in
Outbox.send t.outbox Outbox.send t.outbox
@ -585,6 +585,49 @@ let on_msg_who t mask =
reply t ("315", [mask; "End of WHO list"]); reply t ("315", [mask; "End of WHO list"]);
Ok () Ok ()
let list_whois t user =
let nick = User.nick user in
let { username; hostname; realname } = user.userinfo in
begin
reply t ("311", [nick; username; hostname; "*"; realname]);
reply t ("312", [nick; t.server_info.hostname; t.server_info.hostname]);
(* RPL_WHOISOPERATOR (313) "<client> <nick> :is an IRC operator" *)
(* RPL_WHOISIDLE (317) "<client> <nick> <secs> <signon> :seconds idle, signon time" *)
(* TODO: concat channel names until message becomes too long *)
List.iter
(fun (m : membership) ->
let chan_str = Router.membership_prefix m.mem_priv ^ Chan.name m.mem_chan in
reply t ("319", [nick; chan_str]))
(User.membership user);
reply t ("320", [nick; "is a cat, meow :3"]);
reply t ("379", [nick; Fmt.str "is using modes +%s"
(Mode.Set.to_string (User.mode user))]);
Option.iter
(fun text ->
reply t (away nick text))
(User.away user);
reply t ("318", [nick; "End of /WHOIS list"]);
end
let on_msg_whois t nick =
let* _me = require_registered t in
let* user =
try
match name_type nick with
| `nick -> Ok (Router.find_user t.router nick)
| `chan | `invalid -> raise Not_found
with Not_found ->
Error (nosuchnick nick)
in
list_whois t user;
Ok ()
(* welcome and quit *) (* welcome and quit *)
@ -784,6 +827,8 @@ let dispatch t = function
| "AWAY", args -> on_msg_away t (concat_args args) | "AWAY", args -> on_msg_away t (concat_args args)
| "MODE", tgt :: args when tgt <> "" -> on_msg_mode t tgt args | "MODE", tgt :: args when tgt <> "" -> on_msg_mode t tgt args
| "WHO", mask :: _ when mask <> "" -> on_msg_who t mask | "WHO", mask :: _ when mask <> "" -> on_msg_who t mask
| "WHOIS", ([] | [""] | (_ :: "" :: _)) -> Error nonicknamegiven
| "WHOIS", ([nick] | (_ :: nick :: _)) -> on_msg_whois t nick
| ("USER" | "JOIN" | "NAMES" | "PART" | "KICK" | "MODE" | "WHO") as cmd, _ -> | ("USER" | "JOIN" | "NAMES" | "PART" | "KICK" | "MODE" | "WHO") as cmd, _ ->
Error (needmoreparams cmd) Error (needmoreparams cmd)
(* TODO: "LIST" *) (* TODO: "LIST" *)
@ -795,7 +840,6 @@ let dispatch t = function
(* TODO: "HELP" *) (* TODO: "HELP" *)
(* TODO: "INFO" *) (* TODO: "INFO" *)
(* TODO: "NOTICE" *) (* TODO: "NOTICE" *)
(* TODO: "WHOIS" *)
(* TODO: "WHOWAS" *) (* TODO: "WHOWAS" *)
(* TODO: "KILL" *) (* TODO: "KILL" *)
(* TODO: "REHASH" *) (* TODO: "REHASH" *)