📄 localmucroom.java
字号:
private boolean removeAdmin(String bareJID) {
return admins.remove(bareJID);
}
public List<Presence> addMember(String bareJID, String nickname, MUCRole sendRole)
throws ForbiddenException, ConflictException {
MUCRole.Affiliation oldAffiliation = (members.containsKey(bareJID) ?
MUCRole.Affiliation.member : MUCRole.Affiliation.none);
if (isMembersOnly()) {
if (!canOccupantsInvite()) {
if (MUCRole.Affiliation.admin != sendRole.getAffiliation()
&& MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException();
}
}
}
else {
if (MUCRole.Affiliation.admin != sendRole.getAffiliation()
&& MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
throw new ForbiddenException();
}
}
// Check if the desired nickname is already reserved for another member
if (nickname != null && nickname.trim().length() > 0 && members.containsValue(nickname)) {
if (!nickname.equals(members.get(bareJID))) {
throw new ConflictException();
}
}
// Check that the room always has an owner
if (owners.contains(bareJID) && owners.size() == 1) {
throw new ConflictException();
}
// Associate the reserved nickname with the bareJID. If nickname is null then associate an
// empty string
members.put(bareJID, (nickname == null ? "" : nickname));
// Remove the user from other affiliation lists
if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.Affiliation.owner;
}
else if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.Affiliation.admin;
}
else if (removeOutcast(bareJID)) {
oldAffiliation = MUCRole.Affiliation.outcast;
}
// Update the DB if the room is persistent
MUCPersistenceManager.saveAffiliationToDB(
this,
bareJID,
nickname,
MUCRole.Affiliation.member,
oldAffiliation);
// Update other cluster nodes with new member
CacheFactory.doClusterTask(new AddMember(this, bareJID, (nickname == null ? "" : nickname)));
// Update the presence with the new affiliation and inform all occupants
try {
return changeOccupantAffiliation(bareJID, MUCRole.Affiliation.member,
MUCRole.Role.participant);
}
catch (NotAllowedException e) {
// We should never receive this exception....in theory
return null;
}
}
private boolean removeMember(String bareJID) {
boolean answer = members.containsKey(bareJID);
members.remove(bareJID);
return answer;
}
public List<Presence> addOutcast(String bareJID, String reason, MUCRole senderRole)
throws NotAllowedException, ForbiddenException, ConflictException {
MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
if (MUCRole.Affiliation.admin != senderRole.getAffiliation()
&& MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException();
}
// Check that the room always has an owner
if (owners.contains(bareJID) && owners.size() == 1) {
throw new ConflictException();
}
// Check if user is already an outcast
if (outcasts.contains(bareJID)) {
// Do nothing
return Collections.emptyList();
}
// Update the presence with the new affiliation and inform all occupants
// actorJID will be null if the room itself (ie. via admin console) made the request
JID actorJID = senderRole.getUserAddress();
List<Presence> updatedPresences = changeOccupantAffiliation(
bareJID,
MUCRole.Affiliation.outcast,
MUCRole.Role.none);
Element frag;
// Add the status code and reason why the user was banned to the presences that will
// be sent to the room occupants (the banned user will not receive this presences)
for (Presence presence : updatedPresences) {
frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
// Add the status code 301 that indicates that the user was banned
frag.addElement("status").addAttribute("code", "301");
// Add the reason why the user was banned
if (reason != null && reason.trim().length() > 0) {
frag.element("item").addElement("reason").setText(reason);
}
// Remove the banned users from the room. If a user has joined the room from
// different client resources, he/she will be kicked from all the client resources
// Effectively kick the occupant from the room
kickPresence(presence, actorJID);
}
// Update the affiliation lists
outcasts.add(bareJID);
// Remove the user from other affiliation lists
if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.Affiliation.owner;
}
else if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.Affiliation.admin;
}
else if (removeMember(bareJID)) {
oldAffiliation = MUCRole.Affiliation.member;
}
// Update the DB if the room is persistent
MUCPersistenceManager.saveAffiliationToDB(
this,
bareJID,
null,
MUCRole.Affiliation.outcast,
oldAffiliation);
return updatedPresences;
}
private boolean removeOutcast(String bareJID) {
return outcasts.remove(bareJID);
}
public List<Presence> addNone(String bareJID, MUCRole senderRole) throws ForbiddenException,
ConflictException {
MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
if (MUCRole.Affiliation.admin != senderRole.getAffiliation()
&& MUCRole.Affiliation.owner != senderRole.getAffiliation()) {
throw new ForbiddenException();
}
// Check that the room always has an owner
if (owners.contains(bareJID) && owners.size() == 1) {
throw new ConflictException();
}
List<Presence> updatedPresences = null;
boolean wasMember = members.containsKey(bareJID) || admins.contains(bareJID) ||
owners.contains(bareJID);
// Remove the user from ALL the affiliation lists
if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.Affiliation.owner;
}
else if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.Affiliation.admin;
}
else if (removeMember(bareJID)) {
oldAffiliation = MUCRole.Affiliation.member;
}
else if (removeOutcast(bareJID)) {
oldAffiliation = MUCRole.Affiliation.outcast;
}
// Remove the affiliation of this user from the DB if the room is persistent
MUCPersistenceManager.removeAffiliationFromDB(this, bareJID, oldAffiliation);
// Update the presence with the new affiliation and inform all occupants
try {
MUCRole.Role newRole;
if (isMembersOnly() && wasMember) {
newRole = MUCRole.Role.none;
}
else {
newRole = isModerated() ? MUCRole.Role.visitor : MUCRole.Role.participant;
}
updatedPresences = changeOccupantAffiliation(bareJID, MUCRole.Affiliation.none, newRole);
if (isMembersOnly() && wasMember) {
// If the room is members-only, remove the user from the room including a status
// code of 321 to indicate that the user was removed because of an affiliation
// change
Element frag;
// Add the status code to the presences that will be sent to the room occupants
for (Presence presence : updatedPresences) {
// Set the presence as an unavailable presence
presence.setType(Presence.Type.unavailable);
presence.setStatus(null);
frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
// Add the status code 321 that indicates that the user was removed because of
// an affiliation change
frag.addElement("status").addAttribute("code", "321");
// Remove the ex-member from the room. If a user has joined the room from
// different client resources, he/she will be kicked from all the client
// resources.
// Effectively kick the occupant from the room
JID actorJID = senderRole.getUserAddress();
kickPresence(presence, actorJID);
}
}
}
catch (NotAllowedException e) {
// We should never receive this exception....in theory
}
return updatedPresences;
}
public boolean isLocked() {
return lockedTime > 0;
}
public boolean isManuallyLocked() {
return lockedTime > 0 && creationDate.getTime() != lockedTime;
}
public void presenceUpdated(MUCRole occupantRole, Presence newPresence) {
// Ask other cluster nodes to update the presence of the occupant
UpdatePresence request = new UpdatePresence(this, newPresence.createCopy(), occupantRole.getNickname());
CacheFactory.doClusterTask(request);
// Update the presence of the occupant
request = new UpdatePresence(this, newPresence.createCopy(), occupantRole.getNickname());
request.setOriginator(true);
request.run();
// Broadcast new presence of occupant
broadcastPresence(occupantRole.getPresence().createCopy());
}
/**
* Updates the presence of an occupant with the new presence included in the request.
*
* @param updatePresence request to update an occupant's presence.
*/
public void presenceUpdated(UpdatePresence updatePresence) {
MUCRole occupantRole = occupants.get(updatePresence.getNickname().toLowerCase());
if (occupantRole != null) {
occupantRole.setPresence(updatePresence.getPresence());
}
else {
Log.debug("LocalMUCRoom: Failed to update presence of room occupant. Occupant nickname: " + updatePresence.getNickname());
}
}
public void occupantUpdated(UpdateOccupant update) {
MUCRole occupantRole = occupants.get(update.getNickname().toLowerCase());
if (occupantRole != null) {
if (!occupantRole.isLocal()) {
occupantRole.setPresence(update.getPresence());
try {
occupantRole.setRole(update.getRole());
occupantRole.setAffiliation(update.getAffiliation());
} catch (NotAllowedException e) {
// Ignore. Should never happen with remote roles
}
}
else {
Log.error("Tried to update local occupant with info of local occupant?. Occupant nickname: " +
update.getNickname() + " new role: " + update.getRole() + " new affiliation: " +
update.getAffiliation());
}
}
else {
Log.debug("LocalMUCRoom: Failed to update information of room occupant. Occupant nickname: " + update.getNickname());
}
}
public Presence updateOccupant(UpdateOccupantRequest updateRequest) throws NotAllowedException {
MUCRole occupantRole = occupants.get(updateRequest.getNickname().toLowerCase());
if (occupantRole != null) {
if (updateRequest.isAffiliationChanged()) {
occupantRole.setAffiliation(updateRequest.getAffiliation());
}
occupantRole.setRole(updateRequest.getRole());
// Notify the othe cluster nodes to update the occupant
CacheFactory.doClusterTask(new UpdateOccupant(this, occupantRole));
return occupantRole.getPresence();
}
else {
Log.debug("LocalMUCRoom: Failed to update information of local room occupant. Occupant nickname: " +
updateRequest.getNickname());
}
return null;
}
public void memberAdded(AddMember addMember) {
// Associate the reserved nickname with the bareJID
members.put(addMember.getBareJID(), addMember.getNickname());
}
public void nicknameChanged(MUCRole occupantRole, Presence newPresence, String oldNick, String newNick) {
// Ask other cluster nodes to update the nickname of the occupant
ChangeNickname request = new ChangeNickname(this, oldNick, newNick, newPresence.createCopy());
CacheFactory.doClusterTask(request);
// Update the nickname of the occupant
request = new ChangeNickname(this, oldNick, newNick, newPresence.createCopy());
request.setOriginator(true);
request.run();
// Broadcast new presence of occupant
broadcastPresence(occupantRole.getPresence().createCopy());
}
public void nicknameChanged(ChangeNickname changeNickname) {
MUCRole occupantRole = occupants.get(changeNickname.getOldNick().toLowerCase());
if (occupantRole != null) {
// Update the role with the new info
occupantRole.setPresence(changeNickname.getPresence());
occupantRole.changeNickname(changeNickname.getNewNick());
if (changeNickname.isOriginator()) {
// Fire event that user changed his nickname
server.fireNicknameChanged(getRole().getRoleAddress(), occupantRole.getUserA
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -