Java Code Examples for org.xmpp.packet.JID#asBareJID()

The following examples show how to use org.xmpp.packet.JID#asBareJID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiUserChatServiceImpl.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if a particular JID is allowed to create rooms.
 *
 * @param jid The jid for which to check (cannot be null).
 * @return true if the JID is allowed to create a room, otherwise false.
 */
private boolean isAllowedToCreate(final JID jid) {
    // If room creation is not restricted, everyone is allowed to create a room.
    if (!isRoomCreationRestricted()) {
        return true;
    }

    final JID bareJID = jid.asBareJID();

    // System administrators are always allowed to create rooms.
    if (sysadmins.includes(bareJID)) {
        return true;
    }

    // If the JID of the user has explicitly been given permission, room creation is allowed.
    if (allowedToCreate.includes(bareJID)) {
        return true;
    }

    // Verify the policy that allows all local, registered users to create rooms.
    return allRegisteredUsersAllowedToCreate && UserManager.getInstance().isRegisteredUser(bareJID);

}
 
Example 2
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public MUCRole.Affiliation getAffiliation(@Nonnull JID jid) {
    final JID bareJID = jid.asBareJID();

    if (owners.includes(bareJID)) {
        return MUCRole.Affiliation.owner;
    }
    else if (admins.includes(bareJID)) {
        return MUCRole.Affiliation.admin;
    }
    // explicit outcast status has higher precedence than member status
    else if (outcasts.includes(bareJID)) {
        return MUCRole.Affiliation.outcast;
    }
    else if (members.includesKey(bareJID)) {
        return MUCRole.Affiliation.member;
    }
    return MUCRole.Affiliation.none;
}
 
Example 3
Source File: Node.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the specified user is allowed to administer the node. Node
 * administrator are allowed to retrieve the node configuration, change the node
 * configuration, purge the node, delete the node and get the node affiliations and
 * subscriptions.
 *
 * @param user the user to check if he is an admin.
 * @return true if the specified user is allowed to administer the node.
 */
public boolean isAdmin(JID user) {
    if (getOwners().contains(user) || getService().isServiceAdmin(user)) {
        return true;
    }
    // Check if we should try again but using the bare JID
    if (user.getResource() != null) {
        user = user.asBareJID();
        return isAdmin(user);
    }
    return false;
}
 
Example 4
Source File: IQPEPHandler.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Process an IQ get stanza that was addressed to the service (rather than to a node/user).
 *
 * @param packet The stanza to process.
 * @return A response (can be null).
 */
private IQ handleIQGetToService(IQ packet) {
    final JID senderJID = packet.getFrom();
    final JID bareJidFrom = senderJID.asBareJID();
    packet = packet.createCopy();
    packet.setTo(bareJidFrom);

    final PEPService pepService = pepServiceManager.getPEPService(bareJidFrom);
    pepServiceManager.process(pepService, packet);
    return null;
}
 
Example 5
Source File: AdminManager.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Removes an account from the list of Admin accounts, based off JID.
 *
 * @param jid JID of user to remove from admin list.
 */
public void removeAdminAccount(JID jid) {
    if (adminList == null) {
        loadAdminList();
    }
    
    JID bareJID = jid.asBareJID();
    if (!adminList.contains(bareJID)) {
        return;
    }
    // Remove user from admin list cache.
    adminList.remove(bareJID);
    // Store updated list of admins with provider.
    provider.setAdmins(adminList);
}
 
Example 6
Source File: AdminManager.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a new account to the list of Admin accounts, based off a JID.
 *
 * @param jid JID of account to add to list of admins.
 */
public void addAdminAccount(JID jid) {
    if (adminList == null) {
        loadAdminList();
    }
    JID bareJID = jid.asBareJID();
    if (adminList.contains(bareJID)) {
        // Already have them.
        return;
    }
    // Add new admin to cache.
    adminList.add(bareJID);
    // Store updated list of admins with provider.
    provider.setAdmins(adminList);
}
 
Example 7
Source File: Group.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the provided JID belongs to a user that is part of the group.
 *
 * @param user the JID address of the user to check.
 * @return true if the specified user is a group user.
 */
public boolean isUser(JID user) {
    // Make sure that we are always checking bare JIDs
    if (user != null && user.getResource() != null) {
        user = user.asBareJID();
    }
    return user != null && (members.contains(user) || administrators.contains(user));
}
 
