Search in sources :

Example 1 with Message

use of eu.siacs.conversations.entities.Message in project Conversations by siacs.

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()) || 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()) || 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.getContact().getJid());
                        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()) || 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();
                    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;
        }
    }
    if (resend) {
        if (packet != null && addToConversation) {
            if (account.getXmppConnection().getFeatures().sm() || (conversation.getMode() == Conversation.MODE_MULTI && message.getCounterpart().isBareJid())) {
                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(eu.siacs.conversations.xmpp.stanzas.MessagePacket) Account(eu.siacs.conversations.entities.Account) XmppAxolotlMessage(eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage) Message(eu.siacs.conversations.entities.Message) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException) Conversation(eu.siacs.conversations.entities.Conversation) XmppAxolotlMessage(eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage) SessionImpl(net.java.otr4j.session.SessionImpl)

Example 2 with Message

use of eu.siacs.conversations.entities.Message in project Conversations by siacs.

the class XmppConnectionService method markRead.

public boolean markRead(final Conversation conversation, boolean clear) {
    if (clear) {
        mNotificationService.clear(conversation);
    }
    final List<Message> readMessages = conversation.markRead();
    if (readMessages.size() > 0) {
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                for (Message message : readMessages) {
                    databaseBackend.updateMessage(message);
                }
            }
        };
        mDatabaseExecutor.execute(runnable);
        updateUnreadCountBadge();
        return true;
    } else {
        return false;
    }
}
Also used : XmppAxolotlMessage(eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage) Message(eu.siacs.conversations.entities.Message)

Example 3 with Message

use of eu.siacs.conversations.entities.Message in project Conversations by siacs.

the class ExportLogsService method writeToFile.

private void writeToFile(Conversation conversation) {
    Jid accountJid = resolveAccountUuid(conversation.getAccountUuid());
    Jid contactJid = conversation.getJid();
    File dir = new File(String.format(DIRECTORY_STRING_FORMAT, accountJid.toBareJid().toString()));
    dir.mkdirs();
    BufferedWriter bw = null;
    try {
        for (Message message : mDatabaseBackend.getMessagesIterable(conversation)) {
            if (message == null)
                continue;
            if (message.getType() == Message.TYPE_TEXT || message.hasFileOnRemoteHost()) {
                String date = simpleDateFormat.format(new Date(message.getTimeSent()));
                if (bw == null) {
                    bw = new BufferedWriter(new FileWriter(new File(dir, contactJid.toBareJid().toString() + ".txt")));
                }
                String jid = null;
                switch(message.getStatus()) {
                    case Message.STATUS_RECEIVED:
                        jid = getMessageCounterpart(message);
                        break;
                    case Message.STATUS_SEND:
                    case Message.STATUS_SEND_RECEIVED:
                    case Message.STATUS_SEND_DISPLAYED:
                        jid = accountJid.toBareJid().toString();
                        break;
                }
                if (jid != null) {
                    String body = message.hasFileOnRemoteHost() ? message.getFileParams().url.toString() : message.getBody();
                    bw.write(String.format(MESSAGE_STRING_FORMAT, date, jid, body.replace("\\\n", "\\ \n").replace("\n", "\\ \n")));
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bw != null) {
                bw.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
Also used : Jid(eu.siacs.conversations.xmpp.jid.Jid) Message(eu.siacs.conversations.entities.Message) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter)

Example 4 with Message

use of eu.siacs.conversations.entities.Message in project Conversations by siacs.

the class NotificationService method pushToStack.

private void pushToStack(final Message message) {
    final String conversationUuid = message.getConversationUuid();
    if (notifications.containsKey(conversationUuid)) {
        notifications.get(conversationUuid).add(message);
    } else {
        final ArrayList<Message> mList = new ArrayList<>();
        mList.add(message);
        notifications.put(conversationUuid, mList);
    }
}
Also used : Message(eu.siacs.conversations.entities.Message) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString)

Example 5 with Message

use of eu.siacs.conversations.entities.Message in project Conversations by siacs.

the class NotificationService method modifyForImage.

private void modifyForImage(final Builder builder, final Message message, final ArrayList<Message> messages) {
    try {
        final Bitmap bitmap = mXmppConnectionService.getFileBackend().getThumbnail(message, getPixel(288), false);
        final ArrayList<Message> tmp = new ArrayList<>();
        for (final Message msg : messages) {
            if (msg.getType() == Message.TYPE_TEXT && msg.getTransferable() == null) {
                tmp.add(msg);
            }
        }
        final BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
        bigPictureStyle.bigPicture(bitmap);
        if (tmp.size() > 0) {
            CharSequence text = getMergedBodies(tmp);
            bigPictureStyle.setSummaryText(text);
            builder.setContentText(text);
        } else {
            builder.setContentText(mXmppConnectionService.getString(R.string.received_x_file, UIHelper.getFileDescriptionString(mXmppConnectionService, message)));
        }
        builder.setStyle(bigPictureStyle);
    } catch (final FileNotFoundException e) {
        modifyForTextOnly(builder, messages);
    }
}
Also used : Bitmap(android.graphics.Bitmap) Message(eu.siacs.conversations.entities.Message) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) BigPictureStyle(android.support.v4.app.NotificationCompat.BigPictureStyle)

Aggregations

Message (eu.siacs.conversations.entities.Message)39 XmppAxolotlMessage (eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage)16 Conversation (eu.siacs.conversations.entities.Conversation)12 Account (eu.siacs.conversations.entities.Account)9 Jid (eu.siacs.conversations.xmpp.jid.Jid)7 PendingIntent (android.app.PendingIntent)5 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)5 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)5 ArrayList (java.util.ArrayList)5 SuppressLint (android.annotation.SuppressLint)4 SpannableString (android.text.SpannableString)4 NotificationCompat (android.support.v4.app.NotificationCompat)3 StyleSpan (android.text.style.StyleSpan)3 View (android.view.View)3 TextView (android.widget.TextView)3 PgpEngine (eu.siacs.conversations.crypto.PgpEngine)3 Contact (eu.siacs.conversations.entities.Contact)3 Element (eu.siacs.conversations.xml.Element)3 Cursor (android.database.Cursor)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2