TrinityCore
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 virtual ~ChatHandler() { }
47
48 static char* LineFromMessage(char*& pos);
49
50 // function with different implementation for chat/console
51 virtual char const* GetTrinityString(uint32 entry) const;
52 virtual void SendSysMessage(std::string_view str, bool escapeCharacters = false);
53
54 void SendSysMessage(uint32 entry);
55
56 template<typename... Args>
57 void PSendSysMessage(const char* fmt, Args&&... args)
58 {
59 SendSysMessage(fmt::sprintf(fmt, std::forward<Args>(args)...));
60 }
61
62 template<typename... Args>
63 void PSendSysMessage(uint32 entry, Args&&... args)
64 {
65 SendSysMessage(PGetParseString(entry, std::forward<Args>(args)...).c_str());
66 }
67
68 template<typename... Args>
69 std::string PGetParseString(uint32 entry, Args&&... args) const
70 {
71 return fmt::sprintf(GetTrinityString(entry), std::forward<Args>(args)...);
72 }
73
74 bool _ParseCommands(std::string_view text);
75 virtual bool ParseCommands(std::string_view text);
76
77 void SendGlobalSysMessage(const char *str);
78
79 // function with different implementation for chat/console
80 virtual bool IsHumanReadable() const { return true; }
81 virtual bool HasPermission(uint32 permission) const;
82 virtual std::string GetNameLink() const;
83 virtual bool needReportToTarget(Player* chr) const;
84 virtual LocaleConstant GetSessionDbcLocale() const;
85 virtual LocaleConstant GetSessionDbLocaleIndex() const;
86
87 bool HasLowerSecurity(Player* target, ObjectGuid guid, bool strong = false);
88 bool HasLowerSecurityAccount(WorldSession* target, uint32 account, bool strong = false);
89
90 void SendGlobalGMSysMessage(const char *str);
91 Player* getSelectedPlayer();
92 Creature* getSelectedCreature();
93 Unit* getSelectedUnit();
94 WorldObject* getSelectedObject();
95 // Returns either the selected player or self if there is no selected player
96 Player* getSelectedPlayerOrSelf();
97
98 char* extractKeyFromLink(char* text, char const* linkType, char** something1 = nullptr);
99 char* extractKeyFromLink(char* text, char const* const* linkTypes, int* found_idx, char** something1 = nullptr);
100 char* extractQuotedArg(char* args);
101 ObjectGuid::LowType extractLowGuidFromLink(char* text, HighGuid& guidHigh);
102 bool GetPlayerGroupAndGUIDByName(const char* cname, Player*& player, Group*& group, ObjectGuid& guid, bool offline = false);
103 std::string extractPlayerNameFromLink(char* text);
104 // select by arg (name/link) or in-game selection online/offline player or self if a creature is selected
105 bool extractPlayerTarget(char* args, Player** player, ObjectGuid* player_guid = nullptr, std::string* player_name = nullptr);
106
107 std::string playerLink(std::string const& name) const;
108 std::string GetNameLink(Player* chr) const;
109
110 GameObject* GetNearbyGameObject();
111 GameObject* GetObjectFromPlayerMapByDbGuid(ObjectGuid::LowType lowguid);
112 Creature* GetCreatureFromPlayerMapByDbGuid(ObjectGuid::LowType lowguid);
113 bool HasSentErrorMessage() const { return sentErrorMessage; }
114 void SetSentErrorMessage(bool val){ sentErrorMessage = val; }
115 protected:
116 explicit ChatHandler() : m_session(nullptr), sentErrorMessage(false) { } // for CLI subclass
117
118 private:
119 WorldSession* m_session; // != nullptr for chat command call and nullptr for CLI command
120
121 // common global flag
123};
124
126{
127 public:
128 using Print = void(void*, std::string_view);
129 explicit CliHandler(void* callbackArg, Print* zprint) : m_callbackArg(callbackArg), m_print(zprint) { }
130
131 // overwrite functions
132 char const* GetTrinityString(uint32 entry) const override;
133 bool HasPermission(uint32 /*permission*/) const override { return true; }
134 void SendSysMessage(std::string_view, bool escapeCharacters) override;
135 bool ParseCommands(std::string_view str) override;
136 std::string GetNameLink() const override;
137 bool needReportToTarget(Player* chr) const override;
138 LocaleConstant GetSessionDbcLocale() const override;
139 LocaleConstant GetSessionDbLocaleIndex() const override;
140
141 private:
144};
145
147{
148 public:
149 static std::string const PREFIX;
150
152 bool ParseCommands(std::string_view str) override;
153 void SendSysMessage(std::string_view, bool escapeCharacters) override;
155 bool IsHumanReadable() const override { return humanReadable; }
156
157 private:
158 void Send(std::string const& msg);
159 void SendAck();
160 void SendOK();
161 void SendFailed();
162
163 char const* echo = nullptr;
164 bool hadAck = false;
165 bool humanReadable = false;
166};
167
168#endif
LocaleConstant
Definition: Common.h:48
#define TC_GAME_API
Definition: Define.h:123
uint8_t uint8
Definition: Define.h:144
uint32_t uint32
Definition: Define.h:142
HighGuid
Definition: ObjectGuid.h:75
static std::string const PREFIX
Definition: Chat.h:149
bool IsHumanReadable() const override
Definition: Chat.h:155
virtual bool IsHumanReadable() const
Definition: Chat.h:80
WorldSession * GetSession()
Definition: Chat.h:42
bool HasSentErrorMessage() const
Definition: Chat.h:113
ChatHandler(WorldSession *session)
Definition: Chat.h:45
WorldSession * m_session
Definition: Chat.h:119
std::string PGetParseString(uint32 entry, Args &&... args) const
Definition: Chat.h:69
void PSendSysMessage(const char *fmt, Args &&... args)
Definition: Chat.h:57
virtual ~ChatHandler()
Definition: Chat.h:46
void SetSentErrorMessage(bool val)
Definition: Chat.h:114
ChatHandler()
Definition: Chat.h:116
WorldSession const * GetSession() const
Definition: Chat.h:43
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition: Chat.cpp:113
bool sentErrorMessage
Definition: Chat.h:122
bool IsConsole() const
Definition: Chat.h:41
void PSendSysMessage(uint32 entry, Args &&... args)
Definition: Chat.h:63
CliHandler(void *callbackArg, Print *zprint)
Definition: Chat.h:129
void(void *, std::string_view) Print
Definition: Chat.h:128
bool HasPermission(uint32) const override
Definition: Chat.h:133
void * m_callbackArg
Definition: Chat.h:142
Print * m_print
Definition: Chat.h:143
Definition: Group.h:197
uint64 LowType
Definition: ObjectGuid.h:278
Definition: Unit.h:627
Player session in the World.
Definition: WorldSession.h:963
TC_GAME_API Player * GetPlayer(Map const *, ObjectGuid const &guid)
TC_GAME_API char const * GetTrinityString(ChatHandler const *handler, TrinityStrings which)