Skip to content

Commit

Permalink
Fixes for #34 (comment)
Browse files Browse the repository at this point in the history
Also fixes possible crashes
  • Loading branch information
maksis committed Mar 31, 2019
1 parent 1e6f046 commit 204ffae
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 48 deletions.
4 changes: 2 additions & 2 deletions airdcpp-webapi/api/ApiSettingItem.cpp
Expand Up @@ -364,8 +364,8 @@ namespace webserver {
} else if (si.key == SettingsManager::BIND_ADDRESS || si.key == SettingsManager::BIND_ADDRESS6) {
auto bindAddresses = AirUtil::getBindAdapters(si.key == SettingsManager::BIND_ADDRESS6);
for (const auto& adapter : bindAddresses) {
auto name = adapter.ip + (!adapter.adapterName.empty() ? " (" + adapter.adapterName + ")" : Util::emptyString);
ret.emplace_back(EnumOption({ adapter.ip, name }));
auto title = adapter.ip + (!adapter.adapterName.empty() ? " (" + adapter.adapterName + ")" : Util::emptyString);
ret.emplace_back(EnumOption({ adapter.ip, title }));
}
} else if (si.key == SettingsManager::MAPPER) {
auto mappers = ConnectivityManager::getInstance()->getMappers(false);
Expand Down
6 changes: 3 additions & 3 deletions airdcpp-webapi/api/PrivateChatApi.cpp
Expand Up @@ -93,13 +93,13 @@ namespace webserver {

api_return PrivateChatApi::handlePostChat(ApiRequest& aRequest) {
auto user = Deserializer::deserializeHintedUser(aRequest.getRequestBody());
auto chat = PrivateChatManager::getInstance()->addChat(user, false);
if (!chat) {
auto res = PrivateChatManager::getInstance()->addChat(user, false);
if (!res.second) {
aRequest.setResponseErrorStr("Chat session exists");
return websocketpp::http::status_code::conflict;
}

aRequest.setResponseBody(serializeChat(chat));
aRequest.setResponseBody(serializeChat(res.first));
return websocketpp::http::status_code::ok;
}

Expand Down
3 changes: 3 additions & 0 deletions airdcpp-webapi/api/SessionApi.cpp
Expand Up @@ -123,6 +123,9 @@ namespace webserver {
aRequest.setResponseErrorStr(e.what());
return websocketpp::http::status_code::bad_request;
}
} else {
JsonUtil::throwError("grant_type", JsonUtil::ERROR_INVALID, "Invalid grant_type");
return websocketpp::http::status_code::bad_request;
}

dcassert(session);
Expand Down
2 changes: 1 addition & 1 deletion airdcpp-webapi/api/base/ApiModule.cpp
Expand Up @@ -38,7 +38,7 @@ namespace webserver {
if (aPathTokens.size() < params.size()) {
return nullopt;
}
} else if (method != METHOD_FORWARD && aPathTokens.size() != params.size()) {
} else if (aPathTokens.size() != params.size()) {
return nullopt;
}

Expand Down
4 changes: 2 additions & 2 deletions airdcpp/airdcpp/Client.cpp
Expand Up @@ -369,7 +369,7 @@ bool Client::sendMessage(const string& aMessage, string& error_, bool aThirdPers

auto error = ClientManager::getInstance()->outgoingHubMessageHook.runHooksError(aMessage, aThirdPerson, *this);
if (error) {
error_ = error->formatError(error);
error_ = ActionHookRejection::formatError(error);
return false;
}

Expand All @@ -389,7 +389,7 @@ bool Client::sendPrivateMessage(const OnlineUserPtr& aUser, const string& aMessa

auto error = ClientManager::getInstance()->outgoingPrivateMessageHook.runHooksError(aMessage, aThirdPerson, HintedUser(aUser->getUser(), aUser->getHubUrl()), aEcho);
if (error) {
error_ = error->formatError(error);
error_ = ActionHookRejection::formatError(error);
return false;
}

Expand Down
14 changes: 6 additions & 8 deletions airdcpp/airdcpp/DirectoryListing.cpp
Expand Up @@ -1011,15 +1011,13 @@ void DirectoryListing::onLoadingFinished(int64_t aStartTime, const string& aBase
dir = root;
}

if (dir) {
dir->setLoading(false);
if (!aBackgroundTask) {
updateCurrentLocation(dir);
read = false;
}

onStateChanged();
dir->setLoading(false);
if (!aBackgroundTask) {
updateCurrentLocation(dir);
read = false;
}

onStateChanged();

fire(DirectoryListingListener::LoadingFinished(), aStartTime, dir->getAdcPath(), aBackgroundTask);
}
Expand Down
24 changes: 12 additions & 12 deletions airdcpp/airdcpp/PrivateChatManager.cpp
Expand Up @@ -52,23 +52,22 @@ PrivateChatManager::~PrivateChatManager() noexcept {
ConnectionManager::getInstance()->disconnect();
}

PrivateChatPtr PrivateChatManager::addChat(const HintedUser& aUser, bool aReceivedMessage) noexcept {
if (getChat(aUser.user)) {
return nullptr;
}

pair<PrivateChatPtr, bool> PrivateChatManager::addChat(const HintedUser& aUser, bool aReceivedMessage) noexcept {
PrivateChatPtr chat;

auto user = ClientManager::getInstance()->checkOnlineUrl(aUser);

{
WLock l(cs);
chat = std::make_shared<PrivateChat>(user, getPMConn(user.user));
chats.emplace(user.user, chat);
auto res = chats.emplace(user.user, std::make_shared<PrivateChat>(user, getPMConn(user.user)));
chat = res.first->second;
if (!res.second) {
return { chat, false };
}
}

fire(PrivateChatManagerListener::ChatCreated(), chat, aReceivedMessage);
return chat;
return { chat, true };
}

PrivateChatPtr PrivateChatManager::getChat(const UserPtr& aUser) const noexcept {
Expand Down Expand Up @@ -171,27 +170,28 @@ void PrivateChatManager::onPrivateMessage(const ChatMessagePtr& aMessage) {
}
}

auto c = aMessage->getFrom()->getClient();
if (wndCnt > 200) {
DisconnectCCPM(user);
return;
}


const auto client = aMessage->getFrom()->getClient();
const auto& identity = aMessage->getReplyTo()->getIdentity();
if ((identity.isBot() && !SETTING(POPUP_BOT_PMS)) || (identity.isHub() && !SETTING(POPUP_HUB_PMS))) {
c->addLine(STRING(PRIVATE_MESSAGE_FROM) + " " + identity.getNick() + ": " + aMessage->format());
client->addLine(STRING(PRIVATE_MESSAGE_FROM) + " " + identity.getNick() + ": " + aMessage->format());
return;
}

auto chat = addChat(HintedUser(user, aMessage->getReplyTo()->getClient()->getHubUrl()), true);
auto chat = addChat(HintedUser(user, client->getHubUrl()), true).first;
chat->handleMessage(aMessage);

if (ActivityManager::getInstance()->isAway() && !myPM && (!SETTING(NO_AWAYMSG_TO_BOTS) || !user->isSet(User::BOT))) {
ParamMap params;
aMessage->getFrom()->getIdentity().getParams(params, "user", false);

string error;
chat->sendMessage(ActivityManager::getInstance()->getAwayMessage(c->get(HubSettings::AwayMsg), params), error, false);
chat->sendMessage(ActivityManager::getInstance()->getAwayMessage(client->get(HubSettings::AwayMsg), params), error, false);
}
}

Expand Down
3 changes: 2 additions & 1 deletion airdcpp/airdcpp/PrivateChatManager.h
Expand Up @@ -42,7 +42,8 @@ namespace dcpp {
PrivateChatManager() noexcept;
~PrivateChatManager() noexcept;

PrivateChatPtr addChat(const HintedUser& user, bool aReceivedMessage) noexcept;
// Returns the chat session and boolean whether the session was newly created
pair<PrivateChatPtr, bool> addChat(const HintedUser& user, bool aReceivedMessage) noexcept;
PrivateChatPtr getChat(const UserPtr& aUser) const noexcept;

void DisconnectCCPM(const UserPtr& aUser);
Expand Down
24 changes: 12 additions & 12 deletions airdcpp/airdcpp/QueueItem.cpp
Expand Up @@ -231,36 +231,36 @@ string QueueItem::getListName() const noexcept {

/* INTERNAL */
uint8_t QueueItem::getMaxSegments(int64_t aFileSize) noexcept {
uint8_t maxSegments = 1;
uint8_t ret = 1;

if(SETTING(SEGMENTS_MANUAL)) {
maxSegments = min((uint8_t)SETTING(NUMBER_OF_SEGMENTS), (uint8_t)10);
ret = min((uint8_t)SETTING(NUMBER_OF_SEGMENTS), (uint8_t)10);
} else {
if ((aFileSize >= 2*1048576) && (aFileSize < 15*1048576)) {
maxSegments = 2;
ret = 2;
} else if((aFileSize >= (int64_t)15*1048576) && (aFileSize < (int64_t)30*1048576)) {
maxSegments = 3;
ret = 3;
} else if((aFileSize >= (int64_t)30*1048576) && (aFileSize < (int64_t)60*1048576)) {
maxSegments = 4;
ret = 4;
} else if((aFileSize >= (int64_t)60*1048576) && (aFileSize < (int64_t)120*1048576)) {
maxSegments = 5;
ret = 5;
} else if((aFileSize >= (int64_t)120*1048576) && (aFileSize < (int64_t)240*1048576)) {
maxSegments = 6;
ret = 6;
} else if((aFileSize >= (int64_t)240*1048576) && (aFileSize < (int64_t)480*1048576)) {
maxSegments = 7;
ret = 7;
} else if((aFileSize >= (int64_t)480*1048576) && (aFileSize < (int64_t)960*1048576)) {
maxSegments = 8;
ret = 8;
} else if((aFileSize >= (int64_t)960*1048576) && (aFileSize < (int64_t)1920*1048576)) {
maxSegments = 9;
ret = 9;
} else if(aFileSize >= (int64_t)1920*1048576) {
maxSegments = 10;
ret = 10;
}
}

#ifdef _DEBUG
return 88;
#else
return maxSegments;
return ret;
#endif
}

Expand Down
5 changes: 1 addition & 4 deletions airdcpp/airdcpp/QueueManager.cpp
Expand Up @@ -3457,10 +3457,7 @@ void QueueManager::handleBundleUpdate(QueueToken aBundleToken) noexcept {

if (b) {
if (b->isSet(Bundle::FLAG_UPDATE_SIZE)) {
if (b->isSet(Bundle::FLAG_UPDATE_SIZE)) {
fire(QueueManagerListener::BundleSize(), b);
}

fire(QueueManagerListener::BundleSize(), b);
DownloadManager::getInstance()->sendSizeUpdate(b);
}

Expand Down
4 changes: 2 additions & 2 deletions airdcpp/airdcpp/SharePathValidator.cpp
Expand Up @@ -211,15 +211,15 @@ void SharePathValidator::validate(FileFindIter& aIter, const string& aPath, bool

auto error = directoryValidationHook.runHooksError(aPath);
if (error) {
throw ShareException(error->formatError(error));
throw ShareException(ActionHookRejection::formatError(error));
}
} else {
auto size = aIter->getSize();
checkSharedName(aPath, false, size);

auto error = fileValidationHook.runHooksError(aPath, size);
if (error) {
throw ShareException(error->formatError(error));
throw ShareException(ActionHookRejection::formatError(error));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion airdcpp/airdcpp/UserConnection.cpp
Expand Up @@ -236,7 +236,7 @@ void UserConnection::inf(bool withToken, int mcnSlots) {
bool UserConnection::pm(const string& aMessage, string& error_, bool aThirdPerson) {
auto error = ClientManager::getInstance()->outgoingPrivateMessageHook.runHooksError(aMessage, aThirdPerson, getHintedUser(), true);
if (error) {
error_ = error->formatError(error);
error_ = ActionHookRejection::formatError(error);
return false;
}

Expand Down

0 comments on commit 204ffae

Please sign in to comment.