[7928] Player must standup at stun (from 1.5 client starting)
[getmangos.git] / src / mangosd / CliRunnable.cpp
blob5e7c05dbc2571c6e38613296ac41ff11f60e489e
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /// \addtogroup mangosd
20 /// @{
21 /// \file
23 #include "Common.h"
24 #include "Language.h"
25 #include "Log.h"
26 #include "World.h"
27 #include "ScriptCalls.h"
28 #include "ObjectMgr.h"
29 #include "WorldSession.h"
30 #include "Config/ConfigEnv.h"
31 #include "Util.h"
32 #include "AccountMgr.h"
33 #include "CliRunnable.h"
34 #include "MapManager.h"
35 #include "Player.h"
36 #include "Chat.h"
38 void utf8print(const char* str)
40 #if PLATFORM == PLATFORM_WINDOWS
41 wchar_t wtemp_buf[6000];
42 size_t wtemp_len = 6000-1;
43 if(!Utf8toWStr(str,strlen(str),wtemp_buf,wtemp_len))
44 return;
46 char temp_buf[6000];
47 CharToOemBuffW(&wtemp_buf[0],&temp_buf[0],wtemp_len+1);
48 printf(temp_buf);
49 #else
50 printf(str);
51 #endif
54 /// Delete a user account and all associated characters in this realm
55 /// \todo This function has to be enhanced to respect the login/realm split (delete char, delete account chars in realm, delete account chars in realm then delete account
56 bool ChatHandler::HandleAccountDeleteCommand(const char* args)
58 if(!*args)
59 return false;
61 ///- Get the account name from the command line
62 char *account_name_str=strtok ((char*)args," ");
63 if (!account_name_str)
64 return false;
66 std::string account_name = account_name_str;
67 if(!AccountMgr::normilizeString(account_name))
69 PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
70 SetSentErrorMessage(true);
71 return false;
74 uint32 account_id = accmgr.GetId(account_name);
75 if(!account_id)
77 PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
78 SetSentErrorMessage(true);
79 return false;
82 /// Commands not recommended call from chat, but support anyway
83 /// can delete only for account with less security
84 /// This is also reject self apply in fact
85 if(HasLowerSecurityAccount (NULL,account_id,true))
86 return false;
88 AccountOpResult result = accmgr.DeleteAccount(account_id);
89 switch(result)
91 case AOR_OK:
92 PSendSysMessage(LANG_ACCOUNT_DELETED,account_name.c_str());
93 break;
94 case AOR_NAME_NOT_EXIST:
95 PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
96 SetSentErrorMessage(true);
97 return false;
98 case AOR_DB_INTERNAL_ERROR:
99 PSendSysMessage(LANG_ACCOUNT_NOT_DELETED_SQL_ERROR,account_name.c_str());
100 SetSentErrorMessage(true);
101 return false;
102 default:
103 PSendSysMessage(LANG_ACCOUNT_NOT_DELETED,account_name.c_str());
104 SetSentErrorMessage(true);
105 return false;
108 return true;
111 bool ChatHandler::HandleCharacterDeleteCommand(const char* args)
113 if(!*args)
114 return false;
116 char *character_name_str = strtok((char*)args," ");
117 if(!character_name_str)
118 return false;
120 std::string character_name = character_name_str;
121 if(!normalizePlayerName(character_name))
122 return false;
124 uint64 character_guid;
125 uint32 account_id;
127 Player *player = objmgr.GetPlayer(character_name.c_str());
128 if(player)
130 character_guid = player->GetGUID();
131 account_id = player->GetSession()->GetAccountId();
132 player->GetSession()->KickPlayer();
134 else
136 character_guid = objmgr.GetPlayerGUIDByName(character_name);
137 if(!character_guid)
139 PSendSysMessage(LANG_NO_PLAYER,character_name.c_str());
140 SetSentErrorMessage(true);
141 return false;
144 account_id = objmgr.GetPlayerAccountIdByGUID(character_guid);
147 std::string account_name;
148 accmgr.GetName (account_id,account_name);
150 Player::DeleteFromDB(character_guid, account_id, true);
151 PSendSysMessage(LANG_CHARACTER_DELETED,character_name.c_str(),GUID_LOPART(character_guid),account_name.c_str(), account_id);
152 return true;
155 /// Exit the realm
156 bool ChatHandler::HandleServerExitCommand(const char* /*args*/)
158 SendSysMessage(LANG_COMMAND_EXIT);
159 World::StopNow(SHUTDOWN_EXIT_CODE);
160 return true;
163 /// Display info on users currently in the realm
164 bool ChatHandler::HandleAccountOnlineListCommand(const char* /*args*/)
166 ///- Get the list of accounts ID logged to the realm
167 QueryResult *resultDB = CharacterDatabase.Query("SELECT name,account FROM characters WHERE online > 0");
168 if (!resultDB)
170 SendSysMessage(LANG_ACCOUNT_LIST_EMPTY);
171 return true;
174 ///- Display the list of account/characters online
175 SendSysMessage(LANG_ACCOUNT_LIST_BAR);
176 SendSysMessage(LANG_ACCOUNT_LIST_HEADER);
177 SendSysMessage(LANG_ACCOUNT_LIST_BAR);
179 ///- Circle through accounts
182 Field *fieldsDB = resultDB->Fetch();
183 std::string name = fieldsDB[0].GetCppString();
184 uint32 account = fieldsDB[1].GetUInt32();
186 ///- Get the username, last IP and GM level of each account
187 // No SQL injection. account is uint32.
188 // 0 1 2 3
189 QueryResult *resultLogin = loginDatabase.PQuery("SELECT username, last_ip, gmlevel, expansion FROM account WHERE id = '%u'",account);
191 if(resultLogin)
193 Field *fieldsLogin = resultLogin->Fetch();
194 PSendSysMessage(LANG_ACCOUNT_LIST_LINE,
195 fieldsLogin[0].GetString(),name.c_str(),fieldsLogin[1].GetString(),fieldsLogin[2].GetUInt32(),fieldsLogin[3].GetUInt32());
197 delete resultLogin;
199 else
200 PSendSysMessage(LANG_ACCOUNT_LIST_ERROR,name.c_str());
202 }while(resultDB->NextRow());
204 delete resultDB;
206 SendSysMessage(LANG_ACCOUNT_LIST_BAR);
207 return true;
210 /// Create an account
211 bool ChatHandler::HandleAccountCreateCommand(const char* args)
213 if(!*args)
214 return false;
216 ///- %Parse the command line arguments
217 char *szAcc = strtok((char*)args, " ");
218 char *szPassword = strtok(NULL, " ");
219 if(!szAcc || !szPassword)
220 return false;
222 // normilized in accmgr.CreateAccount
223 std::string account_name = szAcc;
224 std::string password = szPassword;
226 AccountOpResult result = accmgr.CreateAccount(account_name, password);
227 switch(result)
229 case AOR_OK:
230 PSendSysMessage(LANG_ACCOUNT_CREATED,account_name.c_str());
231 break;
232 case AOR_NAME_TOO_LONG:
233 SendSysMessage(LANG_ACCOUNT_TOO_LONG);
234 SetSentErrorMessage(true);
235 return false;
236 case AOR_NAME_ALREDY_EXIST:
237 SendSysMessage(LANG_ACCOUNT_ALREADY_EXIST);
238 SetSentErrorMessage(true);
239 return false;
240 case AOR_DB_INTERNAL_ERROR:
241 PSendSysMessage(LANG_ACCOUNT_NOT_CREATED_SQL_ERROR,account_name.c_str());
242 SetSentErrorMessage(true);
243 return false;
244 default:
245 PSendSysMessage(LANG_ACCOUNT_NOT_CREATED,account_name.c_str());
246 SetSentErrorMessage(true);
247 return false;
250 return true;
253 /// Set the level of logging
254 bool ChatHandler::HandleServerSetLogLevelCommand(const char *args)
256 if(!*args)
257 return false;
259 char *NewLevel = strtok((char*)args, " ");
260 if (!NewLevel)
261 return false;
263 sLog.SetLogLevel(NewLevel);
264 return true;
267 /// @}
269 #ifdef linux
270 // Non-blocking keypress detector, when return pressed, return 1, else always return 0
271 int kb_hit_return()
273 struct timeval tv;
274 fd_set fds;
275 tv.tv_sec = 0;
276 tv.tv_usec = 0;
277 FD_ZERO(&fds);
278 FD_SET(STDIN_FILENO, &fds);
279 select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
280 return FD_ISSET(STDIN_FILENO, &fds);
282 #endif
284 /// %Thread start
285 void CliRunnable::run()
287 ///- Init new SQL thread for the world database (one connection call enough)
288 WorldDatabase.ThreadStart(); // let thread do safe mySQL requests
290 char commandbuf[256];
292 ///- Display the list of available CLI functions then beep
293 sLog.outString();
295 if(sConfig.GetBoolDefault("BeepAtStart", true))
296 printf("\a"); // \a = Alert
298 // print this here the first time
299 // later it will be printed after command queue updates
300 printf("mangos>");
302 ///- As long as the World is running (no World::m_stopEvent), get the command line and handle it
303 while (!World::IsStopped())
305 fflush(stdout);
306 #ifdef linux
307 while (!kb_hit_return() && !World::IsStopped())
308 // With this, we limit CLI to 10commands/second
309 usleep(100);
310 if (World::IsStopped())
311 break;
312 #endif
313 char *command_str = fgets(commandbuf,sizeof(commandbuf),stdin);
314 if (command_str != NULL)
316 for(int x=0;command_str[x];x++)
317 if(command_str[x]=='\r'||command_str[x]=='\n')
319 command_str[x]=0;
320 break;
324 if(!*command_str)
326 printf("mangos>");
327 continue;
330 std::string command;
331 if(!consoleToUtf8(command_str,command)) // convert from console encoding to utf8
333 printf("mangos>");
334 continue;
337 sWorld.QueueCliCommand(&utf8print,command.c_str());
339 else if (feof(stdin))
341 World::StopNow(SHUTDOWN_EXIT_CODE);
345 ///- End the database thread
346 WorldDatabase.ThreadEnd(); // free mySQL thread resources