forked from processone/xmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub_get_pending.erl
130 lines (118 loc) · 3.85 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
%% 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).
-export([decode/1, decode/2, encode/1, encode/2,
format_error/1]).
-include("xmpp_codec.hrl").
-include("pubsub_get_pending.hrl").
-export_type([property/0, result/0, form/0]).
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, "'">>.
decode(Fs) -> decode(Fs, []).
decode(Fs, Acc) ->
case lists:keyfind(<<"FORM_TYPE">>, #xdata_field.var,
Fs)
of
false -> decode(Fs, Acc, [<<"pubsub#node">>]);
#xdata_field{values =
[<<"http://jabber.org/protocol/pubsub#subscribe_a"
"uthorization">>]} ->
decode(Fs, Acc, [<<"pubsub#node">>]);
_ ->
erlang:error({?MODULE,
{form_type_mismatch,
<<"http://jabber.org/protocol/pubsub#subscribe_a"
"uthorization">>}})
end.
encode(Cfg) -> encode(Cfg, fun (Text) -> Text end).
encode(List, Translate) when is_list(List) ->
Fs = [case Opt of
{node, Val} -> [encode_node(Val, default, Translate)];
{node, Val, Opts} ->
[encode_node(Val, Opts, Translate)];
#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)].
decode([#xdata_field{var = <<"pubsub#node">>,
values = [Value]}
| Fs],
Acc, Required) ->
try Value of
Result ->
decode(Fs, [{node, Result} | Acc],
lists:delete(<<"pubsub#node">>, Required))
catch
_:_ ->
erlang:error({?MODULE,
{bad_var_value, <<"pubsub#node">>,
<<"http://jabber.org/protocol/pubsub#subscribe_a"
"uthorization">>}})
end;
decode([#xdata_field{var = <<"pubsub#node">>,
values = []} =
F
| Fs],
Acc, Required) ->
decode([F#xdata_field{var = <<"pubsub#node">>,
values = [<<>>]}
| Fs],
Acc, Required);
decode([#xdata_field{var = <<"pubsub#node">>} | _], _,
_) ->
erlang:error({?MODULE,
{too_many_values, <<"pubsub#node">>,
<<"http://jabber.org/protocol/pubsub#subscribe_a"
"uthorization">>}});
decode([#xdata_field{var = Var} | Fs], Acc, Required) ->
if Var /= <<"FORM_TYPE">> ->
erlang:error({?MODULE,
{unknown_var, Var,
<<"http://jabber.org/protocol/pubsub#subscribe_a"
"uthorization">>}});
true -> decode(Fs, Acc, Required)
end;
decode([], _, [Var | _]) ->
erlang:error({?MODULE,
{missing_required_var, Var,
<<"http://jabber.org/protocol/pubsub#subscribe_a"
"uthorization">>}});
decode([], Acc, []) -> Acc.
encode_node(Value, Options, Translate) ->
Values = case Value of
<<>> -> [];
Value -> [Value]
end,
Opts = if Options == default -> [];
true ->
[#xdata_option{label = Translate(L), value = V}
|| {L, V} <- Options]
end,
#xdata_field{var = <<"pubsub#node">>, values = Values,
required = false, type = 'list-single', options = Opts,
desc = <<>>,
label =
Translate(<<"The NodeID of the relevant node">>)}.