feat(example): Improve the example code

This commit is contained in:
PapaTutuWawa 2023-06-18 20:59:54 +02:00
parent 9fd2daabb2
commit fbbe413148
2 changed files with 60 additions and 26 deletions

View File

@ -1,18 +1,12 @@
import 'package:args/args.dart';
import 'package:chalkdart/chalk.dart';
import 'package:cli_repl/cli_repl.dart';
import 'package:example_dart/socket.dart';
import 'package:logging/logging.dart';
import 'package:moxxmpp/moxxmpp.dart';
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
import 'package:omemo_dart/omemo_dart.dart' as omemo;
class TestingTCPSocketWrapper extends TCPSocketWrapper {
@override
bool onBadCertificate(dynamic certificate, String domain) {
return true;
}
}
void main(List<String> args) async {
// Set up logging
Logger.root.level = Level.ALL;
@ -28,9 +22,23 @@ void main(List<String> args) async {
..addOption('password')
..addOption('host')
..addOption('port')
..addOption('to');
..addOption('to')
..addOption('xmpps-srv');
final options = parser.parse(args);
// Parse a potential xmpps-client SRV record here.
// Format: --xmpps-srv <priority>,<weight>,<target>,<port>
MoxSrvRecord? srvRecord;
if (options['xmpps-srv'] != null) {
final parts = (options['xmpps-srv']! as String).split(',');
srvRecord = MoxSrvRecord(
int.parse(parts[0]),
int.parse(parts[1]),
parts[2],
int.parse(parts[3]),
);
}
// Connect
final jid = JID.fromString(options['jid']! as String);
final to = JID.fromString(options['to']! as String).toBare();
@ -39,7 +47,7 @@ void main(List<String> args) async {
TestingReconnectionPolicy(),
AlwaysConnectedConnectivityManager(),
ClientToServerNegotiator(),
TestingTCPSocketWrapper(),
ExampleTCPSocketWrapper(srvRecord),
)..connectionSettings = ConnectionSettings(
jid: jid,
password: options['password']! as String,
@ -89,13 +97,14 @@ void main(List<String> args) async {
final body = event.encryptionError != null
? chalk.red('Failed to decrypt message: ${event.encryptionError}')
: chalk.green(event.get<MessageBodyData>()?.body ?? '');
print('[${event.from.toString()}] ' + body);
print('[${event.from.toString()}] $body');
}
});
// Connect
Logger.root.info('Connecting...');
final result = await connection.connect(shouldReconnect: false, waitUntilLogin: true);
final result =
await connection.connect(shouldReconnect: false, waitUntilLogin: true);
if (!result.isType<bool>()) {
Logger.root.severe('Authentication failed!');
return;
@ -107,13 +116,16 @@ void main(List<String> args) async {
final device = await oom.getDevice();
final omemoResult = await moxxmppOmemo.publishBundle(await device.toBundle());
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>()}');
return;
}
final repl = Repl(prompt: '> ');
await for (final line in repl.runAsync()) {
await connection.getManagerById<MessageManager>(messageManager)!.sendMessage(
await connection
.getManagerById<MessageManager>(messageManager)!
.sendMessage(
to,
TypedMap<StanzaHandlerExtension>.fromList([
MessageBodyData(line),

View File

@ -0,0 +1,22 @@
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
/// A simple socket for examples that allows injection of SRV records (since
/// we cannot use moxdns here).
class ExampleTCPSocketWrapper extends TCPSocketWrapper {
ExampleTCPSocketWrapper(this.srvRecord);
/// A potential SRV record to inject for testing.
final MoxSrvRecord? srvRecord;
@override
bool onBadCertificate(dynamic certificate, String domain) {
return true;
}
@override
Future<List<MoxSrvRecord>> srvQuery(String domain, bool dnssec) async {
return [
if (srvRecord != null) srvRecord!,
];
}
}