treat URL as file if URL is in oob or contains key

This commit is contained in:
Daniel Gultsch 2017-04-05 22:32:02 +02:00
parent 26badb7f4c
commit 99565a6876
10 changed files with 18 additions and 63 deletions

View File

@ -123,7 +123,7 @@ public class PgpDecryptionService {
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager(); final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
if (message.trusted() if (message.trusted()
&& message.treatAsDownloadable() != Message.Decision.NEVER && message.treatAsDownloadable()
&& manager.getAutoAcceptFileSize() > 0) { && manager.getAutoAcceptFileSize() > 0) {
manager.createNewDownloadConnection(message); manager.createNewDownloadConnection(message);
} }

View File

@ -167,7 +167,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
for (final Message message : this.messages) { for (final Message message : this.messages) {
if (message.getUuid().equals(uuid) if (message.getUuid().equals(uuid)
&& message.getEncryption() != Message.ENCRYPTION_PGP && 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; return message;
} }
} }

View File

@ -492,8 +492,8 @@ public class Message extends AbstractEntity {
this.getBody().length() + message.getBody().length() <= Config.MAX_DISPLAY_MESSAGE_CHARS && this.getBody().length() + message.getBody().length() <= Config.MAX_DISPLAY_MESSAGE_CHARS &&
!GeoHelper.isGeoUri(message.getBody()) && !GeoHelper.isGeoUri(message.getBody()) &&
!GeoHelper.isGeoUri(this.body) && !GeoHelper.isGeoUri(this.body) &&
message.treatAsDownloadable() == Decision.NEVER && !message.treatAsDownloadable() &&
this.treatAsDownloadable() == Decision.NEVER && !this.treatAsDownloadable() &&
!message.getBody().startsWith(ME_COMMAND) && !message.getBody().startsWith(ME_COMMAND) &&
!this.getBody().startsWith(ME_COMMAND) && !this.getBody().startsWith(ME_COMMAND) &&
!this.bodyIsHeart() && !this.bodyIsHeart() &&
@ -603,12 +603,6 @@ public class Message extends AbstractEntity {
this.oob = isOob; this.oob = isOob;
} }
public enum Decision {
MUST,
SHOULD,
NEVER,
}
private static String extractRelevantExtension(URL url) { private static String extractRelevantExtension(URL url) {
String path = url.getPath(); String path = url.getPath();
return extractRelevantExtension(path); return extractRelevantExtension(path);
@ -651,38 +645,20 @@ public class Message extends AbstractEntity {
} }
} }
public Decision treatAsDownloadable() { public boolean treatAsDownloadable() {
if (body.trim().contains(" ")) { if (body.trim().contains(" ")) {
return Decision.NEVER; return false;
} }
try { try {
URL url = new URL(body); final URL url = new URL(body);
String ref = url.getRef(); final String ref = url.getRef();
final String protocol = url.getProtocol(); final String protocol = url.getProtocol();
final boolean encrypted = ref != null && ref.matches("([A-Fa-f0-9]{2}){48}"); final boolean encrypted = ref != null && ref.matches("([A-Fa-f0-9]{2}){48}");
if (AesGcmURLStreamHandler.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted) { return (AesGcmURLStreamHandler.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted)
return Decision.MUST; || ((protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) && (oob || encrypted));
}
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;
}
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
return Decision.NEVER; return false;
} }
} }

View File

