use of org.jivesoftware.openfire.muc.NotAllowedException in project Openfire by igniterealtime.
the class MultiUserChatServiceImpl method checkForTimedOutUsers.
private void checkForTimedOutUsers() {
final long deadline = System.currentTimeMillis() - user_idle;
for (LocalMUCUser user : users.values()) {
try {
// the list of users
if (!user.isJoined()) {
removeUser(user.getAddress());
continue;
}
// Do nothing if this feature is disabled (i.e USER_IDLE equals -1)
if (user_idle == -1) {
continue;
}
if (user.getLastPacketTime() < deadline) {
// Kick the user from all the rooms that he/she had previuosly joined
MUCRoom room;
Presence kickedPresence;
for (LocalMUCRole role : user.getRoles()) {
room = role.getChatRoom();
try {
kickedPresence = room.kickOccupant(user.getAddress(), null, null, null);
// Send the updated presence to the room occupants
room.send(kickedPresence);
} catch (NotAllowedException e) {
// Do nothing since we cannot kick owners or admins
}
}
}
} catch (Throwable e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
}
use of org.jivesoftware.openfire.muc.NotAllowedException in project Openfire by igniterealtime.
the class MultiUserChatServiceImpl method getChatRoom.
@Override
public MUCRoom getChatRoom(String roomName, JID userjid) throws NotAllowedException {
LocalMUCRoom room;
boolean loaded = false;
boolean created = false;
synchronized (roomName.intern()) {
room = localMUCRoomManager.getRoom(roomName);
if (room == null) {
room = new LocalMUCRoom(this, roomName, router);
// If the room is persistent load the configuration values from the DB
try {
// Try to load the room's configuration from the database (if the room is
// persistent but was added to the DB after the server was started up or the
// room may be an old room that was not present in memory)
MUCPersistenceManager.loadFromDB(room);
loaded = true;
} catch (IllegalArgumentException e) {
// (or was deleted somehow and is expected to exist by a delegate).
if (mucEventDelegate != null && mucEventDelegate.shouldRecreate(roomName, userjid)) {
if (mucEventDelegate.loadConfig(room)) {
loaded = true;
if (room.isPersistent()) {
MUCPersistenceManager.saveToDB(room);
}
} else {
// not allow room creation
throw new NotAllowedException();
}
} else {
// The room does not exist so check for creation permissions
// Room creation is always allowed for sysadmin
final JID bareJID = userjid.asBareJID();
if (isRoomCreationRestricted() && !sysadmins.includes(bareJID)) {
// The room creation is only allowed for certain JIDs
if (!allowedToCreate.includes(bareJID)) {
// an exception
throw new NotAllowedException();
}
}
room.addFirstOwner(userjid);
created = true;
}
}
localMUCRoomManager.addRoom(roomName, room);
}
}
if (created) {
// Fire event that a new room has been created
MUCEventDispatcher.roomCreated(room.getRole().getRoleAddress());
}
if (loaded || created) {
// Notify other cluster nodes that a new room is available
CacheFactory.doClusterTask(new RoomAvailableEvent(room));
for (MUCRole role : room.getOccupants()) {
if (role instanceof LocalMUCRole) {
CacheFactory.doClusterTask(new OccupantAddedEvent(room, role));
}
}
}
return room;
}
use of org.jivesoftware.openfire.muc.NotAllowedException in project Openfire by igniterealtime.
the class LocalMUCRoom method applyAffiliationChange.
/**
* Evaluate the given JID to determine what the appropriate affiliation should be
* after a change has been made. Each affected user will be granted the highest
* affiliation they now possess, either explicitly or implicitly via membership
* in one or more groups. If the JID is a user, the effective affiliation is
* applied to each presence corresponding to that user. If the given JID is a group,
* each user in the group is evaluated to determine what their new affiliations will
* be. The returned presence updates will be broadcast to the occupants of the room.
*
* @param senderRole Typically the room itself, or an owner/admin
* @param affiliationJID The JID for the user or group that has been changed
* @param reason An optional reason to explain why a user was kicked from the room
* @return List of presence updates to be delivered to the room's occupants
*/
private List<Presence> applyAffiliationChange(MUCRole senderRole, final JID affiliationJID, String reason) {
// Update the presence(s) for the new affiliation and inform all occupants
List<JID> affectedOccupants = new ArrayList<>();
// first, determine which actual (user) JIDs are affected by the affiliation change
if (GroupJID.isGroup(affiliationJID)) {
try {
Group group = GroupManager.getInstance().getGroup(affiliationJID);
// if so, calculate a new affiliation (if any) for the occupant(s)
for (JID groupMember : group.getAll()) {
if (occupantsByBareJID.containsKey(groupMember)) {
affectedOccupants.add(groupMember);
}
}
} catch (GroupNotFoundException gnfe) {
Log.error("Error updating group presences for " + affiliationJID, gnfe);
}
} else {
if (occupantsByBareJID.containsKey(affiliationJID)) {
affectedOccupants.add(affiliationJID);
}
}
// now update each of the affected occupants with a new role/affiliation
MUCRole.Role newRole;
MUCRole.Affiliation newAffiliation;
List<Presence> updatedPresences = new ArrayList<>();
// new role/affiliation may be granted via group membership
for (JID occupantJID : affectedOccupants) {
Log.info("Applying affiliation change for " + occupantJID);
boolean kickMember = false, isOutcast = false;
if (owners.includes(occupantJID)) {
newRole = MUCRole.Role.moderator;
newAffiliation = MUCRole.Affiliation.owner;
} else if (admins.includes(occupantJID)) {
newRole = MUCRole.Role.moderator;
newAffiliation = MUCRole.Affiliation.admin;
} else // outcast trumps member when an affiliation is changed
if (outcasts.includes(occupantJID)) {
newAffiliation = MUCRole.Affiliation.outcast;
newRole = MUCRole.Role.none;
kickMember = true;
isOutcast = true;
} else if (members.includesKey(occupantJID)) {
newRole = MUCRole.Role.participant;
newAffiliation = MUCRole.Affiliation.member;
} else if (isMembersOnly()) {
newRole = MUCRole.Role.none;
newAffiliation = MUCRole.Affiliation.none;
kickMember = true;
} else {
newRole = isModerated() ? MUCRole.Role.visitor : MUCRole.Role.participant;
newAffiliation = MUCRole.Affiliation.none;
}
Log.info("New affiliation: " + newAffiliation);
try {
List<Presence> thisOccupant = changeOccupantAffiliation(senderRole, occupantJID, newAffiliation, newRole);
if (kickMember) {
// a status code of 301 indicates the user was removed as an outcast
for (Presence presence : thisOccupant) {
presence.setType(Presence.Type.unavailable);
presence.setStatus(null);
Element x = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
if (reason != null && reason.trim().length() > 0) {
x.element("item").addElement("reason").setText(reason);
}
x.addElement("status").addAttribute("code", isOutcast ? "301" : "321");
kickPresence(presence, senderRole.getUserAddress(), senderRole.getNickname());
}
}
updatedPresences.addAll(thisOccupant);
} catch (NotAllowedException e) {
Log.error("Error updating presences for " + occupantJID, e);
}
}
return updatedPresences;
}
Aggregations