-
Notifications
You must be signed in to change notification settings - Fork 2
/
muctextchannel.cc
170 lines (136 loc) · 5.75 KB
/
muctextchannel.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* This file is part of the telepathy-nonsense connection manager.
* Copyright (C) 2016 Alexandr Akulich <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>
*/
#include "muctextchannel.hh"
#include "common.hh"
#include "connection.hh"
#include <QXmppMucManager.h>
#include <QXmppUtils.h>
MucTextChannel::MucTextChannel(Connection *connection, Tp::BaseChannel *baseChannel) :
TextChannel(connection, baseChannel)
{
QXmppMucManager *mucManager = m_connection->qxmppClient()->findExtension<QXmppMucManager>();
const QXmppConfiguration &clientConfig = m_connection->qxmppClient()->configuration();
m_room = mucManager->addRoom(baseChannel->targetID());
m_room->setNickName(clientConfig.user());
if (!m_room->isJoined()) {
m_room->join();
}
connect(m_room, &QXmppMucRoom::participantsChanged, this, &MucTextChannel::onMucParticipantsChanged);
// Default flags:
Tp::ChannelGroupFlags groupFlags = Tp::ChannelGroupFlagChannelSpecificHandles|Tp::ChannelGroupFlagHandleOwnersNotAvailable;
// Permissions (not implemented yet):
groupFlags |= Tp::ChannelGroupFlagCanAdd|Tp::ChannelGroupFlagCanRemove;
m_groupIface = Tp::BaseChannelGroupInterface::create();
m_groupIface->setGroupFlags(groupFlags);
m_groupIface->setSelfHandle(connection->ensureContactHandle(selfJid()));
m_groupIface->setAddMembersCallback(Tp::memFun(this, &MucTextChannel::addMembers));
baseChannel->plugInterface(Tp::AbstractChannelInterfacePtr::dynamicCast(m_groupIface));
const QString roomName = QXmppUtils::jidToUser(m_room->jid());
const QString serverName = QXmppUtils::jidToDomain(m_room->jid());
m_roomIface = Tp::BaseChannelRoomInterface::create(roomName, serverName, /* creator */ QString(), /* creatorHandle */ 0, /* creationTimestamp */ QDateTime());
baseChannel->plugInterface(Tp::AbstractChannelInterfacePtr::dynamicCast(m_roomIface));
m_roomConfigIface = Tp::BaseChannelRoomConfigInterface::create();
if (m_room->isJoined()) {
m_roomConfigIface->setTitle(m_room->name());
m_roomConfigIface->setDescription(m_room->subject());
}
baseChannel->plugInterface(Tp::AbstractChannelInterfacePtr::dynamicCast(m_roomConfigIface));
connect(m_room, &QXmppMucRoom::nameChanged, this, &MucTextChannel::onRoomNameChanged);
connect(m_room, &QXmppMucRoom::messageReceived, this, &MucTextChannel::onMessageReceived);
}
void MucTextChannel::addMembers(const Tp::UIntList &contacts, const QString &reason, Tp::DBusError *error)
{
QStringList jids;
for (uint handle : contacts) {
const QString jid = m_connection->getContactIdentifier(handle);
if (jid.isEmpty()) {
if (error) {
error->set(TP_QT_ERROR_INVALID_HANDLE, QStringLiteral("Unknown handle"));
}
return;
}
jids.append(jid);
}
// Direct Invitation, XEP-0249
for (const QString &jid : jids) {
QUuid messageToken = QUuid::createUuid();
QXmppMessage message;
message.setType(QXmppMessage::Normal);
message.setTo(jid); // contacts
message.setId(messageToken.toString());
message.setMucInvitationJid(m_room->jid());
message.setMucInvitationReason(reason);
m_connection->qxmppClient()->sendPacket(message);
}
}
MucTextChannel::~MucTextChannel()
{
m_room->leave();
}
void MucTextChannel::onMessageReceived(const QXmppMessage &message)
{
uint sender = m_groupIface->memberIdentifiers().key(message.from());
if (sender == 0) {
qCDebug(general) << Q_FUNC_INFO << "unknown sender" << message.from() << "body:" << message.body();
return;
}
if (m_sentIds.contains(message.id())) {
m_sentIds.removeOne(message.id());
// TODO: Mark message as delivered
return;
}
processReceivedMessage(message, sender, message.from());
}
MucTextChannelPtr MucTextChannel::create(Connection *connection, Tp::BaseChannel *baseChannel)
{
return MucTextChannelPtr(new MucTextChannel(connection, baseChannel));
}
void MucTextChannel::onRoomNameChanged(const QString &newName)
{
m_roomConfigIface->setTitle(newName);
}
void MucTextChannel::onMucParticipantsChanged()
{
DBG;
Tp::UIntList handles;
for (const QString &participant : m_room->participants()) {
handles << m_connection->ensureContactHandle(participant);
const QXmppPresence &presence = m_room->participantPresence(participant);
m_connection->updateMucParticipantInfo(participant, presence);
m_connection->updateJidPresence(participant, presence);
}
m_groupIface->setMembers(handles, /* details */ QVariantMap());
}
bool MucTextChannel::sendQXmppMessage(QXmppMessage &message)
{
if (message.state() != QXmppMessage::None) {
return false;
}
m_sentIds << message.id();
message.setType(QXmppMessage::GroupChat);
return TextChannel::sendQXmppMessage(message);
}
QString MucTextChannel::targetJid() const
{
return m_room->jid();
}
QString MucTextChannel::selfJid() const
{
return m_connection->qxmppClient()->configuration().jid();
// return m_room->jid() + QLatin1Char('/') + m_room->nickName();
}