Guest User

Untitled

a guest
Feb 21st, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. -module(strophe_xmpp).
  2. -export([
  3. presence/1,
  4. presence/6,
  5. message/4,
  6. message/6,
  7. iq/4,
  8. iq/3
  9. ]).
  10.  
  11. -include_lib("xmerl/include/xmerl.hrl").
  12. -include_lib("eunit/include/eunit.hrl").
  13. -include("strophe.hrl").
  14.  
  15.  
  16. f({_X, _Y, [A]}) ->
  17. f({x, A});
  18. f({_X, A}) ->
  19. f(A);
  20. f(A) ->
  21. case A of
  22. none -> false;
  23. _ -> true
  24. end.
  25.  
  26. require(List) ->
  27. case lists:all(fun f/1, List) of
  28. false ->
  29. {error, required_element_error};
  30. _ ->
  31. ok
  32. end.
  33.  
  34.  
  35. presence(Show) ->
  36. presence(Show, none, none, none, none, none).
  37.  
  38. presence(Show, Status, Priority, To, Type, From) when is_list(Show) ->
  39. case require([Show]) of
  40. {error, E} ->
  41. {error, E};
  42. ok ->
  43. {ok, {presence,
  44. lists:filter(fun f/1,
  45. [{type, Type},
  46. {to, To},
  47. {from, From},
  48. {id, id_server:new()}]),
  49.  
  50. lists:filter(fun f/1,
  51. [{show, [], [Show]},
  52. {priority, [], [Priority]},
  53. {status, [], [Status]}])
  54. }}
  55. end.
  56.  
  57.  
  58.  
  59. message(To, Body, Type, Subject) ->
  60. message(To, Body, Type, Subject, none, none).
  61.  
  62. message(To, Body, Type, Subject, Thread, From) ->
  63. case require([To, Body, Type]) of
  64. {error, E} ->
  65. {error, E};
  66. ok ->
  67. {ok, {message,
  68. lists:filter(fun f/1,
  69. [{to, To},
  70. {from, From},
  71. {type, Type},
  72. {id, id_server:new()}]),
  73.  
  74. lists:filter(fun f/1,
  75. [{body, [], [Body]},
  76. {subject, [], [Subject]},
  77. {thread, [], [Thread]}])
  78. }}
  79. end.
  80.  
  81.  
  82.  
  83. iq(get, To, QueryXMLNS) ->
  84. Query = {'query', [{xmlns, QueryXMLNS}], []},
  85. iq(To, "get", [Query]);
  86.  
  87. iq(Type, To, Payload) ->
  88. iq(To, Type, Payload, none).
  89.  
  90. iq(Type, To, Payload, From) ->
  91. case require([Type, Payload]) of
  92. {error, E} ->
  93. {error, E};
  94. ok ->
  95. case lists:member(Type, iq_types()) of
  96. false ->
  97. {error, unrecognized_type};
  98. true ->
  99. case check_payload(Type, Payload) of
  100. {error, E} ->
  101. {error, E};
  102. ok ->
  103. {ok, {iq,
  104. lists:filter(fun f/1,
  105. [{to, To},
  106. {from, From},
  107. {type, Type},
  108. {id, id_server:new()}]),
  109. Payload}}
  110. end
  111. end
  112. end.
  113.  
  114. iq_types() ->
  115. ["get", "set", "result", "error"].
  116.  
  117. check_payload(Type, Payload) when (Type =:= "get") or (Type =:= "set") ->
  118. case Payload of
  119. [X] when is_tuple(X) ->
  120. ok;
  121. _ ->
  122. io:format("~p~n", [Payload]),
  123. {error, incorrect_payload}
  124. end;
  125. check_payload(_Type, _Payload) ->
  126. ok.
  127.  
  128.  
  129.  
  130. presence_test_() ->
  131. [
  132. ?_assertMatch(
  133. {ok, {presence,
  134. [{type,"probe"},
  135. {to,"aconbere@gmail.com"},
  136. {id, _}],
  137.  
  138. [{show,[], ["away"]},
  139. {priority,[], [2]},
  140. {status,[],["out and about"]}]}},
  141.  
  142. presence("away", "out and about", 2, "aconbere@gmail.com", "probe", none)),
  143.  
  144. ?_assertMatch(
  145. {ok, {presence, [{id, _}], [{show, [], ["away"]}]}},
  146. presence("away"))
  147. ].
  148.  
  149. message_test_() ->
  150. [
  151. ?_assertMatch(
  152. {ok, {message,
  153. [{to,"aconbere@gmail.com"}, {type,"chat"}, {id, _}],
  154. [{body,[],["test"]}]}},
  155. message("aconbere@gmail.com", "test", "chat", none))
  156. ].
  157.  
  158. iq_test_() ->
  159. [
  160. ?_assertMatch(
  161. {ok, {iq,
  162. [{to, "aconbere@gmail.com"}, {type, "get"}, {id, _}],
  163. [{'query', [{xmlns, "jabber:query"}], []}]
  164. }},
  165. iq(get, "aconbere@gmail.com", "jabber:query"))
  166. ].
Add Comment
Please, Sign In to add comment