TrinityCore
ChatCommandTags.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 "ChatCommandTags.h"
19
20#include "AccountMgr.h"
21#include "CharacterCache.h"
22#include "Chat.h"
23#include "ChatCommandArgs.h"
24#include "ObjectAccessor.h"
25#include "ObjectMgr.h"
26#include "Player.h"
27#include "World.h"
28#include "WorldSession.h"
29
30using namespace Trinity::Impl::ChatCommands;
31
33{
34 if (args.empty())
35 return std::nullopt;
36 if ((args[0] != '"') && (args[0] != '\''))
37 return ArgInfo<std::string>::TryConsume(*this, handler, args);
38
39 char const QUOTE = args[0];
40 for (size_t i = 1; i < args.length(); ++i)
41 {
42 if (args[i] == QUOTE)
43 {
44 auto [remainingToken, tail] = tokenize(args.substr(i + 1));
45 if (remainingToken.empty()) // if this is not empty, then we did not consume the full token
46 return tail;
47 else
48 return std::nullopt;
49 }
50
51 if (args[i] == '\\')
52 {
53 ++i;
54 if (!(i < args.length()))
55 break;
56 }
57 std::string::push_back(args[i]);
58 }
59 // if we reach this, we did not find a closing quote
60 return std::nullopt;
61}
62
64 : _id(session.GetAccountId()), _name(session.GetAccountName()), _session(&session) {}
65
67{
68 std::string_view text;
70 if (!next)
71 return next;
72
73 // first try parsing as account name
74 _name.assign(text);
75 if (!Utf8ToUpperOnlyLatin(_name))
77 _id = AccountMgr::GetId(_name);
78 _session = sWorld->FindSession(_id);
79 if (_id) // account with name exists, we are done
80 return next;
81
82 // try parsing as account id instead
83 Optional<uint32> id = Trinity::StringTo<uint32>(text, 10);
84 if (!id)
86 _id = *id;
87 _session = sWorld->FindSession(_id);
88
89 if (AccountMgr::GetName(_id, _name))
90 return next;
91 else
93}
94
96{
97 if (Player* player = handler->GetPlayer())
98 if (Player* target = player->GetSelectedPlayer())
99 if (WorldSession* session = target->GetSession())
100 return { *session };
101 return std::nullopt;
102}
103
105{
106 Variant<Hyperlink<player>, ObjectGuid::LowType, std::string_view> val;
107 ChatCommandResult next = ArgInfo<decltype(val)>::TryConsume(val, handler, args);
108 if (!next)
109 return next;
110
112 {
113 _guid = ObjectGuid::Create<HighGuid::Player>(val.get<ObjectGuid::LowType>());
114 if ((_player = ObjectAccessor::FindPlayerByLowGUID(_guid.GetCounter())))
115 _name = _player->GetName();
116 else if (!sCharacterCache->GetCharacterNameByGuid(_guid, _name))
117 return FormatTrinityString(handler, LANG_CMDPARSER_CHAR_GUID_NO_EXIST, _guid.ToString().c_str());
118 return next;
119 }
120 else
121 {
123 _name.assign(static_cast<std::string_view>(val.get<Hyperlink<player>>()));
124 else
125 _name.assign(val.get<std::string_view>());
126
127 if (!normalizePlayerName(_name))
129
130 if ((_player = ObjectAccessor::FindPlayerByName(_name)))
131 _guid = _player->GetGUID();
132 else if (!(_guid = sCharacterCache->GetCharacterGuidByName(_name)))
134 return next;
135 }
136}
137
139 : _name(player.GetName()), _guid(player.GetGUID()), _player(&player) {}
140
142{
143 if (Player* player = handler->GetPlayer())
144 if (Player* target = player->GetSelectedPlayer())
145 return { *target };
146 return std::nullopt;
147
148}
149
151{
152 if (Player* player = handler->GetPlayer())
153 return { *player };
154 return std::nullopt;
155}
#define sCharacterCache
#define STRING_VIEW_FMT_ARG(str)
Definition: Define.h:135
@ LANG_CMDPARSER_ACCOUNT_NAME_NO_EXIST
Definition: Language.h:1003
@ LANG_CMDPARSER_CHAR_GUID_NO_EXIST
Definition: Language.h:1005
@ LANG_CMDPARSER_ACCOUNT_ID_NO_EXIST
Definition: Language.h:1004
@ LANG_CMDPARSER_CHAR_NAME_INVALID
Definition: Language.h:1007
@ LANG_CMDPARSER_CHAR_NAME_NO_EXIST
Definition: Language.h:1006
@ LANG_CMDPARSER_INVALID_UTF8
Definition: Language.h:1001
bool normalizePlayerName(std::string &name)
Definition: ObjectMgr.cpp:154
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
bool Utf8ToUpperOnlyLatin(std::string &utf8String)
Definition: Util.cpp:795
static uint32 GetId(std::string_view username)
Definition: AccountMgr.cpp:289
static bool GetName(uint32 accountId, std::string &name)
Definition: AccountMgr.cpp:319
Player * GetPlayer() const
Definition: Chat.cpp:39
uint64 LowType
Definition: ObjectGuid.h:278
Player * GetSelectedPlayer() const
Definition: Player.cpp:24150
Player session in the World.
Definition: WorldSession.h:963
#define sWorld
Definition: World.h:931
TC_GAME_API bool GetName(uint32 accountId, std::string &name)
TC_GAME_API Player * FindPlayerByName(std::string_view name)
TC_GAME_API Player * FindPlayerByLowGUID(ObjectGuid::LowType lowguid)
TokenizeResult tokenize(std::string_view args)
std::string FormatTrinityString(ChatHandler const *handler, TrinityStrings which, Ts &&... args)
TC_GAME_API char const * GetTrinityString(ChatHandler const *handler, TrinityStrings which)
static Optional< AccountIdentifier > FromTarget(ChatHandler *handler)
ChatCommandResult TryConsume(ChatHandler const *handler, std::string_view args)
static Optional< PlayerIdentifier > FromTarget(ChatHandler *handler)
ChatCommandResult TryConsume(ChatHandler const *handler, std::string_view args)
static Optional< PlayerIdentifier > FromSelf(ChatHandler *handler)
TC_GAME_API ChatCommandResult TryConsume(ChatHandler const *handler, std::string_view args)
constexpr bool holds_alternative() const
constexpr decltype(auto) get()