Search in sources :

Example 1 with RefUser

use of com.xabber.android.data.extension.references.RefUser in project xabber-android by redsolution.

the class MessageManager method processCarbonsMessage.

public void processCarbonsMessage(AccountJid account, final Message message, CarbonExtension.Direction direction) {
    if (direction == CarbonExtension.Direction.sent) {
        UserJid companion;
        try {
            companion = UserJid.from(message.getTo()).getBareUserJid();
        } catch (UserJid.UserJidCreateException e) {
            return;
        }
        AbstractChat chat = getChat(account, companion);
        if (chat == null) {
            chat = createChat(account, companion);
        }
        final String body = message.getBody();
        if (body == null) {
            return;
        }
        final AbstractChat finalChat = chat;
        String text = body;
        String uid = UUID.randomUUID().toString();
        RealmList<ForwardId> forwardIds = finalChat.parseForwardedMessage(true, message, uid);
        String originalStanza = message.toXML().toString();
        String originalFrom = message.getFrom().toString();
        // forward comment (to support previous forwarded xep)
        String forwardComment = ForwardManager.parseForwardComment(message);
        if (forwardComment != null)
            text = forwardComment;
        // modify body with references
        Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
        text = bodies.first;
        String markupText = bodies.second;
        MessageItem newMessageItem = finalChat.createNewMessageItem(text);
        newMessageItem.setStanzaId(AbstractChat.getStanzaId(message));
        newMessageItem.setSent(true);
        newMessageItem.setForwarded(true);
        if (markupText != null)
            newMessageItem.setMarkupText(markupText);
        // forwarding
        if (forwardIds != null)
            newMessageItem.setForwardedIds(forwardIds);
        newMessageItem.setOriginalStanza(originalStanza);
        newMessageItem.setOriginalFrom(originalFrom);
        // attachments
        RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(message);
        if (attachments.size() > 0)
            newMessageItem.setAttachments(attachments);
        // groupchat
        RefUser groupchatUser = ReferencesManager.getGroupchatUserFromReferences(message);
        if (groupchatUser != null) {
            GroupchatUserManager.getInstance().saveGroupchatUser(groupchatUser);
            newMessageItem.setGroupchatUserId(groupchatUser.getId());
        }
        BackpressureMessageSaver.getInstance().saveMessageItem(newMessageItem);
        // mark incoming messages as read
        finalChat.markAsReadAll(false);
        // start grace period
        AccountManager.getInstance().startGracePeriod(account);
        return;
    }
    UserJid companion = null;
    try {
        companion = UserJid.from(message.getFrom()).getBareUserJid();
    } catch (UserJid.UserJidCreateException e) {
        return;
    }
    // check for spam
    if (SettingsManager.spamFilterMode() != SettingsManager.SpamFilterMode.disabled && RosterManager.getInstance().getRosterContact(account, companion) == null) {
        // just ignore carbons from not-authorized user
        return;
    }
    boolean processed = false;
    for (AbstractChat chat : chats.getNested(account.toString()).values()) {
        if (chat.onPacket(companion, message, true)) {
            processed = true;
            break;
        }
    }
    if (getChat(account, companion) != null) {
        return;
    }
    if (processed) {
        return;
    }
    final String body = message.getBody();
    if (body == null) {
        return;
    }
    createChat(account, companion).onPacket(companion, message, true);
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) RefUser(com.xabber.android.data.extension.references.RefUser) UserJid(com.xabber.android.data.entity.UserJid) Attachment(com.xabber.android.data.database.messagerealm.Attachment) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId)

Example 2 with RefUser

use of com.xabber.android.data.extension.references.RefUser in project xabber-android by redsolution.

the class RegularChat method parseInnerMessage.

