1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix(group): Condense invalid title handling logic, clang-format

This commit ignores the possibility of invalid length error being thrown
by a title after we've already checked its size the first time. We also
assume that a group chat cannot cease existing while we iterate over the
retrieved groups.
This commit is contained in:
jenli669 2019-06-14 23:18:00 +02:00
parent f77a062120
commit aeddf4822a
No known key found for this signature in database
GPG Key ID: 8267F9F7C2BF7E5E

View File

@ -28,8 +28,8 @@
#include "src/core/toxstring.h" #include "src/core/toxstring.h"
#include "src/model/groupinvite.h" #include "src/model/groupinvite.h"
#include "src/model/status.h" #include "src/model/status.h"
#include "src/nexus.h"
#include "src/net/bootstrapnodeupdater.h" #include "src/net/bootstrapnodeupdater.h"
#include "src/nexus.h"
#include "src/persistence/profile.h" #include "src/persistence/profile.h"
#include "src/util/strongtype.h" #include "src/util/strongtype.h"
@ -49,79 +49,78 @@ const QString Core::TOX_EXT = ".tox";
#define ASSERT_CORE_THREAD assert(QThread::currentThread() == coreThread.get()) #define ASSERT_CORE_THREAD assert(QThread::currentThread() == coreThread.get())
namespace { namespace {
bool LogConferenceTitleError(TOX_ERR_CONFERENCE_TITLE error) bool LogConferenceTitleError(TOX_ERR_CONFERENCE_TITLE error)
{ {
switch(error) switch (error) {
{ case TOX_ERR_CONFERENCE_TITLE_OK:
case TOX_ERR_CONFERENCE_TITLE_OK: break;
break; case TOX_ERR_CONFERENCE_TITLE_CONFERENCE_NOT_FOUND:
case TOX_ERR_CONFERENCE_TITLE_CONFERENCE_NOT_FOUND: qWarning() << "Conference title not found";
qWarning() << "Conference title not found"; break;
break; case TOX_ERR_CONFERENCE_TITLE_INVALID_LENGTH:
case TOX_ERR_CONFERENCE_TITLE_INVALID_LENGTH: qWarning() << "Invalid conference title length";
qWarning() << "Invalid conference title length"; break;
break; case TOX_ERR_CONFERENCE_TITLE_FAIL_SEND:
case TOX_ERR_CONFERENCE_TITLE_FAIL_SEND: qWarning() << "Failed to send title packet";
qWarning() << "Failed to send title packet";
}
return error;
} }
return error;
}
bool parseFriendSendMessageError(Tox_Err_Friend_Send_Message error) bool parseFriendSendMessageError(Tox_Err_Friend_Send_Message error)
{ {
switch (error) { switch (error) {
case TOX_ERR_FRIEND_SEND_MESSAGE_OK: case TOX_ERR_FRIEND_SEND_MESSAGE_OK:
return true; return true;
case TOX_ERR_FRIEND_SEND_MESSAGE_NULL: case TOX_ERR_FRIEND_SEND_MESSAGE_NULL:
qCritical() << "Send friend message passed an unexpected null argument"; qCritical() << "Send friend message passed an unexpected null argument";
return false; return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_FOUND: case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_FOUND:
qCritical() << "Send friend message could not find friend"; qCritical() << "Send friend message could not find friend";
return false; return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED: case TOX_ERR_FRIEND_SEND_MESSAGE_FRIEND_NOT_CONNECTED:
qCritical() << "Send friend message: friend is offline"; qCritical() << "Send friend message: friend is offline";
return false; return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ: case TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ:
qCritical() << "Failed to allocate more message queue"; qCritical() << "Failed to allocate more message queue";
return false; return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG: case TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG:
qCritical() << "Attemped to send message that's too long"; qCritical() << "Attemped to send message that's too long";
return false; return false;
case TOX_ERR_FRIEND_SEND_MESSAGE_EMPTY: case TOX_ERR_FRIEND_SEND_MESSAGE_EMPTY:
qCritical() << "Attempted to send an empty message"; qCritical() << "Attempted to send an empty message";
return false; return false;
default: default:
qCritical() << "Unknown friend send message error:" << static_cast<int>(error); qCritical() << "Unknown friend send message error:" << static_cast<int>(error);
return false; return false;
}
} }
}
bool parseConferenceSendMessageError(Tox_Err_Conference_Send_Message error) bool parseConferenceSendMessageError(Tox_Err_Conference_Send_Message error)
{ {
switch (error) { switch (error) {
case TOX_ERR_CONFERENCE_SEND_MESSAGE_OK: case TOX_ERR_CONFERENCE_SEND_MESSAGE_OK:
return true; return true;
case TOX_ERR_CONFERENCE_SEND_MESSAGE_CONFERENCE_NOT_FOUND: case TOX_ERR_CONFERENCE_SEND_MESSAGE_CONFERENCE_NOT_FOUND:
qCritical() << "Conference not found"; qCritical() << "Conference not found";
return false; return false;
case TOX_ERR_CONFERENCE_SEND_MESSAGE_FAIL_SEND: case TOX_ERR_CONFERENCE_SEND_MESSAGE_FAIL_SEND:
qCritical() << "Conference message failed to send"; qCritical() << "Conference message failed to send";
return false; return false;
case TOX_ERR_CONFERENCE_SEND_MESSAGE_NO_CONNECTION: case TOX_ERR_CONFERENCE_SEND_MESSAGE_NO_CONNECTION:
qCritical() << "No connection"; qCritical() << "No connection";
return false; return false;
case TOX_ERR_CONFERENCE_SEND_MESSAGE_TOO_LONG: case TOX_ERR_CONFERENCE_SEND_MESSAGE_TOO_LONG:
qCritical() << "Message too long"; qCritical() << "Message too long";
return false; return false;
default: default:
qCritical() << "Unknown Tox_Err_Conference_Send_Message error:" << static_cast<int>(error); qCritical() << "Unknown Tox_Err_Conference_Send_Message error:" << static_cast<int>(error);
return false; return false;
}
} }
}
} // namespace } // namespace
Core::Core(QThread* coreThread) Core::Core(QThread* coreThread)
@ -529,7 +528,8 @@ void Core::onUserStatusChanged(Tox*, uint32_t friendId, Tox_User_Status userstat
void Core::onConnectionStatusChanged(Tox*, uint32_t friendId, Tox_Connection status, void* vCore) void Core::onConnectionStatusChanged(Tox*, uint32_t friendId, Tox_Connection status, void* vCore)
{ {
Core* core = static_cast<Core*>(vCore); Core* core = static_cast<Core*>(vCore);
Status::Status friendStatus = status != TOX_CONNECTION_NONE ? Status::Status::Online : Status::Status::Offline; Status::Status friendStatus =
status != TOX_CONNECTION_NONE ? Status::Status::Online : Status::Status::Offline;
// Ignore Online because it will be emited from onUserStatusChanged // Ignore Online because it will be emited from onUserStatusChanged
bool isOffline = friendStatus == Status::Status::Offline; bool isOffline = friendStatus == Status::Status::Offline;
if (isOffline) { if (isOffline) {
@ -669,19 +669,21 @@ void Core::requestFriendship(const ToxId& friendId, const QString& message)
emit saveRequest(); emit saveRequest();
} }
bool Core::sendMessageWithType(uint32_t friendId, const QString& message, Tox_Message_Type type, ReceiptNum& receipt) bool Core::sendMessageWithType(uint32_t friendId, const QString& message, Tox_Message_Type type,
ReceiptNum& receipt)
{ {
int size = message.toUtf8().size(); int size = message.toUtf8().size();
auto maxSize = tox_max_message_length(); auto maxSize = tox_max_message_length();
if (size > maxSize) { if (size > maxSize) {
qCritical() << "Core::sendMessageWithType called with message of size:" << size << "when max is:" << maxSize <<". Ignoring."; qCritical() << "Core::sendMessageWithType called with message of size:" << size
<< "when max is:" << maxSize << ". Ignoring.";
return false; return false;
} }
ToxString cMessage(message); ToxString cMessage(message);
Tox_Err_Friend_Send_Message error; Tox_Err_Friend_Send_Message error;
receipt = ReceiptNum{tox_friend_send_message(tox.get(), friendId, type, receipt = ReceiptNum{tox_friend_send_message(tox.get(), friendId, type, cMessage.data(),
cMessage.data(), cMessage.size(), &error)}; cMessage.size(), &error)};
if (parseFriendSendMessageError(error)) { if (parseFriendSendMessageError(error)) {
return true; return true;
} }
@ -717,7 +719,7 @@ void Core::sendGroupMessageWithType(int groupId, const QString& message, Tox_Mes
for (auto& part : cMessages) { for (auto& part : cMessages) {
ToxString cMsg(part); ToxString cMsg(part);
Tox_Err_Conference_Send_Message error; Tox_Err_Conference_Send_Message error;
bool ok = bool ok =
tox_conference_send_message(tox.get(), groupId, type, cMsg.data(), cMsg.size(), &error); tox_conference_send_message(tox.get(), groupId, type, cMsg.data(), cMsg.size(), &error);
if (!ok || !parseConferenceSendMessageError(error)) { if (!ok || !parseConferenceSendMessageError(error)) {
@ -1029,27 +1031,17 @@ void Core::loadGroups()
auto groupNumbers = new uint32_t[groupCount]; auto groupNumbers = new uint32_t[groupCount];
tox_conference_get_chatlist(tox.get(), groupNumbers); tox_conference_get_chatlist(tox.get(), groupNumbers);
for(size_t i = 0; i < groupCount; ++i) { for (size_t i = 0; i < groupCount; ++i) {
TOX_ERR_CONFERENCE_TITLE error; TOX_ERR_CONFERENCE_TITLE error;
QByteArray nameByteArray;
QString name; QString name;
bool invalidTitle;
const auto groupNumber = groupNumbers[i]; const auto groupNumber = groupNumbers[i];
size_t titleSize = tox_conference_get_title_size(tox.get(), groupNumber, &error); size_t titleSize = tox_conference_get_title_size(tox.get(), groupNumber, &error);
invalidTitle = LogConferenceTitleError(error); if (LogConferenceTitleError(error)) {
if (!invalidTitle)
{
nameByteArray = QByteArray(static_cast<int>(titleSize), Qt::Uninitialized);
tox_conference_get_title(tox.get(), groupNumber, reinterpret_cast<uint8_t*>(nameByteArray.data()), &error);
invalidTitle = LogConferenceTitleError(error);
}
if (error == TOX_ERR_CONFERENCE_TITLE_CONFERENCE_NOT_FOUND)
continue;
if (invalidTitle)
{
name = tr("Groupchat %1").arg(getGroupPersistentId(groupNumber).toString().left(8)); name = tr("Groupchat %1").arg(getGroupPersistentId(groupNumber).toString().left(8));
} else { } else {
QByteArray nameByteArray = QByteArray(static_cast<int>(titleSize), Qt::Uninitialized);
tox_conference_get_title(tox.get(), groupNumber,
reinterpret_cast<uint8_t*>(nameByteArray.data()), &error);
name = ToxString(nameByteArray).getQString(); name = ToxString(nameByteArray).getQString();
} }
@ -1117,7 +1109,8 @@ GroupId Core::getGroupPersistentId(uint32_t groupNumber) const
size_t conferenceIdSize = TOX_CONFERENCE_UID_SIZE; size_t conferenceIdSize = TOX_CONFERENCE_UID_SIZE;
QByteArray groupPersistentId(conferenceIdSize, Qt::Uninitialized); QByteArray groupPersistentId(conferenceIdSize, Qt::Uninitialized);
if (tox_conference_get_id(tox.get(), groupNumber, reinterpret_cast<uint8_t*>(groupPersistentId.data()))) { if (tox_conference_get_id(tox.get(), groupNumber,
reinterpret_cast<uint8_t*>(groupPersistentId.data()))) {
return GroupId{groupPersistentId}; return GroupId{groupPersistentId};
} else { } else {
qCritical() << "Failed to get conference ID of group" << groupNumber; qCritical() << "Failed to get conference ID of group" << groupNumber;
@ -1312,7 +1305,7 @@ uint32_t Core::joinGroupchat(const GroupInvite& inviteInfo)
case TOX_CONFERENCE_TYPE_AV: { case TOX_CONFERENCE_TYPE_AV: {
qDebug() << QString("Trying to join AV groupchat invite sent by friend %1").arg(friendId); qDebug() << QString("Trying to join AV groupchat invite sent by friend %1").arg(friendId);
groupNum = toxav_join_av_groupchat(tox.get(), friendId, cookie, cookieLength, groupNum = toxav_join_av_groupchat(tox.get(), friendId, cookie, cookieLength,
CoreAV::groupCallCallback, const_cast<Core*>(this)); CoreAV::groupCallCallback, const_cast<Core*>(this));
break; break;
} }
default: default: