Skip to content

Commit

Permalink
Merge pull request igniterealtime#616 from guusdk/SMACK-950_MUC-destr…
Browse files Browse the repository at this point in the history
…oy-password

[muc] Add support for 'password' in room destroy
  • Loading branch information
Flowdalic authored Aug 27, 2024
2 parents 9c4fcc0 + 82385ab commit 6c7e88f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void adminRevoked() {
}

@Override
public void roomDestroyed(MultiUserChat alternateMUC, String reason) {
public void roomDestroyed(MultiUserChat alternateMUC, String password, String reason) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void processStanza(final Stanza packet) {
}

for (UserStatusListener listener : userStatusListeners) {
listener.roomDestroyed(alternateMuc, destroy.getReason());
listener.roomDestroyed(alternateMuc, destroy.getPassword(), destroy.getReason());
}
}

Expand Down Expand Up @@ -965,12 +965,32 @@ public void destroy() throws NoResponseException, XMPPErrorException, NotConnect
* @throws InterruptedException if the calling thread was interrupted.
*/
public void destroy(String reason, EntityBareJid alternateJID) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
destroy(reason, alternateJID, null);
}

/**
* Sends a request to the server to destroy the room. The sender of the request
* should be the room's owner. If the sender of the destroy request is not the room's owner
* then the server will answer a "Forbidden" error (403).
*
* @param reason an optional reason for the room destruction.
* @param alternateJID an optional JID of an alternate location.
* @param password an optional password for the alternate location
* @throws XMPPErrorException if an error occurs while trying to destroy the room.
* An error can occur which will be wrapped by an XMPPException --
* XMPP error code 403. The error code can be used to present more
* appropriate error messages to end-users.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
public void destroy(String reason, EntityBareJid alternateJID, String password) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
MUCOwner iq = new MUCOwner();
iq.setTo(room);
iq.setType(IQ.Type.set);

// Create the reason for the room destruction
Destroy destroy = new Destroy(alternateJID, reason);
Destroy destroy = new Destroy(alternateJID, password, reason);
iq.setDestroy(destroy);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* banned, or granted admin permissions or the room is destroyed.
* <p>
* Note that the methods {@link #kicked(Jid, String)}, {@link #banned(Jid, String)} and
* {@link #roomDestroyed(MultiUserChat, String)} will be called before the generic {@link #removed(MUCUser, Presence)}
* {@link #roomDestroyed(MultiUserChat, String, String)} will be called before the generic {@link #removed(MUCUser, Presence)}
* callback will be invoked. The generic {@link #removed(MUCUser, Presence)} callback will be invoked every time the user
* was removed from the MUC involuntarily. It is hence the recommended callback to listen for and act upon.
* </p>
Expand Down Expand Up @@ -161,10 +161,11 @@ default void adminRevoked() {
* Called when the room is destroyed.
*
* @param alternateMUC an alternate MultiUserChat, may be null.
* @param password a password for the alternative MultiUserChat, may be null.
* @param reason the reason why the room was closed, may be null.
* @see #removed(MUCUser, Presence)
*/
default void roomDestroyed(MultiUserChat alternateMUC, String reason) {
default void roomDestroyed(MultiUserChat alternateMUC, String password, String reason) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ public class Destroy implements NamedElement, Serializable {

private final String reason;
private final EntityBareJid jid;
private final String password;

public Destroy(Destroy other) {
this(other.jid, other.reason);
this(other.jid, other.password, other.reason);
}

public Destroy(EntityBareJid alternativeJid, String reason) {
this.jid = alternativeJid;
this.reason = reason;
this.password = null;
}

public Destroy(EntityBareJid alternativeJid, String password, String reason) {
this.jid = alternativeJid;
this.password = password;
this.reason = reason;
}

/**
Expand All @@ -59,6 +67,15 @@ public EntityBareJid getJid() {
return jid;
}

/**
* Returns the password of the alternate location.
*
* @return the password of the alternate location.
*/
public String getPassword() {
return password;
}

/**
* Returns the reason for the room destruction.
*
Expand All @@ -73,6 +90,7 @@ public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclo
XmlStringBuilder xml = new XmlStringBuilder(this);
xml.optAttribute("jid", getJid());
xml.rightAngleBracket();
xml.optElement("password", getPassword());
xml.optElement("reason", getReason());
xml.closeElement(this);
return xml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserExc
final int initialDepth = parser.getDepth();
final EntityBareJid jid = ParserUtils.getBareJidAttribute(parser);
String reason = null;
String password = null;
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch (eventType) {
Expand All @@ -88,6 +89,9 @@ public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserExc
case "reason":
reason = parser.nextText();
break;
case "password":
password = parser.nextText();
break;
}
break;
case END_ELEMENT:
Expand All @@ -100,6 +104,6 @@ public static Destroy parseDestroy(XmlPullParser parser) throws XmlPullParserExc
break;
}
}
return new Destroy(jid, reason);
return new Destroy(jid, password, reason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void mucDestroyTest() throws TimeoutException, Exception {

UserStatusListener userStatusListener = new UserStatusListener() {
@Override
public void roomDestroyed(MultiUserChat alternateMUC, String reason) {
public void roomDestroyed(MultiUserChat alternateMUC, String password, String reason) {
mucDestroyed.signal();
}
};
Expand Down

0 comments on commit 6c7e88f

Please sign in to comment.