args = [], result = {res, rescode}},
#ejabberd_commands{name = reopen_log, tags = [logs, server],
desc = "Reopen the log files",
+ policy = admin,
module = ?MODULE, function = reopen_log,
args = [], result = {res, rescode}},
#ejabberd_commands{name = rotate_log, tags = [logs, server],
process([Call], #request{method = 'POST', data = Data, ip = {IP, _} = IPPort} = Req) ->
Version = get_api_version(Req),
try
- Args = case jiffy:decode(Data) of
- List when is_list(List) -> List;
- {List} when is_list(List) -> List;
- Other -> [Other]
- end,
+ Args = extract_args(Data),
log(Call, Args, IPPort),
case check_permissions(Req, Call) of
{allowed, Cmd, Auth} ->
ErrorResponse ->
ErrorResponse
end
- catch _:{error,{_,invalid_json}} = _Err ->
- ?DEBUG("Bad Request: ~p", [_Err]),
- badrequest_response(<<"Invalid JSON input">>);
- _:_Error ->
+ catch
+ %% TODO We need to refactor to remove redundant error return formatting
+ throw:{error, unknown_command} ->
+ {404, 40, <<"Command not found.">>};
+ _:{error,{_,invalid_json}} = _Err ->
+ ?DEBUG("Bad Request: ~p", [_Err]),
+ badrequest_response(<<"Invalid JSON input">>);
+ _:_Error ->
?DEBUG("Bad Request: ~p ~p", [_Error, erlang:get_stacktrace()]),
badrequest_response()
end;
ErrorResponse ->
ErrorResponse
end
- catch _:_Error ->
+ catch
+ %% TODO We need to refactor to remove redundant error return formatting
+ throw:{error, unknown_command} ->
+ {404, 40, <<"Command not found.">>};
+ _:_Error ->
+
?DEBUG("Bad Request: ~p ~p", [_Error, erlang:get_stacktrace()]),
badrequest_response()
end;
?DEBUG("Bad Request: no handler ~p", [Request]),
badrequest_response().
+%% Be tolerant to make API more easily usable from command-line pipe.
+extract_args(<<"\n">>) -> [];
+extract_args(Data) ->
+ case jiffy:decode(Data) of
+ List when is_list(List) -> List;
+ {List} when is_list(List) -> List;
+ Other -> [Other]
+ end.
+
% get API version N from last "vN" element in URL path
get_api_version(#request{path = Path}) ->
get_api_version(lists:reverse(Path));