mirror of https://github.com/nealey/irc-bot
Channel PRIVMSG and NOTICE
This commit is contained in:
parent
aa2311ead2
commit
2ff9e84199
12
channel.ml
12
channel.ml
|
@ -47,12 +47,22 @@ let broadcast ?(metoo=false) chan sender command args text =
|
|||
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"
|
||||
|
|
12
client.ml
12
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
|
||||
|
|
10
tests.ml
10
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";
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue