aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/parser/MessageParser.java
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2019-09-12 19:44:01 +0200
committerChristian Schneppe <christian@pix-art.de>2019-09-12 19:44:01 +0200
commit85b8ec7702f5423c82d601c9c265415de344e26e (patch)
tree2b62a61dc8fb9540781b44daf781e5fad8a61d40 /src/main/java/de/pixart/messenger/parser/MessageParser.java
parent0abffde5ef442e8bff1ce7cbfe84bf7e750b3136 (diff)
show language in message bubble if multiple language variants were received
XML and by inheritence XMPP has the feature of transmitting multiple language variants for the same content. This can be really useful if, for example, you are talking to an automated system. A chat bot could greet you in your own language. On the wire this will usually look like this: ```xml <message to="you"> <body>Good morning</body> <body xml:lang="de">Guten Morgen</body> </message> ``` However receiving such a message in a group chat can be very confusing and potentially dangerous if the sender puts conflicting information in there and different people get shown different strings. Disabling support for localization entirely isn’t an ideal solution as on principle it is still a good feature; and other clients might still show a localization even if Conversations would always show the default language. So instead we now show the displayed language in a corner of the message bubble if more than one translation has been received. If multiple languages are received we will attempt to find one in the language the operating system is set to. If no such translation can be found it will attempt to display the English string. If English can not be found either (for example a message that only has ru and fr on a phone that is set to de) it will display what ever language came first. Furthermore we will discard (not show at all) messages with with multiple bodies of the same language. (This is considered an invalid message) The language tag will not be shown if we receive a single body in a language not understood by the user. (For example operating system set to 'de' and message received with one body in 'ru' will just display that body as usual.) As a guide line to the user: If you are reading a message where it is important that this message is not interpreted differently by different people (like a vote (+1 / -1) in a chat room) make sure it has *no* language tag.
Diffstat (limited to 'src/main/java/de/pixart/messenger/parser/MessageParser.java')
-rw-r--r--src/main/java/de/pixart/messenger/parser/MessageParser.java38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/main/java/de/pixart/messenger/parser/MessageParser.java b/src/main/java/de/pixart/messenger/parser/MessageParser.java
index e07ac2615..601bd1e56 100644
--- a/src/main/java/de/pixart/messenger/parser/MessageParser.java
+++ b/src/main/java/de/pixart/messenger/parser/MessageParser.java
@@ -43,6 +43,7 @@ import de.pixart.messenger.services.XmppConnectionService;
import de.pixart.messenger.utils.CryptoHelper;
import de.pixart.messenger.utils.Namespace;
import de.pixart.messenger.xml.Element;
+import de.pixart.messenger.xml.LocalizedContent;
import de.pixart.messenger.xmpp.InvalidJid;
import de.pixart.messenger.xmpp.OnMessagePacketReceived;
import de.pixart.messenger.xmpp.chatstate.ChatState;
@@ -430,7 +431,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
if (timestamp == null) {
timestamp = AbstractParser.parseTimestamp(original, AbstractParser.parseTimestamp(packet));
}
- final String body = packet.getBody();
+ final LocalizedContent body = packet.getBody();
final Element mucUserElement = packet.findChild("x", "http://jabber.org/protocol/muc#user");
final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted");
final Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0");
@@ -439,7 +440,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
final URL xP1S3url = xP1S3 == null ? null : P1S3UrlStreamHandler.of(xP1S3);
final String oobUrl = oob != null ? oob.findChildContent("url") : null;
final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id");
- final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX);
+ final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX); //TODO make sure we only have _one_ axolotl element!
int status;
final Jid counterpart;
final Jid to = packet.getTo();
@@ -508,10 +509,13 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, serverMsgId)) {
return;
} else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) {
- Message message = conversation.findSentMessageWithBody(packet.getBody());
- if (message != null) {
- mXmppConnectionService.markMessage(message, status);
- return;
+ LocalizedContent localizedBody = packet.getBody();
+ if (localizedBody != null) {
+ Message message = conversation.findSentMessageWithBody(localizedBody.content);
+ if (message != null) {
+ mXmppConnectionService.markMessage(message, status);
+ return;
+ }
}
}
} else {
@@ -519,15 +523,18 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
}
}
final Message message;
- if (body != null && body.startsWith("?OTR") && Config.supportOtr()) {
+ if (body.content != null && body.content.startsWith("?OTR") && Config.supportOtr()) {
if (!isForwarded && !isTypeGroupChat && isProperlyAddressed && !conversationMultiMode) {
- message = parseOtrChat(body, from, remoteMsgId, conversation);
+ message = parseOtrChat(body.content, from, remoteMsgId, conversation);
if (message == null) {
return;
}
} else {
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring OTR message from " + from + " isForwarded=" + Boolean.toString(isForwarded) + ", isProperlyAddressed=" + Boolean.valueOf(isProperlyAddressed));
- message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
+ message = new Message(conversation, body.content, Message.ENCRYPTION_NONE, status);
+ if (body.count > 1) {
+ message.setBodyLanguage(body.language);
+ }
}
} else if (xP1S3url != null) {
message = new Message(conversation, xP1S3url.toString(), Message.ENCRYPTION_NONE, status);
@@ -599,7 +606,10 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
}
} else {
- message = new Message(conversation, body, Message.ENCRYPTION_NONE, status);
+ message = new Message(conversation, body.content, Message.ENCRYPTION_NONE, status);
+ if (body.count > 1) {
+ message.setBodyLanguage(body.language);
+ }
}
message.setCounterpart(counterpart);
@@ -607,7 +617,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
message.setServerMsgId(serverMsgId);
message.setCarbon(isCarbon);
message.setTime(timestamp);
- if (body != null && body.equals(oobUrl)) {
+ if (body != null && body.content != null && body.content.equals(oobUrl)) {
message.setOob(true);
if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) {
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
@@ -814,11 +824,11 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
mXmppConnectionService.updateConversationUi();
}
if (isTypeGroupChat) {
- if (packet.hasChild("subject")) {
+ if (packet.hasChild("subject")) { //TODO usually we would want to check for lack of body; however some servers do set a body :(
if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0);
- String subject = packet.findInternationalizedChildContent("subject");
- if (conversation.getMucOptions().setSubject(subject)) {
+ final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject");
+ if (subject != null && conversation.getMucOptions().setSubject(subject.content)) {
mXmppConnectionService.updateConversation(conversation);
}
mXmppConnectionService.updateConversationUi();