https://github.com/soroush-app/xmpp_utils
Fast and easy-to-use API for working on well-known XMPP data structures
https://github.com/soroush-app/xmpp_utils
erlang erlang-library erlang-otp xmpp xmpp-library
Last synced: over 1 year ago
JSON representation
Fast and easy-to-use API for working on well-known XMPP data structures
- Host: GitHub
- URL: https://github.com/soroush-app/xmpp_utils
- Owner: soroush-app
- License: other
- Archived: true
- Created: 2017-08-27T10:45:53.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-07T15:54:56.000Z (over 8 years ago)
- Last Synced: 2024-10-24T15:38:26.475Z (over 1 year ago)
- Topics: erlang, erlang-library, erlang-otp, xmpp, xmpp-library
- Language: Erlang
- Size: 10.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xmpp_utils
This library provides fast, atomic and easy-to-use API for encoding/decoding some well-known XMPP data structures (stanza and jid).
### Examples
```erlang
Erlang/OTP 19 [erts-8.3] [source-d5c06c6] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V8.3 (abort with ^G)
1> Pkt = <<"foo">>.
<<"foo">>
2> XMPP_XML = xmpp_utils:parse_pkt(Pkt).
{xmpp_utils_xml,<<"message">>,
{xmpp_utils_jid,<<"1">>,<<"domain.zone">>,<<>>},
{xmpp_utils_jid,<<>>,<<"domain">>,<<>>},
<<"chat">>,<<"12345">>,
[{xmlel,<<"body">>,[],[{xmlcdata,<<"foo">>}]}]}
%% This is an Erlang record and in code is equal to:
%% #xmpp_utils_xml{kind = <<"message">>
%% ,from = #xmpp_utils_jid{username = <<"1">>, sever = <<"domain.zone">>, resource = <<>>}
%% ,to = #xmpp_utils_jid{username = <<>>, server = <<"domain">>, reource = <<>>}
%% ,type = <<"chat">>
%% ,id = <<"12345">>
%% ,children = [#xmlel{name = <<"body">>
%% ,attrs = []
%% ,children = [#xmlcdata{content = <<"foo">>}]}]}
%% Just you need to include "xmpp_utils.hrl"
3> xmpp_utils:make_pkt(XMPP_XML).
<<"foo">>
4> xmpp_utils:make_pkt(xmpp_utils:change_from_to(XMPP_XML)).
<<"foo">>
5> xmpp_utils:make_pkt(xmpp_utils:set_from_to(<<"u@s/r">>, <<"g@conference.s">>, XMPP_XML)).
<<"foo">>
6> xmpp_utils:make_pkt(<<"iq">> %% Kind of packet <<"iq">>, <<"presence">>, <<"message">>, etc.
,<<"me@ourserver/Client1">> %% From jid, can be binary or #xmpp_utils_jid{} record
,{xmpp_utils_jid, <<"you">>, <<"ourserver">>, <<"Client2">>} %% Destination(to) jid, can be binary or #xmpp_utils_jid{} record
,<<"get">> %% Type
,<<"id1234567890">> %% ID
,[{xmlel, <<"query">>, [{<<"xmlns">>, <<"urn:xmpp:ping">>}]}] %% Children
,[{<<"xml:lang">>, <<"en">>}] %% If packet has other attributes, defined them in this way
).
<<"">>
7> xmpp_utils:parse_jid(<<"me@server/res">>).
{xmpp_utils_jid,<<"me">>,<<"server">>,<<"res">>}
%% This is an Erlang record and in code is equal to:
%% #xmpp_utils_jid{username = <<"me">>, server = <<"server">>, resource = <<"res">>}
8> xmpp_utils:get_bare_jid(<<"me@server/res">>).
<<"me@server">>
9> xmpp_utils:get_bare_jid(J).
<<"me@server">>
10> xmpp_utils:make_xmpp_error(<<"not-acceptable">>).
{xmlel,<<"error">>,
[{<<"code">>,<<"406">>},{<<"type">>,<<"modify">>}],
[{xmlel,<<"not-acceptable">>,
[{<<"xmlns">>,<<"urn:ietf:params:xml:ns:xmpp-stanzas">>}],
[]}]}
11> xmpp_utils:make_xmpp_error_pkt(<<"not-acceptable">>).
<<"">>
12> xmpp_utils:make_xmpp_error_pkt(<<"item-not-found">>, <<"Why item not found">>).
<<"Why item not found">>
```