Search in sources :

Example 91 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class SSNManager method onFormReceived.

private void onFormReceived(AccountJid account, Jid from, String session, Feature feature) {
    OtrMode otrMode = getOtrMode(account, session);
    boolean cancel = false;
    Collection<DisclosureValue> disclosureValues = feature.getDisclosureOptions();
    DisclosureValue disclosureValue = DisclosureValue.never;
    if (disclosureValues == null)
        disclosureValue = null;
    else if (!disclosureValues.contains(disclosureValue))
        cancel = true;
    Collection<SecurityValue> securityValues = feature.getSecurityOptions();
    SecurityValue securityValue;
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem != null && accountItem.getConnectionSettings().getTlsMode() == TLSMode.required) {
        securityValue = SecurityValue.c2s;
    } else {
        securityValue = SecurityValue.none;
    }
    if (securityValues == null)
        securityValue = null;
    else if (!securityValues.contains(securityValue))
        cancel = true;
    Collection<LoggingValue> loggingValues = feature.getLoggingOptions();
    LoggingValue loggingValue;
    if (loggingValues == null)
        loggingValue = null;
    else {
        loggingValue = otrMode.selectLoggingValue(loggingValues);
        if (loggingValue == null)
            cancel = true;
    }
    if (cancel) {
        DataForm dataForm = Feature.createDataForm(DataForm.Type.submit);
        if (feature.getAcceptValue() != null) {
            Feature.addAcceptField(dataForm, false);
            sessionStates.remove(account.toString(), session);
        } else {
            Feature.addRenegotiateField(dataForm, false);
        }
        sendFeature(account, from, session, new Feature(dataForm));
        return;
    }
    DataForm dataForm = Feature.createDataForm(DataForm.Type.submit);
    if (feature.getAcceptValue() != null)
        Feature.addAcceptField(dataForm, true);
    else
        Feature.addRenegotiateField(dataForm, true);
    if (disclosureValue != null)
        Feature.addDisclosureField(dataForm, null, disclosureValue);
    if (securityValue != null)
        Feature.addSecurityField(dataForm, null, securityValue);
    if (loggingValue != null) {
        Feature.addLoggingField(dataForm, null, loggingValue);
    }
    sessionStates.put(account.toString(), session, SessionState.active);
    sendFeature(account, from, session, new Feature(dataForm));
}
Also used : LoggingValue(com.xabber.xmpp.ssn.LoggingValue) AccountItem(com.xabber.android.data.account.AccountItem) DisclosureValue(com.xabber.xmpp.ssn.DisclosureValue) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) OtrMode(com.xabber.xmpp.archive.OtrMode) Feature(com.xabber.xmpp.ssn.Feature) SecurityValue(com.xabber.xmpp.ssn.SecurityValue)

Example 92 with AccountItem

use of com.xabber.android.data.account.AccountItem 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 93 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class MUCManager method requestHostedRooms.

public static void requestHostedRooms(final AccountJid account, final DomainBareJid serviceName, final HostedRoomsListener listener) {
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        listener.onHostedRoomsReceived(null);
        return;
    }
    final XMPPConnection xmppConnection = accountItem.getConnection();
    if (!xmppConnection.isAuthenticated()) {
        listener.onHostedRoomsReceived(null);
        return;
    }
    Application.getInstance().runInBackgroundUserRequest(new Runnable() {

        @Override
        public void run() {
            Collection<HostedRoom> hostedRooms = null;
            try {
                hostedRooms = MultiUserChatManager.getInstanceFor(xmppConnection).getHostedRooms(serviceName);
            } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException | MultiUserChatException.NotAMucServiceException e) {
                LogManager.exception(this, e);
            }
            final Collection<HostedRoom> finalHostedRooms = hostedRooms;
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    listener.onHostedRoomsReceived(finalHostedRooms);
                }
            });
        }
    });
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) SmackException(org.jivesoftware.smack.SmackException) Collection(java.util.Collection) MultiUserChatException(org.jivesoftware.smackx.muc.MultiUserChatException) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPException(org.jivesoftware.smack.XMPPException)

Example 94 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class MUCManager method onStanza.

@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
    if (!(connection instanceof AccountItem)) {
        return;
    }
    AccountJid account = ((AccountItem) connection).getAccount();
    Jid from = stanza.getFrom();
    if (from == null || !(stanza instanceof Message)) {
        return;
    }
    Message message = (Message) stanza;
    if (message.getType() != Message.Type.normal && message.getType() != Message.Type.chat) {
        return;
    }
    MUCUser mucUser = MUCUser.from(stanza);
    if (mucUser == null || mucUser.getInvite() == null) {
        return;
    }
    RoomChat roomChat = getRoomChat(account, from.asEntityBareJidIfPossible());
    if (roomChat == null || !roomChat.getState().inUse()) {
        UserJid inviter = null;
        try {
            inviter = UserJid.from(mucUser.getInvite().getFrom());
        } catch (UserJid.UserJidCreateException e) {
            LogManager.exception(this, e);
        }
        if (inviter == null) {
            try {
                inviter = UserJid.from(from);
            } catch (UserJid.UserJidCreateException e) {
                LogManager.exception(this, e);
            }
        }
        try {
            inviteProvider.add(new RoomInvite(account, UserJid.from(from), inviter, mucUser.getInvite().getReason(), mucUser.getPassword()), true);
        } catch (UserJid.UserJidCreateException e) {
            LogManager.exception(this, e);
        }
    }
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) Jid(org.jxmpp.jid.Jid) Message(org.jivesoftware.smack.packet.Message) AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid)

Example 95 with AccountItem

use of com.xabber.android.data.account.AccountItem in project xabber-android by redsolution.

the class StanzaSender method getXmppTcpConnection.

@NonNull
private static XMPPTCPConnection getXmppTcpConnection(AccountJid account) throws NetworkException {
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        throw new NetworkException(R.string.NOT_CONNECTED);
    }
    XMPPTCPConnection returnConnection = accountItem.getConnection();
    if (!returnConnection.isAuthenticated()) {
        throw new NetworkException(R.string.NOT_CONNECTED);
    }
    return returnConnection;
}
Also used : XMPPTCPConnection(com.xabber.xmpp.smack.XMPPTCPConnection) AccountItem(com.xabber.android.data.account.AccountItem) NetworkException(com.xabber.android.data.NetworkException) NonNull(androidx.annotation.NonNull)

Aggregations

AccountItem (com.xabber.android.data.account.AccountItem)96 AccountJid (com.xabber.android.data.entity.AccountJid)24 UserJid (com.xabber.android.data.entity.UserJid)14 View (android.view.View)12 NetworkException (com.xabber.android.data.NetworkException)11 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)11 Message (org.jivesoftware.smack.packet.Message)11 Presence (org.jivesoftware.smack.packet.Presence)10 TextView (android.widget.TextView)9 XMPPException (org.jivesoftware.smack.XMPPException)9 Jid (org.jxmpp.jid.Jid)9 ImageView (android.widget.ImageView)8 StatusMode (com.xabber.android.data.account.StatusMode)6 ConnectionState (com.xabber.android.data.connection.ConnectionState)5 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)5 Realm (io.realm.Realm)5 ColorDrawable (android.graphics.drawable.ColorDrawable)4 AccountManager (com.xabber.android.data.account.AccountManager)4