Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sendMessage() api for exporting data to saved-messages #2555

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions res/raw/webxdc.js
Expand Up @@ -39,5 +39,10 @@ window.webxdc = (() => {
sendUpdate: (payload, descr) => {
InternalJSApi.sendStatusUpdate(JSON.stringify(payload), descr);
},

sendMessage: (payload) => {
payload.__blobBase64 = btoa(payload.blob) // JSON.stringify() does not handle Blob objects
InternalJSApi.sendMessage(JSON.stringify(payload));
},
};
})();
29 changes: 29 additions & 0 deletions src/org/thoughtcrime/securesms/WebxdcActivity.java
@@ -1,5 +1,7 @@
package org.thoughtcrime.securesms;

import static com.b44t.messenger.DcContext.DC_GCL_FOR_FORWARDING;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand All @@ -10,6 +12,7 @@
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -27,6 +30,7 @@
import androidx.core.graphics.drawable.IconCompat;

import com.b44t.messenger.DcChat;
import com.b44t.messenger.DcContact;
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEvent;
import com.b44t.messenger.DcMsg;
Expand All @@ -37,9 +41,12 @@
import org.thoughtcrime.securesms.util.JsonUtils;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.Prefs;
import org.thoughtcrime.securesms.util.StreamUtil;
import org.thoughtcrime.securesms.util.Util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -332,6 +339,28 @@ public boolean sendStatusUpdate(String payload, String descr) {
return true;
}

@JavascriptInterface
public boolean sendMessage(String payload) {
Log.i(TAG, "sendMessage");
try {
JSONObject jsonObject = new JSONObject(payload);
byte[] data = Base64.decode(jsonObject.getString("__blobBase64"), Base64.NO_WRAP | Base64.NO_PADDING);
String[] fileName = jsonObject.getString("fileName").split("\\.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks dangerous, maybe use https://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29 to split at most in two strings, and probably also check the case where the filename has no dot "." (no extension) at all


// consider sending the special webxdc-function with a core function
File file = File.createTempFile(fileName[0], "."+fileName[1], getCacheDir());
StreamUtil.copy(new ByteArrayInputStream(data), new FileOutputStream(file));

DcMsg msg = new DcMsg(dcContext, DcMsg.DC_MSG_FILE);
msg.setText(jsonObject.getString("text"));
msg.setFile(file.getAbsolutePath(), null);
dcContext.sendMsg(dcContext.createChatByContactId(DcContact.DC_CONTACT_ID_SELF), msg);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

@JavascriptInterface
public String getStatusUpdates(int lastKnownSerial) {
Log.i(TAG, "getStatusUpdates");
Expand Down
2 changes: 1 addition & 1 deletion src/org/thoughtcrime/securesms/WelcomeActivity.java
Expand Up @@ -283,7 +283,7 @@ private File copyToCacheDir(Uri uri) throws IOException {
try (InputStream inputStream = getContentResolver().openInputStream(uri)) {
File file = File.createTempFile(TMP_BACKUP_FILE, ".tmp", getCacheDir());
try (OutputStream outputStream = new FileOutputStream(file)) {
StreamUtil.copy(inputStream, outputStream);
StreamUtil.copy(inputStream, new FileOutputStream(file));
}
return file;
}
Expand Down