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);
final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
if (message.trusted()
&& message.treatAsDownloadable() != Message.Decision.NEVER
&& message.treatAsDownloadable()
&& manager.getAutoAcceptFileSize() > 0) {
manager.createNewDownloadConnection(message);
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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()) {

View File

@ -472,10 +472,7 @@ public class NotificationService {
private Message getFirstDownloadableMessage(final Iterable<Message> 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;
}
}

View File

@ -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);
}
}

View File

@ -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) {

View File

@ -766,7 +766,7 @@ public class MessageAdapter extends ArrayAdapter<Message> 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,

View File

@ -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 {

View File

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