add AWAY command

This commit is contained in:
tali 2024-01-30 21:30:54 -05:00
parent a57d5b532a
commit 7de285524a
3 changed files with 24 additions and 4 deletions

View File

@ -49,7 +49,7 @@ let reply t (num, params) =
| None -> "*"
in
let always_trailing = match num with
| "332" | "353" -> true
| "301" | "332" | "353" -> true
| _ -> false
in
Outbox.send t.outbox
@ -57,6 +57,7 @@ let reply t (num, params) =
~prefix ~always_trailing)
let tryagain cmd = "263", [cmd; "Please wait a while and try again."]
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"]
@ -291,8 +292,9 @@ let on_privmsg_chan from chan =
Ok (Chan.name chan, [`to_chan chan])
let on_privmsg_user _from user =
(* TODO: check if user is away *)
Ok (User.nick user, [`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* me = require_registered t in
@ -309,6 +311,19 @@ let on_msg_privmsg t tgt txt =
Router.relay msg ~from:me tgts;
Ok ()
let on_msg_away t status =
let* me = require_registered t in
if status <> User.away me then
begin
let rpl = match status with
| None -> "305", ["You are no longer marked as being away"]
| Some _ -> "306", ["You have been marked as being away"]
in
User.set_away me status;
reply t rpl
end;
Ok ()
let list_names t me chan =
let is_secret = Mode.Set.mem `s (Chan.mode chan) in
let is_invisible user = Mode.Set.mem `i (User.mode user) in
@ -631,7 +646,7 @@ let on_msg_user t username realname =
(* message parsing *)
let concat_args = function
| [] -> None
| [] | [""] -> None
| xs -> Some (String.concat " " xs)
let dispatch t = function
@ -650,6 +665,7 @@ let dispatch t = function
| "PART", tgt :: reason when tgt <> "" -> on_msg_part t tgt (concat_args reason)
| "KICK", chn :: tgt :: comment when chn <> "" && tgt <> "" ->
on_msg_kick t chn tgt (concat_args comment)
| "AWAY", args -> on_msg_away t (concat_args args)
| "MODE", tgt :: args when tgt <> "" -> on_msg_mode t tgt args
| ("USER" | "JOIN" | "NAMES" | "PART" | "KICK" | "MODE") as cmd, _ ->
Error (needmoreparams cmd)

View File

@ -9,6 +9,7 @@ type user = {
mutable nick : name;
mutable nick_key : string_ci;
mutable user_mode : Mode.Set.t;
mutable away : string option;
mutable membership : membership Dllist.t;
}

View File

@ -10,6 +10,7 @@ let make nick ~userinfo ~outbox =
nick;
nick_key = string_ci nick;
user_mode = Irc.Mode.Set.empty;
away = None;
membership = Dllist.create ();
}
@ -17,6 +18,8 @@ let outbox t = t.outbox
let nick t = t.nick
let mode t = t.user_mode
let set_mode t new_mode = t.user_mode <- new_mode
let away t = t.away
let set_away t status = t.away <- status
let set_nick t new_nick =
begin