diff options
author | lookshe <github@lookshe.org> | 2016-03-12 02:28:22 +0100 |
---|---|---|
committer | lookshe <github@lookshe.org> | 2016-03-12 02:28:22 +0100 |
commit | bf49abe865ca746fad452a759f3792ca322e1fce (patch) | |
tree | 89c57470ffb1bd4cf12364ccab8814156e0fa3f8 /src | |
parent | 3041cc74cefb4a25f4d1af7109fcb141ec3293d8 (diff) |
Added method to get body from message if hasMeCommand and harmonized extraction of fileextension (FS#38)
Diffstat (limited to '')
6 files changed, 67 insertions, 44 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index 4a9e83a3..62f976b3 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -759,7 +759,7 @@ public class Conversation extends AbstractEntity implements Blockable { if (message.hasFileOnRemoteHost()) { otherBody = message.getFileParams().url.toString(); } else { - otherBody = message.body; + otherBody = message.getBody(); } if (otherBody != null && otherBody.equals(body)) { return message; diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index f463da54..c377ed4a 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -62,7 +62,7 @@ public class Message extends AbstractEntity { protected String conversationUuid; protected Jid counterpart; protected Jid trueCounterpart; - protected String body; + private String body; protected String encryptedBody; protected long timeSent; protected int encryption; @@ -417,6 +417,14 @@ public class Message extends AbstractEntity { return getBody().startsWith(ME_COMMAND); } + public String getBodyReplacedMeCommand(String replaceString) { + try { + return getBody().replaceAll("^" + Message.ME_COMMAND, replaceString + " "); + } catch (ArrayIndexOutOfBoundsException e) { + return getBody(); + } + } + public boolean trusted() { Contact contact = this.getContact(); return (status > STATUS_RECEIVED || (contact != null && contact.trusted())); @@ -460,26 +468,31 @@ public class Message extends AbstractEntity { NEVER, } - private static String extractRelevantExtension(URL url) { + private String extractRelevantExtension(URL url) { String path = url.getPath(); return extractRelevantExtension(path); } - private static String extractRelevantExtension(String path) { + private String extractRelevantExtension(String path) { if (path == null || path.isEmpty()) { return null; } String filename = path.substring(path.lastIndexOf('/') + 1).toLowerCase(); - int dotPosition = filename.lastIndexOf("."); - if (dotPosition != -1) { - String extension = filename.substring(dotPosition + 1); + final int lastDotPosition = filename.lastIndexOf('.'); + final int secondToLastDotPosition = filename.lastIndexOf('.', lastDotPosition - 1); + final String lastPart = lastDotPosition != -1 ? + filename.substring(lastDotPosition + 1) : ""; + final String secondToLastPart = secondToLastDotPosition != -1 ? + filename.substring(secondToLastDotPosition + 1, lastDotPosition - 1) : ""; + + if (lastDotPosition != -1) { // we want the real file extension, not the crypto one - if (Transferable.VALID_CRYPTO_EXTENSIONS.contains(extension)) { - return extractRelevantExtension(filename.substring(0,dotPosition)); + if (secondToLastDotPosition != -1 && Transferable.VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { + return secondToLastPart; } else { - return extension; + return lastPart; } } return null; diff --git a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java index cdc5d2e7..c8076811 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java @@ -82,22 +82,27 @@ public class HttpDownloadConnection implements Transferable { } else { mUrl = new URL(message.getBody()); } - String[] parts = mUrl.getPath().toLowerCase().split("\\."); - String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null; - String secondToLast = parts.length >= 2 ? parts[parts.length -2] : null; - if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) { + final String sUrlFilename = mUrl.getPath().substring(mUrl.getPath().lastIndexOf('/')).toLowerCase(); + final int lastDotPosition = sUrlFilename.lastIndexOf('.'); + final int secondToLastDotPosition = sUrlFilename.lastIndexOf('.', lastDotPosition - 1); + final String lastPart = lastDotPosition != -1 ? + sUrlFilename.substring(lastDotPosition + 1) : ""; + final String secondToLastPart = secondToLastDotPosition != -1 ? + sUrlFilename.substring(secondToLastDotPosition + 1, lastDotPosition - 1) : ""; + + if (!lastPart.isEmpty() && ("pgp".equals(lastPart) || "gpg".equals(lastPart))) { this.message.setEncryption(Message.ENCRYPTION_PGP); } else if (message.getEncryption() != Message.ENCRYPTION_OTR && message.getEncryption() != Message.ENCRYPTION_AXOLOTL) { this.message.setEncryption(Message.ENCRYPTION_NONE); } String extension; - if (VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { - extension = secondToLast; + if (!lastPart.isEmpty() && VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { + extension = secondToLastPart; } else { extension = lastPart; } - message.setRelativeFilePath(message.getUuid()+"."+extension); + message.setRelativeFilePath(message.getUuid() + "." + extension); this.file = FileBackend.getFile(message, false); String reference = mUrl.getRef(); if (reference != null && reference.length() == 96) { diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index b130df9b..b5d1272c 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -281,9 +281,9 @@ public class MessageAdapter extends ArrayAdapter<Message> { if (message.getBody() != null) { final String nick = UIHelper.getMessageDisplayName(message); String body; - try { - body = message.getBody().replaceAll("^" + Message.ME_COMMAND, nick + " "); - } catch (ArrayIndexOutOfBoundsException e) { + if (message.hasMeCommand()) { + body = message.getBodyReplacedMeCommand(nick); + } else { body = message.getBody(); } final SpannableString formattedBody = new SpannableString(body); diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index 3d091e39..e9dbba31 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -17,7 +17,6 @@ import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Presence; -import eu.siacs.conversations.entities.Presences; import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.xmpp.jid.Jid; @@ -179,9 +178,8 @@ public class UIHelper { return new Pair<>(getFileDescriptionString(context,message),true); } } else { - if (message.getBody().startsWith(Message.ME_COMMAND)) { - return new Pair<>(message.getBody().replaceAll("^" + Message.ME_COMMAND, - UIHelper.getMessageDisplayName(message) + " "), false); + if (message.hasMeCommand()) { + return new Pair<>(message.getBodyReplacedMeCommand(UIHelper.getMessageDisplayName(message)), false); } else if (GeoHelper.isGeoUri(message.getBody())) { if (message.getStatus() == Message.STATUS_RECEIVED) { return new Pair<>(context.getString(R.string.received_location), true); diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index 8ab0fe8a..147d53e2 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -323,27 +323,34 @@ public class JingleConnection implements Transferable { Element fileSize = fileOffer.findChild("size"); Element fileNameElement = fileOffer.findChild("name"); if (fileNameElement != null) { - String[] filename = fileNameElement.getContent() - .toLowerCase(Locale.US).toLowerCase().split("\\."); - String extension = filename[filename.length - 1]; - if (VALID_IMAGE_EXTENSIONS.contains(extension)) { - message.setType(Message.TYPE_IMAGE); - message.setRelativeFilePath(message.getUuid()+"."+extension); - } else if (VALID_CRYPTO_EXTENSIONS.contains( - filename[filename.length - 1])) { - if (filename.length == 3) { - extension = filename[filename.length - 2]; - if (VALID_IMAGE_EXTENSIONS.contains(extension)) { - message.setType(Message.TYPE_IMAGE); - message.setRelativeFilePath(message.getUuid()+"."+extension); - } else { - message.setType(Message.TYPE_FILE); - } - if (filename[filename.length - 1].equals("otr")) { - message.setEncryption(Message.ENCRYPTION_OTR); - } else { - message.setEncryption(Message.ENCRYPTION_PGP); + String filename = fileNameElement.getContent() + .toLowerCase(Locale.US).toLowerCase(); + final int lastDotPosition = filename.lastIndexOf('.'); + final int secondToLastDotPosition = filename.lastIndexOf('.', lastDotPosition - 1); + final String lastPart = lastDotPosition != -1 ? + filename.substring(lastDotPosition + 1) : ""; + final String secondToLastPart = secondToLastDotPosition != -1 ? + filename.substring(secondToLastDotPosition + 1, lastDotPosition - 1) : ""; + if (lastDotPosition != -1) { + if (VALID_IMAGE_EXTENSIONS.contains(lastPart)) { + message.setType(Message.TYPE_IMAGE); + message.setRelativeFilePath(message.getUuid()+"."+lastPart); + } else if (VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { + if (secondToLastDotPosition != -1) { + if (VALID_IMAGE_EXTENSIONS.contains(secondToLastPart)) { + message.setType(Message.TYPE_IMAGE); + message.setRelativeFilePath(message.getUuid()+"."+secondToLastPart); + } else { + message.setType(Message.TYPE_FILE); + } + if (lastPart.equals("otr")) { + message.setEncryption(Message.ENCRYPTION_OTR); + } else { + message.setEncryption(Message.ENCRYPTION_PGP); + } } + } else { + message.setType(Message.TYPE_FILE); } } else { message.setType(Message.TYPE_FILE); |