Search in sources :

Example 16 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class TransportBuddy method sendPresence.

/**
     * Sends the current presence to the session user.
     *
     * @param to JID to send presence updates to.
     */
public void sendPresence(JID to) {
    // TODO: Should figure out best way to handle unknown here.
    Presence p = new Presence();
    p.setTo(to);
    p.setFrom(jid);
    getManager().getSession().getTransport().setUpPresencePacket(p, presence);
    if (verboseStatus != null && verboseStatus.length() > 0) {
        p.setStatus(verboseStatus);
    }
    if (avatarSet && avatar != null) {
        Element vcard = p.addChildElement("x", NameSpace.VCARD_TEMP_X_UPDATE);
        vcard.addElement("photo").addCDATA(avatar.getXmppHash());
        vcard.addElement("hash").addCDATA(avatar.getXmppHash());
    }
    getManager().sendPacket(p);
}
Also used : Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence)

Example 17 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class InternalComponentManager method checkPresences.

private void checkPresences() {
    for (JID prober : presenceMap.keySet()) {
        JID probee = presenceMap.get(prober);
        if (routingTable.hasComponentRoute(probee)) {
            Presence presence = new Presence();
            presence.setFrom(prober);
            presence.setTo(probee);
            routingTable.routePacket(probee, presence, false);
            // No reason to hold onto prober reference.
            presenceMap.remove(prober);
        }
    }
}
Also used : JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence)

Example 18 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class LocalMUCRoom method presenceUpdated.

/**
     * Handles occupants updating their presence in the chatroom. Assumes the user updates their presence whenever their
     * availability in the room changes. This method should not be called to handle other presence related updates, such
     * as nickname changes.
     * {@inheritDoc}
     */
@Override
public void presenceUpdated(final MUCRole occupantRole, final Presence newPresence) {
    final String occupantNickName = occupantRole.getNickname();
    // Update the presence of the occupant on the local node with the occupant's new availability. Updates the
    // local node first so the remote nodes receive presence that correctly reflects the occupant's new
    // availability and previously existing role and affiliation with the room.
    final UpdatePresence localUpdateRequest = new UpdatePresence(this, newPresence.createCopy(), occupantNickName);
    localUpdateRequest.setOriginator(true);
    localUpdateRequest.run();
    // Get the new, updated presence for the occupant in the room. The presence reflects the occupant's updated
    // availability and their existing association.
    final Presence updatedPresence = occupantRole.getPresence().createCopy();
    // Ask other cluster nodes to update the presence of the occupant. Uses the updated presence from the local
    // MUC role.
    final UpdatePresence clusterUpdateRequest = new UpdatePresence(this, updatedPresence, occupantNickName);
    CacheFactory.doClusterTask(clusterUpdateRequest);
    // Broadcast updated presence of occupant.
    broadcastPresence(updatedPresence, false);
}
Also used : UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Example 19 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class LocalMUCRoom method leaveRoom.

@Override
public void leaveRoom(MUCRole leaveRole) {
    if (leaveRole.isLocal()) {
        // Ask other cluster nodes to remove occupant from room
        OccupantLeftEvent event = new OccupantLeftEvent(this, leaveRole);
        CacheFactory.doClusterTask(event);
    }
    try {
        Presence originalPresence = leaveRole.getPresence();
        Presence presence = originalPresence.createCopy();
        presence.setType(Presence.Type.unavailable);
        presence.setStatus(null);
        // Change (or add) presence information about roles and affiliations
        Element childElement = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
        if (childElement == null) {
            childElement = presence.addChildElement("x", "http://jabber.org/protocol/muc#user");
        }
        Element item = childElement.element("item");
        if (item == null) {
            item = childElement.addElement("item");
        }
        item.addAttribute("role", "none");
        // set the role to "none" above, which is always broadcast.
        if (!shouldBroadcastPresence(originalPresence)) {
            // Inform the leaving user that he/she has left the room
            leaveRole.send(presence);
        } else {
            if (getOccupantsByNickname(leaveRole.getNickname()).size() <= 1) {
                // Inform the rest of the room occupants that the user has left the room
                broadcastPresence(presence, false);
            }
        }
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    }
    // Remove occupant from room and destroy room if empty and not persistent
    OccupantLeftEvent event = new OccupantLeftEvent(this, leaveRole);
    event.setOriginator(true);
    event.run();
}
Also used : OccupantLeftEvent(org.jivesoftware.openfire.muc.cluster.OccupantLeftEvent) Element(org.dom4j.Element) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence) ForbiddenException(org.jivesoftware.openfire.muc.ForbiddenException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException) RoomLockedException(org.jivesoftware.openfire.muc.RoomLockedException) CannotBeInvitedException(org.jivesoftware.openfire.muc.CannotBeInvitedException) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) NotFoundException(org.jivesoftware.util.NotFoundException) ConflictException(org.jivesoftware.openfire.muc.ConflictException) RegistrationRequiredException(org.jivesoftware.openfire.muc.RegistrationRequiredException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) IOException(java.io.IOException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) NotAcceptableException(org.jivesoftware.openfire.muc.NotAcceptableException) ServiceUnavailableException(org.jivesoftware.openfire.muc.ServiceUnavailableException)

