// Load all searches from manager void WaitingUsersFrame::LoadAll() { // Load queue User::List users = UploadManager::getInstance()->getWaitingUsers(); for (User::Iter uit = users.begin(); uit != users.end(); ++uit) { HTREEITEM lastInserted = ctrlQueued.InsertItem(TVIF_PARAM | TVIF_TEXT, (WinUtil::getNicks(*uit) + _T(" - ") + WinUtil::getHubNames(*uit).first).c_str(), 0, 0, 0, 0, (LPARAM)(new UserPtr(*uit)), TVI_ROOT, TVI_LAST); UploadManager::FileSet files = UploadManager::getInstance()->getWaitingUserFiles(*uit); for (UploadManager::FileSet::const_iterator fit = files.begin(); fit != files.end(); ++fit) { ctrlQueued.InsertItem(Text::toT(*fit).c_str(), lastInserted, TVI_LAST); } } }
void UploadManager::on(TimerManagerListener::Minute, time_t /* aTick */) throw() { User::List disconnects; { Lock l(cs); UserList::iterator i = stable_partition(waitingUsers.begin(), waitingUsers.end(), WaitingUserFresh()); for (UserList::iterator j = i; j != waitingUsers.end(); ++j) { FilesMap::iterator fit = waitingFiles.find(j->first); if (fit != waitingFiles.end()) waitingFiles.erase(fit); fire(UploadManagerListener::WaitingRemoveUser(), j->first); } waitingUsers.erase(i, waitingUsers.end()); if( BOOLSETTING(AUTO_KICK) ) { for(Upload::Iter i = uploads.begin(); i != uploads.end(); ++i) { Upload* u = *i; if(u->getUser()->isOnline()) { u->unsetFlag(Upload::FLAG_PENDING_KICK); continue; } if(u->isSet(Upload::FLAG_PENDING_KICK)) { disconnects.push_back(u->getUser()); continue; } if(BOOLSETTING(AUTO_KICK_NO_FAVS) && u->getUser()->isFavoriteUser()) { continue; } u->setFlag(Upload::FLAG_PENDING_KICK); } } } for(User::Iter i = disconnects.begin(); i != disconnects.end(); ++i) { LogManager::getInstance()->message(STRING(DISCONNECTED_USER) + (*i)->getNick()); ConnectionManager::getInstance()->disconnect(*i, false); } }
void NmdcHub::onLine(const string& aLine) throw() { lastActivity = GET_TICK(); if(aLine.length() == 0) return; if(aLine[0] != '$') { // Check if we're being banned... if(state != STATE_CONNECTED) { if(Util::findSubString(aLine, "banned") != string::npos) { reconnect = false; } } Speaker<NmdcHubListener>::fire(NmdcHubListener::Message(), this, Util::validateMessage(fromNmdc(aLine), true)); return; } string cmd; string param; string::size_type x; if( (x = aLine.find(' ')) == string::npos) { cmd = aLine; } else { cmd = aLine.substr(0, x); param = aLine.substr(x+1); } if(cmd == "$Search") { if(state != STATE_CONNECTED) { return; } string::size_type i = 0; string::size_type j = param.find(' ', i); if(j == string::npos || i == j) return; string seeker = fromNmdc(param.substr(i, j-i)); // Filter own searches if(SETTING(CONNECTION_TYPE) == SettingsManager::CONNECTION_ACTIVE) { if(seeker == (getLocalIp() + ":" + Util::toString(SETTING(IN_PORT)))) { return; } } else { // Hub:seeker if(Util::stricmp(seeker.c_str() + 4, getNick().c_str()) == 0) { return; } } i = j + 1; { Lock l(cs); u_int32_t tick = GET_TICK(); seekers.push_back(make_pair(seeker, tick)); // First, check if it's a flooder FloodIter fi; for(fi = flooders.begin(); fi != flooders.end(); ++fi) { if(fi->first == seeker) { return; } } int count = 0; for(fi = seekers.begin(); fi != seekers.end(); ++fi) { if(fi->first == seeker) count++; if(count > 7) { if(seeker.compare(0, 4, "Hub:") == 0) Speaker<NmdcHubListener>::fire(NmdcHubListener::SearchFlood(), this, seeker.substr(4)); else Speaker<NmdcHubListener>::fire(NmdcHubListener::SearchFlood(), this, seeker + STRING(NICK_UNKNOWN)); flooders.push_back(make_pair(seeker, tick)); return; } } } int a; if(param[i] == 'F') { a = SearchManager::SIZE_DONTCARE; } else if(param[i+2] == 'F') { a = SearchManager::SIZE_ATLEAST; } else { a = SearchManager::SIZE_ATMOST; } i += 4; j = param.find('?', i); if(j == string::npos || i == j) return; string size = param.substr(i, j-i); i = j + 1; j = param.find('?', i); if(j == string::npos || i == j) return; int type = Util::toInt(param.substr(i, j-i)) - 1; i = j + 1; param = param.substr(i); if(param.size() > 0) { Speaker<NmdcHubListener>::fire(NmdcHubListener::Search(), this, seeker, a, Util::toInt64(size), type, fromNmdc(param)); if(seeker.compare(0, 4, "Hub:") == 0) { User::Ptr u; { Lock l(cs); User::NickIter ni = users.find(seeker.substr(4)); if(ni != users.end() && !ni->second->isSet(User::PASSIVE)) { u = ni->second; u->setFlag(User::PASSIVE); } } if(u) { updated(u); } } } } else if(cmd == "$MyINFO") { string::size_type i, j; i = 5; j = param.find(' ', i); if( (j == string::npos) || (j == i) ) return; string nick = fromNmdc(param.substr(i, j-i)); i = j + 1; User::Ptr u; dcassert(nick.size() > 0); { Lock l(cs); User::NickIter ni = users.find(nick); if(ni == users.end()) { u = users[nick] = ClientManager::getInstance()->getUser(nick, this); } else { u = ni->second; } } j = param.find('$', i); if(j == string::npos) return; string tmpDesc = Util::validateMessage(fromNmdc(param.substr(i, j-i)), true); // Look for a tag... if(tmpDesc.size() > 0 && tmpDesc[tmpDesc.size()-1] == '>') { x = tmpDesc.rfind('<'); if(x != string::npos) { // Hm, we have something... u->setTag(tmpDesc.substr(x)); tmpDesc.erase(x); } else { u->setTag(Util::emptyString); } } else { u->setTag(Util::emptyString); } u->setDescription(tmpDesc); i = j + 3; j = param.find('$', i); if(j == string::npos) return; u->setConnection(fromNmdc(param.substr(i, j-i-1))); i = j + 1; j = param.find('$', i); if(j == string::npos) return; u->setEmail(Util::validateMessage(fromNmdc(param.substr(i, j-i)), true)); i = j + 1; j = param.find('$', i); if(j == string::npos) return; u->setBytesShared(param.substr(i, j-i)); Speaker<NmdcHubListener>::fire(NmdcHubListener::MyInfo(), this, u); } else if(cmd == "$Quit") { if(!param.empty()) { User::Ptr u; { Lock l(cs); User::NickIter i = users.find(fromNmdc(param)); if(i == users.end()) { dcdebug("C::onLine Quitting user %s not found\n", param.c_str()); return; } u = i->second; users.erase(i); } Speaker<NmdcHubListener>::fire(NmdcHubListener::Quit(), this, u); ClientManager::getInstance()->putUserOffline(u, true); } } else if(cmd == "$ConnectToMe") { if(state != STATE_CONNECTED) { return; } string::size_type i = param.find(' '); string::size_type j; if( (i == string::npos) || ((i + 1) >= param.size()) ) { return; } i++; j = param.find(':', i); if(j == string::npos) { return; } string server = fromNmdc(param.substr(i, j-i)); if(j+1 >= param.size()) { return; } string port = param.substr(j+1); ConnectionManager::getInstance()->connect(server, (short)Util::toInt(port), getNick()); Speaker<NmdcHubListener>::fire(NmdcHubListener::ConnectToMe(), this, server, (short)Util::toInt(port)); } else if(cmd == "$RevConnectToMe") { if(state != STATE_CONNECTED) { return; } User::Ptr u; bool up = false; { Lock l(cs); string::size_type j = param.find(' '); if(j == string::npos) { return; } User::NickIter i = users.find(fromNmdc(param.substr(0, j))); if(i == users.end()) { return; } u = i->second; if(!u->isSet(User::PASSIVE)) { u->setFlag(User::PASSIVE); up = true; } } if(u) { if(SETTING(CONNECTION_TYPE) == SettingsManager::CONNECTION_ACTIVE) { connectToMe(u); Speaker<NmdcHubListener>::fire(NmdcHubListener::RevConnectToMe(), this, u); } else { // Notify the user that we're passive too... if(up) revConnectToMe(u); } if(up) updated(u); } } else if(cmd == "$SR") { SearchManager::getInstance()->onSearchResult(aLine); } else if(cmd == "$HubName") { name = fromNmdc(param); Speaker<NmdcHubListener>::fire(NmdcHubListener::HubName(), this); } else if(cmd == "$Supports") { StringTokenizer<string> st(param, ' '); StringList& sl = st.getTokens(); for(StringIter i = sl.begin(); i != sl.end(); ++i) { if(*i == "UserCommand") { supportFlags |= SUPPORTS_USERCOMMAND; } else if(*i == "NoGetINFO") { supportFlags |= SUPPORTS_NOGETINFO; } else if(*i == "UserIP2") { supportFlags |= SUPPORTS_USERIP2; } } Speaker<NmdcHubListener>::fire(NmdcHubListener::Supports(), this, sl); } else if(cmd == "$UserCommand") { string::size_type i = 0; string::size_type j = param.find(' '); if(j == string::npos) return; int type = Util::toInt(param.substr(0, j)); i = j+1; if(type == UserCommand::TYPE_SEPARATOR || type == UserCommand::TYPE_CLEAR) { int ctx = Util::toInt(param.substr(i)); Speaker<NmdcHubListener>::fire(NmdcHubListener::UserCommand(), this, type, ctx, Util::emptyString, Util::emptyString); } else if(type == UserCommand::TYPE_RAW || type == UserCommand::TYPE_RAW_ONCE) { j = param.find(' ', i); if(j == string::npos) return; int ctx = Util::toInt(param.substr(i)); i = j+1; j = param.find('$'); if(j == string::npos) return; string name = fromNmdc(param.substr(i, j-i)); i = j+1; string command = fromNmdc(param.substr(i, param.length() - i)); Speaker<NmdcHubListener>::fire(NmdcHubListener::UserCommand(), this, type, ctx, Util::validateMessage(name, true, false), Util::validateMessage(command, true, false)); } } else if(cmd == "$Lock") { if(state != STATE_LOCK) { return; } state = STATE_HELLO; if(!param.empty()) { string::size_type j = param.find(" Pk="); string lock, pk; if( j != string::npos ) { lock = param.substr(0, j); pk = param.substr(j + 4); } else { // Workaround for faulty linux hubs... j = param.find(" "); if(j != string::npos) lock = param.substr(0, j); else lock = param; } if(CryptoManager::getInstance()->isExtended(lock)) { StringList feat; feat.push_back("UserCommand"); feat.push_back("NoGetINFO"); feat.push_back("NoHello"); feat.push_back("UserIP2"); feat.push_back("TTHSearch"); if(BOOLSETTING(COMPRESS_TRANSFERS)) feat.push_back("GetZBlock"); supports(feat); } key(CryptoManager::getInstance()->makeKey(lock)); validateNick(getNick()); Speaker<NmdcHubListener>::fire(NmdcHubListener::CLock(), this, lock, pk); } } else if(cmd == "$Hello") { if(!param.empty()) { string nick = fromNmdc(param); User::Ptr u = ClientManager::getInstance()->getUser(nick, this); { Lock l(cs); users[nick] = u; } if(getNick() == nick) { setMe(u); u->setFlag(User::DCPLUSPLUS); if(SETTING(CONNECTION_TYPE) != SettingsManager::CONNECTION_ACTIVE) u->setFlag(User::PASSIVE); else u->unsetFlag(User::PASSIVE); } if(state == STATE_HELLO) { state = STATE_CONNECTED; updateCounts(false); version(); getNickList(); myInfo(); } Speaker<NmdcHubListener>::fire(NmdcHubListener::Hello(), this, u); } } else if(cmd == "$ForceMove") { disconnect(); Speaker<NmdcHubListener>::fire(NmdcHubListener::Redirect(), this, param); } else if(cmd == "$HubIsFull") { Speaker<NmdcHubListener>::fire(NmdcHubListener::HubFull(), this); } else if(cmd == "$ValidateDenide") { // Mind the spelling... disconnect(); Speaker<NmdcHubListener>::fire(NmdcHubListener::ValidateDenied(), this); } else if(cmd == "$UserIP") { if(!param.empty()) { User::List v; StringTokenizer<string> t(fromNmdc(param), "$$"); StringList& l = t.getTokens(); for(StringIter it = l.begin(); it != l.end(); ++it) { string::size_type j = 0; if((j = it->find(' ')) == string::npos) continue; if((j+1) == it->length()) continue; v.push_back(ClientManager::getInstance()->getUser(it->substr(0, j), this)); v.back()->setIp(it->substr(j+1)); } Speaker<NmdcHubListener>::fire(NmdcHubListener::UserIp(), this, v); } } else if(cmd == "$NickList") { if(!param.empty()) { User::List v; StringTokenizer<string> t(fromNmdc(param), "$$"); StringList& sl = t.getTokens(); for(StringIter it = sl.begin(); it != sl.end(); ++it) { v.push_back(ClientManager::getInstance()->getUser(*it, this)); } { Lock l(cs); for(User::Iter it2 = v.begin(); it2 != v.end(); ++it2) { users[(*it2)->getNick()] = *it2; } } if(!(getSupportFlags() & SUPPORTS_NOGETINFO)) { string tmp; // Let's assume 10 characters per nick... tmp.reserve(v.size() * (11 + 10 + getNick().length())); for(User::List::const_iterator i = v.begin(); i != v.end(); ++i) { tmp += "$GetINFO "; tmp += (*i)->getNick(); tmp += ' '; tmp += getNick(); tmp += '|'; } if(!tmp.empty()) { send(tmp); } } Speaker<NmdcHubListener>::fire(NmdcHubListener::NickList(), this, v); } } else if(cmd == "$OpList") { if(!param.empty()) { User::List v; StringTokenizer<string> t(fromNmdc(param), "$$"); StringList& sl = t.getTokens(); for(StringIter it = sl.begin(); it != sl.end(); ++it) { v.push_back(ClientManager::getInstance()->getUser(*it, this)); v.back()->setFlag(User::OP); } { Lock l(cs); for(User::Iter it2 = v.begin(); it2 != v.end(); ++it2) { users[(*it2)->getNick()] = *it2; } } Speaker<NmdcHubListener>::fire(NmdcHubListener::OpList(), this, v); updateCounts(false); // Special...to avoid op's complaining that their count is not correctly // updated when they log in (they'll be counted as registered first...) myInfo(); } } else if(cmd == "$To:") { string::size_type i = param.find("From:"); if(i != string::npos) { i+=6; string::size_type j = param.find("$"); if(j != string::npos) { string from = fromNmdc(param.substr(i, j - 1 - i)); if(from.size() > 0 && param.size() > (j + 1)) { Speaker<NmdcHubListener>::fire(NmdcHubListener::PrivateMessage(), this, ClientManager::getInstance()->getUser(from, this, false), Util::validateMessage(fromNmdc(param.substr(j + 1)), true)); } } } } else if(cmd == "$GetPass") { setRegistered(true); Speaker<NmdcHubListener>::fire(NmdcHubListener::GetPassword(), this); } else if(cmd == "$BadPass") { Speaker<NmdcHubListener>::fire(NmdcHubListener::BadPassword(), this); } else if(cmd == "$LogedIn") { Speaker<NmdcHubListener>::fire(NmdcHubListener::LoggedIn(), this); } else { dcassert(cmd[0] == '$'); dcdebug("NmdcHub::onLine Unknown command %s\n", aLine.c_str()); } }
void ConnectionManager::on(TimerManagerListener::Second, uint32_t aTick) throw() { User::List passiveUsers; ConnectionQueueItem::List removed; User::List idlers; { Lock l(cs); bool attemptDone = false; idlers = checkIdle; checkIdle.clear(); for(ConnectionQueueItem::Iter i = downloads.begin(); i != downloads.end(); ++i) { ConnectionQueueItem* cqi = *i; if(cqi->getState() != ConnectionQueueItem::ACTIVE) { if(!cqi->getUser()->isOnline()) { // Not online anymore...remove it from the pending... removed.push_back(cqi); continue; } if(cqi->getUser()->isSet(User::PASSIVE) && !ClientManager::getInstance()->isActive()) { passiveUsers.push_back(cqi->getUser()); removed.push_back(cqi); continue; } if( ((cqi->getLastAttempt() + 60*1000) < aTick) && !attemptDone ) { cqi->setLastAttempt(aTick); QueueItem::Priority prio = QueueManager::getInstance()->hasDownload(cqi->getUser()); if(prio == QueueItem::PAUSED) { removed.push_back(cqi); continue; } bool startDown = DownloadManager::getInstance()->startDownload(prio); if(cqi->getState() == ConnectionQueueItem::WAITING) { if(startDown) { cqi->setState(ConnectionQueueItem::CONNECTING); ClientManager::getInstance()->connect(cqi->getUser()); fire(ConnectionManagerListener::StatusChanged(), cqi); attemptDone = true; } else { cqi->setState(ConnectionQueueItem::NO_DOWNLOAD_SLOTS); fire(ConnectionManagerListener::Failed(), cqi, STRING(ALL_DOWNLOAD_SLOTS_TAKEN)); } } else if(cqi->getState() == ConnectionQueueItem::NO_DOWNLOAD_SLOTS && startDown) { cqi->setState(ConnectionQueueItem::WAITING); } } else if(((cqi->getLastAttempt() + 50*1000) < aTick) && (cqi->getState() == ConnectionQueueItem::CONNECTING)) { fire(ConnectionManagerListener::Failed(), cqi, STRING(CONNECTION_TIMEOUT)); cqi->setState(ConnectionQueueItem::WAITING); } } } for(ConnectionQueueItem::Iter m = removed.begin(); m != removed.end(); ++m) { putCQI(*m); } } for(User::Iter i = idlers.begin(); i != idlers.end(); ++i) { DownloadManager::getInstance()->checkIdle(*i); } for(User::Iter ui = passiveUsers.begin(); ui != passiveUsers.end(); ++ui) { QueueManager::getInstance()->removeSource(*ui, QueueItem::Source::FLAG_PASSIVE); } }
void ConnectionManager::on(TimerManagerListener::Second, u_int32_t aTick) throw() { ConnectionQueueItem::List failPassive; ConnectionQueueItem::List connecting; ConnectionQueueItem::List removed; User::List getDown; { Lock l(cs); { for(User::Iter k = pendingAdd.begin(); k != pendingAdd.end(); ++k) { ConnectionQueueItem::Iter i = find(downPool.begin(), downPool.end(), *k); if(i == downPool.end()) { // Hm, connection must have failed before it could be collected... getDown.push_back(*k); } else { ConnectionQueueItem* cqi = *i; downPool.erase(i); dcassert(find(active.begin(), active.end(), cqi) == active.end()); active.push_back(cqi); dcassert(cqi->getConnection()); dcassert(cqi->getConnection()->getCQI() == cqi); cqi->getConnection()->removeListener(this); DownloadManager::getInstance()->addConnection(cqi->getConnection()); } } pendingAdd.clear(); } bool tooMany = ((SETTING(DOWNLOAD_SLOTS) != 0) && DownloadManager::getInstance()->getDownloads() >= (size_t)SETTING(DOWNLOAD_SLOTS)); bool tooFast = ((SETTING(MAX_DOWNLOAD_SPEED) != 0 && DownloadManager::getInstance()->getAverageSpeed() >= (SETTING(MAX_DOWNLOAD_SPEED)*1024))); bool startDown = !tooMany && !tooFast; int attempts = 0; ConnectionQueueItem::Iter i = pendingDown.begin(); while(i != pendingDown.end()) { ConnectionQueueItem* cqi = *i; dcassert(cqi->getUser()); if(!cqi->getUser()->isOnline()) { // Not online anymore...remove him from the pending... i = pendingDown.erase(i); removed.push_back(cqi); continue; } if( ((cqi->getLastAttempt() + 60*1000) < aTick) && (attempts < 2) ) { cqi->setLastAttempt(aTick); if(!QueueManager::getInstance()->hasDownload(cqi->getUser())) { i = pendingDown.erase(i); removed.push_back(cqi); continue; } if(cqi->getUser()->isSet(User::PASSIVE) && (SETTING(CONNECTION_TYPE) != SettingsManager::CONNECTION_ACTIVE)) { i = pendingDown.erase(i); failPassive.push_back(cqi); continue; } // Always start high-priority downloads unless we have 3 more than maxdownslots already... if(!startDown) { bool extraFull = (SETTING(DOWNLOAD_SLOTS) != 0) && (DownloadManager::getInstance()->getDownloads() >= (size_t)(SETTING(DOWNLOAD_SLOTS)+3)); startDown = !extraFull && QueueManager::getInstance()->hasDownload(cqi->getUser(), QueueItem::HIGHEST); } if(cqi->getState() == ConnectionQueueItem::WAITING) { if(startDown) { cqi->setState(ConnectionQueueItem::CONNECTING); cqi->getUser()->connect(); fire(ConnectionManagerListener::StatusChanged(), cqi); attempts++; } else { cqi->setState(ConnectionQueueItem::NO_DOWNLOAD_SLOTS); fire(ConnectionManagerListener::Failed(), cqi, STRING(ALL_DOWNLOAD_SLOTS_TAKEN)); } } else if(cqi->getState() == ConnectionQueueItem::NO_DOWNLOAD_SLOTS && startDown) { cqi->setState(ConnectionQueueItem::WAITING); } } else if(((cqi->getLastAttempt() + 50*1000) < aTick) && (cqi->getState() == ConnectionQueueItem::CONNECTING)) { fire(ConnectionManagerListener::Failed(), cqi, STRING(CONNECTION_TIMEOUT)); cqi->setState(ConnectionQueueItem::WAITING); } ++i; } } ConnectionQueueItem::Iter m; for(m = removed.begin(); m != removed.end(); ++m) { fire(ConnectionManagerListener::Removed(), *m); delete *m; } for(m = failPassive.begin(); m != failPassive.end(); ++m) { QueueManager::getInstance()->removeSources((*m)->getUser(), QueueItem::Source::FLAG_PASSIVE); fire(ConnectionManagerListener::Removed(), *m); delete *m; } for(User::Iter n = getDown.begin(); n != getDown.end(); ++n) { getDownloadConnection(*n); } }