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 "Log.h"
22#include "ObjectAccessor.h"
23#include "ObjectMgr.h"
24#include "Player.h"
25#include "QueryCallback.h"
26#include "RBAC.h"
27#include "Realm.h"
28#include "SocialMgr.h"
29#include "SocialPackets.h"
30#include "World.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 team = Player::TeamForRace(friendCharacterInfo->Race),
57 friendNote = std::move(packet.Name)]()
58 {
59 if (playerGuid.GetCounter() != m_GUIDLow)
60 return; // not the player initiating request, do nothing
61
62 FriendsResult friendResult = FRIEND_NOT_FOUND;
63 if (friendGuid == GetPlayer()->GetGUID())
64 friendResult = FRIEND_SELF;
66 friendResult = FRIEND_ENEMY;
67 else if (GetPlayer()->GetSocial()->HasFriend(friendGuid))
68 friendResult = FRIEND_ALREADY;
69 else
70 {
71 Player* pFriend = ObjectAccessor::FindPlayer(friendGuid);
72 if (pFriend && pFriend->IsVisibleGloballyFor(GetPlayer()))
73 friendResult = FRIEND_ADDED_ONLINE;
74 else
75 friendResult = FRIEND_ADDED_OFFLINE;
76 if (GetPlayer()->GetSocial()->AddToSocialList(friendGuid, SOCIAL_FLAG_FRIEND))
77 GetPlayer()->GetSocial()->SetFriendNote(friendGuid, friendNote);
78 else
79 friendResult = FRIEND_LIST_FULL;
80 }
81
82 sSocialMgr->SendFriendStatus(GetPlayer(), friendResult, friendGuid);
83 };
84
86 {
87 processFriendRequest();
88 return;
89 }
90
91 // First try looking up friend candidate security from online object
92 if (Player* friendPlayer = ObjectAccessor::FindPlayer(friendCharacterInfo->Guid))
93 {
94 if (!AccountMgr::IsPlayerAccount(friendPlayer->GetSession()->GetSecurity()))
95 {
97 return;
98 }
99
100 processFriendRequest();
101 return;
102 }
103
104 // When not found, consult database
106 [this, continuation = std::move(processFriendRequest)](uint32 friendSecurity)
107 {
108 if (!AccountMgr::IsPlayerAccount(friendSecurity))
109 {
110 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_NOT_FOUND, ObjectGuid::Empty);
111 return;
112 }
113
114 continuation();
115 }));
116}
117
119{
120 TC_LOG_DEBUG("network", "WorldSession::HandleDelFriendOpcode: {}", packet.Player.ToString());
121
123
124 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_REMOVED, packet.Player);
125}
126
128{
129 if (!normalizePlayerName(packet.Name))
130 return;
131
132 TC_LOG_DEBUG("network", "WorldSession::HandleAddIgnoreOpcode: {} asked to Ignore: {}",
133 GetPlayer()->GetName(), packet.Name);
134
135 ObjectGuid ignoreGuid;
137
138 if (CharacterCacheEntry const* characterInfo = sCharacterCache->GetCharacterCacheByName(packet.Name))
139 {
140 ignoreGuid = characterInfo->Guid;
141 if (ignoreGuid == GetPlayer()->GetGUID()) //not add yourself
142 ignoreResult = FRIEND_IGNORE_SELF;
143 else if (GetPlayer()->GetSocial()->HasIgnore(ignoreGuid))
144 ignoreResult = FRIEND_IGNORE_ALREADY;
145 else
146 {
147 ignoreResult = FRIEND_IGNORE_ADDED;
148
149 // ignore list full
150 if (!GetPlayer()->GetSocial()->AddToSocialList(ignoreGuid, SOCIAL_FLAG_IGNORED))
151 ignoreResult = FRIEND_IGNORE_FULL;
152 }
153 }
154
155 sSocialMgr->SendFriendStatus(GetPlayer(), ignoreResult, ignoreGuid);
156}
157
159{
160 TC_LOG_DEBUG("network", "WorldSession::HandleDelIgnoreOpcode: {}", packet.Player.ToString());
161
163
164 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_IGNORE_REMOVED, packet.Player);
165}
166
168{
169 TC_LOG_DEBUG("network", "WorldSession::HandleSetContactNotesOpcode: Contact: {}, Notes: {}", packet.Player.ToString(), packet.Notes);
170 _player->GetSocial()->SetFriendNote(packet.Player, packet.Notes);
171}
#define sCharacterCache
uint32_t uint32
Definition Define.h:133
#define TC_LOG_DEBUG(filterType__,...)
Definition Log.h:156
bool normalizePlayerName(std::string &name)
Role Based Access Control related classes definition.
FriendsResult
Results of friend related commands.
Definition SocialMgr.h:66
@ FRIEND_IGNORE_FULL
Definition SocialMgr.h:78
@ FRIEND_IGNORE_REMOVED
Definition SocialMgr.h:83
@ FRIEND_ENEMY
Definition SocialMgr.h:77
@ FRIEND_ALREADY
Definition SocialMgr.h:75
@ FRIEND_ADDED_OFFLINE
Definition SocialMgr.h:74
@ FRIEND_NOT_FOUND
Definition SocialMgr.h:71
@ FRIEND_ADDED_ONLINE
Definition SocialMgr.h:73
@ FRIEND_IGNORE_ADDED
Definition SocialMgr.h:82
@ FRIEND_IGNORE_NOT_FOUND
Definition SocialMgr.h:80
@ FRIEND_IGNORE_SELF
Definition SocialMgr.h:79
@ FRIEND_IGNORE_ALREADY
Definition SocialMgr.h:81
@ FRIEND_SELF
Definition SocialMgr.h:76
@ FRIEND_REMOVED
Definition SocialMgr.h:72
@ FRIEND_LIST_FULL
Definition SocialMgr.h:68
#define sSocialMgr
Definition SocialMgr.h:159
@ 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)
static ObjectGuid const Empty
Definition ObjectGuid.h:140
std::string ToString() const
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
void SetFriendNote(ObjectGuid const &guid, std::string const &note)
bool HasFriend(ObjectGuid const &friendGuid)
void RemoveFromSocialList(ObjectGuid const &guid, SocialFlag flag)
Definition SocialMgr.cpp:75
void SendSocialList(Player *player, uint32 flags)
uint32 GetTeam() const
Definition Player.h:1818
static uint32 TeamForRace(uint8 race)
Definition Player.cpp:6271
bool IsVisibleGloballyFor(Player const *player) const
Definition Player.cpp:22110
PlayerSocial * GetSocial()
Definition Player.h:960
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 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)
Realm realm
Definition World.cpp:3610
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
uint32 Realm
Definition Realm.h:44
RealmHandle Id
Definition Realm.h:67