1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
|
/*
* Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdinc.h"
#include "ClientManager.h"
#include "ShareManager.h"
#include "SearchManager.h"
#include "ConnectionManager.h"
#include "CryptoManager.h"
#include "FavoriteManager.h"
#include "SimpleXML.h"
#include "UserCommand.h"
#include "SearchResult.h"
#include "AdcHub.h"
#include "NmdcHub.h"
#ifdef WITH_DHT
#include "dht/DHT.h"
#endif
#include "FinishedManager.h"//sdc
#include "QueueManager.h"
namespace dcpp {
Client* ClientManager::getClient(const string& aHubURL) {
Client* c;
if(strncmp("adc://", aHubURL.c_str(), 6) == 0) {
c = new AdcHub(aHubURL, false);
} else if(strncmp("adcs://", aHubURL.c_str(), 7) == 0) {
c = new AdcHub(aHubURL, true);
} else if(strncmp("nmdcs://", aHubURL.c_str(), 8) == 0) {
c = new NmdcHub(aHubURL, true);
} else {
c = new NmdcHub(aHubURL, false);
}
{
Lock l(cs);
clients.push_back(c);
}
c->addListener(this);
return c;
}
void ClientManager::putClient(Client* aClient) {
fire(ClientManagerListener::ClientDisconnected(), aClient);
aClient->removeListeners();
{
Lock l(cs);
clients.remove(aClient);
}
aClient->shutdown();
delete aClient;
}
size_t ClientManager::getUserCount() const {
Lock l(cs);
return onlineUsers.size();
}
StringList ClientManager::getHubs(const CID& cid, const string& hintUrl) {
return getHubs(cid, hintUrl, FavoriteManager::getInstance()->isPrivate(hintUrl));
}
StringList ClientManager::getHubNames(const CID& cid, const string& hintUrl) {
return getHubNames(cid, hintUrl, FavoriteManager::getInstance()->isPrivate(hintUrl));
}
StringList ClientManager::getNicks(const CID& cid, const string& hintUrl) {
return getNicks(cid, hintUrl, FavoriteManager::getInstance()->isPrivate(hintUrl));
}
StringList ClientManager::getHubs(const CID& cid, const string& hintUrl, bool priv) {
Lock l(cs);
StringList lst;
if(!priv) {
OnlinePairC op = onlineUsers.equal_range(cid);
for(auto i = op.first; i != op.second; ++i) {
lst.push_back(i->second->getClient().getHubUrl());
}
} else {
OnlineUser* u = findOnlineUserHint(cid, hintUrl);
if(u)
lst.push_back(u->getClient().getHubUrl());
}
return lst;
}
StringList ClientManager::getHubNames(const CID& cid, const string& hintUrl, bool priv) {
Lock l(cs);
StringList lst;
if(!priv) {
OnlinePairC op = onlineUsers.equal_range(cid);
for(auto i = op.first; i != op.second; ++i) {
lst.push_back(i->second->getClient().getHubName());
}
} else {
OnlineUser* u = findOnlineUserHint(cid, hintUrl);
if(u)
lst.push_back(u->getClient().getHubName());
}
return lst;
}
StringList ClientManager::getNicks(const CID& cid, const string& hintUrl, bool priv) {
Lock l(cs);
StringSet ret;
if(!priv) {
OnlinePairC op = onlineUsers.equal_range(cid);
for(auto i = op.first; i != op.second; ++i) {
ret.insert(i->second->getIdentity().getNick());
}
} else {
OnlineUser* u = findOnlineUserHint(cid, hintUrl);
if(u)
ret.insert(u->getIdentity().getNick());
}
if(ret.empty()) {
auto i = nicks.find(cid);
if(i != nicks.end()) {
ret.insert(i->second.first);
} else {
// Offline perhaps?
ret.insert('{' + cid.toBase32() + '}');
}
}
return StringList(ret.begin(), ret.end());
}
string ClientManager::getField(const CID& cid, const string& hint, const char* field) const {
Lock l(cs);
OnlinePairC p;
auto u = findOnlineUserHint(cid, hint, p);
if(u) {
auto value = u->getIdentity().get(field);
if(!value.empty()) {
return value;
}
}
for(auto i = p.first; i != p.second; ++i) {
auto value = i->second->getIdentity().get(field);
if(!value.empty()) {
return value;
}
}
return Util::emptyString;
}
string ClientManager::getConnection(const CID& cid) const {
Lock l(cs);
auto i = onlineUsers.find(cid);
if(i != onlineUsers.end()) {
return i->second->getIdentity().getConnection();
}
return _("Offline");
}
int64_t ClientManager::getAvailable() const {
Lock l(cs);
int64_t bytes = 0;
for(auto i = onlineUsers.begin(); i != onlineUsers.end(); ++i) {
bytes += i->second->getIdentity().getBytesShared();
}
return bytes;
}
uint8_t ClientManager::getSlots(const CID& cid) const {
Lock l(cs);
OnlineIterC i = onlineUsers.find(cid);
if(i != onlineUsers.end()) {
return static_cast<uint8_t>(Util::toInt(i->second->getIdentity().get("SL")));
}
return 0;
}
bool ClientManager::isConnected(const string& aUrl) const {
Lock l(cs);
for(auto i = clients.begin(); i != clients.end(); ++i) {
if((*i)->getHubUrl() == aUrl) {
return true;
}
}
return false;
}
string ClientManager::findHub(const string& ipPort) const {
Lock l(cs);
string ip;
uint16_t port = 411;
Util::parseIpPort(ipPort, ip, port);
string url;
for(auto i = clients.begin(); i != clients.end(); ++i) {
const Client* c = *i;
if(c->getIp() == ip) {
// If exact match is found, return it
if(c->getPort() == port)
return c->getHubUrl();
// Port is not always correct, so use this as a best guess...
url = c->getHubUrl();
}
}
return url;
}
string ClientManager::findHubEncoding(const string& aUrl) const {
Lock l(cs);
for(auto i = clients.begin(); i != clients.end(); ++i) {
if((*i)->getHubUrl() == aUrl) {
return (*i)->getEncoding();
}
}
return Text::hubDefaultCharset;
}
UserPtr ClientManager::findLegacyUser(const string& aNick) const noexcept {
if (aNick.empty())
return UserPtr();
Lock l(cs);
for(auto i = onlineUsers.begin(); i != onlineUsers.end(); ++i) {
const OnlineUser* ou = i->second;
if(ou->getUser()->isSet(User::NMDC) && Util::stricmp(ou->getIdentity().getNick(), aNick) == 0)
return ou->getUser();
}
return UserPtr();
}
UserPtr ClientManager::getUser(const string& aNick, const string& aHubUrl) noexcept {
CID cid = makeCid(aNick, aHubUrl);
Lock l(cs);
auto ui = users.find(cid);
if(ui != users.end()) {
ui->second->setFlag(User::NMDC);
return ui->second;
}
UserPtr p(new User(cid));
p->setFlag(User::NMDC);
users.insert(make_pair(cid, p));
return p;
}
UserPtr ClientManager::getUser(const CID& cid) noexcept {
Lock l(cs);
auto ui = users.find(cid);
if(ui != users.end()) {
return ui->second;
}
UserPtr p(new User(cid));
users.insert(make_pair(cid, p));
return p;
}
UserPtr ClientManager::findUser(const CID& cid) const noexcept {
Lock l(cs);
auto ui = users.find(cid);
return ui == users.end() ? 0 : ui->second;
}
bool ClientManager::isOp(const UserPtr& user, const string& aHubUrl) const {
Lock l(cs);
OnlinePairC p = onlineUsers.equal_range(user->getCID());
for(auto i = p.first; i != p.second; ++i) {
if(i->second->getClient().getHubUrl() == aHubUrl) {
return i->second->getIdentity().isOp();
}
}
return false;
}
CID ClientManager::makeCid(const string& aNick, const string& aHubUrl) const noexcept {
string n = Text::toLower(aNick);
TigerHash th;
th.update(n.c_str(), n.length());
th.update(Text::toLower(aHubUrl).c_str(), aHubUrl.length());
// Construct hybrid CID from the bits of the tiger hash - should be
// fairly random, and hopefully low-collision
return CID(th.finalize());
}
void ClientManager::putOnline(OnlineUser* ou) noexcept {
{
Lock l(cs);
onlineUsers.insert(make_pair(ou->getUser()->getCID(), ou));
}
if(!ou->getUser()->isOnline()) {
ou->getUser()->setFlag(User::ONLINE);
fire(ClientManagerListener::UserConnected(), ou->getUser());
}
}
void ClientManager::putOffline(OnlineUser* ou, bool disconnect) noexcept {
bool lastUser = false;
{
Lock l(cs);
OnlinePair op = onlineUsers.equal_range(ou->getUser()->getCID());
dcassert(op.first != op.second);
for(OnlineIter i = op.first; i != op.second; ++i) {
OnlineUser* ou2 = i->second;
if(ou == ou2) {
lastUser = (distance(op.first, op.second) == 1);
onlineUsers.erase(i);
break;
}
}
}
if(lastUser) {
UserPtr& u = ou->getUser();
u->unsetFlag(User::ONLINE);
if(disconnect)
ConnectionManager::getInstance()->disconnect(u);
fire(ClientManagerListener::UserDisconnected(), u);
}
}
OnlineUser* ClientManager::findOnlineUserHint(const CID& cid, const string& hintUrl, OnlinePairC& p) const {
p = onlineUsers.equal_range(cid);
if(p.first == p.second) // no user found with the given CID.
return 0;
if(!hintUrl.empty()) {
for(auto i = p.first; i != p.second; ++i) {
OnlineUser* u = i->second;
if(u->getClient().getHubUrl() == hintUrl) {
return u;
}
}
}
return 0;
}
OnlineUser* ClientManager::findOnlineUser(const HintedUser& user, bool priv) {
return findOnlineUser(user.user->getCID(), user.hint, priv);
}
OnlineUser* ClientManager::findOnlineUser(const CID& cid, const string& hintUrl, bool priv) {
OnlinePairC p;
OnlineUser* u = findOnlineUserHint(cid, hintUrl, p);
if(u) // found an exact match (CID + hint).
return u;
if(p.first == p.second) // no user found with the given CID.
return 0;
// if the hint hub is private, don't allow connecting to the same user from another hub.
if(priv)
return 0;
// ok, hub not private, return a random user that matches the given CID but not the hint.
return p.first->second;
}
void ClientManager::connect(const HintedUser& user, const string& token) {
bool priv = FavoriteManager::getInstance()->isPrivate(user.hint);
Lock l(cs);
OnlineUser* u = findOnlineUser(user, priv);
if(u) {
u->getClient().connect(*u, token);
}
}
void ClientManager::privateMessage(const HintedUser& user, const string& msg, bool thirdPerson) {
bool priv = FavoriteManager::getInstance()->isPrivate(user.hint);
Lock l(cs);
OnlineUser* u = findOnlineUser(user, priv);
if(u) {
u->getClient().privateMessage(*u, msg, thirdPerson);
}
}
void ClientManager::userCommand(const HintedUser& user, const UserCommand& uc, StringMap& params, bool compatibility) {
Lock l(cs);
/** @todo we allow wrong hints for now ("false" param of findOnlineUser) because users
* extracted from search results don't always have a correct hint; see
* SearchManager::onRES(const AdcCommand& cmd, ...). when that is done, and SearchResults are
* switched to storing only reliable HintedUsers (found with the token of the ADC command),
* change this call to findOnlineUserHint. */
OnlineUser* ou = findOnlineUser(user.user->getCID(), user.hint.empty() ? uc.getHub() : user.hint, false);
if(!ou
#ifdef WITH_DHT
|| ou->getClientBase().type == ClientBase::DHT
#endif
)
return;
ou->getIdentity().getParams(params, "user", compatibility);
ou->getClient().getHubIdentity().getParams(params, "hub", false);
ou->getClient().getMyIdentity().getParams(params, "my", compatibility);
ou->getClient().escapeParams(params);
ou->getClient().sendUserCmd(uc, params);
}
void ClientManager::send(AdcCommand& cmd, const CID& cid) {
Lock l(cs);
auto i = onlineUsers.find(cid);
if(i != onlineUsers.end()) {
OnlineUser& u = *i->second;
if(cmd.getType() == AdcCommand::TYPE_UDP && !u.getIdentity().isUdpActive()) {
if(u.getUser()->isNMDC()
#ifdef WITH_DHT
|| u.getClientBase().getType() == Client::DHT
#endif
)
return;
cmd.setType(AdcCommand::TYPE_DIRECT);
cmd.setTo(u.getIdentity().getSID());
u.getClient().send(cmd);
} else {
try {
udp.writeTo(u.getIdentity().getIp(), static_cast<uint16_t>(Util::toInt(u.getIdentity().getUdpPort())), cmd.toString(getMe()->getCID()));
} catch(const SocketException&) {
dcdebug("Socket exception sending ADC UDP command\n");
}
}
}
}
void ClientManager::infoUpdated() {
Lock l(cs);
for(auto i = clients.begin(); i != clients.end(); ++i) {
if((*i)->isConnected()) {
(*i)->info(false);
}
}
}
void ClientManager::on(NmdcSearch, Client* aClient, const string& aSeeker, int aSearchType, int64_t aSize,
int aFileType, const string& aString) noexcept
{
Speaker<ClientManagerListener>::fire(ClientManagerListener::IncomingSearch(), aString);
bool isPassive = (aSeeker.compare(0, 4, "Hub:") == 0);
bool isTTHSearch = ((aFileType == SearchManager::TYPE_TTH) && (aString.compare(0, 4, "TTH:") == 0));
// We don't wan't to answer passive searches if we're in passive mode...
if(isPassive && !ClientManager::getInstance()->isActive(aClient->getHubUrl())) {
return;
}
SearchResultList l;
ShareManager::getInstance()->search(l, aString, aSearchType, aSize, aFileType, aClient, isPassive ? 5 : 10);
// dcdebug("Found %d items (%s)\n", l.size(), aString.c_str());
if(!l.empty()) {
if(isPassive) {
string name = aSeeker.substr(4);
// Good, we have a passive seeker, those are easier...
string str;
for(auto i = l.begin(); i != l.end(); ++i) {
const SearchResultPtr& sr = *i;
str += sr->toSR(*aClient);
str[str.length()-1] = 5;
str += name;
str += '|';
}
if(!str.empty())
aClient->send(str);
} else {
try {
string ip;
uint16_t port = 0;
Util::parseIpPort(aSeeker, ip, port);
ip = Socket::resolve(ip);
if(static_cast<NmdcHub*>(aClient)->isProtectedIP(ip))
return;
if(port == 0)
port = 412;
for(auto i = l.begin(); i != l.end(); ++i) {
const SearchResultPtr& sr = *i;
udp.writeTo(ip, port, sr->toSR(*aClient));
}
} catch(const SocketException& /* e */) {
dcdebug("Search caught error\n");
}
}
} else if(!isPassive && isTTHSearch) {
PartsInfo partialInfo;
TTHValue aTTH(aString.substr(4));
if(!QueueManager::getInstance()->handlePartialSearch(aTTH, partialInfo)) {
// if not found, try to find in finished list
if(!FinishedManager::getInstance()->handlePartialRequest(aTTH, partialInfo)) {
return;
}
}
string ip;
uint16_t port = 0;
Util::parseIpPort(aSeeker, ip, port);
if (port == 0) {
return;
}
try {
AdcCommand cmd = SearchManager::getInstance()->toPSR(true, aClient->getMyNick(), aClient->getIpPort(), aTTH.toBase32(), partialInfo);
Socket s;
s.writeTo(Socket::resolve(ip), port, cmd.toString(ClientManager::getInstance()->getMe()->getCID()));
} catch(...) {
dcdebug("Partial search caught error\n");
}
}
}
void ClientManager::on(AdcSearch, Client* c, const AdcCommand& adc, const CID& from) noexcept {
bool isUdpActive = false;
{
Lock l(cs);
auto i = onlineUsers.find(from);
if(i != onlineUsers.end()) {
OnlineUser& u = *i->second;
isUdpActive = u.getIdentity().isUdpActive();
}
}
SearchManager::getInstance()->respond(adc, from, isUdpActive, c->getIpPort());
}
void ClientManager::search(int aSizeMode, int64_t aSize, int aFileType, const string& aString, const string& aToken, void* aOwner) {
#ifdef WITH_DHT
if(BOOLSETTING(USE_DHT) && aFileType == SearchManager::TYPE_TTH)
dht::DHT::getInstance()->findFile(aString);
#endif
Lock l(cs);
for(auto i = clients.begin(); i != clients.end(); ++i) {
if((*i)->isConnected()) {
(*i)->search(aSizeMode, aSize, aFileType, aString, aToken, StringList() /*ExtList*/, aOwner);
}
}
}
uint64_t ClientManager::search(StringList& who, int aSizeMode, int64_t aSize, int aFileType, const string& aString, const string& aToken, const StringList& aExtList, void* aOwner) {
#ifdef WITH_DHT
if(BOOLSETTING(USE_DHT) && aFileType == SearchManager::TYPE_TTH)
dht::DHT::getInstance()->findFile(aString, aToken);
#endif
Lock l(cs);
uint64_t estimateSearchSpan = 0;
for(auto it = who.begin(); it != who.end(); ++it) {
string& client = *it;
for(auto j = clients.begin(); j != clients.end(); ++j) {
Client* c = *j;
if(c->isConnected() && c->getHubUrl() == client) {
uint64_t ret = c->search(aSizeMode, aSize, aFileType, aString, aToken, aExtList, aOwner);
estimateSearchSpan = max(estimateSearchSpan, ret);
}
}
}
return estimateSearchSpan;
}
void ClientManager::on(TimerManagerListener::Minute, uint64_t /* aTick */) noexcept {
Lock l(cs);
// Collect some garbage...
auto i = users.begin();
while(i != users.end()) {
if(i->second->unique()) {
users.erase(i++);
} else {
++i;
}
}
for(auto j = clients.begin(); j != clients.end(); ++j) {
(*j)->info(false);
}
}
UserPtr& ClientManager::getMe() {
if(!me) {
Lock l(cs);
if(!me) {
me = new User(getMyCID());
users.insert(make_pair(me->getCID(), me));
}
}
return me;
}
const CID& ClientManager::getMyPID() {
if(pid.isZero())
pid = CID(SETTING(PRIVATE_ID));
return pid;
}
CID ClientManager::getMyCID() {
TigerHash tiger;
tiger.update(getMyPID().data(), CID::SIZE);
return CID(tiger.finalize());
}
void ClientManager::updateNick(const OnlineUser& user) noexcept {
if(!user.getIdentity().getNick().empty()) {
Lock l(cs);
auto i = nicks.find(user.getUser()->getCID());
if(i == nicks.end()) {
nicks[user.getUser()->getCID()] = std::make_pair(user.getIdentity().getNick(), false);
} else {
i->second.first = user.getIdentity().getNick();
}
}
}
void ClientManager::loadUsers() {
try {
SimpleXML xml;
xml.fromXML(File(getUsersFile(), File::READ, File::OPEN).read());
if(xml.findChild("Users")) {
xml.stepIn();
{
Lock l(cs);
while(xml.findChild("User")) {
nicks[CID(xml.getChildAttrib("CID"))] = std::make_pair(xml.getChildAttrib("Nick"), false);
}
}
xml.stepOut();
}
} catch(const Exception&) { }
}
void ClientManager::saveUsers() const {
try {
SimpleXML xml;
xml.addTag("Users");
xml.stepIn();
{
Lock l(cs);
for(auto i = nicks.begin(), iend = nicks.end(); i != iend; ++i) {
if(i->second.second) {
xml.addTag("User");
xml.addChildAttrib("CID", i->first.toBase32());
xml.addChildAttrib("Nick", i->second.first);
}
}
}
xml.stepOut();
const string fName = getUsersFile();
File out(fName + ".tmp", File::WRITE, File::CREATE | File::TRUNCATE);
BufferedOutputStream<false> f(&out);
f.write(SimpleXML::utf8Header);
xml.toXML(&f);
f.flush();
out.close();
File::deleteFile(fName);
File::renameFile(fName + ".tmp", fName);
} catch(const Exception&) { }
}
void ClientManager::saveUser(const CID& cid) {
Lock l(cs);
auto i = nicks.find(cid);
if(i != nicks.end())
i->second.second = true;
}
void ClientManager::on(Connected, Client* c) noexcept {
fire(ClientManagerListener::ClientConnected(), c);
}
void ClientManager::on(UserUpdated, Client*, const OnlineUser& user) noexcept {
updateNick(user);
fire(ClientManagerListener::UserUpdated(), user);
}
void ClientManager::on(UsersUpdated, Client* c, const OnlineUserList& l) noexcept {
for(auto i = l.cbegin(), iend = l.cend(); i != iend; ++i) {
updateNick(*(*i));
fire(ClientManagerListener::UserUpdated(), *(*i));
}
}
void ClientManager::on(HubUpdated, Client* c) noexcept {
fire(ClientManagerListener::ClientUpdated(), c);
}
void ClientManager::on(Failed, Client* client, const string&) noexcept {
fire(ClientManagerListener::ClientDisconnected(), client);
}
void ClientManager::on(HubUserCommand, Client* client, int aType, int ctx, const string& name, const string& command) noexcept {
if(BOOLSETTING(HUB_USER_COMMANDS)) {
if(aType == UserCommand::TYPE_REMOVE) {
int cmd = FavoriteManager::getInstance()->findUserCommand(name, client->getHubUrl());
if(cmd != -1)
FavoriteManager::getInstance()->removeUserCommand(cmd);
} else if(aType == UserCommand::TYPE_CLEAR) {
FavoriteManager::getInstance()->removeHubUserCommands(ctx, client->getHubUrl());
} else {
FavoriteManager::getInstance()->addUserCommand(aType, ctx, UserCommand::FLAG_NOSAVE, name, command, "", client->getHubUrl());
}
}
}
int ClientManager::getMode(const string& aHubUrl) const {
if(aHubUrl.empty())
return SETTING(INCOMING_CONNECTIONS);
int mode = 0;
const FavoriteHubEntry* hub = FavoriteManager::getInstance()->getFavoriteHubEntry(aHubUrl);
if(hub) {
switch(hub->getMode()) {
case 1 :
mode = SettingsManager::INCOMING_DIRECT;
break;
case 2 :
mode = SettingsManager::INCOMING_FIREWALL_PASSIVE;
break;
default:
mode = SETTING(INCOMING_CONNECTIONS);
}
} else {
mode = SETTING(INCOMING_CONNECTIONS);
}
return mode;
}
void ClientManager::cancelSearch(void* aOwner) {
Lock l(cs);
for(auto i = clients.begin(); i != clients.end(); ++i) {
(*i)->cancelSearch(aOwner);
}
}
#ifdef WITH_DHT
OnlineUserPtr ClientManager::findDHTNode(const CID& cid) const {
Lock l(cs);
OnlinePairC op = onlineUsers.equal_range(cid);
for(auto i = op.first; i != op.second; ++i) {
OnlineUser* ou = i->second;
// user not in DHT, so don't bother with other hubs
if(!ou->getUser()->isSet(User::DHT))
break;
if(ou->getClientBase().getType() == Client::DHT)
return ou;
}
return NULL;
}
#endif
#ifdef LUA_SCRIPT
bool ClientManager::ucExecuteLua(const string& ucCommand, StringMap& params) noexcept {
bool executedlua = false;
string::size_type i, j, k;
i = j = k = 0;
string tmp = ucCommand;
while( (i = tmp.find("%[lua:", i)) != string::npos) {
i += 6;
j = tmp.find(']', i);
if(j == string::npos)
break;
string chunk = tmp.substr(i, j-i);
// for making possible using %[nick] and similar parameters too
// !%!{nick!}
k = 0;
while( (k = chunk.find("!%")) != string::npos) {
chunk.erase(k, 2);
chunk.insert(k, "%");
}
k = 0;
while( (k = chunk.find("!{")) != string::npos) {
chunk.erase(k, 2);
chunk.insert(k, "[");
}
k = 0;
while( (k = chunk.find("!}")) != string::npos) {
chunk.erase(k, 2);
chunk.insert(k, "]");
}
//@todo: use filter? I opted for no here, but this means Lua has to be careful about
//filtering if it cares.
ScriptManager::getInstance()->EvaluateChunk(Util::formatParams(chunk, params, false));
executedlua = true;
i = j + 1;
}
return executedlua;
}
#endif // LUA_SCRIPT
} // namespace dcpp
|