add NOTICE command

This commit is contained in:
tali 2024-01-31 17:28:09 -05:00
parent 41bf46b8dc
commit 8acfc89f0d
1 changed files with 15 additions and 11 deletions

View File

@ -73,7 +73,7 @@ let away nick text = "301", [nick; text]
let nosuchnick tgt = "401", [tgt; "No such nick/channel"]
let nosuchchannel tgt = "403", [tgt; "No such channel"]
let cannotsendtochan tgt = "404", [tgt; "Cannot send to channel"]
let norecipient = "411", ["No recipient given (PRIVMSG)"]
let norecipient cmd = "411", [Fmt.str "No recipient given (%s)" cmd]
let notexttosend = "412", ["No text to send"]
let unknowncommand cmd = "421", [cmd; "Unknown command"]
let nonicknamegiven = "431", ["No nickname given"]
@ -282,7 +282,7 @@ let on_msg_mode t name args =
(* messages and channels *)
let on_privmsg_chan from chan =
let send_to_chan ~from chan =
let cannot_send =
try
let mem = Router.membership chan from in
@ -300,25 +300,25 @@ let on_privmsg_chan from chan =
else
Ok (Chan.name chan, [`to_chan chan])
let on_privmsg_user _from user =
let send_to_user user =
match User.away user with
| Some text ->
Error (away (User.nick user) text)
| None ->
Ok (User.nick user, [`to_user user])
let on_msg_privmsg t tgt txt =
let on_msg_privmsg ?(cmd = "PRIVMSG") t tgt txt =
let* me = require_registered t in
let* name, tgts =
try
match name_type tgt with
| `chan -> on_privmsg_chan me (Router.find_chan t.router tgt)
| `nick -> on_privmsg_user me (Router.find_user t.router tgt)
| `chan -> send_to_chan (Router.find_chan t.router tgt) ~from:me
| `nick -> send_to_user (Router.find_user t.router tgt)
| `invalid -> raise Not_found
with Not_found ->
Error (nosuchnick tgt)
in
let msg = Msg.make "PRIVMSG" [name; txt] ~always_trailing:true in
let msg = Msg.make cmd [name; txt] ~always_trailing:true in
Router.relay msg ~from:me tgts;
Ok ()
@ -920,10 +920,6 @@ let dispatch t = function
| "HELP", args -> on_msg_help t (concat_args args)
| "PING", args -> on_msg_ping t (concat_args args)
| "PONG", args -> on_msg_pong t (concat_args args)
| "PRIVMSG", ([] | "" :: _) -> Error norecipient
| "PRIVMSG", ([_] | _ :: "" :: _) -> Error notexttosend
| "PRIVMSG", tgt :: msg :: _ -> on_msg_privmsg t tgt msg
(* TODO: "NOTICE" *)
| "JOIN", tgt :: _ when tgt <> "" -> on_msg_join t tgt
| "JOIN 0", _ -> (* hack; see split_command_params *) on_msg_join_0 t
| "NAMES", tgt :: _ when tgt <> "" -> on_msg_names t tgt
@ -944,6 +940,14 @@ let dispatch t = function
Error (needmoreparams cmd)
| ("CONNECT" | "KILL" | "REHASH" | "RESTART" | "STATS" | "SQUIT" | "WALLOPS"), _ ->
Error noprivileges
| ("PRIVMSG" | "NOTICE") as cmd, args ->
begin match args with
| [] | "" :: _ -> Error (norecipient cmd)
| [_] | _ :: "" :: _ -> Error notexttosend
| tgt :: msg :: _ -> on_msg_privmsg t tgt msg ~cmd
end
(* TODO: "LIST" *)
| cmd, _ ->
Error (unknowncommand cmd)