forked from processone/xmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xep0115.erl
147 lines (126 loc) · 4.93 KB
/
xep0115.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
%% Created automatically by XML generator (fxml_gen.erl)
%% Source: xmpp_codec.spec
-module(xep0115).
-compile(export_all).
do_decode(<<"c">>,
<<"http://jabber.org/protocol/caps">>, El, Opts) ->
decode_caps(<<"http://jabber.org/protocol/caps">>,
Opts,
El);
do_decode(Name, <<>>, _, _) ->
erlang:error({xmpp_codec, {missing_tag_xmlns, Name}});
do_decode(Name, XMLNS, _, _) ->
erlang:error({xmpp_codec, {unknown_tag, Name, XMLNS}}).
tags() ->
[{<<"c">>, <<"http://jabber.org/protocol/caps">>}].
do_encode({caps, _, _, _, _} = C, TopXMLNS) ->
encode_caps(C, TopXMLNS).
do_get_name({caps, _, _, _, _}) -> <<"c">>.
do_get_ns({caps, _, _, _, _}) ->
<<"http://jabber.org/protocol/caps">>.
pp(caps, 4) -> [node, version, hash, exts];
pp(_, _) -> no.
records() -> [{caps, 4}].
join([], _Sep) -> <<>>;
join([H | T], Sep) ->
<<H/binary, << <<Sep, X/binary>> || X <- T >>/binary>>.
decode_caps(__TopXMLNS, __Opts,
{xmlel, <<"c">>, _attrs, _els}) ->
{Hash, Node, Exts, Version} =
decode_caps_attrs(__TopXMLNS,
_attrs,
undefined,
undefined,
undefined,
undefined),
{caps, Node, Version, Hash, Exts}.
decode_caps_attrs(__TopXMLNS,
[{<<"hash">>, _val} | _attrs], _Hash, Node, Exts,
Version) ->
decode_caps_attrs(__TopXMLNS,
_attrs,
_val,
Node,
Exts,
Version);
decode_caps_attrs(__TopXMLNS,
[{<<"node">>, _val} | _attrs], Hash, _Node, Exts,
Version) ->
decode_caps_attrs(__TopXMLNS,
_attrs,
Hash,
_val,
Exts,
Version);
decode_caps_attrs(__TopXMLNS,
[{<<"ext">>, _val} | _attrs], Hash, Node, _Exts,
Version) ->
decode_caps_attrs(__TopXMLNS,
_attrs,
Hash,
Node,
_val,
Version);
decode_caps_attrs(__TopXMLNS,
[{<<"ver">>, _val} | _attrs], Hash, Node, Exts,
_Version) ->
decode_caps_attrs(__TopXMLNS,
_attrs,
Hash,
Node,
Exts,
_val);
decode_caps_attrs(__TopXMLNS, [_ | _attrs], Hash, Node,
Exts, Version) ->
decode_caps_attrs(__TopXMLNS,
_attrs,
Hash,
Node,
Exts,
Version);
decode_caps_attrs(__TopXMLNS, [], Hash, Node, Exts,
Version) ->
{decode_caps_attr_hash(__TopXMLNS, Hash),
decode_caps_attr_node(__TopXMLNS, Node),
decode_caps_attr_ext(__TopXMLNS, Exts),
decode_caps_attr_ver(__TopXMLNS, Version)}.
encode_caps({caps, Node, Version, Hash, Exts},
__TopXMLNS) ->
__NewTopXMLNS =
xmpp_codec:choose_top_xmlns(<<"http://jabber.org/protocol/caps">>,
[],
__TopXMLNS),
_els = [],
_attrs = encode_caps_attr_ver(Version,
encode_caps_attr_ext(Exts,
encode_caps_attr_node(Node,
encode_caps_attr_hash(Hash,
xmpp_codec:enc_xmlns_attrs(__NewTopXMLNS,
__TopXMLNS))))),
{xmlel, <<"c">>, _attrs, _els}.
decode_caps_attr_hash(__TopXMLNS, undefined) -> <<>>;
decode_caps_attr_hash(__TopXMLNS, _val) -> _val.
encode_caps_attr_hash(<<>>, _acc) -> _acc;
encode_caps_attr_hash(_val, _acc) ->
[{<<"hash">>, _val} | _acc].
decode_caps_attr_node(__TopXMLNS, undefined) -> <<>>;
decode_caps_attr_node(__TopXMLNS, _val) -> _val.
encode_caps_attr_node(<<>>, _acc) -> _acc;
encode_caps_attr_node(_val, _acc) ->
[{<<"node">>, _val} | _acc].
decode_caps_attr_ext(__TopXMLNS, undefined) -> [];
decode_caps_attr_ext(__TopXMLNS, _val) ->
case catch re:split(_val, "\\h+") of
{'EXIT', _} ->
erlang:error({xmpp_codec,
{bad_attr_value, <<"ext">>, <<"c">>, __TopXMLNS}});
_res -> _res
end.
encode_caps_attr_ext([], _acc) -> _acc;
encode_caps_attr_ext(_val, _acc) ->
[{<<"ext">>, join(_val, 32)} | _acc].
decode_caps_attr_ver(__TopXMLNS, undefined) -> <<>>;
decode_caps_attr_ver(__TopXMLNS, _val) -> _val.
encode_caps_attr_ver(<<>>, _acc) -> _acc;
encode_caps_attr_ver(_val, _acc) ->
[{<<"ver">>, _val} | _acc].