TrinityCore
Loading...
Searching...
No Matches
Chat.h
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#ifndef TRINITYCORE_CHAT_H
19#define TRINITYCORE_CHAT_H
20
21#include "ObjectGuid.h"
22#include "StringFormat.h"
23#include <fmt/printf.h>
24
25class ChatHandler;
26class Creature;
27class GameObject;
28class Group;
29class Player;
30class Unit;
31class WorldSession;
32class WorldObject;
33
34struct GameTele;
35
37
39{
40 public:
41 bool IsConsole() const { return (m_session == nullptr); }
42 WorldSession* GetSession() { return m_session; }
43 WorldSession const* GetSession() const { return m_session; }
44 Player* GetPlayer() const;
45 explicit ChatHandler(WorldSession* session) : m_session(session), sentErrorMessage(false) { }
46 ChatHandler(ChatHandler const&) = delete;
50 virtual ~ChatHandler() = default;
51
52 static char* LineFromMessage(char*& pos);
53
54 // function with different implementation for chat/console
55 virtual char const* GetTrinityString(uint32 entry) const;
56 virtual void SendSysMessage(std::string_view str, bool escapeCharacters = false);
57
58 void SendSysMessage(uint32 entry);
59 void SendSysMessage(std::string_view messageFormat, fmt::printf_args messageFormatArgs) noexcept;
60
61 template<typename... Args>
62 void PSendSysMessage(char const* fmt, Args&&... args)
63 {
64 this->SendSysMessage(fmt, fmt::make_printf_args(args...));
65 }
66
67 template<typename... Args>
68 void PSendSysMessage(uint32 entry, Args&&... args)
69 {
70 this->PSendSysMessage(GetTrinityString(entry), std::forward<Args>(args)...);
71 }
72
73 template<typename... Args>
74 static std::string PGetParseString(std::string_view fmt, Args&&... args) noexcept
75 {
76 return StringVPrintf(fmt, fmt::make_printf_args(args...));
77 }
78
79 template<typename... Args>
80 std::string PGetParseString(uint32 entry, Args&&... args) const noexcept
81 {
82 return PGetParseString(GetTrinityString(entry), std::forward<Args>(args)...);
83 }
84
85 static std::string StringVPrintf(std::string_view messageFormat, fmt::printf_args messageFormatArgs) noexcept;
86
87 bool _ParseCommands(std::string_view text);
88 virtual bool ParseCommands(std::string_view text);
89
90 void SendGlobalSysMessage(const char *str);
91
92 // function with different implementation for chat/console
93 virtual bool IsHumanReadable() const { return true; }
94 virtual bool HasPermission(uint32 permission) const;
95 virtual std::string GetNameLink() const;
96 virtual bool needReportToTarget(Player* chr) const;
97 virtual LocaleConstant GetSessionDbcLocale() const;
98 virtual LocaleConstant GetSessionDbLocaleIndex() const;
99
100 bool HasLowerSecurity(Player* target, ObjectGuid guid, bool strong = false);
101 bool HasLowerSecurityAccount(WorldSession* target, uint32 account, bool strong = false);
102
103 void SendGlobalGMSysMessage(const char *str);
104 Player* getSelectedPlayer();
105 Creature* getSelectedCreature();
106 Unit* getSelectedUnit();
107 WorldObject* getSelectedObject();
108 // Returns either the selected player or self if there is no selected player
109 Player* getSelectedPlayerOrSelf();
110
111 char* extractKeyFromLink(char* text, char const* linkType, char** something1 = nullptr);
112 char* extractKeyFromLink(char* text, char const* const* linkTypes, int* found_idx, char** something1 = nullptr);
113 char* extractQuotedArg(char* args);
114 ObjectGuid::LowType extractLowGuidFromLink(char* text, HighGuid& guidHigh);
115 bool GetPlayerGroupAndGUIDByName(const char* cname, Player*& player, Group*& group, ObjectGuid& guid, bool offline = false);
116 std::string extractPlayerNameFromLink(char* text);
117 // select by arg (name/link) or in-game selection online/offline player or self if a creature is selected
118 bool extractPlayerTarget(char* args, Player** player, ObjectGuid* player_guid = nullptr, std::string* player_name = nullptr);
119
120 std::string playerLink(std::string const& name) const;
121 std::string GetNameLink(Player* chr) const;
122
123 GameObject* GetNearbyGameObject();
124 GameObject* GetObjectFromPlayerMapByDbGuid(ObjectGuid::LowType lowguid);
125 Creature* GetCreatureFromPlayerMapByDbGuid(ObjectGuid::LowType lowguid);
126 bool HasSentErrorMessage() const { return sentErrorMessage; }
127 void SetSentErrorMessage(bool val){ sentErrorMessage = val; }
128 protected:
129 explicit ChatHandler() : m_session(nullptr), sentErrorMessage(false) { } // for CLI subclass
130
131 private:
132 WorldSession* m_session; // != nullptr for chat command call and nullptr for CLI command
133
134 // common global flag
136};
137
139{
140 public:
141 using Print = void(void*, std::string_view);
142 explicit CliHandler(void* callbackArg, Print* zprint) : m_callbackArg(callbackArg), m_print(zprint) { }
143
144 // overwrite functions
145 char const* GetTrinityString(uint32 entry) const override;
146 bool HasPermission(uint32 /*permission*/) const override { return true; }
147 void SendSysMessage(std::string_view, bool escapeCharacters) override;
148 bool ParseCommands(std::string_view str) override;
149 std::string GetNameLink() const override;
150 bool needReportToTarget(Player* chr) const override;
151 LocaleConstant GetSessionDbcLocale() const override;
152 LocaleConstant GetSessionDbLocaleIndex() const override;
153
154 private:
157};
158
160{
161 public:
162 static std::string const PREFIX;
163
165 bool ParseCommands(std::string_view str) override;
166 void SendSysMessage(std::string_view, bool escapeCharacters) override;
168 bool IsHumanReadable() const override { return humanReadable; }
169
170 private:
171 void Send(std::string const& msg);
172 void SendAck();
173 void SendOK();
174 void SendFailed();
175
176 char const* echo = nullptr;
177 bool hadAck = false;
178 bool humanReadable = false;
179};
180
181#endif
LocaleConstant
Definition Common.h:51
#define TC_GAME_API
Definition Define.h:129
uint8_t uint8
Definition Define.h:156
uint32_t uint32
Definition Define.h:154
HighGuid
Definition ObjectGuid.h:109
static std::string const PREFIX
Definition Chat.h:162
bool IsHumanReadable() const override
Definition Chat.h:168
ChatHandler & operator=(ChatHandler const &)=delete
virtual bool IsHumanReadable() const
Definition Chat.h:93
ChatHandler(ChatHandler const &)=delete
ChatHandler & operator=(ChatHandler &&)=delete
WorldSession * GetSession()
Definition Chat.h:42
bool HasSentErrorMessage() const
Definition Chat.h:126
ChatHandler(WorldSession *session)
Definition Chat.h:45
WorldSession * m_session
Definition Chat.h:132
virtual ~ChatHandler()=default
std::string PGetParseString(uint32 entry, Args &&... args) const noexcept
Definition Chat.h:80
static std::string PGetParseString(std::string_view fmt, Args &&... args) noexcept
Definition Chat.h:74
void SetSentErrorMessage(bool val)
Definition Chat.h:127
ChatHandler()
Definition Chat.h:129
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:62
WorldSession const * GetSession() const
Definition Chat.h:43
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:111
bool sentErrorMessage
Definition Chat.h:135
bool IsConsole() const
Definition Chat.h:41
void PSendSysMessage(uint32 entry, Args &&... args)
Definition Chat.h:68
ChatHandler(ChatHandler &&)=delete
CliHandler(void *callbackArg, Print *zprint)
Definition Chat.h:142
void(void *, std::string_view) Print
Definition Chat.h:141
bool HasPermission(uint32) const override
Definition Chat.h:146
void * m_callbackArg
Definition Chat.h:155
Print * m_print
Definition Chat.h:156
Definition Group.h:205
uint64 LowType
Definition ObjectGuid.h:321
Definition Unit.h:635
Player session in the World.