Search in sources :

Example 16 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method sendMessage.

private void sendMessage(final Message message, final boolean resend, final boolean delay) {
    final Account account = message.getConversation().getAccount();
    if (account.setShowErrorNotification(true)) {
        databaseBackend.updateAccount(account);
        mNotificationService.updateErrorNotification();
    }
    final Conversation conversation = message.getConversation();
    account.deactivateGracePeriod();
    MessagePacket packet = null;
    final boolean addToConversation = (conversation.getMode() != Conversation.MODE_MULTI || !Patches.BAD_MUC_REFLECTION.contains(account.getServerIdentity())) && !message.edited();
    boolean saveInDb = addToConversation;
    message.setStatus(Message.STATUS_WAITING);
    if (!resend && message.getEncryption() != Message.ENCRYPTION_OTR) {
        message.getConversation().endOtrIfNeeded();
        message.getConversation().findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, new Conversation.OnMessageFound() {

            @Override
            public void onMessageFound(Message message) {
                markMessage(message, Message.STATUS_SEND_FAILED);
            }
        });
    }
    if (account.isOnlineAndConnected()) {
        switch(message.getEncryption()) {
            case Message.ENCRYPTION_NONE:
                if (message.needsUploading()) {
                    if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize()) || conversation.getMode() == Conversation.MODE_MULTI || message.fixCounterpart()) {
                        this.sendFileMessage(message, delay);
                    } else {
                        break;
                    }
                } else {
                    packet = mMessageGenerator.generateChat(message);
                }
                break;
            case Message.ENCRYPTION_PGP:
            case Message.ENCRYPTION_DECRYPTED:
                if (message.needsUploading()) {
                    if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize()) || conversation.getMode() == Conversation.MODE_MULTI || message.fixCounterpart()) {
                        this.sendFileMessage(message, delay);
                    } else {
                        break;
                    }
                } else {
                    packet = mMessageGenerator.generatePgpChat(message);
                }
                break;
            case Message.ENCRYPTION_OTR:
                SessionImpl otrSession = conversation.getOtrSession();
                if (otrSession != null && otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
                    try {
                        message.setCounterpart(Jid.fromSessionID(otrSession.getSessionID()));
                    } catch (InvalidJidException e) {
                        break;
                    }
                    if (message.needsUploading()) {
                        mJingleConnectionManager.createNewConnection(message);
                    } else {
                        packet = mMessageGenerator.generateOtrChat(message);
                    }
                } else if (otrSession == null) {
                    if (message.fixCounterpart()) {
                        conversation.startOtrSession(message.getCounterpart().getResourcepart(), true);
                    } else {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not fix counterpart for OTR message to contact " + message.getCounterpart());
                        break;
                    }
                } else {
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + " OTR session with " + message.getContact() + " is in wrong state: " + otrSession.getSessionStatus().toString());
                }
                break;
            case Message.ENCRYPTION_AXOLOTL:
                message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
                if (message.needsUploading()) {
                    if (account.httpUploadAvailable(fileBackend.getFile(message, false).getSize()) || conversation.getMode() == Conversation.MODE_MULTI || message.fixCounterpart()) {
                        this.sendFileMessage(message, delay);
                    } else {
                        break;
                    }
                } else {
                    XmppAxolotlMessage axolotlMessage = account.getAxolotlService().fetchAxolotlMessageFromCache(message);
                    if (axolotlMessage == null) {
                        account.getAxolotlService().preparePayloadMessage(message, delay);
                    } else {
                        packet = mMessageGenerator.generateAxolotlChat(message, axolotlMessage);
                    }
                }
                break;
        }
        if (packet != null) {
            if (account.getXmppConnection().getFeatures().sm() || (conversation.getMode() == Conversation.MODE_MULTI && message.getCounterpart().isBareJid())) {
                message.setStatus(Message.STATUS_UNSEND);
            } else {
                message.setStatus(Message.STATUS_SEND);
            }
        }
    } else {
        switch(message.getEncryption()) {
            case Message.ENCRYPTION_DECRYPTED:
                if (!message.needsUploading()) {
                    String pgpBody = message.getEncryptedBody();
                    String decryptedBody = message.getBody();
                    // TODO might throw NPE
                    message.setBody(pgpBody);
                    message.setEncryption(Message.ENCRYPTION_PGP);
                    if (message.edited()) {
                        message.setBody(decryptedBody);
                        message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                        databaseBackend.updateMessage(message, message.getEditedId());
                        updateConversationUi();
                        return;
                    } else {
                        databaseBackend.createMessage(message);
                        saveInDb = false;
                        message.setBody(decryptedBody);
                        message.setEncryption(Message.ENCRYPTION_DECRYPTED);
                    }
                }
                break;
            case Message.ENCRYPTION_OTR:
                if (!conversation.hasValidOtrSession() && message.getCounterpart() != null) {
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": create otr session without starting for " + message.getContact().getJid());
                    conversation.startOtrSession(message.getCounterpart().getResourcepart(), false);
                }
                break;
            case Message.ENCRYPTION_AXOLOTL:
                message.setFingerprint(account.getAxolotlService().getOwnFingerprint());
                break;
        }
    }
    boolean mucMessage = conversation.getMode() == Conversation.MODE_MULTI && message.getType() != Message.TYPE_PRIVATE;
    if (mucMessage) {
        message.setCounterpart(conversation.getMucOptions().getSelf().getFullJid());
    }
    if (resend) {
        if (packet != null && addToConversation) {
            if (account.getXmppConnection().getFeatures().sm() || mucMessage) {
                markMessage(message, Message.STATUS_UNSEND);
            } else {
                markMessage(message, Message.STATUS_SEND);
            }
        }
    } else {
        if (addToConversation) {
            conversation.add(message);
        }
        if (saveInDb) {
            databaseBackend.createMessage(message);
        } else if (message.edited()) {
            databaseBackend.updateMessage(message, message.getEditedId());
        }
        updateConversationUi();
    }
    if (packet != null) {
        if (delay) {
            mMessageGenerator.addDelay(packet, message.getTimeSent());
        }
        if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
            if (this.sendChatStates()) {
                packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
            }
        }
        sendMessagePacket(account, packet);
    }
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) Account(de.pixart.messenger.entities.Account) XmppAxolotlMessage(de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage) Message(de.pixart.messenger.entities.Message) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Conversation(de.pixart.messenger.entities.Conversation) XmppAxolotlMessage(de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage) SessionImpl(net.java.otr4j.session.SessionImpl)

