From 99565a68764ede1157d9df4cd92e7a7deb624541 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 5 Apr 2017 22:32:02 +0200 Subject: [PATCH] treat URL as file if URL is in oob or contains key --- .../crypto/PgpDecryptionService.java | 2 +- .../conversations/entities/Conversation.java | 2 +- .../siacs/conversations/entities/Message.java | 42 ++++--------------- .../conversations/parser/MessageParser.java | 2 +- .../services/NotificationService.java | 5 +-- .../ui/ConversationActivity.java | 2 +- .../ui/ConversationFragment.java | 18 +------- .../ui/adapter/MessageAdapter.java | 2 +- .../siacs/conversations/utils/UIHelper.java | 2 +- src/main/res/menu/message_context.xml | 4 -- 10 files changed, 18 insertions(+), 63 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java b/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java index 87b58613e..53aa3e9f4 100644 --- a/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java +++ b/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java @@ -123,7 +123,7 @@ public class PgpDecryptionService { message.setEncryption(Message.ENCRYPTION_DECRYPTED); final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager(); if (message.trusted() - && message.treatAsDownloadable() != Message.Decision.NEVER + && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) { manager.createNewDownloadConnection(message); } diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index edba9cf3c..b7faccecc 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -167,7 +167,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl for (final Message message : this.messages) { if (message.getUuid().equals(uuid) && message.getEncryption() != Message.ENCRYPTION_PGP - && (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE || message.treatAsDownloadable() != Message.Decision.NEVER)) { + && (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE || message.treatAsDownloadable())) { 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 318c19806..c433f9c95 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -492,8 +492,8 @@ public class Message extends AbstractEntity { this.getBody().length() + message.getBody().length() <= Config.MAX_DISPLAY_MESSAGE_CHARS && !GeoHelper.isGeoUri(message.getBody()) && !GeoHelper.isGeoUri(this.body) && - message.treatAsDownloadable() == Decision.NEVER && - this.treatAsDownloadable() == Decision.NEVER && + !message.treatAsDownloadable() && + !this.treatAsDownloadable() && !message.getBody().startsWith(ME_COMMAND) && !this.getBody().startsWith(ME_COMMAND) && !this.bodyIsHeart() && @@ -603,12 +603,6 @@ public class Message extends AbstractEntity { this.oob = isOob; } - public enum Decision { - MUST, - SHOULD, - NEVER, - } - private static String extractRelevantExtension(URL url) { String path = url.getPath(); return extractRelevantExtension(path); @@ -651,38 +645,20 @@ public class Message extends AbstractEntity { } } - public Decision treatAsDownloadable() { + public boolean treatAsDownloadable() { if (body.trim().contains(" ")) { - return Decision.NEVER; + return false; } try { - URL url = new URL(body); - String ref = url.getRef(); + final URL url = new URL(body); + final String ref = url.getRef(); final String protocol = url.getProtocol(); final boolean encrypted = ref != null && ref.matches("([A-Fa-f0-9]{2}){48}"); - if (AesGcmURLStreamHandler.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted) { - return Decision.MUST; - } - if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) { - return Decision.NEVER; - } else if (oob || encrypted) { - return Decision.MUST; - } - final String extension = extractRelevantExtension(url); - - if (extension == null - || encryption == Message.ENCRYPTION_OTR - || encryption == Message.ENCRYPTION_AXOLOTL) { - return Decision.NEVER; - } else if (Transferable.VALID_IMAGE_EXTENSIONS.contains(extension) - || Transferable.WELL_KNOWN_EXTENSIONS.contains(extension)) { - return Decision.SHOULD; - } else { - return Decision.NEVER; - } + return (AesGcmURLStreamHandler.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted) + || ((protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) && (oob || encrypted)); } catch (MalformedURLException e) { - return Decision.NEVER; + return false; } } diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 13d932499..8266e3821 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -619,7 +619,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece mXmppConnectionService.databaseBackend.createMessage(message); final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager(); - if (message.trusted() && message.treatAsDownloadable() != Message.Decision.NEVER && manager.getAutoAcceptFileSize() > 0) { + if (message.trusted() && message.treatAsDownloadable() && manager.getAutoAcceptFileSize() > 0) { manager.createNewDownloadConnection(message); } else if (notify) { if (query != null && query.isCatchup()) { diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index 7dee883a5..b8271e7d2 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -472,10 +472,7 @@ public class NotificationService { private Message getFirstDownloadableMessage(final Iterable messages) { for (final Message message : messages) { - if (message.getTransferable() != null - && (message.getType() == Message.TYPE_FILE - || message.getType() == Message.TYPE_IMAGE - || message.treatAsDownloadable() != Message.Decision.NEVER)) { + if (message.getTransferable() != null || (message.getType() == Message.TYPE_TEXT && message.treatAsDownloadable())) { return message; } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 2765d332a..6384ad122 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -665,7 +665,7 @@ public class ConversationActivity extends XmppActivity if (!transferable.start()) { Toast.makeText(this, R.string.not_connected_try_again, Toast.LENGTH_SHORT).show(); } - } else if (message.treatAsDownloadable() != Message.Decision.NEVER) { + } else if (message.treatAsDownloadable()) { xmppConnectionService.getHttpConnectionManager().createNewDownloadConnection(message, true); } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 89f210906..dff6bce6e 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -609,13 +609,12 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa MenuItem shareWith = menu.findItem(R.id.share_with); MenuItem sendAgain = menu.findItem(R.id.send_again); MenuItem copyUrl = menu.findItem(R.id.copy_url); - MenuItem downloadFile = menu.findItem(R.id.download_file); MenuItem cancelTransmission = menu.findItem(R.id.cancel_transmission); MenuItem deleteFile = menu.findItem(R.id.delete_file); MenuItem showErrorMessage = menu.findItem(R.id.show_error_message); if (!treatAsFile && !GeoHelper.isGeoUri(m.getBody()) - && m.treatAsDownloadable() != Message.Decision.MUST) { + && !m.treatAsDownloadable()) { copyText.setVisible(true); selectText.setVisible(ListSelectionManager.isSupported()); } @@ -635,15 +634,10 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } if (m.hasFileOnRemoteHost() || GeoHelper.isGeoUri(m.getBody()) - || m.treatAsDownloadable() == Message.Decision.MUST + || m.treatAsDownloadable() || (t != null && t instanceof HttpDownloadConnection)) { copyUrl.setVisible(true); } - if ((m.getType() == Message.TYPE_TEXT && t == null && m.treatAsDownloadable() != Message.Decision.NEVER) - || (m.isFileOrImage() && t instanceof TransferablePlaceholder && m.hasFileOnRemoteHost())){ - downloadFile.setVisible(true); - downloadFile.setTitle(activity.getString(R.string.download_x_file,UIHelper.getFileDescriptionString(activity, m))); - } boolean waitingOfferedSending = m.getStatus() == Message.STATUS_WAITING || m.getStatus() == Message.STATUS_UNSEND || m.getStatus() == Message.STATUS_OFFERED; @@ -684,9 +678,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa case R.id.copy_url: copyUrl(selectedMessage); return true; - case R.id.download_file: - downloadFile(selectedMessage); - return true; case R.id.cancel_transmission: cancelTransmission(selectedMessage); return true; @@ -808,11 +799,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } } - private void downloadFile(Message message) { - activity.xmppConnectionService.getHttpConnectionManager() - .createNewDownloadConnection(message,true); - } - private void cancelTransmission(Message message) { Transferable transferable = message.getTransferable(); if (transferable != null) { 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 f67726d5a..99475bbb0 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -766,7 +766,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie displayLocationMessage(viewHolder,message); } else if (message.bodyIsHeart()) { displayHeartMessage(viewHolder, message.getBody().trim()); - } else if (message.treatAsDownloadable() == Message.Decision.MUST) { + } else if (message.treatAsDownloadable()) { try { URL url = new URL(message.getBody()); displayDownloadableMessage(viewHolder, diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index f1f8d0066..94ae13a8c 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -196,7 +196,7 @@ public class UIHelper { } else { return new Pair<>(context.getString(R.string.location), true); } - } else if (message.treatAsDownloadable() == Message.Decision.MUST) { + } else if (message.treatAsDownloadable()) { return new Pair<>(context.getString(R.string.x_file_offered_for_download, getFileDescriptionString(context,message)),true); } else { diff --git a/src/main/res/menu/message_context.xml b/src/main/res/menu/message_context.xml index 283d3f2fc..3ba14e084 100644 --- a/src/main/res/menu/message_context.xml +++ b/src/main/res/menu/message_context.xml @@ -33,10 +33,6 @@ android:id="@+id/send_again" android:title="@string/send_again" android:visible="false"/> -