feat(example): Improve the example code
This commit is contained in:
parent
9fd2daabb2
commit
fbbe413148
@ -1,18 +1,12 @@
|
|||||||
import 'package:args/args.dart';
|
import 'package:args/args.dart';
|
||||||
import 'package:chalkdart/chalk.dart';
|
import 'package:chalkdart/chalk.dart';
|
||||||
import 'package:cli_repl/cli_repl.dart';
|
import 'package:cli_repl/cli_repl.dart';
|
||||||
|
import 'package:example_dart/socket.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:moxxmpp/moxxmpp.dart';
|
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 TestingTCPSocketWrapper extends TCPSocketWrapper {
|
|
||||||
@override
|
|
||||||
bool onBadCertificate(dynamic certificate, String domain) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void main(List<String> args) async {
|
void main(List<String> args) async {
|
||||||
// Set up logging
|
// Set up logging
|
||||||
Logger.root.level = Level.ALL;
|
Logger.root.level = Level.ALL;
|
||||||
@ -28,9 +22,23 @@ void main(List<String> args) async {
|
|||||||
..addOption('password')
|
..addOption('password')
|
||||||
..addOption('host')
|
..addOption('host')
|
||||||
..addOption('port')
|
..addOption('port')
|
||||||
..addOption('to');
|
..addOption('to')
|
||||||
|
..addOption('xmpps-srv');
|
||||||
final options = parser.parse(args);
|
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
|
// Connect
|
||||||
final jid = JID.fromString(options['jid']! as String);
|
final jid = JID.fromString(options['jid']! as String);
|
||||||
final to = JID.fromString(options['to']! as String).toBare();
|
final to = JID.fromString(options['to']! as String).toBare();
|
||||||
@ -39,13 +47,13 @@ void main(List<String> args) async {
|
|||||||
TestingReconnectionPolicy(),
|
TestingReconnectionPolicy(),
|
||||||
AlwaysConnectedConnectivityManager(),
|
AlwaysConnectedConnectivityManager(),
|
||||||
ClientToServerNegotiator(),
|
ClientToServerNegotiator(),
|
||||||
TestingTCPSocketWrapper(),
|
ExampleTCPSocketWrapper(srvRecord),
|
||||||
)..connectionSettings = ConnectionSettings(
|
)..connectionSettings = ConnectionSettings(
|
||||||
jid: jid,
|
jid: jid,
|
||||||
password: options['password']! as String,
|
password: options['password']! as String,
|
||||||
host: options['host'] as String?,
|
host: options['host'] as String?,
|
||||||
port: portString != null ? int.parse(portString) : null,
|
port: portString != null ? int.parse(portString) : null,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Generate OMEMO data
|
// Generate OMEMO data
|
||||||
omemo.OmemoManager? oom;
|
omemo.OmemoManager? oom;
|
||||||
@ -87,15 +95,16 @@ void main(List<String> args) async {
|
|||||||
Logger.root.info(event.extensions.keys.toList());
|
Logger.root.info(event.extensions.keys.toList());
|
||||||
|
|
||||||
final body = event.encryptionError != null
|
final body = event.encryptionError != null
|
||||||
? chalk.red('Failed to decrypt message: ${event.encryptionError}')
|
? chalk.red('Failed to decrypt message: ${event.encryptionError}')
|
||||||
: chalk.green(event.get<MessageBodyData>()?.body ?? '');
|
: chalk.green(event.get<MessageBodyData>()?.body ?? '');
|
||||||
print('[${event.from.toString()}] ' + body);
|
print('[${event.from.toString()}] $body');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Connect
|
// Connect
|
||||||
Logger.root.info('Connecting...');
|
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>()) {
|
if (!result.isType<bool>()) {
|
||||||
Logger.root.severe('Authentication failed!');
|
Logger.root.severe('Authentication failed!');
|
||||||
return;
|
return;
|
||||||
@ -107,18 +116,21 @@ void main(List<String> args) async {
|
|||||||
final device = await oom.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>()}');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final repl = Repl(prompt: '> ');
|
final repl = Repl(prompt: '> ');
|
||||||
await for (final line in repl.runAsync()) {
|
await for (final line in repl.runAsync()) {
|
||||||
await connection.getManagerById<MessageManager>(messageManager)!.sendMessage(
|
await connection
|
||||||
to,
|
.getManagerById<MessageManager>(messageManager)!
|
||||||
TypedMap<StanzaHandlerExtension>.fromList([
|
.sendMessage(
|
||||||
MessageBodyData(line),
|
to,
|
||||||
]),
|
TypedMap<StanzaHandlerExtension>.fromList([
|
||||||
);
|
MessageBodyData(line),
|
||||||
|
]),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnect
|
// Disconnect
|
||||||
|
22
examples_dart/lib/socket.dart
Normal file
22
examples_dart/lib/socket.dart
Normal 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!,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user