add validation of topics and protected topic mode

This commit is contained in:
tali 2024-01-27 15:54:14 -05:00
parent d5cdc71ac0
commit e7978a01f3
1 changed files with 25 additions and 15 deletions

View File

@ -68,11 +68,27 @@ let modeunknownflag = "501", ["Didn't understand MODE command"]
let usersdontmatch_set = "502", ["Can't change mode for other users"]
let usersdontmatch_get = "502", ["Can't view mode for other users"]
(* permission checking *)
let require_registered t : User.t result =
match t.user with
| Some me -> Ok me
| None -> Error notregistered
let require_same_user user me =
if (user : User.t) == me then Ok () else Error usersdontmatch_get
let require_membership chan me =
match Router.membership chan me with
| mem -> Ok mem
| exception Not_found -> Error (notonchannel (Chan.name chan))
let require_chan_op (m : Router.membership) =
match m.mem_priv with
| Operator -> Ok ()
| _ -> Error (chanoprivsneeded (Chan.name m.mem_chan))
(* modes *)
@ -139,14 +155,6 @@ let set_member_priv ~from (mem : Router.membership) (priv : Router.priv) =
Router.relay msg ~from [`to_chan chan; `to_self];
mem.mem_priv <- priv
let require_same_user user me =
if (user : User.t) == me then Ok () else Error usersdontmatch_get
let require_chan_op chan me =
match Router.membership chan me with
| m when m.mem_priv = Operator -> Ok (* m *) ()
| _ | exception Not_found -> Error (chanoprivsneeded (Chan.name chan))
let on_get_user_mode user me =
let* () = require_same_user user me in
Ok [
@ -189,8 +197,6 @@ let on_get_chan_mode chan me =
Ok (List.flatten rpls)
let on_set_chan_mode chan me modestr args ~router =
let* () = require_chan_op chan me in
let* chg = try Ok (Mode.Parse.chan_modes modestr args)
with Mode.Parse.Error ->
(* TODO: ERR_INVALIDMODEPARAM (696)
@ -201,6 +207,9 @@ let on_set_chan_mode chan me modestr args ~router =
Error modeunknownflag
in
let* mem = require_membership chan me in
let* () = require_chan_op mem in
set_chan_mode chan ~from:me ~add:chg.chan_modes.add ~rem:chg.chan_modes.rem;
Option.iter (set_chan_key chan ~from:me) chg.chan_key;
Option.iter (set_chan_limit chan ~from:me) chg.chan_limit;
@ -358,15 +367,16 @@ let on_msg_topic t name args =
with Not_found ->
Error (nosuchchannel name)
in
let* mem = require_membership chan me in
match args with
| [] ->
(* TODO: if +s then don't send topic to non-members *)
_todo_validation_please ();
get_topic t chan;
Ok ()
| args ->
(* TODO: if +t then only allow +o to set topic *)
_todo_validation_please ();
let* () =
if Mode.Set.mem `t (Chan.mode chan) then require_chan_op mem
else Ok ()
in
let topic = String.concat " " args in
let msg = Msg.make "TOPIC" [Chan.name chan; topic] ~always_trailing:true in
Router.relay msg ~from:me [`to_chan chan; `to_self];
@ -403,7 +413,7 @@ let on_msg_join t name =
| exception Not_found ->
begin
(* TODO: +k *)
(* TODO: +l *)
(* TODO: check chan user limit (+l) *)
_todo_validation_please ();
join t me chan;
get_topic t chan ~reply_if_missing:false;