Search in sources :

Example 1 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project Smack by igniterealtime.

the class ForwardedProvider method parse.

@Override
public Forwarded parse(XmlPullParser parser, int initialDepth) throws Exception {
    DelayInformation di = null;
    Stanza packet = null;
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(name) {
                    case DelayInformation.ELEMENT:
                        if (DelayInformation.NAMESPACE.equals(namespace)) {
                            di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth());
                        } else {
                            LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '" + DelayInformation.NAMESPACE + "'");
                        }
                        break;
                    case Message.ELEMENT:
                        packet = PacketParserUtils.parseMessage(parser);
                        break;
                    default:
                        LOGGER.warning("Unsupported forwarded packet type: " + name);
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    if (packet == null)
        throw new SmackException("forwarded extension must contain a packet");
    return new Forwarded(di, packet);
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Stanza(org.jivesoftware.smack.packet.Stanza) SmackException(org.jivesoftware.smack.SmackException) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded)

Example 2 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project xabber-android by redsolution.

the class MamManager method getMessageItems.

private List<MessageItem> getMessageItems(org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult, AbstractChat chat) {
    List<MessageItem> messageItems = new ArrayList<>();
    for (Forwarded forwarded : mamQueryResult.forwardedMessages) {
        if (!(forwarded.getForwardedStanza() instanceof Message)) {
            continue;
        }
        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) {
            continue;
        }
        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(chat.getAccount(), chat.getUser(), body);
                    if (OTRManager.getInstance().isEncrypted(body)) {
                        continue;
                    }
                } catch (Exception e) {
                    continue;
                }
            } else
                body = ((PlainTextMessage) otrMessage).cleanText;
        }
        boolean incoming = message.getFrom().asBareJid().equals(chat.getUser().getJid().asBareJid());
        MessageItem messageItem = new MessageItem();
        messageItem.setAccount(chat.getAccount());
        messageItem.setUser(chat.getUser());
        messageItem.setResource(chat.getUser().getJid().getResourceOrNull());
        messageItem.setText(body);
        messageItem.setTimestamp(delayInformation.getStamp().getTime());
        if (messageDelay != null) {
            messageItem.setDelayTimestamp(messageDelay.getStamp().getTime());
        }
        messageItem.setIncoming(incoming);
        messageItem.setStanzaId(message.getStanzaId());
        messageItem.setReceivedFromMessageArchive(true);
        messageItem.setRead(true);
        messageItem.setSent(true);
        messageItem.setEncrypted(encrypted);
        FileManager.processFileMessage(messageItem);
        messageItems.add(messageItem);
    }
    return messageItems;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Message(org.jivesoftware.smack.packet.Message) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded)

Example 3 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project xabber-android by redsolution.

the class NextMamManager method getNextDate.

private Date getNextDate(MamManager.MamQueryResult queryResult) {
    Date date = null;
    if (queryResult.forwardedMessages != null && !queryResult.forwardedMessages.isEmpty()) {
        Forwarded forwarded = queryResult.forwardedMessages.get(queryResult.forwardedMessages.size() - 1);
        DelayInformation delayInformation = forwarded.getDelayInformation();
        date = new Date(delayInformation.getStamp().getTime() + 1);
    }
    return date;
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) Date(java.util.Date)

Example 4 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project xabber-android by redsolution.

the class NextMamManager method parseMessage.

private List<MessageItem> parseMessage(AccountItem accountItem, AccountJid account, UserJid user, List<Forwarded> forwardedMessages, String prevID) {
    List<MessageItem> messageItems = new ArrayList<>();
    String lastOutgoingId = null;
    for (Forwarded forwarded : forwardedMessages) {
        MessageItem message = parseMessage(accountItem, account, user, forwarded, prevID);
        if (message != null) {
            messageItems.add(message);
            prevID = message.getArchivedId();
            if (!message.isIncoming())
                lastOutgoingId = message.getUniqueId();
        }
    }
    // mark messages before outgoing as read
    if (lastOutgoingId != null) {
        for (MessageItem message : messageItems) {
            if (lastOutgoingId.equals(message.getUniqueId())) {
                break;
            }
            message.setRead(true);
        }
    }
    return messageItems;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) ArrayList(java.util.ArrayList) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded)

Example 5 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project xabber-android by redsolution.

the class NextMamManager method loadMissedMessages.

private void loadMissedMessages(Realm realm, AccountItem accountItem, AbstractChat chat, MessageItem m1) {
    LogManager.d(LOG_TAG, "load missed messages in chat: " + chat.getUser());
    MessageItem m2 = getMessageForCloseMissedMessages(realm, m1);
    if (m2 != null && !m2.getUniqueId().equals(m1.getUniqueId())) {
        Date startDate = new Date(m2.getTimestamp());
        Date endDate = new Date(m1.getTimestamp());
        List<Forwarded> messages = new ArrayList<>();
        boolean complete = false;
        while (!complete && startDate != null) {
            MamManager.MamQueryResult queryResult = requestMissedMessages(accountItem, chat, startDate, endDate);
            if (queryResult != null) {
                messages.addAll(queryResult.forwardedMessages);
                complete = queryResult.mamFin.isComplete();
                startDate = getNextDate(queryResult);
            } else
                complete = true;
        }
        if (!messages.isEmpty()) {
            List<MessageItem> savedMessages = saveOrUpdateMessages(realm, parseMessage(accountItem, chat.getAccount(), chat.getUser(), messages, m2.getArchivedId()));
            if (savedMessages != null && !savedMessages.isEmpty()) {
                realm.beginTransaction();
                m1.setPreviousId(savedMessages.get(savedMessages.size() - 1).getArchivedId());
                realm.commitTransaction();
            }
        } else {
            realm.beginTransaction();
            m1.setPreviousId(m2.getArchivedId());
            realm.commitTransaction();
        }
    }
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) MamManager(org.jivesoftware.smackx.mam.MamManager) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)19 ArrayList (java.util.ArrayList)10 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)8 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)5 Date (java.util.Date)5 Message (org.jivesoftware.smack.packet.Message)5 Stanza (org.jivesoftware.smack.packet.Stanza)4 MamManager (org.jivesoftware.smackx.mam.MamManager)4 SmackException (org.jivesoftware.smack.SmackException)3 NonNull (androidx.annotation.NonNull)2 IOException (java.io.IOException)2 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)2 MamResultExtension (org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension)2 ForwardId (com.xabber.android.data.database.messagerealm.ForwardId)1 AccountJid (com.xabber.android.data.entity.AccountJid)1 UserJid (com.xabber.android.data.entity.UserJid)1 AbstractChat (com.xabber.android.data.message.AbstractChat)1 RealmList (io.realm.RealmList)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1