00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "commandhandler.h"
00024 #include "channelmanager.h"
00025 #include "channel.h"
00026 #include "game.h"
00027 #include "localplayer.h"
00028
00029 #include "gui/widgets/channeltab.h"
00030 #include "gui/widgets/chattab.h"
00031 #include "gui/chat.h"
00032
00033 #include "net/adminhandler.h"
00034 #include "net/chathandler.h"
00035 #include "net/maphandler.h"
00036 #include "net/net.h"
00037
00038 #include "utils/gettext.h"
00039 #include "utils/stringutils.h"
00040 #include "utils/strprintf.h"
00041
00042 CommandHandler::CommandHandler()
00043 {}
00044
00045 void CommandHandler::handleCommand(const std::string &command, ChatTab *tab)
00046 {
00047 std::string::size_type pos = command.find(' ');
00048 std::string type(command, 0, pos);
00049 std::string args(command, pos == std::string::npos ? command.size() : pos + 1);
00050
00051 if (type == "help")
00052 {
00053 handleHelp(args, tab);
00054 }
00055 else if (tab->handleCommand(type, args))
00056 {
00057
00058 }
00059 else if (type == "announce")
00060 {
00061 handleAnnounce(args, tab);
00062 }
00063 else if (type == "where")
00064 {
00065 handleWhere(args, tab);
00066 }
00067 else if (type == "who")
00068 {
00069 handleWho(args, tab);
00070 }
00071 else if (type == "msg" || type == "whisper" || type == "w")
00072 {
00073 handleMsg(args, tab);
00074 }
00075 else if (type == "query" || type == "q")
00076 {
00077 handleQuery(args, tab);
00078 }
00079 else if (type == "join")
00080 {
00081 handleJoin(args, tab);
00082 }
00083 else if (type == "list")
00084 {
00085 handleListChannels(args, tab);
00086 }
00087 else if (type == "clear")
00088 {
00089 handleClear(args, tab);
00090 }
00091 else if (type == "party")
00092 {
00093 handleParty(args, tab);
00094 }
00095 else if (type == "me")
00096 {
00097 handleMe(args, tab);
00098 }
00099 else if (type == "record")
00100 {
00101 handleRecord(args, tab);
00102 }
00103 else if (type == "toggle")
00104 {
00105 handleToggle(args, tab);
00106 }
00107 else if (type == "present")
00108 {
00109 handlePresent(args, tab);
00110 }
00111 else
00112 {
00113 tab->chatLog("Unknown command");
00114 }
00115 }
00116
00117 char CommandHandler::parseBoolean(const std::string &value)
00118 {
00119 std::string opt = value.substr(0, 1);
00120
00121 if (opt == "1" ||
00122 opt == "y" || opt == "Y" ||
00123 opt == "t" || opt == "T")
00124 return 1;
00125 else if (opt == "0" ||
00126 opt == "n" || opt == "N" ||
00127 opt == "f" || opt == "F")
00128 return 0;
00129 else
00130 return -1;
00131 }
00132
00133 void CommandHandler::handleAnnounce(const std::string &args, ChatTab *tab)
00134 {
00135 Net::getAdminHandler()->announce(args);
00136 }
00137
00138 void CommandHandler::handleHelp(const std::string &args, ChatTab *tab)
00139 {
00140 if (args == "")
00141 {
00142 tab->chatLog(_("-- Help --"));
00143 tab->chatLog(_("/help > Display this help"));
00144
00145 tab->chatLog(_("/where > Display map name"));
00146 tab->chatLog(_("/who > Display number of online users"));
00147 tab->chatLog(_("/me > Tell something about yourself"));
00148
00149 tab->chatLog(_("/clear > Clears this window"));
00150
00151 tab->chatLog(_("/msg > Send a private message to a user"));
00152 tab->chatLog(_("/whisper > Alias of msg"));
00153 tab->chatLog(_("/w > Alias of msg"));
00154 tab->chatLog(_("/query > Makes a tab for private messages with another user"));
00155 tab->chatLog(_("/q > Alias of query"));
00156
00157 tab->chatLog(_("/list > Display all public channels"));
00158 tab->chatLog(_("/join > Join or create a channel"));
00159
00160 tab->chatLog(_("/party > Invite a user to party"));
00161
00162 tab->chatLog(_("/record > Start recording the chat to an external file"));
00163 tab->chatLog(_("/toggle > Determine whether <return> toggles the chat log"));
00164 tab->chatLog(_("/present > Get list of players present (sent to chat log, if logging)"));
00165
00166 tab->chatLog(_("/announce > Global announcement (GM only)"));
00167
00168 tab->showHelp();
00169
00170 tab->chatLog(_("For more information, type /help <command>"));
00171 }
00172 else if (args == "help")
00173 {
00174 tab->chatLog(_("Command: /help"));
00175 tab->chatLog(_("This command displays a list of all commands available."));
00176 tab->chatLog(_("Command: /help <command>"));
00177 tab->chatLog(_("This command displays help on <command>."));
00178 }
00179 else if (tab->handleCommand("help", args))
00180 {
00181
00182 }
00183 else if (args == "announce")
00184 {
00185 tab->chatLog(_("Command: /announce <msg>"));
00186 tab->chatLog(_("*** only available to a GM ***"));
00187 tab->chatLog(_("This command sends the message <msg> to "
00188 "all players currently online."));
00189 }
00190 else if (args == "clear")
00191 {
00192 tab->chatLog(_("Command: /clear"));
00193 tab->chatLog(_("This command clears the chat log of previous chat."));
00194 }
00195 else if (args == "join")
00196 {
00197 tab->chatLog(_("Command: /join <channel>"));
00198 tab->chatLog(_("This command makes you enter <channel>."));
00199 tab->chatLog(_("If <channel> doesn't exist, it's created."));
00200 }
00201 else if (args == "list")
00202 {
00203 tab->chatLog(_("Command: /list"));
00204 tab->chatLog(_("This command shows a list of all channels."));
00205 }
00206 else if (args == "me")
00207 {
00208 tab->chatLog(_("Command: /me <message>"));
00209 tab->chatLog(_("This command tell others you are (doing) <msg>."));
00210 }
00211 else if (args == "msg" || args == "whisper" || args == "w")
00212 {
00213 tab->chatLog(_("Command: /msg <nick> <message>"));
00214 tab->chatLog(_("Command: /whisper <nick> <message>"));
00215 tab->chatLog(_("Command: /w <nick> <message>"));
00216 tab->chatLog(_("This command sends the text <message> to <nick>."));
00217 tab->chatLog(_("If the <nick> has spaces in it, enclose it in "
00218 "double quotes (\")."));
00219 }
00220 else if (args == "query" || args == "q")
00221 {
00222 tab->chatLog(_("Command: /query <nick>"));
00223 tab->chatLog(_("Command: /q <nick>"));
00224 tab->chatLog(_("This command tries to make a tab for whispers between"
00225 "you and <nick>."));
00226 }
00227 else if (args == "party")
00228 {
00229 tab->chatLog(_("Command: /party <nick>"));
00230 tab->chatLog(_("This command invites <nick> to party with you."));
00231 tab->chatLog(_("If the <nick> has spaces in it, enclose it in "
00232 "double quotes (\")."));
00233 }
00234 else if (args == "present")
00235 {
00236 tab->chatLog(_("Command: /present"));
00237 tab->chatLog(_("This command gets a list of players within hearing and "
00238 "sends it to either the record log if recording, or the chat "
00239 "log otherwise."));
00240 }
00241 else if (args == "record")
00242 {
00243 tab->chatLog(_("Command: /record <filename>"));
00244 tab->chatLog(_("This command starts recording the chat log to the file "
00245 "<filename>."));
00246 tab->chatLog(_("Command: /record"));
00247 tab->chatLog(_("This command finishes a recording session."));
00248 }
00249 else if (args == "toggle")
00250 {
00251 tab->chatLog(_("Command: /toggle <state>"));
00252 tab->chatLog(_("This command sets whether the return key should toggle the "
00253 "chat log, or whether the chat log turns off automatically."));
00254 tab->chatLog(_("<state> can be one of \"1\", \"yes\", \"true\" to "
00255 "turn the toggle on, or \"0\", \"no\", \"false\" to turn the "
00256 "toggle off."));
00257 tab->chatLog(_("Command: /toggle"));
00258 tab->chatLog(_("This command displays the return toggle status."));
00259 }
00260 else if (args == "where")
00261 {
00262 tab->chatLog(_("Command: /where"));
00263 tab->chatLog(_("This command displays the name of the current map."));
00264 }
00265 else if (args == "who")
00266 {
00267 tab->chatLog(_("Command: /who"));
00268 tab->chatLog(_("This command displays the number of players currently "
00269 "online."));
00270 }
00271 else
00272 {
00273 tab->chatLog(_("Unknown command."));
00274 tab->chatLog(_("Type /help for a list of commands."));
00275 }
00276 }
00277
00278 void CommandHandler::handleWhere(const std::string &args, ChatTab *tab)
00279 {
00280
00281 tab->chatLog(map_path, BY_SERVER);
00282 }
00283
00284 void CommandHandler::handleWho(const std::string &args, ChatTab *tab)
00285 {
00286 Net::getChatHandler()->who();
00287 }
00288
00289 void CommandHandler::handleMsg(const std::string &args, ChatTab *tab)
00290 {
00291 std::string recvnick = "";
00292 std::string msg = "";
00293
00294 if (args.substr(0, 1) == "\"")
00295 {
00296 const std::string::size_type pos = args.find('"', 1);
00297 if (pos != std::string::npos)
00298 {
00299 recvnick = args.substr(1, pos - 1);
00300 if (pos + 2 < args.length())
00301 msg = args.substr(pos + 2, args.length());
00302 }
00303 }
00304 else
00305 {
00306 const std::string::size_type pos = args.find(" ");
00307 if (pos != std::string::npos)
00308 {
00309 recvnick = args.substr(0, pos);
00310 if (pos + 1 < args.length())
00311 msg = args.substr(pos + 1, args.length());
00312 }
00313 else
00314 {
00315 recvnick = std::string(args);
00316 msg = "";
00317 }
00318 }
00319
00320 trim(msg);
00321
00322 if (msg.length() > 0)
00323 {
00324 std::string playerName = player_node->getName();
00325 std::string tempNick = recvnick;
00326
00327 toLower(playerName);
00328 toLower(tempNick);
00329
00330 if (tempNick.compare(playerName) == 0 || args.empty())
00331 return;
00332
00333 chatWindow->whisper(recvnick, msg, true);
00334 }
00335 else
00336 tab->chatLog(_("Cannot send empty whispers!"), BY_SERVER);
00337 }
00338
00339 void CommandHandler::handleQuery(const std::string &args, ChatTab *tab) {
00340 if (chatWindow->addWhisperTab(args, true))
00341 return;
00342
00343 tab->chatLog(strprintf(_("Cannot create a whisper tab for nick '%s'! "
00344 "It either already exists, or is you."), args.c_str()), BY_SERVER);
00345 }
00346
00347 void CommandHandler::handleClear(const std::string &args, ChatTab *tab)
00348 {
00349 chatWindow->clearTab();
00350 }
00351
00352 void CommandHandler::handleJoin(const std::string &args, ChatTab *tab)
00353 {
00354 std::string::size_type pos = args.find(' ');
00355 std::string name(args, 0, pos);
00356 std::string password(args, pos+1);
00357 tab->chatLog("Requesting to join channel " + name);
00358 Net::getChatHandler()->enterChannel(name, password);
00359 }
00360
00361 void CommandHandler::handleListChannels(const std::string &args, ChatTab *tab)
00362 {
00363 Net::getChatHandler()->channelList();
00364 }
00365
00366 void CommandHandler::handleParty(const std::string &args, ChatTab *tab)
00367 {
00368 if (args != "")
00369 player_node->inviteToParty(args);
00370 else
00371 tab->chatLog("Please specify a name.", BY_SERVER);
00372 }
00373
00374 void CommandHandler::handleMe(const std::string &args, ChatTab *tab)
00375 {
00376 Net::getChatHandler()->me(args);
00377 }
00378
00379 void CommandHandler::handleRecord(const std::string &args, ChatTab *tab)
00380 {
00381 chatWindow->setRecordingFile(args);
00382 }
00383
00384 void CommandHandler::handleToggle(const std::string &args, ChatTab *tab)
00385 {
00386 if (args.empty())
00387 {
00388 tab->chatLog(chatWindow->getReturnTogglesChat() ?
00389 _("Return toggles chat.") : _("Message closes chat."));
00390 return;
00391 }
00392
00393 char opt = parseBoolean(args);
00394
00395 switch (opt)
00396 {
00397 case 1:
00398 tab->chatLog(_("Return now toggles chat."));
00399 chatWindow->setReturnTogglesChat(true);
00400 return;
00401 case 0:
00402 tab->chatLog(_("Message now closes chat."));
00403 chatWindow->setReturnTogglesChat(false);
00404 return;
00405 case -1:
00406 tab->chatLog(strprintf(BOOLEAN_OPTIONS, "toggle"));
00407 }
00408 }
00409
00410 void CommandHandler::handlePresent(const std::string &args, ChatTab *tab)
00411 {
00412 chatWindow->doPresent();
00413 }