Skip to content

Commit

Permalink
feat: make handle_command/2 optional
Browse files Browse the repository at this point in the history
Default to respond with "500 Unknown command".
  • Loading branch information
sntran committed Mar 11, 2021
1 parent f0e2d4a commit c184f73
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/gen_nntp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ defmodule GenNNTP do
handle_ARTICLE: 2,
handle_HEAD: 2,
handle_BODY: 2,
handle_STAT: 2
handle_STAT: 2,
handle_command: 2
]
end
26 changes: 16 additions & 10 deletions src/gen_nntp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@
handle_ARTICLE/2,
handle_HEAD/2,
handle_BODY/2,
handle_STAT/2
handle_STAT/2,
handle_command/2
]).

%% ==================================================================
Expand Down Expand Up @@ -615,15 +616,20 @@ handle_command(<<"QUIT">>, Client) ->
handle_command(Command, Client) ->
#client{module = Module, state = State} = Client,

case Module:handle_command(Command, State) of
{reply, Reply, State1} ->
{reply, Reply, Client#client{state = State1}};
{noreply, State1} ->
{noreply, Client#client{state = State1}};
{stop, Reason, State1} ->
{stop, Reason, Client#client{state = State1}};
{stop, Reason, Reply, State1} ->
{stop, Reason, Reply, Client#client{state = State1}}
case erlang:function_exported(Module, handle_command, 2) of
false ->
{reply, <<"500 Unknown command">>, State};
true ->
case Module:handle_command(Command, State) of
{reply, Reply, State1} ->
{reply, Reply, Client#client{state = State1}};
{noreply, State1} ->
{noreply, Client#client{state = State1}};
{stop, Reason, State1} ->
{stop, Reason, Client#client{state = State1}};
{stop, Reason, Reply, State1} ->
{stop, Reason, Reply, Client#client{state = State1}}
end
end.

% Trim the leading whitespace if any.
Expand Down
5 changes: 0 additions & 5 deletions test/support/test_nntp_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ defmodule TestNNTPServer do
end
end

@impl GenNNTP
def handle_command(_command, state) do
{:noreply, state}
end

defp maybe_apply(server, fun, args, default_reply) do
case Access.get(server, fun) do
nil ->
Expand Down

0 comments on commit c184f73

Please sign in to comment.