Search in sources :

Example 16 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class MUCParserUtils method parseItem.

public static MUCItem parseItem(XmlPullParser parser) throws XmlPullParserException, IOException {
    int initialDepth = parser.getDepth();
    MUCAffiliation affiliation = MUCAffiliation.fromString(parser.getAttributeValue("", "affiliation"));
    Resourcepart nick = ParserUtils.getResourcepartAttribute(parser, "nick");
    MUCRole role = MUCRole.fromString(parser.getAttributeValue("", "role"));
    Jid jid = ParserUtils.getJidAttribute(parser);
    Jid actor = null;
    Resourcepart actorNick = null;
    String reason = null;
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String name = parser.getName();
                switch(name) {
                    case "actor":
                        actor = ParserUtils.getJidAttribute(parser);
                        // TODO change to
                        // actorNick = Resourcepart.from(parser.getAttributeValue("", "nick"));
                        // once a newer version of JXMPP is used that supports from(null).
                        String actorNickString = parser.getAttributeValue("", "nick");
                        if (actorNickString != null) {
                            actorNick = Resourcepart.from(actorNickString);
                        }
                        break;
                    case "reason":
                        reason = parser.nextText();
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return new MUCItem(affiliation, role, actor, reason, jid, nick, actorNick);
}
Also used : MUCItem(org.jivesoftware.smackx.muc.packet.MUCItem) MUCAffiliation(org.jivesoftware.smackx.muc.MUCAffiliation) MUCRole(org.jivesoftware.smackx.muc.MUCRole) Jid(org.jxmpp.jid.Jid) EntityBareJid(org.jxmpp.jid.EntityBareJid) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 17 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class Roster method getAllPresences.

/**
 * Returns a List of Presence objects for all of a user's current presences if no presence information is available,
 * such as when you are not subscribed to the user's presence updates.
 *
 * @param bareJid an XMPP ID, e.g. jdoe@example.com.
 * @return a List of Presence objects for all the user's current presences, or an unavailable presence if no
 *         presence information is available.
 */
public List<Presence> getAllPresences(BareJid bareJid) {
    Map<Resourcepart, Presence> userPresences = getPresencesInternal(bareJid);
    List<Presence> res;
    if (userPresences == null) {
        // Create an unavailable presence if none was found
        Presence unavailable = synthesizeUnvailablePresence(bareJid);
        res = new ArrayList<>(Arrays.asList(unavailable));
    } else {
        res = new ArrayList<>(userPresences.values().size());
        for (Presence presence : userPresences.values()) {
            res.add(presence);
        }
    }
    return res;
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 18 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class Roster method getPresenceResource.

/**
 * Returns the presence info for a particular user's resource, or unavailable presence
 * if the user is offline or if no presence information is available, such as
 * when you are not subscribed to the user's presence updates.
 *
 * @param userWithResource a fully qualified XMPP ID including a resource (user@domain/resource).
 * @return the user's current presence, or unavailable presence if the user is offline
 *         or if no presence information is available.
 */
public Presence getPresenceResource(FullJid userWithResource) {
    BareJid key = userWithResource.asBareJid();
    Resourcepart resource = userWithResource.getResourcepart();
    Map<Resourcepart, Presence> userPresences = getPresencesInternal(key);
    if (userPresences == null) {
        Presence presence = synthesizeUnvailablePresence(userWithResource);
        return presence;
    } else {
        Presence presence = userPresences.get(resource);
        if (presence == null) {
            presence = synthesizeUnvailablePresence(userWithResource);
            return presence;
        } else {
            return presence;
        }
    }
}
Also used : EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 19 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class Roster method getPresence.

/**
 * Returns the presence info for a particular user. If the user is offline, or
 * if no presence data is available (such as when you are not subscribed to the
 * user's presence updates), unavailable presence will be returned.
 *
 * If the user has several presences (one for each resource), then the presence with
 * highest priority will be returned. If multiple presences have the same priority,
 * the one with the "most available" presence mode will be returned. In order,
 * that's {@link org.jivesoftware.smack.packet.Presence.Mode#chat free to chat},
 * {@link org.jivesoftware.smack.packet.Presence.Mode#available available},
 * {@link org.jivesoftware.smack.packet.Presence.Mode#away away},
 * {@link org.jivesoftware.smack.packet.Presence.Mode#xa extended away}, and
 * {@link org.jivesoftware.smack.packet.Presence.Mode#dnd do not disturb}.
 *
 * <p>
 * Note that presence information is received asynchronously. So, just after logging
 * in to the server, presence values for users in the roster may be unavailable
 * even if they are actually online. In other words, the value returned by this
 * method should only be treated as a snapshot in time, and may not accurately reflect
 * other user's presence instant by instant. If you need to track presence over time,
 * such as when showing a visual representation of the roster, consider using a
 * {@link RosterListener}.
 * </p>
 *
 * @param jid the XMPP address of the user (eg "jsmith@example.com"). The
 *             address must be a bare JID e.g. "domain/resource" or
 *             "user@domain".
 * @return the user's current presence, or unavailable presence if the user is offline
 *         or if no presence information is available..
 */
public Presence getPresence(BareJid jid) {
    Map<Resourcepart, Presence> userPresences = getPresencesInternal(jid);
    if (userPresences == null) {
        Presence presence = synthesizeUnvailablePresence(jid);
        return presence;
    } else {
        // Find the resource with the highest priority
        // Might be changed to use the resource with the highest availability instead.
        Presence presence = null;
        // This is used in case no available presence is found
        Presence unavailable = null;
        for (Presence p : userPresences.values()) {
            if (!p.isAvailable()) {
                unavailable = p;
                continue;
            }
            // Chose presence with highest priority first.
            if (presence == null || p.getPriority() > presence.getPriority()) {
                presence = p;
            } else // If equal priority, choose "most available" by the mode value.
            if (p.getPriority() == presence.getPriority()) {
                Presence.Mode pMode = p.getMode();
                // Default to presence mode of available.
                if (pMode == null) {
                    pMode = Presence.Mode.available;
                }
                Presence.Mode presenceMode = presence.getMode();
                // Default to presence mode of available.
                if (presenceMode == null) {
                    presenceMode = Presence.Mode.available;
                }
                if (pMode.compareTo(presenceMode) < 0) {
                    presence = p;
                }
            }
        }
        if (presence == null) {
            if (unavailable != null) {
                return unavailable;
            } else {
                presence = synthesizeUnvailablePresence(jid);
                return presence;
            }
        } else {
            return presence;
        }
    }
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 20 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project Smack by igniterealtime.

the class MultiUserChatRolesAffiliationsPrivilegesIntegrationTest method mucRoleTestForRemovingModerator.

/**
 * Asserts that a user who undergoes a role change receives that change as a presence update
 *
 * <p>From XEP-0045 § 5.1.3:</p>
 * <blockquote>
 * ...a MUC service implementation MUST change the occupant's role to reflect the change and communicate the change
 * to all occupants...
 * </blockquote>
 *
 * <p>From XEP-0045 § 9.7:</p>
 * <blockquote>
 * The service MUST then send updated presence from this individual to all occupants, indicating the removal of
 * moderator status...
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucRoleTestForRemovingModerator() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByTwo = mucManagerTwo.getMultiUserChat(mucAddress);
    final ResultSyncPoint<String, Exception> resultSyncPoint = new ResultSyncPoint<>();
    mucAsSeenByTwo.addUserStatusListener(new UserStatusListener() {

        @Override
        public void moderatorRevoked() {
            resultSyncPoint.signal("done");
        }
    });
    createMuc(mucAsSeenByOne, "one-" + randomString);
    try {
        final Resourcepart nicknameTwo = Resourcepart.from("two-" + randomString);
        mucAsSeenByTwo.join(nicknameTwo);
        mucAsSeenByOne.grantModerator(nicknameTwo);
        mucAsSeenByOne.revokeModerator(nicknameTwo);
        resultSyncPoint.waitForResult(timeout);
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
}
Also used : ResultSyncPoint(org.igniterealtime.smack.inttest.util.ResultSyncPoint) EntityBareJid(org.jxmpp.jid.EntityBareJid) SmackException(org.jivesoftware.smack.SmackException) TestNotPossibleException(org.igniterealtime.smack.inttest.TestNotPossibleException) XMPPException(org.jivesoftware.smack.XMPPException) Resourcepart(org.jxmpp.jid.parts.Resourcepart) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Aggregations

Resourcepart (org.jxmpp.jid.parts.Resourcepart)54 EntityBareJid (org.jxmpp.jid.EntityBareJid)24 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)20 XMPPException (org.jivesoftware.smack.XMPPException)19 TestNotPossibleException (org.igniterealtime.smack.inttest.TestNotPossibleException)17 SmackException (org.jivesoftware.smack.SmackException)16 Presence (org.jivesoftware.smack.packet.Presence)16 ResultSyncPoint (org.igniterealtime.smack.inttest.util.ResultSyncPoint)15 EntityFullJid (org.jxmpp.jid.EntityFullJid)13 UserJid (com.xabber.android.data.entity.UserJid)11 Jid (org.jxmpp.jid.Jid)9 AccountJid (com.xabber.android.data.entity.AccountJid)8 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)8 DomainBareJid (org.jxmpp.jid.DomainBareJid)7 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)6 AccountItem (com.xabber.android.data.account.AccountItem)4 Localpart (org.jxmpp.jid.parts.Localpart)4 Attachment (com.xabber.android.data.database.messagerealm.Attachment)3 ForwardId (com.xabber.android.data.database.messagerealm.ForwardId)3 ArrayList (java.util.ArrayList)3