@Override
protected String parseInnerMessage(boolean ui, Message message, Date timestamp, String parentMessageId) {
    if (message.getType() == Message.Type.error)
        return null;
    MUCUser mucUser = MUCUser.from(message);
    if (mucUser != null && mucUser.getInvite() != null)
        return null;
    final Jid fromJid = message.getFrom();
    Resourcepart resource = null;
    if (fromJid != null)
        resource = fromJid.getResourceOrNull();
    String text = message.getBody();
    if (text == null)
        return null;
    boolean encrypted = OTRManager.getInstance().isEncrypted(text);
    RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(message);
    String uid = UUID.randomUUID().toString();
    RealmList<ForwardId> forwardIds = parseForwardedMessage(ui, message, uid);
    String originalStanza = message.toXML().toString();
    String originalFrom = "";
    if (fromJid != null)
        originalFrom = fromJid.toString();
    boolean fromMuc = message.getType().equals(Type.groupchat);
    // groupchat
    String gropchatUserId = null;
    RefUser groupchatUser = ReferencesManager.getGroupchatUserFromReferences(message);
    if (groupchatUser != null) {
        gropchatUserId = groupchatUser.getId();
        GroupchatUserManager.getInstance().saveGroupchatUser(groupchatUser, timestamp.getTime());
    }
    // forward comment (to support previous forwarded xep)
    String forwardComment = ForwardManager.parseForwardComment(message);
    if (forwardComment != null && !forwardComment.isEmpty())
        text = forwardComment;
    // modify body with references
    Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
    text = bodies.first;
    String markupText = bodies.second;
    // create message with file-attachments
    if (attachments.size() > 0)
        createAndSaveFileMessage(ui, uid, resource, text, markupText, null, timestamp, getDelayStamp(message), true, false, encrypted, false, getStanzaId(message), attachments, originalStanza, parentMessageId, originalFrom, fromMuc, true, gropchatUserId);
    else
        // create message without attachments
        createAndSaveNewMessage(ui, uid, resource, text, markupText, null, timestamp, getDelayStamp(message), true, false, encrypted, false, getStanzaId(message), originalStanza, parentMessageId, originalFrom, forwardIds, fromMuc, true, gropchatUserId);
    return uid;
}
Also used : RefUser(com.xabber.android.data.extension.references.RefUser) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) Jid(org.jxmpp.jid.Jid) Attachment(com.xabber.android.data.database.messagerealm.Attachment) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 3 with RefUser

use of com.xabber.android.data.extension.references.RefUser in project xabber-android by redsolution.

the class RegularChat method onPacket.

