TrinityCore
Loading...
Searching...
No Matches
SocialHandler.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "WorldSession.h"
19#include "AccountMgr.h"
20#include "CharacterCache.h"
21#include "DatabaseEnv.h"
22#include "Log.h"
23#include "ObjectAccessor.h"
24#include "ObjectMgr.h"
25#include "Player.h"
26#include "QueryCallback.h"
27#include "RBAC.h"
28#include "RealmList.h"
29#include "SocialMgr.h"
30#include "SocialPackets.h"
31
33{
34 TC_LOG_DEBUG("network", "WorldSession::HandleContactListOpcode: Flags: {}", packet.Flags);
36}
37
39{
40 if (!normalizePlayerName(packet.Name))
41 return;
42
43 TC_LOG_DEBUG("network", "WorldSession::HandleAddFriendOpcode: {} asked to add friend: {}",
44 GetPlayerInfo(), packet.Name);
45
46 CharacterCacheEntry const* friendCharacterInfo = sCharacterCache->GetCharacterCacheByName(packet.Name);
47 if (!friendCharacterInfo)
48 {
50 return;
51 }
52
53 auto processFriendRequest = [this,
54 playerGuid = _player->GetGUID(),
55 friendGuid = friendCharacterInfo->Guid,
56 friendAccountGuid = ObjectGuid::Create<HighGuid::WowAccount>(friendCharacterInfo->AccountId),
57 team = Player::TeamForRace(friendCharacterInfo->Race),
58 friendNote = std::move(packet.Notes)]()
59 {
60 if (playerGuid.GetCounter() != m_GUIDLow)
61 return; // not the player initiating request, do nothing
62
63 FriendsResult friendResult = FRIEND_NOT_FOUND;
64 if (friendGuid == GetPlayer()->GetGUID())
65 friendResult = FRIEND_SELF;
67 friendResult = FRIEND_ENEMY;
68 else if (GetPlayer()->GetSocial()->HasFriend(friendGuid))
69 friendResult = FRIEND_ALREADY;
70 else
71 {
72 Player* pFriend = ObjectAccessor::FindPlayer(friendGuid);
73 if (pFriend && pFriend->IsVisibleGloballyFor(GetPlayer()))
74 friendResult = FRIEND_ADDED_ONLINE;
75 else
76 friendResult = FRIEND_ADDED_OFFLINE;
77 if (GetPlayer()->GetSocial()->AddToSocialList(friendGuid, friendAccountGuid, SOCIAL_FLAG_FRIEND))
78 GetPlayer()->GetSocial()->SetFriendNote(friendGuid, friendNote);
79 else
80 friendResult = FRIEND_LIST_FULL;
81 }
82
83 sSocialMgr->SendFriendStatus(GetPlayer(), friendResult, friendGuid);
84 };
85
87 {
88 processFriendRequest();
89 return;
90 }
91
92 // First try looking up friend candidate security from online object
93 if (Player* friendPlayer = ObjectAccessor::FindPlayer(friendCharacterInfo->Guid))
94 {
95 if (!AccountMgr::IsPlayerAccount(friendPlayer->GetSession()->GetSecurity()))
96 {
98 return;
99 }
100
101 processFriendRequest();
102 return;
103 }
104
105 // When not found, consult database
106 GetQueryProcessor().AddCallback(AccountMgr::GetSecurityAsync(friendCharacterInfo->AccountId, sRealmList->GetCurrentRealmId().Realm,
107 [this, continuation = std::move(processFriendRequest)](uint32 friendSecurity)
108 {
109 if (!AccountMgr::IsPlayerAccount(friendSecurity))
110 {
111 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_NOT_FOUND, ObjectGuid::Empty);
112 return;
113 }
114
115 continuation();
116 }));
117}
118
120{
122 TC_LOG_DEBUG("network", "WorldSession::HandleDelFriendOpcode: {}", packet.Player.Guid.ToString());
123
125
126 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_REMOVED, packet.Player.Guid);
127}
128
130{
131 if (!normalizePlayerName(packet.Name))
132 return;
133
134 TC_LOG_DEBUG("network", "WorldSession::HandleAddIgnoreOpcode: {} asked to Ignore: {}",
135 GetPlayer()->GetName(), packet.Name);
136
137 ObjectGuid ignoreGuid;
139
140 if (CharacterCacheEntry const* characterInfo = sCharacterCache->GetCharacterCacheByName(packet.Name))
141 {
142 ignoreGuid = characterInfo->Guid;
143 ObjectGuid ignoreAccountGuid = ObjectGuid::Create<HighGuid::WowAccount>(characterInfo->AccountId);
144 if (ignoreGuid == GetPlayer()->GetGUID()) //not add yourself
145 ignoreResult = FRIEND_IGNORE_SELF;
146 else if (GetPlayer()->GetSocial()->HasIgnore(ignoreGuid, ignoreAccountGuid))
147 ignoreResult = FRIEND_IGNORE_ALREADY;
148 else
149 {
150 ignoreResult = FRIEND_IGNORE_ADDED;
151
152 // ignore list full
153 if (!GetPlayer()->GetSocial()->AddToSocialList(ignoreGuid, ignoreAccountGuid, SOCIAL_FLAG_IGNORED))
154 ignoreResult = FRIEND_IGNORE_FULL;
155 }
156 }
157
158 sSocialMgr->SendFriendStatus(GetPlayer(), ignoreResult, ignoreGuid);
159}
160
162{
164 TC_LOG_DEBUG("network", "WorldSession::HandleDelIgnoreOpcode: {}", packet.Player.Guid.ToString());
165
167
168 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_IGNORE_REMOVED, packet.Player.Guid);
169}
170
172{
174 TC_LOG_DEBUG("network", "WorldSession::HandleSetContactNotesOpcode: Contact: {}, Notes: {}", packet.Player.Guid.ToString(), packet.Notes);
175 _player->GetSocial()->SetFriendNote(packet.Player.Guid, packet.Notes);
176}
177
#define sCharacterCache
uint32_t uint32
Definition Define.h:154
#define TC_LOG_DEBUG(filterType__, message__,...)
Definition Log.h:181
bool normalizePlayerName(std::string &name)
Role Based Access Control related classes definition.
#define sRealmList
Definition RealmList.h:93
FriendsResult
Results of friend related commands.
Definition SocialMgr.h:68
@ FRIEND_IGNORE_FULL
Definition SocialMgr.h:80
@ FRIEND_IGNORE_REMOVED
Definition SocialMgr.h:85
@ FRIEND_ENEMY
Definition SocialMgr.h:79
@ FRIEND_ALREADY
Definition SocialMgr.h:77
@ FRIEND_ADDED_OFFLINE
Definition SocialMgr.h:76
@ FRIEND_NOT_FOUND
Definition SocialMgr.h:73
@ FRIEND_ADDED_ONLINE
Definition SocialMgr.h:75
@ FRIEND_IGNORE_ADDED
Definition SocialMgr.h:84
@ FRIEND_IGNORE_NOT_FOUND
Definition SocialMgr.h:82
@ FRIEND_IGNORE_SELF
Definition SocialMgr.h:81
@ FRIEND_IGNORE_ALREADY
Definition SocialMgr.h:83
@ FRIEND_SELF
Definition SocialMgr.h:78
@ FRIEND_REMOVED
Definition SocialMgr.h:74
@ FRIEND_LIST_FULL
Definition SocialMgr.h:70
#define sSocialMgr
Definition SocialMgr.h:173
@ SOCIAL_FLAG_FRIEND
Definition SocialMgr.h:40
@ SOCIAL_FLAG_IGNORED
Definition SocialMgr.h:41
static bool IsPlayerAccount(uint32 gmlevel)
static QueryCallback GetSecurityAsync(uint32 accountId, int32 realmId, std::function< void(uint32)> callback)
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
static ObjectGuid const Empty
Definition ObjectGuid.h:314
std::string ToString() const
void SetFriendNote(ObjectGuid const &guid, std::string const &note)
bool HasFriend(ObjectGuid const &friendGuid)
void RemoveFromSocialList(ObjectGuid const &guid, SocialFlag flag)
Definition SocialMgr.cpp:86
void SendSocialList(Player *player, uint32 flags)
static Team TeamForRace(uint8 race)
Definition Player.cpp:6461
PlayerSocial * GetSocial() const
Definition Player.h:1290
bool IsVisibleGloballyFor(Player const *player) const
Definition Player.cpp:24528
Team GetTeam() const
Definition Player.h:2423
void HandleDelFriendOpcode(WorldPackets::Social::DelFriend &packet)
std::string GetPlayerInfo() const
void HandleSetContactNotesOpcode(WorldPackets::Social::SetContactNotes &packet)
Player * GetPlayer() const
QueryCallbackProcessor & GetQueryProcessor()
ObjectGuid::LowType m_GUIDLow
bool HasPermission(uint32 permissionId)
void SendPacket(WorldPacket const *packet, bool forced=false)
Send a packet to the client.
void HandleAddIgnoreOpcode(WorldPackets::Social::AddIgnore &packet)
void HandleAddFriendOpcode(WorldPackets::Social::AddFriend &packet)
Player * _player
void HandleDelIgnoreOpcode(WorldPackets::Social::DelIgnore &packet)
void HandleContactListOpcode(WorldPackets::Social::SendContactList &packet)
void HandleSocialContractRequest(WorldPackets::Social::SocialContractRequest &socialContractRequest)
TC_GAME_API Player * FindPlayer(ObjectGuid const &)
@ RBAC_PERM_ALLOW_GM_FRIEND
Definition RBAC.h:93
@ RBAC_PERM_TWO_SIDE_ADD_FRIEND
Definition RBAC.h:82