Example 8
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public List<Presence> addAdmins(List<JID> newAdmins, MUCRole senderRole)
        throws ForbiddenException, ConflictException {
    List<Presence> answer = new ArrayList<>(newAdmins.size());
    for (JID newAdmin : newAdmins) {
        final JID bareJID = newAdmin.asBareJID();
        if (!admins.contains(bareJID)) {
            answer.addAll(addAdmin(bareJID, senderRole));
        }
    }
    return answer;
}
 
Example 9
Source File: PEPServiceManager.java    From Openfire with Apache License 2.0 5 votes vote down vote up
public PEPService create(JID owner) {
    // Return an error if the packet is from an anonymous, unregistered user
    // or remote user
    if (!XMPPServer.getInstance().isLocal(owner)
            || !UserManager.getInstance().isRegisteredUser(owner.getNode())) {
        throw new IllegalArgumentException(
                "Request must be initiated by a local, registered user, but is not: "
                        + owner);
    }

    PEPService pepService = null;
    final JID bareJID = owner.asBareJID();
    final Lock lock = pepServices.getLock(bareJID);
    lock.lock();
    try {

        if (pepServices.get(bareJID) != null) {
            pepService = pepServices.get(bareJID).get();
        }

        if (pepService == null) {
            pepService = new PEPService(XMPPServer.getInstance(), bareJID);
            pepServices.put(bareJID, CacheableOptional.of(pepService));
            pepService.initialize();

            if (Log.isDebugEnabled()) {
                Log.debug("PEPService created for : " + bareJID);
            }
        }
    } finally {
        lock.unlock();
    }

    return pepService;
}
 
Example 10
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the role of the occupant from all the internal occupants collections. The role will
 * also be removed from the user's roles.
 *
 * @param leaveRole the role to remove.
 */
void removeOccupantRole(MUCRole leaveRole) {
    Log.trace( "Remove occupant from room {}: {}", this.getJID(), leaveRole );
    JID userAddress = leaveRole.getUserAddress();
    // Notify the user that he/she is no longer in the room
    leaveRole.destroy();
    // Update the tables of occupants based on the bare and full JID
    JID bareJID = userAddress.asBareJID();

    String nickname = leaveRole.getNickname();
    lock.writeLock().lock();
    try {
        occupantsByNickname.computeIfPresent(nickname.toLowerCase(), (n, occupants) -> {
            occupants.remove(leaveRole);
            return occupants.isEmpty() ? null : occupants;
        });

        occupantsByBareJID.computeIfPresent(bareJID,(jid, occupants) -> {
            occupants.remove(leaveRole);
            return occupants.isEmpty() ? null : occupants;
        });

        occupantsByFullJID.remove(userAddress);
    }
    finally {
        lock.writeLock().unlock();
    }
}
 
Example 11
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public String getReservedNickname(JID jid) {
    final JID bareJID = jid.asBareJID();
    String answer = members.get(bareJID);
    if (answer == null || answer.trim().length() == 0) {
        return null;
    }
    return answer;
}
 
Example 12
Source File: MultiUserChatServiceImpl.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public void removeSysadmin(final JID userJID) {
    final JID bareJID = userJID.asBareJID();

    sysadmins.remove(bareJID);

    // Update the config.
    final String[] jids = new String[sysadmins.size()];
    for (int i = 0; i < jids.length; i++) {
        jids[i] = sysadmins.get(i).toBareJID();
    }
    MUCPersistenceManager.setProperty(chatServiceName, "sysadmin.jid", fromArray(jids));
}
 
