Skip to content

Commit a4a8673

Browse files
committed
Provide stub MODE replies
Clients often expect MODE to be implemented
1 parent 4f36b08 commit a4a8673

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

lib/irc/handler.ex

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,43 @@ defmodule M51.IrcConn.Handler do
11281128
close_connection(sup_pid)
11291129
end
11301130

1131+
{"MODE", [target]} ->
1132+
# TODO: check channel exists, and return the appropriate error if it does not
1133+
case target do
1134+
<< ?#, _::binary >> ->
1135+
# RPL_CHANNELMODEIS
1136+
send_numeric.("324", [target, "+nt"])
1137+
<< ?!, _::binary >> ->
1138+
# RPL_CHANNELMODEIS
1139+
send_numeric.("324", [target, "+nt"])
1140+
^nick ->
1141+
# RPL_UMODEIS
1142+
send_numeric.("221", ["+i"])
1143+
_ ->
1144+
# ERR_USERSDONTMATCH
1145+
send_numeric.("502", ["Can't view mode of other users"])
1146+
end
1147+
1148+
{"MODE", [target, _modestring | _]} ->
1149+
case target do
1150+
<< ?#, _::binary >> ->
1151+
# ERR_CHANOPRIVSNEEDED
1152+
send_numeric.("482", [target, "You're not a channel operator"])
1153+
<< ?!, _ ::binary>> ->
1154+
# ERR_CHANOPRIVSNEEDED
1155+
send_numeric.("482", [target, "You're not a channel operator"])
1156+
^nick ->
1157+
# ERR_UMODEUNKNOWNFLAG (kind of abusing the meaning, but it's the best I
1158+
# could find)
1159+
send_numeric.("501", ["Setting user modes are not supported"])
1160+
_ ->
1161+
# ERR_USERSDONTMATCH
1162+
send_numeric.("502", ["Can't set mode of other users"])
1163+
end
1164+
1165+
{"MODE", []} ->
1166+
send_needmoreparams.()
1167+
11311168
{"BATCH", _} ->
11321169
send_needmoreparams.()
11331170

test/irc/handler_test.exs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,62 @@ defmodule M51.IrcConn.HandlerTest do
826826
assert_line("BATCH :-#{batch_id}\r\n")
827827
end
828828

829+
test "MODE on user", %{handler: handler} do
830+
do_connection_registration(handler)
831+
832+
send(handler, cmd("@label=l1 MODE unknown_user:example.com"))
833+
834+
assert_line(
835+
"@label=l1 :server. 502 foo:example.org :Can't view mode of other users\r\n"
836+
)
837+
838+
send(handler, cmd("@label=l2 MODE foo:example.org"))
839+
840+
assert_line(
841+
"@label=l2 :server. 221 foo:example.org :+i\r\n"
842+
)
843+
844+
send(handler, cmd("@label=l3 MODE unknown_user:example.com +i"))
845+
846+
assert_line(
847+
"@label=l3 :server. 502 foo:example.org :Can't set mode of other users\r\n"
848+
)
849+
850+
send(handler, cmd("@label=l4 MODE foo:example.org +i"))
851+
852+
assert_line(
853+
"@label=l4 :server. 501 foo:example.org :Setting user modes are not supported\r\n"
854+
)
855+
end
856+
857+
test "MODE on channel", %{handler: handler} do
858+
do_connection_registration(handler)
859+
860+
send(handler, cmd("@label=l1 MODE #unknown_channel:example.com"))
861+
862+
assert_line(
863+
"@label=l1 :server. 324 foo:example.org #unknown_channel:example.com :+nt\r\n"
864+
)
865+
866+
send(handler, cmd("@label=l2 MODE !unknown_channel:example.com"))
867+
868+
assert_line(
869+
"@label=l2 :server. 324 foo:example.org !unknown_channel:example.com :+nt\r\n"
870+
)
871+
872+
send(handler, cmd("@label=l3 MODE #unknown_channel:example.com +i"))
873+
874+
assert_line(
875+
"@label=l3 :server. 482 foo:example.org #unknown_channel:example.com :You're not a channel operator\r\n"
876+
)
877+
878+
send(handler, cmd("@label=l4 MODE !unknown_channel:example.com +i"))
879+
880+
assert_line(
881+
"@label=l4 :server. 482 foo:example.org !unknown_channel:example.com :You're not a channel operator\r\n"
882+
)
883+
end
884+
829885
test "CHATHISTORY AROUND", %{handler: handler} do
830886
do_connection_registration(handler, ["message-tags"])
831887

0 commit comments

Comments
 (0)