From d7ab5e1a4b157b95de3c6585b38f8496fd20af93 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 21 Feb 2023 18:23:35 +0100 Subject: [PATCH] add http upload manager --- .../android/tls/TrustManager.java | 31 +++++++++++++++++++ .../android/ui/activity/MainActivity.java | 19 ++++++------ .../android/ui/model/MainViewModel.java | 2 -- .../conversations/android/xmpp/Managers.java | 6 ++++ .../android/xmpp/XmppConnection.java | 3 +- .../android/xmpp/manager/DiscoManager.java | 5 +-- .../xmpp/manager/HttpUploadManager.java | 11 +++++++ .../xmpp/manager/JingleConnectionManager.java | 16 ++++++++++ .../android/xmpp/model/error/Error.java | 12 +++++++ .../android/xmpp/model/jingle/Jingle.java | 13 ++++++++ .../android/xmpp/processor/IqProcessor.java | 9 +++++- 11 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/im/conversations/android/tls/TrustManager.java create mode 100644 app/src/main/java/im/conversations/android/xmpp/manager/HttpUploadManager.java create mode 100644 app/src/main/java/im/conversations/android/xmpp/manager/JingleConnectionManager.java create mode 100644 app/src/main/java/im/conversations/android/xmpp/model/jingle/Jingle.java diff --git a/app/src/main/java/im/conversations/android/tls/TrustManager.java b/app/src/main/java/im/conversations/android/tls/TrustManager.java new file mode 100644 index 000000000..46fac9cb7 --- /dev/null +++ b/app/src/main/java/im/conversations/android/tls/TrustManager.java @@ -0,0 +1,31 @@ +package im.conversations.android.tls; + +import android.content.Context; +import im.conversations.android.database.model.Account; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import javax.net.ssl.X509TrustManager; + +public class TrustManager implements X509TrustManager { + + private final Context context; + private final Account account; + + public TrustManager(final Context context, final Account account) { + this.context = context; + this.account = account; + } + + @Override + public void checkClientTrusted(final X509Certificate[] chain, final String authType) + throws CertificateException {} + + @Override + public void checkServerTrusted(final X509Certificate[] chain, final String authType) + throws CertificateException {} + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } +} diff --git a/app/src/main/java/im/conversations/android/ui/activity/MainActivity.java b/app/src/main/java/im/conversations/android/ui/activity/MainActivity.java index db8436210..1f3f2942c 100644 --- a/app/src/main/java/im/conversations/android/ui/activity/MainActivity.java +++ b/app/src/main/java/im/conversations/android/ui/activity/MainActivity.java @@ -3,15 +3,12 @@ package im.conversations.android.ui.activity; import android.content.Intent; import android.os.Bundle; import androidx.databinding.DataBindingUtil; -import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; - import im.conversations.android.R; import im.conversations.android.databinding.ActivityMainBinding; import im.conversations.android.service.ForegroundService; import im.conversations.android.ui.Activities; import im.conversations.android.ui.model.MainViewModel; -import im.conversations.android.ui.model.SetupViewModel; public class MainActivity extends BaseActivity { @@ -24,12 +21,16 @@ public class MainActivity extends BaseActivity { final ViewModelProvider viewModelProvider = new ViewModelProvider(this, getDefaultViewModelProviderFactory()); final var mainViewModel = viewModelProvider.get(MainViewModel.class); - mainViewModel.hasNoAccounts().observe(this, hasNoAccounts -> { - if (Boolean.TRUE.equals(hasNoAccounts)) { - startActivity(new Intent(this,SetupActivity.class)); - finish(); - } - }); + mainViewModel + .hasNoAccounts() + .observe( + this, + hasNoAccounts -> { + if (Boolean.TRUE.equals(hasNoAccounts)) { + startActivity(new Intent(this, SetupActivity.class)); + finish(); + } + }); Activities.setStatusAndNavigationBarColors(this, binding.getRoot()); } } diff --git a/app/src/main/java/im/conversations/android/ui/model/MainViewModel.java b/app/src/main/java/im/conversations/android/ui/model/MainViewModel.java index 0f4e55d3c..36e923cf0 100644 --- a/app/src/main/java/im/conversations/android/ui/model/MainViewModel.java +++ b/app/src/main/java/im/conversations/android/ui/model/MainViewModel.java @@ -1,12 +1,10 @@ package im.conversations.android.ui.model; import android.app.Application; - import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.Transformations; - import im.conversations.android.repository.AccountRepository; public class MainViewModel extends AndroidViewModel { diff --git a/app/src/main/java/im/conversations/android/xmpp/Managers.java b/app/src/main/java/im/conversations/android/xmpp/Managers.java index c720f963d..882335eac 100644 --- a/app/src/main/java/im/conversations/android/xmpp/Managers.java +++ b/app/src/main/java/im/conversations/android/xmpp/Managers.java @@ -12,6 +12,8 @@ import im.conversations.android.xmpp.manager.BookmarkManager; import im.conversations.android.xmpp.manager.CarbonsManager; import im.conversations.android.xmpp.manager.ChatStateManager; import im.conversations.android.xmpp.manager.DiscoManager; +import im.conversations.android.xmpp.manager.HttpUploadManager; +import im.conversations.android.xmpp.manager.JingleConnectionManager; import im.conversations.android.xmpp.manager.NickManager; import im.conversations.android.xmpp.manager.PepManager; import im.conversations.android.xmpp.manager.PresenceManager; @@ -36,6 +38,10 @@ public final class Managers { .put(CarbonsManager.class, new CarbonsManager(context, connection)) .put(ChatStateManager.class, new ChatStateManager(context, connection)) .put(DiscoManager.class, new DiscoManager(context, connection)) + .put(HttpUploadManager.class, new HttpUploadManager(context, connection)) + .put( + JingleConnectionManager.class, + new JingleConnectionManager(context, connection)) .put(NickManager.class, new NickManager(context, connection)) .put(PepManager.class, new PepManager(context, connection)) .put(PresenceManager.class, new PresenceManager(context, connection)) diff --git a/app/src/main/java/im/conversations/android/xmpp/XmppConnection.java b/app/src/main/java/im/conversations/android/xmpp/XmppConnection.java index 1a2552904..35c4e7113 100644 --- a/app/src/main/java/im/conversations/android/xmpp/XmppConnection.java +++ b/app/src/main/java/im/conversations/android/xmpp/XmppConnection.java @@ -1793,13 +1793,14 @@ public class XmppConnection implements Runnable { this.sendPacket(response); } - public void sendErrorFor(final Iq request, final Condition condition) { + public void sendErrorFor(final Iq request, final Error.Type type, final Condition condition) { final var from = request.getFrom(); final var id = request.getId(); final var response = new Iq(Iq.Type.ERROR); response.setTo(from); response.setId(id); final Error error = response.addExtension(new Error()); + error.setType(type); error.setCondition(condition); this.sendPacket(response); } diff --git a/app/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java b/app/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java index 7e1756004..c0bf212ed 100644 --- a/app/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java +++ b/app/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java @@ -24,6 +24,7 @@ import im.conversations.android.xmpp.model.disco.info.InfoQuery; import im.conversations.android.xmpp.model.disco.items.Item; import im.conversations.android.xmpp.model.disco.items.ItemsQuery; import im.conversations.android.xmpp.model.error.Condition; +import im.conversations.android.xmpp.model.error.Error; import im.conversations.android.xmpp.model.stanza.Iq; import im.conversations.android.xmpp.model.version.Version; import java.util.Arrays; @@ -303,7 +304,7 @@ public class DiscoManager extends AbstractManager { serviceDescription = cachedServiceDescription; } else { LOGGER.warn("No disco info was cached for node {}", nodeRequest); - connection.sendErrorFor(request, new Condition.ItemNotFound()); + connection.sendErrorFor(request, Error.Type.CANCEL, new Condition.ItemNotFound()); return; } } @@ -314,7 +315,7 @@ public class DiscoManager extends AbstractManager { public void handleVersion(final Iq request) { if (isPrivacyModeEnabled()) { - connection.sendErrorFor(request, new Condition.ServiceUnavailable()); + connection.sendErrorFor(request, Error.Type.CANCEL, new Condition.ServiceUnavailable()); } else { final var version = new Version(); version.setSoftwareName(BuildConfig.APP_NAME); diff --git a/app/src/main/java/im/conversations/android/xmpp/manager/HttpUploadManager.java b/app/src/main/java/im/conversations/android/xmpp/manager/HttpUploadManager.java new file mode 100644 index 000000000..9ef6be4bd --- /dev/null +++ b/app/src/main/java/im/conversations/android/xmpp/manager/HttpUploadManager.java @@ -0,0 +1,11 @@ +package im.conversations.android.xmpp.manager; + +import android.content.Context; +import im.conversations.android.xmpp.XmppConnection; + +public class HttpUploadManager extends AbstractManager { + + public HttpUploadManager(Context context, XmppConnection connection) { + super(context, connection); + } +} diff --git a/app/src/main/java/im/conversations/android/xmpp/manager/JingleConnectionManager.java b/app/src/main/java/im/conversations/android/xmpp/manager/JingleConnectionManager.java new file mode 100644 index 000000000..9dfa11a9f --- /dev/null +++ b/app/src/main/java/im/conversations/android/xmpp/manager/JingleConnectionManager.java @@ -0,0 +1,16 @@ +package im.conversations.android.xmpp.manager; + +import android.content.Context; + +import im.conversations.android.xmpp.XmppConnection; +import im.conversations.android.xmpp.model.stanza.Iq; + +public class JingleConnectionManager extends AbstractManager { + public JingleConnectionManager(Context context, XmppConnection connection) { + super(context, connection); + } + + public void handleJingle(Iq packet) { + + } +} diff --git a/app/src/main/java/im/conversations/android/xmpp/model/error/Error.java b/app/src/main/java/im/conversations/android/xmpp/model/error/Error.java index 87668c8ed..699092d07 100644 --- a/app/src/main/java/im/conversations/android/xmpp/model/error/Error.java +++ b/app/src/main/java/im/conversations/android/xmpp/model/error/Error.java @@ -3,6 +3,7 @@ package im.conversations.android.xmpp.model.error; import im.conversations.android.annotation.XmlElement; import im.conversations.android.xml.Namespace; import im.conversations.android.xmpp.model.Extension; +import java.util.Locale; @XmlElement(namespace = Namespace.JABBER_CLIENT) public class Error extends Extension { @@ -22,4 +23,15 @@ public class Error extends Extension { public Text getText() { return this.getExtension(Text.class); } + + public void setType(final Type type) { + this.setAttribute("type", type.toString().toLowerCase(Locale.ROOT)); + } + + public enum Type { + MODIFY, + CANCEL, + AUTH, + WAIT + } } diff --git a/app/src/main/java/im/conversations/android/xmpp/model/jingle/Jingle.java b/app/src/main/java/im/conversations/android/xmpp/model/jingle/Jingle.java new file mode 100644 index 000000000..e181ecdce --- /dev/null +++ b/app/src/main/java/im/conversations/android/xmpp/model/jingle/Jingle.java @@ -0,0 +1,13 @@ +package im.conversations.android.xmpp.model.jingle; + +import im.conversations.android.annotation.XmlElement; +import im.conversations.android.xmpp.model.Extension; + +@XmlElement +public class Jingle extends Extension { + + + public Jingle() { + super(Jingle.class); + } +} diff --git a/app/src/main/java/im/conversations/android/xmpp/processor/IqProcessor.java b/app/src/main/java/im/conversations/android/xmpp/processor/IqProcessor.java index 16b177bd6..893c03dc5 100644 --- a/app/src/main/java/im/conversations/android/xmpp/processor/IqProcessor.java +++ b/app/src/main/java/im/conversations/android/xmpp/processor/IqProcessor.java @@ -5,11 +5,14 @@ import com.google.common.base.Preconditions; import im.conversations.android.xmpp.XmppConnection; import im.conversations.android.xmpp.manager.BlockingManager; import im.conversations.android.xmpp.manager.DiscoManager; +import im.conversations.android.xmpp.manager.JingleConnectionManager; import im.conversations.android.xmpp.manager.RosterManager; import im.conversations.android.xmpp.model.blocking.Block; import im.conversations.android.xmpp.model.blocking.Unblock; import im.conversations.android.xmpp.model.disco.info.InfoQuery; import im.conversations.android.xmpp.model.error.Condition; +import im.conversations.android.xmpp.model.error.Error; +import im.conversations.android.xmpp.model.jingle.Jingle; import im.conversations.android.xmpp.model.ping.Ping; import im.conversations.android.xmpp.model.roster.Query; import im.conversations.android.xmpp.model.stanza.Iq; @@ -68,9 +71,13 @@ public class IqProcessor extends XmppConnection.Delegate implements Consumer return; } + if (type == Iq.Type.SET && packet.hasExtension(Jingle.class)) { + getManager(JingleConnectionManager.class).handleJingle(packet); + } + final var extensionIds = packet.getExtensionIds(); LOGGER.info("Received from {} type {}", packet.getFrom(), type); LOGGER.info("Could not handle {}. Sending feature-not-implemented", extensionIds); - connection.sendErrorFor(packet, new Condition.FeatureNotImplemented()); + connection.sendErrorFor(packet, Error.Type.CANCEL, new Condition.FeatureNotImplemented()); } }