Example 17 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class ShareViaAccountActivity method onBackendConnected.

@Override
void onBackendConnected() {
    final int numAccounts = xmppConnectionService.getAccounts().size();
    if (numAccounts == 1) {
        final String body = getIntent().getStringExtra(EXTRA_BODY);
        final Account account = xmppConnectionService.getAccounts().get(0);
        try {
            final Jid contact = Jid.fromString(getIntent().getStringExtra(EXTRA_CONTACT));
            final Conversation conversation = xmppConnectionService.findOrCreateConversation(account, contact, false, false);
            switchToConversation(conversation, body, false);
        } catch (InvalidJidException e) {
        // ignore error
        }
        finish();
    } else {
        refreshUiReal();
    }
}
Also used : Account(de.pixart.messenger.entities.Account) Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Conversation(de.pixart.messenger.entities.Conversation)

Example 18 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class ShareWithActivity method share.

private void share() {
    final Conversation conversation;
    if (share.uuid != null) {
        conversation = xmppConnectionService.findConversationByUuid(share.uuid);
        if (conversation == null) {
            return;
        }
    } else {
        Account account;
        try {
            account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
        } catch (final InvalidJidException e) {
            account = null;
        }
        if (account == null) {
            return;
        }
        try {
            conversation = xmppConnectionService.findOrCreateConversation(account, Jid.fromString(share.contact), false, true);
        } catch (final InvalidJidException e) {
            return;
        }
    }
    ContactChosen = true;
    share(conversation);
}
Also used : Account(de.pixart.messenger.entities.Account) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Conversation(de.pixart.messenger.entities.Conversation)

Example 19 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class StartConversationActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (resultCode == RESULT_OK) {
        if (xmppConnectionServiceBound) {
            this.mPostponedActivityResult = null;
            if (requestCode == REQUEST_CREATE_CONFERENCE) {
                Account account = extractAccount(intent);
                final String subject = intent.getStringExtra("subject");
                List<Jid> jids = new ArrayList<>();
                if (intent.getBooleanExtra("multiple", false)) {
                    String[] toAdd = intent.getStringArrayExtra("contacts");
                    for (String item : toAdd) {
                        try {
                            jids.add(Jid.fromString(item));
                        } catch (InvalidJidException e) {
                        // ignored
                        }
                    }
                } else {
                    try {
                        jids.add(Jid.fromString(intent.getStringExtra("contact")));
                    } catch (Exception e) {
                    // ignored
                    }
                }
                if (account != null && jids.size() > 0) {
                    if (xmppConnectionService.createAdhocConference(account, subject, jids, mAdhocConferenceCallback)) {
                        mToast = Toast.makeText(this, R.string.creating_conference, Toast.LENGTH_LONG);
                        mToast.show();
                    }
                }
            }
        } else {
            this.mPostponedActivityResult = new Pair<>(requestCode, intent);
        }
    }
    super.onActivityResult(requestCode, requestCode, intent);
}
Also used : Account(de.pixart.messenger.entities.Account) Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) ActivityNotFoundException(android.content.ActivityNotFoundException) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException)