Example 13
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 4 votes vote down vote up
@Override
public List<Presence> addOwner(JID jid, MUCRole sendRole) throws ForbiddenException {
    
    final JID bareJID = jid.asBareJID();
    lock.writeLock().lock();
    try {
        MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
        if (MUCRole.Affiliation.owner != sendRole.getAffiliation()) {
            throw new ForbiddenException();
        }
        // Check if user is already an owner (explicitly)
        if (owners.contains(bareJID)) {
            // Do nothing
            return Collections.emptyList();
        }
        owners.add(bareJID);
        // Remove the user from other affiliation lists
        if (removeAdmin(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.admin;
        }
        else if (removeMember(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.member;
        }
        else if (removeOutcast(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.outcast;
        }
        // Update the DB if the room is persistent
        MUCPersistenceManager.saveAffiliationToDB(
            this,
            bareJID,
            null,
            MUCRole.Affiliation.owner,
            oldAffiliation);
    }
    finally {
        lock.writeLock().unlock();
    }
    // Update other cluster nodes with new affiliation
    CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.owner));

    // apply the affiliation change, assigning a new affiliation
    // based on the group(s) of the affected user(s)
    return applyAffiliationChange(getRole(), bareJID, null);
}
 
Example 14
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 4 votes vote down vote up
@Override
public List<Presence> addAdmin(JID jid, MUCRole sendRole) throws ForbiddenException,
        ConflictException {
    final JID bareJID = jid.asBareJID();
    lock.writeLock().lock();
    try {
        MUCRole.Affiliation oldAffiliation = MUCRole.Affiliation.none;
        if (MUCRole.Affiliation.owner != sendRole.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 admin
        if (admins.contains(bareJID)) {
            // Do nothing
            return Collections.emptyList();
        }
        admins.add(bareJID);
        // Remove the user from other affiliation lists
        if (removeOwner(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.owner;
        }
        else if (removeMember(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.member;
        }
        else if (removeOutcast(bareJID)) {
            oldAffiliation = MUCRole.Affiliation.outcast;
        }
        // Update the DB if the room is persistent
        MUCPersistenceManager.saveAffiliationToDB(
            this,
            bareJID,
            null,
            MUCRole.Affiliation.admin,
            oldAffiliation);
    }
    finally {
        lock.writeLock().unlock();
    }
    // Update other cluster nodes with new affiliation
    CacheFactory.doClusterTask(new AddAffiliation(this, jid.toBareJID(), MUCRole.Affiliation.admin));
    
    // apply the affiliation change, assigning a new affiliation
    // based on the group(s) of the affected user(s)
    return applyAffiliationChange(getRole(), bareJID, null);
}
 
Example 15
Source File: PEPService.java    From Openfire with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a PEPService.
 * 
 * @param server  the XMPP server.
 * @param bareJID the bare JID (service ID) of the user owning the service.
 */
public PEPService(XMPPServer server, JID bareJID) {
    this.serviceOwner = bareJID.asBareJID();
    router = server.getPacketRouter();

    // Initialize the ad-hoc commands manager to use for this pep service
    adHocCommandManager = new AdHocCommandManager();
    adHocCommandManager.addCommand(new PendingSubscriptionsCommand(this));

    // Load default configuration for leaf nodes
    leafDefaultConfiguration = PubSubPersistenceProviderManager.getInstance().getProvider().loadDefaultConfiguration(this.getUniqueIdentifier(), true);
    if (leafDefaultConfiguration == null) {
        // Create and save default configuration for leaf nodes;
        leafDefaultConfiguration = new DefaultNodeConfiguration(true);
        leafDefaultConfiguration.setAccessModel(AccessModel.presence);
        leafDefaultConfiguration.setPublisherModel(PublisherModel.publishers);
        leafDefaultConfiguration.setDeliverPayloads(true);
        leafDefaultConfiguration.setLanguage("English");
        leafDefaultConfiguration.setMaxPayloadSize(5120);
        leafDefaultConfiguration.setNotifyConfigChanges(true);
        leafDefaultConfiguration.setNotifyDelete(true);
        leafDefaultConfiguration.setNotifyRetract(true);
        leafDefaultConfiguration.setPersistPublishedItems(false);
        leafDefaultConfiguration.setMaxPublishedItems(1);
        leafDefaultConfiguration.setPresenceBasedDelivery(false);
        leafDefaultConfiguration.setSendItemSubscribe(true);
        leafDefaultConfiguration.setSubscriptionEnabled(true);
        leafDefaultConfiguration.setReplyPolicy(null);
        PubSubPersistenceProviderManager.getInstance().getProvider().createDefaultConfiguration(this.getUniqueIdentifier(), leafDefaultConfiguration);
    }
    // Load default configuration for collection nodes
    collectionDefaultConfiguration = PubSubPersistenceProviderManager.getInstance().getProvider().loadDefaultConfiguration(this.getUniqueIdentifier(), false);
    if (collectionDefaultConfiguration == null) {
        // Create and save default configuration for collection nodes;
        collectionDefaultConfiguration = new DefaultNodeConfiguration(false);
        collectionDefaultConfiguration.setAccessModel(AccessModel.presence);
        collectionDefaultConfiguration.setPublisherModel(PublisherModel.publishers);
        collectionDefaultConfiguration.setDeliverPayloads(false);
        collectionDefaultConfiguration.setLanguage("English");
        collectionDefaultConfiguration.setNotifyConfigChanges(true);
        collectionDefaultConfiguration.setNotifyDelete(true);
        collectionDefaultConfiguration.setNotifyRetract(true);
        collectionDefaultConfiguration.setPresenceBasedDelivery(false);
        collectionDefaultConfiguration.setSubscriptionEnabled(true);
        collectionDefaultConfiguration.setReplyPolicy(null);
        collectionDefaultConfiguration.setAssociationPolicy(CollectionNode.LeafNodeAssociationPolicy.all);
        collectionDefaultConfiguration.setMaxLeafNodes(-1);
        PubSubPersistenceProviderManager.getInstance().getProvider().createDefaultConfiguration(this.getUniqueIdentifier(), collectionDefaultConfiguration);
    }
}
 
Example 16
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 4 votes vote down vote up
@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));
    
    // apply the affiliation change, assigning a new affiliation
    // based on the group(s) of the affected user(s)
    return applyAffiliationChange(senderRole, bareJID, reason);
}
 
Example 17
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 4 votes vote down vote up
@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) {
        // apply the affiliation change, assigning a new affiliation
        // based on the group(s) of the affected user(s)
        return applyAffiliationChange(senderRole, bareJID, null);
    } else {
        // no presence updates needed
        return Collections.emptyList();
    }
}
 
