2023-07-01 03:46:51 +00:00
|
|
|
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<String> 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()
|
2023-07-01 11:59:51 +00:00
|
|
|
..parser.addOption('muc', help: 'The MUC to send messages to')
|
2023-07-01 03:46:51 +00:00
|
|
|
..parser.addOption('nick', help: 'The nickname with which to join the MUC');
|
|
|
|
final options = parser.handleArguments(args);
|
|
|
|
if (options == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect
|
2023-07-01 11:59:51 +00:00
|
|
|
final muc = JID.fromString(options['muc']! as String).toBare();
|
2023-07-01 03:46:51 +00:00
|
|
|
final nick = options['nick']! as String;
|
|
|
|
final connection = XmppConnection(
|
|
|
|
TestingReconnectionPolicy(),
|
|
|
|
AlwaysConnectedConnectivityManager(),
|
|
|
|
ClientToServerNegotiator(),
|
|
|
|
ExampleTCPSocketWrapper(parser.srvRecord),
|
|
|
|
)..connectionSettings = parser.connectionSettings;
|
|
|
|
|
|
|
|
// 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<bool>()) {
|
|
|
|
Logger.root.severe('Authentication failed!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Logger.root.info('Connected.');
|
|
|
|
|
|
|
|
// Join room
|
2023-07-01 11:59:51 +00:00
|
|
|
await connection.getManagerById<MUCManager>(mucManager)!.joinRoom(muc, nick);
|
2023-07-01 03:46:51 +00:00
|
|
|
|
|
|
|
final repl = Repl(prompt: '> ');
|
|
|
|
await for (final line in repl.runAsync()) {
|
|
|
|
await connection
|
|
|
|
.getManagerById<MessageManager>(messageManager)!
|
|
|
|
.sendMessage(
|
2023-07-01 11:59:51 +00:00
|
|
|
muc,
|
2023-07-01 03:46:51 +00:00
|
|
|
TypedMap<StanzaHandlerExtension>.fromList([
|
|
|
|
MessageBodyData(line),
|
|
|
|
]),
|
|
|
|
type: 'groupchat');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave room
|
2023-07-01 11:59:51 +00:00
|
|
|
await connection.getManagerById<MUCManager>(mucManager)!.leaveRoom(muc);
|
2023-07-01 03:46:51 +00:00
|
|
|
|
|
|
|
// Disconnect
|
|
|
|
await connection.disconnect();
|
|
|
|
}
|