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);
}
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;
}
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;
}
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;
}
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();
}
}
}
Aggregations