forked from processone/xmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xep0203.erl
119 lines (98 loc) · 4.1 KB
/
xep0203.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
%% Created automatically by XML generator (fxml_gen.erl)
%% Source: xmpp_codec.spec
-module(xep0203).
-compile(export_all).
do_decode(<<"delay">>, <<"urn:xmpp:delay">>, El,
Opts) ->
decode_delay(<<"urn:xmpp:delay">>, 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() -> [{<<"delay">>, <<"urn:xmpp:delay">>}].
do_encode({delay, _, _, _} = Delay, TopXMLNS) ->
encode_delay(Delay, TopXMLNS).
do_get_name({delay, _, _, _}) -> <<"delay">>.
do_get_ns({delay, _, _, _}) -> <<"urn:xmpp:delay">>.
pp(delay, 3) -> [stamp, from, desc];
pp(_, _) -> no.
records() -> [{delay, 3}].
dec_utc(Val) -> xmpp_util:decode_timestamp(Val).
enc_utc(Val) -> xmpp_util:encode_timestamp(Val).
decode_delay(__TopXMLNS, __Opts,
{xmlel, <<"delay">>, _attrs, _els}) ->
Desc = decode_delay_els(__TopXMLNS, __Opts, _els, <<>>),
{Stamp, From} = decode_delay_attrs(__TopXMLNS,
_attrs,
undefined,
undefined),
{delay, Stamp, From, Desc}.
decode_delay_els(__TopXMLNS, __Opts, [], Desc) ->
decode_delay_cdata(__TopXMLNS, Desc);
decode_delay_els(__TopXMLNS, __Opts,
[{xmlcdata, _data} | _els], Desc) ->
decode_delay_els(__TopXMLNS,
__Opts,
_els,
<<Desc/binary, _data/binary>>);
decode_delay_els(__TopXMLNS, __Opts, [_ | _els],
Desc) ->
decode_delay_els(__TopXMLNS, __Opts, _els, Desc).
decode_delay_attrs(__TopXMLNS,
[{<<"stamp">>, _val} | _attrs], _Stamp, From) ->
decode_delay_attrs(__TopXMLNS, _attrs, _val, From);
decode_delay_attrs(__TopXMLNS,
[{<<"from">>, _val} | _attrs], Stamp, _From) ->
decode_delay_attrs(__TopXMLNS, _attrs, Stamp, _val);
decode_delay_attrs(__TopXMLNS, [_ | _attrs], Stamp,
From) ->
decode_delay_attrs(__TopXMLNS, _attrs, Stamp, From);
decode_delay_attrs(__TopXMLNS, [], Stamp, From) ->
{decode_delay_attr_stamp(__TopXMLNS, Stamp),
decode_delay_attr_from(__TopXMLNS, From)}.
encode_delay({delay, Stamp, From, Desc}, __TopXMLNS) ->
__NewTopXMLNS =
xmpp_codec:choose_top_xmlns(<<"urn:xmpp:delay">>,
[],
__TopXMLNS),
_els = encode_delay_cdata(Desc, []),
_attrs = encode_delay_attr_from(From,
encode_delay_attr_stamp(Stamp,
xmpp_codec:enc_xmlns_attrs(__NewTopXMLNS,
__TopXMLNS))),
{xmlel, <<"delay">>, _attrs, _els}.
decode_delay_attr_stamp(__TopXMLNS, undefined) ->
erlang:error({xmpp_codec,
{missing_attr, <<"stamp">>, <<"delay">>, __TopXMLNS}});
decode_delay_attr_stamp(__TopXMLNS, _val) ->
case catch dec_utc(_val) of
{'EXIT', _} ->
erlang:error({xmpp_codec,
{bad_attr_value,
<<"stamp">>,
<<"delay">>,
__TopXMLNS}});
_res -> _res
end.
encode_delay_attr_stamp(_val, _acc) ->
[{<<"stamp">>, enc_utc(_val)} | _acc].
decode_delay_attr_from(__TopXMLNS, undefined) ->
undefined;
decode_delay_attr_from(__TopXMLNS, _val) ->
case catch jid:decode(_val) of
{'EXIT', _} ->
erlang:error({xmpp_codec,
{bad_attr_value,
<<"from">>,
<<"delay">>,
__TopXMLNS}});
_res -> _res
end.
encode_delay_attr_from(undefined, _acc) -> _acc;
encode_delay_attr_from(_val, _acc) ->
[{<<"from">>, jid:encode(_val)} | _acc].
decode_delay_cdata(__TopXMLNS, <<>>) -> <<>>;
decode_delay_cdata(__TopXMLNS, _val) -> _val.
encode_delay_cdata(<<>>, _acc) -> _acc;
encode_delay_cdata(_val, _acc) ->
[{xmlcdata, _val} | _acc].