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