%% @doc Send a message to a Jabber account.
%% @spec (Type::binary(), From::binary(), To::binary(), Subject::binary(), Body::binary()) -> ok
send_message(Type, From, To, Subject, Body) ->
+ FromJID = jid:from_string(From),
+ ToJID = jid:from_string(To),
Packet = build_packet(Type, Subject, Body),
- send_packet_all_resources(From, To, Packet).
-
-%% @doc Send a packet to a Jabber account.
-%% If a resource was specified in the JID,
-%% the packet is sent only to that specific resource.
-%% If no resource was specified in the JID,
-%% and the user is remote or local but offline,
-%% the packet is sent to the bare JID.
-%% If the user is local and is online in several resources,
-%% the packet is sent to all its resources.
-send_packet_all_resources(FromJIDString, ToJIDString, Packet) ->
- FromJID = jid:from_string(FromJIDString),
- ToJID = jid:from_string(ToJIDString),
- ToUser = ToJID#jid.user,
- ToServer = ToJID#jid.server,
- case ToJID#jid.resource of
- <<>> ->
- send_packet_all_resources(FromJID, ToUser, ToServer, Packet);
- Res ->
- send_packet_all_resources(FromJID, ToUser, ToServer, Res, Packet)
- end.
-
-send_packet_all_resources(FromJID, ToUser, ToServer, Packet) ->
- case ejabberd_sm:get_user_resources(ToUser, ToServer) of
- [] ->
- send_packet_all_resources(FromJID, ToUser, ToServer, <<>>, Packet);
- ToResources ->
- lists:foreach(
- fun(ToResource) ->
- send_packet_all_resources(FromJID, ToUser, ToServer,
- ToResource, Packet)
- end,
- ToResources)
- end.
-
-send_packet_all_resources(FromJID, ToU, ToS, ToR, Packet) ->
- ToJID = jid:make(ToU, ToS, ToR),
ejabberd_router:route(FromJID, ToJID, Packet).
build_packet(Type, Subject, Body) ->