Example 20 with InvalidJidException

use of de.pixart.messenger.xmpp.jid.InvalidJidException in project Pix-Art-Messenger by kriztan.

the class StartConversationActivity method showJoinConferenceDialog.

@SuppressLint("InflateParams")
protected void showJoinConferenceDialog(final String prefilledJid) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.join_conference);
    final View dialogView = getLayoutInflater().inflate(R.layout.join_conference_dialog, null);
    final TextView yourAccount = dialogView.findViewById(R.id.your_account);
    final Spinner spinner = dialogView.findViewById(R.id.account);
    final AutoCompleteTextView jid = dialogView.findViewById(R.id.jid);
    DelayedHintHelper.setHint(R.string.conference_address_example, jid);
    jid.setAdapter(new KnownHostsAdapter(this, R.layout.simple_list_item, mKnownConferenceHosts));
    if (prefilledJid != null) {
        jid.append(prefilledJid);
    }
    if (xmppConnectionService.multipleAccounts()) {
        yourAccount.setVisibility(View.VISIBLE);
        spinner.setVisibility(View.VISIBLE);
    } else {
        yourAccount.setVisibility(View.GONE);
        spinner.setVisibility(View.GONE);
    }
    populateAccountSpinner(this, mActivatedAccounts, spinner);
    final Checkable bookmarkCheckBox = (CheckBox) dialogView.findViewById(R.id.bookmark);
    builder.setView(dialogView);
    builder.setNegativeButton(R.string.cancel, null);
    builder.setPositiveButton(R.string.join, null);
    final AlertDialog dialog = builder.create();
    dialog.show();
    mCurrentDialog = dialog;
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
        if (!xmppConnectionServiceBound) {
            return;
        }
        final Account account = getSelectedAccount(spinner);
        if (account == null) {
            return;
        }
        final Jid conferenceJid;
        try {
            conferenceJid = Jid.fromString(jid.getText().toString());
        } catch (final InvalidJidException e) {
            jid.setError(getString(R.string.invalid_jid));
            return;
        }
        if (bookmarkCheckBox.isChecked()) {
            if (account.hasBookmarkFor(conferenceJid)) {
                jid.setError(getString(R.string.bookmark_already_exists));
            } else {
                final Bookmark bookmark = new Bookmark(account, conferenceJid.toBareJid());
                bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin)));
                String nick = conferenceJid.getResourcepart();
                if (nick != null && !nick.isEmpty()) {
                    bookmark.setNick(nick);
                }
                account.getBookmarks().add(bookmark);
                xmppConnectionService.pushBookmarks(account);
                final Conversation conversation = xmppConnectionService.findOrCreateConversation(account, conferenceJid, true, true, true);
                bookmark.setConversation(conversation);
                dialog.dismiss();
                mCurrentDialog = null;
                switchToConversation(conversation);
            }
        } else {
            final Conversation conversation = xmppConnectionService.findOrCreateConversation(account, conferenceJid, true, true, true);
            dialog.dismiss();
            mCurrentDialog = null;
            switchToConversation(conversation);
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) KnownHostsAdapter(de.pixart.messenger.ui.adapter.KnownHostsAdapter) Account(de.pixart.messenger.entities.Account) Jid(de.pixart.messenger.xmpp.jid.Jid) Spinner(android.widget.Spinner) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Conversation(de.pixart.messenger.entities.Conversation) SpannableString(android.text.SpannableString) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) AutoCompleteTextView(android.widget.AutoCompleteTextView) Bookmark(de.pixart.messenger.entities.Bookmark) CheckBox(android.widget.CheckBox) TextView(android.widget.TextView) AutoCompleteTextView(android.widget.AutoCompleteTextView) Checkable(android.widget.Checkable) AutoCompleteTextView(android.widget.AutoCompleteTextView) SuppressLint(android.annotation.SuppressLint)

Aggregations

InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)33 Jid (de.pixart.messenger.xmpp.jid.Jid)25 Account (de.pixart.messenger.entities.Account)14 Conversation (de.pixart.messenger.entities.Conversation)13 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)5 AlertDialog (android.support.v7.app.AlertDialog)4 Contact (de.pixart.messenger.entities.Contact)4 Message (de.pixart.messenger.entities.Message)4 IqPacket (de.pixart.messenger.xmpp.stanzas.IqPacket)4 View (android.view.View)3 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 OnIqPacketReceived (de.pixart.messenger.xmpp.OnIqPacketReceived)3 ArrayList (java.util.ArrayList)3 SuppressLint (android.annotation.SuppressLint)2 Bundle (android.os.Bundle)2 SpannableString (android.text.SpannableString)2 Pair (android.util.Pair)2 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)2 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)2