From 946d97074f09cabe4c644e7aa9a9d35839752269 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 11 Dec 2017 15:55:10 +0100 Subject: [PATCH] try to detect invalid nick (w/ emoji) in MUCs --- .../conversations/entities/MucOptions.java | 1 + .../conversations/parser/PresenceParser.java | 47 +++++++++++++------ .../ui/ConversationFragment.java | 2 + src/main/res/values/strings.xml | 1 + 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/MucOptions.java b/src/main/java/eu/siacs/conversations/entities/MucOptions.java index 8414fd999..6bb6f3120 100644 --- a/src/main/java/eu/siacs/conversations/entities/MucOptions.java +++ b/src/main/java/eu/siacs/conversations/entities/MucOptions.java @@ -141,6 +141,7 @@ public class MucOptions { MEMBERS_ONLY, KICKED, SHUTDOWN, + INVALID_NICK, UNKNOWN } diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index eec6bd140..0ed6ca6ad 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -72,10 +72,7 @@ public class PresenceParser extends AbstractParser implements || ((codes.isEmpty() || codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED)) && jid.equals(item.getAttributeAsJid("jid")))) { mucOptions.setOnline(); mucOptions.setSelf(user); - if (mucOptions.onRenameListener != null) { - mucOptions.onRenameListener.onSuccess(); - mucOptions.onRenameListener = null; - } + invokeRenameListener(mucOptions, true); } boolean isNew = mucOptions.updateUser(user); final AxolotlService axolotlService = conversation.getAccount().getAxolotlService(); @@ -143,27 +140,50 @@ public class PresenceParser extends AbstractParser implements } } } else if (type.equals("error")) { - Element error = packet.findChild("error"); - if (error != null && error.hasChild("conflict")) { + final Element error = packet.findChild("error"); + if (error == null) { + return; + } + if (error.hasChild("conflict")) { if (mucOptions.online()) { - if (mucOptions.onRenameListener != null) { - mucOptions.onRenameListener.onFailure(); - mucOptions.onRenameListener = null; - } + invokeRenameListener(mucOptions, false); } else { mucOptions.setError(MucOptions.Error.NICK_IN_USE); } - } else if (error != null && error.hasChild("not-authorized")) { + } else if (error.hasChild("not-authorized")) { mucOptions.setError(MucOptions.Error.PASSWORD_REQUIRED); - } else if (error != null && error.hasChild("forbidden")) { + } else if (error.hasChild("forbidden")) { mucOptions.setError(MucOptions.Error.BANNED); - } else if (error != null && error.hasChild("registration-required")) { + } else if (error.hasChild("registration-required")) { mucOptions.setError(MucOptions.Error.MEMBERS_ONLY); + } else { + final String text = error.findChildContent("text"); + if (text != null && text.contains("attribute 'to'")) { + if (mucOptions.online()) { + invokeRenameListener(mucOptions, false); + } else { + mucOptions.setError(MucOptions.Error.INVALID_NICK); + } + } else { + mucOptions.setError(MucOptions.Error.UNKNOWN); + Log.d(Config.LOGTAG, "unknown error in conference: " + packet); + } } } } } + private static void invokeRenameListener(final MucOptions options, boolean success) { + if (options.onRenameListener != null) { + if (success) { + options.onRenameListener.onSuccess(); + } else { + options.onRenameListener.onFailure(); + } + options.onRenameListener = null; + } + } + private static List getStatusCodes(Element x) { List codes = new ArrayList<>(); if (x != null) { @@ -292,5 +312,4 @@ public class PresenceParser extends AbstractParser implements this.parseContactPresence(packet, account); } } - } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 9bd90902f..2c47b7d16 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -1109,6 +1109,8 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa case UNKNOWN: showSnackbar(R.string.conference_unknown_error, R.string.try_again, joinMuc); break; + case INVALID_NICK: + showSnackbar(R.string.invalid_muc_nick, R.string.edit, clickToMuc); case SHUTDOWN: showSnackbar(R.string.conference_shutdown, R.string.try_again, joinMuc); break; diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 98588824a..834870e7e 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -30,6 +30,7 @@ Decrypting message. Please wait… OpenPGP encrypted message Nickname is already in use + Invalid nickname Admin Owner Moderator