forked from processone/xmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub_get_pending.erl
248 lines (203 loc) · 7.04 KB
/
pubsub_get_pending.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
%% Created automatically by xdata generator (xdata_codec.erl)
%% Source: pubsub_get_pending.xdata
%% Form type: http://jabber.org/protocol/pubsub#subscribe_authorization
%% Document: XEP-0060
-module(pubsub_get_pending).
-compile({nowarn_unused_function,
[{dec_int, 3}, {dec_int, 1}, {dec_enum, 2},
{dec_enum_int, 2}, {dec_enum_int, 4}, {enc_int, 1},
{enc_enum, 1}, {enc_enum_int, 1}, {not_empty, 1},
{dec_bool, 1}, {enc_bool, 1}, {dec_ip, 1},
{enc_ip, 1}]}).
-compile(nowarn_unused_vars).
-dialyzer({nowarn_function, {dec_int, 3}}).
-export([encode/1, encode/2, encode/3]).
-export([decode/1, decode/2, decode/3, format_error/1,
io_format_error/1]).
-include("xmpp_codec.hrl").
-include("pubsub_get_pending.hrl").
-export_type([property/0, result/0, form/0,
error_reason/0]).
-define(T(S), <<S>>).
-spec format_error(error_reason()) -> binary().
-spec io_format_error(error_reason()) -> {binary(),
[binary()]}.
-spec decode([xdata_field()]) -> result().
-spec decode([xdata_field()],
[binary(), ...]) -> result().
-spec decode([xdata_field()], [binary(), ...],
[binary()]) -> result().
-spec decode([xdata_field()], [binary(), ...],
[binary()], result()) -> result().
-spec do_decode([xdata_field()], binary(), [binary()],
result()) -> result().
-spec encode(form()) -> [xdata_field()].
-spec encode(form(), binary()) -> [xdata_field()].
-spec encode(form(), binary(),
[node]) -> [xdata_field()].
dec_int(Val) -> dec_int(Val, infinity, infinity).
dec_int(Val, Min, Max) ->
case erlang:binary_to_integer(Val) of
Int when Int =< Max, Min == infinity -> Int;
Int when Int =< Max, Int >= Min -> Int
end.
enc_int(Int) -> integer_to_binary(Int).
dec_enum(Val, Enums) ->
AtomVal = erlang:binary_to_existing_atom(Val, utf8),
case lists:member(AtomVal, Enums) of
true -> AtomVal
end.
enc_enum(Atom) -> erlang:atom_to_binary(Atom, utf8).
dec_enum_int(Val, Enums) ->
try dec_int(Val) catch _:_ -> dec_enum(Val, Enums) end.
dec_enum_int(Val, Enums, Min, Max) ->
try dec_int(Val, Min, Max) catch
_:_ -> dec_enum(Val, Enums)
end.
enc_enum_int(Int) when is_integer(Int) -> enc_int(Int);
enc_enum_int(Atom) -> enc_enum(Atom).
dec_bool(<<"1">>) -> true;
dec_bool(<<"0">>) -> false;
dec_bool(<<"true">>) -> true;
dec_bool(<<"false">>) -> false.
enc_bool(true) -> <<"1">>;
enc_bool(false) -> <<"0">>.
not_empty(<<_, _/binary>> = Val) -> Val.
dec_ip(Val) ->
{ok, Addr} = inet_parse:address(binary_to_list(Val)),
Addr.
enc_ip({0, 0, 0, 0, 0, 65535, A, B}) ->
enc_ip({(A bsr 8) band 255, A band 255,
(B bsr 8) band 255, B band 255});
enc_ip(Addr) -> list_to_binary(inet_parse:ntoa(Addr)).
format_error({form_type_mismatch, Type}) ->
<<"FORM_TYPE doesn't match '", Type/binary, "'">>;
format_error({bad_var_value, Var, Type}) ->
<<"Bad value of field '", Var/binary, "' of type '",
Type/binary, "'">>;
format_error({missing_value, Var, Type}) ->
<<"Missing value of field '", Var/binary, "' of type '",
Type/binary, "'">>;
format_error({too_many_values, Var, Type}) ->
<<"Too many values for field '", Var/binary,
"' of type '", Type/binary, "'">>;
format_error({unknown_var, Var, Type}) ->
<<"Unknown field '", Var/binary, "' of type '",
Type/binary, "'">>;
format_error({missing_required_var, Var, Type}) ->
<<"Missing required field '", Var/binary, "' of type '",
Type/binary, "'">>.
io_format_error({form_type_mismatch, Type}) ->
{<<"FORM_TYPE doesn't match '~s'">>, [Type]};
io_format_error({bad_var_value, Var, Type}) ->
{<<"Bad value of field '~s' of type '~s'">>,
[Var, Type]};
io_format_error({missing_value, Var, Type}) ->
{<<"Missing value of field '~s' of type "
"'~s'">>,
[Var, Type]};
io_format_error({too_many_values, Var, Type}) ->
{<<"Too many values for field '~s' of type "
"'~s'">>,
[Var, Type]};
io_format_error({unknown_var, Var, Type}) ->
{<<"Unknown field '~s' of type '~s'">>, [Var, Type]};
io_format_error({missing_required_var, Var, Type}) ->
{<<"Missing required field '~s' of type "
"'~s'">>,
[Var, Type]}.
decode(Fs) ->
decode(Fs,
[<<"http://jabber.org/protocol/pubsub#subscribe_a"
"uthorization">>],
[<<"pubsub#node">>], []).
decode(Fs, XMLNSList) ->
decode(Fs, XMLNSList, [<<"pubsub#node">>], []).
decode(Fs, XMLNSList, Required) ->
decode(Fs, XMLNSList, Required, []).
decode(Fs, [_ | _] = XMLNSList, Required, Acc) ->
case lists:keyfind(<<"FORM_TYPE">>, #xdata_field.var,
Fs)
of
false -> do_decode(Fs, hd(XMLNSList), Required, Acc);
#xdata_field{values = [XMLNS]} ->
case lists:member(XMLNS, XMLNSList) of
true -> do_decode(Fs, XMLNS, Required, Acc);
false ->
erlang:error({?MODULE, {form_type_mismatch, XMLNS}})
end
end.
encode(Cfg) -> encode(Cfg, <<"en">>, [node]).
encode(Cfg, Lang) -> encode(Cfg, Lang, [node]).
encode(List, Lang, Required) ->
Fs = [case Opt of
{node, Val} ->
[encode_node(Val, default, Lang,
lists:member(node, Required))];
{node, Val, Opts} ->
[encode_node(Val, Opts, Lang,
lists:member(node, Required))];
#xdata_field{} -> [Opt]
end
|| Opt <- List],
FormType = #xdata_field{var = <<"FORM_TYPE">>,
type = hidden,
values =
[<<"http://jabber.org/protocol/pubsub#subscribe_a"
"uthorization">>]},
[FormType | lists:flatten(Fs)].
do_decode([#xdata_field{var = <<"pubsub#node">>,
values = [Value]}
| Fs],
XMLNS, Required, Acc) ->
try Value of
Result ->
do_decode(Fs, XMLNS,
lists:delete(<<"pubsub#node">>, Required),
[{node, Result} | Acc])
catch
_:_ ->
erlang:error({?MODULE,
{bad_var_value, <<"pubsub#node">>, XMLNS}})
end;
do_decode([#xdata_field{var = <<"pubsub#node">>,
values = []} =
F
| Fs],
XMLNS, Required, Acc) ->
do_decode([F#xdata_field{var = <<"pubsub#node">>,
values = [<<>>]}
| Fs],
XMLNS, Required, Acc);
do_decode([#xdata_field{var = <<"pubsub#node">>} | _],
XMLNS, _, _) ->
erlang:error({?MODULE,
{too_many_values, <<"pubsub#node">>, XMLNS}});
do_decode([#xdata_field{var = Var} | Fs], XMLNS,
Required, Acc) ->
if Var /= <<"FORM_TYPE">> ->
erlang:error({?MODULE, {unknown_var, Var, XMLNS}});
true -> do_decode(Fs, XMLNS, Required, Acc)
end;
do_decode([], XMLNS, [Var | _], _) ->
erlang:error({?MODULE,
{missing_required_var, Var, XMLNS}});
do_decode([], _, [], Acc) -> Acc.
-spec encode_node(binary(), default | options(binary()),
binary(), boolean()) -> xdata_field().
encode_node(Value, Options, Lang, IsRequired) ->
Values = case Value of
<<>> -> [];
Value -> [Value]
end,
Opts = if Options == default -> [];
true ->
[#xdata_option{label = xmpp_tr:tr(Lang, L), value = V}
|| {L, V} <- Options]
end,
#xdata_field{var = <<"pubsub#node">>, values = Values,
required = IsRequired, type = 'list-single',
options = Opts, desc = <<>>,
label =
xmpp_tr:tr(Lang,
?T("The NodeID of the relevant node"))}.