Mana
Loading...
Searching...
No Matches
commandhandler.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2008-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "commandhandler.h"
23
24#include "actorspritemanager.h"
25#include "channelmanager.h"
26#include "configuration.h"
27#include "game.h"
28#include "localplayer.h"
29#include "playerrelations.h"
30
31#include "gui/widgets/chattab.h"
32
33#include "net/chathandler.h"
34#include "net/net.h"
35#include "net/partyhandler.h"
36
37#include "utils/gettext.h"
38#include "utils/stringutils.h"
39
40std::string booleanOptionInstructions(const char *command)
41{
42 return strprintf(_("Options to /%s are \"yes\", \"no\", \"true\", \"false\", \"1\", \"0\"."),
43 command);
44}
45
48
49void CommandHandler::handleCommand(const std::string &command, ChatTab *tab)
50{
51 std::string::size_type pos = command.find(' ');
52 std::string type(command, 0, pos);
53 std::string args(command, pos == std::string::npos ?
54 command.size() : pos + 1);
55 trim(args);
56
57 if (type == "help") // Do help before tabs so they can't override it
58 {
59 handleHelp(args, tab);
60 }
61 else if (tab->handleCommand(type, args))
62 {
63 // Nothing to do
64 }
65 else if (type == "where")
66 {
67 handleWhere(args, tab);
68 }
69 else if (type == "who")
70 {
71 handleWho(args, tab);
72 }
73 else if (type == "msg" || type == "whisper" || type == "w")
74 {
75 handleMsg(args, tab);
76 }
77 else if (type == "query" || type == "q")
78 {
79 handleQuery(args, tab);
80 }
81 else if (type == "ignore")
82 {
83 handleIgnore(args, tab);
84 }
85 else if (type == "unignore")
86 {
87 handleUnignore(args, tab);
88 }
89 else if (type == "join")
90 {
91 handleJoin(args, tab);
92 }
93 else if (type == "list")
94 {
95 handleListChannels(args, tab);
96 }
97 else if (type == "clear")
98 {
99 handleClear(args, tab);
100 }
101 else if (type == "createparty")
102 {
103 handleCreateParty(args, tab);
104 }
105 else if (type == "party")
106 {
107 handleParty(args, tab);
108 }
109 else if (type == "me")
110 {
111 handleMe(args, tab);
112 }
113 else if (type == "record")
114 {
115 handleRecord(args, tab);
116 }
117 else if (type == "toggle")
118 {
119 handleToggle(args, tab);
120 }
121 else if (type == "present")
122 {
123 handlePresent(args, tab);
124 }
125 else if (type == "showip" && Net::getNetworkType() == ServerType::TmwAthena)
126 {
127 handleShowIp(args, tab);
128 }
129 else if (type == "away")
130 {
131 handleAway(args, tab);
132 }
133 else
134 {
135 tab->chatLog(_("Unknown command."));
136 }
137}
138
139char CommandHandler::parseBoolean(const std::string &value)
140{
141 std::string opt = value.substr(0, 1);
142
143 if (opt == "1" ||
144 opt == "y" || opt == "Y" ||
145 opt == "t" || opt == "T")
146 return 1;
147 else if (opt == "0" ||
148 opt == "n" || opt == "N" ||
149 opt == "f" || opt == "F")
150 return 0;
151 else
152 return -1;
153}
154
155void CommandHandler::handleHelp(const std::string &args, ChatTab *tab)
156{
157 if (args.empty())
158 {
159 tab->chatLog(_("-- Help --"));
160 tab->chatLog(_("/help > Display this help"));
161
162 tab->chatLog(_("/where > Display map name"));
163
164 if (Net::getChatHandler()->whoSupported())
165 tab->chatLog(_("/who > Display number of online users"));
166
167 tab->chatLog(_("/me > Tell something about yourself"));
168
169 tab->chatLog(_("/clear > Clears this window"));
170
171 tab->chatLog(_("/msg > Send a private message to a user"));
172 tab->chatLog(_("/whisper > Alias of msg"));
173 tab->chatLog(_("/w > Alias of msg"));
174 tab->chatLog(_("/query > Makes a tab for private messages "
175 "with another user"));
176 tab->chatLog(_("/q > Alias of query"));
177
178 tab->chatLog(_("/away > Tell the other whispering players "
179 "you're away from keyboard."));
180
181 tab->chatLog(_("/ignore > ignore a player"));
182 tab->chatLog(_("/unignore > stop ignoring a player"));
183
184 tab->chatLog(_("/list > Display all public channels"));
185 tab->chatLog(_("/join > Join or create a channel"));
186
187 tab->chatLog(_("/createparty > Create a new party"));
188 tab->chatLog(_("/party > Invite a user to party"));
189
190 tab->chatLog(_("/record > Start recording the chat "
191 "to an external file"));
192 tab->chatLog(_("/toggle > Determine whether <return> "
193 "toggles the chat log"));
194 tab->chatLog(_("/present > Get list of players present "
195 "(sent to chat log, if logging)"));
196
197 tab->showHelp(); // Allow the tab to show it's help
198
199 tab->chatLog(_("For more information, type /help <command>."));
200 }
201 else if (args == "help") // Do this before tabs so they can't change it
202 {
203 tab->chatLog(_("Command: /help"));
204 tab->chatLog(_("This command displays a list "
205 "of all commands available."));
206 tab->chatLog(_("Command: /help <command>"));
207 tab->chatLog(_("This command displays help on <command>."));
208 }
209 else if (tab->handleCommand("help", args))
210 {
211 // Nothing to do
212 }
213 else if (args == "clear")
214 {
215 tab->chatLog(_("Command: /clear"));
216 tab->chatLog(_("This command clears the chat log of previous chat."));
217 }
218 else if (args == "ignore")
219 {
220 tab->chatLog(_("Command: /ignore <player>"));
221 tab->chatLog(_("This command ignores the given player regardless of "
222 "current relations."));
223 }
224 else if (args == "join")
225 {
226 tab->chatLog(_("Command: /join <channel>"));
227 tab->chatLog(_("This command makes you enter <channel>."));
228 tab->chatLog(_("If <channel> doesn't exist, it's created."));
229 }
230 else if (args == "list")
231 {
232 tab->chatLog(_("Command: /list"));
233 tab->chatLog(_("This command shows a list of all channels."));
234 }
235 else if (args == "me")
236 {
237 tab->chatLog(_("Command: /me <message>"));
238 tab->chatLog(_("This command tell others you are (doing) <msg>."));
239 }
240 else if (args == "msg" || args == "whisper" || args == "w")
241 {
242 tab->chatLog(_("Command: /msg <nick> <message>"));
243 tab->chatLog(_("Command: /whisper <nick> <message>"));
244 tab->chatLog(_("Command: /w <nick> <message>"));
245 tab->chatLog(_("This command sends the text <message> to <nick>."));
246 tab->chatLog(_("If the <nick> has spaces in it, enclose it in "
247 "double quotes (\")."));
248 }
249 else if (args == "query" || args == "q")
250 {
251 tab->chatLog(_("Command: /query <nick>"));
252 tab->chatLog(_("Command: /q <nick>"));
253 tab->chatLog(_("This command tries to make a tab for whispers between "
254 "you and <nick>."));
255 }
256 else if (args == "away")
257 {
258 tab->chatLog(_("Command: /away <afk reason>"));
259 tab->chatLog(_("This command tells "
260 "you're away from keyboard with the given reason."));
261 tab->chatLog(_("Command: /away"));
262 tab->chatLog(_("This command clears the away status and message."));
263 }
264 else if (args == "createparty")
265 {
266 tab->chatLog(_("Command: /createparty <name>"));
267 tab->chatLog(_("This command creates a new party called <name>."));
268 }
269 else if (args == "party")
270 {
271 tab->chatLog(_("Command: /party <nick>"));
272 tab->chatLog(_("This command invites <nick> to party with you."));
273 tab->chatLog(_("If the <nick> has spaces in it, enclose it in "
274 "double quotes (\")."));
275 }
276 else if (args == "present")
277 {
278 tab->chatLog(_("Command: /present"));
279 tab->chatLog(_("This command gets a list of players within hearing and "
280 "sends it to either the record log if recording, or the chat "
281 "log otherwise."));
282 }
283 else if (args == "record")
284 {
285 tab->chatLog(_("Command: /record <filename>"));
286 tab->chatLog(_("This command starts recording the chat log to the file "
287 "<filename>."));
288 tab->chatLog(_("Command: /record"));
289 tab->chatLog(_("This command finishes a recording session."));
290 }
291 else if (args == "toggle")
292 {
293 tab->chatLog(_("Command: /toggle <state>"));
294 tab->chatLog(_("This command sets whether the return key should toggle the "
295 "chat log, or whether the chat log turns off automatically."));
296 tab->chatLog(_("<state> can be one of \"1\", \"yes\", \"true\" to "
297 "turn the toggle on, or \"0\", \"no\", \"false\" to turn the "
298 "toggle off."));
299 tab->chatLog(_("Command: /toggle"));
300 tab->chatLog(_("This command displays the return toggle status."));
301 }
302 else if (args == "unignore")
303 {
304 tab->chatLog(_("Command: /unignore <player>"));
305 tab->chatLog(_("This command stops ignoring the given player if they "
306 "are being ignored"));
307 }
308 else if (args == "where")
309 {
310 tab->chatLog(_("Command: /where"));
311 tab->chatLog(_("This command displays the name of the current map."));
312 }
313 else if (args == "who" && Net::getChatHandler()->whoSupported())
314 {
315 tab->chatLog(_("Command: /who"));
316 tab->chatLog(_("This command displays the number of players currently "
317 "online."));
318 }
319 else
320 {
321 tab->chatLog(_("Unknown command."));
322 tab->chatLog(_("Type /help for a list of commands."));
323 }
324}
325
326void CommandHandler::handleWhere(const std::string &args, ChatTab *tab)
327{
328 std::ostringstream where;
329 where << Game::instance()->getCurrentMapName() << ", coordinates: "
330 << local_player->getTileX() << ", "
332
333 tab->chatLog(where.str(), BY_SERVER);
334}
335
336void CommandHandler::handleWho(const std::string &args, ChatTab *tab)
337{
339}
340
341void CommandHandler::handleMsg(const std::string &args, ChatTab *tab)
342{
343 std::string recvnick;
344 std::string msg;
345
346 if (args.substr(0, 1) == "\"")
347 {
348 const std::string::size_type pos = args.find('"', 1);
349 if (pos != std::string::npos)
350 {
351 recvnick = args.substr(1, pos - 1);
352 if (pos + 2 < args.length())
353 msg = args.substr(pos + 2, args.length());
354 }
355 }
356 else
357 {
358 const std::string::size_type pos = args.find(" ");
359 if (pos != std::string::npos)
360 {
361 recvnick = args.substr(0, pos);
362 if (pos + 1 < args.length())
363 msg = args.substr(pos + 1, args.length());
364 }
365 else
366 {
367 recvnick = std::string(args);
368 msg.clear();
369 }
370 }
371
372 trim(msg);
373
374 if (msg.length() > 0)
375 {
376 std::string playerName = local_player->getName();
377 std::string tempNick = recvnick;
378
379 toLower(playerName);
380 toLower(tempNick);
381
382 if (tempNick.compare(playerName) == 0 || args.empty())
383 return;
384
385 chatWindow->whisper(recvnick, msg, BY_PLAYER);
386 }
387 else
388 tab->chatLog(_("Cannot send empty whispers!"), BY_SERVER);
389}
390
391void CommandHandler::handleQuery(const std::string &args, ChatTab *tab)
392{
393 if (args.empty())
394 {
395 tab->chatLog(_("No <nick> was given."), BY_SERVER);
396 return;
397 }
398
399 if (args.length() > 1 && args[0] == '\"' && args[args.length() - 1] == '\"')
400 {
401 if (chatWindow->addWhisperTab(args.substr(1, args.length() - 2), true))
402 return;
403 }
404 else if (chatWindow->addWhisperTab(args, true))
405 {
406 return;
407 }
408
409 tab->chatLog(strprintf(_("Cannot create a whisper tab for nick \"%s\"! "
410 "It either already exists, or is you."), args.c_str()), BY_SERVER);
411}
412
413void CommandHandler::handleClear(const std::string &args, ChatTab *tab)
414{
416}
417
418void CommandHandler::handleJoin(const std::string &args, ChatTab *tab)
419{
420 std::string::size_type pos = args.find(' ');
421 std::string name(args, 0, pos);
422 std::string password(args, pos + 1);
423 tab->chatLog(strprintf(_("Requesting to join channel %s."), name.c_str()));
424 Net::getChatHandler()->enterChannel(name, password);
425}
426
427void CommandHandler::handleListChannels(const std::string &args, ChatTab *tab)
428{
430}
431
432void CommandHandler::handleCreateParty(const std::string &args, ChatTab *tab)
433{
434 if (args.empty())
435 tab->chatLog(_("Party name is missing."), BY_SERVER);
436 else
438}
439
440void CommandHandler::handleParty(const std::string &args, ChatTab *tab)
441{
442 if (args.empty())
443 tab->chatLog(_("Please specify a name."), BY_SERVER);
444 else
446}
447
448void CommandHandler::handleMe(const std::string &args, ChatTab *tab)
449{
450 Net::getChatHandler()->me(args);
451}
452
453void CommandHandler::handleRecord(const std::string &args, ChatTab *tab)
454{
456}
457
458void CommandHandler::handleToggle(const std::string &args, ChatTab *tab)
459{
460 if (args.empty())
461 {
462 tab->chatLog(config.returnTogglesChat ? _("Return toggles chat.")
463 : _("Message closes chat."));
464 return;
465 }
466
467 char opt = parseBoolean(args);
468
469 switch (opt)
470 {
471 case 1:
472 tab->chatLog(_("Return now toggles chat."));
474 return;
475 case 0:
476 tab->chatLog(_("Message now closes chat."));
478 return;
479 case -1:
480 tab->chatLog(booleanOptionInstructions("toggle"));
481 }
482}
483
484void CommandHandler::handleShowIp(const std::string &args, ChatTab *tab)
485{
486 if (args.empty())
487 {
489 _("Show IP: On") : _("Show IP: Off"));
490 return;
491 }
492
493 char opt = parseBoolean(args);
494
495 switch (opt)
496 {
497 case 0:
498 tab->chatLog(_("Show IP: Off"));
499 local_player->setShowIp(false);
500 break;
501 case 1:
502 tab->chatLog(_("Show IP: On"));
503 local_player->setShowIp(true);
504 break;
505 case -1:
506 tab->chatLog(booleanOptionInstructions("showip"));
507 return;
508 }
509
511}
512
513void CommandHandler::handlePresent(const std::string &args, ChatTab *tab)
514{
516}
517
518void CommandHandler::handleIgnore(const std::string &args, ChatTab *tab)
519{
520 if (args.empty())
521 {
522 tab->chatLog(_("Please specify a name."), BY_SERVER);
523 return;
524 }
525
527 {
528 tab->chatLog(_("Player already ignored!"), BY_SERVER);
529 return;
530 }
531 else
533
535 tab->chatLog(_("Player successfully ignored!"), BY_SERVER);
536 else
537 tab->chatLog(_("Player could not be ignored!"), BY_SERVER);
538}
539
540void CommandHandler::handleUnignore(const std::string &args, ChatTab *tab)
541{
542 if (args.empty())
543 {
544 tab->chatLog(_("Please specify a name."), BY_SERVER);
545 return;
546 }
547
550 else
551 {
552 tab->chatLog(_("Player wasn't ignored!"), BY_SERVER);
553 return;
554 }
555
557 tab->chatLog(_("Player no longer ignored!"), BY_SERVER);
558 else
559 tab->chatLog(_("Player could not be unignored!"), BY_SERVER);
560}
561
562void CommandHandler::handleAway(const std::string &args, ChatTab *tab)
563{
564 if (!args.empty())
565 config.afkMessage = args;
567}
ActorSpriteManager * actorSpriteManager
Definition game.cpp:110
@ BY_PLAYER
Definition chatwindow.h:48
@ BY_SERVER
Definition chatwindow.h:50
int getTileX() const override
Returns the tile x coord.
Definition being.h:146
const std::string & getName() const
Returns the name of the being.
Definition being.h:190
int getTileY() const override
Returns the tile y coord.
Definition being.h:152
A tab for the chat window.
Definition chattab.h:36
void chatLog(std::string line, Own own=BY_SERVER, bool ignoreRecord=false)
Adds a line of text to our message list.
Definition chattab.cpp:111
virtual bool handleCommand(const std::string &type, const std::string &args)
Handle special commands.
Definition chattab.h:94
virtual void showHelp()
Add any extra help text to the output.
Definition chattab.h:85
void whisper(const std::string &nick, const std::string &mes, Own own=BY_OTHER)
void clearTab()
Clear the current tab.
void setRecordingFile(const std::string &msg)
Sets the file being recorded to.
ChatTab * addWhisperTab(const std::string &nick, bool switchTo=false)
void doPresent()
void handlePresent(const std::string &args, ChatTab *tab)
Handle a present command.
void handleCommand(const std::string &command, ChatTab *tab=localChatTab)
Parse and handle the given command.
void handleQuery(const std::string &args, ChatTab *tab)
Handle a msg tab request.
void handleIgnore(const std::string &args, ChatTab *tab)
Handle an ignore command.
void handleShowIp(const std::string &args, ChatTab *tab)
void handleClear(const std::string &args, ChatTab *tab)
Handle a clear command.
static char parseBoolean(const std::string &value)
void handleParty(const std::string &args, ChatTab *tab)
Handle a party command.
void handleMe(const std::string &args, ChatTab *tab)
Handle a me command.
void handleListChannels(const std::string &args, ChatTab *tab)
Handle a listchannels command.
void handleWhere(const std::string &args, ChatTab *tab)
Handle a where command.
void handleRecord(const std::string &args, ChatTab *tab)
Handle a record command.
void handleWho(const std::string &args, ChatTab *tab)
Handle a who command.
void handleHelp(const std::string &args, ChatTab *tab)
Handle a help command.
void handleCreateParty(const std::string &args, ChatTab *tab)
Handle a createparty command.
void handleJoin(const std::string &args, ChatTab *tab)
Handle a join command.
void handleAway(const std::string &args, ChatTab *tab)
Handle away command.
void handleUnignore(const std::string &args, ChatTab *tab)
Handle an unignore command.
void handleToggle(const std::string &args, ChatTab *tab)
Handle a toggle command.
void handleMsg(const std::string &args, ChatTab *tab)
Handle a msg command.
const std::string & getCurrentMapName()
Definition game.h:72
static Game * instance()
Provides access to the game instance.
Definition game.h:53
bool getAwayMode() const
void setShowIp(bool show)
void setAwayMode(bool away)
bool getShowIp() const
virtual void enterChannel(const std::string &channel, const std::string &password)=0
virtual void who()=0
virtual void me(const std::string &text)=0
virtual void channelList()=0
virtual void create(const std::string &name=std::string())=0
virtual void invite(Being *player)=0
PlayerRelation getRelation(const std::string &name) const
Updates the relationship with this player.
void setRelation(const std::string &name, PlayerRelation relation)
Updates the relationship with this player.
void removePlayer(const std::string &name)
Deletes the information recorded for a player.
Config config
Global settings (config.xml)
Definition client.cpp:97
std::string booleanOptionInstructions(const char *command)
std::string booleanOptionInstructions(const char *command)
ChatWindow * chatWindow
Definition game.cpp:93
#define _(s)
Definition gettext.h:38
LocalPlayer * local_player
ServerType getNetworkType()
Definition net.cpp:200
ChatHandler * getChatHandler()
Definition net.cpp:70
PartyHandler * getPartyHandler()
Definition net.cpp:105
PlayerRelationsManager player_relations
std::string & toLower(std::string &str)
Converts the given string to lower case.
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.
std::string & trim(std::string &str)
Trims spaces off the end and the beginning of the given string.
bool returnTogglesChat
std::string afkMessage