diff --git a/src/api/contact.h b/src/api/contact.h index 6a4e7535b2b601216fbb3bf39b40037f57ff32ec..07a6b76daebf0b270995fb827b6047ddb6e4026c 100644 --- a/src/api/contact.h +++ b/src/api/contact.h @@ -39,6 +39,7 @@ struct Info std::string registeredName; bool isTrusted = false; bool isPresent = false; + bool isBanned = false; }; } // namespace contact diff --git a/src/bannedcontactmodel.cpp b/src/bannedcontactmodel.cpp index cbe3b8f46955964f48bb53b22a49471bb06d3bcc..e4b7e88118788045d1a5aab672d7ec45c129f42e 100644 --- a/src/bannedcontactmodel.cpp +++ b/src/bannedcontactmodel.cpp @@ -137,8 +137,9 @@ BannedContactModel::columnCount( const QModelIndex& parent ) const void BannedContactModel::add(ContactMethod* cm) { - if (d_ptr->m_lBanned.contains(cm)) + if (isBanned(cm)) return; + beginInsertRows(QModelIndex(),d_ptr->m_lBanned.size(),d_ptr->m_lBanned.size()); d_ptr->m_lBanned << cm; endInsertRows(); @@ -151,6 +152,7 @@ BannedContactModel::add(ContactMethod* cm) void BannedContactModel::remove(ContactMethod* cm) { + // Do not remove contact if contact isn't banned auto rowIndex = d_ptr->m_lBanned.indexOf(cm); if (rowIndex < 0) return; @@ -166,3 +168,13 @@ BannedContactModel::remove(ContactMethod* cm) ConfigurationManager::instance().addContact(cm->account()->id(), cm->uri()); } + +/** + * this function returns whether passed ContactMethod is in the banned list or not. + * @param cm, the ContactMethod whose presence in the banned list should be checked. + */ +bool +BannedContactModel::isBanned(ContactMethod* cm) +{ + return d_ptr->m_lBanned.contains(cm); +} diff --git a/src/bannedcontactmodel.h b/src/bannedcontactmodel.h index 84ec37362b79c35b222425698b4f12a6a32fb182..98143df097835d89df8fd08a246b0f4972e44429 100644 --- a/src/bannedcontactmodel.h +++ b/src/bannedcontactmodel.h @@ -45,6 +45,7 @@ public: // Helper void add(ContactMethod* cm); void remove(ContactMethod* cm); + bool isBanned(ContactMethod* cm); private: virtual ~BannedContactModel(); diff --git a/src/contactmodel.cpp b/src/contactmodel.cpp index a5d0486de1613105261f69c091a5ab38537988d5..9dd339c7b4e708ce53a54a541a4b23fa56e27319 100644 --- a/src/contactmodel.cpp +++ b/src/contactmodel.cpp @@ -37,6 +37,7 @@ #include "contactmethod.h" #include "namedirectory.h" #include "phonedirectorymodel.h" +#include "bannedcontactmodel.h" #include "private/vcardutils.h" #include "authority/daemon.h" @@ -470,6 +471,7 @@ ContactModelPimpl::fillsWithRINGContacts() { contact::Info contactInfo; contactInfo.profileInfo = profileInfo; contactInfo.registeredName = cm->registeredName().toStdString(); + contactInfo.isBanned = account->bannedContactModel()->isBanned(cm); { std::lock_guard<std::mutex> lk(contactsMtx_); @@ -546,6 +548,13 @@ ContactModelPimpl::addToContacts(ContactMethod* cm, const profile::Type& type) auto contactInfo = database::buildContactFromProfileId(db, contactId); contactInfo.registeredName = cm->registeredName().toStdString(); + auto* account = AccountModel::instance().getById(linked.owner.id.c_str()); + if (not account) { + qDebug() << "ContactModel::addToContacts(), nullptr"; + return; + } + + contactInfo.isBanned = account->bannedContactModel()->isBanned(cm); contactInfo.isPresent = cm->isPresent(); contactInfo.profileInfo.type = type; // Because PENDING should not be stored in the database auto iter = contacts.find(contactInfo.profileInfo.uri); diff --git a/src/conversationmodel.cpp b/src/conversationmodel.cpp index 20e898cd89d26eededbbd625eacafa5598a4a5ed..21fc64294cf33e9d2c7713e1cde4cd58b9f25975 100644 --- a/src/conversationmodel.cpp +++ b/src/conversationmodel.cpp @@ -503,6 +503,12 @@ ConversationModelPimpl::placeCall(const std::string& uid, bool isAudioOnly) if (url.empty()) return; // Incorrect item + // Don't call banned contact + if (contactInfo.isBanned) { + qDebug() << "ContactModel::placeCall: denied, contact is banned"; + return; + } + sendContactRequest(participant); if (linked.owner.profileInfo.type != profile::Type::SIP) { @@ -510,8 +516,13 @@ ConversationModelPimpl::placeCall(const std::string& uid, bool isAudioOnly) } // If call is with temporary contact, conversation has been removed and must be updated + int contactIndex; + if (isTemporary && (contactIndex = indexOfContact(convId)) < 0) { + qDebug() << "Can't place call: Other participant is not a contact"; + return; + } - auto& newConv = isTemporary ? conversations.at(indexOfContact(convId)) : conversation; + auto& newConv = isTemporary ? conversations.at(contactIndex) : conversation; convId = newConv.uid; newConv.callId = linked.owner.callModel->createCall(url, isAudioOnly); @@ -556,6 +567,12 @@ ConversationModel::sendMessage(const std::string& uid, const std::string& body) auto status = interaction::Status::SENDING; for (const auto& participant: conversation.participants) { auto contactInfo = owner.contactModel->getContact(participant); + + if (contactInfo.isBanned) { + qDebug() << "ContactModel::sendMessage: denied, contact is banned"; + return; + } + pimpl_->sendContactRequest(participant); QStringList callLists = CallManager::instance().getCallList(); // no auto @@ -576,8 +593,13 @@ ConversationModel::sendMessage(const std::string& uid, const std::string& body) // If first interaction with temporary contact, we have to update the conversations info // at this stage + int contactIndex; + if (isTemporary && (contactIndex = pimpl_->indexOfContact(convId)) < 0) { + qDebug() << "Can't send message: Other participant is not a contact"; + return; + } - auto& newConv = isTemporary ? pimpl_->conversations.at(pimpl_->indexOfContact(convId)) : conversation; + auto& newConv = isTemporary ? pimpl_->conversations.at(contactIndex) : conversation; convId = newConv.uid; // Add interaction to database