add Router.Chan.mode, currently unused

This commit is contained in:
tali 2024-01-18 11:57:27 -05:00
parent 7f941a68a1
commit 5a355b1e45
3 changed files with 22 additions and 31 deletions

View File

@ -86,11 +86,6 @@ module Set = struct
let pp ppf s = let pp ppf s =
Format.pp_print_string ppf (to_string s) Format.pp_print_string ppf (to_string s)
let of_list l =
List.fold_left (fun s e -> add e s) empty l
(* TODO: i dont think the following two functions are useful outside of the expect tests *)
let of_string s = let of_string s =
let chr = function let chr = function
| 'i' -> `i | 'm' -> `m | 'n' -> `n | 'o' -> `o | 'i' -> `i | 'm' -> `m | 'n' -> `n | 'o' -> `o
@ -98,14 +93,6 @@ module Set = struct
| _ -> invalid_arg "Irc.Mode.Set.of_string" | _ -> invalid_arg "Irc.Mode.Set.of_string"
in in
String.fold_left (fun s c -> add (chr c) s) empty s String.fold_left (fun s c -> add (chr c) s) empty s
let to_list s =
let cons x xs =
if mem x s then x :: xs
else xs
in
cons `i @@ cons `m @@ cons `n @@ cons `o @@
cons `s @@ cons `t @@ cons `w []
end end
@ -177,15 +164,14 @@ let%expect_test _ =
let print_bool_nl b = print_endline (if b then "true" else "false") in let print_bool_nl b = print_endline (if b then "true" else "false") in
print_set_nl Set.empty; [%expect {| [] |}]; print_set_nl Set.empty; [%expect {| [] |}];
print_set_nl (Set.of_list [`i]); [%expect {| [i] |}]; print_set_nl Set.(of_string "i"); [%expect {| [i] |}];
print_set_nl (Set.of_list [`n; `o]); [%expect {| [no] |}]; print_set_nl Set.(of_string "no"); [%expect {| [no] |}];
print_set_nl (Set.of_list [`s; `m]); [%expect {| [ms] |}]; print_set_nl Set.(of_string "sm"); [%expect {| [ms] |}];
print_set_nl (Set.of_string "wi"); [%expect {| [iw] |}]; print_set_nl Set.(of_string "wi"); [%expect {| [iw] |}];
print_set_nl (Set.of_string "wi"); [%expect {| [iw] |}]; print_bool_nl Set.(mem `i (of_string "ins")); [%expect "true"];
print_bool_nl (Set.(mem `i (of_string "ins"))); [%expect "true"]; print_bool_nl Set.(mem `w (of_string "ins")); [%expect "false"];
print_bool_nl (Set.(mem `w (of_string "ins"))); [%expect "false"]; print_bool_nl Set.(mem `w (of_string "wwww")); [%expect "true"];
print_bool_nl (Set.(mem `w (of_string "wwww"))); [%expect "true"]; print_bool_nl Set.(mem `t (of_string "imnosw")); [%expect "false"];
print_bool_nl (Set.(mem `t (of_string "imnosw"))); [%expect "false"];
let print_user_mode_set_nl um = let print_user_mode_set_nl um =
Format.printf "%a\n" Parse.pp_user_mode_set um Format.printf "%a\n" Parse.pp_user_mode_set um

View File

@ -19,9 +19,9 @@
Type D: Type D:
+i Invite-Only Channel Mode +i Invite-Only Channel Mode
+m Moderated Channel Mode +m Moderated Channel Mode
+n No External Messages Mode
+s Secret Channel Mode +s Secret Channel Mode
+t Protected Topic Channel Mode +t Protected Topic Channel Mode
+n No External Messages Mode
*) *)
type user = [`i | `o | `w] type user = [`i | `o | `w]
@ -54,8 +54,8 @@ module Set : sig
val pp : Format.formatter -> t -> unit val pp : Format.formatter -> t -> unit
val to_string : t -> string val to_string : t -> string
val of_string : string -> t val of_string : string -> t
val to_list : t -> elt list (* val to_list : t -> elt list *)
val of_list : [< elt] list -> t (* val of_list : [< elt] list -> t *)
end end
module Parse : sig module Parse : sig

View File

@ -8,7 +8,7 @@ type t = {
and user = { and user = {
outbox : Outbox.t; outbox : Outbox.t;
userinfo : Irc.userinfo; userinfo : Irc.userinfo;
mutable mode : Irc.Mode.Set.t; mutable user_mode : Irc.Mode.Set.t;
mutable nick : Irc.name; mutable nick : Irc.name;
mutable nick_key : string_ci; mutable nick_key : string_ci;
mutable membership : membership Dllist.t; mutable membership : membership Dllist.t;
@ -19,6 +19,10 @@ and chan = {
name_key : string_ci; name_key : string_ci;
mutable topic : string option; mutable topic : string option;
mutable members : membership Dllist.t; mutable members : membership Dllist.t;
mutable chan_mode : Irc.Mode.Set.t; (* +imstn *)
(* TODO: +b, +o, +v *)
(* TODO: +k *)
(* TODO: +l *)
} }
and membership = { and membership = {
@ -79,13 +83,14 @@ module User = struct
userinfo; userinfo;
nick = "*"; nick = "*";
nick_key = empty_string_ci; nick_key = empty_string_ci;
mode = Irc.Mode.Set.of_list [`i; `w]; user_mode = Irc.Mode.Set.of_string "iw";
membership = Dllist.create (); membership = Dllist.create ();
} }
let outbox t = t.outbox let outbox t = t.outbox
let nick t = t.nick let nick t = t.nick
let mode t = t.mode let mode t = t.user_mode
let set_mode t new_mode = t.user_mode <- new_mode
let channels = user_channels let channels = user_channels
let prefix = user_prefix let prefix = user_prefix
(* let is_registered t = t.nick_key <> empty_string_ci *) (* let is_registered t = t.nick_key <> empty_string_ci *)
@ -108,9 +113,6 @@ module User = struct
`nick_set `nick_set
end end
let set_mode t new_mode =
t.mode <- new_mode
let rec part_all t = let rec part_all t =
(* List.iter (fun c -> Chan.part c t) (channels t) *) (* List.iter (fun c -> Chan.part c t) (channels t) *)
match Dllist.take_l t.membership with match Dllist.take_l t.membership with
@ -131,12 +133,15 @@ module Chan = struct
name_key = string_ci name; name_key = string_ci name;
topic = None; topic = None;
members = Dllist.create (); members = Dllist.create ();
chan_mode = Irc.Mode.Set.of_string "nst";
} }
let name t = t.name let name t = t.name
let topic t = t.topic let topic t = t.topic
let members = chan_members let members = chan_members
let no_members t = Dllist.is_empty t.members let no_members t = Dllist.is_empty t.members
let mode t = t.chan_mode
let set_mode t new_mode = t.chan_mode <- new_mode
let register t ~router = let register t ~router =
Hashtbl.replace router.channels t.name_key t Hashtbl.replace router.channels t.name_key t