Search in sources :

Example 36 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project xabber-android by redsolution.

the class RoomChat method onPacket.

@Override
protected boolean onPacket(UserJid bareAddress, Stanza stanza, boolean isCarbons) {
    if (!super.onPacket(bareAddress, stanza, isCarbons)) {
        return false;
    }
    // MUCUser mucUserExtension = MUCUser.from(stanza);
    // if (mucUserExtension != null && mucUserExtension.getInvite() != null) {
    // return false;
    // }
    final org.jxmpp.jid.Jid from = stanza.getFrom();
    final Resourcepart resource = from.getResourceOrNull();
    if (stanza instanceof Message) {
        final Message message = (Message) stanza;
        if (message.getType() == Message.Type.error) {
            UserJid invite = invites.remove(message.getStanzaId());
            if (invite != null) {
                newAction(nickname, invite.toString(), ChatAction.invite_error, true);
            }
            return true;
        }
        MUCUser mucUser = MUCUser.from(stanza);
        if (mucUser != null && mucUser.getDecline() != null) {
            onInvitationDeclined(mucUser.getDecline().getFrom(), mucUser.getDecline().getReason());
            return true;
        }
        if (mucUser != null && mucUser.getStatus() != null && mucUser.getStatus().contains(MUCUser.Status.create("100")) && ChatManager.getInstance().isSuppress100(account, user)) {
            // 'This room is not anonymous'
            return true;
        }
        String text = message.getBody();
        final String subject = message.getSubject();
        if (text == null && subject == null) {
            return true;
        }
        if (subject != null) {
            if (this.subject.equals(subject)) {
                return true;
            }
            this.subject = subject;
            RosterManager.onContactChanged(account, bareAddress);
            newAction(resource, subject, ChatAction.subject, true);
        } else {
            boolean notify = true;
            Date delay = getDelayStamp(message);
            if (delay != null) {
                notify = false;
            }
            // forward comment (to support previous forwarded xep)
            String forwardComment = ForwardManager.parseForwardComment(stanza);
            if (forwardComment != null)
                text = forwardComment;
            // modify body with references
            Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
            text = bodies.first;
            String markupText = bodies.second;
            String originalFrom = stanza.getFrom().toString();
            String messageUId = getMessageIdIfInHistory(getStanzaId(message), text);
            if (messageUId != null) {
                if (isSelf(resource)) {
                    markMessageAsDelivered(messageUId, originalFrom);
                }
                return true;
            }
            if (isSelf(resource)) {
                notify = false;
            }
            updateThreadId(message.getThread());
            RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(stanza);
            String uid = UUID.randomUUID().toString();
            RealmList<ForwardId> forwardIds = parseForwardedMessage(true, stanza, uid);
            String originalStanza = stanza.toXML().toString();
            // create message with file-attachments
            if (attachments.size() > 0)
                createAndSaveFileMessage(true, uid, resource, text, markupText, null, null, delay, true, notify, false, false, getStanzaId(message), attachments, originalStanza, null, originalFrom, true, false, null);
            else
                // create message without attachments
                createAndSaveNewMessage(true, uid, resource, text, markupText, null, null, delay, true, notify, false, false, getStanzaId(message), originalStanza, null, originalFrom, forwardIds, true, false, null);
            EventBus.getDefault().post(new NewIncomingMessageEvent(account, user));
        }
    } else if (stanza instanceof Presence) {
        Presence presence = (Presence) stanza;
        if (presence.getType() == Presence.Type.available) {
            Occupant oldOccupant = occupants.get(resource);
            Occupant newOccupant = createOccupant(resource, presence);
            newOccupant.setJid(from);
            occupants.put(resource, newOccupant);
            if (oldOccupant == null) {
                onAvailable(resource);
                RosterManager.onContactChanged(account, user);
            } else {
                boolean changed = false;
                if (oldOccupant.getAffiliation() != newOccupant.getAffiliation()) {
                    changed = true;
                    onAffiliationChanged(resource, newOccupant.getAffiliation());
                }
                if (oldOccupant.getRole() != newOccupant.getRole()) {
                    changed = true;
                    onRoleChanged(resource, newOccupant.getRole());
                }
                if (oldOccupant.getStatusMode() != newOccupant.getStatusMode() || !oldOccupant.getStatusText().equals(newOccupant.getStatusText())) {
                    changed = true;
                    onStatusChanged(resource, newOccupant.getStatusMode(), newOccupant.getStatusText());
                }
                if (changed) {
                    RosterManager.onContactChanged(account, user);
                }
            }
        } else if (presence.getType() == Presence.Type.unavailable && state == RoomState.available) {
            occupants.remove(resource);
            MUCUser mucUser = MUCUser.from(presence);
            if (mucUser != null && mucUser.getStatus() != null) {
                if (mucUser.getStatus().contains(MUCUser.Status.KICKED_307)) {
                    onKick(resource, mucUser.getItem().getActor());
                } else if (mucUser.getStatus().contains(MUCUser.Status.BANNED_301)) {
                    onBan(resource, mucUser.getItem().getActor());
                } else if (mucUser.getStatus().contains(MUCUser.Status.NEW_NICKNAME_303)) {
                    Resourcepart newNick = mucUser.getItem().getNick();
                    if (newNick == null) {
                        return true;
                    }
                    onRename(resource, newNick);
                    Occupant occupant = createOccupant(newNick, presence);
                    occupants.put(newNick, occupant);
                } else if (mucUser.getStatus().contains(MUCUser.Status.REMOVED_AFFIL_CHANGE_321)) {
                    onRevoke(resource, mucUser.getItem().getActor());
                }
            } else {
                onLeave(resource);
            }
            RosterManager.onContactChanged(account, user);
        }
    }
    return true;
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) NewIncomingMessageEvent(com.xabber.android.data.message.NewIncomingMessageEvent) Message(org.jivesoftware.smack.packet.Message) UserJid(com.xabber.android.data.entity.UserJid) Attachment(com.xabber.android.data.database.messagerealm.Attachment) Date(java.util.Date) Resourcepart(org.jxmpp.jid.parts.Resourcepart) Presence(org.jivesoftware.smack.packet.Presence) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId)