@Override
protected boolean onPacket(UserJid bareAddress, Stanza packet, boolean isCarbons) {
    if (!super.onPacket(bareAddress, packet, isCarbons))
        return false;
    final Resourcepart resource = packet.getFrom().getResourceOrNull();
    if (packet instanceof Presence) {
        final Presence presence = (Presence) packet;
        if (this.resource != null && presence.getType() == Presence.Type.unavailable && resource != null && this.resource.equals(resource)) {
            this.resource = null;
        }
        if (packet.hasExtension(RefUser.NAMESPACE)) {
            this.isGroupchat = true;
        }
    // if (presence.getType() == Presence.Type.unavailable) {
    // OTRManager.getInstance().onContactUnAvailable(account, user);
    // }
    } else if (packet instanceof Message) {
        final Message message = (Message) packet;
        if (message.getType() == Message.Type.error)
            return true;
        MUCUser mucUser = MUCUser.from(message);
        if (mucUser != null && mucUser.getInvite() != null)
            return true;
        String text = message.getBody();
        if (text == null)
            return true;
        DelayInformation delayInformation = message.getExtension(DelayInformation.ELEMENT, DelayInformation.NAMESPACE);
        if (delayInformation != null && "Offline Storage".equals(delayInformation.getReason())) {
            return true;
        }
        // Xabber service message received
        if (message.getType() == Type.headline) {
            if (XMPPAuthManager.getInstance().isXabberServiceMessage(message.getStanzaId()))
                return true;
        }
        String thread = message.getThread();
        updateThreadId(thread);
        if (resource != null && !resource.equals(Resourcepart.EMPTY)) {
            this.resource = resource;
        }
        boolean encrypted = OTRManager.getInstance().isEncrypted(text);
        if (!isCarbons) {
            try {
                text = OTRManager.getInstance().transformReceiving(account, user, text);
            } catch (OtrException e) {
                if (e.getCause() instanceof OTRUnencryptedException) {
                    text = ((OTRUnencryptedException) e.getCause()).getText();
                    encrypted = false;
                } else {
                    LogManager.exception(this, e);
                    // Invalid message received.
                    return true;
                }
            }
        }
        // groupchat
        String gropchatUserId = null;
        RefUser groupchatUser = ReferencesManager.getGroupchatUserFromReferences(packet);
        if (groupchatUser != null) {
            gropchatUserId = groupchatUser.getId();
            GroupchatUserManager.getInstance().saveGroupchatUser(groupchatUser);
        }
        RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(packet);
        String uid = UUID.randomUUID().toString();
        RealmList<ForwardId> forwardIds = parseForwardedMessage(true, packet, uid);
        String originalStanza = packet.toXML().toString();
        String originalFrom = packet.getFrom().toString();
        // forward comment (to support previous forwarded xep)
        String forwardComment = ForwardManager.parseForwardComment(packet);
        if (forwardComment != null)
            text = forwardComment;
        // System message received.
        if ((text == null || text.trim().equals("")) && (forwardIds == null || forwardIds.isEmpty()))
            return true;
        // modify body with references
        Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
        text = bodies.first;
        String markupText = bodies.second;
        // create message with file-attachments
        if (attachments.size() > 0)
            createAndSaveFileMessage(true, uid, resource, text, markupText, null, null, getDelayStamp(message), true, true, encrypted, isOfflineMessage(account.getFullJid().getDomain(), packet), getStanzaId(message), attachments, originalStanza, null, originalFrom, false, false, gropchatUserId);
        else
            // create message without attachments
            createAndSaveNewMessage(true, uid, resource, text, markupText, null, null, getDelayStamp(message), true, true, encrypted, isOfflineMessage(account.getFullJid().getDomain(), packet), getStanzaId(message), originalStanza, null, originalFrom, forwardIds, false, false, gropchatUserId);
        EventBus.getDefault().post(new NewIncomingMessageEvent(account, user));
    }
    return true;
}
Also used : RefUser(com.xabber.android.data.extension.references.RefUser) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) RealmList(io.realm.RealmList) Message(org.jivesoftware.smack.packet.Message) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Presence(org.jivesoftware.smack.packet.Presence) OtrException(net.java.otr4j.OtrException) OTRUnencryptedException(com.xabber.android.data.extension.otr.OTRUnencryptedException) Resourcepart(org.jxmpp.jid.parts.Resourcepart) Pair(android.util.Pair)

Example 4 with RefUser

use of com.xabber.android.data.extension.references.RefUser in project xabber-android by redsolution.

the class NextMamManager method parseMessage.

