From 6fdd47cd127971127ca5d9ba0b01e4e934e42400 Mon Sep 17 00:00:00 2001 From: tali Date: Wed, 31 Jan 2024 16:47:42 -0500 Subject: [PATCH] use ptime for displaying channel/server creation time --- lib/server/chan.ml | 8 +++++++- lib/server/connection.ml | 4 ++-- lib/server/dune | 2 +- lib/server/import.ml | 5 +++-- lib/server/router_types.ml | 2 +- lib/server/server_info.ml | 8 ++++++-- lib/server/test_router.ml | 4 ++-- 7 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/server/chan.ml b/lib/server/chan.ml index b9000fa..a2f7aeb 100644 --- a/lib/server/chan.ml +++ b/lib/server/chan.ml @@ -3,10 +3,15 @@ include Router_types type t = chan -let make ~name = +let make ?creation_time name = + let creation_time = match creation_time with + | None -> Ptime_clock.now () + | Some ts -> ts + in { name; name_key = string_ci name; + creation_time; topic = None; members = Dllist.create (); member_count = 0; @@ -16,6 +21,7 @@ let make ~name = } let name t = t.name +let creation_time t = t.creation_time let topic t = t.topic let set_topic t s = t.topic <- s let mode t = t.chan_mode diff --git a/lib/server/connection.ml b/lib/server/connection.ml index 2279be8..ec51efb 100644 --- a/lib/server/connection.ml +++ b/lib/server/connection.ml @@ -210,7 +210,7 @@ let on_get_chan_mode chan me = | None -> [] end; - (* TODO: RPL_CREATIONTIME (329) *) + ["329", [Chan.name chan; Fmt.str "%a" pp_unixtime (Chan.creation_time chan)]]; ] in Ok (List.flatten rpls) @@ -447,7 +447,7 @@ let on_msg_join t name = Error (nosuchchannel name) with Not_found -> debug (fun m -> m "making new channel %S" name); - Ok (Chan.make ~name) + Ok (Chan.make name) in match Router.membership chan me with | _already_a_member -> Ok () diff --git a/lib/server/dune b/lib/server/dune index 2f21d0b..914b6d8 100644 --- a/lib/server/dune +++ b/lib/server/dune @@ -2,7 +2,7 @@ (package talircd) (name server) (libraries - lwt lwt.unix fmt + lwt lwt.unix fmt ptime ptime.clock.os logging irc data) (inline_tests) (preprocess (pps ppx_expect))) diff --git a/lib/server/import.ml b/lib/server/import.ml index 860ba42..632287c 100644 --- a/lib/server/import.ml +++ b/lib/server/import.ml @@ -8,8 +8,9 @@ let pp_sockaddr ppf = function | Unix.ADDR_INET (adr, port) -> Fmt.pf ppf "%s:%d" (Unix.string_of_inet_addr adr) port | Unix.ADDR_UNIX path -> Fmt.string ppf path -let defer f = - Lwt.on_success (Lwt.pause ()) f +let pp_unixtime ppf t = + let dt = Ptime.diff t Ptime.epoch in + Fmt.pf ppf "%.0f" (Ptime.Span.to_float_s dt) module Result_syntax = struct let ( let* ) = Result.bind diff --git a/lib/server/router_types.ml b/lib/server/router_types.ml index 9bc2219..6f9c0a8 100644 --- a/lib/server/router_types.ml +++ b/lib/server/router_types.ml @@ -16,6 +16,7 @@ type user = { and chan = { name : name; name_key : string_ci; + creation_time : Ptime.t; mutable topic : string option; mutable members : membership Dllist.t; mutable member_count : int; @@ -23,7 +24,6 @@ and chan = { mutable chan_limit : int option; (* +l *) mutable chan_key : string option; (* +k *) (* TODO: +b *) - (* TODO: creation time *) } and membership = { diff --git a/lib/server/server_info.ml b/lib/server/server_info.ml index 21bbcd1..956d8c3 100644 --- a/lib/server/server_info.ml +++ b/lib/server/server_info.ml @@ -39,8 +39,12 @@ let make ~hostname = { (* TODO: generate version string at build time? *) "0.0.0"; created = - (* TODO: stringify timestamp *) - "Sun Jan 7 09:58:24 PM EST 2024"; + begin + let ts = Ptime_clock.now () in + let tz_offset_s = Ptime_clock.current_tz_offset_s () in + let pp = Ptime.pp_human () ?tz_offset_s in + Fmt.str "%a" pp ts + end; hostname; admin_info = (* TODO: make configurable *) diff --git a/lib/server/test_router.ml b/lib/server/test_router.ml index f227f3c..f40b7a1 100644 --- a/lib/server/test_router.ml +++ b/lib/server/test_router.ml @@ -31,8 +31,8 @@ let%expect_test _ = ~outbox:(Outbox.make ()) in - let c1 = Chan.make ~name:"#wire-fraud" in - let c2 = Chan.make ~name:"#spiderman" in + let c1 = Chan.make "#wire-fraud" in + let c2 = Chan.make "#spiderman" in User.register u1 ~router; User.register u2 ~router;