Skip to content

Commit

Permalink
add support for extended addressing (xep-0033)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekgorny committed Sep 3, 2018
1 parent e7eece2 commit ae76330
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
8 changes: 8 additions & 0 deletions include/escalus.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@
event_client :: any(),
props :: list()
}).

-record(extaddress, {
type = to :: to | cc | bcc | replyto | replyroom | noreply | ofrom,
desc :: binary() | undefined,
jid :: binary() | undefined,
uri :: binary() | undefined,
node :: binary() | undefined
}).
37 changes: 34 additions & 3 deletions src/escalus_stanza.erl
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ error_element(Type, Condition) ->

-spec message(From, Recipient, Type, Msg) -> exml:element() when
From :: undefined | binary(),
Recipient :: escalus_utils:jid_spec(),
Recipient :: escalus_utils:jid_spec() | {escalus_utils:jid_spec(), [escalus_utils:extaddress()]},
Type :: binary(),
Msg :: binary().
message(From, Recipient, Type, Msg) when is_atom(Recipient) ->
Expand All @@ -308,11 +308,42 @@ message1(From, Recipient, Type, Msg) ->
undefined -> [];
_ -> [{<<"from">>, From}]
end,
{To, AdrsElement} = case Recipient of
{MulticastService, Addresses} ->
{MulticastService, [make_address_block(Addresses)]};
_ ->
{Recipient, []}
end,
#xmlel{name = <<"message">>,
attrs = FromAttr ++ [{<<"type">>, Type},
{<<"to">>, escalus_utils:get_jid(Recipient)}],
{<<"to">>, escalus_utils:get_jid(To)}],
children = [#xmlel{name = <<"body">>,
children = [#xmlcdata{content = Msg}]}]}.
children = [#xmlcdata{content = Msg}]}]
++ AdrsElement
}.

make_address_block(AdrList) ->
#xmlel{name = <<"addresses">>, attrs = [{<<"xmlns">>, ?NS_ADDRESS}],
children = lists:map(fun make_address_element/1, AdrList)}.

make_address_element(Jid) when is_binary(Jid) ->
make_address_element(#extaddress{jid = Jid});
make_address_element(#extaddress{} = Eadr) ->
Attrs = [{<<"type">>, to_type(Eadr#extaddress.type)},
{<<"jid">>, Eadr#extaddress.jid},
{<<"uri">>, Eadr#extaddress.uri},
{<<"node">>, Eadr#extaddress.node},
{<<"desc">>, Eadr#extaddress.desc}],
ValidAttrs = lists:filter(fun({_, undefined}) -> false; (_) -> true end, Attrs),
#xmlel{name = <<"address">>, attrs = ValidAttrs, children = []}.

to_type(to) -> <<"to">>;
to_type(cc) -> <<"cc">>;
to_type(bcc) -> <<"bcc">>;
to_type(replyto) -> <<"replyto">>;
to_type(replyroom) -> <<"replyroom">>;
to_type(noreply) -> <<"noreply">>;
to_type(ofrom) -> <<"ofrom">>.

chat_to(Recipient, Msg) ->
message(undefined, Recipient, <<"chat">>, Msg).
Expand Down

0 comments on commit ae76330

Please sign in to comment.