@ -619,7 +619,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
mXmppConnectionService.databaseBackend.createMessage(message); mXmppConnectionService.databaseBackend.createMessage(message);
final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager(); 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); manager.createNewDownloadConnection(message);
} else if (notify) { } else if (notify) {
if (query != null && query.isCatchup()) { if (query != null && query.isCatchup()) {

View File

@ -472,10 +472,7 @@ public class NotificationService {
private Message getFirstDownloadableMessage(final Iterable<Message> messages) { private Message getFirstDownloadableMessage(final Iterable<Message> messages) {
for (final Message message : messages) { for (final Message message : messages) {
if (message.getTransferable() != null if (message.getTransferable() != null || (message.getType() == Message.TYPE_TEXT && message.treatAsDownloadable())) {
&& (message.getType() == Message.TYPE_FILE
|| message.getType() == Message.TYPE_IMAGE
|| message.treatAsDownloadable() != Message.Decision.NEVER)) {
return message; return message;
} }
} }

View File

@ -665,7 +665,7 @@ public class ConversationActivity extends XmppActivity
if (!transferable.start()) { if (!transferable.start()) {
Toast.makeText(this, R.string.not_connected_try_again, Toast.LENGTH_SHORT).show(); 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); xmppConnectionService.getHttpConnectionManager().createNewDownloadConnection(message, true);
} }
} }

View File

@ -609,13 +609,12 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
MenuItem shareWith = menu.findItem(R.id.share_with); MenuItem shareWith = menu.findItem(R.id.share_with);
MenuItem sendAgain = menu.findItem(R.id.send_again); MenuItem sendAgain = menu.findItem(R.id.send_again);
MenuItem copyUrl = menu.findItem(R.id.copy_url); 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 cancelTransmission = menu.findItem(R.id.cancel_transmission);
MenuItem deleteFile = menu.findItem(R.id.delete_file); MenuItem deleteFile = menu.findItem(R.id.delete_file);
MenuItem showErrorMessage = menu.findItem(R.id.show_error_message); MenuItem showErrorMessage = menu.findItem(R.id.show_error_message);
if (!treatAsFile if (!treatAsFile
&& !GeoHelper.isGeoUri(m.getBody()) && !GeoHelper.isGeoUri(m.getBody())
&& m.treatAsDownloadable() != Message.Decision.MUST) { && !m.treatAsDownloadable()) {
copyText.setVisible(true); copyText.setVisible(true);
selectText.setVisible(ListSelectionManager.isSupported()); selectText.setVisible(ListSelectionManager.isSupported());
} }
@ -635,15 +634,10 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
} }
if (m.hasFileOnRemoteHost() if (m.hasFileOnRemoteHost()
|| GeoHelper.isGeoUri(m.getBody()) || GeoHelper.isGeoUri(m.getBody())
|| m.treatAsDownloadable() == Message.Decision.MUST || m.treatAsDownloadable()
|| (t != null && t instanceof HttpDownloadConnection)) { || (t != null && t instanceof HttpDownloadConnection)) {
copyUrl.setVisible(true); 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 boolean waitingOfferedSending = m.getStatus() == Message.STATUS_WAITING
|| m.getStatus() == Message.STATUS_UNSEND || m.getStatus() == Message.STATUS_UNSEND
|| m.getStatus() == Message.STATUS_OFFERED; || m.getStatus() == Message.STATUS_OFFERED;
@ -684,9 +678,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
case R.id.copy_url: case R.id.copy_url:
copyUrl(selectedMessage); copyUrl(selectedMessage);
return true; return true;
case R.id.download_file:
downloadFile(selectedMessage);
return true;
case R.id.cancel_transmission: case R.id.cancel_transmission:
cancelTransmission(selectedMessage); cancelTransmission(selectedMessage);
return true; 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) { private void cancelTransmission(Message message) {
Transferable transferable = message.getTransferable(); Transferable transferable = message.getTransferable();
if (transferable != null) { if (transferable != null) {

View File

@ -766,7 +766,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
displayLocationMessage(viewHolder,message); displayLocationMessage(viewHolder,message);
} else if (message.bodyIsHeart()) { } else if (message.bodyIsHeart()) {
displayHeartMessage(viewHolder, message.getBody().trim()); displayHeartMessage(viewHolder, message.getBody().trim());
} else if (message.treatAsDownloadable() == Message.Decision.MUST) { } else if (message.treatAsDownloadable()) {
try { try {
URL url = new URL(message.getBody()); URL url = new URL(message.getBody());
displayDownloadableMessage(viewHolder, displayDownloadableMessage(viewHolder,

View File

@ -196,7 +196,7 @@ public class UIHelper {
} else { } else {
return new Pair<>(context.getString(R.string.location), true); 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, return new Pair<>(context.getString(R.string.x_file_offered_for_download,
getFileDescriptionString(context,message)),true); getFileDescriptionString(context,message)),true);
} else { } else {

View File

@ -33,10 +33,6 @@
android:id="@+id/send_again" android:id="@+id/send_again"
android:title="@string/send_again" android:title="@string/send_again"
android:visible="false"/> android:visible="false"/>
<item
android:id="@+id/download_file"
android:title="@string/download_x_file"
android:visible="false"/>
<item <item
android:id="@+id/cancel_transmission" android:id="@+id/cancel_transmission"
android:title="@string/cancel_transmission" android:title="@string/cancel_transmission"