Skip to content

Instantly share code, notes, and snippets.

@metajack
Created November 20, 2008 16:12
Show Gist options
  • Save metajack/27088 to your computer and use it in GitHub Desktop.
Save metajack/27088 to your computer and use it in GitHub Desktop.
-module(strophe_xmpp).
-export([
presence/1,
presence/6,
message/4,
message/6,
iq/4,
iq/3
]).
-include_lib("xmerl/include/xmerl.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("strophe.hrl").
f({_X, _Y, [A]}) ->
f({x, A});
f({_X, A}) ->
f(A);
f(A) ->
case A of
none -> false;
_ -> true
end.
require(List) ->
case lists:all(fun f/1, List) of
false ->
{error, required_element_error};
_ ->
ok
end.
presence(Show) ->
presence(Show, none, none, none, none, none).
presence(Show, Status, Priority, To, Type, From) when is_list(Show) ->
case require([Show]) of
{error, E} ->
{error, E};
ok ->
{ok, {presence,
lists:filter(fun f/1,
[{type, Type},
{to, To},
{from, From},
{id, id_server:new()}]),
lists:filter(fun f/1,
[{show, [], [Show]},
{priority, [], [Priority]},
{status, [], [Status]}])
}}
end.
message(To, Body, Type, Subject) ->
message(To, Body, Type, Subject, none, none).
message(To, Body, Type, Subject, Thread, From) ->
case require([To, Body, Type]) of
{error, E} ->
{error, E};
ok ->
{ok, {message,
lists:filter(fun f/1,
[{to, To},
{from, From},
{type, Type},
{id, id_server:new()}]),
lists:filter(fun f/1,
[{body, [], [Body]},
{subject, [], [Subject]},
{thread, [], [Thread]}])
}}
end.
iq(get, To, QueryXMLNS) ->
Query = {'query', [{xmlns, QueryXMLNS}], []},
iq(To, "get", [Query]);
iq(Type, To, Payload) ->
iq(To, Type, Payload, none).
iq(Type, To, Payload, From) ->
case require([Type, Payload]) of
{error, E} ->
{error, E};
ok ->
case lists:member(Type, iq_types()) of
false ->
{error, unrecognized_type};
true ->
case check_payload(Type, Payload) of
{error, E} ->
{error, E};
ok ->
{ok, {iq,
lists:filter(fun f/1,
[{to, To},
{from, From},
{type, Type},
{id, id_server:new()}]),
Payload}}
end
end
end.
iq_types() ->
["get", "set", "result", "error"].
check_payload(Type, Payload) when (Type =:= "get") or (Type =:= "set") ->
case Payload of
[X] when is_tuple(X) ->
ok;
_ ->
io:format("~p~n", [Payload]),
{error, incorrect_payload}
end;
check_payload(_Type, _Payload) ->
ok.
presence_test_() ->
[
?_assertMatch(
{ok, {presence,
[{type,"probe"},
{to,"aconbere@gmail.com"},
{id, _}],
[{show,[], ["away"]},
{priority,[], [2]},
{status,[],["out and about"]}]}},
presence("away", "out and about", 2, "aconbere@gmail.com", "probe", none)),
?_assertMatch(
{ok, {presence, [{id, _}], [{show, [], ["away"]}]}},
presence("away"))
].
message_test_() ->
[
?_assertMatch(
{ok, {message,
[{to,"aconbere@gmail.com"}, {type,"chat"}, {id, _}],
[{body,[],["test"]}]}},
message("aconbere@gmail.com", "test", "chat", none))
].
iq_test_() ->
[
?_assertMatch(
{ok, {iq,
[{to, "aconbere@gmail.com"}, {type, "get"}, {id, _}],
[{'query', [{xmlns, "jabber:query"}], []}]
}},
iq(get, "aconbere@gmail.com", "jabber:query"))
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment