TrinityCore
Loading...
Searching...
No Matches
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
32ChatCommandResult Trinity::Impl::ChatCommands::TryConsumExactSequencee(std::string_view sequence, ChatHandler const* handler, std::string_view args)
33{
34 if (args.empty())
35 return std::nullopt;
36 std::string_view start = args.substr(0, sequence.length());
37 if (StringEqualI(start, sequence))
38 {
39 auto [remainingToken, tail] = Trinity::Impl::ChatCommands::tokenize(args.substr(sequence.length()));
40 if (remainingToken.empty()) // if this is not empty, then we did not consume the full token
41 return tail;
42 start = args.substr(0, sequence.length() + remainingToken.length());
43 }
45}
46
48{
49 if (args.empty())
50 return std::nullopt;
51 if ((args[0] != '"') && (args[0] != '\''))
52 return ArgInfo<std::string>::TryConsume(*this, handler, args);
53
54 char const QUOTE = args[0];
55 for (size_t i = 1; i < args.length(); ++i)
56 {
57 if (args[i] == QUOTE)
58 {
59 auto [remainingToken, tail] = tokenize(args.substr(i + 1));
60 if (remainingToken.empty()) // if this is not empty, then we did not consume the full token
61 return tail;
62 else
63 return std::nullopt;
64 }
65
66 if (args[i] == '\\')
67 {
68 ++i;
69 if (!(i < args.length()))
70 break;
71 }
72 std::string::push_back(args[i]);
73 }
74 // if we reach this, we did not find a closing quote
75 return std::nullopt;
76}
77
79 : _id(session.GetAccountId()), _name(session.GetAccountName()), _session(&session) {}
80
82{
83 std::string_view text;
85 if (!next)
86 return next;
87
88 // first try parsing as account name
89 _name.assign(text);
90 if (!Utf8ToUpperOnlyLatin(_name))
91 {
93 return next;
94 }
95
96 _id = AccountMgr::GetId(_name);
97 _session = sWorld->FindSession(_id);
98 if (_id) // account with name exists, we are done
99 return next;
100
101 // try parsing as account id instead
102 if (Optional<uint32> id = Trinity::StringTo<uint32>(text, 10))
103 {
104 _id = *id;
105 _session = sWorld->FindSession(_id);
106
107 if (!AccountMgr::GetName(_id, _name))
109 }
110 else
112
113 return next;
114}
115
117{
118 if (Player* player = handler->GetPlayer())
119 if (Player* target = player->GetSelectedPlayer())
120 if (WorldSession* session = target->GetSession())
121 return { *session };
122 return std::nullopt;
123}
124
126{
127 Variant<Hyperlink<player>, ObjectGuid::LowType, std::string_view> val;
128 ChatCommandResult next = ArgInfo<decltype(val)>::TryConsume(val, handler, args);
129 if (!next)
130 return next;
131
133 {
134 _guid = ObjectGuid::Create<HighGuid::Player>(val.get<ObjectGuid::LowType>());
135 if ((_player = ObjectAccessor::FindPlayerByLowGUID(_guid.GetCounter())))
136 _name = _player->GetName();
137 else if (!sCharacterCache->GetCharacterNameByGuid(_guid, _name))
138 next = FormatTrinityString(handler, LANG_CMDPARSER_CHAR_GUID_NO_EXIST, _guid.ToString().c_str());
139 return next;
140 }
141 else
142 {
144 _name.assign(static_cast<std::string_view>(val.get<Hyperlink<player>>()));
145 else
146 _name.assign(val.get<std::string_view>());
147
148 if (normalizePlayerName(_name))
149 {
150 if ((_player = ObjectAccessor::FindPlayerByName(_name)))
151 _guid = _player->GetGUID();
152 else if (!(_guid = sCharacterCache->GetCharacterGuidByName(_name)))
154 }
155 else
157
158 return next;
159 }
160}
161
163 : _name(player.GetName()), _guid(player.GetGUID()), _player(&player) {}
164
166{
167 if (Player* player = handler->GetPlayer())
168 if (Player* target = player->GetSelectedPlayer())
169 return { *target };
170 return std::nullopt;
171
172}
173
175{
176 if (Player* player = handler->GetPlayer())
177 return { *player };
178 return std::nullopt;
179}
#define sCharacterCache
#define STRING_VIEW_FMT_ARG(str)
Definition Define.h:147
@ LANG_CMDPARSER_ACCOUNT_NAME_NO_EXIST
Definition Language.h:1012
@ LANG_CMDPARSER_CHAR_GUID_NO_EXIST
Definition Language.h:1014
@ LANG_CMDPARSER_EXACT_SEQ_MISMATCH
Definition Language.h:1022
@ LANG_CMDPARSER_ACCOUNT_ID_NO_EXIST
Definition Language.h:1013
@ LANG_CMDPARSER_CHAR_NAME_INVALID
Definition Language.h:1016
@ LANG_CMDPARSER_CHAR_NAME_NO_EXIST
Definition Language.h:1015
@ LANG_CMDPARSER_INVALID_UTF8
Definition Language.h:1010
bool normalizePlayerName(std::string &name)
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
TC_COMMON_API bool StringEqualI(std::string_view str1, std::string_view str2)
Definition Util.cpp:849
bool Utf8ToUpperOnlyLatin(std::string &utf8String)
Definition Util.cpp:752
static uint32 GetId(std::string_view username)
static bool GetName(uint32 accountId, std::string &name)
Player * GetPlayer() const
Definition Chat.cpp:37
uint64 LowType
Definition ObjectGuid.h:321
Player * GetSelectedPlayer() const
Definition Player.cpp:24902
Player session in the World.
#define sWorld
Definition World.h:916
TC_GAME_API Player * FindPlayerByName(std::string_view name)
TC_GAME_API Player * FindPlayerByLowGUID(ObjectGuid::LowType lowguid)
TokenizeResult tokenize(std::string_view args) noexcept
TC_GAME_API ChatCommandResult TryConsumExactSequencee(std::string_view sequence, ChatHandler const *handler, std::string_view args)
TC_GAME_API char const * GetTrinityString(ChatHandler const *handler, TrinityStrings which)
TC_GAME_API std::string FormatTrinityString(std::string_view messageFormat, fmt::printf_args messageFormatArgs)
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()