aboutsummaryrefslogtreecommitdiff
path: root/src/adhoc.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/adhoc.erl')
-rw-r--r--src/adhoc.erl177
1 files changed, 104 insertions, 73 deletions
diff --git a/src/adhoc.erl b/src/adhoc.erl
index 94353aead..6af65e5d1 100644
--- a/src/adhoc.erl
+++ b/src/adhoc.erl
@@ -25,11 +25,14 @@
%%%----------------------------------------------------------------------
-module(adhoc).
+
-author('henoch@dtek.chalmers.se').
--export([parse_request/1,
- produce_response/2,
- produce_response/1]).
+-export([
+ parse_request/1,
+ produce_response/2,
+ produce_response/1
+]).
-include("ejabberd.hrl").
-include("jlib.hrl").
@@ -37,93 +40,121 @@
%% Parse an ad-hoc request. Return either an adhoc_request record or
%% an {error, ErrorType} tuple.
+%%
+-spec(parse_request/1 ::
+(
+ IQ :: iq_request())
+ -> adhoc_response()
+ %%
+ | {error, _}
+).
+
parse_request(#iq{type = set, lang = Lang, sub_el = SubEl, xmlns = ?NS_COMMANDS}) ->
?DEBUG("entering parse_request...", []),
- Node = xml:get_tag_attr_s("node", SubEl),
- SessionID = xml:get_tag_attr_s("sessionid", SubEl),
- Action = xml:get_tag_attr_s("action", SubEl),
+ Node = xml:get_tag_attr_s(<<"node">>, SubEl),
+ SessionID = xml:get_tag_attr_s(<<"sessionid">>, SubEl),
+ Action = xml:get_tag_attr_s(<<"action">>, SubEl),
XData = find_xdata_el(SubEl),
- {xmlelement, _, _, AllEls} = SubEl,
+ #xmlel{children = AllEls} = SubEl,
Others = case XData of
- false ->
- AllEls;
- _ ->
- lists:delete(XData, AllEls)
+ false -> AllEls;
+ _ -> lists:delete(XData, AllEls)
end,
-
- #adhoc_request{lang = Lang,
- node = Node,
- sessionid = SessionID,
- action = Action,
- xdata = XData,
- others = Others};
-parse_request(_) ->
- {error, ?ERR_BAD_REQUEST}.
+ #adhoc_request{
+ lang = Lang,
+ node = Node,
+ sessionid = SessionID,
+ action = Action,
+ xdata = XData,
+ others = Others
+ };
+parse_request(_) -> {error, ?ERR_BAD_REQUEST}.
%% Borrowed from mod_vcard.erl
-find_xdata_el({xmlelement, _Name, _Attrs, SubEls}) ->
+find_xdata_el(#xmlel{children = SubEls}) ->
find_xdata_el1(SubEls).
-find_xdata_el1([]) ->
- false;
-find_xdata_el1([{xmlelement, Name, Attrs, SubEls} | Els]) ->
- case xml:get_attr_s("xmlns", Attrs) of
- ?NS_XDATA ->
- {xmlelement, Name, Attrs, SubEls};
- _ ->
- find_xdata_el1(Els)
+find_xdata_el1([]) -> false;
+find_xdata_el1([El | Els]) when is_record(El, xmlel) ->
+ case xml:get_tag_attr_s(<<"xmlns">>, El) of
+ ?NS_XDATA -> El;
+ _ -> find_xdata_el1(Els)
end;
-find_xdata_el1([_ | Els]) ->
- find_xdata_el1(Els).
+find_xdata_el1([_ | Els]) -> find_xdata_el1(Els).
%% Produce a <command/> node to use as response from an adhoc_response
%% record, filling in values for language, node and session id from
%% the request.
-produce_response(#adhoc_request{lang = Lang,
- node = Node,
- sessionid = SessionID},
- Response) ->
- produce_response(Response#adhoc_response{lang = Lang,
- node = Node,
- sessionid = SessionID}).
+%%
+-spec(produce_response/2 ::
+(
+ Adhoc_Request :: adhoc_request(),
+ Adhoc_Response :: adhoc_response())
+ -> Xmlel::xmlel()
+).
%% Produce a <command/> node to use as response from an adhoc_response
%% record.
-produce_response(#adhoc_response{lang = _Lang,
- node = Node,
- sessionid = ProvidedSessionID,
- status = Status,
- defaultaction = DefaultAction,
- actions = Actions,
- notes = Notes,
- elements = Elements}) ->
- SessionID = if is_list(ProvidedSessionID), ProvidedSessionID /= "" ->
- ProvidedSessionID;
- true ->
- jlib:now_to_utc_string(now())
- end,
+produce_response(#adhoc_request{lang = Lang, node = Node, sessionid = SessionID},
+ Adhoc_Response) ->
+ produce_response(Adhoc_Response#adhoc_response{
+ lang = Lang, node = Node, sessionid = SessionID
+ }).
+
+%%
+-spec(produce_response/1 ::
+(
+ Adhoc_Response::adhoc_response())
+ -> Xmlel::xmlel()
+).
+
+produce_response(
+ #adhoc_response{
+ %lang = _Lang,
+ node = Node,
+ sessionid = ProvidedSessionID,
+ status = Status,
+ defaultaction = DefaultAction,
+ actions = Actions,
+ notes = Notes,
+ elements = Elements
+ }) ->
+ SessionID = if is_binary(ProvidedSessionID),
+ ProvidedSessionID /= <<"">> -> ProvidedSessionID;
+ true -> jlib:now_to_utc_string(now())
+ end,
case Actions of
- [] ->
- ActionsEls = [];
- _ ->
- case DefaultAction of
- "" ->
- ActionsElAttrs = [];
- _ ->
- ActionsElAttrs = [{"execute", DefaultAction}]
- end,
- ActionsEls = [{xmlelement, "actions",
- ActionsElAttrs,
- [{xmlelement, Action, [], []} || Action <- Actions]}]
+ [] ->
+ ActionsEls = [];
+ _ ->
+ case DefaultAction of
+ <<"">> -> ActionsElAttrs = [];
+ _ -> ActionsElAttrs = [{<<"execute">>, DefaultAction}]
+ end,
+ ActionsEls = [
+ #xmlel{
+ name = <<"actions">>,
+ attrs = ActionsElAttrs,
+ children = [
+ #xmlel{name = Action, attrs = [], children = []}
+ || Action <- Actions]
+ }
+ ]
end,
NotesEls = lists:map(fun({Type, Text}) ->
- {xmlelement, "note",
- [{"type", Type}],
- [{xmlcdata, Text}]}
- end, Notes),
- {xmlelement, "command",
- [{"xmlns", ?NS_COMMANDS},
- {"sessionid", SessionID},
- {"node", Node},
- {"status", atom_to_list(Status)}],
- ActionsEls ++ NotesEls ++ Elements}.
+ #xmlel{
+ name = <<"note">>,
+ attrs = [{<<"type">>, Type}],
+ children = [{xmlcdata, Text}]
+ }
+ end, Notes),
+ #xmlel{
+ name = <<"command">>,
+ attrs = [
+ {<<"xmlns">>, ?NS_COMMANDS},
+ {<<"sessionid">>, SessionID},
+ {<<"node">>, Node},
+ {<<"status">>, iolist_to_binary(atom_to_list(Status))}
+ ],
+ children = ActionsEls ++ NotesEls ++ Elements
+ }.