This commit is contained in:
Daniel Gultsch 2018-03-25 18:49:49 +02:00
parent fe8f0bd7f0
commit ecedda8613

View file

@ -39,10 +39,43 @@ import rocks.xmpp.addr.Jid;
public class MessageParser extends AbstractParser implements OnMessagePacketReceived { public class MessageParser extends AbstractParser implements OnMessagePacketReceived {
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
public MessageParser(XmppConnectionService service) { public MessageParser(XmppConnectionService service) {
super(service); super(service);
} }
private static String extractStanzaId(Element packet, boolean isTypeGroupChat, Conversation conversation) {
final Jid by;
final boolean safeToExtract;
if (isTypeGroupChat) {
by = conversation.getJid().asBareJid();
safeToExtract = conversation.getMucOptions().hasFeature(Namespace.STANZA_IDS);
} else {
Account account = conversation.getAccount();
by = account.getJid().asBareJid();
safeToExtract = account.getXmppConnection().getFeatures().stanzaIds();
}
return safeToExtract ? extractStanzaId(packet, by) : null;
}
private static String extractStanzaId(Element packet, Jid by) {
for (Element child : packet.getChildren()) {
if (child.getName().equals("stanza-id")
&& Namespace.STANZA_IDS.equals(child.getNamespace())
&& by.equals(child.getAttributeAsJid("by"))) {
return child.getAttribute("id");
}
}
return null;
}
private static Jid getTrueCounterpart(Element mucUserElement, Jid fallback) {
final Element item = mucUserElement == null ? null : mucUserElement.findChild("item");
Jid result = item == null ? null : item.getAttributeAsJid("jid");
return result != null ? result : fallback;
}
private boolean extractChatState(Conversation c, final boolean isTypeGroupChat, final MessagePacket packet) { private boolean extractChatState(Conversation c, final boolean isTypeGroupChat, final MessagePacket packet) {
ChatState state = ChatState.parse(packet); ChatState state = ChatState.parse(packet);
if (state != null && c != null) { if (state != null && c != null) {
@ -95,32 +128,6 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
return null; return null;
} }
private class Invite {
final Jid jid;
final String password;
final Contact inviter;
Invite(Jid jid, String password, Contact inviter) {
this.jid = jid;
this.password = password;
this.inviter = inviter;
}
public boolean execute(Account account) {
if (jid != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true, false);
if (!conversation.getMucOptions().online()) {
conversation.getMucOptions().setPassword(password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
mXmppConnectionService.joinMuc(conversation, inviter != null && inviter.mutualPresenceSubscription());
mXmppConnectionService.updateConversationUi();
}
return true;
}
return false;
}
}
private Invite extractInvite(Account account, Element message) { private Invite extractInvite(Account account, Element message) {
Element x = message.findChild("x", "http://jabber.org/protocol/muc#user"); Element x = message.findChild("x", "http://jabber.org/protocol/muc#user");
if (x != null) { if (x != null) {
@ -142,31 +149,6 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
return null; return null;
} }
private static String extractStanzaId(Element packet, boolean isTypeGroupChat, Conversation conversation) {
final Jid by;
final boolean safeToExtract;
if (isTypeGroupChat) {
by = conversation.getJid().asBareJid();
safeToExtract = conversation.getMucOptions().hasFeature(Namespace.STANZA_IDS);
} else {
Account account = conversation.getAccount();
by = account.getJid().asBareJid();
safeToExtract = account.getXmppConnection().getFeatures().stanzaIds();
}
return safeToExtract ? extractStanzaId(packet, by) : null;
}
private static String extractStanzaId(Element packet, Jid by) {
for (Element child : packet.getChildren()) {
if (child.getName().equals("stanza-id")
&& Namespace.STANZA_IDS.equals(child.getNamespace())
&& by.equals(child.getAttributeAsJid("by"))) {
return child.getAttribute("id");
}
}
return null;
}
private void parseEvent(final Element event, final Jid from, final Account account) { private void parseEvent(final Element event, final Jid from, final Account account) {
Element items = event.findChild("items"); Element items = event.findChild("items");
String node = items == null ? null : items.getAttribute("node"); String node = items == null ? null : items.getAttribute("node");
@ -761,12 +743,6 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
} }
} }
private static Jid getTrueCounterpart(Element mucUserElement, Jid fallback) {
final Element item = mucUserElement == null ? null : mucUserElement.findChild("item");
Jid result = item == null ? null : item.getAttributeAsJid("jid");
return result != null ? result : fallback;
}
private void processMessageReceipts(Account account, MessagePacket packet, MessageArchiveService.Query query) { private void processMessageReceipts(Account account, MessagePacket packet, MessageArchiveService.Query query) {
final boolean markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0"); final boolean markable = packet.hasChild("markable", "urn:xmpp:chat-markers:0");
final boolean request = packet.hasChild("request", "urn:xmpp:receipts"); final boolean request = packet.hasChild("request", "urn:xmpp:receipts");
@ -792,11 +768,35 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
} }
} }
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
private void activateGracePeriod(Account account) { private void activateGracePeriod(Account account) {
long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000; long duration = mXmppConnectionService.getLongPreference("grace_period_length", R.integer.grace_period) * 1000;
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration))); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": activating grace period till " + TIME_FORMAT.format(new Date(System.currentTimeMillis() + duration)));
account.activateGracePeriod(duration); account.activateGracePeriod(duration);
} }
private class Invite {
final Jid jid;
final String password;
final Contact inviter;
Invite(Jid jid, String password, Contact inviter) {
this.jid = jid;
this.password = password;
this.inviter = inviter;
}
public boolean execute(Account account) {
if (jid != null) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, jid, true, false);
if (!conversation.getMucOptions().online()) {
conversation.getMucOptions().setPassword(password);
mXmppConnectionService.databaseBackend.updateConversation(conversation);
mXmppConnectionService.joinMuc(conversation, inviter != null && inviter.mutualPresenceSubscription());
mXmppConnectionService.updateConversationUi();
}
return true;
}
return false;
}
}
} }