diff --git a/channel.ml b/channel.ml index 8b79f5f..28b1f98 100644 --- a/channel.ml +++ b/channel.ml @@ -46,13 +46,23 @@ let broadcast ?(metoo=false) chan sender command args text = Iobuf.write iobuf cmd in String_map.iter bwrite !(chan.clients) - let reply iobuf nick num ?(args=[]) text = write iobuf num (nick :: args) (Some text) let handle_command cli nuhost cmd = match (Command.as_tuple cmd) with + | (None, ("NOTICE" as cmd_name), [name], Some text) + | (None, ("PRIVMSG" as cmd_name), [name], Some text) -> + let nick = Irc.nick nuhost in + (try + let chan = String_map.find name !by_name in + if String_map.mem nick !(chan.clients) then + broadcast chan (cli, nuhost) cmd_name [name] (Some text) + else + reply cli nick "404" ~args:[name] "Cannot send to channel" + with Not_found -> + reply cli nick "403" ~args:[name] "No such channel") | (None, "JOIN", ["0"], None) -> (* Leave all channels *) failwith "XXX: JOIN 0" diff --git a/client.ml b/client.ml index 89676c3..4452412 100644 --- a/client.ml +++ b/client.ml @@ -23,8 +23,7 @@ let lookup nick = let error num args text = Error (Command.create (Some !(Irc.name)) num args (Some text)) -let uhost cli = - (!(cli.nick) ^ "!" ^ cli.username ^ "@" ^ (Iobuf.addr cli.iobuf)) +let nuhost cli = (!(cli.nick), cli.username, (Iobuf.addr cli.iobuf)) let kill cli message = Iobuf.close cli.iobuf ("Killed: " ^ message) @@ -58,8 +57,7 @@ let handle_command cli iobuf cmd = | (None, "JOIN", ["0"], None) -> () | (None, "JOIN", [channels], None) -> - let nuhost = (!(cli.nick), cli.username, (Iobuf.addr cli.iobuf)) in - Channel.handle_command cli.iobuf nuhost cmd + Channel.handle_command cli.iobuf (nuhost cli) cmd | (None, "JOIN", [channels; keys], None) -> () | (None, "PART", [channels], message) -> @@ -79,12 +77,14 @@ let handle_command cli iobuf cmd = | (None, ("PRIVMSG" as command), [target], Some text) | (None, ("NOTICE" as command), [target], Some text) -> if Channel.is_channel_name target then - reply cli "403" ~args:[target] "No such channel" + Channel.handle_command cli.iobuf (nuhost cli) cmd else begin try let peer = lookup target in - write peer (Some (uhost cli)) command [target] (Some text) + write peer + (Some (Irc.string_of_nuhost (nuhost cli))) + command [target] (Some text) with Not_found -> reply cli "401" ~args:[target] "No such nick/channel" end diff --git a/tests.ml b/tests.ml index d51c8e5..8fe89ff 100644 --- a/tests.ml +++ b/tests.ml @@ -418,6 +418,8 @@ let regression_tests = Recv ":alice!alice@UDS JOIN #foo\r\n"; Send "PRIVMSG bob :Hi Bob!\r\n"; Recv ":bob!bob@UDS JOIN #foo\r\n"; + Send "PRIVMSG #foo :hello bob\r\n"; + Recv ":bob!bob@UDS NOTICE #foo :hello alice\r\n"; Send "QUIT :foo\r\n"; Recv ":testserver.test ERROR :So long\r\n"; ] @@ -425,11 +427,17 @@ let regression_tests = let script2 = (do_login "bob") @ [ - Send "ISON alice\r\n"; + Send "ISON alice charlie\r\n"; Recv ":testserver.test 303 bob :alice\r\n"; Recv ":alice!alice@UDS PRIVMSG bob :Hi Bob!\r\n"; + Send "PRIVMSG #foo :snot\r\n"; + Recv ":testserver.test 404 bob #foo :Cannot send to channel\r\n"; + Send "NOTICE #foo :snot\r\n"; + Recv ":testserver.test 404 bob #foo :Cannot send to channel\r\n"; Send "JOIN #foo\r\n"; Recv ":bob!bob@UDS JOIN #foo\r\n"; + Recv ":alice!alice@UDS PRIVMSG #foo :hello bob\r\n"; + Send "NOTICE #foo :hello alice\r\n"; Send "QUIT :foo\r\n"; Recv ":testserver.test ERROR :So long\r\n"; ]