TrinityCore
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 "Realm.h"
29#include "SocialMgr.h"
30#include "SocialPackets.h"
31#include "World.h"
32
34{
35 TC_LOG_DEBUG("network", "WorldSession::HandleContactListOpcode: Flags: {}", packet.Flags);
37}
38
40{
41 if (!normalizePlayerName(packet.Name))
42 return;
43
44 TC_LOG_DEBUG("network", "WorldSession::HandleAddFriendOpcode: {} asked to add friend: {}",
45 GetPlayerInfo(), packet.Name);
46
47 CharacterCacheEntry const* friendCharacterInfo = sCharacterCache->GetCharacterCacheByName(packet.Name);
48 if (!friendCharacterInfo)
49 {
51 return;
52 }
53
54 auto processFriendRequest = [this,
55 playerGuid = _player->GetGUID(),
56 friendGuid = friendCharacterInfo->Guid,
57 friendAccountGuid = ObjectGuid::Create<HighGuid::WowAccount>(friendCharacterInfo->AccountId),
58 team = Player::TeamForRace(friendCharacterInfo->Race),
59 friendNote = std::move(packet.Notes)]()
60 {
61 if (playerGuid.GetCounter() != m_GUIDLow)
62 return; // not the player initiating request, do nothing
63
64 FriendsResult friendResult = FRIEND_NOT_FOUND;
65 if (friendGuid == GetPlayer()->GetGUID())
66 friendResult = FRIEND_SELF;
68 friendResult = FRIEND_ENEMY;
69 else if (GetPlayer()->GetSocial()->HasFriend(friendGuid))
70 friendResult = FRIEND_ALREADY;
71 else
72 {
73 Player* pFriend = ObjectAccessor::FindPlayer(friendGuid);
74 if (pFriend && pFriend->IsVisibleGloballyFor(GetPlayer()))
75 friendResult = FRIEND_ADDED_ONLINE;
76 else
77 friendResult = FRIEND_ADDED_OFFLINE;
78 if (GetPlayer()->GetSocial()->AddToSocialList(friendGuid, friendAccountGuid, SOCIAL_FLAG_FRIEND))
79 GetPlayer()->GetSocial()->SetFriendNote(friendGuid, friendNote);
80 else
81 friendResult = FRIEND_LIST_FULL;
82 }
83
84 sSocialMgr->SendFriendStatus(GetPlayer(), friendResult, friendGuid);
85 };
86
88 {
89 processFriendRequest();
90 return;
91 }
92
93 // First try looking up friend candidate security from online object
94 if (Player* friendPlayer = ObjectAccessor::FindPlayer(friendCharacterInfo->Guid))
95 {
96 if (!AccountMgr::IsPlayerAccount(friendPlayer->GetSession()->GetSecurity()))
97 {
99 return;
100 }
101
102 processFriendRequest();
103 return;
104 }
105
106 // When not found, consult database
108 [this, continuation = std::move(processFriendRequest)](uint32 friendSecurity)
109 {
110 if (!AccountMgr::IsPlayerAccount(friendSecurity))
111 {
112 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_NOT_FOUND, ObjectGuid::Empty);
113 return;
114 }
115
116 continuation();
117 }));
118}
119
121{
123 TC_LOG_DEBUG("network", "WorldSession::HandleDelFriendOpcode: {}", packet.Player.Guid.ToString());
124
126
127 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_REMOVED, packet.Player.Guid);
128}
129
131{
132 if (!normalizePlayerName(packet.Name))
133 return;
134
135 TC_LOG_DEBUG("network", "WorldSession::HandleAddIgnoreOpcode: {} asked to Ignore: {}",
136 GetPlayer()->GetName(), packet.Name);
137
138 ObjectGuid ignoreGuid;
140
141 if (CharacterCacheEntry const* characterInfo = sCharacterCache->GetCharacterCacheByName(packet.Name))
142 {
143 ignoreGuid = characterInfo->Guid;
144 ObjectGuid ignoreAccountGuid = ObjectGuid::Create<HighGuid::WowAccount>(characterInfo->AccountId);
145 if (ignoreGuid == GetPlayer()->GetGUID()) //not add yourself
146 ignoreResult = FRIEND_IGNORE_SELF;
147 else if (GetPlayer()->GetSocial()->HasIgnore(ignoreGuid, ignoreAccountGuid))
148 ignoreResult = FRIEND_IGNORE_ALREADY;
149 else
150 {
151 ignoreResult = FRIEND_IGNORE_ADDED;
152
153 // ignore list full
154 if (!GetPlayer()->GetSocial()->AddToSocialList(ignoreGuid, ignoreAccountGuid, SOCIAL_FLAG_IGNORED))
155 ignoreResult = FRIEND_IGNORE_FULL;
156 }
157 }
158
159 sSocialMgr->SendFriendStatus(GetPlayer(), ignoreResult, ignoreGuid);
160}
161
163{
165 TC_LOG_DEBUG("network", "WorldSession::HandleDelIgnoreOpcode: {}", packet.Player.Guid.ToString());
166
168
169 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_IGNORE_REMOVED, packet.Player.Guid);
170}
171
173{
175 TC_LOG_DEBUG("network", "WorldSession::HandleSetContactNotesOpcode: Contact: {}, Notes: {}", packet.Player.Guid.ToString(), packet.Notes);
176 _player->GetSocial()->SetFriendNote(packet.Player.Guid, packet.Notes);
177}
178
180{
182 response.ShowSocialContract = false;
183 SendPacket(response.Write());
184}
#define sCharacterCache
uint32_t uint32
Definition: Define.h:142
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:156
bool normalizePlayerName(std::string &name)
Definition: ObjectMgr.cpp:154
Role Based Access Control related classes definition.
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:161
@ SOCIAL_FLAG_FRIEND
Definition: SocialMgr.h:40
@ SOCIAL_FLAG_IGNORED
Definition: SocialMgr.h:41
static bool IsPlayerAccount(uint32 gmlevel)
Definition: AccountMgr.cpp:431
static QueryCallback GetSecurityAsync(uint32 accountId, int32 realmId, std::function< void(uint32)> callback)
Definition: AccountMgr.cpp:308
static ObjectGuid const Empty
Definition: ObjectGuid.h:274
std::string ToString() const
Definition: ObjectGuid.cpp:554
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
void SetFriendNote(ObjectGuid const &guid, std::string const &note)
Definition: SocialMgr.cpp:123
bool HasFriend(ObjectGuid const &friendGuid)
Definition: SocialMgr.cpp:184
void RemoveFromSocialList(ObjectGuid const &guid, SocialFlag flag)
Definition: SocialMgr.cpp:79
void SendSocialList(Player *player, uint32 flags)
Definition: SocialMgr.cpp:141
static Team TeamForRace(uint8 race)
Definition: Player.cpp:6454
PlayerSocial * GetSocial() const
Definition: Player.h:1163
bool IsVisibleGloballyFor(Player const *player) const
Definition: Player.cpp:23778
Team GetTeam() const
Definition: Player.h:2235
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)
Realm realm
Definition: World.cpp:3966
TC_GAME_API bool GetName(uint32 accountId, std::string &name)
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
Battlenet::RealmHandle Id
Definition: Realm.h:82