Example 18
Source File: AddAffiliation.java    From Openfire with Apache License 2.0 4 votes vote down vote up
public AddAffiliation(LocalMUCRoom room, JID bareJID, MUCRole.Affiliation affiliation) {
    super(room);
    this.bareJID = bareJID.asBareJID();
    this.affiliation = affiliation;
}
 
Example 19
Source File: AddMember.java    From Openfire with Apache License 2.0 4 votes vote down vote up
public AddMember(LocalMUCRoom room, JID bareJID, String nickname) {
    super(room);
    this.bareJID = bareJID.asBareJID();
    this.nickname = nickname;
}
 
Example 20
Source File: PEPServiceManager.java    From Openfire with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieves a PEP service -- attempting first from memory, then from the
 * database.
 *
 * This method can automatically create a PEP service if one does not exist.
 *
 * @param jid
 *            the JID of the user that owns the PEP service.
 * @param autoCreate
 *            true if a PEP service that does not yet exist needs to be created.
 * @return the requested PEP service if found or null if not found.
 */
public PEPService getPEPService( JID jid, boolean autoCreate ) {
    jid = jid.asBareJID();
    PEPService pepService;

    final Lock lock = pepServices.getLock(jid);
    lock.lock();
    try {
        if (pepServices.containsKey(jid)) {
            // lookup in cache
            if ( pepServices.get(jid).isAbsent() && autoCreate ) {
                // needs auto-create despite negative cache.
                pepService = null;
            } else {
                return pepServices.get(jid).get();
            }
        } else {
            // lookup in database.
            pepService = PubSubPersistenceProviderManager.getInstance().getProvider().loadPEPServiceFromDB(jid);
            pepServices.put(jid, CacheableOptional.of(pepService));
            if ( pepService != null ) {
                pepService.initialize();
            }
        }

        if ( pepService != null ) {
            Log.debug("PEP: Restored service for {} from the database.", jid);
            pubSubEngine.start(pepService);
        } else if (autoCreate) {
            Log.debug("PEP: Auto-created service for {}.", jid);
            pepService = this.create(jid);

            // Probe presences
            pubSubEngine.start(pepService);

            // Those who already have presence subscriptions to jidFrom
            // will now automatically be subscribed to this new
            // PEPService.
            XMPPServer.getInstance().getIQPEPHandler().addSubscriptionForRosterItems( pepService );
        }
    } finally {
        lock.unlock();
    }

    return pepService;
}