From 0beb1c8116a6f396a63d797a3da80c7f9a6920d6 Mon Sep 17 00:00:00 2001 From: Badlop Date: Wed, 6 Feb 2008 20:30:58 +0000 Subject: [PATCH] * src/mod_muc/mod_muc_room.erl: Support for decline of invitation to MUC room (EJAB-515) SVN Revision: 1175 --- ChangeLog | 5 ++++ src/mod_muc/mod_muc_room.erl | 53 +++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 609986079..d93df9332 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2008-02-06 Badlop + + * src/mod_muc/mod_muc_room.erl: Support for decline of invitation + to MUC room (EJAB-515) + 2008-02-06 Mickael Remond * src/mod_pubsub/node_buddy.erl: Fixed typo diff --git a/src/mod_muc/mod_muc_room.erl b/src/mod_muc/mod_muc_room.erl index 90c70780a..4837afc11 100644 --- a/src/mod_muc/mod_muc_room.erl +++ b/src/mod_muc/mod_muc_room.erl @@ -368,10 +368,7 @@ normal_state({route, From, "", "error" -> ok; _ -> - ErrText = "Only occupants are allowed to send messages to the conference", - Err = jlib:make_error_reply( - Packet, ?ERRT_NOT_ACCEPTABLE(Lang, ErrText)), - ejabberd_router:route(StateData#state.jid, From, Err) + handle_roommessage_from_nonparticipant(Packet, Lang, StateData, From) end, {next_state, normal_state, StateData} end; @@ -3101,6 +3098,54 @@ check_invitation(From, Els, Lang, StateData) -> JID end. +%% Handle a message sent to the room by a non-participant. +%% If it is a decline, send to the inviter. +%% Otherwise, an error message is sent to the sender. +handle_roommessage_from_nonparticipant(Packet, Lang, StateData, From) -> + case catch check_decline_invitation(Packet) of + {true, Decline_data} -> + send_decline_invitation(Decline_data, StateData#state.jid); + _ -> + send_error_only_occupants(Packet, Lang, StateData#state.jid, From) + end. + +%% Check in the packet is a decline. +%% If so, also returns the splitted packet. +%% This function must be catched, +%% because it crashes when the packet is not a decline message. +check_decline_invitation(Packet) -> + {xmlelement, "message", PAttrs, _} = Packet, + XEl = xml:get_subtag(Packet, "x"), + ?NS_MUC_USER = xml:get_tag_attr_s("xmlns", XEl), + DEl = xml:get_subtag(XEl, "decline"), + {value, FromString} = xml:get_attr("from", PAttrs), + ToString = xml:get_tag_attr_s("to", DEl), + ToJID = jlib:string_to_jid(ToString), + {true, {Packet, XEl, DEl, FromString, ToJID}}. + +%% Send the decline to the inviter user. +%% The original stanza must be slightly modified. +send_decline_invitation({Packet, XEl, DEl, FromString, ToJID}, RoomJID) -> + {xmlelement, "decline", DAttrs, DEls} = DEl, + DAttrs2 = lists:keydelete("to", 1, DAttrs), + DAttrs3 = [{"from", FromString} | DAttrs2], + DEl2 = {xmlelement, "decline", DAttrs3, DEls}, + XEl2 = replace_subelement(XEl, DEl2), + Packet2 = replace_subelement(Packet, XEl2), + ejabberd_router:route(RoomJID, ToJID, Packet2). + +%% Given an element and a new subelement, +%% replace the instance of the subelement in element with the new subelement. +replace_subelement({xmlelement, Name, Attrs, SubEls}, NewSubEl) -> + {_, NameNewSubEl, _, _} = NewSubEl, + SubEls2 = lists:keyreplace(NameNewSubEl, 2, SubEls, NewSubEl), + {xmlelement, Name, Attrs, SubEls2}. + +send_error_only_occupants(Packet, Lang, RoomJID, From) -> + ErrText = "Only occupants are allowed to send messages to the conference", + Err = jlib:make_error_reply(Packet, ?ERRT_NOT_ACCEPTABLE(Lang, ErrText)), + ejabberd_router:route(RoomJID, From, Err). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Logging -- 2.40.0