Example 37 with Resourcepart

use of org.jxmpp.jid.parts.Resourcepart in project xabber-android by redsolution.

the class MUCManager method joinRoom.

/**
 * Requests to join to the room.
 *
 * @param requested Whether user request to join the room.
 */
public void joinRoom(final AccountJid account, final EntityBareJid room, boolean requested) {
    final RoomChat roomChat;
    final Resourcepart nickname;
    final String password;
    roomChat = getRoomChat(account, room);
    if (roomChat == null) {
        Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
        return;
    }
    RoomState state = roomChat.getState();
    if (state == RoomState.available || state == RoomState.occupation) {
        Application.getInstance().onError(R.string.ALREADY_JOINED);
        return;
    }
    if (state == RoomState.creating || state == RoomState.joining) {
        Application.getInstance().onError(R.string.ALREADY_IN_PROGRESS);
        return;
    }
    nickname = roomChat.getNickname();
    password = roomChat.getPassword();
    requestToWriteRoom(account, room, nickname, password, true);
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        return;
    }
    final MultiUserChat multiUserChat;
    try {
        multiUserChat = MultiUserChatManager.getInstanceFor(accountItem.getConnection()).getMultiUserChat(room);
    } catch (IllegalStateException e) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    roomChat.setState(RoomState.joining);
    roomChat.setMultiUserChat(multiUserChat);
    roomChat.setRequested(requested);
    Application.getInstance().runInBackgroundUserRequest(new Runnable() {

        @Override
        public void run() {
            try {
                if (roomChat.getMultiUserChat() != multiUserChat) {
                    return;
                }
                multiUserChat.join(nickname, password);
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        if (roomChat.getState() == RoomState.joining) {
                            roomChat.setState(RoomState.occupation);
                        }
                        removeAuthorizationError(account, room);
                        try {
                            RosterManager.onContactChanged(account, UserJid.from(room));
                            VCardManager.getInstance().request(account, room);
                        } catch (UserJid.UserJidCreateException e) {
                            LogManager.exception(this, e);
                        }
                    }
                });
                return;
            } catch (final XMPPException.XMPPErrorException e) {
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        roomChat.setState(RoomState.error);
                        addAuthorizationError(account, room);
                        XMPPError xmppError = e.getXMPPError();
                        if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.conflict) {
                            Application.getInstance().onError(R.string.NICK_ALREADY_USED);
                        } else if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.not_authorized) {
                            Application.getInstance().onError(R.string.AUTHENTICATION_FAILED);
                        } else {
                            Application.getInstance().onError(R.string.NOT_CONNECTED);
                        }
                        try {
                            RosterManager.onContactChanged(account, UserJid.from(room));
                        } catch (UserJid.UserJidCreateException e) {
                            LogManager.exception(this, e);
                        }
                    }
                });
                return;
            } catch (Exception e) {
                LogManager.exception(this, e);
            }
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (roomChat.getMultiUserChat() != multiUserChat) {
                        return;
                    }
                    roomChat.setState(RoomState.waiting);
                    Application.getInstance().onError(R.string.NOT_CONNECTED);
                    try {
                        RosterManager.onContactChanged(account, UserJid.from(room));
                    } catch (UserJid.UserJidCreateException e) {
                        LogManager.exception(this, e);
                    }
                }
            });
        }
    });
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) AccountItem(com.xabber.android.data.account.AccountItem) UserJid(com.xabber.android.data.entity.UserJid) XMPPError(org.jivesoftware.smack.packet.XMPPError) SmackException(org.jivesoftware.smack.SmackException) MultiUserChatException(org.jivesoftware.smackx.muc.MultiUserChatException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NetworkException(com.xabber.android.data.NetworkException) XMPPException(org.jivesoftware.smack.XMPPException) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 38 with Resourcepart

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

the class MultiUserChatEntityIntegrationTest method mucTestForRejectingDiscoOnRoomOccupantByNonOccupant.

/**
 * Asserts that a non-occupant receives a Bad Request error when attempting to query an occupant by their
 * occupant JID.
 *
 * <p>From XEP-0045 § 6.6:</p>
 * <blockquote>
 * If a non-occupant attempts to send a disco request to an address of the form &lt;room@service/nick&gt;, a MUC service
 * MUST return a &lt;bad-request/&gt; error
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucTestForRejectingDiscoOnRoomOccupantByNonOccupant() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest-discoitems");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    final Resourcepart nicknameOne = Resourcepart.from("one-" + randomString);
    createMuc(mucAsSeenByOne, nicknameOne);
    final EntityFullJid mucAsSeenByOneUserJid = mucAsSeenByOne.getMyRoomJid();
    // be non-null after we created and joined the room. But it can not hurt to explicitly test for it either.
    if (mucAsSeenByOneUserJid == null) {
        throw new AssertionError();
    }
    XMPPException.XMPPErrorException xe;
    try {
        xe = assertThrows(XMPPException.XMPPErrorException.class, () -> ServiceDiscoveryManager.getInstanceFor(conTwo).discoverItems(mucAsSeenByOneUserJid));
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
    final StanzaError.Condition expectedCondition;
    switch(sinttestConfiguration.compatibilityMode) {
        default:
            expectedCondition = StanzaError.Condition.bad_request;
            break;
        case ejabberd:
            expectedCondition = StanzaError.Condition.not_acceptable;
            break;
    }
    assertEquals(xe.getStanzaError().getCondition(), expectedCondition);
}
Also used : EntityFullJid(org.jxmpp.jid.EntityFullJid) XMPPException(org.jivesoftware.smack.XMPPException) EntityBareJid(org.jxmpp.jid.EntityBareJid) Resourcepart(org.jxmpp.jid.parts.Resourcepart) StanzaError(org.jivesoftware.smack.packet.StanzaError) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 39 with Resourcepart

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

the class MultiUserChatLowLevelIntegrationTest method testMucBookmarksAutojoin.

@SmackIntegrationTest
public void testMucBookmarksAutojoin(AbstractXMPPConnection connection) throws InterruptedException, TestNotPossibleException, XMPPException, SmackException, IOException {
    final BookmarkManager bookmarkManager = BookmarkManager.getBookmarkManager(connection);
    if (!bookmarkManager.isSupported()) {
        throw new TestNotPossibleException("Private data storage not supported");
    }
    final MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);
    final Resourcepart mucNickname = Resourcepart.from("Nick-" + StringUtils.randomString(6));
    final String randomMucName = StringUtils.randomString(6);
    final DomainBareJid mucComponent = multiUserChatManager.getMucServiceDomains().get(0);
    final MultiUserChat muc = multiUserChatManager.getMultiUserChat(JidCreate.entityBareFrom(Localpart.from(randomMucName), mucComponent));
    MucCreateConfigFormHandle handle = muc.createOrJoin(mucNickname);
    if (handle != null) {
        handle.makeInstant();
    }
    muc.leave();
    bookmarkManager.addBookmarkedConference("Smack Inttest: " + testRunId, muc.getRoom(), true, mucNickname, null);
    connection.disconnect();
    connection.connect().login();
    // MucBookmarkAutojoinManager is also able to do its task automatically
    // after every login, it's not deterministic when this will be finished.
    // So we trigger it manually here.
    MucBookmarkAutojoinManager.getInstanceFor(connection).autojoinBookmarkedConferences();
    assertTrue(muc.isJoined());
    // If the test went well, leave the MUC
    muc.leave();
}
Also used : TestNotPossibleException(org.igniterealtime.smack.inttest.TestNotPossibleException) DomainBareJid(org.jxmpp.jid.DomainBareJid) MucCreateConfigFormHandle(org.jivesoftware.smackx.muc.MultiUserChat.MucCreateConfigFormHandle) BookmarkManager(org.jivesoftware.smackx.bookmarks.BookmarkManager) Resourcepart(org.jxmpp.jid.parts.Resourcepart) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 40 with Resourcepart

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

the class MultiUserChatRolesAffiliationsPrivilegesIntegrationTest method mucTestDefaultRoleForAffiliationInUnmoderatedRoom.

/**
 * Asserts that an unmoderated room assigns the correct default roles for a given affiliation
 *
 * <p>From XEP-0045 § 5.1.2:</p>
 * <blockquote>
 * ...the initial default roles that a service SHOULD set based on the user's affiliation...
 * </blockquote>
 *
 * @throws Exception when errors occur
 */
@SmackIntegrationTest
public void mucTestDefaultRoleForAffiliationInUnmoderatedRoom() throws Exception {
    EntityBareJid mucAddress = getRandomRoom("smack-inttest-unmoderatedroles");
    MultiUserChat mucAsSeenByOne = mucManagerOne.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByTwo = mucManagerTwo.getMultiUserChat(mucAddress);
    MultiUserChat mucAsSeenByThree = mucManagerThree.getMultiUserChat(mucAddress);
    final Resourcepart nicknameOne = Resourcepart.from("one-" + randomString);
    final Resourcepart nicknameTwo = Resourcepart.from("two-" + randomString);
    final Resourcepart nicknameThree = Resourcepart.from("three-" + randomString);
    createMuc(mucAsSeenByOne, nicknameOne);
    try {
        mucAsSeenByTwo.join(nicknameTwo);
        mucAsSeenByThree.join(nicknameThree);
        final ResultSyncPoint<String, Exception> resultSyncPoint = new ResultSyncPoint<>();
        mucAsSeenByOne.addParticipantStatusListener(new ParticipantStatusListener() {

            @Override
            public void adminGranted(EntityFullJid participant) {
                resultSyncPoint.signal("done");
            }
        });
        mucAsSeenByOne.grantAdmin(conTwo.getUser().asBareJid());
        resultSyncPoint.waitForResult(timeout);
        assertEquals(mucAsSeenByOne.getOccupantsCount(), 3);
        assertEquals(MUCRole.moderator, mucAsSeenByOne.getOccupant(JidCreate.entityFullFrom(mucAddress, nicknameOne)).getRole());
        assertEquals(MUCRole.moderator, mucAsSeenByOne.getOccupant(JidCreate.entityFullFrom(mucAddress, nicknameTwo)).getRole());
        assertEquals(MUCRole.participant, mucAsSeenByOne.getOccupant(JidCreate.entityFullFrom(mucAddress, nicknameThree)).getRole());
    } finally {
        tryDestroy(mucAsSeenByOne);
    }
}
Also used : EntityFullJid(org.jxmpp.jid.EntityFullJid) 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