void SearchManager::onRES(const AdcCommand& cmd, const UserPtr& from, const string& remoteIp) { int freeSlots = -1; int64_t size = -1; string file; string tth; string token; for(auto& str: cmd.getParameters()) { if(str.compare(0, 2, "FN") == 0) { file = Util::toNmdcFile(str.substr(2)); } else if(str.compare(0, 2, "SL") == 0) { freeSlots = Util::toInt(str.substr(2)); } else if(str.compare(0, 2, "SI") == 0) { size = Util::toInt64(str.substr(2)); } else if(str.compare(0, 2, "TR") == 0) { tth = str.substr(2); } else if(str.compare(0, 2, "TO") == 0) { token = str.substr(2); } } if(file.empty() || freeSlots == -1 || size == -1) { return; } auto type = (*(file.end() - 1) == '\\' ? SearchResult::TYPE_DIRECTORY : SearchResult::TYPE_FILE); if(type == SearchResult::TYPE_FILE && tth.empty()) { return; } string hubUrl; // token format: [per-hub unique id] "/" [per-search actual token] (see AdcHub::search) auto slash = token.find('/'); if(slash == string::npos) { return; } { auto uniqueId = Util::toUInt32(token.substr(0, slash)); auto lock = ClientManager::getInstance()->lock(); auto& clients = ClientManager::getInstance()->getClients(); auto i = boost::find_if(clients, [uniqueId](const Client* client) { return client->getUniqueId() == uniqueId; }); if(i == clients.end()) { return; } hubUrl = (*i)->getHubUrl(); } token.erase(0, slash + 1); StringList names = ClientManager::getInstance()->getHubNames(from->getCID()); string hubName = names.empty() ? _("Offline") : Util::toString(names); /// @todo Something about the slots fire(SearchManagerListener::SR(), SearchResultPtr(new SearchResult(HintedUser(from, hubUrl), type, 0, freeSlots, size, file, hubName, remoteIp, TTHValue(tth), token))); }
void SearchManager::respond(const AdcCommand& cmd, const OnlineUser& user) { // Filter own searches if(user.getUser() == ClientManager::getInstance()->getMe()) return; auto results = ShareManager::getInstance()->search(cmd.getParameters(), user.getIdentity().isUdpActive() ? 10 : 5); if(results.empty()) return; string token; cmd.getParam("TO", 0, token); for(auto& i: results) { AdcCommand res = i->toRES(AdcCommand::TYPE_UDP); if(!token.empty()) res.addParam("TO", token); ClientManager::getInstance()->sendUDP(res, user); } }
void UploadManager::on(AdcCommand::GFI, UserConnection* aSource, const AdcCommand& c) throw() { if(c.getParameters().size() < 2) { aSource->send(AdcCommand(AdcCommand::SEV_RECOVERABLE, AdcCommand::ERROR_PROTOCOL_GENERIC, "Missing parameters")); return; } const string& type = c.getParam(0); const string& ident = c.getParam(1); if(type == Transfer::TYPE_FILE) { try { aSource->send(ShareManager::getInstance()->getFileInfo(ident)); } catch(const ShareException&) { aSource->fileNotAvail(); } } else { aSource->fileNotAvail(); } }
void SearchManager::respond(const AdcCommand& adc, const CID& from, bool isUdpActive, const string& hubIpPort) { // Filter own searches if(from == ClientManager::getInstance()->getMe()->getCID()) return; UserPtr p = ClientManager::getInstance()->findUser(from); if(!p) return; SearchResultList results; ShareManager::getInstance()->search(results, adc.getParameters(), isUdpActive ? 10 : 5); string token; adc.getParam("TO", 0, token); // TODO: don't send replies to passive users if(results.empty()) { string tth; if(!adc.getParam("TR", 0, tth)) return; PartsInfo partialInfo; if(!QueueManager::getInstance()->handlePartialSearch(TTHValue(tth), partialInfo)) { // if not found, try to find in finished list if(!FinishedManager::getInstance()->handlePartialRequest(TTHValue(tth), partialInfo)) { return; } } AdcCommand cmd = toPSR(true, Util::emptyString, hubIpPort, tth, partialInfo); ClientManager::getInstance()->send(cmd, from); return; } for(SearchResultList::const_iterator i = results.begin(); i != results.end(); ++i) { AdcCommand cmd = (*i)->toRES(AdcCommand::TYPE_UDP); if(!token.empty()) cmd.addParam("TO", token); ClientManager::getInstance()->send(cmd, from); } }
void SearchManager::onPSR(const AdcCommand& cmd, UserPtr from, const string& remoteIp) { uint16_t udpPort = 0; uint32_t partialCount = 0; string tth; string hubIpPort; string nick; PartsInfo partialInfo; for(StringIterC i = cmd.getParameters().begin(); i != cmd.getParameters().end(); ++i) { const string& str = *i; if(str.compare(0, 2, "U4") == 0) { udpPort = static_cast<uint16_t>(Util::toInt(str.substr(2))); } else if(str.compare(0, 2, "NI") == 0) { nick = str.substr(2); } else if(str.compare(0, 2, "HI") == 0) { hubIpPort = str.substr(2); } else if(str.compare(0, 2, "TR") == 0) { tth = str.substr(2); } else if(str.compare(0, 2, "PC") == 0) { partialCount = Util::toUInt32(str.substr(2))*2; } else if(str.compare(0, 2, "PI") == 0) { StringTokenizer<string> tok(str.substr(2), ','); for(StringIter i = tok.getTokens().begin(); i != tok.getTokens().end(); ++i) { partialInfo.push_back((uint16_t)Util::toInt(*i)); } } } string url = ClientManager::getInstance()->findHub(hubIpPort); if(!from || from == ClientManager::getInstance()->getMe()) { // for NMDC support if(nick.empty() || hubIpPort.empty()) { return; } from = ClientManager::getInstance()->findUser(nick, url); if(!from) { // Could happen if hub has multiple URLs / IPs from = ClientManager::getInstance()->findLegacyUser(nick); if(!from) { dcdebug("Search result from unknown user"); return; } } } //ClientManager::getInstance()->setIPUser(from, remoteIp, udpPort); if(partialInfo.size() != partialCount) { // what to do now ? just ignore partial search result :-/ return; } PartsInfo outPartialInfo; QueueItem::PartialSource ps(from->isNMDC() ? ClientManager::getInstance()->getClient(url)->getMyIdentity().getNick() : Util::emptyString, hubIpPort, remoteIp, udpPort); ps.setPartialInfo(partialInfo); QueueManager::getInstance()->handlePartialResult(from, url, TTHValue(tth), ps, outPartialInfo); if((udpPort > 0) && !outPartialInfo.empty()) { try { AdcCommand cmd = SearchManager::getInstance()->toPSR(false, ps.getMyNick(), hubIpPort, tth, outPartialInfo); ClientManager::getInstance()->send(cmd, from->getCID()); } catch(...) { dcdebug("Partial search caught error\n"); } } }