From 6790c22c4a9d7e95e2f1e81a68879c1dd85aa3ff Mon Sep 17 00:00:00 2001 From: tali Date: Thu, 1 Feb 2024 13:28:31 -0500 Subject: [PATCH] load MOTD from file --- bin/main.ml | 10 ++++++++-- lib/server/server.ml | 30 ++++++++++++++++++++++-------- lib/server/server_info.ml | 23 +++++++++-------------- talircd.service | 1 + 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/bin/main.ml b/bin/main.ml index 6400c27..3f1d70a 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -23,6 +23,8 @@ let () = ~timestamp:(not no_timestamp) ~namespace:(not no_namespace) +(* TODO: s-exp/json/toml config format *) + let port = try let port = int_of_string (Sys.getenv "IRC_PORT") in @@ -36,14 +38,18 @@ let hostname = | Some x -> x | None -> "irc.tali.software" +let motd_file = + match Sys.getenv_opt "IRC_MOTD" with + | Some x -> x + | None -> "./motd.txt" + let config : Server.config = { port; hostname; listen_backlog = 8; ping_interval = 60; whowas_history_len = 1000; - - (* TODO: motd *) + motd_file; } let () = diff --git a/lib/server/server.ml b/lib/server/server.ml index 048c068..987bdbb 100644 --- a/lib/server/server.ml +++ b/lib/server/server.ml @@ -10,7 +10,7 @@ let listener ~(port : int) ~(listen_backlog : int) : (fd * sockaddr) Lwt_stream. let sock : fd Lwt.t = let fd = Lwt_unix.socket PF_INET SOCK_STREAM 0 in Lwt_unix.setsockopt fd SO_KEEPALIVE false; - Lwt_unix.setsockopt fd SO_REUSEPORT true; + Lwt_unix.setsockopt fd SO_REUSEPORT false; let srv_adr = Unix.ADDR_INET (Unix.inet_addr_any, port) in let* () = Lwt_unix.bind fd srv_adr in Lwt_unix.listen fd listen_backlog; @@ -98,18 +98,32 @@ type config = { ping_interval : int; whowas_history_len : int; hostname : string; - (* TODO: motd *) + motd_file : string; } -let run { port; listen_backlog; ping_interval; - whowas_history_len; hostname } : unit Lwt.t +let run { port; listen_backlog; ping_interval; whowas_history_len; + hostname; motd_file } : unit Lwt.t = - let server_info = - Server_info.make - ~hostname - (* ~motd *) + debug (fun m -> m "ping interval:@ %ds" ping_interval); + debug (fun m -> m "whowas history:@ %d" whowas_history_len); + + let* motd = + let* file = Lwt_io.open_file motd_file ~mode:Input in + let* lines = Lwt_io.read_lines file |> Lwt_stream.to_list in + let+ () = Lwt_io.close file in + debug (fun m -> m "motd file:@ %d lines" (List.length lines)); + lines in + let server_info = + Server_info.make () + ~hostname + ~motd + in + info (fun m -> m "hostname:@ %s" server_info.hostname); + info (fun m -> m "version:@ %s" server_info.version); + info (fun m -> m "created:@ %s" server_info.created); + let router : Router.t = Router.make ~whowas_history_len diff --git a/lib/server/server_info.ml b/lib/server/server_info.ml index f26f2d7..45bc0a6 100644 --- a/lib/server/server_info.ml +++ b/lib/server/server_info.ml @@ -37,22 +37,17 @@ let default_conf = { init_cmode = Mode.Set.of_list [`n; `s; `t]; } -let make ~hostname = { - version = - (* TODO: generate version string at build time? *) - "0.0.0"; +let admin_info = "the admin of this server is @iitalics@octodon.social" +let version = "0.0.0" +(* TODO: generate version string at build time? *) + +let make ?(conf = default_conf) ~hostname ~motd () = { + version = version; created = Fmt.str "%a" pp_time (Ptime_clock.now ()); hostname; - admin_info = - (* TODO: make configurable *) - "the admin of this server is @iitalics@octodon.social"; - motd = [ - (* TODO: load from file *) - "MEOW MEOW MEOW MEOW MEOW"; - "meow meow meow meow meow"; - "meowmeowmeowmeowmeowmeow"; - ]; - conf = default_conf; + admin_info; + motd; + conf; } let prefix t = Msg.Server_prefix t.hostname diff --git a/talircd.service b/talircd.service index 9ef0e0a..cac8450 100644 --- a/talircd.service +++ b/talircd.service @@ -3,6 +3,7 @@ Description=tali IRCd [Service] Environment=IRC_HOSTNAME=irc.tali.software +Environment=IRC_MOTD=/usr/local/share/talircd/motd #Environment=IRC_PORT=6667 #Environment=LOG_LEVEL=DEBUG ExecStart=/usr/local/bin/talircd