Search in sources :

Example 16 with NetworkException

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

the class AccountManager method addAccount.

/**
     * Creates new account.
     *
     * @param user          full or bare jid.
     * @param password
     * @param accountType   xmpp account type can be replaced depend on server part.
     * @param syncable
     * @param storePassword
     * @param useOrbot
     * @return assigned account name.
     * @throws NetworkException if user or server part are invalid.
     */
public String addAccount(String user, String password, AccountType accountType, boolean syncable, boolean storePassword, boolean useOrbot, boolean registerNewAccount) throws NetworkException {
    if (accountType.getProtocol().isOAuth()) {
        int index = 1;
        while (true) {
            user = String.valueOf(index);
            boolean found = false;
            for (AccountItem accountItem : accountItems.values()) {
                if (accountItem.getConnectionSettings().getServerName().equals(accountType.getFirstServer()) && accountItem.getConnectionSettings().getUserName().equals(user)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                break;
            }
            index++;
        }
    }
    if (user == null) {
        throw new NetworkException(R.string.EMPTY_USER_NAME);
    }
    if (!user.contains("@")) {
        if ("".equals(accountType.getFirstServer())) {
            throw new NetworkException(R.string.EMPTY_SERVER_NAME);
        } else {
            user += "@" + accountType.getFirstServer();
        }
    }
    String serverName = XmppStringUtils.parseDomain(user);
    String userName = XmppStringUtils.parseLocalpart(user);
    String resource = XmppStringUtils.parseResource(user);
    String host = accountType.getHost();
    int port = accountType.getPort();
    boolean tlsRequired = accountType.isTLSRequired();
    if (useOrbot) {
        tlsRequired = true;
    }
    if ("".equals(serverName)) {
        throw new NetworkException(R.string.EMPTY_SERVER_NAME);
    } else if (!accountType.isAllowServer() && !serverName.equals(accountType.getFirstServer())) {
        throw new NetworkException(R.string.INCORRECT_USER_NAME);
    }
    if ("".equals(userName)) {
        throw new NetworkException(R.string.EMPTY_USER_NAME);
    }
    if ("".equals(resource)) {
        resource = generateResource();
    }
    if (accountType.getId() == R.array.account_type_xmpp) {
        host = serverName;
        for (AccountType check : accountTypes) {
            if (check.getServers().contains(serverName)) {
                accountType = check;
                host = check.getHost();
                port = check.getPort();
                tlsRequired = check.isTLSRequired();
                break;
            }
        }
    }
    AccountItem accountItem;
    while (true) {
        if (getAccount(userName + '@' + serverName + '/' + resource) == null) {
            break;
        }
        resource = generateResource();
    }
    boolean useCustomHost = application.getResources().getBoolean(R.bool.account_use_custom_host_default);
    if (accountType.getProtocol() == AccountProtocol.gtalk) {
        useCustomHost = true;
    }
    boolean useCompression = application.getResources().getBoolean(R.bool.account_use_compression_default);
    ArchiveMode archiveMode = ArchiveMode.valueOf(application.getString(R.string.account_archive_mode_default_value));
    accountItem = addAccount(accountType.getProtocol(), useCustomHost, host, port, serverName, userName, storePassword, password, resource, getNextColorIndex(), 0, StatusMode.available, SettingsManager.statusText(), true, true, tlsRequired ? TLSMode.required : TLSMode.enabled, useCompression, useOrbot ? ProxyType.orbot : ProxyType.none, "localhost", 8080, "", "", syncable, null, null, archiveMode, registerNewAccount);
    if (accountItem == null) {
        throw new NetworkException(R.string.ACCOUNT_REGISTER_FAILED);
    }
    onAccountChanged(accountItem.getAccount());
    if (accountItems.size() > 1 && SettingsManager.contactsEnableShowAccounts()) {
        SettingsManager.enableContactsShowAccount();
    }
    return accountItem.getAccount();
}
Also used : NetworkException(com.xabber.android.data.NetworkException)

Example 17 with NetworkException

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

the class ConnectionManager method sendStanza.

/**
     * Send stanza to authenticated connection.
     *
     * @param account
     * @param stanza
     */
public void sendStanza(String account, Stanza stanza) throws NetworkException {
    ConnectionThread connectionThread = null;
    for (ConnectionThread check : managedConnections) {
        if (check.getConnectionItem() instanceof AccountItem && ((AccountItem) check.getConnectionItem()).getAccount().equals(account)) {
            connectionThread = check;
            break;
        }
    }
    if (connectionThread == null || !connectionThread.getConnectionItem().getState().isConnected()) {
        throw new NetworkException(R.string.NOT_CONNECTED);
    }
    XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
    try {
        xmppConnection.sendStanza(stanza);
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
        throw new NetworkException(R.string.XMPP_EXCEPTION);
    }
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) SmackException(org.jivesoftware.smack.SmackException) XMPPConnection(org.jivesoftware.smack.XMPPConnection) NetworkException(com.xabber.android.data.NetworkException)

Example 18 with NetworkException

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

the class ConnectionThread method passwordRequest.

/**
     * Request to renew password using OAuth.
     */
private void passwordRequest() {
    final OAuthResult oAuthResult;
    try {
        oAuthResult = OAuthManager.getInstance().requestAccessToken(protocol, token);
    } catch (NetworkException e) {
        throw new RuntimeException(e);
    }
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            onPasswordReceived(oAuthResult);
        }
    });
}
Also used : OAuthResult(com.xabber.android.data.account.OAuthResult) NetworkException(com.xabber.android.data.NetworkException)

