TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Chat.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITYCORE_CHAT_H
20 #define TRINITYCORE_CHAT_H
21 
22 #include "SharedDefines.h"
23 #include "StringFormat.h"
24 #include "WorldSession.h"
25 #include "RBAC.h"
26 #include "Packets/ChatPackets.h"
27 
28 #include <vector>
29 
30 class ChatHandler;
31 class Creature;
32 class Group;
33 class Player;
34 class Unit;
35 class WorldSession;
36 class WorldObject;
37 
38 struct GameTele;
39 
41 {
42  typedef bool(*pHandler)(ChatHandler*, char const*);
43 
44  public:
45  ChatCommand(char const* name, uint32 permission, bool allowConsole, pHandler handler, std::string help, std::vector<ChatCommand> childCommands = std::vector<ChatCommand>())
46  : Name(ASSERT_NOTNULL(name)), Permission(permission), AllowConsole(allowConsole), Handler(handler), Help(std::move(help)), ChildCommands(std::move(childCommands)) { }
47 
48  char const* Name;
49  uint32 Permission; // function pointer required correct align (use uint32)
51  pHandler Handler;
52  std::string Help;
53  std::vector<ChatCommand> ChildCommands;
54 };
55 
56 class TC_GAME_API ChatHandler
57 {
58  public:
59  WorldSession* GetSession() { return m_session; }
60  explicit ChatHandler(WorldSession* session) : m_session(session), sentErrorMessage(false) { }
61  virtual ~ChatHandler() { }
62 
63  static char* LineFromMessage(char*& pos) { char* start = strtok(pos, "\n"); pos = NULL; return start; }
64 
65  // function with different implementation for chat/console
66  virtual char const* GetTrinityString(uint32 entry) const;
67  virtual void SendSysMessage(char const* str, bool escapeCharacters = false);
68 
69  void SendSysMessage(uint32 entry);
70 
71  template<typename... Args>
72  void PSendSysMessage(const char* fmt, Args&&... args)
73  {
74  SendSysMessage(Trinity::StringFormat(fmt, std::forward<Args>(args)...).c_str());
75  }
76 
77  template<typename... Args>
78  void PSendSysMessage(uint32 entry, Args&&... args)
79  {
80  SendSysMessage(PGetParseString(entry, std::forward<Args>(args)...).c_str());
81  }
82 
83  template<typename... Args>
84  std::string PGetParseString(uint32 entry, Args&&... args) const
85  {
86  return Trinity::StringFormat(GetTrinityString(entry), std::forward<Args>(args)...);
87  }
88 
89  bool ParseCommands(const char* text);
90 
91  static std::vector<ChatCommand> const& getCommandTable();
92 
93  bool isValidChatMessage(const char* msg);
94  void SendGlobalSysMessage(const char *str);
95 
96  bool hasStringAbbr(const char* name, const char* part);
97 
98  // function with different implementation for chat/console
99  virtual bool isAvailable(ChatCommand const& cmd) const;
100  virtual bool HasPermission(uint32 permission) const { return m_session->HasPermission(permission); }
101  virtual std::string GetNameLink() const { return GetNameLink(m_session->GetPlayer()); }
102  virtual bool needReportToTarget(Player* chr) const;
103  virtual LocaleConstant GetSessionDbcLocale() const;
104  virtual int GetSessionDbLocaleIndex() const;
105 
106  bool HasLowerSecurity(Player* target, ObjectGuid guid, bool strong = false);
107  bool HasLowerSecurityAccount(WorldSession* target, uint32 account, bool strong = false);
108 
109  void SendGlobalGMSysMessage(const char *str);
110  Player* getSelectedPlayer();
111  Creature* getSelectedCreature();
112  Unit* getSelectedUnit();
113  WorldObject* getSelectedObject();
114  // Returns either the selected player or self if there is no selected player
115  Player* getSelectedPlayerOrSelf();
116 
117  char* extractKeyFromLink(char* text, char const* linkType, char** something1 = NULL);
118  char* extractKeyFromLink(char* text, char const* const* linkTypes, int* found_idx, char** something1 = NULL);
119 
120  // if args have single value then it return in arg2 and arg1 == NULL
121  void extractOptFirstArg(char* args, char** arg1, char** arg2);
122  char* extractQuotedArg(char* args);
123 
124  uint32 extractSpellIdFromLink(char* text);
125  ObjectGuid extractGuidFromLink(char* text);
126  GameTele const* extractGameTeleFromLink(char* text);
127  bool GetPlayerGroupAndGUIDByName(const char* cname, Player*& player, Group*& group, ObjectGuid& guid, bool offline = false);
128  std::string extractPlayerNameFromLink(char* text);
129  // select by arg (name/link) or in-game selection online/offline player or self if a creature is selected
130  bool extractPlayerTarget(char* args, Player** player, ObjectGuid* player_guid = NULL, std::string* player_name = NULL);
131 
132  std::string playerLink(std::string const& name) const { return m_session ? "|cffffffff|Hplayer:"+name+"|h["+name+"]|h|r" : name; }
133  std::string GetNameLink(Player* chr) const;
134 
135  GameObject* GetNearbyGameObject();
136  GameObject* GetObjectGlobalyWithGuidOrNearWithDbGuid(ObjectGuid::LowType lowguid, uint32 entry);
137  bool HasSentErrorMessage() const { return sentErrorMessage; }
138  void SetSentErrorMessage(bool val){ sentErrorMessage = val; }
139  static bool LoadCommandTable() { return load_command_table; }
140  static void SetLoadCommandTable(bool val) { load_command_table = val; }
141 
142  bool ShowHelpForCommand(std::vector<ChatCommand> const& table, const char* cmd);
143  protected:
144  explicit ChatHandler() : m_session(NULL), sentErrorMessage(false) { } // for CLI subclass
145  static bool SetDataForCommandInTable(std::vector<ChatCommand>& table, const char* text, uint32 permission, std::string const& help, std::string const& fullcommand);
146  bool ExecuteCommandInTable(std::vector<ChatCommand> const& table, const char* text, std::string const& fullcmd);
147  bool ShowHelpForSubCommands(std::vector<ChatCommand> const& table, char const* cmd, char const* subcmd);
148 
149  private:
150  WorldSession* m_session; // != NULL for chat command call and NULL for CLI command
151 
152  // common global flag
153  static bool load_command_table;
155 };
156 
157 class TC_GAME_API CliHandler : public ChatHandler
158 {
159  public:
160  typedef void Print(void*, char const*);
161  explicit CliHandler(void* callbackArg, Print* zprint) : m_callbackArg(callbackArg), m_print(zprint) { }
162 
163  // overwrite functions
164  char const* GetTrinityString(uint32 entry) const override;
165  bool isAvailable(ChatCommand const& cmd) const override;
166  bool HasPermission(uint32 /*permission*/) const override { return true; }
167  void SendSysMessage(const char *str, bool escapeCharacters) override;
168  std::string GetNameLink() const override;
169  bool needReportToTarget(Player* chr) const override;
170  LocaleConstant GetSessionDbcLocale() const override;
171  int GetSessionDbLocaleIndex() const override;
172 
173  private:
175  Print* m_print;
176 };
177 
178 #endif
void PSendSysMessage(uint32 entry, Args &&...args)
Definition: Chat.h:78
virtual int GetSessionDbLocaleIndex() const
Definition: Chat.cpp:1067
static bool LoadCommandTable()
Definition: Chat.h:139
void SetSentErrorMessage(bool val)
Definition: Chat.h:138
T * ASSERT_NOTNULL(T *pointer)
Definition: Errors.h:58
ChatHandler(WorldSession *session)
Definition: Chat.h:60
virtual bool isAvailable(ChatCommand const &cmd) const
Definition: Chat.cpp:72
static bool load_command_table
Definition: Chat.h:153
static char * LineFromMessage(char *&pos)
Definition: Chat.h:63
std::string PGetParseString(uint32 entry, Args &&...args) const
Definition: Chat.h:84
STL namespace.
arena_t NULL
Definition: jemalloc_internal.h:624
#define false
Definition: CascPort.h:18
Definition: ObjectMgr.h:125
void Print(void *, char const *)
Definition: Chat.h:160
Role Based Access Control related classes definition.
Definition: Object.h:423
Definition: Creature.h:467
std::string Help
Definition: Chat.h:52
WorldSession * m_session
Definition: Chat.h:150
uint64 LowType
Definition: ObjectGuid.h:199
#define bool
Definition: CascPort.h:16
CliHandler(void *callbackArg, Print *zprint)
Definition: Chat.h:161
uint32 Permission
Definition: Chat.h:49
bool HasPermission(uint32) const override
Definition: Chat.h:166
Print * m_print
Definition: Chat.h:175
char const * Name
Definition: Chat.h:48
LocaleConstant
Definition: Common.h:115
virtual std::string GetNameLink() const
Definition: Chat.h:101
bool sentErrorMessage
Definition: Chat.h:154
Definition: Chat.h:157
uint32_t uint32
Definition: Define.h:150
Definition: GameObject.h:880
virtual ~ChatHandler()
Definition: Chat.h:61
static void SetLoadCommandTable(bool val)
Definition: Chat.h:140
ChatCommand(char const *name, uint32 permission, bool allowConsole, pHandler handler, std::string help, std::vector< ChatCommand > childCommands=std::vector< ChatCommand >())
Definition: Chat.h:45
Definition: Chat.h:40
ChatHandler()
Definition: Chat.h:144
bool HasSentErrorMessage() const
Definition: Chat.h:137
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
Definition: Chat.h:56
std::vector< ChatCommand > ChildCommands
Definition: Chat.h:53
#define TC_GAME_API
Definition: Define.h:134
virtual char const * GetTrinityString(uint32 entry) const
Definition: Chat.cpp:67
WorldSession * GetSession()
Definition: Chat.h:59
Definition: format.h:285
void PSendSysMessage(const char *fmt, Args &&...args)
Definition: Chat.h:72
Player session in the World.
Definition: WorldSession.h:882
virtual LocaleConstant GetSessionDbcLocale() const
Definition: Chat.cpp:1062
Definition: ObjectGuid.h:189
virtual void SendSysMessage(char const *str, bool escapeCharacters=false)
Definition: Chat.cpp:152
Definition: Unit.h:1305
virtual bool needReportToTarget(Player *chr) const
Definition: Chat.cpp:1056
virtual bool HasPermission(uint32 permission) const
Definition: Chat.h:100
pHandler Handler
Definition: Chat.h:51
bool AllowConsole
Definition: Chat.h:50
std::string playerLink(std::string const &name) const
Definition: Chat.h:132
Definition: Group.h:191
void * m_callbackArg
Definition: Chat.h:174