Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include "stdafx.h"
  2. #include "constants.h"
  3. #include "gm.h"
  4. #include "messenger_manager.h"
  5. #include "buffer_manager.h"
  6. #include "desc_client.h"
  7. #include "log.h"
  8. #include "config.h"
  9. #include "p2p.h"
  10. #include "crc32.h"
  11. #include "char.h"
  12. #include "char_manager.h"
  13. #include "questmanager.h"
  14.  
  15. MessengerManager::MessengerManager()
  16. {
  17. }
  18.  
  19. MessengerManager::~MessengerManager()
  20. {
  21. }
  22.  
  23. void MessengerManager::Initialize()
  24. {
  25. }
  26.  
  27. void MessengerManager::Destroy()
  28. {
  29. }
  30.  
  31. void MessengerManager::P2PLogin(MessengerManager::keyA account)
  32. {
  33. Login(account);
  34. }
  35.  
  36. void MessengerManager::P2PLogout(MessengerManager::keyA account)
  37. {
  38. Logout(account);
  39. }
  40.  
  41. void MessengerManager::Login(MessengerManager::keyA account)
  42. {
  43. if (m_set_loginAccount.find(account) != m_set_loginAccount.end())
  44. return;
  45.  
  46. DBManager::instance().FuncQuery(std::bind(&MessengerManager::LoadList, this, std::placeholders::_1),
  47. "SELECT account, companion FROM messenger_list%s WHERE account='%s'", get_table_postfix(), account.c_str());
  48.  
  49. m_set_loginAccount.insert(account);
  50.  
  51. #ifdef ENABLE_MESSENGER_TEAM
  52. DBManager::instance().FuncQuery(std::bind1st(std::mem_fun(&MessengerManager::LoadTeamList), this),
  53. "SELECT '%s',mName FROM common.gmlist WHERE mName!='%s'", account.c_str(), account.c_str());
  54. #endif
  55. }
  56.  
  57. #ifdef ENABLE_MESSENGER_TEAM
  58. void MessengerManager::LoadTeamList(SQLMsg * msg)
  59. {
  60. if (NULL == msg or NULL == msg->Get() or msg->Get()->uiNumRows == 0)
  61. return;
  62.  
  63. std::string account;
  64.  
  65. for (uint i = 0; i < msg->Get()->uiNumRows; ++i)
  66. {
  67. MYSQL_ROW row = mysql_fetch_row(msg->Get()->pSQLResult);
  68.  
  69. if (row[0] && row[1])
  70. {
  71. if (account.length() == 0)
  72. account = row[0];
  73.  
  74. m_TeamRelation[row[0]].insert(row[1]);
  75. m_InverseTeamRelation[row[1]].insert(row[0]);
  76. }
  77. }
  78.  
  79. SendTeamList(account);
  80.  
  81. std::set<MessengerManager::keyT>::iterator it;
  82.  
  83. for (it = m_InverseTeamRelation[account].begin(); it != m_InverseTeamRelation[account].end(); ++it)
  84. SendTeamLogin(*it, account);
  85. }
  86.  
  87. void MessengerManager::SendTeamList(MessengerManager::keyA account)
  88. {
  89. LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  90.  
  91. if (!ch)
  92. return;
  93.  
  94. LPDESC d = ch->GetDesc();
  95.  
  96. if (!d)
  97. return;
  98.  
  99. TPacketGCMessenger pack;
  100.  
  101. pack.header = HEADER_GC_MESSENGER;
  102. pack.subheader = MESSENGER_SUBHEADER_GC_TEAM_LIST;
  103. pack.size = sizeof(TPacketGCMessenger);
  104.  
  105. TPacketGCMessengerTeamListOffline pack_offline;
  106. TPacketGCMessengerTeamListOnline pack_online;
  107.  
  108. TEMP_BUFFER buf(128 * 1024);
  109.  
  110. itertype(m_TeamRelation[account]) it = m_TeamRelation[account].begin(), eit = m_TeamRelation[account].end();
  111.  
  112. while (it != eit)
  113. {
  114. if (m_set_loginAccount.find(*it) != m_set_loginAccount.end())
  115. {
  116. pack_online.connected = 1;
  117.  
  118. pack_online.length = it->size();
  119.  
  120. buf.write(&pack_online, sizeof(TPacketGCMessengerTeamListOnline));
  121. buf.write(it->c_str(), it->size());
  122. }
  123. else
  124. {
  125. pack_offline.connected = 0;
  126.  
  127. pack_offline.length = it->size();
  128.  
  129. buf.write(&pack_offline, sizeof(TPacketGCMessengerTeamListOffline));
  130. buf.write(it->c_str(), it->size());
  131. }
  132.  
  133. ++it;
  134. }
  135.  
  136. pack.size += buf.size();
  137.  
  138. d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  139. d->Packet(buf.read_peek(), buf.size());
  140. }
  141.  
  142. void MessengerManager::SendTeamLogin(MessengerManager::keyA account, MessengerManager::keyA companion)
  143. {
  144. LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  145. LPDESC d = ch ? ch->GetDesc() : NULL;
  146.  
  147. if (!d)
  148. return;
  149.  
  150. if (!d->GetCharacter())
  151. return;
  152.  
  153. BYTE bLen = companion.size();
  154.  
  155. TPacketGCMessenger pack;
  156.  
  157. pack.header = HEADER_GC_MESSENGER;
  158. pack.subheader = MESSENGER_SUBHEADER_GC_TEAM_LOGIN;
  159. pack.size = sizeof(TPacketGCMessenger) + sizeof(BYTE) + bLen;
  160.  
  161. d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  162. d->BufferedPacket(&bLen, sizeof(BYTE));
  163. d->Packet(companion.c_str(), companion.size());
  164. }
  165.  
  166. void MessengerManager::SendTeamLogout(MessengerManager::keyA account, MessengerManager::keyA companion)
  167. {
  168. if (!companion.size())
  169. return;
  170.  
  171. LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  172. LPDESC d = ch ? ch->GetDesc() : NULL;
  173.  
  174. if (!d)
  175. return;
  176.  
  177. BYTE bLen = companion.size();
  178.  
  179. TPacketGCMessenger pack;
  180.  
  181. pack.header = HEADER_GC_MESSENGER;
  182. pack.subheader = MESSENGER_SUBHEADER_GC_TEAM_LOGOUT;
  183. pack.size = sizeof(TPacketGCMessenger) + sizeof(BYTE) + bLen;
  184.  
  185. d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  186. d->BufferedPacket(&bLen, sizeof(BYTE));
  187. d->Packet(companion.c_str(), companion.size());
  188. }
  189. #endif
  190.  
  191. void MessengerManager::LoadList(SQLMsg * msg)
  192. {
  193. if (NULL == msg)
  194. return;
  195.  
  196. if (NULL == msg->Get())
  197. return;
  198.  
  199. if (msg->Get()->uiNumRows == 0)
  200. return;
  201.  
  202. std::string account;
  203.  
  204. sys_log(1, "Messenger::LoadList");
  205.  
  206. for (uint i = 0; i < msg->Get()->uiNumRows; ++i)
  207. {
  208. MYSQL_ROW row = mysql_fetch_row(msg->Get()->pSQLResult);
  209.  
  210. if (row[0] && row[1])
  211. {
  212. if (account.length() == 0)
  213. account = row[0];
  214.  
  215. m_Relation[row[0]].insert(row[1]);
  216. m_InverseRelation[row[1]].insert(row[0]);
  217. }
  218. }
  219.  
  220. SendList(account);
  221.  
  222. std::set<MessengerManager::keyT>::iterator it;
  223.  
  224. for (it = m_InverseRelation[account].begin(); it != m_InverseRelation[account].end(); ++it)
  225. SendLogin(*it, account);
  226. }
  227.  
  228. void MessengerManager::Logout(MessengerManager::keyA account)
  229. {
  230. if (m_set_loginAccount.find(account) == m_set_loginAccount.end())
  231. return;
  232.  
  233. m_set_loginAccount.erase(account);
  234.  
  235. std::set<MessengerManager::keyT>::iterator it;
  236.  
  237. for (it = m_InverseRelation[account].begin(); it != m_InverseRelation[account].end(); ++it)
  238. {
  239. SendLogout(*it, account);
  240. }
  241.  
  242. std::map<keyT, std::set<keyT> >::iterator it2 = m_Relation.begin();
  243.  
  244. while (it2 != m_Relation.end())
  245. {
  246. it2->second.erase(account);
  247. ++it2;
  248. }
  249.  
  250. m_Relation.erase(account);
  251. //m_map_stMobile.erase(account);
  252.  
  253. #ifdef ENABLE_MESSENGER_TEAM
  254. std::set<MessengerManager::keyT>::iterator it5;
  255.  
  256. for (it5 = m_InverseTeamRelation[account].begin(); it5 != m_InverseTeamRelation[account].end(); ++it5)
  257. {
  258. SendTeamLogout(*it5, account);
  259. }
  260.  
  261. std::map<keyT, std::set<keyT> >::iterator it6 = m_TeamRelation.begin();
  262.  
  263. while (it6 != m_TeamRelation.end())
  264. {
  265. it6->second.erase(account);
  266. ++it6;
  267. }
  268. m_TeamRelation.erase(account);
  269. #endif
  270. }
  271.  
  272. void MessengerManager::RequestToAdd(LPCHARACTER ch, LPCHARACTER target)
  273. {
  274. if (!ch->IsPC() || !target->IsPC())
  275. return;
  276.  
  277. if (quest::CQuestManager::instance().GetPCForce(ch->GetPlayerID())->IsRunning() == true)
  278. {
  279. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("상대방이 친구 추가를 받을 수 없는 상태입니다."));
  280. return;
  281. }
  282.  
  283. if (quest::CQuestManager::instance().GetPCForce(target->GetPlayerID())->IsRunning() == true)
  284. return;
  285.  
  286. DWORD dw1 = GetCRC32(ch->GetName(), strlen(ch->GetName()));
  287. DWORD dw2 = GetCRC32(target->GetName(), strlen(target->GetName()));
  288.  
  289. char buf[64];
  290. snprintf(buf, sizeof(buf), "%u:%u", dw1, dw2);
  291. DWORD dwComplex = GetCRC32(buf, strlen(buf));
  292.  
  293. m_set_requestToAdd.insert(dwComplex);
  294.  
  295. target->ChatPacket(CHAT_TYPE_COMMAND, "messenger_auth %s", ch->GetName());
  296. }
  297.  
  298. // void MessengerManager::AuthToAdd(MessengerManager::keyA account, MessengerManager::keyA companion, bool bDeny)
  299. // {
  300. // DWORD dw1 = GetCRC32(companion.c_str(), companion.length());
  301. // DWORD dw2 = GetCRC32(account.c_str(), account.length());
  302.  
  303. // char buf[64];
  304. // snprintf(buf, sizeof(buf), "%u:%u", dw1, dw2);
  305. // DWORD dwComplex = GetCRC32(buf, strlen(buf));
  306.  
  307. // if (m_set_requestToAdd.find(dwComplex) == m_set_requestToAdd.end())
  308. // {
  309. // sys_log(0, "MessengerManager::AuthToAdd : request not exist %s -> %s", companion.c_str(), account.c_str());
  310. // return;
  311. // }
  312.  
  313. // m_set_requestToAdd.erase(dwComplex);
  314.  
  315. // if (!bDeny)
  316. // {
  317. // AddToList(companion, account);
  318. // AddToList(account, companion);
  319. // }
  320. // }
  321.  
  322. bool MessengerManager::AuthToAdd(MessengerManager::keyA account, MessengerManager::keyA companion, bool bDeny)
  323. {
  324. DWORD dw1 = GetCRC32(companion.c_str(), companion.length());
  325. DWORD dw2 = GetCRC32(account.c_str(), account.length());
  326.  
  327. char buf[64];
  328. snprintf(buf, sizeof(buf), "%u:%u", dw1, dw2);
  329. DWORD dwComplex = GetCRC32(buf, strlen(buf));
  330.  
  331. if (m_set_requestToAdd.find(dwComplex) == m_set_requestToAdd.end())
  332. {
  333. sys_log(0, "MessengerManager::AuthToAdd : request not exist %s -> %s", companion.c_str(), account.c_str());
  334. return false;
  335. }
  336.  
  337. m_set_requestToAdd.erase(dwComplex);
  338.  
  339. if (!bDeny)
  340. {
  341. AddToList(companion, account);
  342. AddToList(account, companion);
  343. }
  344.  
  345. return true;
  346. }
  347.  
  348. void MessengerManager::__AddToList(MessengerManager::keyA account, MessengerManager::keyA companion)
  349. {
  350. m_Relation[account].insert(companion);
  351. m_InverseRelation[companion].insert(account);
  352.  
  353. LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  354. LPDESC d = ch ? ch->GetDesc() : NULL;
  355.  
  356. if (d)
  357. {
  358. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("<메신져> %s 님을 친구로 추가하였습니다."), companion.c_str());
  359. }
  360.  
  361. LPCHARACTER tch = CHARACTER_MANAGER::instance().FindPC(companion.c_str());
  362.  
  363. if (tch)
  364. SendLogin(account, companion);
  365. else
  366. SendLogout(account, companion);
  367. }
  368.  
  369. void MessengerManager::AddToList(MessengerManager::keyA account, MessengerManager::keyA companion)
  370. {
  371. if (companion.size() == 0)
  372. return;
  373.  
  374. if (m_Relation[account].find(companion) != m_Relation[account].end())
  375. return;
  376.  
  377. sys_log(0, "Messenger Add %s %s", account.c_str(), companion.c_str());
  378. DBManager::instance().Query("INSERT INTO messenger_list%s VALUES ('%s', '%s')",
  379. get_table_postfix(), account.c_str(), companion.c_str());
  380.  
  381. __AddToList(account, companion);
  382.  
  383. TPacketGGMessenger p2ppck;
  384.  
  385. p2ppck.bHeader = HEADER_GG_MESSENGER_ADD;
  386. strlcpy(p2ppck.szAccount, account.c_str(), sizeof(p2ppck.szAccount));
  387. strlcpy(p2ppck.szCompanion, companion.c_str(), sizeof(p2ppck.szCompanion));
  388. P2P_MANAGER::instance().Send(&p2ppck, sizeof(TPacketGGMessenger));
  389. }
  390.  
  391. void MessengerManager::__RemoveFromList(MessengerManager::keyA account, MessengerManager::keyA companion)
  392. {
  393. m_Relation[account].erase(companion);
  394. m_InverseRelation[companion].erase(account);
  395.  
  396. LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  397. LPDESC d = ch ? ch->GetDesc() : NULL;
  398.  
  399. if (d)
  400. ch->ChatPacket(CHAT_TYPE_INFO, LC_TEXT("<메신져> %s 님을 메신저에서 삭제하였습니다."), companion.c_str());
  401. }
  402.  
  403. bool MessengerManager::IsInList(MessengerManager::keyA account, MessengerManager::keyA companion) // Fix
  404. {
  405. if (m_Relation.find(account) == m_Relation.end())
  406. return false;
  407.  
  408. if (m_Relation[account].empty())
  409. return false;
  410.  
  411. return m_Relation[account].find(companion) != m_Relation[account].end();
  412. }
  413.  
  414. void MessengerManager::RemoveFromList(MessengerManager::keyA account, MessengerManager::keyA companion)
  415. {
  416. if (companion.empty())
  417. return;
  418.  
  419. if (companion.size() == 0)
  420. return;
  421.  
  422. if (!IsInList(account, companion)) // Fix
  423. return;
  424.  
  425. char companionEscaped[CHARACTER_NAME_MAX_LEN * 2 + 1];
  426. DBManager::instance().EscapeString(companionEscaped, sizeof(companionEscaped), companion.c_str(), companion.length());
  427.  
  428. sys_log(1, "Messenger Remove %s %s", account.c_str(), companion.c_str());
  429.  
  430. // DBManager::instance().Query("DELETE FROM messenger_list%s WHERE account='%s' AND companion = '%s'",
  431. // get_table_postfix(), account.c_str(), companion.c_str());
  432.  
  433. // Fix
  434. DBManager::instance().Query("DELETE FROM messenger_list%s WHERE account='%s' AND companion = '%s'",
  435. get_table_postfix(), account.c_str(), companionEscaped);
  436.  
  437. __RemoveFromList(account, companion);
  438.  
  439. TPacketGGMessenger p2ppck;
  440.  
  441. p2ppck.bHeader = HEADER_GG_MESSENGER_REMOVE;
  442. strlcpy(p2ppck.szAccount, account.c_str(), sizeof(p2ppck.szAccount));
  443. strlcpy(p2ppck.szCompanion, companion.c_str(), sizeof(p2ppck.szCompanion));
  444. P2P_MANAGER::instance().Send(&p2ppck, sizeof(TPacketGGMessenger));
  445. }
  446.  
  447. void MessengerManager::RemoveAllList(keyA account)
  448. {
  449. std::set<keyT> company(m_Relation[account]);
  450.  
  451. /* SQL Data 삭제 */
  452. DBManager::instance().Query("DELETE FROM messenger_list%s WHERE account='%s' OR companion='%s'",
  453. get_table_postfix(), account.c_str(), account.c_str());
  454.  
  455. /* 내가 가지고있는 리스트 삭제 */
  456. for (std::set<keyT>::iterator iter = company.begin();
  457. iter != company.end();
  458. iter++ )
  459. {
  460. this->RemoveFromList(account, *iter);
  461. }
  462.  
  463. /* 복사한 데이타 삭제 */
  464. for (std::set<keyT>::iterator iter = company.begin();
  465. iter != company.end();
  466. )
  467. {
  468. company.erase(iter++);
  469. }
  470.  
  471. company.clear();
  472. }
  473.  
  474.  
  475. void MessengerManager::SendList(MessengerManager::keyA account)
  476. {
  477. LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  478.  
  479. if (!ch)
  480. return;
  481.  
  482. LPDESC d = ch->GetDesc();
  483.  
  484. if (!d)
  485. return;
  486.  
  487. if (m_Relation.find(account) == m_Relation.end())
  488. return;
  489.  
  490. if (m_Relation[account].empty())
  491. return;
  492.  
  493. TPacketGCMessenger pack;
  494.  
  495. pack.header = HEADER_GC_MESSENGER;
  496. pack.subheader = MESSENGER_SUBHEADER_GC_LIST;
  497. pack.size = sizeof(TPacketGCMessenger);
  498.  
  499. TPacketGCMessengerListOffline pack_offline;
  500. TPacketGCMessengerListOnline pack_online;
  501.  
  502. TEMP_BUFFER buf(128 * 1024); // 128k
  503.  
  504. itertype(m_Relation[account]) it = m_Relation[account].begin(), eit = m_Relation[account].end();
  505.  
  506. while (it != eit)
  507. {
  508. if (m_set_loginAccount.find(*it) != m_set_loginAccount.end())
  509. {
  510. pack_online.connected = 1;
  511.  
  512. // Online
  513. pack_online.length = it->size();
  514.  
  515. buf.write(&pack_online, sizeof(TPacketGCMessengerListOnline));
  516. buf.write(it->c_str(), it->size());
  517. }
  518. else
  519. {
  520. pack_offline.connected = 0;
  521.  
  522. // Offline
  523. pack_offline.length = it->size();
  524.  
  525. buf.write(&pack_offline, sizeof(TPacketGCMessengerListOffline));
  526. buf.write(it->c_str(), it->size());
  527. }
  528.  
  529. ++it;
  530. }
  531.  
  532. pack.size += buf.size();
  533.  
  534. d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  535. d->Packet(buf.read_peek(), buf.size());
  536. }
  537.  
  538. void MessengerManager::SendLogin(MessengerManager::keyA account, MessengerManager::keyA companion)
  539. {
  540. LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  541. LPDESC d = ch ? ch->GetDesc() : NULL;
  542.  
  543. if (!d)
  544. return;
  545.  
  546. if (!d->GetCharacter())
  547. return;
  548.  
  549. if (ch->GetGMLevel() == GM_PLAYER && gm_get_level(companion.c_str()) != GM_PLAYER)
  550. return;
  551.  
  552. BYTE bLen = companion.size();
  553.  
  554. TPacketGCMessenger pack;
  555.  
  556. pack.header = HEADER_GC_MESSENGER;
  557. pack.subheader = MESSENGER_SUBHEADER_GC_LOGIN;
  558. pack.size = sizeof(TPacketGCMessenger) + sizeof(BYTE) + bLen;
  559.  
  560. d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  561. d->BufferedPacket(&bLen, sizeof(BYTE));
  562. d->Packet(companion.c_str(), companion.size());
  563. }
  564.  
  565. void MessengerManager::SendLogout(MessengerManager::keyA account, MessengerManager::keyA companion)
  566. {
  567. if (!companion.size())
  568. return;
  569.  
  570. LPCHARACTER ch = CHARACTER_MANAGER::instance().FindPC(account.c_str());
  571. LPDESC d = ch ? ch->GetDesc() : NULL;
  572.  
  573. if (!d)
  574. return;
  575.  
  576. BYTE bLen = companion.size();
  577.  
  578. TPacketGCMessenger pack;
  579.  
  580. pack.header = HEADER_GC_MESSENGER;
  581. pack.subheader = MESSENGER_SUBHEADER_GC_LOGOUT;
  582. pack.size = sizeof(TPacketGCMessenger) + sizeof(BYTE) + bLen;
  583.  
  584. d->BufferedPacket(&pack, sizeof(TPacketGCMessenger));
  585. d->BufferedPacket(&bLen, sizeof(BYTE));
  586. d->Packet(companion.c_str(), companion.size());
  587. }
  588.  
  589.