Commit 3acba09c authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Added ability to manually lock/unlock a room. JM-46


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@723 b35dd754-fafc-0310-a699-88a17e54d16e
parent 76ec6476
...@@ -381,6 +381,15 @@ public interface MUCRoom extends ChatDeliverer { ...@@ -381,6 +381,15 @@ public interface MUCRoom extends ChatDeliverer {
*/ */
public boolean isLocked(); public boolean isLocked();
/**
* Returns true if the room is locked and it was locked by a room owner after the room was
* initially configured.
*
* @return true if the room is locked and it was locked by a room owner after the room was
* initially configured.
*/
public boolean isManuallyLocked();
/** /**
* An event callback fired whenever an occupant changes his nickname within the chatroom. * An event callback fired whenever an occupant changes his nickname within the chatroom.
* *
...@@ -767,14 +776,24 @@ public interface MUCRoom extends ChatDeliverer { ...@@ -767,14 +776,24 @@ public interface MUCRoom extends ChatDeliverer {
*/ */
public boolean canBroadcastPresence(String roleToBroadcast); public boolean canBroadcastPresence(String roleToBroadcast);
/**
* Locks the room so that users cannot join the room. Only the owner of the room can lock/unlock
* the room.
*
* @param senderRole the role of the occupant that locked the room.
* @throws ForbiddenException If the user is not an owner of the room.
*/
public void lock(MUCRole senderRole) throws ForbiddenException;
/** /**
* Unlocks the room so that users can join the room. The room is locked when created and only * Unlocks the room so that users can join the room. The room is locked when created and only
* the owner of the room can unlock it by sending the configuration form to the Multi-User Chat * the owner of the room can unlock it by sending the configuration form to the Multi-User Chat
* service. * service.
* *
* @param senderRole the role of the occupant that unlocked the room. * @param senderRole the role of the occupant that unlocked the room.
* @throws ForbiddenException If the user is not an owner of the room.
*/ */
public void unlockRoom(MUCRole senderRole); public void unlock(MUCRole senderRole) throws ForbiddenException;
/** /**
* Sends an invitation to a user. The invitation will be sent as if the room is inviting the * Sends an invitation to a user. The invitation will be sent as if the room is inviting the
......
...@@ -309,8 +309,8 @@ public class IQOwnerHandler { ...@@ -309,8 +309,8 @@ public class IQOwnerHandler {
} }
// If the room was locked, unlock it and send to the owner the "room is now unlocked" // If the room was locked, unlock it and send to the owner the "room is now unlocked"
// message // message
if (room.isLocked()) { if (room.isLocked() && !room.isManuallyLocked()) {
room.unlockRoom(senderRole); room.unlock(senderRole);
} }
} }
} }
......
...@@ -41,7 +41,7 @@ public class MUCPersistenceManager { ...@@ -41,7 +41,7 @@ public class MUCPersistenceManager {
private static final String GET_RESERVED_NAME = private static final String GET_RESERVED_NAME =
"SELECT nickname FROM mucMember WHERE roomID=? AND jid=?"; "SELECT nickname FROM mucMember WHERE roomID=? AND jid=?";
private static final String LOAD_ROOM = private static final String LOAD_ROOM =
"SELECT roomID, creationDate, modificationDate, naturalName, description, " + "SELECT roomID, creationDate, modificationDate, naturalName, description, lockedDate, " +
"canChangeSubject, maxUsers, publicRoom, moderated, membersOnly, canInvite, " + "canChangeSubject, maxUsers, publicRoom, moderated, membersOnly, canInvite, " +
"password, canDiscoverJID, logEnabled, subject, rolesToBroadcast " + "password, canDiscoverJID, logEnabled, subject, rolesToBroadcast " +
"FROM mucRoom WHERE name=?"; "FROM mucRoom WHERE name=?";
...@@ -54,7 +54,7 @@ public class MUCPersistenceManager { ...@@ -54,7 +54,7 @@ public class MUCPersistenceManager {
"WHERE time>? AND roomID=? AND (nickname != \"\" OR subject IS NOT NULL) ORDER BY time"; "WHERE time>? AND roomID=? AND (nickname != \"\" OR subject IS NOT NULL) ORDER BY time";
private static final String LOAD_ALL_ROOMS = private static final String LOAD_ALL_ROOMS =
"SELECT roomID, creationDate, modificationDate, name, naturalName, description, " + "SELECT roomID, creationDate, modificationDate, name, naturalName, description, " +
"canChangeSubject, maxUsers, publicRoom, moderated, membersOnly, canInvite, " + "lockedDate, canChangeSubject, maxUsers, publicRoom, moderated, membersOnly, canInvite, " +
"password, canDiscoverJID, logEnabled, subject, rolesToBroadcast " + "password, canDiscoverJID, logEnabled, subject, rolesToBroadcast " +
"FROM mucRoom"; "FROM mucRoom";
private static final String LOAD_ALL_AFFILIATIONS = private static final String LOAD_ALL_AFFILIATIONS =
...@@ -71,11 +71,13 @@ public class MUCPersistenceManager { ...@@ -71,11 +71,13 @@ public class MUCPersistenceManager {
"WHERE roomID=?"; "WHERE roomID=?";
private static final String ADD_ROOM = private static final String ADD_ROOM =
"INSERT INTO mucRoom (roomID, creationDate, modificationDate, name, naturalName, " + "INSERT INTO mucRoom (roomID, creationDate, modificationDate, name, naturalName, " +
"description, canChangeSubject, maxUsers, publicRoom, moderated, membersOnly, " + "description, lockedDate, canChangeSubject, maxUsers, publicRoom, moderated, " +
"canInvite, password, canDiscoverJID, logEnabled, subject, rolesToBroadcast)" + "membersOnly, canInvite, password, canDiscoverJID, logEnabled, subject, rolesToBroadcast)" +
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
private static final String UPDATE_SUBJECT = private static final String UPDATE_SUBJECT =
"UPDATE mucRoom SET subject=? WHERE roomID=?"; "UPDATE mucRoom SET subject=? WHERE roomID=?";
private static final String UPDATE_LOCK =
"UPDATE mucRoom SET lockedDate=? WHERE roomID=?";
private static final String DELETE_ROOM = private static final String DELETE_ROOM =
"DELETE FROM mucRoom WHERE roomID=?"; "DELETE FROM mucRoom WHERE roomID=?";
private static final String DELETE_AFFILIATIONS = private static final String DELETE_AFFILIATIONS =
...@@ -139,7 +141,7 @@ public class MUCPersistenceManager { ...@@ -139,7 +141,7 @@ public class MUCPersistenceManager {
* *
* @param room the room to load from the database if persistent * @param room the room to load from the database if persistent
*/ */
public static void loadFromDB(MUCRoom room) { public static void loadFromDB(MUCRoomImpl room) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
...@@ -155,18 +157,19 @@ public class MUCPersistenceManager { ...@@ -155,18 +157,19 @@ public class MUCPersistenceManager {
room.setModificationDate(new Date(Long.parseLong(rs.getString(3).trim()))); // modification date room.setModificationDate(new Date(Long.parseLong(rs.getString(3).trim()))); // modification date
room.setNaturalLanguageName(rs.getString(4)); room.setNaturalLanguageName(rs.getString(4));
room.setDescription(rs.getString(5)); room.setDescription(rs.getString(5));
room.setCanOccupantsChangeSubject(rs.getInt(6) == 1 ? true : false); room.setLockedDate(new Date(Long.parseLong(rs.getString(6).trim())));
room.setMaxUsers(rs.getInt(7)); room.setCanOccupantsChangeSubject(rs.getInt(7) == 1 ? true : false);
room.setPublicRoom(rs.getInt(8) == 1 ? true : false); room.setMaxUsers(rs.getInt(8));
room.setModerated(rs.getInt(9) == 1 ? true : false); room.setPublicRoom(rs.getInt(9) == 1 ? true : false);
room.setMembersOnly(rs.getInt(10) == 1 ? true : false); room.setModerated(rs.getInt(10) == 1 ? true : false);
room.setCanOccupantsInvite(rs.getInt(11) == 1 ? true : false); room.setMembersOnly(rs.getInt(11) == 1 ? true : false);
room.setPassword(rs.getString(12)); room.setCanOccupantsInvite(rs.getInt(12) == 1 ? true : false);
room.setCanAnyoneDiscoverJID(rs.getInt(13) == 1 ? true : false); room.setPassword(rs.getString(13));
room.setLogEnabled(rs.getInt(14) == 1 ? true : false); room.setCanAnyoneDiscoverJID(rs.getInt(14) == 1 ? true : false);
room.setSubject(rs.getString(15)); room.setLogEnabled(rs.getInt(15) == 1 ? true : false);
room.setSubject(rs.getString(16));
List<String> rolesToBroadcast = new ArrayList<String>(); List<String> rolesToBroadcast = new ArrayList<String>();
String roles = Integer.toBinaryString(rs.getInt(16)); String roles = Integer.toBinaryString(rs.getInt(17));
if (roles.charAt(0) == '1') { if (roles.charAt(0) == '1') {
rolesToBroadcast.add("moderator"); rolesToBroadcast.add("moderator");
} }
...@@ -269,7 +272,7 @@ public class MUCPersistenceManager { ...@@ -269,7 +272,7 @@ public class MUCPersistenceManager {
* *
* @param room The room to save its configuration. * @param room The room to save its configuration.
*/ */
public static void saveToDB(MUCRoom room) { public static void saveToDB(MUCRoomImpl room) {
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
try { try {
...@@ -300,17 +303,18 @@ public class MUCPersistenceManager { ...@@ -300,17 +303,18 @@ public class MUCPersistenceManager {
pstmt.setString(4, room.getName()); pstmt.setString(4, room.getName());
pstmt.setString(5, room.getNaturalLanguageName()); pstmt.setString(5, room.getNaturalLanguageName());
pstmt.setString(6, room.getDescription()); pstmt.setString(6, room.getDescription());
pstmt.setInt(7, (room.canOccupantsChangeSubject() ? 1 : 0)); pstmt.setString(7, StringUtils.dateToMillis(room.getLockedDate()));
pstmt.setInt(8, room.getMaxUsers()); pstmt.setInt(8, (room.canOccupantsChangeSubject() ? 1 : 0));
pstmt.setInt(9, (room.isPublicRoom() ? 1 : 0)); pstmt.setInt(9, room.getMaxUsers());
pstmt.setInt(10, (room.isModerated() ? 1 : 0)); pstmt.setInt(10, (room.isPublicRoom() ? 1 : 0));
pstmt.setInt(11, (room.isMembersOnly() ? 1 : 0)); pstmt.setInt(11, (room.isModerated() ? 1 : 0));
pstmt.setInt(12, (room.canOccupantsInvite() ? 1 : 0)); pstmt.setInt(12, (room.isMembersOnly() ? 1 : 0));
pstmt.setString(13, room.getPassword()); pstmt.setInt(13, (room.canOccupantsInvite() ? 1 : 0));
pstmt.setInt(14, (room.canAnyoneDiscoverJID() ? 1 : 0)); pstmt.setString(14, room.getPassword());
pstmt.setInt(15, (room.isLogEnabled() ? 1 : 0)); pstmt.setInt(15, (room.canAnyoneDiscoverJID() ? 1 : 0));
pstmt.setString(16, room.getSubject()); pstmt.setInt(16, (room.isLogEnabled() ? 1 : 0));
pstmt.setInt(17, marshallRolesToBroadcast(room)); pstmt.setString(17, room.getSubject());
pstmt.setInt(18, marshallRolesToBroadcast(room));
pstmt.executeUpdate(); pstmt.executeUpdate();
} }
} }
...@@ -382,7 +386,7 @@ public class MUCPersistenceManager { ...@@ -382,7 +386,7 @@ public class MUCPersistenceManager {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_ALL_ROOMS); pstmt = con.prepareStatement(LOAD_ALL_ROOMS);
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();
MUCRoom room = null; MUCRoomImpl room = null;
while (rs.next()) { while (rs.next()) {
room = new MUCRoomImpl(chatserver, rs.getString(4), packetRouter); room = new MUCRoomImpl(chatserver, rs.getString(4), packetRouter);
room.setID(rs.getLong(1)); room.setID(rs.getLong(1));
...@@ -390,18 +394,19 @@ public class MUCPersistenceManager { ...@@ -390,18 +394,19 @@ public class MUCPersistenceManager {
room.setModificationDate(new Date(Long.parseLong(rs.getString(3).trim()))); // modification date room.setModificationDate(new Date(Long.parseLong(rs.getString(3).trim()))); // modification date
room.setNaturalLanguageName(rs.getString(5)); room.setNaturalLanguageName(rs.getString(5));
room.setDescription(rs.getString(6)); room.setDescription(rs.getString(6));
room.setCanOccupantsChangeSubject(rs.getInt(7) == 1 ? true : false); room.setLockedDate(new Date(Long.parseLong(rs.getString(7).trim())));
room.setMaxUsers(rs.getInt(8)); room.setCanOccupantsChangeSubject(rs.getInt(8) == 1 ? true : false);
room.setPublicRoom(rs.getInt(9) == 1 ? true : false); room.setMaxUsers(rs.getInt(9));
room.setModerated(rs.getInt(10) == 1 ? true : false); room.setPublicRoom(rs.getInt(10) == 1 ? true : false);
room.setMembersOnly(rs.getInt(11) == 1 ? true : false); room.setModerated(rs.getInt(11) == 1 ? true : false);
room.setCanOccupantsInvite(rs.getInt(12) == 1 ? true : false); room.setMembersOnly(rs.getInt(12) == 1 ? true : false);
room.setPassword(rs.getString(13)); room.setCanOccupantsInvite(rs.getInt(13) == 1 ? true : false);
room.setCanAnyoneDiscoverJID(rs.getInt(14) == 1 ? true : false); room.setPassword(rs.getString(14));
room.setLogEnabled(rs.getInt(15) == 1 ? true : false); room.setCanAnyoneDiscoverJID(rs.getInt(15) == 1 ? true : false);
room.setSubject(rs.getString(16)); room.setLogEnabled(rs.getInt(16) == 1 ? true : false);
room.setSubject(rs.getString(17));
List<String> rolesToBroadcast = new ArrayList<String>(); List<String> rolesToBroadcast = new ArrayList<String>();
String roles = Integer.toBinaryString(rs.getInt(17)); String roles = Integer.toBinaryString(rs.getInt(18));
if (roles.charAt(0) == '1') { if (roles.charAt(0) == '1') {
rolesToBroadcast.add("moderator"); rolesToBroadcast.add("moderator");
} }
...@@ -425,7 +430,7 @@ public class MUCPersistenceManager { ...@@ -425,7 +430,7 @@ public class MUCPersistenceManager {
// Load the rooms conversations from the last two days // Load the rooms conversations from the last two days
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
room = rooms.get(rs.getLong(1)); room = (MUCRoomImpl) rooms.get(rs.getLong(1));
// Skip to the next position if the room does not exist // Skip to the next position if the room does not exist
if (room == null) { if (room == null) {
continue; continue;
...@@ -463,7 +468,7 @@ public class MUCPersistenceManager { ...@@ -463,7 +468,7 @@ public class MUCPersistenceManager {
while (rs.next()) { while (rs.next()) {
String jid = rs.getString(2); String jid = rs.getString(2);
int affiliation = rs.getInt(3); int affiliation = rs.getInt(3);
room = rooms.get(rs.getLong(1)); room = (MUCRoomImpl) rooms.get(rs.getLong(1));
try { try {
switch (affiliation) { switch (affiliation) {
case MUCRole.OWNER: case MUCRole.OWNER:
...@@ -490,7 +495,7 @@ public class MUCPersistenceManager { ...@@ -490,7 +495,7 @@ public class MUCPersistenceManager {
pstmt = con.prepareStatement(LOAD_ALL_MEMBERS); pstmt = con.prepareStatement(LOAD_ALL_MEMBERS);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
room = rooms.get(rs.getLong(1)); room = (MUCRoomImpl) rooms.get(rs.getLong(1));
try { try {
room.addMember(rs.getString(2), rs.getString(3), room.getRole()); room.addMember(rs.getString(2), rs.getString(3), room.getRole());
} }
...@@ -523,9 +528,8 @@ public class MUCPersistenceManager { ...@@ -523,9 +528,8 @@ public class MUCPersistenceManager {
* Updates the room's subject in the database. * Updates the room's subject in the database.
* *
* @param room the room to update its subject in the database. * @param room the room to update its subject in the database.
* @param subject the new subject of the room.
*/ */
public static void updateRoomSubject(MUCRoom room, String subject) { public static void updateRoomSubject(MUCRoom room) {
if (!room.isPersistent() || !room.wasSavedToDB()) { if (!room.isPersistent() || !room.wasSavedToDB()) {
return; return;
} }
...@@ -535,7 +539,37 @@ public class MUCPersistenceManager { ...@@ -535,7 +539,37 @@ public class MUCPersistenceManager {
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_SUBJECT); pstmt = con.prepareStatement(UPDATE_SUBJECT);
pstmt.setString(1, subject); pstmt.setString(1, room.getSubject());
pstmt.setLong(2, room.getID());
pstmt.executeUpdate();
}
catch (SQLException sqle) {
Log.error(sqle);
}
finally {
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) { Log.error(e); }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
}
}
/**
* Updates the room's lock status in the database.
*
* @param room the room to update its lock status in the database.
*/
public static void updateRoomLock(MUCRoomImpl room) {
if (!room.isPersistent() || !room.wasSavedToDB()) {
return;
}
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_LOCK);
pstmt.setString(1, StringUtils.dateToMillis(room.getLockedDate()));
pstmt.setLong(2, room.getID()); pstmt.setLong(2, room.getID());
pstmt.executeUpdate(); pstmt.executeUpdate();
} }
......
...@@ -36,14 +36,6 @@ import org.dom4j.Element; ...@@ -36,14 +36,6 @@ import org.dom4j.Element;
*/ */
public class MUCRoomImpl implements MUCRoom { public class MUCRoomImpl implements MUCRoom {
/**
* The timeout period to unlock a room. If the period expired the default means that the default
* configuration was accepted for the room.
*/
// TODO Set this variable from a default configuration. Add setters and getters.
// Default value is 30 min ( 30(min) * 60(sec) * 1000(mill) )
public static long LOCK_TIMEOUT = 1800000;
/** /**
* The server hosting the room. * The server hosting the room.
*/ */
...@@ -108,14 +100,9 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -108,14 +100,9 @@ public class MUCRoomImpl implements MUCRoom {
private MUCRoomHistory roomHistory; private MUCRoomHistory roomHistory;
/** /**
* Flag that indicates whether a room is locked or not. * Time when the room was locked. A value of zero means that the room is unlocked.
*/
private boolean roomLocked;
/**
* The time when the room was locked.
*/ */
long lockedTime; private long lockedTime;
/** /**
* List of chatroom's owner. The list contains only bare jid. * List of chatroom's owner. The list contains only bare jid.
...@@ -273,7 +260,6 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -273,7 +260,6 @@ public class MUCRoomImpl implements MUCRoom {
this.iqOwnerHandler = new IQOwnerHandler(this, packetRouter); this.iqOwnerHandler = new IQOwnerHandler(this, packetRouter);
this.iqAdminHandler = new IQAdminHandler(this, packetRouter); this.iqAdminHandler = new IQAdminHandler(this, packetRouter);
// No one can join the room except the room's owner // No one can join the room except the room's owner
this.roomLocked = true;
this.lockedTime = startTime; this.lockedTime = startTime;
// Set the default roles for which presence is broadcast // Set the default roles for which presence is broadcast
rolesToBroadcastPresence.add("moderator"); rolesToBroadcastPresence.add("moderator");
...@@ -394,7 +380,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -394,7 +380,7 @@ public class MUCRoomImpl implements MUCRoom {
} }
boolean isOwner = owners.contains(user.getAddress().toBareJID()); boolean isOwner = owners.contains(user.getAddress().toBareJID());
// If the room is locked and this user is not an owner raise a RoomLocked exception // If the room is locked and this user is not an owner raise a RoomLocked exception
if (roomLocked) { if (isLocked()) {
if (!isOwner) { if (!isOwner) {
throw new RoomLockedException(); throw new RoomLockedException();
} }
...@@ -500,8 +486,8 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -500,8 +486,8 @@ public class MUCRoomImpl implements MUCRoom {
} }
if (joinRole != null) { if (joinRole != null) {
// It is assumed that the room is new based on the fact that it's locked and // It is assumed that the room is new based on the fact that it's locked and
// it has only one occupants (the owner). // that it was locked when it was created.
boolean isRoomNew = roomLocked && occupants.size() == 1; boolean isRoomNew = isLocked() && creationDate.getTime() == lockedTime;
try { try {
// Send the presence of this new occupant to existing occupants // Send the presence of this new occupant to existing occupants
Presence joinPresence = joinRole.getPresence().createCopy(); Presence joinPresence = joinRole.getPresence().createCopy();
...@@ -512,7 +498,6 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -512,7 +498,6 @@ public class MUCRoomImpl implements MUCRoom {
} }
joinPresence.setFrom(joinRole.getRoleAddress()); joinPresence.setFrom(joinRole.getRoleAddress());
broadcastPresence(joinPresence); broadcastPresence(joinPresence);
} }
catch (Exception e) { catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
...@@ -520,6 +505,15 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -520,6 +505,15 @@ public class MUCRoomImpl implements MUCRoom {
// If the room has just been created send the "room locked until configuration is // If the room has just been created send the "room locked until configuration is
// confirmed" message // confirmed" message
if (isRoomNew) { if (isRoomNew) {
Message message = new Message();
message.setType(Message.Type.groupchat);
message.setBody(LocaleUtils.getLocalizedString("muc.new"));
message.setFrom(role.getRoleAddress());
message.setTo(user.getAddress());
router.route(message);
}
else if (isLocked()) {
// Warn the owner that the room is locked but it's not new
Message message = new Message(); Message message = new Message();
message.setType(Message.Type.groupchat); message.setType(Message.Type.groupchat);
message.setBody(LocaleUtils.getLocalizedString("muc.locked")); message.setBody(LocaleUtils.getLocalizedString("muc.locked"));
...@@ -1231,11 +1225,11 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1231,11 +1225,11 @@ public class MUCRoomImpl implements MUCRoom {
} }
public boolean isLocked() { public boolean isLocked() {
if (System.currentTimeMillis() - startTime > LOCK_TIMEOUT) { return lockedTime > 0;
// Unlock the room. The default configuration is assumed to be accepted by the owner. }
roomLocked = false;
} public boolean isManuallyLocked() {
return roomLocked; return lockedTime > 0 && creationDate.getTime() != lockedTime;
} }
public void nicknameChanged(String oldNick, String newNick) { public void nicknameChanged(String oldNick, String newNick) {
...@@ -1254,7 +1248,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1254,7 +1248,7 @@ public class MUCRoomImpl implements MUCRoom {
} }
// Set the new subject to the room // Set the new subject to the room
subject = packet.getSubject(); subject = packet.getSubject();
MUCPersistenceManager.updateRoomSubject(this, subject); MUCPersistenceManager.updateRoomSubject(this);
// Notify all the occupants that the subject has changed // Notify all the occupants that the subject has changed
packet.setFrom(role.getRoleAddress()); packet.setFrom(role.getRoleAddress());
send(packet); send(packet);
...@@ -1578,11 +1572,6 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1578,11 +1572,6 @@ public class MUCRoomImpl implements MUCRoom {
public void setSavedToDB(boolean saved) { public void setSavedToDB(boolean saved) {
this.savedToDB = saved; this.savedToDB = saved;
if (saved) {
// Unlock the room now. This is necessary only when a persistent room has been
// retrieved from the database.
this.roomLocked = false;
}
} }
public void setPersistent(boolean persistent) { public void setPersistent(boolean persistent) {
...@@ -1609,7 +1598,7 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1609,7 +1598,7 @@ public class MUCRoomImpl implements MUCRoom {
/** /**
* Returns true if we need to check whether a presence could be sent or not. * Returns true if we need to check whether a presence could be sent or not.
* *
* @return true if we need to check whether a presence could be sent or not. * @return true if we need to check whether a presence could be sent or not.
*/ */
private boolean hasToCheckRoleToBroadcastPresence() { private boolean hasToCheckRoleToBroadcastPresence() {
...@@ -1621,9 +1610,35 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1621,9 +1610,35 @@ public class MUCRoomImpl implements MUCRoom {
return "none".equals(roleToBroadcast) || rolesToBroadcastPresence.contains(roleToBroadcast); return "none".equals(roleToBroadcast) || rolesToBroadcastPresence.contains(roleToBroadcast);
} }
public void unlockRoom(MUCRole senderRole) { public void lock(MUCRole senderRole) throws ForbiddenException {
roomLocked = false; if (MUCRole.OWNER != senderRole.getAffiliation()) {
this.lockedTime = 0; throw new ForbiddenException();
}
if (isLocked()) {
// Do nothing if the room was already locked
return;
}
setLocked(true);
if (senderRole.getChatUser() != null) {
// Send to the occupant that locked the room a message saying so
Message message = new Message();
message.setType(Message.Type.groupchat);
message.setBody(LocaleUtils.getLocalizedString("muc.locked"));
message.setFrom(getRole().getRoleAddress());
message.setTo(senderRole.getChatUser().getAddress());
router.route(message);
}
}
public void unlock(MUCRole senderRole) throws ForbiddenException {
if (MUCRole.OWNER != senderRole.getAffiliation()) {
throw new ForbiddenException();
}
if (!isLocked()) {
// Do nothing if the room was already unlocked
return;
}
setLocked(false);
if (senderRole.getChatUser() != null) { if (senderRole.getChatUser() != null) {
// Send to the occupant that unlocked the room a message saying so // Send to the occupant that unlocked the room a message saying so
Message message = new Message(); Message message = new Message();
...@@ -1635,6 +1650,40 @@ public class MUCRoomImpl implements MUCRoom { ...@@ -1635,6 +1650,40 @@ public class MUCRoomImpl implements MUCRoom {
} }
} }
private void setLocked(boolean locked) {
if (locked) {
this.lockedTime = System.currentTimeMillis();
}
else {
this.lockedTime = 0;
}
MUCPersistenceManager.updateRoomLock(this);
}
/**
* Sets the date when the room was locked. Initially when the room is created it is locked so
* the locked date is the creation date of the room. Afterwards, the room may be manually
* locked and unlocked so the locked date may be in these cases different than the creation
* date. A Date with time 0 means that the the room is unlocked.
*
* @param lockedTime the date when the room was locked.
*/
void setLockedDate(Date lockedTime) {
this.lockedTime = lockedTime.getTime();
}
/**
* Returns the date when the room was locked. Initially when the room is created it is locked so
* the locked date is the creation date of the room. Afterwards, the room may be manually
* locked and unlocked so the locked date may be in these cases different than the creation
* date. When the room is unlocked a Date with time 0 is returned.
*
* @return the date when the room was locked.
*/
Date getLockedDate() {
return new Date(lockedTime);
}
public List<Presence> addAdmins(List<String> newAdmins, MUCRole senderRole) public List<Presence> addAdmins(List<String> newAdmins, MUCRole senderRole)
throws ForbiddenException, ConflictException { throws ForbiddenException, ConflictException {
List<Presence> answer = new ArrayList<Presence>(newAdmins.size()); List<Presence> answer = new ArrayList<Presence>(newAdmins.size());
......
...@@ -356,8 +356,8 @@ public class MUCUserImpl implements MUCUser { ...@@ -356,8 +356,8 @@ public class MUCUserImpl implements MUCUser {
roles.put(group, role); roles.put(group, role);
// If the client that created the room is non-MUC compliant then // If the client that created the room is non-MUC compliant then
// unlock the room thus creating an "instant" room // unlock the room thus creating an "instant" room
if (room.isLocked() && mucInfo == null) { if (mucInfo == null && room.isLocked() && !room.isManuallyLocked()) {
room.unlockRoom(role); room.unlock(role);
} }
} }
catch (UnauthorizedException e) { catch (UnauthorizedException e) {
......
...@@ -276,7 +276,7 @@ public class MultiUserChatServerImpl extends BasicModule implements MultiUserCha ...@@ -276,7 +276,7 @@ public class MultiUserChatServerImpl extends BasicModule implements MultiUserCha
try { try {
// Try to load the room's configuration from the database (if the room is // 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) // persistent but was added to the DB after the server was started up)
MUCPersistenceManager.loadFromDB(room); MUCPersistenceManager.loadFromDB((MUCRoomImpl) room);
} }
catch (IllegalArgumentException e) { catch (IllegalArgumentException e) {
// The room does not exist so check for creation permissions // The room does not exist so check for creation permissions
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment