feat(xep): Adjust to more omemo_dart changes

This commit is contained in:
PapaTutuWawa 2023-06-17 23:51:52 +02:00
parent 8252472fae
commit 9fd2daabb2
2 changed files with 43 additions and 42 deletions

View File

@ -6,24 +6,6 @@ import 'package:moxxmpp/moxxmpp.dart';
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart'; import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
import 'package:omemo_dart/omemo_dart.dart' as omemo; import 'package:omemo_dart/omemo_dart.dart' as omemo;
class TestingOmemoManager extends BaseOmemoManager {
TestingOmemoManager(this._encryptToJid);
final JID _encryptToJid;
late omemo.OmemoManager manager;
@override
Future<omemo.OmemoManager> getOmemoManager() async {
return manager;
}
@override
Future<bool> shouldEncryptStanza(JID toJid, Stanza stanza) async {
return toJid.toBare() == _encryptToJid;
}
}
class TestingTCPSocketWrapper extends TCPSocketWrapper { class TestingTCPSocketWrapper extends TCPSocketWrapper {
@override @override
bool onBadCertificate(dynamic certificate, String domain) { bool onBadCertificate(dynamic certificate, String domain) {
@ -66,17 +48,21 @@ void main(List<String> args) async {
); );
// Generate OMEMO data // Generate OMEMO data
final moxxmppOmemo = TestingOmemoManager(to); omemo.OmemoManager? oom;
final omemoManager = omemo.OmemoManager( final moxxmppOmemo = OmemoManager(
() async => oom!,
(toJid, _) async => toJid == to,
);
oom = omemo.OmemoManager(
await omemo.OmemoDevice.generateNewDevice(jid.toString(), opkAmount: 5), await omemo.OmemoDevice.generateNewDevice(jid.toString(), opkAmount: 5),
omemo.BlindTrustBeforeVerificationTrustManager(), omemo.BlindTrustBeforeVerificationTrustManager(),
moxxmppOmemo.sendEmptyMessageImpl, moxxmppOmemo.sendEmptyMessageImpl,
moxxmppOmemo.fetchDeviceList, moxxmppOmemo.fetchDeviceList,
moxxmppOmemo.fetchDeviceBundle, moxxmppOmemo.fetchDeviceBundle,
moxxmppOmemo.subscribeToDeviceListImpl, moxxmppOmemo.subscribeToDeviceListImpl,
moxxmppOmemo.publishDeviceImpl,
); );
moxxmppOmemo.manager = omemoManager; final deviceId = await oom.getDeviceId();
final deviceId = await omemoManager.getDeviceId();
Logger.root.info('Our device id: $deviceId'); Logger.root.info('Our device id: $deviceId');
// Register the managers and negotiators // Register the managers and negotiators
@ -118,7 +104,7 @@ void main(List<String> args) async {
// Publish our bundle // Publish our bundle
Logger.root.info('Publishing bundle'); Logger.root.info('Publishing bundle');
final device = await moxxmppOmemo.manager.getDevice(); final device = await oom.getDevice();
final omemoResult = await moxxmppOmemo.publishBundle(await device.toBundle()); final omemoResult = await moxxmppOmemo.publishBundle(await device.toBundle());
if (!omemoResult.isType<bool>()) { if (!omemoResult.isType<bool>()) {
Logger.root.severe('Failed to publish OMEMO bundle: ${omemoResult.get<OmemoError>()}'); Logger.root.severe('Failed to publish OMEMO bundle: ${omemoResult.get<OmemoError>()}');

View File

@ -26,6 +26,18 @@ import 'package:moxxmpp/src/xeps/xep_0384/types.dart';
import 'package:omemo_dart/omemo_dart.dart' as omemo; import 'package:omemo_dart/omemo_dart.dart' as omemo;
import 'package:xml/xml.dart'; import 'package:xml/xml.dart';
/// A callback that is executed whenever we need to acquire the OmemoManager backing
/// the manager.
typedef GetOmemoManagerCallback = Future<omemo.OmemoManager> Function();
/// A callback for figuring out whether a stanza should be encrypted or not. Note that
/// returning true here does not necessarily mean that a stanza gets encrypted because
/// handlers can indicate that a stanza should not be encrypted, e.g. PubSub.
typedef ShouldEncryptStanzaCallback = Future<bool> Function(
JID toJid,
Stanza stanza,
);
const _doNotEncryptList = [ const _doNotEncryptList = [
// XEP-0033 // XEP-0033
DoNotEncrypt('addresses', extendedAddressingXmlns), DoNotEncrypt('addresses', extendedAddressingXmlns),
@ -42,8 +54,15 @@ const _doNotEncryptList = [
DoNotEncrypt('stanza-id', stableIdXmlns), DoNotEncrypt('stanza-id', stableIdXmlns),
]; ];
abstract class BaseOmemoManager extends XmppManagerBase { class OmemoManager extends XmppManagerBase {
BaseOmemoManager() : super(omemoManager); OmemoManager(this._getOmemoManager, this._shouldEncryptStanza)
: super(omemoManager);
/// Callback for getting the [omemo.OmemoManager].
final GetOmemoManagerCallback _getOmemoManager;
/// Callback for checking whether a stanza should be encrypted or not.
final ShouldEncryptStanzaCallback _shouldEncryptStanza;
// TODO(Unknown): Technically, this is not always true // TODO(Unknown): Technically, this is not always true
@override @override
@ -112,22 +131,19 @@ abstract class BaseOmemoManager extends XmppManagerBase {
} }
// Tell the OmemoManager // Tell the OmemoManager
await (await getOmemoManager()).onDeviceListUpdate(jid.toString(), ids); await (await _getOmemoManager()).onDeviceListUpdate(jid.toString(), ids);
// Generate an event // Generate an event
getAttributes().sendEvent(OmemoDeviceListUpdatedEvent(jid, ids)); getAttributes().sendEvent(OmemoDeviceListUpdatedEvent(jid, ids));
} }
} }
@visibleForOverriding
Future<omemo.OmemoManager> getOmemoManager();
/// Wrapper around using getSessionManager and then calling getDeviceId on it. /// Wrapper around using getSessionManager and then calling getDeviceId on it.
Future<int> _getDeviceId() async => (await getOmemoManager()).getDeviceId(); Future<int> _getDeviceId() async => (await _getOmemoManager()).getDeviceId();
/// Wrapper around using getSessionManager and then calling getDeviceId on it. /// Wrapper around using getSessionManager and then calling getDeviceId on it.
Future<omemo.OmemoBundle> _getDeviceBundle() async { Future<omemo.OmemoBundle> _getDeviceBundle() async {
final om = await getOmemoManager(); final om = await _getOmemoManager();
final device = await om.getDevice(); final device = await om.getDevice();
return device.toBundle(); return device.toBundle();
} }
@ -279,7 +295,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
/// Send a heartbeat message to [jid]. /// Send a heartbeat message to [jid].
Future<void> sendOmemoHeartbeat(String jid) async { Future<void> sendOmemoHeartbeat(String jid) async {
final om = await getOmemoManager(); final om = await _getOmemoManager();
await om.sendOmemoHeartbeat(jid); await om.sendOmemoHeartbeat(jid);
} }
@ -320,7 +336,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
} }
final toJid = JID.fromString(stanza.to!).toBare(); final toJid = JID.fromString(stanza.to!).toBare();
final shouldEncryptResult = await shouldEncryptStanza(toJid, stanza); final shouldEncryptResult = await _shouldEncryptStanza(toJid, stanza);
if (!shouldEncryptResult && !state.forceEncryption) { if (!shouldEncryptResult && !state.forceEncryption) {
logger.finest( logger.finest(
'Not encrypting stanza for $toJid: Both shouldEncryptStanza and forceEncryption are false.', 'Not encrypting stanza for $toJid: Both shouldEncryptStanza and forceEncryption are false.',
@ -347,7 +363,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
.getManagerById<CarbonsManager>(carbonsManager) .getManagerById<CarbonsManager>(carbonsManager)
?.isEnabled ?? ?.isEnabled ??
false; false;
final om = await getOmemoManager(); final om = await _getOmemoManager();
final encryptToJids = [ final encryptToJids = [
toJid.toString(), toJid.toString(),
if (carbonsEnabled) getAttributes().getFullJID().toBare().toString(), if (carbonsEnabled) getAttributes().getFullJID().toBare().toString(),
@ -397,12 +413,6 @@ abstract class BaseOmemoManager extends XmppManagerBase {
..encrypted = true; ..encrypted = true;
} }
/// This function is called whenever a message is to be encrypted. If it returns true,
/// then the message will be encrypted. If it returns false, the message won't be
/// encrypted.
@visibleForOverriding
Future<bool> shouldEncryptStanza(JID toJid, Stanza stanza);
Future<StanzaHandlerData> _onIncomingStanza( Future<StanzaHandlerData> _onIncomingStanza(
Stanza stanza, Stanza stanza,
StanzaHandlerData state, StanzaHandlerData state,
@ -434,7 +444,7 @@ abstract class BaseOmemoManager extends XmppManagerBase {
} }
final sid = int.parse(header.attributes['sid']! as String); final sid = int.parse(header.attributes['sid']! as String);
final om = await getOmemoManager(); final om = await _getOmemoManager();
final result = await om.onIncomingStanza( final result = await om.onIncomingStanza(
omemo.OmemoIncomingStanza( omemo.OmemoIncomingStanza(
fromJid.toString(), fromJid.toString(),
@ -644,6 +654,11 @@ abstract class BaseOmemoManager extends XmppManagerBase {
await pm.subscribe(JID.fromString(jid), omemoDevicesXmlns); await pm.subscribe(JID.fromString(jid), omemoDevicesXmlns);
} }
/// Implementation for publishing our device [device].
Future<void> publishDeviceImpl(omemo.OmemoDevice device) async {
await publishBundle(await device.toBundle());
}
/// Attempts to find out if [jid] supports omemo:2. /// Attempts to find out if [jid] supports omemo:2.
/// ///
/// On success, returns whether [jid] has published a device list and device bundles. /// On success, returns whether [jid] has published a device list and device bundles.