Example 19 with NetworkException

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

the class MessageArchiveManager method checkForDefaults.

private void checkForDefaults(String account, Default received) {
    if (received == null || !received.isUnset() || received.getSave() != SaveMode.fls)
        return;
    Default defaultItem = new Default();
    defaultItem.setExpire(received.getExpire());
    defaultItem.setOtr(received.getOtr());
    defaultItem.setSave(SaveMode.body);
    Pref pref = new Pref();
    pref.setDefault(defaultItem);
    pref.setType(Type.set);
    try {
        ConnectionManager.getInstance().sendStanza(account, pref);
    } catch (NetworkException e) {
    }
}
Also used : Pref(com.xabber.xmpp.archive.Pref) Default(com.xabber.xmpp.archive.Default) NetworkException(com.xabber.android.data.NetworkException)

Example 20 with NetworkException

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

the class MessageArchiveManager method requestChat.

private void requestChat(String account, CollectionHeader header, String after, boolean modification) {
    Retrieve packet = new Retrieve();
    packet.setType(Type.get);
    Set rsm = new Set();
    rsm.setMax(RSM_MAX);
    rsm.setAfter(after);
    packet.setRsm(rsm);
    packet.setWith(header.getWith());
    packet.setStartString(header.getStartString());
    modificationRequests.put(account, packet.getPacketID(), modification);
    try {
        if (!modification) {
            ConnectionManager.getInstance().sendStanza(account, packet);
            return;
        }
        ConnectionManager.getInstance().sendRequest(account, packet, new OnResponseListener() {

            @Override
            public void onReceived(String account, String packetId, IQ iq) {
                if (iq instanceof Chat && ((Chat) iq).isValid())
                    onChatReceived(account, (Chat) iq);
                else
                    onError(account, packetId, iq);
            }

            @Override
            public void onError(String account, String packetId, IQ iq) {
                onModifiedAvailable(account);
            }

            @Override
            public void onTimeout(String account, String packetId) {
                onError(account, packetId, null);
            }

            @Override
            public void onDisconnect(String account, String packetId) {
            }
        });
    } catch (NetworkException e) {
    }
}
Also used : Retrieve(com.xabber.xmpp.archive.Retrieve) Set(com.xabber.xmpp.rsm.Set) OnResponseListener(com.xabber.android.data.connection.OnResponseListener) IQ(org.jivesoftware.smack.packet.IQ) AbstractChat(com.xabber.android.data.message.AbstractChat) Chat(com.xabber.xmpp.archive.Chat) NetworkException(com.xabber.android.data.NetworkException)

Aggregations

NetworkException (com.xabber.android.data.NetworkException)64 Message (org.jivesoftware.smack.packet.Message)13 AccountJid (com.xabber.android.data.entity.AccountJid)11 OtrException (net.java.otr4j.OtrException)11 AccountItem (com.xabber.android.data.account.AccountItem)10 AbstractChat (com.xabber.android.data.message.AbstractChat)10 OnResponseListener (com.xabber.android.data.connection.OnResponseListener)6 Date (java.util.Date)6 IQ (org.jivesoftware.smack.packet.IQ)6 UserJid (com.xabber.android.data.entity.UserJid)5 ArrayList (java.util.ArrayList)4 Presence (org.jivesoftware.smack.packet.Presence)4 Intent (android.content.Intent)3 Set (com.xabber.xmpp.rsm.Set)3 File (java.io.File)3 SmackException (org.jivesoftware.smack.SmackException)3 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)3 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 Captcha (com.xabber.android.data.extension.captcha.Captcha)2 RoomChat (com.xabber.android.data.extension.muc.RoomChat)2