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 // Builds chat packet and returns receiver guid position in the packet to substitute in whisper builders
53 static size_t BuildChatPacket(WorldPacket& data, ChatMsg chatType, Language language, ObjectGuid senderGUID, ObjectGuid receiverGUID, std::string_view message, uint8 chatTag,
54 std::string const& senderName = "", std::string const& receiverName = "",
55 uint32 achievementId = 0, bool gmMessage = false, std::string const& channelName = "");
56
57 // Builds chat packet and returns receiver guid position in the packet to substitute in whisper builders
58 static size_t BuildChatPacket(WorldPacket& data, ChatMsg chatType, Language language, WorldObject const* sender, WorldObject const* receiver, std::string_view message, uint32 achievementId = 0, std::string const& channelName = "", LocaleConstant locale = DEFAULT_LOCALE);
59
60 static char* LineFromMessage(char*& pos) { char* start = strtok(pos, "\n"); pos = nullptr; return start; }
61
62 // function with different implementation for chat/console
63 virtual char const* GetTrinityString(uint32 entry) const;
64 virtual void SendSysMessage(std::string_view str, bool escapeCharacters = false);
65
66 void SendSysMessage(uint32 entry);
67
68 template<typename... Args>
69 void PSendSysMessage(char const* fmt, Args&&... args)
70 {
71 SendSysMessage(StringVPrintf(fmt, fmt::make_printf_args(args...)));
72 }
73
74 template<typename... Args>
75 void PSendSysMessage(uint32 entry, Args&&... args)
76 {
77 SendSysMessage(PGetParseString(entry, std::forward<Args>(args)...));
78 }
79
80 template<typename... Args>
81 static std::string PGetParseString(std::string_view fmt, Args&&... args)
82 {
83 return StringVPrintf(fmt, fmt::make_printf_args(args...));
84 }
85
86 template<typename... Args>
87 std::string PGetParseString(uint32 entry, Args&&... args) const
88 {
89 return PGetParseString(GetTrinityString(entry), std::forward<Args>(args)...);
90 }
91
92 static std::string StringVPrintf(std::string_view messageFormat, fmt::printf_args messageFormatArgs);
93
94 bool _ParseCommands(std::string_view text);
95 virtual bool ParseCommands(std::string_view text);
96
97 void SendGlobalSysMessage(const char *str);
98
99 // function with different implementation for chat/console
100 virtual bool IsHumanReadable() const { return true; }
101 virtual bool HasPermission(uint32 permission) const;
102 virtual std::string GetNameLink() const;
103 virtual bool needReportToTarget(Player* chr) const;
104 virtual LocaleConstant GetSessionDbcLocale() const;
105 virtual int GetSessionDbLocaleIndex() const;
106
107 bool HasLowerSecurity(Player* target, ObjectGuid guid, bool strong = false);
108 bool HasLowerSecurityAccount(WorldSession* target, uint32 account, bool strong = false);
109
110 void SendGlobalGMSysMessage(const char *str);
111 Player* getSelectedPlayer();
112 Creature* getSelectedCreature();
113 Unit* getSelectedUnit();
114 WorldObject* getSelectedObject();
115 // Returns either the selected player or self if there is no selected player
116 Player* getSelectedPlayerOrSelf();
117
118 char* extractKeyFromLink(char* text, char const* linkType, char** something1 = nullptr);
119 char* extractKeyFromLink(char* text, char const* const* linkTypes, int* found_idx, char** something1 = nullptr);
120 char* extractQuotedArg(char* args);
121 ObjectGuid::LowType extractLowGuidFromLink(char* text, HighGuid& guidHigh);
122 bool GetPlayerGroupAndGUIDByName(char const* cname, Player*& player, Group*& group, ObjectGuid& guid, bool offline = false);
123 std::string extractPlayerNameFromLink(char* text);
124 // select by arg (name/link) or in-game selection online/offline player or self if a creature is selected
125 bool extractPlayerTarget(char* args, Player** player, ObjectGuid* player_guid = nullptr, std::string* player_name = nullptr);
126
127 std::string playerLink(std::string const& name) const { return m_session ? "|cffffffff|Hplayer:"+name+"|h["+name+"]|h|r" : name; }
128 std::string GetNameLink(Player const* chr) const;
129
130 GameObject* GetNearbyGameObject();
131 GameObject* GetObjectFromPlayerMapByDbGuid(ObjectGuid::LowType lowguid);
132 Creature* GetCreatureFromPlayerMapByDbGuid(ObjectGuid::LowType lowguid);
133 bool HasSentErrorMessage() const { return sentErrorMessage; }
134 void SetSentErrorMessage(bool val){ sentErrorMessage = val; }
135 protected:
136 explicit ChatHandler() : m_session(nullptr), sentErrorMessage(false) { } // for CLI subclass
137
138 private:
139 WorldSession* m_session; // != nullptr for chat command call and nullptr for CLI command
140
141 // common global flag
143};
144
146{
147 public:
148 using Print = void(void*, std::string_view);
149 explicit CliHandler(void* callbackArg, Print* zprint) : m_callbackArg(callbackArg), m_print(zprint) { }
150
151 // overwrite functions
152 char const* GetTrinityString(uint32 entry) const override;
153 bool HasPermission(uint32 /*permission*/) const override { return true; }
154 void SendSysMessage(std::string_view, bool escapeCharacters) override;
155 bool ParseCommands(std::string_view str) override;
156 std::string GetNameLink() const override;
157 bool needReportToTarget(Player* chr) const override;
158 LocaleConstant GetSessionDbcLocale() const override;
159 int GetSessionDbLocaleIndex() const override;
160
161 private:
164};
165
167{
168 public:
170 bool ParseCommands(std::string_view str) override;
171 void SendSysMessage(std::string_view, bool escapeCharacters) override;
173 bool IsHumanReadable() const override { return humanReadable; }
174
175 private:
176 void Send(std::string const& msg);
177 void SendAck();
178 void SendOK();
179 void SendFailed();
180
181 char const* echo = nullptr;
182 bool hadAck = false;
183 bool humanReadable = false;
184};
185
186#endif
LocaleConstant
Definition Common.h:48
#define DEFAULT_LOCALE
Definition Common.h:62
#define TC_GAME_API
Definition Define.h:114
uint8_t uint8
Definition Define.h:135
uint32_t uint32
Definition Define.h:133
HighGuid
Definition ObjectGuid.h:63
Language
ChatMsg
bool IsHumanReadable() const override
Definition Chat.h:173
virtual bool IsHumanReadable() const
Definition Chat.h:100
std::string playerLink(std::string const &name) const
Definition Chat.h:127
static std::string PGetParseString(std::string_view fmt, Args &&... args)
Definition Chat.h:81
WorldSession * GetSession()
Definition Chat.h:46
bool HasSentErrorMessage() const
Definition Chat.h:133
ChatHandler(WorldSession *session)
Definition Chat.h:49
WorldSession * m_session
Definition Chat.h:139
std::string PGetParseString(uint32 entry, Args &&... args) const
Definition Chat.h:87
virtual ~ChatHandler()
Definition Chat.h:50
void SetSentErrorMessage(bool val)
Definition Chat.h:134
ChatHandler()
Definition Chat.h:136
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
WorldSession const * GetSession() const
Definition Chat.h:47
static char * LineFromMessage(char *&pos)
Definition Chat.h:60
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:101
bool sentErrorMessage
Definition Chat.h:142
bool IsConsole() const
Definition Chat.h:45
void PSendSysMessage(uint32 entry, Args &&... args)
Definition Chat.h:75
CliHandler(void *callbackArg, Print *zprint)
Definition Chat.h:149
void(void *, std::string_view) Print
Definition Chat.h:148
bool HasPermission(uint32) const override
Definition Chat.h:153
void * m_callbackArg
Definition Chat.h:162
Print * m_print
Definition Chat.h:163
Definition Group.h:165
uint32 LowType
Definition ObjectGuid.h:142
Definition Unit.h:769
Player session in the World.