2024-01-10 00:35:03 +00:00
|
|
|
open! Import
|
|
|
|
|
2024-01-12 02:49:48 +00:00
|
|
|
(* include (val Logging.sublogs logger "Outbox") *)
|
|
|
|
|
2024-01-10 00:35:03 +00:00
|
|
|
type t = {
|
|
|
|
stream : Irc.Msg.t Lwt_stream.t;
|
|
|
|
push : Irc.Msg.t option -> unit;
|
2024-01-14 18:21:16 +00:00
|
|
|
mutable bcc_incl : bool;
|
2024-01-10 00:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let make () =
|
|
|
|
let stream, push = Lwt_stream.create () in
|
2024-01-14 18:21:16 +00:00
|
|
|
{ stream; push; bcc_incl = false }
|
2024-01-10 00:35:03 +00:00
|
|
|
|
|
|
|
let stream t = t.stream
|
|
|
|
let send t msg = try t.push (Some msg) with Lwt_stream.Closed -> ()
|
|
|
|
let close t = try t.push None with Lwt_stream.Closed -> ()
|
2024-01-10 02:19:22 +00:00
|
|
|
|
2024-01-11 04:41:01 +00:00
|
|
|
module Bcc = struct
|
2024-01-14 18:21:16 +00:00
|
|
|
(** this module is used to send a message to a number of outboxes at once without sending
|
|
|
|
to the same destination twice. it uses a global linked list of recipients with an
|
|
|
|
intrusive bool ([outbox.bcc_incl]) to prevent enqueuing the same destination twice.
|
|
|
|
|
|
|
|
[Bcc.add a; Bcc.add b; Bcc.send_all msg;
|
|
|
|
(* sends [msg] to [a] and [b]. *)]
|
|
|
|
*)
|
2024-01-11 04:41:01 +00:00
|
|
|
|
2024-01-14 18:21:16 +00:00
|
|
|
let _recipients = Dllist.create ()
|
2024-01-11 04:41:01 +00:00
|
|
|
|
2024-01-14 18:21:16 +00:00
|
|
|
let add obx =
|
|
|
|
if not obx.bcc_incl then
|
|
|
|
let _ = Dllist.add_r obx _recipients in
|
|
|
|
obx.bcc_incl <- true
|
2024-01-11 04:41:01 +00:00
|
|
|
|
|
|
|
let rec send_all msg =
|
|
|
|
match Dllist.take_l _recipients with
|
2024-01-14 18:21:16 +00:00
|
|
|
| obx -> obx.bcc_incl <- false; send obx msg; send_all msg
|
2024-01-11 04:41:01 +00:00
|
|
|
| exception Dllist.Empty -> ()
|
|
|
|
end
|