@Nullable
private MessageItem parseMessage(AccountItem accountItem, AccountJid account, UserJid user, Forwarded forwarded, String prevID) {
    if (!(forwarded.getForwardedStanza() instanceof Message)) {
        return null;
    }
    Message message = (Message) forwarded.getForwardedStanza();
    DelayInformation delayInformation = forwarded.getDelayInformation();
    DelayInformation messageDelay = DelayInformation.from(message);
    String body = message.getBody();
    net.java.otr4j.io.messages.AbstractMessage otrMessage;
    try {
        otrMessage = SerializationUtils.toMessage(body);
    } catch (IOException e) {
        return null;
    }
    boolean encrypted = false;
    if (otrMessage != null) {
        if (otrMessage.messageType != net.java.otr4j.io.messages.AbstractMessage.MESSAGE_PLAINTEXT) {
            encrypted = true;
            try {
                // this transforming just decrypt message if have keys. No action as injectMessage or something else
                body = OTRManager.getInstance().transformReceivingIfSessionExist(account, user, body);
                if (OTRManager.getInstance().isEncrypted(body)) {
                    return null;
                }
            } catch (Exception e) {
                return null;
            }
        } else
            body = ((PlainTextMessage) otrMessage).cleanText;
    }
    // forward comment (to support previous forwarded xep)
    String forwardComment = ForwardManager.parseForwardComment(message);
    if (forwardComment != null)
        body = forwardComment;
    // modify body with references
    Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, body);
    body = bodies.first;
    String markupBody = bodies.second;
    boolean incoming = message.getFrom().asBareJid().equals(user.getJid().asBareJid());
    String uid = UUID.randomUUID().toString();
    MessageItem messageItem = new MessageItem(uid);
    messageItem.setPreviousId(prevID);
    String archivedId = ArchivedHelper.getArchivedId(forwarded.getForwardedStanza());
    if (archivedId != null)
        messageItem.setArchivedId(archivedId);
    long timestamp = delayInformation.getStamp().getTime();
    messageItem.setAccount(account);
    messageItem.setUser(user);
    messageItem.setResource(user.getJid().getResourceOrNull());
    messageItem.setText(body);
    if (markupBody != null)
        messageItem.setMarkupText(markupBody);
    messageItem.setTimestamp(timestamp);
    if (messageDelay != null) {
        messageItem.setDelayTimestamp(messageDelay.getStamp().getTime());
    }
    messageItem.setIncoming(incoming);
    messageItem.setStanzaId(AbstractChat.getStanzaId(message));
    messageItem.setPacketId(message.getStanzaId());
    messageItem.setReceivedFromMessageArchive(true);
    messageItem.setRead(timestamp <= accountItem.getStartHistoryTimestamp());
    messageItem.setSent(true);
    messageItem.setEncrypted(encrypted);
    // attachments
    FileManager.processFileMessage(messageItem);
    RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(message);
    if (attachments.size() > 0)
        messageItem.setAttachments(attachments);
    // forwarded
    messageItem.setOriginalStanza(message.toXML().toString());
    messageItem.setOriginalFrom(message.getFrom().toString());
    // groupchat
    RefUser groupchatUser = ReferencesManager.getGroupchatUserFromReferences(message);
    if (groupchatUser != null) {
        GroupchatUserManager.getInstance().saveGroupchatUser(groupchatUser, timestamp);
        messageItem.setGroupchatUserId(groupchatUser.getId());
    }
    return messageItem;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) RefUser(com.xabber.android.data.extension.references.RefUser) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Message(org.jivesoftware.smack.packet.Message) Attachment(com.xabber.android.data.database.messagerealm.Attachment) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) IOException(java.io.IOException) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Nullable(androidx.annotation.Nullable)

Aggregations

RefUser (com.xabber.android.data.extension.references.RefUser)4 Attachment (com.xabber.android.data.database.messagerealm.Attachment)3 ForwardId (com.xabber.android.data.database.messagerealm.ForwardId)2 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 UserJid (com.xabber.android.data.entity.UserJid)2 Message (org.jivesoftware.smack.packet.Message)2 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)2 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)2 Resourcepart (org.jxmpp.jid.parts.Resourcepart)2 Pair (android.util.Pair)1 Nullable (androidx.annotation.Nullable)1 AccountJid (com.xabber.android.data.entity.AccountJid)1 OTRUnencryptedException (com.xabber.android.data.extension.otr.OTRUnencryptedException)1 RealmList (io.realm.RealmList)1 IOException (java.io.IOException)1 OtrException (net.java.otr4j.OtrException)1 PlainTextMessage (net.java.otr4j.io.messages.PlainTextMessage)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 Presence (org.jivesoftware.smack.packet.Presence)1