diff options
author | Alban Gruin | 2020-12-30 17:40:57 +0100 |
---|---|---|
committer | Alban Gruin | 2020-12-30 17:40:57 +0100 |
commit | 3e9d3b3437f2d63a47f9c962ba9187c5ef692328 (patch) | |
tree | 8b1f5e122ac9caeff3f56849a516e7b10d788d80 | |
parent | 8dff924f4d598da653794e29fb9929017e161aa3 (diff) |
ics: print the timezone name in the ICS file
Add the correct timezone in generated ICS files. The timezone is
fetched from /etc/timezone.
Even though this is not standard, if a tool needs it, it should be able
to understand it correctly.
Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
-rw-r--r-- | src/ics.ml | 14 | ||||
-rw-r--r-- | src/ics.mli | 2 | ||||
-rw-r--r-- | src/server.ml | 15 |
3 files changed, 28 insertions, 3 deletions
@@ -61,8 +61,19 @@ type t = Event.t list let make events = events -let to_string events = +let gen_vtimezone tz = + if tz <> "" then + ["BEGIN:VTIMEZONE"; + "TZID:" ^ tz; + "END:VTIMEZONE\n"] + |> List.map ics_split_line + |> String.concat "\n" + else + "" + +let to_string tz events = let date = current_date () in + let vtimezone = gen_vtimezone tz in let rec gen_events id str = function | [] -> str | event :: l -> @@ -70,5 +81,6 @@ let to_string events = "BEGIN:VCALENDAR\n\ VERSION:2.0\n\ PRODID:-//ucs//\n" + ^ vtimezone ^ gen_events 0 "" events ^ "END:VCALENDAR\n" diff --git a/src/ics.mli b/src/ics.mli index 219f851..a4cbbd6 100644 --- a/src/ics.mli +++ b/src/ics.mli @@ -26,4 +26,4 @@ end type t val make : Event.t list -> t -val to_string : t -> string +val to_string : string -> t -> string diff --git a/src/server.ml b/src/server.ml index d3cf00e..c1a0ba9 100644 --- a/src/server.ml +++ b/src/server.ml @@ -47,7 +47,20 @@ let respond ?(mime="text/html; charset=utf-8") ?(status=`OK) body = let headers = Header.init_with "Content-Type" mime in Server.respond_string ~status ~body ~headers () +let get_tz () = + Lwt_unix.file_exists "/etc/timezone" >>= fun exists -> + if exists then + Lwt_io.(open_file ~mode:Input "/etc/timezone") >>= fun file -> + Lwt.finalize + (fun () -> + Lwt_io.read file >|= + String.trim) + (fun () -> Lwt_io.close file) + else + Lwt.return "" + let serve base_url celcat_url mode stop = + get_tz () >>= fun tzname -> let fetch = fetch celcat_url in let callback _conn req _body = let meth = Request.meth req and @@ -68,7 +81,7 @@ let serve base_url celcat_url mode stop = let group = String.(sub file 0 (length file - 4)) in fetch group >>= fun body -> Course.decode body - |> Ics.to_string + |> Ics.to_string tzname |> respond ~mime:"text/calendar; charset=utf-8" | `GET, _, _ -> Server.respond_string ~status:`Not_found ~body:"Not found\n" () |