Example 20 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class LocalMUCRoom method changeOccupantAffiliation.

/**
     * Updates all the presences of the given user with the new affiliation and role information. Do
     * nothing if the given jid is not present in the room. If the user has joined the room from
     * several client resources, all his/her occupants' presences will be updated.
     *
     * @param jid the bare jid of the user to update his/her role.
     * @param newAffiliation the new affiliation for the JID.
     * @param newRole the new role for the JID.
     * @return the list of updated presences of all the client resources that the client used to
     *         join the room.
     * @throws NotAllowedException If trying to change the moderator role to an owner or an admin or
     *         if trying to ban an owner or an administrator.
     */
private List<Presence> changeOccupantAffiliation(MUCRole senderRole, JID jid, MUCRole.Affiliation newAffiliation, MUCRole.Role newRole) throws NotAllowedException {
    List<Presence> presences = new ArrayList<>();
    // Get all the roles (i.e. occupants) of this user based on his/her bare JID
    JID bareJID = jid.asBareJID();
    List<MUCRole> roles = occupantsByBareJID.get(bareJID);
    if (roles == null) {
        return presences;
    }
    // Collect all the updated presences of these roles
    for (MUCRole role : roles) {
        // Update the presence with the new affiliation and role
        if (role.isLocal()) {
            role.setAffiliation(newAffiliation);
            role.setRole(newRole);
            // Notify the other cluster nodes to update the occupant
            CacheFactory.doClusterTask(new UpdateOccupant(this, role));
            // Prepare a new presence to be sent to all the room occupants
            presences.add(role.getPresence().createCopy());
        } else {
            // Ask the cluster node hosting the occupant to make the changes. Note that if the change
            // is not allowed a NotAllowedException will be thrown
            Element element = (Element) CacheFactory.doSynchronousClusterTask(new UpdateOccupantRequest(this, role.getNickname(), newAffiliation, newRole), role.getNodeID().toByteArray());
            if (element != null) {
                // Prepare a new presence to be sent to all the room occupants
                presences.add(new Presence(element, true));
            } else {
                throw new NotAllowedException();
            }
        }
    }
    // Answer all the updated presences
    return presences;
}
Also used : UpdateOccupant(org.jivesoftware.openfire.muc.cluster.UpdateOccupant) MUCRole(org.jivesoftware.openfire.muc.MUCRole) GroupJID(org.jivesoftware.openfire.group.GroupJID) JID(org.xmpp.packet.JID) NotAllowedException(org.jivesoftware.openfire.muc.NotAllowedException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) UpdateOccupantRequest(org.jivesoftware.openfire.muc.cluster.UpdateOccupantRequest) Presence(org.xmpp.packet.Presence) UpdatePresence(org.jivesoftware.openfire.muc.cluster.UpdatePresence)

Aggregations

Presence (org.xmpp.packet.Presence)109 JID (org.xmpp.packet.JID)38 Element (org.dom4j.Element)34 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)20 Message (org.xmpp.packet.Message)17 IQ (org.xmpp.packet.IQ)16 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)14 NotFoundException (org.jivesoftware.util.NotFoundException)12 ArrayList (java.util.ArrayList)11 MUCRole (org.jivesoftware.openfire.muc.MUCRole)10 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)9 DefaultElement (org.dom4j.tree.DefaultElement)8 IOException (java.io.IOException)7 SQLException (java.sql.SQLException)7 GroupJID (org.jivesoftware.openfire.group.GroupJID)7 NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)7 Date (java.util.Date)6 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)6 ConflictException (org.jivesoftware.openfire.muc.ConflictException)6 ForbiddenException (org.jivesoftware.openfire.muc.ForbiddenException)6