add fake JOIN command

This commit is contained in:
tali 2024-01-08 00:31:05 -05:00
parent 4dc454e9a2
commit bd5afe7ad1
2 changed files with 20 additions and 9 deletions

View File

@ -42,10 +42,8 @@ let attempt_to_register t =
| `nick_in_use -> `nicknameinuse nick | `nick_in_use -> `nicknameinuse nick
| `nick_set -> | `nick_set ->
Logs.debug (fun m -> m "init mode: +%a" Irc.Mode.pp t.user.mode); Logs.debug (fun m -> m "init mode: +%a" Irc.Mode.pp t.user.mode);
User.send t.user send t (Irc.Msg.make "MODE" [nick; Fmt.str "+%a" Irc.Mode.pp t.user.mode]
(Irc.Msg.make "MODE" [nick; Fmt.str "+%a" Irc.Mode.pp t.user.mode] ~prefix:(User.prefix t.user));
~prefix:(User_prefix (nick, None, None)));
(* TODO: set initial mode *)
`welcome `welcome
end end
| _, _ -> `ok | _, _ -> `ok
@ -78,14 +76,18 @@ let on_msg_privmsg t tgt msg _ =
| None -> `nosuchnick tgt | None -> `nosuchnick tgt
| Some dst -> Router.privmsg t.user (`user dst) msg; `ok | Some dst -> Router.privmsg t.user (`user dst) msg; `ok
let on_msg_join t tgt _ =
send t (Irc.Msg.make "JOIN" [tgt] ~prefix:(User.prefix t.user));
`names ("@", tgt, ["@", User.nick t.user; "", "moe"; "", "barry"])
let on_msg_privmsg t tgt msg = require_registered t (on_msg_privmsg t tgt msg) let on_msg_privmsg t tgt msg = require_registered t (on_msg_privmsg t tgt msg)
let on_msg_join t tgt = require_registered t (on_msg_join t tgt)
(* > misc *) (* > misc *)
let on_msg_quit t why = let on_msg_quit t why =
let why = String.concat " " why in let why = String.concat " " why in
Logs.debug (fun m -> m "%a: quit: %S" pp_sockaddr t.addr why); Logs.debug (fun m -> m "%a: quit: %S" pp_sockaddr t.addr why);
(* broadcast "quit" message *)
`quit `quit
(* message sending *) (* message sending *)
@ -123,6 +125,12 @@ let rpl_motd t =
rpl t "376" ["End of /MOTD command"]; rpl t "376" ["End of /MOTD command"];
end end
let rpl_names t chan_prefix chan users =
begin
List.iter (fun (up, u) -> rpl t "353" [chan_prefix; chan; up ^ u]) users;
rpl t "366" [chan; "End of NAMES list"]
end
let err_alreadyregistered t = rpl t "462" ["Unauthorized command (already registered)"] let err_alreadyregistered t = rpl t "462" ["Unauthorized command (already registered)"]
let err_needmoreparams t cmd = rpl t "461" [cmd; "Not enough parameters"] let err_needmoreparams t cmd = rpl t "461" [cmd; "Not enough parameters"]
let err_nicknameinuse t nick = rpl t "433" [nick; "Nickname is already in use"] let err_nicknameinuse t nick = rpl t "433" [nick; "Nickname is already in use"]
@ -147,13 +155,16 @@ let on_msg t (msg : Irc.Msg.t) : unit =
| "PRIVMSG", tgt :: msg :: _ -> | "PRIVMSG", tgt :: msg :: _ ->
on_msg_privmsg t tgt msg on_msg_privmsg t tgt msg
| "PRIVMSG", [_] -> `notexttosend | "PRIVMSG", [] -> `norecipient | "PRIVMSG", [_] -> `notexttosend | "PRIVMSG", [] -> `norecipient
| "NICK", _ | "USER", _ -> `needmoreparams | "JOIN", tgt :: _ ->
on_msg_join t tgt
| "NICK", _ | "USER", _ | "JOIN", _ -> `needmoreparams
| _, _ -> `unknowncommand | _, _ -> `unknowncommand
in in
match result with match result with
| `ok -> () | `ok -> ()
| `quit -> close t | `quit -> close t
| `welcome -> rpl_welcome t; rpl_motd t | `welcome -> rpl_welcome t; rpl_motd t
| `names (cp, ch, us) -> rpl_names t cp ch us
| `tryagain -> rpl_tryagain t msg.command | `tryagain -> rpl_tryagain t msg.command
| `alreadyregistered -> err_alreadyregistered t | `alreadyregistered -> err_alreadyregistered t
| `needmoreparams -> err_needmoreparams t msg.command | `needmoreparams -> err_needmoreparams t msg.command

View File

@ -60,7 +60,7 @@ module User = struct
if Hashtbl.mem t.router.users key then if Hashtbl.mem t.router.users key then
`nick_in_use `nick_in_use
else begin else begin
((* TODO: notify others of nick change *)); ((* TODO: relay NICK message *));
unset_nick t; unset_nick t;
Hashtbl.add t.router.users key t; Hashtbl.add t.router.users key t;
t.key <- key; t.key <- key;
@ -68,14 +68,14 @@ module User = struct
end end
let cleanup t = let cleanup t =
(* TODO: notify other users of quit *) (* TODO: relay QUIT message *)
close t; close t;
unset_nick t unset_nick t
end end
let privmsg src dst txt = let privmsg src dst txt =
let prefix = User.prefix src in
match dst with match dst with
| `user dst -> | `user dst ->
let prefix = User.prefix src in
let msg = Irc.Msg.make "PRIVMSG" [User.nick dst; txt] ~prefix in let msg = Irc.Msg.make "PRIVMSG" [User.nick dst; txt] ~prefix in
User.send dst msg User.send dst msg