1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-23 13:07:59 +01:00

Introduce MUCRole and MUCAffiliation enums

Also rework MUCItem and parseItem to follow current coding guidelines.
This commit is contained in:
Florian Schmaus 2014-08-24 18:30:18 +02:00
parent 754bdada18
commit e19cfa4d83
7 changed files with 215 additions and 156 deletions

View file

@ -28,15 +28,14 @@ import org.jivesoftware.smackx.muc.packet.MUCItem;
*/ */
public class Affiliate { public class Affiliate {
// Fields that must have a value // Fields that must have a value
private String jid; private final String jid;
private String affiliation; private final MUCAffiliation affiliation;
// Fields that may have a value // Fields that may have a value
private String role; private final MUCRole role;
private String nick; private final String nick;
Affiliate(MUCItem item) { Affiliate(MUCItem item) {
super();
this.jid = item.getJid(); this.jid = item.getJid();
this.affiliation = item.getAffiliation(); this.affiliation = item.getAffiliation();
this.role = item.getRole(); this.role = item.getRole();
@ -58,18 +57,18 @@ public class Affiliate {
* *
* @return the affiliation of the afffiliated user. * @return the affiliation of the afffiliated user.
*/ */
public String getAffiliation() { public MUCAffiliation getAffiliation() {
return affiliation; return affiliation;
} }
/** /**
* Returns the current role of the affiliated user if the user is currently in the room. * Returns the current role of the affiliated user if the user is currently in the room.
* If the user is not present in the room then the answer will be null. * If the user is not present in the room then the answer will be 'none'.
* *
* @return the current role of the affiliated user in the room or null if the user is not in * @return the current role of the affiliated user in the room or null if the user is not in
* the room. * the room.
*/ */
public String getRole() { public MUCRole getRole() {
return role; return role;
} }

View file

@ -0,0 +1,41 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.muc;
import java.util.Locale;
/**
* XEP-0045: Multi-User-Chat - 5.2 Affiliations
*
* @see <a href="http://xmpp.org/extensions/xep-0045.html#affil">XEP-0045: Multi-User-Chat - 5.2 Affiliations</a>
*/
public enum MUCAffiliation {
owner,
admin,
member,
outcast,
none;
public static MUCAffiliation fromString(String string) {
if (string == null) {
return null;
}
return MUCAffiliation.valueOf(string.toLowerCase(Locale.US));
}
}

View file

@ -0,0 +1,39 @@
/**
*
* Copyright © 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.muc;
import java.util.Locale;
/**
* XEP-0045: Multi User Chat - 5.1 Roles
*
* @see <a href="http://xmpp.org/extensions/xep-0045.html#roles">XEP-0045: Multi-User-Chat - 5.1 Roles</a>
*/
public enum MUCRole {
moderator,
none,
participant,
visitor;
public static MUCRole fromString(String string) {
if (string == null) {
return null;
}
return MUCRole.valueOf(string.toLowerCase(Locale.US));
}
}

View file

@ -1018,7 +1018,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void kickParticipant(String nickname, String reason) throws XMPPErrorException, NoResponseException, NotConnectedException { public void kickParticipant(String nickname, String reason) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nickname, "none", reason); changeRole(nickname, MUCRole.none, reason);
} }
/** /**
@ -1035,7 +1035,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantVoice(Collection<String> nicknames) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantVoice(Collection<String> nicknames) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nicknames, "participant"); changeRole(nicknames, MUCRole.participant);
} }
/** /**
@ -1052,7 +1052,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantVoice(String nickname) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantVoice(String nickname) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nickname, "participant", null); changeRole(nickname, MUCRole.participant, null);
} }
/** /**
@ -1069,7 +1069,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeVoice(Collection<String> nicknames) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeVoice(Collection<String> nicknames) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nicknames, "visitor"); changeRole(nicknames, MUCRole.visitor);
} }
/** /**
@ -1086,7 +1086,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeVoice(String nickname) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeVoice(String nickname) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nickname, "visitor", null); changeRole(nickname, MUCRole.visitor, null);
} }
/** /**
@ -1104,7 +1104,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void banUsers(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException { public void banUsers(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jids, "outcast"); changeAffiliationByAdmin(jids, MUCAffiliation.outcast);
} }
/** /**
@ -1123,7 +1123,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void banUser(String jid, String reason) throws XMPPErrorException, NoResponseException, NotConnectedException { public void banUser(String jid, String reason) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jid, "outcast", reason); changeAffiliationByAdmin(jid, MUCAffiliation.outcast, reason);
} }
/** /**
@ -1137,7 +1137,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantMembership(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantMembership(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jids, "member"); changeAffiliationByAdmin(jids, MUCAffiliation.member);
} }
/** /**
@ -1151,7 +1151,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantMembership(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantMembership(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jid, "member", null); changeAffiliationByAdmin(jid, MUCAffiliation.member, null);
} }
/** /**
@ -1166,7 +1166,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeMembership(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeMembership(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jids, "none"); changeAffiliationByAdmin(jids, MUCAffiliation.none);
} }
/** /**
@ -1181,7 +1181,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeMembership(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeMembership(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jid, "none", null); changeAffiliationByAdmin(jid, MUCAffiliation.none, null);
} }
/** /**
@ -1195,7 +1195,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantModerator(Collection<String> nicknames) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantModerator(Collection<String> nicknames) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nicknames, "moderator"); changeRole(nicknames, MUCRole.moderator);
} }
/** /**
@ -1209,7 +1209,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantModerator(String nickname) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantModerator(String nickname) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nickname, "moderator", null); changeRole(nickname, MUCRole.moderator, null);
} }
/** /**
@ -1224,7 +1224,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeModerator(Collection<String> nicknames) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeModerator(Collection<String> nicknames) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nicknames, "participant"); changeRole(nicknames, MUCRole.participant);
} }
/** /**
@ -1239,7 +1239,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeModerator(String nickname) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeModerator(String nickname) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeRole(nickname, "participant", null); changeRole(nickname, MUCRole.participant, null);
} }
/** /**
@ -1254,7 +1254,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantOwnership(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantOwnership(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jids, "owner"); changeAffiliationByAdmin(jids, MUCAffiliation.owner);
} }
/** /**
@ -1269,7 +1269,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantOwnership(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantOwnership(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jid, "owner", null); changeAffiliationByAdmin(jid, MUCAffiliation.owner, null);
} }
/** /**
@ -1283,7 +1283,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeOwnership(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeOwnership(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jids, "admin"); changeAffiliationByAdmin(jids, MUCAffiliation.admin);
} }
/** /**
@ -1297,7 +1297,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeOwnership(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeOwnership(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jid, "admin", null); changeAffiliationByAdmin(jid, MUCAffiliation.admin, null);
} }
/** /**
@ -1311,7 +1311,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantAdmin(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantAdmin(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jids, "admin"); changeAffiliationByAdmin(jids, MUCAffiliation.admin);
} }
/** /**
@ -1326,7 +1326,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void grantAdmin(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException { public void grantAdmin(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jid, "admin"); changeAffiliationByAdmin(jid, MUCAffiliation.admin);
} }
/** /**
@ -1340,7 +1340,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeAdmin(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeAdmin(Collection<String> jids) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jids, "member"); changeAffiliationByAdmin(jids, MUCAffiliation.admin);
} }
/** /**
@ -1355,7 +1355,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public void revokeAdmin(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException { public void revokeAdmin(String jid) throws XMPPErrorException, NoResponseException, NotConnectedException {
changeAffiliationByAdmin(jid, "member"); changeAffiliationByAdmin(jid, MUCAffiliation.member);
} }
/** /**
@ -1367,7 +1367,7 @@ public class MultiUserChat {
* @throws NoResponseException * @throws NoResponseException
* @throws NotConnectedException * @throws NotConnectedException
*/ */
private void changeAffiliationByAdmin(String jid, String affiliation) private void changeAffiliationByAdmin(String jid, MUCAffiliation affiliation)
throws NoResponseException, XMPPErrorException, throws NoResponseException, XMPPErrorException,
NotConnectedException { NotConnectedException {
changeAffiliationByAdmin(jid, affiliation, null); changeAffiliationByAdmin(jid, affiliation, null);
@ -1383,56 +1383,50 @@ public class MultiUserChat {
* @throws NoResponseException * @throws NoResponseException
* @throws NotConnectedException * @throws NotConnectedException
*/ */
private void changeAffiliationByAdmin(String jid, String affiliation, String reason) throws NoResponseException, XMPPErrorException, NotConnectedException private void changeAffiliationByAdmin(String jid, MUCAffiliation affiliation, String reason) throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
MUCAdmin iq = new MUCAdmin(); MUCAdmin iq = new MUCAdmin();
iq.setTo(room); iq.setTo(room);
iq.setType(IQ.Type.set); iq.setType(IQ.Type.set);
// Set the new affiliation. // Set the new affiliation.
MUCItem item = new MUCItem(affiliation, null); MUCItem item = new MUCItem(affiliation, jid, reason);
item.setJid(jid);
item.setReason(reason);
iq.addItem(item); iq.addItem(item);
connection.createPacketCollectorAndSend(iq).nextResultOrThrow(); connection.createPacketCollectorAndSend(iq).nextResultOrThrow();
} }
private void changeAffiliationByAdmin(Collection<String> jids, String affiliation) private void changeAffiliationByAdmin(Collection<String> jids, MUCAffiliation affiliation)
throws NoResponseException, XMPPErrorException, NotConnectedException { throws NoResponseException, XMPPErrorException, NotConnectedException {
MUCAdmin iq = new MUCAdmin(); MUCAdmin iq = new MUCAdmin();
iq.setTo(room); iq.setTo(room);
iq.setType(IQ.Type.set); iq.setType(IQ.Type.set);
for (String jid : jids) { for (String jid : jids) {
// Set the new affiliation. // Set the new affiliation.
MUCItem item = new MUCItem(affiliation, null); MUCItem item = new MUCItem(affiliation, jid);
item.setJid(jid);
iq.addItem(item); iq.addItem(item);
} }
connection.createPacketCollectorAndSend(iq).nextResultOrThrow(); connection.createPacketCollectorAndSend(iq).nextResultOrThrow();
} }
private void changeRole(String nickname, String role, String reason) throws NoResponseException, XMPPErrorException, NotConnectedException { private void changeRole(String nickname, MUCRole role, String reason) throws NoResponseException, XMPPErrorException, NotConnectedException {
MUCAdmin iq = new MUCAdmin(); MUCAdmin iq = new MUCAdmin();
iq.setTo(room); iq.setTo(room);
iq.setType(IQ.Type.set); iq.setType(IQ.Type.set);
// Set the new role. // Set the new role.
MUCItem item = new MUCItem(null, role); MUCItem item = new MUCItem(role, nickname, reason);
item.setNick(nickname);
item.setReason(reason);
iq.addItem(item); iq.addItem(item);
connection.createPacketCollectorAndSend(iq).nextResultOrThrow(); connection.createPacketCollectorAndSend(iq).nextResultOrThrow();
} }
private void changeRole(Collection<String> nicknames, String role) throws NoResponseException, XMPPErrorException, NotConnectedException { private void changeRole(Collection<String> nicknames, MUCRole role) throws NoResponseException, XMPPErrorException, NotConnectedException {
MUCAdmin iq = new MUCAdmin(); MUCAdmin iq = new MUCAdmin();
iq.setTo(room); iq.setTo(room);
iq.setType(IQ.Type.set); iq.setType(IQ.Type.set);
for (String nickname : nicknames) { for (String nickname : nicknames) {
// Set the new role. // Set the new role.
MUCItem item = new MUCItem(null, role); MUCItem item = new MUCItem(role, nickname);
item.setNick(nickname);
iq.addItem(item); iq.addItem(item);
} }
@ -1532,7 +1526,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public Collection<Affiliate> getOwners() throws NoResponseException, XMPPErrorException, NotConnectedException { public Collection<Affiliate> getOwners() throws NoResponseException, XMPPErrorException, NotConnectedException {
return getAffiliatesByAdmin("owner"); return getAffiliatesByAdmin(MUCAffiliation.owner);
} }
/** /**
@ -1544,7 +1538,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public Collection<Affiliate> getAdmins() throws NoResponseException, XMPPErrorException, NotConnectedException { public Collection<Affiliate> getAdmins() throws NoResponseException, XMPPErrorException, NotConnectedException {
return getAffiliatesByAdmin("admin"); return getAffiliatesByAdmin(MUCAffiliation.admin);
} }
/** /**
@ -1556,7 +1550,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public Collection<Affiliate> getMembers() throws NoResponseException, XMPPErrorException, NotConnectedException { public Collection<Affiliate> getMembers() throws NoResponseException, XMPPErrorException, NotConnectedException {
return getAffiliatesByAdmin("member"); return getAffiliatesByAdmin(MUCAffiliation.member);
} }
/** /**
@ -1568,7 +1562,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public Collection<Affiliate> getOutcasts() throws NoResponseException, XMPPErrorException, NotConnectedException { public Collection<Affiliate> getOutcasts() throws NoResponseException, XMPPErrorException, NotConnectedException {
return getAffiliatesByAdmin("outcast"); return getAffiliatesByAdmin(MUCAffiliation.outcast);
} }
/** /**
@ -1581,12 +1575,12 @@ public class MultiUserChat {
* @throws NoResponseException if there was no response from the server. * @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException * @throws NotConnectedException
*/ */
private Collection<Affiliate> getAffiliatesByAdmin(String affiliation) throws NoResponseException, XMPPErrorException, NotConnectedException { private Collection<Affiliate> getAffiliatesByAdmin(MUCAffiliation affiliation) throws NoResponseException, XMPPErrorException, NotConnectedException {
MUCAdmin iq = new MUCAdmin(); MUCAdmin iq = new MUCAdmin();
iq.setTo(room); iq.setTo(room);
iq.setType(IQ.Type.get); iq.setType(IQ.Type.get);
// Set the specified affiliation. This may request the list of owners/admins/members/outcasts. // Set the specified affiliation. This may request the list of owners/admins/members/outcasts.
MUCItem item = new MUCItem(affiliation, null); MUCItem item = new MUCItem(affiliation);
iq.addItem(item); iq.addItem(item);
MUCAdmin answer = (MUCAdmin) connection.createPacketCollectorAndSend(iq).nextResultOrThrow(); MUCAdmin answer = (MUCAdmin) connection.createPacketCollectorAndSend(iq).nextResultOrThrow();
@ -1608,7 +1602,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public Collection<Occupant> getModerators() throws NoResponseException, XMPPErrorException, NotConnectedException { public Collection<Occupant> getModerators() throws NoResponseException, XMPPErrorException, NotConnectedException {
return getOccupants("moderator"); return getOccupants(MUCRole.moderator);
} }
/** /**
@ -1620,7 +1614,7 @@ public class MultiUserChat {
* @throws NotConnectedException * @throws NotConnectedException
*/ */
public Collection<Occupant> getParticipants() throws NoResponseException, XMPPErrorException, NotConnectedException { public Collection<Occupant> getParticipants() throws NoResponseException, XMPPErrorException, NotConnectedException {
return getOccupants("participant"); return getOccupants(MUCRole.participant);
} }
/** /**
@ -1633,12 +1627,12 @@ public class MultiUserChat {
* @throws NoResponseException if there was no response from the server. * @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException * @throws NotConnectedException
*/ */
private Collection<Occupant> getOccupants(String role) throws NoResponseException, XMPPErrorException, NotConnectedException { private Collection<Occupant> getOccupants(MUCRole role) throws NoResponseException, XMPPErrorException, NotConnectedException {
MUCAdmin iq = new MUCAdmin(); MUCAdmin iq = new MUCAdmin();
iq.setTo(room); iq.setTo(room);
iq.setType(IQ.Type.get); iq.setType(IQ.Type.get);
// Set the specified role. This may request the list of moderators/participants. // Set the specified role. This may request the list of moderators/participants.
MUCItem item = new MUCItem(null, role); MUCItem item = new MUCItem(role);
iq.addItem(item); iq.addItem(item);
MUCAdmin answer = (MUCAdmin) connection.createPacketCollectorAndSend(iq).nextResultOrThrow(); MUCAdmin answer = (MUCAdmin) connection.createPacketCollectorAndSend(iq).nextResultOrThrow();
@ -1945,12 +1939,12 @@ public class MultiUserChat {
if (oldPresence != null) { if (oldPresence != null) {
// Get the previous occupant's affiliation & role // Get the previous occupant's affiliation & role
MUCUser mucExtension = MUCUser.getFrom(packet); MUCUser mucExtension = MUCUser.getFrom(packet);
String oldAffiliation = mucExtension.getItem().getAffiliation(); MUCAffiliation oldAffiliation = mucExtension.getItem().getAffiliation();
String oldRole = mucExtension.getItem().getRole(); MUCRole oldRole = mucExtension.getItem().getRole();
// Get the new occupant's affiliation & role // Get the new occupant's affiliation & role
mucExtension = MUCUser.getFrom(packet); mucExtension = MUCUser.getFrom(packet);
String newAffiliation = mucExtension.getItem().getAffiliation(); MUCAffiliation newAffiliation = mucExtension.getItem().getAffiliation();
String newRole = mucExtension.getItem().getRole(); MUCRole newRole = mucExtension.getItem().getRole();
// Fire role modification events // Fire role modification events
checkRoleModifications(oldRole, newRole, isUserStatusModification, from); checkRoleModifications(oldRole, newRole, isUserStatusModification, from);
// Fire affiliation modification events // Fire affiliation modification events
@ -2054,8 +2048,8 @@ public class MultiUserChat {
* (e.g. room@conference.jabber.org/nick). * (e.g. room@conference.jabber.org/nick).
*/ */
private void checkRoleModifications( private void checkRoleModifications(
String oldRole, MUCRole oldRole,
String newRole, MUCRole newRole,
boolean isUserModification, boolean isUserModification,
String from) { String from) {
// Voice was granted to a visitor // Voice was granted to a visitor
@ -2168,8 +2162,8 @@ public class MultiUserChat {
* (e.g. room@conference.jabber.org/nick). * (e.g. room@conference.jabber.org/nick).
*/ */
private void checkAffiliationModifications( private void checkAffiliationModifications(
String oldAffiliation, MUCAffiliation oldAffiliation,
String newAffiliation, MUCAffiliation newAffiliation,
boolean isUserModification, boolean isUserModification,
String from) { String from) {
// First check for revoked affiliation and then for granted affiliations. The idea is to // First check for revoked affiliation and then for granted affiliations. The idea is to

View file

@ -30,11 +30,11 @@ import org.jxmpp.util.XmppStringUtils;
*/ */
public class Occupant { public class Occupant {
// Fields that must have a value // Fields that must have a value
private String affiliation; private final MUCAffiliation affiliation;
private String role; private final MUCRole role;
// Fields that may have a value // Fields that may have a value
private String jid; private final String jid;
private String nick; private final String nick;
Occupant(MUCItem item) { Occupant(MUCItem item) {
this.jid = item.getJid(); this.jid = item.getJid();
@ -72,7 +72,7 @@ public class Occupant {
* *
* @return the affiliation of the occupant. * @return the affiliation of the occupant.
*/ */
public String getAffiliation() { public MUCAffiliation getAffiliation() {
return affiliation; return affiliation;
} }
@ -82,7 +82,7 @@ public class Occupant {
* *
* @return the current role of the occupant in the room. * @return the current role of the occupant in the room.
*/ */
public String getRole() { public MUCRole getRole() {
return role; return role;
} }

View file

@ -19,6 +19,8 @@ package org.jivesoftware.smackx.muc.packet;
import org.jivesoftware.smack.packet.Element; import org.jivesoftware.smack.packet.Element;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.muc.MUCAffiliation;
import org.jivesoftware.smackx.muc.MUCRole;
/** /**
* Item child that holds information about roles, affiliation, jids and nicks. * Item child that holds information about roles, affiliation, jids and nicks.
@ -28,20 +30,35 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
public class MUCItem implements Element { public class MUCItem implements Element {
public static final String ELEMENT = IQ.ITEM; public static final String ELEMENT = IQ.ITEM;
private final String affiliation; private final MUCAffiliation affiliation;
private String role; private final MUCRole role;
private String actor; private final String actor;
private String reason; private final String reason;
private String jid; private final String jid;
private String nick; private final String nick;
/** public MUCItem(MUCAffiliation affiliation) {
* Creates a new item child. this(affiliation, null, null, null, null, null);
* }
* @param affiliation the actor's affiliation to the room
*/ public MUCItem(MUCRole role) {
public MUCItem(String affiliation) { this(null, role, null, null, null, null);
this.affiliation = affiliation; }
public MUCItem(MUCRole role, String nick) {
this(null, role, null, null, null, nick);
}
public MUCItem(MUCAffiliation affiliation, String jid, String reason) {
this(affiliation, null, null, reason, jid, null);
}
public MUCItem(MUCAffiliation affiliation, String jid) {
this(affiliation, null, null, null, jid, null);
}
public MUCItem(MUCRole role, String nick, String reason) {
this(null, role, null, reason, null, nick);
} }
/** /**
@ -49,10 +66,19 @@ public class MUCItem implements Element {
* *
* @param affiliation the actor's affiliation to the room * @param affiliation the actor's affiliation to the room
* @param role the privilege level of an occupant within a room. * @param role the privilege level of an occupant within a room.
* @param actor
* @param reason
* @param jid
* @param nick
*/ */
public MUCItem(String affiliation, String role) { public MUCItem(MUCAffiliation affiliation, MUCRole role, String actor,
this(affiliation); String reason, String jid, String nick) {
this.affiliation = affiliation;
this.role = role; this.role = role;
this.actor = actor;
this.reason = reason;
this.jid = jid;
this.nick = nick;
} }
/** /**
@ -82,7 +108,7 @@ public class MUCItem implements Element {
* *
* @return the actor's affiliation to the room * @return the actor's affiliation to the room
*/ */
public String getAffiliation() { public MUCAffiliation getAffiliation() {
return affiliation; return affiliation;
} }
@ -108,71 +134,23 @@ public class MUCItem implements Element {
/** /**
* Returns the temporary position or privilege level of an occupant within a room. The possible * Returns the temporary position or privilege level of an occupant within a room. The possible
* roles are "moderator", "participant", and "visitor" (it is also possible to have no defined * roles are "moderator", "participant", "visitor" and "none" (it is also possible to have no defined
* role). A role lasts only for the duration of an occupant's visit to a room. * role). A role lasts only for the duration of an occupant's visit to a room.
* *
* @return the privilege level of an occupant within a room. * @return the privilege level of an occupant within a room.
*/ */
public String getRole() { public MUCRole getRole() {
return role; return role;
} }
/**
* Sets the actor (JID of an occupant in the room) that was kicked or banned.
*
* @param actor the actor (JID of an occupant in the room) that was kicked or banned.
*/
public void setActor(String actor) {
this.actor = actor;
}
/**
* Sets the reason for the item child. The reason is optional and could be used to explain the
* reason why a user (occupant) was kicked or banned.
*
* @param reason the reason why a user (occupant) was kicked or banned.
*/
public void setReason(String reason) {
this.reason = reason;
}
/**
* Sets the <room@service/nick> by which an occupant is identified within the context of a room.
* If the room is non-anonymous, the JID will be included in the item.
*
* @param jid the JID by which an occupant is identified within a room.
*/
public void setJid(String jid) {
this.jid = jid;
}
/**
* Sets the new nickname of an occupant that is changing his/her nickname. The new nickname is
* sent as part of the unavailable presence.
*
* @param nick the new nickname of an occupant that is changing his/her nickname.
*/
public void setNick(String nick) {
this.nick = nick;
}
/**
* Sets the temporary position or privilege level of an occupant within a room. The possible
* roles are "moderator", "participant", and "visitor" (it is also possible to have no defined
* role). A role lasts only for the duration of an occupant's visit to a room.
*
* @param role the new privilege level of an occupant within a room.
*/
public void setRole(String role) {
this.role = role;
}
public XmlStringBuilder toXML() { public XmlStringBuilder toXML() {
XmlStringBuilder xml = new XmlStringBuilder(this); XmlStringBuilder xml = new XmlStringBuilder(this);
xml.optAttribute("affiliation", getAffiliation()); xml.optAttribute("affiliation", getAffiliation());
xml.optAttribute("jid", getJid()); xml.optAttribute("jid", getJid());
xml.optAttribute("nick", getNick()); xml.optAttribute("nick", getNick());
xml.optAttribute("role", getRole()); if (role != MUCRole.none) {
xml.attribute("role", getRole());
}
xml.rightAngleBracket(); xml.rightAngleBracket();
xml.optElement("reason", getReason()); xml.optElement("reason", getReason());
if (getActor() != null) { if (getActor() != null) {

View file

@ -16,34 +16,42 @@
*/ */
package org.jivesoftware.smackx.muc.provider; package org.jivesoftware.smackx.muc.provider;
import org.jivesoftware.smackx.muc.MUCAffiliation;
import org.jivesoftware.smackx.muc.MUCRole;
import org.jivesoftware.smackx.muc.packet.Destroy; import org.jivesoftware.smackx.muc.packet.Destroy;
import org.jivesoftware.smackx.muc.packet.MUCItem; import org.jivesoftware.smackx.muc.packet.MUCItem;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
public class MUCParserUtils { public class MUCParserUtils {
public static MUCItem parseItem(XmlPullParser parser) throws Exception { public static MUCItem parseItem(XmlPullParser parser) throws Exception {
boolean done = false; int initialDepth = parser.getDepth();
MUCItem item = new MUCItem(parser.getAttributeValue("", "affiliation")); MUCAffiliation affiliation = MUCAffiliation.fromString(parser.getAttributeValue("", "affiliation"));
item.setNick(parser.getAttributeValue("", "nick")); String nick = parser.getAttributeValue("", "nick");
item.setRole(parser.getAttributeValue("", "role")); MUCRole role = MUCRole.fromString(parser.getAttributeValue("", "role"));
item.setJid(parser.getAttributeValue("", "jid")); String jid = parser.getAttributeValue("", "jid");
while (!done) { String actor = null;
String reason = null;
outerloop: while (true) {
int eventType = parser.next(); int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) { switch (eventType) {
if (parser.getName().equals("actor")) { case XmlPullParser.START_TAG:
item.setActor(parser.getAttributeValue("", "jid")); String name = parser.getName();
switch (name) {
case "actor":
actor = parser.getAttributeValue("", "jid");
break;
case "reason":
reason = parser.nextText();
break;
} }
if (parser.getName().equals("reason")) { case XmlPullParser.END_TAG:
item.setReason(parser.nextText()); if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
} }
} }
else if (eventType == XmlPullParser.END_TAG) { return new MUCItem(affiliation, role, actor, reason, jid, nick);
if (parser.getName().equals("item")) {
done = true;
}
}
}
return item;
} }
public static Destroy parseDestroy(XmlPullParser parser) throws Exception { public static Destroy parseDestroy(XmlPullParser parser) throws Exception {