1 |
|
%% @doc Functions to work with iq record. |
2 |
|
%% |
3 |
|
%%============================================================================== |
4 |
|
%% Copyright 2015 Erlang Solutions Ltd. |
5 |
|
%% |
6 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
7 |
|
%% you may not use this file except in compliance with the License. |
8 |
|
%% You may obtain a copy of the License at |
9 |
|
%% |
10 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
11 |
|
%% |
12 |
|
%% Unless required by applicable law or agreed to in writing, software |
13 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
14 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 |
|
%% See the License for the specific language governing permissions and |
16 |
|
%% limitations under the License. |
17 |
|
%%============================================================================== |
18 |
|
%% |
19 |
|
-module(mongoose_iq). |
20 |
|
-export([iq_to_sub_el/1, |
21 |
|
empty_result_iq/1]). |
22 |
|
-export([update_acc_info/1]). |
23 |
|
-export([info/1, xmlns/1, command/1]). |
24 |
|
|
25 |
|
-ignore_xref([command/1, empty_result_iq/1, iq_to_sub_el/1, update_acc_info/1]). |
26 |
|
-ignore_xref([xmlns/1]). |
27 |
|
|
28 |
|
-include("jlib.hrl"). |
29 |
|
|
30 |
|
%% --------------------------------------------------------- |
31 |
|
%% API |
32 |
|
%% --------------------------------------------------------- |
33 |
|
|
34 |
|
iq_to_sub_el(#iq{sub_el = SubEl}) -> |
35 |
2 |
SubEl. |
36 |
|
|
37 |
|
empty_result_iq(IQ) -> |
38 |
2 |
IQ#iq{type = result, sub_el = []}. |
39 |
|
|
40 |
|
%% Fills mongoose_acc with useful IQ information |
41 |
|
%% If the info cache is already present, then it is updated only if |
42 |
|
%% IQ data ref() doesn't match stanza ref() inside acc. |
43 |
|
-spec update_acc_info(mongoose_acc:t()) -> mongoose_acc:t(). |
44 |
|
update_acc_info(Acc0) -> |
45 |
33130 |
IQRef = mongoose_acc:get(iq, ref, not_exists, Acc0), |
46 |
33130 |
case mongoose_acc:stanza_ref(Acc0) of |
47 |
|
IQRef -> |
48 |
|
% Up to date |
49 |
2139 |
Acc0; |
50 |
|
CurrentRef -> |
51 |
30991 |
El = mongoose_acc:element(Acc0), |
52 |
30991 |
IQ = jlib:iq_query_or_response_info(El), |
53 |
30991 |
{XMLNS, Command} = case IQ of |
54 |
|
#iq{ xmlns = XMLNS0, sub_el = SubEl } -> |
55 |
30979 |
{XMLNS0, sub_el_to_command(SubEl)}; |
56 |
|
_ -> |
57 |
12 |
{undefined, undefined} |
58 |
|
end, |
59 |
30991 |
IqData = [{record, IQ}, {xmlns, XMLNS}, {command, Command}, {ref, CurrentRef}], |
60 |
30991 |
mongoose_acc:set(iq, IqData, Acc0) |
61 |
|
end. |
62 |
|
|
63 |
|
%% update_and_get updates only when it is actually necessary |
64 |
|
%% and adds IQ data if it's missing |
65 |
|
|
66 |
|
-spec info(mongoose_acc:t()) -> {invalid | not_iq | jlib:iq(), UpdatedAcc :: mongoose_acc:t()}. |
67 |
7075 |
info(Acc) -> update_and_get(record, Acc). |
68 |
|
|
69 |
|
-spec xmlns(mongoose_acc:t()) -> {binary() | undefined, UpdatedAcc :: mongoose_acc:t()}. |
70 |
:-( |
xmlns(Acc) -> update_and_get(xmlns, Acc). |
71 |
|
|
72 |
|
-spec command(mongoose_acc:t()) -> {binary() | undefined, UpdatedAcc :: mongoose_acc:t()}. |
73 |
:-( |
command(Acc) -> update_and_get(command, Acc). |
74 |
|
|
75 |
|
%% --------------------------------------------------------- |
76 |
|
%% Internal functions |
77 |
|
%% --------------------------------------------------------- |
78 |
|
|
79 |
|
-spec sub_el_to_command([exml:element()] | exml:element()) -> binary() | undefined. |
80 |
1578 |
sub_el_to_command([]) -> undefined; |
81 |
11602 |
sub_el_to_command([SubEl | _]) -> SubEl#xmlel.name; |
82 |
17799 |
sub_el_to_command(#xmlel{ name = Name }) -> Name. |
83 |
|
|
84 |
|
-type iq_acc_field() :: record | xmlns | command. |
85 |
|
|
86 |
|
-spec update_and_get(iq_acc_field(), mongoose_acc:t()) -> |
87 |
|
{FieldValue :: any(), mongoose_acc:t()}. |
88 |
|
update_and_get(Field, Acc0) -> |
89 |
7075 |
Acc1 = update_acc_info(Acc0), |
90 |
7075 |
{mongoose_acc:get(iq, Field, Acc1), Acc1}. |