コード例 #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);
    }
  }
コード例 #2
0
  private void refreshConfigurationFormValues() {
    room.lock.readLock().lock();
    try {
      FormField field = configurationForm.getField("muc#roomconfig_roomname");
      field.clearValues();
      field.addValue(room.getNaturalLanguageName());

      field = configurationForm.getField("muc#roomconfig_roomdesc");
      field.clearValues();
      field.addValue(room.getDescription());

      field = configurationForm.getField("muc#roomconfig_changesubject");
      field.clearValues();
      field.addValue((room.canOccupantsChangeSubject() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_maxusers");
      field.clearValues();
      field.addValue(Integer.toString(room.getMaxUsers()));

      field = configurationForm.getField("muc#roomconfig_presencebroadcast");
      field.clearValues();
      for (String roleToBroadcast : room.getRolesToBroadcastPresence()) {
        field.addValue(roleToBroadcast);
      }

      field = configurationForm.getField("muc#roomconfig_publicroom");
      field.clearValues();
      field.addValue((room.isPublicRoom() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_persistentroom");
      field.clearValues();
      field.addValue((room.isPersistent() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_moderatedroom");
      field.clearValues();
      field.addValue((room.isModerated() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_membersonly");
      field.clearValues();
      field.addValue((room.isMembersOnly() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_allowinvites");
      field.clearValues();
      field.addValue((room.canOccupantsInvite() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_passwordprotectedroom");
      field.clearValues();
      field.addValue((room.isPasswordProtected() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_roomsecret");
      field.clearValues();
      field.addValue(room.getPassword());

      field = configurationForm.getField("muc#roomconfig_whois");
      field.clearValues();
      field.addValue((room.canAnyoneDiscoverJID() ? "anyone" : "moderators"));

      field = configurationForm.getField("muc#roomconfig_enablelogging");
      field.clearValues();
      field.addValue((room.isLogEnabled() ? "1" : "0"));

      field = configurationForm.getField("x-muc#roomconfig_reservednick");
      field.clearValues();
      field.addValue((room.isLoginRestrictedToNickname() ? "1" : "0"));

      field = configurationForm.getField("x-muc#roomconfig_canchangenick");
      field.clearValues();
      field.addValue((room.canChangeNickname() ? "1" : "0"));

      field = configurationForm.getField("x-muc#roomconfig_registration");
      field.clearValues();
      field.addValue((room.isRegistrationEnabled() ? "1" : "0"));

      field = configurationForm.getField("muc#roomconfig_roomadmins");
      field.clearValues();
      for (JID jid : room.getAdmins()) {
        field.addValue(jid.toString());
      }

      field = configurationForm.getField("muc#roomconfig_roomowners");
      field.clearValues();
      for (JID jid : room.getOwners()) {
        field.addValue(jid.toString());
      }

      // Remove the old element
      probeResult.remove(probeResult.element(QName.get("x", "jabber:x:data")));
      // Add the new representation of configurationForm as an element
      probeResult.add(configurationForm.getElement());

    } finally {
      room.lock.readLock().unlock();
    }
  }
コード例 #3
0
  void configure(DataForm options) {
    List<String> values;
    String booleanValue;

    boolean wasUsingPresence = !presenceStates.isEmpty();

    // Remove this field from the form
    options.removeField("FORM_TYPE");
    // Process and remove specific collection node fields
    FormField collectionField = options.getField("pubsub#subscription_type");
    if (collectionField != null) {
      values = collectionField.getValues();
      if (values.size() > 0) {
        type = Type.valueOf(values.get(0));
      }
      options.removeField("pubsub#subscription_type");
    }
    collectionField = options.getField("pubsub#subscription_depth");
    if (collectionField != null) {
      values = collectionField.getValues();
      depth = "all".equals(values.get(0)) ? 0 : 1;
      options.removeField("pubsub#subscription_depth");
    }
    // If there are more fields in the form then process them and set that
    // the subscription has been configured
    for (FormField field : options.getFields()) {
      boolean fieldExists = true;
      if ("pubsub#deliver".equals(field.getVariable())) {
        values = field.getValues();
        booleanValue = (values.size() > 0 ? values.get(0) : "1");
        deliverNotifications = "1".equals(booleanValue);
      } else if ("pubsub#digest".equals(field.getVariable())) {
        values = field.getValues();
        booleanValue = (values.size() > 0 ? values.get(0) : "1");
        usingDigest = "1".equals(booleanValue);
      } else if ("pubsub#digest_frequency".equals(field.getVariable())) {
        values = field.getValues();
        digestFrequency = values.size() > 0 ? Integer.parseInt(values.get(0)) : 86400000;
      } else if ("pubsub#expire".equals(field.getVariable())) {
        values = field.getValues();
        synchronized (dateFormat) {
          try {
            expire = dateFormat.parse(values.get(0));
          } catch (ParseException e) {
            Log.error("Error parsing date", e);
          }
        }
      } else if ("pubsub#include_body".equals(field.getVariable())) {
        values = field.getValues();
        booleanValue = (values.size() > 0 ? values.get(0) : "1");
        includingBody = "1".equals(booleanValue);
      } else if ("pubsub#show-values".equals(field.getVariable())) {
        // Get the new list of presence states for which an entity wants to
        // receive notifications
        presenceStates = new ArrayList<String>();
        for (String value : field.getValues()) {
          try {
            presenceStates.add(value);
          } catch (Exception e) {
            // Do nothing
          }
        }
      } else if ("x-pubsub#keywords".equals(field.getVariable())) {
        values = field.getValues();
        keyword = values.isEmpty() ? null : values.get(0);
      } else {
        fieldExists = false;
      }
      if (fieldExists) {
        // Subscription has been configured so set the next state
        if (node.getAccessModel().isAuthorizationRequired() && !node.isAdmin(owner)) {
          state = State.pending;
        } else {
          state = State.subscribed;
        }
      }
    }
    if (savedToDB) {
      // Update the subscription in the backend store
      PubSubPersistenceManager.saveSubscription(service, node, this, false);
    }
    // Check if the service needs to subscribe or unsubscribe from the owner presence
    if (!node.isPresenceBasedDelivery() && wasUsingPresence != !presenceStates.isEmpty()) {
      if (presenceStates.isEmpty()) {
        service.presenceSubscriptionNotRequired(node, owner);
      } else {
        service.presenceSubscriptionRequired(node, owner);
      }
    }
  }