use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method addOutcast.
@Override
public List<Presence> addOutcast(JID jid, String reason, MUCRole senderRole) throws NotAllowedException, ForbiddenException, ConflictException {
final JID bareJID = jid.asBareJID();
lock.writeLock().lock();
try {
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 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);
} finally {
lock.writeLock().unlock();
}
// Update other cluster nodes with new affiliation
CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.outcast));
// based on the group(s) of the affected user(s)
return applyAffiliationChange(senderRole, bareJID, reason);
}
use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method sendPrivatePacket.
@Override
public void sendPrivatePacket(Packet packet, MUCRole senderRole) throws NotFoundException, ForbiddenException {
switch(// intended fall-through
senderRole.getRole()) {
case none:
throw new ForbiddenException();
default:
case visitor:
if (canSendPrivateMessage().equals("participants"))
throw new ForbiddenException();
case participant:
if (canSendPrivateMessage().equals("moderators"))
throw new ForbiddenException();
case moderator:
if (canSendPrivateMessage().equals("none"))
throw new ForbiddenException();
}
String resource = packet.getTo().getResource();
List<MUCRole> occupants = occupantsByNickname.get(resource.toLowerCase());
if (occupants == null || occupants.size() == 0) {
throw new NotFoundException();
}
for (MUCRole occupant : occupants) {
packet.setFrom(senderRole.getRoleAddress());
occupant.send(packet);
if (packet instanceof Message) {
Message message = (Message) packet;
MUCEventDispatcher.privateMessageRecieved(occupant.getUserAddress(), senderRole.getUserAddress(), message);
}
}
}
use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method destroyRoom.
public void destroyRoom(DestroyRoomRequest destroyRequest) {
JID alternateJID = destroyRequest.getAlternateJID();
String reason = destroyRequest.getReason();
Collection<MUCRole> removedRoles = new ArrayList<>();
lock.writeLock().lock();
try {
boolean hasRemoteOccupants = false;
// Remove each occupant
for (MUCRole leaveRole : occupantsByFullJID.values()) {
if (leaveRole != null) {
// list of removed occupants to process later outside of the lock.
if (leaveRole.isLocal()) {
removedRoles.add(leaveRole);
} else {
hasRemoteOccupants = true;
}
removeOccupantRole(leaveRole, destroyRequest.isOriginator());
}
}
endTime = System.currentTimeMillis();
// Set that the room has been destroyed
isDestroyed = true;
if (destroyRequest.isOriginator()) {
if (hasRemoteOccupants) {
// Ask other cluster nodes to remove occupants since room is being destroyed
CacheFactory.doClusterTask(new DestroyRoomRequest(this, alternateJID, reason));
}
// Removes the room from the list of rooms hosted in the service
mucService.removeChatRoom(name);
}
} finally {
lock.writeLock().unlock();
}
// Send an unavailable presence to each removed occupant
for (MUCRole removedRole : removedRoles) {
try {
// Send a presence stanza of type "unavailable" to the occupant
Presence presence = createPresence(Presence.Type.unavailable);
presence.setFrom(removedRole.getRoleAddress());
// A fragment containing the x-extension for room destruction.
Element fragment = presence.addChildElement("x", "http://jabber.org/protocol/muc#user");
Element item = fragment.addElement("item");
item.addAttribute("affiliation", "none");
item.addAttribute("role", "none");
if (alternateJID != null) {
fragment.addElement("destroy").addAttribute("jid", alternateJID.toString());
}
if (reason != null && reason.length() > 0) {
Element destroy = fragment.element("destroy");
if (destroy == null) {
destroy = fragment.addElement("destroy");
}
destroy.addElement("reason").setText(reason);
}
removedRole.send(presence);
} catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
if (destroyRequest.isOriginator()) {
// Remove the room from the DB if the room was persistent
MUCPersistenceManager.deleteFromDB(this);
// Fire event that the room has been destroyed
MUCEventDispatcher.roomDestroyed(getRole().getRoleAddress());
}
}
use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method nicknameChanged.
public void nicknameChanged(ChangeNickname changeNickname) {
List<MUCRole> occupants = occupantsByNickname.get(changeNickname.getOldNick().toLowerCase());
if (occupants != null && occupants.size() > 0) {
for (MUCRole occupant : occupants) {
// Update the role with the new info
occupant.setPresence(changeNickname.getPresence());
occupant.changeNickname(changeNickname.getNewNick());
}
if (changeNickname.isOriginator()) {
// Fire event that user changed his nickname
MUCEventDispatcher.nicknameChanged(getRole().getRoleAddress(), occupants.get(0).getUserAddress(), changeNickname.getOldNick(), changeNickname.getNewNick());
}
// Associate the existing MUCRole with the new nickname
occupantsByNickname.put(changeNickname.getNewNick().toLowerCase(), occupants);
// Remove the old nickname
occupantsByNickname.remove(changeNickname.getOldNick().toLowerCase());
}
}
use of org.jivesoftware.openfire.muc.MUCRole in project Openfire by igniterealtime.
the class LocalMUCRoom method addNone.
@Override
public List<Presence> addNone(JID jid, MUCRole senderRole) throws ForbiddenException, ConflictException {
final JID bareJID = jid.asBareJID();
MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
boolean jidWasAffiliated = false;
lock.writeLock().lock();
try {
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();
}
// Remove the jid from ALL the affiliation lists
if (removeOwner(bareJID)) {
oldAffiliation = MUCRole.Affiliation.owner;
jidWasAffiliated = true;
} else if (removeAdmin(bareJID)) {
oldAffiliation = MUCRole.Affiliation.admin;
jidWasAffiliated = true;
} else if (removeMember(bareJID)) {
oldAffiliation = MUCRole.Affiliation.member;
jidWasAffiliated = true;
} 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);
} finally {
lock.writeLock().unlock();
}
// Update other cluster nodes with new affiliation
CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.none));
if (jidWasAffiliated) {
// based on the group(s) of the affected user(s)
return applyAffiliationChange(senderRole, bareJID, null);
} else {
// no presence updates needed
return Collections.emptyList();
}
}
Aggregations