QXmppMucManagerV2 Class
XEP-0045: Multi-User Chat manager. More...
| Header: | #include <QXmppMucManagerV2.h> |
| Since: | QXmpp 1.13 |
| Inherits: | QXmppClientExtension |
- List of all members, including inherited members
- QXmppMucManagerV2 is part of Managers.
Public Functions
| QXmppMucManagerV2() | |
(since QXmpp 1.16) QXmppTask<QXmpp::Result<QXmppMucRoomV2>> | createRoom(QString serviceJid, QString nickname, std::optional<QString> roomName = std::nullopt) |
(since QXmpp 1.16) QXmppTask<QXmpp::SendResult> | declineInvitation(const QString &roomJid, QXmpp::Muc::Decline decline) |
| std::optional<QXmppMucRoomV2> | findRoom(const QString &jid) |
(since QXmpp 1.17) QXmppTask<QXmpp::Result<bool>> | isMucRoom(const QString &jid) |
| QXmppTask<QXmpp::Result<QXmppMucRoomV2>> | joinRoom(const QString &jid, const QString &nickname) |
| QXmppTask<QXmpp::Result<QXmppMucRoomV2>> | joinRoom(const QString &jid, const QString &nickname, std::optional<QXmpp::Muc::HistoryOptions> history, const QString &password = {}) |
(since QXmpp 1.17) QBindable<QList<QXmppDiscoService>> | mucServiceInfos() const |
(since QXmpp 1.16) QBindable<QStringList> | mucServices() const |
(since QXmpp 1.16) QBindable<bool> | mucServicesLoaded() const |
(since QXmpp 1.16) QXmppTask<QXmpp::Result<QString>> | requestUniqueRoomName(QString serviceJid) |
(since QXmpp 1.16) std::chrono::milliseconds | selfPingSilenceThreshold() const |
(since QXmpp 1.16) QXmppTask<QXmpp::SendResult> | sendDirectInvitation(const QString &to, QXmpp::Muc::DirectInvitation invitation, QXmppMessage message = {}) |
(since QXmpp 1.16) void | setSelfPingSilenceThreshold(std::chrono::milliseconds threshold) |
Signals
(since QXmpp 1.16) void | directInvitationReceived(const QXmpp::Muc::DirectInvitation &invitation, const QString &from, const QXmppMessage &message) |
(since QXmpp 1.16) void | invitationDeclined(const QString &roomJid, const QXmpp::Muc::Decline &decline) |
(since QXmpp 1.16) void | invitationReceived(const QString &roomJid, const QXmpp::Muc::Invite &invite, const QString &password) |
(since QXmpp 1.17) void | messageRetracted(const QString &roomJid, const QXmppMessageRetraction &retraction) |
| void | participantJoined(const QString &roomJid, const QXmppMucParticipant &participant) |
| void | participantLeft(const QString &roomJid, const QXmppMucParticipant &participant, QXmpp::Muc::LeaveReason reason) |
| void | removedFromRoom(const QString &roomJid, QXmpp::Muc::LeaveReason reason, const std::optional<QXmpp::Muc::Destroy> &destroy) |
| void | roomHistoryReceived(const QString &roomJid, const QList<QXmppMessage> &messages) |
(since QXmpp 1.16) void | voiceRequestReceived(const QString &roomJid, const QXmppMucVoiceRequest &request) |
Detailed Description
Setup
QXmppMucManagerV2 requires QXmppDiscoveryManager to be registered with the client:
auto *muc = new QXmppMucManagerV2;
auto *disco = new QXmppDiscoveryManager;
client.addExtension(muc);
client.addExtension(disco);
For bookmark management, see QXmppPepBookmarkManager.
Joining a room
Call joinRoom() to join a room. The returned task resolves once all initial occupant presences have been received, so the participant list is already populated when the task finishes.
muc->joinRoom(u"room@conference.example.org"_s, u"alice"_s).then(this, [](auto result) {
if (auto *room = std::get_if<QXmppMucRoomV2>(&result)) {
qDebug() << "Joined as" << room->nickname().value();
}
});
After joining, retrieve the room handle at any time via room():
auto r = muc->room(u"room@conference.example.org"_s);
if (r.isValid()) {
// use r
}
Rejoining rooms
Joined rooms are rejoined automatically when the connection is lost and the stream is not resumed (XEP-0198: Stream Management), and when XEP-0410: MUC Self-Ping detects that the client has been removed from the room silently. The room handles stay valid: QXmppMucRoomV2::joined() is false until the room has been rejoined. Afterwards, only the occupants that have joined or left meanwhile are reported via participantJoined() and participantLeft(). The discussion history is requested from the last message received before, unless no history was requested in joinRoom(), and delivered via roomHistoryReceived(). Because the local clock may differ from the clock of the service, it starts some minutes earlier and may contain messages that have been received before. QXmppMucRoomV2::joining() is true while the room waits to be rejoined.
joinRoom() waits for a running rejoin, it fails if another nickname is requested. If rejoining fails, removedFromRoom() is emitted. After QXmppClient::disconnectFromServer(), the rooms are not rejoined.
Receiving messages
Messages of joined rooms are delivered via QXmppClient::messageReceived(), together with all other messages. They run through the same pipeline, so they are decrypted and the real JID of the sender is resolved (see QXmpp::Messages::Context::senderJid()). The discussion history received while joining is delivered via roomHistoryReceived(). Subject changes are only reported via QXmppMucRoomV2::subject().
Sending messages
Messages to joined rooms are sent via QXmppClient::sendMessage() like all other messages, with the type QXmppMessage::GroupChat. An origin-id (XEP-0359: Unique and Stable Stanza IDs) is added if not already set and used to match the reflection of the room. The task finishes when the room has reflected the message (QXmpp::Messages::DeliveryState::Confirmed), with the ID of the message in the archive of the room (QXmpp::Messages::Sent::archiveId()), or with an error if the room responds with an error or the reflection times out.
QXmppMessage msg(QString(), u"room@conference.example.org"_s, u"Hello, room!"_s);
msg.setType(QXmppMessage::GroupChat);
client->sendMessage(std::move(msg)).then(this, [](QXmpp::Result<QXmpp::Messages::Sent> &&result) {
if (auto *sent = std::get_if<QXmpp::Messages::Sent>(&result); sent && sent->archiveId()) {
// e.g. use sent->archiveId()->id for reactions
}
});
Creating a room
createRoom() creates a new reserved (locked) room. The task resolves once the server confirms room creation and the configuration form has been fetched. Configure the room via setRoomConfig() to unlock it, or cancel with cancelRoomCreation().
muc->createRoom(u"newroom@conference.example.org"_s, u"alice"_s).then(this, [](auto result) {
if (auto *room = std::get_if<QXmppMucRoomV2>(&result)) {
auto config = room->roomConfig().value().value_or(QXmppMucRoomConfig{});
config.setName(u"My New Room"_s);
room->setRoomConfig(config);
}
});
Moderation and affiliation management
Use setRole() to change a participant's role (e.g. kick or grant/revoke voice) and setAffiliation() to change a user's persistent affiliation (e.g. ban or grant membership). Use requestAffiliationList() to retrieve the full list of users with a given affiliation.
// Kick a participant
r.setRole(participant, QXmpp::Muc::Role::None, u"Misbehaving"_s);
// Ban a user
r.setAffiliation(u"user@example.org"_s, QXmpp::Muc::Affiliation::Outcast);
// Fetch the member list
r.requestAffiliationList(QXmpp::Muc::Affiliation::Member).then(this, [](auto result) {
if (auto *list = std::get_if<QList<QXmpp::Muc::Item>>(&result)) {
for (const auto &item : *list) {
qDebug() << item.jid() << item.affiliation();
}
}
});
Room configuration
Call requestRoomConfig() to retrieve a typed configuration form. Edit the returned QXmppMucRoomConfig and submit it with setRoomConfig(). Pass watch = true (or call setWatchRoomConfig()) to be notified of configuration changes via the roomConfig() bindable.
r.requestRoomConfig(true).then(this, [r](auto result) mutable {
if (auto *config = std::get_if<QXmppMucRoomConfig>(&result)) {
config->setMaxUsers(50);
r.setRoomConfig(*config);
}
});
Participants and permissions
participants() returns lightweight handles to all current occupants. selfParticipant() returns your own participant entry, which exposes your current role and affiliation as QBindables.
The capability QBindables (canSendMessages(), canSetRoles(), canConfigureRoom(), …) update automatically whenever the MUC service changes your permissions. Observer lifetime and the other pitfalls of bindable state are covered in Reactive Properties.
The muc_bot and muc_create programs in the examples directory show the manager in use, from joining and echoing messages to discovering services and creating a room.
Warning: THIS API IS NOT FINALIZED YET and may still change in incompatible ways before it is released.
See also Reactive Properties.
Member Function Documentation
QXmppMucManagerV2::QXmppMucManagerV2()
Default constructor.
[since QXmpp 1.16] QXmppTask<QXmpp::Result<QXmppMucRoomV2>> QXmppMucManagerV2::createRoom(QString serviceJid, QString nickname, std::optional<QString> roomName = std::nullopt)
Creates a new reserved (locked) MUC room on serviceJid with the given nickname.
If roomName is set, the room is created at roomName@serviceJid. If roomName is unset, createRoom() asks the service for a unique localpart via XEP-0307 (muc#unique); if that query fails for any reason (service does not support XEP-0307, IQ error, …), it falls back to a client-generated UUID localpart so creation still succeeds against any compliant MUC service.
The room is created in a locked state; no other users can join until the owner submits the configuration form via QXmppMucRoomV2::setRoomConfig(). The task resolves once the server has confirmed room creation (XEP-0045 status code 201) and the configuration form has been fetched. If the room is already tracked, i.e. joined or being joined or created (see findRoom()), the task fails immediately.
QXmppMucRoomV2::joined() is still false when the task resolves. It is true once the task of QXmppMucRoomV2::setRoomConfig() has resolved, and only then the room is usable, e.g. for sending messages.
If the user may not create rooms on the service, the task fails with the stanza error of the service. XEP-0045: Multi-User Chat requires not-allowed for this, but some services use forbidden.
This function was introduced in QXmpp 1.16.
[since QXmpp 1.16] QXmppTask<QXmpp::SendResult> QXmppMucManagerV2::declineInvitation(const QString &roomJid, QXmpp::Muc::Decline decline)
Declines a mediated invitation by sending decline as a message to roomJid.
Can be called before joining the room. Set decline.to() to the inviter's JID.
This function was introduced in QXmpp 1.16.
[signal, since QXmpp 1.16] void QXmppMucManagerV2::directInvitationReceived(const QXmpp::Muc::DirectInvitation &invitation, const QString &from, const QXmppMessage &message)
Emitted when a XEP-0249: Direct MUC Invitations invitation is received.
The from is the full JID of the user who sent the invitation, and message is the full message stanza it was received in.
This function was introduced in QXmpp 1.16.
std::optional<QXmppMucRoomV2> QXmppMucManagerV2::findRoom(const QString &jid)
Returns a handle for the tracked room at jid, or std::nullopt if the room is not tracked. This does not join the room or send any request.
A room is tracked from the start of joinRoom() or createRoom() until it is left, and its state is kept up to date automatically in the meantime. A left room is still returned as long as another handle to it exists, with its last known state.
[signal, since QXmpp 1.16] void QXmppMucManagerV2::invitationDeclined(const QString &roomJid, const QXmpp::Muc::Decline &decline)
Emitted when the room at roomJid reports that an invitee declined an invitation via decline.
This function was introduced in QXmpp 1.16.
[signal, since QXmpp 1.16] void QXmppMucManagerV2::invitationReceived(const QString &roomJid, const QXmpp::Muc::Invite &invite, const QString &password)
Emitted when a mediated invite is received from the room at roomJid, protected by the given password (if any).
The user is not yet in the room at this point. Call joinRoom() to accept or declineInvitation() to decline.
This function was introduced in QXmpp 1.16.
[since QXmpp 1.17] QXmppTask<QXmpp::Result<bool>> QXmppMucManagerV2::isMucRoom(const QString &jid)
Determines whether jid hosts a XEP-0045: Multi-User Chat room.
Sends a XEP-0030: Service Discovery info query and inspects the response. The task resolves to true if the JID is a MUC room, and to false if it is anything else — a MIX channel, a MUC service, an ordinary account — or if no such room exists (item-not-found). Any other failure, such as a timeout, resolves to a QXmppError.
Use this to tell a MUC room from a MIX channel before joining. For the room's details, query QXmppDiscoveryManager::info() directly; the same response carries the muc\#roominfo form and the muc_* feature flags.
muc->isMucRoom(u"room@conference.example.org"_s).then(this, [](auto result) {
if (auto *isRoom = std::get_if<bool>(&result); isRoom && *isRoom) {
// ... join it
}
});
This function was introduced in QXmpp 1.17.
QXmppTask<QXmpp::Result<QXmppMucRoomV2>> QXmppMucManagerV2::joinRoom(const QString &jid, const QString &nickname)
Joins the MUC room at jid with the given nickname.
Sends an initial presence to the room and waits for all occupant presences, the discussion history and the subject that the MUC service sends back. The returned task resolves once the subject and the room's service discovery info have been received, so the participant list is already fully populated when the task finishes. The service discovery info tells whether the room supports occupant IDs (XEP-0421: Occupant identifiers for semi-anonymous MUCs).
QXmppMucRoomV2::joined() is already true when the task resolves, so a notifier added afterwards does not report it. Use QBindable::subscribe() or read the value first, see Reactive Properties.
If a join for the same room is already in progress the task fails immediately. If the room is already joined the existing room handle is returned as a success. If the room is rejoined after a connection loss, the task finishes with the result of the rejoin, see Rejoining rooms. The task fails if the requested nickname differs from the nickname of the rejoin; the history and password of the original join are used.
jid is the bare JID of the room (e.g. room\@conference.example.org). nickname is the desired nickname — must not be empty.
QXmppTask<QXmpp::Result<QXmppMucRoomV2>> QXmppMucManagerV2::joinRoom(const QString &jid, const QString &nickname, std::optional<QXmpp::Muc::HistoryOptions> history, const QString &password = {})
Joins the room with optional history request parameters and an optional password for password-protected rooms (XEP-0045: Multi-User Chat §7.2.5).
This is an overloaded function.
[signal, since QXmpp 1.17] void QXmppMucManagerV2::messageRetracted(const QString &roomJid, const QXmppMessageRetraction &retraction)
Emitted when a message in the joined room at roomJid is retracted via XEP-0424: Message Retraction or XEP-0425: Moderated Message Retraction.
The retracted message is identified by retraction.retractedId() of retraction (the message's XEP-0359: stanza id). For moderated retractions, retraction.moderation() carries the moderator and reason. Spoofed moderated retractions (claiming moderation but not sent by the room itself) never trigger this signal.
The retraction message is also delivered via QXmppClient::messageReceived() (with its QXmppMessage::retraction() set), so clients that need the raw stanza still receive it.
This function was introduced in QXmpp 1.17.
[since QXmpp 1.17] QBindable<QList<QXmppDiscoService>> QXmppMucManagerV2::mucServiceInfos() const
Returns the discovered MUC services on the server, including their full XEP-0030: Service Discovery info.
This is the same set of services as mucServices(), but with the identities, features and data forms each service advertises. Use it when the plain JIDs are not enough, for example to check whether a service supports XEP-0307: Unique Room Names before creating a room without an explicit name.
for (const auto &service : muc->mucServiceInfos().value()) {
qDebug() << service.jid << service.info.features();
}
This function was introduced in QXmpp 1.17.
See also mucServices() and mucServicesLoaded().
[since QXmpp 1.16] QBindable<QStringList> QXmppMucManagerV2::mucServices() const
Returns the JIDs of discovered MUC services on the server.
The list is populated automatically after connecting and updated reactively. It is empty before service discovery has run, so an empty list on its own does not mean the server has no MUC service — check mucServicesLoaded() to tell the two apart.
A server with no MUC service cannot host group chats at all. Whether a particular user may create rooms on a service is server policy and is not advertised anywhere in XEP-0045: Multi-User Chat, so that only becomes visible when createRoom() fails.
This function was introduced in QXmpp 1.16.
See also mucServiceInfos(), mucServicesLoaded(), and Reactive Properties.
[since QXmpp 1.16] QBindable<bool> QXmppMucManagerV2::mucServicesLoaded() const
Returns whether MUC service discovery has completed.
This function was introduced in QXmpp 1.16.
[signal] void QXmppMucManagerV2::participantJoined(const QString &roomJid, const QXmppMucParticipant &participant)
Emitted when participant joins the room at roomJid.
[signal] void QXmppMucManagerV2::participantLeft(const QString &roomJid, const QXmppMucParticipant &participant, QXmpp::Muc::LeaveReason reason)
Emitted when participant leaves the room at roomJid for the given reason.
[signal] void QXmppMucManagerV2::removedFromRoom(const QString &roomJid, QXmpp::Muc::LeaveReason reason, const std::optional<QXmpp::Muc::Destroy> &destroy)
Emitted when we are forcibly removed from the room at roomJid (kicked, banned, etc.) for the given reason.
The room state is still accessible during this signal's emission. After all handlers return, the room state is cleaned up. The destroy parameter contains room destruction info if the reason is RoomDestroyed.
Not emitted for QXmppMucRoomV2::leave() and QXmppMucRoomV2::destroyRoom().
Rooms are rejoined automatically when the connection is lost and the stream is not resumed, see Rejoining rooms. If rejoining fails, it is emitted with the reason of the failure, e.g. LeaveReason::Banned, or LeaveReason::ConnectionLost if the room did not answer. Rooms still being joined are not rejoined, their joinRoom() tasks fail instead.
After QXmppClient::disconnectFromServer(), it is emitted with LeaveReason::ConnectionLost for each joined room, and the rooms are not rejoined.
To clean up whenever the room is no longer joined, including after leave() and destroyRoom(), use QXmppMucRoomV2::joined() instead.
[since QXmpp 1.16] QXmppTask<QXmpp::Result<QString>> QXmppMucManagerV2::requestUniqueRoomName(QString serviceJid)
Requests a unique room localpart from the MUC serviceJid (XEP-0307: Unique Room Names for Multi-User Chat).
Returns the full room JID (localpart@serviceJid) which can be passed to createRoom(). Fails if the service does not support XEP-0307: Unique Room Names for Multi-User Chat or returns an empty name.
This function was introduced in QXmpp 1.16.
[signal] void QXmppMucManagerV2::roomHistoryReceived(const QString &roomJid, const QList<QXmppMessage> &messages)
Emitted with the discussion history messages received for the room at roomJid during joining.
The messages have been decrypted like the messages received after joining, which are delivered via QXmppClient::messageReceived().
[since QXmpp 1.16] std::chrono::milliseconds QXmppMucManagerV2::selfPingSilenceThreshold() const
Returns the XEP-0410: MUC Self-Ping silence threshold.
After a room has been silent for this duration, the manager sends a self-ping to verify that the client is still joined. A zero value disables self-pinging entirely.
This function was introduced in QXmpp 1.16.
See also setSelfPingSilenceThreshold().
[since QXmpp 1.16] QXmppTask<QXmpp::SendResult> QXmppMucManagerV2::sendDirectInvitation(const QString &to, QXmpp::Muc::DirectInvitation invitation, QXmppMessage message = {})
Sends a direct invitation (XEP-0249) to to.
Direct invitations are sent peer-to-peer and are not routed through the room. The optional message parameter can be used to set custom extensions on the message stanza; the to, type, and invitation fields will be overwritten.
This function was introduced in QXmpp 1.16.
[since QXmpp 1.16] void QXmppMucManagerV2::setSelfPingSilenceThreshold(std::chrono::milliseconds threshold)
Sets the XEP-0410: MUC Self-Ping silence threshold.
After threshold of silence on a joined room, the manager sends an XEP-0199: XMPP Ping to the user's own occupant JID and interprets the response to detect silent server-side eviction ("ghosting"). Defaults to 5 minutes. Pass a zero duration to disable self-pinging entirely.
Ghosted rooms are rejoined, see Rejoining rooms.
This function was introduced in QXmpp 1.16.
See also selfPingSilenceThreshold().
[signal, since QXmpp 1.16] void QXmppMucManagerV2::voiceRequestReceived(const QString &roomJid, const QXmppMucVoiceRequest &request)
Emitted when a voice request is received in the joined moderated room at roomJid.
This signal is only emitted for moderators. The moderator can approve or deny the request by calling QXmppMucRoomV2::answerVoiceRequest().
This function was introduced in QXmpp 1.16.