2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : jlib.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : General XMPP library.
|
|
|
|
%%% Created : 23 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2002-11-23 21:55:05 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(jlib).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 59, '1.0'}).
|
|
|
|
-protocol({xep, 82, '1.1'}).
|
|
|
|
-protocol({xep, 203, '2.0'}).
|
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-compile({no_auto_import, [atom_to_binary/2,
|
|
|
|
binary_to_integer/1,
|
|
|
|
integer_to_binary/1]}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-export([make_result_iq_reply/1, make_error_reply/3,
|
|
|
|
make_error_reply/2, make_error_element/2,
|
|
|
|
make_correct_from_to_attrs/3, replace_from_to_attrs/3,
|
|
|
|
replace_from_to/3, replace_from_attrs/2, replace_from/2,
|
2015-11-24 16:44:13 +01:00
|
|
|
remove_attr/2, tolower/1,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_iq_namespace/1, iq_query_info/1,
|
|
|
|
iq_query_or_response_info/1, is_iq_request_type/1,
|
2014-12-07 15:55:18 +01:00
|
|
|
iq_to_xml/1, parse_xdata_submit/1,
|
2015-11-25 08:58:22 +01:00
|
|
|
is_standalone_chat_state/1,
|
2014-12-07 15:55:18 +01:00
|
|
|
add_delay_info/3, add_delay_info/4,
|
2016-01-23 09:56:12 +01:00
|
|
|
timestamp_to_legacy/1, timestamp_to_iso_basic/1, timestamp_to_iso/2,
|
2014-12-07 15:55:18 +01:00
|
|
|
now_to_utc_string/1, now_to_local_string/1,
|
|
|
|
datetime_string_to_timestamp/1,
|
2014-03-19 00:51:33 +01:00
|
|
|
term_to_base64/1, base64_to_term/1,
|
2013-03-14 10:33:02 +01:00
|
|
|
decode_base64/1, encode_base64/1, ip_to_list/1,
|
|
|
|
rsm_encode/1, rsm_encode/2, rsm_decode/1,
|
2013-07-04 10:07:53 +02:00
|
|
|
binary_to_integer/1, binary_to_integer/2,
|
|
|
|
integer_to_binary/1, integer_to_binary/2,
|
|
|
|
atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1,
|
2016-05-05 15:42:48 +02:00
|
|
|
l2i/1, i2l/1, i2l/2, queue_drop_while/2,
|
|
|
|
expr_to_term/1, term_to_expr/1]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-11-24 16:44:13 +01:00
|
|
|
%% The following functions are deprecated and will be removed soon
|
|
|
|
%% Use corresponding functions from jid.erl instead
|
|
|
|
-export([make_jid/3, make_jid/1, split_jid/1, string_to_jid/1,
|
|
|
|
jid_to_string/1, is_nodename/1, nodeprep/1,
|
|
|
|
nameprep/1, resourceprep/1, jid_tolower/1,
|
|
|
|
jid_remove_resource/1, jid_replace_resource/2]).
|
2015-11-03 14:40:52 +01:00
|
|
|
|
2015-11-24 18:24:43 +01:00
|
|
|
-deprecated([{make_jid, '_'},
|
|
|
|
{split_jid, 1},
|
|
|
|
{string_to_jid, 1},
|
|
|
|
{jid_to_string, 1},
|
|
|
|
{is_nodename, 1},
|
|
|
|
{nodeprep, 1},
|
|
|
|
{nameprep, 1},
|
|
|
|
{resourceprep, 1},
|
|
|
|
{jid_tolower, 1},
|
|
|
|
{jid_remove_resource, 1},
|
|
|
|
{jid_replace_resource, 2}]).
|
|
|
|
|
2015-02-11 12:08:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2002-11-27 21:46:29 +01:00
|
|
|
%send_iq(From, To, ID, SubTags) ->
|
|
|
|
% ok.
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec make_result_iq_reply(xmlel()) -> xmlel().
|
|
|
|
|
|
|
|
make_result_iq_reply(#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = SubTags}) ->
|
2002-11-30 19:46:16 +01:00
|
|
|
NewAttrs = make_result_iq_reply_attrs(Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = Name, attrs = NewAttrs,
|
|
|
|
children = SubTags}.
|
|
|
|
|
|
|
|
-spec make_result_iq_reply_attrs([attr()]) -> [attr()].
|
2002-11-30 19:46:16 +01:00
|
|
|
|
|
|
|
make_result_iq_reply_attrs(Attrs) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
To = fxml:get_attr(<<"to">>, Attrs),
|
|
|
|
From = fxml:get_attr(<<"from">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs1 = lists:keydelete(<<"to">>, 1, Attrs),
|
|
|
|
Attrs2 = lists:keydelete(<<"from">>, 1, Attrs1),
|
2002-11-30 19:46:16 +01:00
|
|
|
Attrs3 = case To of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, ToVal} -> [{<<"from">>, ToVal} | Attrs2];
|
|
|
|
_ -> Attrs2
|
2002-11-30 19:46:16 +01:00
|
|
|
end,
|
|
|
|
Attrs4 = case From of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, FromVal} -> [{<<"to">>, FromVal} | Attrs3];
|
|
|
|
_ -> Attrs3
|
2002-11-30 19:46:16 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs5 = lists:keydelete(<<"type">>, 1, Attrs4),
|
|
|
|
Attrs6 = [{<<"type">>, <<"result">>} | Attrs5],
|
2002-11-30 19:46:16 +01:00
|
|
|
Attrs6.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec make_error_reply(xmlel(), binary(), binary()) -> xmlel().
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
make_error_reply(#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = SubTags},
|
|
|
|
Code, Desc) ->
|
|
|
|
NewAttrs = make_error_reply_attrs(Attrs),
|
|
|
|
#xmlel{name = Name, attrs = NewAttrs,
|
|
|
|
children =
|
|
|
|
SubTags ++
|
|
|
|
[#xmlel{name = <<"error">>,
|
|
|
|
attrs = [{<<"code">>, Code}],
|
|
|
|
children = [{xmlcdata, Desc}]}]}.
|
|
|
|
|
|
|
|
-spec make_error_reply(xmlel(), xmlel()) -> xmlel().
|
|
|
|
|
|
|
|
make_error_reply(#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = SubTags},
|
|
|
|
Error) ->
|
2003-03-09 21:46:47 +01:00
|
|
|
NewAttrs = make_error_reply_attrs(Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = Name, attrs = NewAttrs,
|
|
|
|
children = SubTags ++ [Error]}.
|
|
|
|
|
|
|
|
-spec make_error_reply_attrs([attr()]) -> [attr()].
|
2003-03-09 21:46:47 +01:00
|
|
|
|
2002-11-29 21:55:12 +01:00
|
|
|
make_error_reply_attrs(Attrs) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
To = fxml:get_attr(<<"to">>, Attrs),
|
|
|
|
From = fxml:get_attr(<<"from">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs1 = lists:keydelete(<<"to">>, 1, Attrs),
|
|
|
|
Attrs2 = lists:keydelete(<<"from">>, 1, Attrs1),
|
2002-11-27 21:46:29 +01:00
|
|
|
Attrs3 = case To of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, ToVal} -> [{<<"from">>, ToVal} | Attrs2];
|
|
|
|
_ -> Attrs2
|
2002-11-27 21:46:29 +01:00
|
|
|
end,
|
|
|
|
Attrs4 = case From of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, FromVal} -> [{<<"to">>, FromVal} | Attrs3];
|
|
|
|
_ -> Attrs3
|
2002-11-27 21:46:29 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs5 = lists:keydelete(<<"type">>, 1, Attrs4),
|
|
|
|
Attrs6 = [{<<"type">>, <<"error">>} | Attrs5],
|
2002-11-27 21:46:29 +01:00
|
|
|
Attrs6.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec make_error_element(binary(), binary()) -> xmlel().
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
make_error_element(Code, Desc) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"error">>, attrs = [{<<"code">>, Code}],
|
|
|
|
children = [{xmlcdata, Desc}]}.
|
|
|
|
|
|
|
|
-spec make_correct_from_to_attrs(binary(), binary(), [attr()]) -> [attr()].
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2002-11-27 21:46:29 +01:00
|
|
|
make_correct_from_to_attrs(From, To, Attrs) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs1 = lists:keydelete(<<"from">>, 1, Attrs),
|
2016-02-03 19:03:17 +01:00
|
|
|
Attrs2 = case fxml:get_attr(<<"to">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, _} -> Attrs1;
|
|
|
|
_ -> [{<<"to">>, To} | Attrs1]
|
2002-11-27 21:46:29 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs3 = [{<<"from">>, From} | Attrs2],
|
2002-11-27 21:46:29 +01:00
|
|
|
Attrs3.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec replace_from_to_attrs(binary(), binary(), [attr()]) -> [attr()].
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
replace_from_to_attrs(From, To, Attrs) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs1 = lists:keydelete(<<"to">>, 1, Attrs),
|
|
|
|
Attrs2 = lists:keydelete(<<"from">>, 1, Attrs1),
|
|
|
|
Attrs3 = [{<<"to">>, To} | Attrs2],
|
|
|
|
Attrs4 = [{<<"from">>, From} | Attrs3],
|
2002-11-27 21:46:29 +01:00
|
|
|
Attrs4.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec replace_from_to(jid(), jid(), xmlel()) -> xmlel().
|
|
|
|
|
|
|
|
replace_from_to(From, To,
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els}) ->
|
|
|
|
NewAttrs =
|
2015-11-24 16:44:13 +01:00
|
|
|
replace_from_to_attrs(jid:to_string(From),
|
|
|
|
jid:to_string(To), Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = Name, attrs = NewAttrs, children = Els}.
|
|
|
|
|
|
|
|
-spec replace_from_attrs(binary(), [attr()]) -> [attr()].
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2008-12-08 10:21:36 +01:00
|
|
|
replace_from_attrs(From, Attrs) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs1 = lists:keydelete(<<"from">>, 1, Attrs),
|
|
|
|
[{<<"from">>, From} | Attrs1].
|
|
|
|
|
|
|
|
-spec replace_from(jid(), xmlel()) -> xmlel().
|
2008-12-08 10:21:36 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
replace_from(From,
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
NewAttrs = replace_from_attrs(jid:to_string(From),
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs),
|
|
|
|
#xmlel{name = Name, attrs = NewAttrs, children = Els}.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec remove_attr(binary(), xmlel()) -> xmlel().
|
|
|
|
|
|
|
|
remove_attr(Attr,
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els}) ->
|
2002-12-08 18:23:21 +01:00
|
|
|
NewAttrs = lists:keydelete(Attr, 1, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = Name, attrs = NewAttrs, children = Els}.
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec make_jid(binary(), binary(), binary()) -> jid() | error.
|
2003-10-07 22:31:44 +02:00
|
|
|
|
|
|
|
make_jid(User, Server, Resource) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(User, Server, Resource).
|
2003-10-07 22:31:44 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec make_jid({binary(), binary(), binary()}) -> jid() | error.
|
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
make_jid({User, Server, Resource}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make({User, Server, Resource}).
|
2003-10-07 22:31:44 +02:00
|
|
|
|
2015-08-04 09:48:59 +02:00
|
|
|
%% This is the reverse of make_jid/1
|
2015-11-03 14:40:52 +01:00
|
|
|
-spec split_jid(jid()) -> {binary(), binary(), binary()} | error.
|
2015-11-24 16:44:13 +01:00
|
|
|
split_jid(J) ->
|
|
|
|
jid:split(J).
|
2015-08-04 09:48:59 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec string_to_jid(binary()) -> jid() | error.
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
string_to_jid(S) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:from_string(S).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec jid_to_string(jid() | ljid()) -> binary().
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2015-11-24 16:44:13 +01:00
|
|
|
jid_to_string(J) ->
|
|
|
|
jid:to_string(J).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec is_nodename(binary()) -> boolean().
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
is_nodename(Node) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:is_nodename(Node).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2003-01-26 21:16:53 +01:00
|
|
|
%tolower_c(C) when C >= $A, C =< $Z ->
|
|
|
|
% C + 32;
|
|
|
|
%tolower_c(C) ->
|
|
|
|
% C.
|
|
|
|
|
|
|
|
-define(LOWER(Char),
|
2013-03-14 10:33:02 +01:00
|
|
|
if Char >= $A, Char =< $Z -> Char + 32;
|
|
|
|
true -> Char
|
|
|
|
end).
|
2003-01-26 21:16:53 +01:00
|
|
|
|
|
|
|
%tolower(S) ->
|
|
|
|
% lists:map(fun tolower_c/1, S).
|
|
|
|
|
|
|
|
%tolower(S) ->
|
|
|
|
% [?LOWER(Char) || Char <- S].
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec tolower(binary()) -> binary().
|
|
|
|
|
|
|
|
tolower(B) ->
|
|
|
|
iolist_to_binary(tolower_s(binary_to_list(B))).
|
|
|
|
|
|
|
|
tolower_s([C | Cs]) ->
|
|
|
|
if C >= $A, C =< $Z -> [C + 32 | tolower_s(Cs)];
|
|
|
|
true -> [C | tolower_s(Cs)]
|
2003-07-20 22:35:35 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
tolower_s([]) -> [].
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2003-07-20 22:35:35 +02:00
|
|
|
%tolower([C | Cs]) when C >= $A, C =< $Z ->
|
|
|
|
% [C + 32 | tolower(Cs)];
|
|
|
|
%tolower([C | Cs]) ->
|
|
|
|
% [C | tolower(Cs)];
|
|
|
|
%tolower([]) ->
|
|
|
|
% [].
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec nodeprep(binary()) -> binary() | error.
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2015-11-24 16:44:13 +01:00
|
|
|
nodeprep(S) -> jid:nodeprep(S).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec nameprep(binary()) -> binary() | error.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2015-11-24 16:44:13 +01:00
|
|
|
nameprep(S) -> jid:nameprep(S).
|
2003-10-07 22:31:44 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec resourceprep(binary()) -> binary() | error.
|
|
|
|
|
2015-11-24 16:44:13 +01:00
|
|
|
resourceprep(S) -> jid:resourceprep(S).
|
2003-10-07 22:31:44 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec jid_tolower(jid() | ljid()) -> error | ljid().
|
2003-10-07 22:31:44 +02:00
|
|
|
|
2015-11-24 16:44:13 +01:00
|
|
|
jid_tolower(J) ->
|
|
|
|
jid:tolower(J).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec jid_remove_resource(jid()) -> jid();
|
|
|
|
(ljid()) -> ljid().
|
|
|
|
|
2015-11-24 16:44:13 +01:00
|
|
|
jid_remove_resource(J) -> jid:remove_resource(J).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec jid_replace_resource(jid(), binary()) -> error | jid().
|
2002-12-25 20:41:25 +01:00
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
jid_replace_resource(JID, Resource) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:replace_resource(JID, Resource).
|
2003-10-07 22:31:44 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_iq_namespace(xmlel()) -> binary().
|
2003-10-07 22:31:44 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_iq_namespace(#xmlel{name = <<"iq">>, children = Els}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:remove_cdata(Els) of
|
|
|
|
[#xmlel{attrs = Attrs}] -> fxml:get_attr_s(<<"xmlns">>, Attrs);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> <<"">>
|
2002-12-08 18:23:21 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_iq_namespace(_) -> <<"">>.
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(iq_query_info/1 ::
|
|
|
|
(
|
|
|
|
Xmlel :: xmlel())
|
|
|
|
-> iq_request() | 'reply' | 'invalid' | 'not_iq'
|
|
|
|
).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2010-10-16 20:31:44 +02:00
|
|
|
%% @spec (xmlelement()) -> iq() | reply | invalid | not_iq
|
2013-03-14 10:33:02 +01:00
|
|
|
iq_query_info(El) -> iq_info_internal(El, request).
|
2010-10-16 20:31:44 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
%%
|
|
|
|
-spec(iq_query_or_response_info/1 ::
|
|
|
|
(
|
|
|
|
Xmlel :: xmlel())
|
|
|
|
-> iq_request() | iq_reply() | 'reply' | 'invalid' | 'not_iq'
|
|
|
|
).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
|
|
|
iq_query_or_response_info(El) ->
|
|
|
|
iq_info_internal(El, any).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
iq_info_internal(#xmlel{name = <<"iq">>, attrs = Attrs, children = Els}, Filter) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
ID = fxml:get_attr_s(<<"id">>, Attrs),
|
|
|
|
Lang = fxml:get_attr_s(<<"xml:lang">>, Attrs),
|
|
|
|
{Type, Class} = case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"set">> -> {set, request};
|
|
|
|
<<"get">> -> {get, request};
|
|
|
|
<<"result">> -> {result, reply};
|
|
|
|
<<"error">> -> {error, reply};
|
|
|
|
_ -> {invalid, invalid}
|
|
|
|
end,
|
|
|
|
if Type == invalid -> invalid; Class == request; Filter == any ->
|
2016-02-03 19:03:17 +01:00
|
|
|
FilteredEls = fxml:remove_cdata(Els),
|
2013-03-14 10:33:02 +01:00
|
|
|
{XMLNS, SubEl} = case {Class, FilteredEls} of
|
|
|
|
{request, [#xmlel{attrs = Attrs2}]} ->
|
2016-02-03 19:03:17 +01:00
|
|
|
{fxml:get_attr_s(<<"xmlns">>, Attrs2), hd(FilteredEls)};
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, _} ->
|
|
|
|
NonErrorEls = [El || #xmlel{name = SubName} = El <- FilteredEls,
|
|
|
|
SubName /= <<"error">>],
|
|
|
|
{case NonErrorEls of
|
2016-02-03 19:03:17 +01:00
|
|
|
[NonErrorEl] -> fxml:get_tag_attr_s(<<"xmlns">>, NonErrorEl);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> <<"">>
|
|
|
|
end,
|
|
|
|
FilteredEls};
|
|
|
|
_ ->
|
|
|
|
{<<"">>, []}
|
|
|
|
end,
|
|
|
|
if XMLNS == <<"">>, Class == request ->
|
|
|
|
invalid;
|
|
|
|
true ->
|
|
|
|
#iq{id = ID, type = Type, xmlns = XMLNS, lang = Lang, sub_el = SubEl}
|
|
|
|
end;
|
|
|
|
Class == reply, Filter /= any ->
|
|
|
|
reply
|
2002-12-08 18:23:21 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
iq_info_internal(_, _) -> not_iq.
|
|
|
|
|
|
|
|
-spec is_iq_request_type(set | get | result | error) -> boolean().
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
is_iq_request_type(set) -> true;
|
|
|
|
is_iq_request_type(get) -> true;
|
|
|
|
is_iq_request_type(_) -> false.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
iq_type_to_string(set) -> <<"set">>;
|
|
|
|
iq_type_to_string(get) -> <<"get">>;
|
|
|
|
iq_type_to_string(result) -> <<"result">>;
|
|
|
|
iq_type_to_string(error) -> <<"error">>.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec(iq_to_xml/1 ::
|
|
|
|
(
|
|
|
|
IQ :: iq())
|
|
|
|
-> xmlel()
|
|
|
|
).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
iq_to_xml(#iq{id = ID, type = Type, sub_el = SubEl}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
if ID /= <<"">> ->
|
|
|
|
#xmlel{name = <<"iq">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"id">>, ID}, {<<"type">>, iq_type_to_string(Type)}],
|
2014-07-05 15:53:45 +02:00
|
|
|
children = SubEl};
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
#xmlel{name = <<"iq">>,
|
|
|
|
attrs = [{<<"type">>, iq_type_to_string(Type)}],
|
2014-07-05 15:53:45 +02:00
|
|
|
children = SubEl}
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec(parse_xdata_submit/1 ::
|
|
|
|
(
|
|
|
|
El :: xmlel())
|
|
|
|
-> [{Var::binary(), Values::[binary()]}]
|
|
|
|
%%
|
|
|
|
| 'invalid'
|
|
|
|
).
|
|
|
|
|
|
|
|
parse_xdata_submit(#xmlel{attrs = Attrs, children = Els}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"submit">> ->
|
|
|
|
lists:reverse(parse_xdata_fields(Els, []));
|
|
|
|
<<"form">> -> %% This is a workaround to accept Psi's wrong forms
|
|
|
|
lists:reverse(parse_xdata_fields(Els, []));
|
|
|
|
_ ->
|
|
|
|
invalid
|
2003-01-03 20:36:59 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec(parse_xdata_fields/2 ::
|
|
|
|
(
|
|
|
|
Xmlels :: [xmlel() | cdata()],
|
|
|
|
Res :: [{Var::binary(), Values :: [binary()]}])
|
|
|
|
-> [{Var::binary(), Values::[binary()]}]
|
|
|
|
).
|
|
|
|
|
|
|
|
parse_xdata_fields([], Res) -> Res;
|
|
|
|
parse_xdata_fields([#xmlel{name = <<"field">>, attrs = Attrs, children = SubEls}
|
|
|
|
| Els], Res) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"var">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<>> ->
|
|
|
|
parse_xdata_fields(Els, Res);
|
|
|
|
Var ->
|
|
|
|
Field = {Var, lists:reverse(parse_xdata_values(SubEls, []))},
|
|
|
|
parse_xdata_fields(Els, [Field | Res])
|
2003-01-03 20:36:59 +01:00
|
|
|
end;
|
|
|
|
parse_xdata_fields([_ | Els], Res) ->
|
|
|
|
parse_xdata_fields(Els, Res).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec(parse_xdata_values/2 ::
|
|
|
|
(
|
|
|
|
Xmlels :: [xmlel() | cdata()],
|
|
|
|
Res :: [binary()])
|
|
|
|
-> [binary()]
|
|
|
|
).
|
|
|
|
|
|
|
|
parse_xdata_values([], Res) -> Res;
|
|
|
|
parse_xdata_values([#xmlel{name = <<"value">>, children = SubEls} | Els], Res) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Val = fxml:get_cdata(SubEls),
|
2013-03-14 10:33:02 +01:00
|
|
|
parse_xdata_values(Els, [Val | Res]);
|
2003-01-03 20:36:59 +01:00
|
|
|
parse_xdata_values([_ | Els], Res) ->
|
|
|
|
parse_xdata_values(Els, Res).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec rsm_decode(iq() | xmlel()) -> none | rsm_in().
|
2008-12-23 14:04:42 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
rsm_decode(#iq{sub_el = SubEl}) -> rsm_decode(SubEl);
|
|
|
|
rsm_decode(#xmlel{} = SubEl) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_subtag(SubEl, <<"set">>) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> none;
|
|
|
|
#xmlel{name = <<"set">>, children = SubEls} ->
|
|
|
|
lists:foldl(fun rsm_parse_element/2, #rsm_in{}, SubEls)
|
|
|
|
end.
|
2008-12-23 14:04:42 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
rsm_parse_element(#xmlel{name = <<"max">>, attrs = []} =
|
|
|
|
Elem,
|
|
|
|
RsmIn) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
CountStr = fxml:get_tag_cdata(Elem),
|
2013-03-14 10:33:02 +01:00
|
|
|
{Count, _} = str:to_integer(CountStr),
|
|
|
|
RsmIn#rsm_in{max = Count};
|
|
|
|
rsm_parse_element(#xmlel{name = <<"before">>,
|
|
|
|
attrs = []} =
|
|
|
|
Elem,
|
|
|
|
RsmIn) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
UID = fxml:get_tag_cdata(Elem),
|
2013-03-14 10:33:02 +01:00
|
|
|
RsmIn#rsm_in{direction = before, id = UID};
|
|
|
|
rsm_parse_element(#xmlel{name = <<"after">>,
|
|
|
|
attrs = []} =
|
|
|
|
Elem,
|
|
|
|
RsmIn) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
UID = fxml:get_tag_cdata(Elem),
|
2013-03-14 10:33:02 +01:00
|
|
|
RsmIn#rsm_in{direction = aft, id = UID};
|
|
|
|
rsm_parse_element(#xmlel{name = <<"index">>,
|
|
|
|
attrs = []} =
|
|
|
|
Elem,
|
|
|
|
RsmIn) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
IndexStr = fxml:get_tag_cdata(Elem),
|
2013-03-14 10:33:02 +01:00
|
|
|
{Index, _} = str:to_integer(IndexStr),
|
|
|
|
RsmIn#rsm_in{index = Index};
|
|
|
|
rsm_parse_element(_, RsmIn) -> RsmIn.
|
|
|
|
|
|
|
|
-spec rsm_encode(iq(), rsm_out()) -> iq().
|
|
|
|
|
|
|
|
rsm_encode(#iq{sub_el = SubEl} = IQ, RsmOut) ->
|
|
|
|
Set = #xmlel{name = <<"set">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_RSM}],
|
|
|
|
children = lists:reverse(rsm_encode_out(RsmOut))},
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = SubEls} =
|
|
|
|
SubEl,
|
|
|
|
New = #xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = [Set | SubEls]},
|
|
|
|
IQ#iq{sub_el = New}.
|
|
|
|
|
|
|
|
-spec rsm_encode(none | rsm_out()) -> [xmlel()].
|
|
|
|
|
|
|
|
rsm_encode(none) -> [];
|
|
|
|
rsm_encode(RsmOut) ->
|
|
|
|
[#xmlel{name = <<"set">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_RSM}],
|
|
|
|
children = lists:reverse(rsm_encode_out(RsmOut))}].
|
|
|
|
|
|
|
|
rsm_encode_out(#rsm_out{count = Count, index = Index,
|
|
|
|
first = First, last = Last}) ->
|
2008-12-23 14:04:42 +01:00
|
|
|
El = rsm_encode_first(First, Index, []),
|
2013-03-14 10:33:02 +01:00
|
|
|
El2 = rsm_encode_last(Last, El),
|
2008-12-23 14:04:42 +01:00
|
|
|
rsm_encode_count(Count, El2).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
rsm_encode_first(undefined, undefined, Arr) -> Arr;
|
2008-12-23 14:04:42 +01:00
|
|
|
rsm_encode_first(First, undefined, Arr) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"first">>, attrs = [],
|
|
|
|
children = [{xmlcdata, First}]}
|
|
|
|
| Arr];
|
2008-12-23 14:04:42 +01:00
|
|
|
rsm_encode_first(First, Index, Arr) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"first">>,
|
|
|
|
attrs = [{<<"index">>, i2l(Index)}],
|
|
|
|
children = [{xmlcdata, First}]}
|
|
|
|
| Arr].
|
2008-12-23 14:04:42 +01:00
|
|
|
|
|
|
|
rsm_encode_last(undefined, Arr) -> Arr;
|
|
|
|
rsm_encode_last(Last, Arr) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"last">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Last}]}
|
|
|
|
| Arr].
|
2008-12-23 14:04:42 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
rsm_encode_count(undefined, Arr) -> Arr;
|
|
|
|
rsm_encode_count(Count, Arr) ->
|
|
|
|
[#xmlel{name = <<"count">>, attrs = [],
|
|
|
|
children = [{xmlcdata, i2l(Count)}]}
|
|
|
|
| Arr].
|
2008-12-23 14:04:42 +01:00
|
|
|
|
2015-11-25 08:58:22 +01:00
|
|
|
-spec is_standalone_chat_state(xmlel()) -> boolean().
|
|
|
|
|
2016-04-24 17:09:56 +02:00
|
|
|
is_standalone_chat_state(#xmlel{name = <<"message">>, children = Els}) ->
|
2016-04-24 17:16:28 +02:00
|
|
|
Stripped = [El || #xmlel{name = Name, attrs = Attrs} = El <- Els,
|
|
|
|
fxml:get_attr_s(<<"xmlns">>, Attrs) /= ?NS_CHATSTATES,
|
2016-04-24 17:09:56 +02:00
|
|
|
Name /= <<"thread">>],
|
|
|
|
Stripped == [];
|
2015-11-25 08:58:22 +01:00
|
|
|
is_standalone_chat_state(_El) -> false.
|
|
|
|
|
2015-01-23 00:42:18 +01:00
|
|
|
-spec add_delay_info(xmlel(), jid() | ljid() | binary(), erlang:timestamp())
|
|
|
|
-> xmlel().
|
2014-12-07 15:55:18 +01:00
|
|
|
|
|
|
|
add_delay_info(El, From, Time) ->
|
|
|
|
add_delay_info(El, From, Time, <<"">>).
|
|
|
|
|
2015-01-23 00:42:18 +01:00
|
|
|
-spec add_delay_info(xmlel(), jid() | ljid() | binary(), erlang:timestamp(),
|
2014-12-07 15:55:18 +01:00
|
|
|
binary()) -> xmlel().
|
|
|
|
|
|
|
|
add_delay_info(El, From, Time, Desc) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_subtag_with_xmlns(El, <<"delay">>, ?NS_DELAY) of
|
2014-12-07 15:55:18 +01:00
|
|
|
false ->
|
|
|
|
%% Add new tag
|
2015-05-18 21:26:51 +02:00
|
|
|
DelayTag = create_delay_tag(Time, From, Desc),
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:append_subtags(El, [DelayTag]);
|
2014-12-07 15:55:18 +01:00
|
|
|
DelayTag ->
|
|
|
|
%% Update existing tag
|
|
|
|
NewDelayTag =
|
2016-02-03 19:03:17 +01:00
|
|
|
case {fxml:get_tag_cdata(DelayTag), Desc} of
|
2014-12-07 15:55:18 +01:00
|
|
|
{<<"">>, <<"">>} ->
|
|
|
|
DelayTag;
|
|
|
|
{OldDesc, <<"">>} ->
|
|
|
|
DelayTag#xmlel{children = [{xmlcdata, OldDesc}]};
|
|
|
|
{<<"">>, NewDesc} ->
|
|
|
|
DelayTag#xmlel{children = [{xmlcdata, NewDesc}]};
|
|
|
|
{OldDesc, NewDesc} ->
|
|
|
|
case binary:match(OldDesc, NewDesc) of
|
|
|
|
nomatch ->
|
|
|
|
FinalDesc = <<OldDesc/binary, ", ", NewDesc/binary>>,
|
|
|
|
DelayTag#xmlel{children = [{xmlcdata, FinalDesc}]};
|
|
|
|
_ ->
|
|
|
|
DelayTag#xmlel{children = [{xmlcdata, OldDesc}]}
|
|
|
|
end
|
|
|
|
end,
|
2016-02-03 19:03:17 +01:00
|
|
|
NewEl = fxml:remove_subtags(El, <<"delay">>, {<<"xmlns">>, ?NS_DELAY}),
|
|
|
|
fxml:append_subtags(NewEl, [NewDelayTag])
|
2014-12-07 15:55:18 +01:00
|
|
|
end.
|
|
|
|
|
2015-05-20 12:13:15 +02:00
|
|
|
-spec create_delay_tag(erlang:timestamp(), jid() | ljid() | binary(), binary())
|
|
|
|
-> xmlel() | error.
|
2014-12-07 15:55:18 +01:00
|
|
|
|
2015-05-18 21:26:51 +02:00
|
|
|
create_delay_tag(TimeStamp, FromJID, Desc) when is_tuple(FromJID) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
From = jid:to_string(FromJID),
|
2015-05-18 12:38:07 +02:00
|
|
|
Stamp = now_to_utc_string(TimeStamp, 3),
|
2014-12-07 15:55:18 +01:00
|
|
|
Children = case Desc of
|
|
|
|
<<"">> -> [];
|
|
|
|
_ -> [{xmlcdata, Desc}]
|
|
|
|
end,
|
2015-05-18 12:38:07 +02:00
|
|
|
#xmlel{name = <<"delay">>,
|
2014-12-07 15:55:18 +01:00
|
|
|
attrs =
|
2015-05-18 21:26:51 +02:00
|
|
|
[{<<"xmlns">>, ?NS_DELAY}, {<<"from">>, From},
|
2014-12-07 15:55:18 +01:00
|
|
|
{<<"stamp">>, Stamp}],
|
|
|
|
children = Children};
|
2015-05-18 21:26:51 +02:00
|
|
|
create_delay_tag(DateTime, Host, Desc) when is_binary(Host) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
FromJID = jid:make(<<"">>, Host, <<"">>),
|
2015-05-18 21:26:51 +02:00
|
|
|
create_delay_tag(DateTime, FromJID, Desc).
|
2014-12-07 15:55:18 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-type tz() :: {binary(), {integer(), integer()}} | {integer(), integer()} | utc.
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2010-11-29 13:33:56 +01:00
|
|
|
%% Timezone = utc | {Sign::string(), {Hours, Minutes}} | {Hours, Minutes}
|
2009-06-30 18:51:25 +02:00
|
|
|
%% Hours = integer()
|
|
|
|
%% Minutes = integer()
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec timestamp_to_iso(calendar:datetime(), tz()) -> {binary(), binary()}.
|
|
|
|
|
2016-01-23 09:56:12 +01:00
|
|
|
%% This is the XEP-0082 date and time format
|
|
|
|
%% http://xmpp.org/extensions/xep-0082.html
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
timestamp_to_iso({{Year, Month, Day},
|
|
|
|
{Hour, Minute, Second}},
|
|
|
|
Timezone) ->
|
2009-06-30 18:51:25 +02:00
|
|
|
Timestamp_string =
|
2014-12-12 23:50:03 +01:00
|
|
|
lists:flatten(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0B",
|
2013-03-14 10:33:02 +01:00
|
|
|
[Year, Month, Day, Hour, Minute, Second])),
|
|
|
|
Timezone_string = case Timezone of
|
|
|
|
utc -> "Z";
|
|
|
|
{Sign, {TZh, TZm}} ->
|
2014-12-12 23:50:03 +01:00
|
|
|
io_lib:format("~s~2..0B:~2..0B", [Sign, TZh, TZm]);
|
2013-03-14 10:33:02 +01:00
|
|
|
{TZh, TZm} ->
|
|
|
|
Sign = case TZh >= 0 of
|
|
|
|
true -> "+";
|
|
|
|
false -> "-"
|
|
|
|
end,
|
2014-12-12 23:50:03 +01:00
|
|
|
io_lib:format("~s~2..0B:~2..0B",
|
2013-03-14 10:33:02 +01:00
|
|
|
[Sign, abs(TZh), TZm])
|
|
|
|
end,
|
|
|
|
{iolist_to_binary(Timestamp_string), iolist_to_binary(Timezone_string)}.
|
|
|
|
|
|
|
|
|
2016-01-23 09:56:12 +01:00
|
|
|
-spec timestamp_to_legacy(calendar:datetime()) -> binary().
|
|
|
|
%% This is the jabber legacy format
|
|
|
|
%% http://xmpp.org/extensions/xep-0091.html#time
|
|
|
|
timestamp_to_legacy({{Year, Month, Day},
|
2013-03-14 10:33:02 +01:00
|
|
|
{Hour, Minute, Second}}) ->
|
2014-12-12 23:50:03 +01:00
|
|
|
iolist_to_binary(io_lib:format("~4..0B~2..0B~2..0BT~2..0B:~2..0B:~2..0B",
|
2013-03-14 10:33:02 +01:00
|
|
|
[Year, Month, Day, Hour, Minute, Second])).
|
|
|
|
|
2016-01-23 09:56:12 +01:00
|
|
|
-spec timestamp_to_iso_basic(calendar:datetime()) -> binary().
|
|
|
|
%% This is the ISO 8601 basic bormat
|
|
|
|
timestamp_to_iso_basic({{Year, Month, Day},
|
|
|
|
{Hour, Minute, Second}}) ->
|
|
|
|
iolist_to_binary(io_lib:format("~4..0B~2..0B~2..0BT~2..0B~2..0B~2..0B",
|
|
|
|
[Year, Month, Day, Hour, Minute, Second])).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec now_to_utc_string(erlang:timestamp()) -> binary().
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2004-09-17 21:52:59 +02:00
|
|
|
now_to_utc_string({MegaSecs, Secs, MicroSecs}) ->
|
2014-12-11 23:11:35 +01:00
|
|
|
now_to_utc_string({MegaSecs, Secs, MicroSecs}, 6).
|
|
|
|
|
|
|
|
-spec now_to_utc_string(erlang:timestamp(), 1..6) -> binary().
|
|
|
|
|
|
|
|
now_to_utc_string({MegaSecs, Secs, MicroSecs}, Precision) ->
|
2004-09-17 21:52:59 +02:00
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} =
|
2013-03-14 10:33:02 +01:00
|
|
|
calendar:now_to_universal_time({MegaSecs, Secs,
|
|
|
|
MicroSecs}),
|
2015-01-21 11:06:06 +01:00
|
|
|
Max = round(math:pow(10, Precision)),
|
2015-01-21 11:20:26 +01:00
|
|
|
case round(MicroSecs / math:pow(10, 6 - Precision)) of
|
|
|
|
Max ->
|
|
|
|
now_to_utc_string({MegaSecs, Secs + 1, 0}, Precision);
|
|
|
|
FracOfSec ->
|
|
|
|
list_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT"
|
|
|
|
"~2..0B:~2..0B:~2..0B.~*..0BZ",
|
|
|
|
[Year, Month, Day, Hour, Minute, Second,
|
|
|
|
Precision, FracOfSec]))
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec now_to_local_string(erlang:timestamp()) -> binary().
|
2004-09-17 21:52:59 +02:00
|
|
|
|
|
|
|
now_to_local_string({MegaSecs, Secs, MicroSecs}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
LocalTime = calendar:now_to_local_time({MegaSecs, Secs,
|
|
|
|
MicroSecs}),
|
|
|
|
UTCTime = calendar:now_to_universal_time({MegaSecs,
|
|
|
|
Secs, MicroSecs}),
|
|
|
|
Seconds =
|
|
|
|
calendar:datetime_to_gregorian_seconds(LocalTime) -
|
|
|
|
calendar:datetime_to_gregorian_seconds(UTCTime),
|
|
|
|
{{H, M, _}, Sign} = if Seconds < 0 ->
|
|
|
|
{calendar:seconds_to_time(-Seconds), "-"};
|
|
|
|
true -> {calendar:seconds_to_time(Seconds), "+"}
|
|
|
|
end,
|
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} =
|
|
|
|
LocalTime,
|
2014-12-12 23:50:03 +01:00
|
|
|
list_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0B.~6."
|
|
|
|
".0B~s~2..0B:~2..0B",
|
2013-03-14 10:33:02 +01:00
|
|
|
[Year, Month, Day, Hour, Minute, Second,
|
|
|
|
MicroSecs, Sign, H, M])).
|
2004-09-17 21:52:59 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec datetime_string_to_timestamp(binary()) -> undefined | erlang:timestamp().
|
2004-09-17 21:52:59 +02:00
|
|
|
|
|
|
|
datetime_string_to_timestamp(TimeStr) ->
|
|
|
|
case catch parse_datetime(TimeStr) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', _Err} -> undefined;
|
|
|
|
TimeStamp -> TimeStamp
|
2004-09-17 21:52:59 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
parse_datetime(TimeStr) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[Date, Time] = str:tokens(TimeStr, <<"T">>),
|
2004-09-17 21:52:59 +02:00
|
|
|
D = parse_date(Date),
|
|
|
|
{T, MS, TZH, TZM} = parse_time(Time),
|
|
|
|
S = calendar:datetime_to_gregorian_seconds({D, T}),
|
2013-03-14 10:33:02 +01:00
|
|
|
S1 = calendar:datetime_to_gregorian_seconds({{1970, 1,
|
|
|
|
1},
|
|
|
|
{0, 0, 0}}),
|
|
|
|
Seconds = S - S1 - TZH * 60 * 60 - TZM * 60,
|
2004-09-17 21:52:59 +02:00
|
|
|
{Seconds div 1000000, Seconds rem 1000000, MS}.
|
|
|
|
|
|
|
|
% yyyy-mm-dd
|
|
|
|
parse_date(Date) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[Y, M, D] = str:tokens(Date, <<"-">>),
|
|
|
|
Date1 = {binary_to_integer(Y), binary_to_integer(M),
|
|
|
|
binary_to_integer(D)},
|
2004-09-17 21:52:59 +02:00
|
|
|
case calendar:valid_date(Date1) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> Date1;
|
|
|
|
_ -> false
|
2004-09-17 21:52:59 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
% hh:mm:ss[.sss]TZD
|
|
|
|
parse_time(Time) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case str:str(Time, <<"Z">>) of
|
|
|
|
0 -> parse_time_with_timezone(Time);
|
|
|
|
_ ->
|
|
|
|
[T | _] = str:tokens(Time, <<"Z">>),
|
|
|
|
{TT, MS} = parse_time1(T),
|
|
|
|
{TT, MS, 0, 0}
|
2004-09-17 21:52:59 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
parse_time_with_timezone(Time) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case str:str(Time, <<"+">>) of
|
|
|
|
0 ->
|
|
|
|
case str:str(Time, <<"-">>) of
|
|
|
|
0 -> false;
|
|
|
|
_ -> parse_time_with_timezone(Time, <<"-">>)
|
|
|
|
end;
|
|
|
|
_ -> parse_time_with_timezone(Time, <<"+">>)
|
2004-09-17 21:52:59 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
parse_time_with_timezone(Time, Delim) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[T, TZ] = str:tokens(Time, Delim),
|
2004-09-17 21:52:59 +02:00
|
|
|
{TZH, TZM} = parse_timezone(TZ),
|
|
|
|
{TT, MS} = parse_time1(T),
|
|
|
|
case Delim of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"-">> -> {TT, MS, -TZH, -TZM};
|
|
|
|
<<"+">> -> {TT, MS, TZH, TZM}
|
2004-09-17 21:52:59 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
parse_timezone(TZ) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[H, M] = str:tokens(TZ, <<":">>),
|
2004-09-17 21:52:59 +02:00
|
|
|
{[H1, M1], true} = check_list([{H, 12}, {M, 60}]),
|
|
|
|
{H1, M1}.
|
|
|
|
|
|
|
|
parse_time1(Time) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[HMS | T] = str:tokens(Time, <<".">>),
|
2004-09-17 21:52:59 +02:00
|
|
|
MS = case T of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] -> 0;
|
|
|
|
[Val] -> binary_to_integer(str:left(Val, 6, $0))
|
2004-09-17 21:52:59 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
[H, M, S] = str:tokens(HMS, <<":">>),
|
|
|
|
{[H1, M1, S1], true} = check_list([{H, 24}, {M, 60},
|
|
|
|
{S, 60}]),
|
2004-09-17 21:52:59 +02:00
|
|
|
{{H1, M1, S1}, MS}.
|
|
|
|
|
|
|
|
check_list(List) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:mapfoldl(fun ({L, N}, B) ->
|
|
|
|
V = binary_to_integer(L),
|
|
|
|
if (V >= 0) and (V =< N) -> {V, B};
|
|
|
|
true -> {false, false}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
true, List).
|
2003-03-09 21:46:47 +01:00
|
|
|
|
|
|
|
%
|
|
|
|
% Base64 stuff (based on httpd_util.erl)
|
|
|
|
%
|
|
|
|
|
2014-03-19 00:51:33 +01:00
|
|
|
-spec term_to_base64(term()) -> binary().
|
|
|
|
|
|
|
|
term_to_base64(Term) ->
|
|
|
|
encode_base64(term_to_binary(Term)).
|
|
|
|
|
|
|
|
-spec base64_to_term(binary()) -> {term, term()} | error.
|
|
|
|
|
|
|
|
base64_to_term(Base64) ->
|
|
|
|
case catch binary_to_term(decode_base64(Base64), [safe]) of
|
|
|
|
{'EXIT', _} ->
|
|
|
|
error;
|
|
|
|
Term ->
|
|
|
|
{term, Term}
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec decode_base64(binary()) -> binary().
|
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
decode_base64(S) ->
|
2014-10-25 02:05:02 +02:00
|
|
|
case catch binary:last(S) of
|
|
|
|
C when C == $\n; C == $\s ->
|
|
|
|
decode_base64(binary:part(S, 0, byte_size(S) - 1));
|
|
|
|
_ ->
|
|
|
|
decode_base64_bin(S, <<>>)
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-11-03 14:40:52 +01:00
|
|
|
take_without_spaces(Bin, Count) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
take_without_spaces(Bin, Count, <<>>).
|
|
|
|
|
|
|
|
take_without_spaces(Bin, 0, Acc) ->
|
|
|
|
{Acc, Bin};
|
|
|
|
take_without_spaces(<<>>, _, Acc) ->
|
|
|
|
{Acc, <<>>};
|
|
|
|
take_without_spaces(<<$\s, Tail/binary>>, Count, Acc) ->
|
|
|
|
take_without_spaces(Tail, Count, Acc);
|
|
|
|
take_without_spaces(<<$\t, Tail/binary>>, Count, Acc) ->
|
|
|
|
take_without_spaces(Tail, Count, Acc);
|
|
|
|
take_without_spaces(<<$\n, Tail/binary>>, Count, Acc) ->
|
|
|
|
take_without_spaces(Tail, Count, Acc);
|
|
|
|
take_without_spaces(<<$\r, Tail/binary>>, Count, Acc) ->
|
|
|
|
take_without_spaces(Tail, Count, Acc);
|
|
|
|
take_without_spaces(<<Char:8, Tail/binary>>, Count, Acc) ->
|
|
|
|
take_without_spaces(Tail, Count-1, <<Acc/binary, Char:8>>).
|
|
|
|
|
|
|
|
decode_base64_bin(<<>>, Acc) ->
|
|
|
|
Acc;
|
|
|
|
decode_base64_bin(Bin, Acc) ->
|
|
|
|
case take_without_spaces(Bin, 4) of
|
|
|
|
{<<A, B, $=, $=>>, _} ->
|
|
|
|
<<Acc/binary, (d(A)):6, (d(B) bsr 4):2>>;
|
|
|
|
{<<A, B, C, $=>>, _} ->
|
|
|
|
<<Acc/binary, (d(A)):6, (d(B)):6, (d(C) bsr 2):4>>;
|
|
|
|
{<<A, B, C, D>>, Tail} ->
|
|
|
|
Acc2 = <<Acc/binary, (d(A)):6, (d(B)):6, (d(C)):6, (d(D)):6>>,
|
|
|
|
decode_base64_bin(Tail, Acc2);
|
|
|
|
_ ->
|
|
|
|
<<"">>
|
|
|
|
end.
|
|
|
|
|
|
|
|
d(X) when X >= $A, X =< $Z -> X - 65;
|
|
|
|
d(X) when X >= $a, X =< $z -> X - 71;
|
|
|
|
d(X) when X >= $0, X =< $9 -> X + 4;
|
2003-03-09 21:46:47 +01:00
|
|
|
d($+) -> 62;
|
|
|
|
d($/) -> 63;
|
|
|
|
d(_) -> 63.
|
|
|
|
|
|
|
|
|
2008-05-05 18:22:06 +02:00
|
|
|
%% Convert Erlang inet IP to list
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec encode_base64(binary()) -> binary().
|
|
|
|
|
|
|
|
encode_base64(Data) ->
|
|
|
|
encode_base64_bin(Data, <<>>).
|
|
|
|
|
|
|
|
encode_base64_bin(<<A:6, B:6, C:6, D:6, Tail/binary>>, Acc) ->
|
|
|
|
encode_base64_bin(Tail, <<Acc/binary, (e(A)):8, (e(B)):8, (e(C)):8, (e(D)):8>>);
|
|
|
|
encode_base64_bin(<<A:6, B:6, C:4>>, Acc) ->
|
|
|
|
<<Acc/binary, (e(A)):8, (e(B)):8, (e(C bsl 2)):8, $=>>;
|
|
|
|
encode_base64_bin(<<A:6, B:2>>, Acc) ->
|
|
|
|
<<Acc/binary, (e(A)):8, (e(B bsl 4)):8, $=, $=>>;
|
|
|
|
encode_base64_bin(<<>>, Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
e(X) when X >= 0, X < 26 -> X + 65;
|
|
|
|
e(X) when X > 25, X < 52 -> X + 71;
|
|
|
|
e(X) when X > 51, X < 62 -> X - 4;
|
|
|
|
e(62) -> $+;
|
|
|
|
e(63) -> $/;
|
|
|
|
e(X) -> exit({bad_encode_base64_token, X}).
|
|
|
|
|
|
|
|
-spec ip_to_list(inet:ip_address() | undefined |
|
|
|
|
{inet:ip_address(), inet:port_number()}) -> binary().
|
|
|
|
|
2008-05-05 18:22:06 +02:00
|
|
|
ip_to_list({IP, _Port}) ->
|
|
|
|
ip_to_list(IP);
|
2010-11-02 13:51:36 +01:00
|
|
|
%% This function clause could use inet_parse too:
|
2013-03-14 10:33:02 +01:00
|
|
|
ip_to_list(undefined) ->
|
|
|
|
<<"unknown">>;
|
2010-12-20 16:56:24 +01:00
|
|
|
ip_to_list(IP) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
list_to_binary(inet_parse:ntoa(IP)).
|
|
|
|
|
|
|
|
binary_to_atom(Bin) ->
|
|
|
|
erlang:binary_to_atom(Bin, utf8).
|
|
|
|
|
2015-04-08 15:11:06 +02:00
|
|
|
binary_to_integer(Bin) ->
|
2015-12-22 11:48:25 +01:00
|
|
|
erlang:binary_to_integer(Bin).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
binary_to_integer(Bin, Base) ->
|
2015-12-22 11:48:25 +01:00
|
|
|
erlang:binary_to_integer(Bin, Base).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
integer_to_binary(I) ->
|
2015-12-22 11:48:25 +01:00
|
|
|
erlang:integer_to_binary(I).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
integer_to_binary(I, Base) ->
|
2015-12-22 11:48:25 +01:00
|
|
|
erlang:integer_to_binary(I, Base).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
tuple_to_binary(T) ->
|
|
|
|
iolist_to_binary(tuple_to_list(T)).
|
|
|
|
|
|
|
|
atom_to_binary(A) ->
|
|
|
|
erlang:atom_to_binary(A, utf8).
|
2013-07-04 10:07:53 +02:00
|
|
|
|
2016-05-05 15:42:48 +02:00
|
|
|
expr_to_term(Expr) ->
|
|
|
|
Str = binary_to_list(<<Expr/binary, ".">>),
|
|
|
|
{ok, Tokens, _} = erl_scan:string(Str),
|
|
|
|
{ok, Term} = erl_parse:parse_term(Tokens),
|
|
|
|
Term.
|
|
|
|
|
|
|
|
term_to_expr(Term) ->
|
|
|
|
list_to_binary(io_lib:print(Term)).
|
2013-07-04 10:07:53 +02:00
|
|
|
|
|
|
|
l2i(I) when is_integer(I) -> I;
|
|
|
|
l2i(L) when is_binary(L) -> binary_to_integer(L).
|
|
|
|
|
|
|
|
i2l(I) when is_integer(I) -> integer_to_binary(I);
|
|
|
|
i2l(L) when is_binary(L) -> L.
|
|
|
|
|
|
|
|
i2l(I, N) when is_integer(I) -> i2l(i2l(I), N);
|
|
|
|
i2l(L, N) when is_binary(L) ->
|
|
|
|
case str:len(L) of
|
|
|
|
N -> L;
|
|
|
|
C when C > N -> L;
|
|
|
|
_ -> i2l(<<$0, L/binary>>, N)
|
|
|
|
end.
|
2014-03-12 23:34:14 +01:00
|
|
|
|
2015-02-11 12:08:16 +01:00
|
|
|
-spec queue_drop_while(fun((term()) -> boolean()), ?TQUEUE) -> ?TQUEUE.
|
2014-03-12 23:34:14 +01:00
|
|
|
|
|
|
|
queue_drop_while(F, Q) ->
|
|
|
|
case queue:peek(Q) of
|
|
|
|
{value, Item} ->
|
|
|
|
case F(Item) of
|
|
|
|
true ->
|
|
|
|
queue_drop_while(F, queue:drop(Q));
|
|
|
|
_ ->
|
|
|
|
Q
|
|
|
|
end;
|
|
|
|
empty ->
|
|
|
|
Q
|
|
|
|
end.
|