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 "World.h"
30
37
39{
40 std::string friendName, friendNote;
41 recvData >> friendName >> friendNote;
42
43 if (!normalizePlayerName(friendName))
44 return;
45
46 TC_LOG_DEBUG("network", "WorldSession::HandleAddFriendOpcode: {} asked to add friend: {}",
47 GetPlayer()->GetName(), friendName);
48
49 CharacterCacheEntry const* friendCharacterInfo = sCharacterCache->GetCharacterCacheByName(friendName);
50 if (!friendCharacterInfo)
51 {
53 return;
54 }
55
56 auto processFriendRequest = [this,
57 playerGuid = _player->GetGUID(),
58 friendGuid = friendCharacterInfo->Guid,
59 team = Player::TeamForRace(friendCharacterInfo->Race),
60 friendNote = std::move(friendNote)]()
61 {
62 if (playerGuid.GetCounter() != m_GUIDLow)
63 return; // not the player initiating request, do nothing
64
65 FriendsResult friendResult = FRIEND_NOT_FOUND;
66 if (friendGuid == GetPlayer()->GetGUID())
67 friendResult = FRIEND_SELF;
69 friendResult = FRIEND_ENEMY;
70 else if (GetPlayer()->GetSocial()->HasFriend(friendGuid))
71 friendResult = FRIEND_ALREADY;
72 else
73 {
74 Player* pFriend = ObjectAccessor::FindPlayer(friendGuid);
75 if (pFriend && pFriend->IsVisibleGloballyFor(GetPlayer()))
76 friendResult = FRIEND_ADDED_ONLINE;
77 else
78 friendResult = FRIEND_ADDED_OFFLINE;
79 if (GetPlayer()->GetSocial()->AddToSocialList(friendGuid, SOCIAL_FLAG_FRIEND))
80 GetPlayer()->GetSocial()->SetFriendNote(friendGuid, friendNote);
81 else
82 friendResult = FRIEND_LIST_FULL;
83 }
84
85 sSocialMgr->SendFriendStatus(GetPlayer(), friendResult, friendGuid);
86 };
87
89 {
90 processFriendRequest();
91 return;
92 }
93
94 // First try looking up friend candidate security from online object
95 if (Player* friendPlayer = ObjectAccessor::FindPlayer(friendCharacterInfo->Guid))
96 {
97 if (!AccountMgr::IsPlayerAccount(friendPlayer->GetSession()->GetSecurity()))
98 {
100 return;
101 }
102
103 processFriendRequest();
104 return;
105 }
106
107 // When not found, consult database
109 [this, continuation = std::move(processFriendRequest)](uint32 friendSecurity)
110 {
111 if (!AccountMgr::IsPlayerAccount(friendSecurity))
112 {
113 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_NOT_FOUND, ObjectGuid::Empty);
114 return;
115 }
116
117 continuation();
118 }));
119}
120
122{
123 ObjectGuid friendGuid;
124 recvData >> friendGuid;
125 TC_LOG_DEBUG("network", "WorldSession::HandleDelFriendOpcode: {}", friendGuid.ToString());
126
128
129 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_REMOVED, friendGuid);
130}
131
133{
134 std::string ignoreName;
135 recvData >> ignoreName;
136
137 if (!normalizePlayerName(ignoreName))
138 return;
139
140 TC_LOG_DEBUG("network", "WorldSession::HandleAddIgnoreOpcode: {} asked to Ignore: {}",
141 GetPlayer()->GetName(), ignoreName);
142
143 ObjectGuid ignoreGuid = sCharacterCache->GetCharacterGuidByName(ignoreName);
145 if (!ignoreGuid.IsEmpty())
146 {
147 if (ignoreGuid == GetPlayer()->GetGUID()) //not add yourself
148 ignoreResult = FRIEND_IGNORE_SELF;
149 else if (GetPlayer()->GetSocial()->HasIgnore(ignoreGuid))
150 ignoreResult = FRIEND_IGNORE_ALREADY;
151 else
152 {
153 ignoreResult = FRIEND_IGNORE_ADDED;
154
155 // ignore list full
156 if (!GetPlayer()->GetSocial()->AddToSocialList(ignoreGuid, SOCIAL_FLAG_IGNORED))
157 ignoreResult = FRIEND_IGNORE_FULL;
158 }
159 }
160
161 sSocialMgr->SendFriendStatus(GetPlayer(), ignoreResult, ignoreGuid);
162}
163
165{
166 ObjectGuid ignoreGuid;
167 recvData >> ignoreGuid;
168
169 TC_LOG_DEBUG("network", "WorldSession::HandleDelIgnoreOpcode: {}", ignoreGuid.ToString());
170
172
173 sSocialMgr->SendFriendStatus(GetPlayer(), FRIEND_IGNORE_REMOVED, ignoreGuid);
174}
175
177{
178 ObjectGuid guid;
179 std::string note;
180 recvData >> guid >> note;
181
182 TC_LOG_DEBUG("network", "WorldSession::HandleSetContactNotesOpcode: Contact: {}, Notes: {}", guid.ToString(), note);
183
184 _player->GetSocial()->SetFriendNote(guid, note);
185}
#define sCharacterCache
uint32_t uint32
Definition Define.h:133
uint16 flags
#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
bool IsEmpty() const
Definition ObjectGuid.h:172
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:1832
static uint32 TeamForRace(uint8 race)
Definition Player.cpp:6269
bool IsVisibleGloballyFor(Player const *player) const
Definition Player.cpp:22113
PlayerSocial * GetSocial()
Definition Player.h:982
void HandleDelIgnoreOpcode(WorldPacket &recvPacket)
Player * GetPlayer() const
void HandleDelFriendOpcode(WorldPacket &recvPacket)
void HandleContactListOpcode(WorldPacket &recvPacket)
QueryCallbackProcessor & GetQueryProcessor()
ObjectGuid::LowType m_GUIDLow
bool HasPermission(uint32 permissionId)
void HandleSetContactNotesOpcode(WorldPacket &recvPacket)
Player * _player
void HandleAddIgnoreOpcode(WorldPacket &recvPacket)
void HandleAddFriendOpcode(WorldPacket &recvPacket)
Realm realm
Definition World.cpp:3605
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