64 lines
1.5 KiB
OCaml
64 lines
1.5 KiB
OCaml
open! Import
|
|
include Router_types
|
|
|
|
type t = chan
|
|
|
|
let value_or_now = function
|
|
| None -> Ptime_clock.now ()
|
|
| Some ts -> ts
|
|
|
|
let make ?time name = {
|
|
name;
|
|
name_key = string_ci name;
|
|
creation_time = value_or_now time;
|
|
topic = None, None;
|
|
members = Dllist.create ();
|
|
member_count = 0;
|
|
chan_mode = Mode.Set.empty;
|
|
chan_limit = None;
|
|
chan_key = None;
|
|
}
|
|
|
|
let name t = t.name
|
|
let creation_time t = t.creation_time
|
|
let topic t = fst t.topic
|
|
let topic_who_time t = snd t.topic
|
|
let set_topic ~who ?time t text = t.topic <- text, Some (who, value_or_now time)
|
|
let member_count t = t.member_count
|
|
let mode t = t.chan_mode
|
|
let set_mode t new_mode = t.chan_mode <- new_mode
|
|
let limit t = t.chan_limit
|
|
let set_limit t n = t.chan_limit <- n
|
|
let key t = t.chan_key
|
|
let set_key t k = t.chan_key <- k
|
|
|
|
let register t ~router =
|
|
begin
|
|
Hashtbl.replace router.channels t.name_key t;
|
|
router.luserchannels <- succ router.luserchannels;
|
|
end
|
|
|
|
let unregister t ~router =
|
|
begin
|
|
Hashtbl.remove router.channels t.name_key;
|
|
router.luserchannels <- pred router.luserchannels;
|
|
end
|
|
|
|
let is_registered t ~router = Hashtbl.mem router.channels t.name_key
|
|
|
|
let members t =
|
|
Dllist.fold_r (fun m xs -> m.mem_user :: xs) t.members []
|
|
|
|
let membership t =
|
|
Dllist.fold_r (fun m xs -> m :: xs) t.members []
|
|
|
|
let membership_when f t =
|
|
Dllist.fold_r (fun m xs -> if f m then m :: xs else xs) t.members []
|
|
|
|
let is_empty t =
|
|
t.member_count = 0
|
|
|
|
let is_full t = match t.chan_limit with
|
|
| Some n -> t.member_count >= n
|
|
| None -> false
|