コード例 #1
0
  /**
   * Processes the completed form sent by an owner of the room. This will modify the room's
   * configuration as well as the list of owners and admins.
   *
   * @param completedForm the completed form sent by an owner of the room.
   * @param senderRole the role of the user that sent the completed form.
   * @throws ForbiddenException if the user does not have enough privileges.
   * @throws ConflictException If the room was going to lose all of its owners.
   */
  private void processConfigurationForm(DataForm completedForm, MUCRole senderRole)
      throws ForbiddenException, ConflictException {
    List<String> values;
    String booleanValue;
    FormField field;

    // Get the new list of admins
    field = completedForm.getField("muc#roomconfig_roomadmins");
    boolean adminsSent = field != null;
    List<JID> admins = new ArrayList<JID>();
    if (field != null) {
      for (String value : field.getValues()) {
        // XEP-0045: "Affiliations are granted, revoked, and
        // maintained based on the user's bare JID, (...)"
        if (value != null && value.trim().length() != 0) {
          admins.add(new JID(value.trim()).asBareJID());
        }
      }
    }

    // Get the new list of owners
    field = completedForm.getField("muc#roomconfig_roomowners");
    boolean ownersSent = field != null;
    List<JID> owners = new ArrayList<JID>();
    if (field != null) {
      for (String value : field.getValues()) {
        // XEP-0045: "Affiliations are granted, revoked, and
        // maintained based on the user's bare JID, (...)"
        if (value != null && value.trim().length() != 0) {
          owners.add(new JID(value.trim()).asBareJID());
        }
      }
    }

    // Answer a conflic error if all the current owners will be removed
    if (ownersSent && owners.isEmpty()) {
      throw new ConflictException();
    }

    // Keep a registry of the updated presences
    List<Presence> presences = new ArrayList<Presence>(admins.size() + owners.size());

    field = completedForm.getField("muc#roomconfig_roomname");
    if (field != null) {
      final String value = field.getFirstValue();
      room.setNaturalLanguageName((value != null ? value : " "));
    }

    field = completedForm.getField("muc#roomconfig_roomdesc");
    if (field != null) {
      final String value = field.getFirstValue();
      room.setDescription((value != null ? value : " "));
    }

    field = completedForm.getField("muc#roomconfig_changesubject");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setCanOccupantsChangeSubject(("1".equals(booleanValue)));
    }

    field = completedForm.getField("muc#roomconfig_maxusers");
    if (field != null) {
      final String value = field.getFirstValue();
      room.setMaxUsers((value != null ? Integer.parseInt(value) : 30));
    }

    field = completedForm.getField("muc#roomconfig_presencebroadcast");
    if (field != null) {
      values = new ArrayList<String>(field.getValues());
      room.setRolesToBroadcastPresence(values);
    }

    field = completedForm.getField("muc#roomconfig_publicroom");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setPublicRoom(("1".equals(booleanValue)));
    }

    field = completedForm.getField("muc#roomconfig_persistentroom");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      boolean isPersistent = ("1".equals(booleanValue));
      // Delete the room from the DB if it's no longer persistent
      if (room.isPersistent() && !isPersistent) {
        MUCPersistenceManager.deleteFromDB(room);
      }
      room.setPersistent(isPersistent);
    }

    field = completedForm.getField("muc#roomconfig_moderatedroom");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setModerated(("1".equals(booleanValue)));
    }

    field = completedForm.getField("muc#roomconfig_membersonly");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      presences.addAll(room.setMembersOnly(("1".equals(booleanValue))));
    }

    field = completedForm.getField("muc#roomconfig_allowinvites");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setCanOccupantsInvite(("1".equals(booleanValue)));
    }

    field = completedForm.getField("muc#roomconfig_passwordprotectedroom");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      boolean isPasswordProtected = "1".equals(booleanValue);
      if (isPasswordProtected) {
        // The room is password protected so set the new password
        field = completedForm.getField("muc#roomconfig_roomsecret");
        if (field != null) {
          final String secret = completedForm.getField("muc#roomconfig_roomsecret").getFirstValue();
          room.setPassword(secret);
        }
      } else {
        // The room is not password protected so remove any previous password
        room.setPassword(null);
      }
    }

    field = completedForm.getField("muc#roomconfig_whois");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setCanAnyoneDiscoverJID(("anyone".equals(booleanValue)));
    }

    field = completedForm.getField("muc#roomconfig_enablelogging");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setLogEnabled(("1".equals(booleanValue)));
    }

    field = completedForm.getField("x-muc#roomconfig_reservednick");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setLoginRestrictedToNickname(("1".equals(booleanValue)));
    }

    field = completedForm.getField("x-muc#roomconfig_canchangenick");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setChangeNickname(("1".equals(booleanValue)));
    }

    field = completedForm.getField("x-muc#roomconfig_registration");
    if (field != null) {
      final String value = field.getFirstValue();
      booleanValue = ((value != null ? value : "1"));
      room.setRegistrationEnabled(("1".equals(booleanValue)));
    }

    // Update the modification date to reflect the last time when the room's configuration
    // was modified
    room.setModificationDate(new Date());

    if (room.isPersistent()) {
      room.saveToDB();
    }

    // Set the new owners and admins of the room
    presences.addAll(room.addOwners(owners, senderRole));
    presences.addAll(room.addAdmins(admins, senderRole));

    if (ownersSent) {
      // Change the affiliation to "member" for the current owners that won't be neither
      // owner nor admin (if the form included the owners field)
      List<JID> ownersToRemove = new ArrayList<JID>(room.owners);
      ownersToRemove.removeAll(admins);
      ownersToRemove.removeAll(owners);
      for (JID jid : ownersToRemove) {
        presences.addAll(room.addMember(jid, null, senderRole));
      }
    }

    if (adminsSent) {
      // Change the affiliation to "member" for the current admins that won't be neither
      // owner nor admin (if the form included the admins field)
      List<JID> adminsToRemove = new ArrayList<JID>(room.admins);
      adminsToRemove.removeAll(admins);
      adminsToRemove.removeAll(owners);
      for (JID jid : adminsToRemove) {
        presences.addAll(room.addMember(jid, null, senderRole));
      }
    }

    // Destroy the room if the room is no longer persistent and there are no occupants in
    // the room
    if (!room.isPersistent() && room.getOccupantsCount() == 0) {
      room.destroyRoom(null, null);
    }

    // Send the updated presences to the room occupants
    for (Object presence : presences) {
      room.send((Presence) presence);
    }
  }