Compare commits

...

21 Commits

Author SHA1 Message Date
1e7279e23b Merge pull request 'Implement XEP-0045 support in moxxmpp' (#46) from ikjot-2605/moxxmpp:xep_0045 into master
Reviewed-on: https://codeberg.org/moxxy/moxxmpp/pulls/46
2023-07-01 19:42:12 +00:00
ikjot-2605
b2724aba0c Merge branch 'master' into xep_0045 2023-07-01 12:01:33 +00:00
Ikjot Singh Dhody
d3742ea156 feat(xep): Small fixes - MUC Example.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-07-01 17:29:51 +05:30
Ikjot Singh Dhody
8b00e85167 feat(xep): Add example for XEP 0045 Moxxmpp.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-07-01 09:16:51 +05:30
Ikjot Singh Dhody
04dfc6d2ac feat(xep): Replace DiscoError with StanzaError.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-30 18:33:00 +05:30
ikjot-2605
9e70e802ef Merge branch 'master' into xep_0045 2023-06-30 04:56:15 +00:00
Ikjot Singh Dhody
3ebd9b86ec feat(xep): Fix lint issues and use moxlib for result.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-30 10:25:17 +05:30
Ikjot Singh Dhody
a873edb9ec feat(xep): Check for null nick before leaveRoom.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-21 12:20:14 +05:30
Ikjot Singh Dhody
e6bd6d05cd feat(xep): Remove NOOP cache access.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-21 00:41:51 +05:30
Ikjot Singh Dhody
b7d53b8f47 feat(xep): Add docstings for the XEP-0045 routines
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-20 17:44:24 +05:30
Ikjot Singh Dhody
217c3ac236 feat(xep): Fix cache issue with join/leaveRoom.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-20 17:38:30 +05:30
Ikjot Singh Dhody
51bca6c25d feat(xep): XEP-0045 cache fixes.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-19 18:40:39 +05:30
Ikjot Singh Dhody
8728166a4d feat(xep): Add cache and roomstate to MUC implementation.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-18 20:49:13 +05:30
Ikjot Singh Dhody
1f1321b269 feat(xep): Small fixes - review cycle 1.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-18 20:09:06 +05:30
Ikjot Singh Dhody
66195f66fa Merge branch 'master' into xep_0045
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-14 10:48:28 +05:30
Ikjot Singh Dhody
70fdfaf16d feat(xep): Fix imports for xep_0045 files.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-14 10:47:51 +05:30
Ikjot Singh Dhody
cd73f89e63 feat(xep): Remove duplicate manager string
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-14 10:47:51 +05:30
Ikjot Singh Dhody
05c41d3185 feat(xep): Refactor sendMessage to allow groupchat
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-14 10:47:50 +05:30
Ikjot Singh Dhody
64a8de6caa feat(xep): Set base for XEP 0045 implementation
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-14 10:47:07 +05:30
Ikjot Singh Dhody
68809469f6 feat(xep): Add joinRoom, leaveRoom routines.
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-14 10:46:48 +05:30
Ikjot Singh Dhody
762cf1c77a feat(xep): Set base for XEP 0045 implementation
Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
2023-06-14 10:46:48 +05:30
8 changed files with 265 additions and 3 deletions

View File

@ -0,0 +1,80 @@
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()
..parser.addOption('muc', help: 'The MUC 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 muc = JID.fromString(options['muc']! as String).toBare();
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
await connection.getManagerById<MUCManager>(mucManager)!.joinRoom(muc, nick);
final repl = Repl(prompt: '> ');
await for (final line in repl.runAsync()) {
await connection
.getManagerById<MessageManager>(messageManager)!
.sendMessage(
muc,
TypedMap<StanzaHandlerExtension>.fromList([
MessageBodyData(line),
]),
type: 'groupchat');
}
// Leave room
await connection.getManagerById<MUCManager>(mucManager)!.leaveRoom(muc);
// Disconnect
await connection.disconnect();
}

View File

@ -47,6 +47,9 @@ export 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
export 'package:moxxmpp/src/xeps/xep_0030/helpers.dart'; export 'package:moxxmpp/src/xeps/xep_0030/helpers.dart';
export 'package:moxxmpp/src/xeps/xep_0030/types.dart'; export 'package:moxxmpp/src/xeps/xep_0030/types.dart';
export 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart'; export 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
export 'package:moxxmpp/src/xeps/xep_0045/errors.dart';
export 'package:moxxmpp/src/xeps/xep_0045/types.dart';
export 'package:moxxmpp/src/xeps/xep_0045/xep_0045.dart';
export 'package:moxxmpp/src/xeps/xep_0054.dart'; export 'package:moxxmpp/src/xeps/xep_0054.dart';
export 'package:moxxmpp/src/xeps/xep_0060/errors.dart'; export 'package:moxxmpp/src/xeps/xep_0060/errors.dart';
export 'package:moxxmpp/src/xeps/xep_0060/helpers.dart'; export 'package:moxxmpp/src/xeps/xep_0060/helpers.dart';

View File

@ -33,3 +33,4 @@ const stickersManager = 'org.moxxmpp.stickersmanager';
const entityCapabilitiesManager = 'org.moxxmpp.entitycapabilities'; const entityCapabilitiesManager = 'org.moxxmpp.entitycapabilities';
const messageProcessingHintManager = 'org.moxxmpp.messageprocessinghint'; const messageProcessingHintManager = 'org.moxxmpp.messageprocessinghint';
const occupantIdManager = 'org.moxxmpp.occupantidmanager'; const occupantIdManager = 'org.moxxmpp.occupantidmanager';
const mucManager = 'org.moxxmpp.mucmanager';

View File

@ -103,14 +103,15 @@ class MessageManager extends XmppManagerBase {
/// data for building the message. /// data for building the message.
Future<void> sendMessage( Future<void> sendMessage(
JID to, JID to,
TypedMap<StanzaHandlerExtension> extensions, TypedMap<StanzaHandlerExtension> extensions, {
) async { String type = 'chat',
}) async {
await getAttributes().sendStanza( await getAttributes().sendStanza(
StanzaDetails( StanzaDetails(
Stanza.message( Stanza.message(
to: to.toString(), to: to.toString(),
id: extensions.get<MessageIdData>()?.id, id: extensions.get<MessageIdData>()?.id,
type: 'chat', type: type,
children: _messageSendingCallbacks children: _messageSendingCallbacks
.map((c) => c(extensions)) .map((c) => c(extensions))
.flattened .flattened

View File

@ -21,6 +21,9 @@ const discoItemsXmlns = 'http://jabber.org/protocol/disco#items';
// XEP-0033 // XEP-0033
const extendedAddressingXmlns = 'http://jabber.org/protocol/address'; const extendedAddressingXmlns = 'http://jabber.org/protocol/address';
// XEP-0045
const mucXmlns = 'http://jabber.org/protocol/muc';
// XEP-0054 // XEP-0054
const vCardTempXmlns = 'vcard-temp'; const vCardTempXmlns = 'vcard-temp';
const vCardTempUpdate = 'vcard-temp:x:update'; const vCardTempUpdate = 'vcard-temp:x:update';

View File

@ -0,0 +1,19 @@
/// Represents an error related to Multi-User Chat (MUC).
abstract class MUCError {}
/// Error indicating an invalid (non-supported) stanza received while going
/// through normal operation/flow of an MUC.
class InvalidStanzaFormat extends MUCError {}
/// Represents an error indicating an abnormal condition while parsing
/// the DiscoInfo response stanza in Multi-User Chat (MUC).
class InvalidDiscoInfoResponse extends MUCError {}
/// Returned when no nickname was specified from the client side while trying to
/// perform some actions on the MUC, such as joining the room.
class NoNicknameSpecified extends MUCError {}
/// This error occurs when a user attempts to perform an action that requires
/// them to be a member of a room, but they are not currently joined to
/// that room.
class RoomNotJoinedError extends MUCError {}

View File

@ -0,0 +1,43 @@
import 'package:moxxmpp/src/jid.dart';
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
class RoomInformation {
/// Represents information about a Multi-User Chat (MUC) room.
RoomInformation({
required this.jid,
required this.features,
required this.name,
});
/// Constructs a [RoomInformation] object from a [DiscoInfo] object.
/// The [DiscoInfo] object contains the necessary information to populate
/// the [RoomInformation] fields.
factory RoomInformation.fromDiscoInfo({
required DiscoInfo discoInfo,
}) =>
RoomInformation(
jid: discoInfo.jid!,
features: discoInfo.features,
name: discoInfo.identities
.firstWhere((i) => i.category == 'conference')
.name!,
);
/// The JID of the Multi-User Chat (MUC) room.
final JID jid;
/// A list of features supported by the Multi-User Chat (MUC) room.
final List<String> features;
/// The name or title of the Multi-User Chat (MUC) room.
final String name;
}
class RoomState {
RoomState({
required this.roomJid,
this.nick,
});
final JID roomJid;
String? nick;
}

View File

@ -0,0 +1,112 @@
import 'package:moxlib/moxlib.dart';
import 'package:moxxmpp/src/jid.dart';
import 'package:moxxmpp/src/managers/base.dart';
import 'package:moxxmpp/src/managers/namespaces.dart';
import 'package:moxxmpp/src/namespaces.dart';
import 'package:moxxmpp/src/stanza.dart';
import 'package:moxxmpp/src/stringxml.dart';
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
import 'package:moxxmpp/src/xeps/xep_0045/errors.dart';
import 'package:moxxmpp/src/xeps/xep_0045/types.dart';
import 'package:synchronized/synchronized.dart';
class MUCManager extends XmppManagerBase {
MUCManager() : super(mucManager);
@override
Future<bool> isSupported() async => true;
/// Map full JID to RoomState
final Map<JID, RoomState> _mucRoomCache = {};
/// Cache lock
final Lock _cacheLock = Lock();
/// Queries the information of a Multi-User Chat room.
///
/// Retrieves the information about the specified MUC room by performing a
/// disco info query. Returns a [Result] with the [RoomInformation] on success
/// or an appropriate [MUCError] on failure.
Future<Result<RoomInformation, MUCError>> queryRoomInformation(
JID roomJID,
) async {
final result = await getAttributes()
.getManagerById<DiscoManager>(discoManager)!
.discoInfoQuery(roomJID);
if (result.isType<StanzaError>()) {
return Result(InvalidStanzaFormat());
}
try {
final roomInformation = RoomInformation.fromDiscoInfo(
discoInfo: result.get<DiscoInfo>(),
);
return Result(roomInformation);
} catch (e) {
return Result(InvalidDiscoInfoResponse);
}
}
/// Joins a Multi-User Chat room.
///
/// Joins the specified MUC room using the provided nickname. Sends a presence
/// stanza with the appropriate attributes to join the room. Returns a [Result]
/// with a boolean value indicating success or failure, or an [MUCError]
/// if applicable.
Future<Result<bool, MUCError>> joinRoom(
JID roomJid,
String nick,
) async {
if (nick.isEmpty) {
return Result(NoNicknameSpecified());
}
await getAttributes().sendStanza(
StanzaDetails(
Stanza.presence(
to: roomJid.withResource(nick).toString(),
children: [
XMLNode.xmlns(
tag: 'x',
xmlns: mucXmlns,
)
],
),
),
);
await _cacheLock.synchronized(
() {
_mucRoomCache[roomJid] = RoomState(roomJid: roomJid, nick: nick);
},
);
return const Result(true);
}
/// Leaves a Multi-User Chat room.
///
/// Leaves the specified MUC room by sending an 'unavailable' presence stanza.
/// Removes the corresponding room entry from the cache. Returns a [Result]
/// with a boolean value indicating success or failure, or an [MUCError]
/// if applicable.
Future<Result<bool, MUCError>> leaveRoom(
JID roomJid,
) async {
final nick = await _cacheLock.synchronized(() {
final nick = _mucRoomCache[roomJid]?.nick;
_mucRoomCache.remove(roomJid);
return nick;
});
if (nick == null) {
return Result(RoomNotJoinedError);
}
await getAttributes().sendStanza(
StanzaDetails(
Stanza.presence(
to: roomJid.withResource(nick).toString(),
type: 'unavailable',
),
),
);
return const Result(true);
}
}