From 8b00e8516743c7e5cb73634d42680e3d752298c8 Mon Sep 17 00:00:00 2001 From: Ikjot Singh Dhody Date: Sat, 1 Jul 2023 09:16:51 +0530 Subject: [PATCH] feat(xep): Add example for XEP 0045 Moxxmpp. Signed-off-by: Ikjot Singh Dhody --- examples_dart/bin/muc_client.dart | 87 +++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 examples_dart/bin/muc_client.dart diff --git a/examples_dart/bin/muc_client.dart b/examples_dart/bin/muc_client.dart new file mode 100644 index 0000000..7455c78 --- /dev/null +++ b/examples_dart/bin/muc_client.dart @@ -0,0 +1,87 @@ +import 'package:cli_repl/cli_repl.dart'; +import 'package:example_dart/arguments.dart'; +import 'package:example_dart/socket.dart'; +import 'package:logging/logging.dart'; +import 'package:moxxmpp/moxxmpp.dart'; + +void main(List args) async { + // Set up logging + Logger.root.level = Level.ALL; + Logger.root.onRecord.listen((record) { + // ignore: avoid_print + print( + '[${record.level.name}] (${record.loggerName}) ${record.time}: ${record.message}', + ); + }); + + final parser = ArgumentParser() + ..parser.addOption('to', help: 'The JID to send messages to') + ..parser.addOption('nick', help: 'The nickname with which to join the MUC'); + final options = parser.handleArguments(args); + if (options == null) { + return; + } + + // Connect + final jid = parser.jid; + final to = JID.fromString(options['to']! as String).toBare(); + final nick = options['nick']! as String; + print(parser.connectionSettings); + final connection = XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + ClientToServerNegotiator(), + ExampleTCPSocketWrapper(parser.srvRecord), + )..connectionSettings = parser.connectionSettings; + + print(parser.connectionSettings.host); + print(parser.connectionSettings.jid); + print(parser.connectionSettings.password); + print(parser.connectionSettings.port); + + // Register the managers and negotiators + await connection.registerManagers([ + PresenceManager(), + DiscoManager([]), + PubSubManager(), + MessageManager(), + MUCManager(), + ]); + await connection.registerFeatureNegotiators([ + SaslPlainNegotiator(), + ResourceBindingNegotiator(), + StartTlsNegotiator(), + SaslScramNegotiator(10, '', '', ScramHashType.sha1), + ]); + + // Connect + Logger.root.info('Connecting...'); + final result = + await connection.connect(shouldReconnect: false, waitUntilLogin: true); + if (!result.isType()) { + Logger.root.severe('Authentication failed!'); + return; + } + Logger.root.info('Connected.'); + + // Join room + await connection.getManagerById(mucManager)!.joinRoom(to, nick); + + final repl = Repl(prompt: '> '); + await for (final line in repl.runAsync()) { + await connection + .getManagerById(messageManager)! + .sendMessage( + to, + TypedMap.fromList([ + MessageBodyData(line), + ]), + type: 'groupchat'); + } + + // Leave room + await connection.getManagerById(mucManager)!.leaveRoom(to); + + // Disconnect + await connection.disconnect(); +}