parse comma separated targets into individual commands

This commit is contained in:
tali 2024-01-11 00:13:08 -05:00
parent 1f81abfad2
commit f383706931
1 changed files with 38 additions and 24 deletions

View File

@ -155,7 +155,6 @@ let on_msg_user t username realname =
let on_msg_privmsg t tgt txt = let on_msg_privmsg t tgt txt =
let* me = require_registered t in let* me = require_registered t in
(* TODO: comma-separated list of targets *)
let* dst = let* dst =
try try
match Irc.name_type tgt with match Irc.name_type tgt with
@ -184,7 +183,6 @@ let list_names t chan =
let on_msg_names t name = let on_msg_names t name =
let* _me = require_registered t in let* _me = require_registered t in
(* TODO: comma-separated list of channels *)
let* chan = let* chan =
try try
match Irc.name_type name with match Irc.name_type name with
@ -199,7 +197,7 @@ let on_msg_names t name =
let on_msg_join t name = let on_msg_join t name =
let* me = require_registered t in let* me = require_registered t in
(* TODO: comma-separated list of channels *) (* TODO: keys parameter *)
(* TODO: "0" parameter means part from all channels *) (* TODO: "0" parameter means part from all channels *)
let* chan = let* chan =
try try
@ -213,7 +211,7 @@ let on_msg_join t name =
(* TODO: make user +o *) (* TODO: make user +o *)
Ok chan Ok chan
in in
(* TODO: check if channel is +k, get associated key from parameters *) (* TODO: check if channel is +k *)
Chan.join chan me; Chan.join chan me;
let msg = Irc.Msg.make "JOIN" [name] in let msg = Irc.Msg.make "JOIN" [name] in
Router.relay msg ~from:me `to_self; Router.relay msg ~from:me `to_self;
@ -264,10 +262,7 @@ let on_msg_quit t reason =
(* message parsing *) (* message parsing *)
let on_msg t (msg : Irc.Msg.t) : unit = let dispatch t = function
(* Logs.debug (fun m -> m "%a: %a" pp_sockaddr t.addr Irc.Msg.pp msg); *)
let result =
match msg.command, msg.params with
| "NICK", nick :: _ when nick <> "" -> on_msg_nick t nick | "NICK", nick :: _ when nick <> "" -> on_msg_nick t nick
| "NICK", _ -> Error nonicknamegiven | "NICK", _ -> Error nonicknamegiven
| "USER", unm :: _ :: _ :: rnm :: _ -> on_msg_user t unm rnm | "USER", unm :: _ :: _ :: rnm :: _ -> on_msg_user t unm rnm
@ -278,10 +273,29 @@ let on_msg t (msg : Irc.Msg.t) : unit =
| "JOIN", tgt :: _ when tgt <> "" -> on_msg_join t tgt | "JOIN", tgt :: _ when tgt <> "" -> on_msg_join t tgt
| "NAMES", tgt :: _ when tgt <> "" -> on_msg_names t tgt | "NAMES", tgt :: _ when tgt <> "" -> on_msg_names t tgt
| "PART", tgt :: _ when tgt <> "" -> on_msg_part t tgt | "PART", tgt :: _ when tgt <> "" -> on_msg_part t tgt
| "USER", _ | "JOIN", _ | "NAMES", _ | "PART", _ -> | ("USER" | "JOIN" | "NAMES" | "PART") as cmd, _ ->
Error (needmoreparams msg.command) Error (needmoreparams cmd)
| _, _ -> Error (unknowncommand msg.command) | cmd, _ ->
in Error (unknowncommand cmd)
match result with
let split_command_params cmd params =
match cmd, params with
| ("PRIVMSG" | "JOIN" | "NAMES" | "PART"), tgts :: rest
when String.contains tgts ',' ->
(* TODO: "JOIN" should be handled specially *)
String.split_on_char ',' tgts |>
List.map (fun tgt -> cmd, tgt :: rest)
| _ ->
[cmd, params]
let pp_args ppf (cmd, params) =
Fmt.pf ppf "%s %a" cmd (Fmt.list (Fmt.fmt "%S") ~sep:Fmt.sp) params
let on_msg t (msg : Irc.Msg.t) : unit =
split_command_params msg.command msg.params |>
List.iter
(fun args ->
Logs.debug (fun m -> m "%a: %a" pp_sockaddr t.addr pp_args args);
match dispatch t args with
| Ok () -> () | Ok () -> ()
| Error err -> reply t err | Error err -> reply t err)