summaryrefslogtreecommitdiff
path: root/src/commandhandler.cpp
blob: fa5fbb673bb535b13a4f6e1808d38c91380003f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
 *  The Mana World
 *  Copyright 2008 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  The Mana World is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "commandhandler.h"
#include "channelmanager.h"
#include "channel.h"
#include "game.h"
#include "localplayer.h"

#include "gui/chat.h"

#ifdef TMWSERV_SUPPORT
#include "net/tmwserv/chatserver/chatserver.h"
#include "net/tmwserv/gameserver/player.h"
#else
#include "party.h"
#include "net/messageout.h"
#include "net/ea/protocol.h"
#endif

#include "utils/gettext.h"
#include "utils/stringutils.h"
#include "utils/strprintf.h"

#ifdef TMWSERV_SUPPORT
CommandHandler::CommandHandler()
#else
CommandHandler::CommandHandler(Network *network):
    mNetwork(network)
#endif
{}

void CommandHandler::handleCommand(const std::string &command)
{
    std::string::size_type pos = command.find(' ');
    std::string type(command, 0, pos);
    std::string args(command, pos == std::string::npos ? command.size() : pos + 1);

    if (type == "announce")
    {
        handleAnnounce(args);
    }
    else if (type == "help")
    {
        handleHelp(args);
    }
    else if (type == "where")
    {
        handleWhere();
    }
    else if (type == "who")
    {
        handleWho();
    }
    else if (type == "msg" || type == "whisper" || type == "w")
    {
        handleMsg(args);
    }
#ifdef TMWSERV_SUPPORT
    else if (type == "join")
    {
        handleJoin(args);
    }
    else if (type == "list")
    {
        handleListChannels();
    }
    else if (type == "users")
    {
        handleListUsers();
    }
    else if (type == "quit")
    {
        handleQuit();
    }
    else if (type == "topic")
    {
        handleTopic(args);
    }
    else if (type == "clear")
    {
        handleClear();
    }
    else if (type == "op")
    {
        handleOp(args);
    }
    else if (type == "kick")
    {
        handleKick(args);
    }
#endif
    else if (type == "party")
    {
        handleParty(args);
    }
    else if (type == "me")
    {
        handleMe(args);
    }
    else if (type == "record")
    {
        handleRecord(args);
    }
    else if (type == "toggle")
    {
        handleToggle(args);
    }
    else if (type == "present")
    {
        handlePresent(args);
    }
    else
    {
        chatWindow->chatLog("Unknown command");
    }
}

void CommandHandler::handleAnnounce(const std::string &args)
{
#ifdef TMWSERV_SUPPORT
    Net::ChatServer::announce(args);
#else
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(0x0099);
    outMsg.writeInt16(args.length() + 4);
    outMsg.writeString(args, args.length());
#endif
}

void CommandHandler::handleHelp(const std::string &args)
{
    if (args == "")
    {
        chatWindow->chatLog(_("-- Help --"));
        chatWindow->chatLog(_("/help > Display this help."));

        chatWindow->chatLog(_("/where > Display map name"));
        chatWindow->chatLog(_("/who > Display number of online users"));
        chatWindow->chatLog(_("/me > Tell something about yourself"));

        chatWindow->chatLog(_("/msg > Send a private message to a user"));
        chatWindow->chatLog(_("/whisper > Alias of msg"));
        chatWindow->chatLog(_("/w > Alias of msg"));

#ifdef TMWSERV_SUPPORT
        chatWindow->chatLog(_("/list > Display all public channels"));
        chatWindow->chatLog(_("/users > Lists the users in the current channel"));
        chatWindow->chatLog(_("/join > Join or create a channel"));
        chatWindow->chatLog(_("/topic > Set the topic of the current channel"));
        chatWindow->chatLog(_("/quit > Leave a channel"));
        chatWindow->chatLog(_("/clear > Clears this window"));
        chatWindow->chatLog(_("/op > Make a user a channel operator"));
        chatWindow->chatLog(_("/kick > Kick a user from the channel"));

        chatWindow->chatLog(_("/party > Invite a user to party"));
#else
        chatWindow->chatLog(_("/party > Party-related commands"));
#endif

        chatWindow->chatLog(_("/record > Start recording the chat to an external file"));
        chatWindow->chatLog(_("/toggle > Determine whether <return> toggles the chat log"));
        chatWindow->chatLog(_("/present > Get list of players present (sent to chat log, if logging)"));

        chatWindow->chatLog(_("/announce > Global announcement (GM only)"));

        chatWindow->chatLog(_("For more information, type /help <command>"));
    }
    else if (args == "announce")
    {
        chatWindow->chatLog(_("Command: /announce <msg>"));
        chatWindow->chatLog(_("*** only available to a GM ***"));
        chatWindow->chatLog(_("This command sends the message <msg> to "
                            "all players currently online."));
    }
    else if (args == "clear")
    {
        chatWindow->chatLog(_("Command: /clear"));
        chatWindow->chatLog(_("This command clears the chat log of previous chat."));
    }
    else if (args == "help")
    {
        chatWindow->chatLog(_("Command: /help"));
        chatWindow->chatLog(_("This command displays a list of all commands available."));
        chatWindow->chatLog(_("Command: /help <command>"));
        chatWindow->chatLog(_("This command displays help on <command>."));
    }
    else if (args == "join")
    {
        chatWindow->chatLog(_("Command: /join <channel>"));
        chatWindow->chatLog(_("This command makes you enter <channel>."));
        chatWindow->chatLog(_("If <channel> doesn't exist, it's created."));
    }
    else if (args == "kick")
    {
        chatWindow->chatLog(_("Command: /kick <nick>"));
        chatWindow->chatLog(_("This command makes <nick> leave the channel."));
        chatWindow->chatLog(_("If the <nick> has spaces in it, enclose it in "
                            "double quotes (\")."));
    }
    else if (args == "list")
    {
        chatWindow->chatLog(_("Command: /list"));
        chatWindow->chatLog(_("This command shows a list of all channels."));
    }
    else if (args == "me")
    {
        chatWindow->chatLog(_("Command: /me <message>"));
        chatWindow->chatLog(_("This command tell others you are (doing) <msg>."));
    }
    else if (args == "msg" || args == "whisper" || args == "w")
    {
        chatWindow->chatLog(_("Command: /msg <nick> <message>"));
        chatWindow->chatLog(_("Command: /whisper <nick> <message>"));
        chatWindow->chatLog(_("Command: /w <nick> <message>"));
        chatWindow->chatLog(_("This command sends the text <message> to <nick>."));
        chatWindow->chatLog(_("If the <nick> has spaces in it, enclose it in "
                            "double quotes (\")."));
    }
    else if (args == "op")
    {
        chatWindow->chatLog(_("Command: /op <nick>"));
        chatWindow->chatLog(_("This command makes <nick> a channel operator."));
        chatWindow->chatLog(_("If the <nick> has spaces in it, enclose it in "
                            "double quotes (\")."));
        chatWindow->chatLog(_("Channel operators can kick and op other users "
                            "from the channel."));
    }
#ifdef TMWSERV_SUPPORT
    else if (args == "party")
    {
        chatWindow->chatLog(_("Command: /party <nick>"));
        chatWindow->chatLog(_("This command invites <nick> to party with you."));
        chatWindow->chatLog(_("If the <nick> has spaces in it, enclose it in "
                            "double quotes (\")."));
#else
    else if (args.substr(0, 5) == "party")
    {
        playerParty->help(args);
#endif
    }
    else if (args == "present")
    {
        chatWindow->chatLog(_("Command: /present"));
        chatWindow->chatLog(_("This command gets a list of players within hearing and "
                  "sends it to either the record log if recording, or the chat "
                  "log otherwise."));
    }
    else if (args == "quit")
    {
        chatWindow->chatLog(_("Command: /quit"));
        chatWindow->chatLog(_("This command leaves the current channel."));
        chatWindow->chatLog(_("If you're the last person in the channel, it will be deleted."));
    }
    else if (args == "record")
    {
        chatWindow->chatLog(_("Command: /record <filename>"));
        chatWindow->chatLog(_("This command starts recording the chat log to the file "
                  "<filename>."));
        chatWindow->chatLog(_("Command: /record"));
        chatWindow->chatLog(_("This command finishes a recording session."));
    }
    else if (args == "toggle")
    {
        chatWindow->chatLog(_("Command: /toggle <state>"));
        chatWindow->chatLog(_("This command sets whether the return key should toggle the "
                  "chat log, or whether the chat log turns off automatically."));
        chatWindow->chatLog(_("<state> can be one of \"1\", \"yes\", \"true\" to "
                  "turn the toggle on, or \"0\", \"no\", \"false\" to turn the "
                  "toggle off."));
        chatWindow->chatLog(_("Command: /toggle"));
        chatWindow->chatLog(_("This command displays the return toggle status."));
    }
    else if (args == "topic")
    {
        chatWindow->chatLog(_("Command: /topic <message>"));
        chatWindow->chatLog(_("This command sets the topic to <message>."));
    }
    else if (args == "users")
    {
        chatWindow->chatLog(_("Command: /users <channel>"));
        chatWindow->chatLog(_("This command shows the users in <channel>."));
    }
    else if (args == "where")
    {
        chatWindow->chatLog(_("Command: /where"));
        chatWindow->chatLog(_("This command displays the name of the current map."));
    }
    else if (args == "who")
    {
        chatWindow->chatLog(_("Command: /who"));
        chatWindow->chatLog(_("This command displays the number of players currently "
                            "online."));
    }
    else
    {
        chatWindow->chatLog(_("Unknown command."));
        chatWindow->chatLog(_("Type /help for a list of commands."));
    }
}

void CommandHandler::handleWhere()
{
    // TODO: add position
    chatWindow->chatLog(map_path, BY_SERVER);
}

void CommandHandler::handleWho()
{
#ifdef TMWSERV_SUPPORT
    //TODO
#else
    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(0x00c1);
#endif
}

void CommandHandler::handleMsg(const std::string &args)
{
#ifdef TMWSERV_SUPPORT
    std::string::size_type pos = args.find(' ');
    std::string recipient(args, 0, pos);
    std::string text(args, pos+1);
    Net::ChatServer::privMsg(recipient, text);
#else
    std::string recvnick = "";
    std::string msg = "";

    if (args.substr(0, 1) == "\"")
    {
        const std::string::size_type pos = args.find('"', 1);
        if (pos != std::string::npos)
        {
            recvnick = args.substr(1, pos - 1);
            msg = args.substr(pos + 2, args.length());
        }
    }
    else
    {
        const std::string::size_type pos = msg.find(" ");
        if (pos != std::string::npos)
        {
            recvnick = args.substr(0, pos);
            msg = args.substr(pos + 1, args.length());
        }
    }

    trim(msg);

    std::string playerName = player_node->getName();
    std::string tempNick = recvnick;

    toLower(playerName);
    toLower(tempNick);

    if (tempNick.compare(playerName) == 0 || args.empty())
        return;

    MessageOut outMsg(mNetwork);
    outMsg.writeInt16(CMSG_CHAT_WHISPER);
    outMsg.writeInt16(msg.length() + 28);
    outMsg.writeString(recvnick, 24);
    outMsg.writeString(msg, msg.length());

    chatWindow->chatLog(strprintf(_("Whispering to %s: %s"),
                        recvnick.c_str(), msg.c_str()),
                        BY_PLAYER);
#endif
}

void CommandHandler::handleClear()
{
    chatWindow->clearTab(chatWindow->getFocused());
}

#ifdef TMWSERV_SUPPORT

void CommandHandler::handleJoin(const std::string &args)
{
    std::string::size_type pos = args.find(' ');
    std::string name(args, 0, pos);
    std::string password(args, pos+1);
    chatWindow->chatLog("Requesting to join channel " + name);
    Net::ChatServer::enterChannel(name, password);
}

void CommandHandler::handleListChannels()
{
    Net::ChatServer::getChannelList();
}

void CommandHandler::handleListUsers()
{
    Net::ChatServer::getUserList(chatWindow->getFocused());
}

void CommandHandler::handleTopic(const std::string &args)
{
    if (Channel *channel = channelManager->findByName(chatWindow->getFocused()))
    {
        Net::ChatServer::setChannelTopic(channel->getId(), args);
    }
    else
    {
        chatWindow->chatLog("Unable to set this channel's topic", BY_CHANNEL);
    }
}

void CommandHandler::handleQuit()
{
    if (Channel *channel = channelManager->findByName(chatWindow->getFocused()))
    {
        Net::ChatServer::quitChannel(channel->getId());
    }
    else
    {
        chatWindow->chatLog("Unable to quit this channel", BY_CHANNEL);
    }
}

void CommandHandler::handleOp(const std::string &args)
{
    if (Channel *channel = channelManager->findByName(chatWindow->getFocused()))
    {
        // set the user mode 'o' to op a user
        if (args != "")
        {
            Net::ChatServer::setUserMode(channel->getId(), args, 'o');
        }
    }
    else
    {
        chatWindow->chatLog("Unable to set this user's mode", BY_CHANNEL);
    }
}

void CommandHandler::handleKick(const std::string &args)
{
    if (Channel *channel = channelManager->findByName(chatWindow->getFocused()))
    {
        if (args != "")
        {
            Net::ChatServer::kickUser(channel->getId(), args);
        }
    }
    else
    {
        chatWindow->chatLog("Unable to kick user", BY_CHANNEL);
    }
}

#endif

void CommandHandler::handleParty(const std::string &args)
{
#ifdef TMWSERV_SUPPORT
    if (args != "")
    {
        player_node->inviteToParty(args);
    }
#else
    if (args.empty())
    {
        chatWindow->chatLog(_("Unknown party command... Type \"/help\" party for more "
                "information."), BY_SERVER);
        return;
    }

    const std::string::size_type space = args.find(" ");
    std::string command;
    std::string rest;

    if (space == std::string::npos)
    {
        command = args;
    }
    else
    {
        command = args.substr(0, space);
        rest = args.substr(space + 1, args.length());
    }

    if (command == "prefix")
    {
        if (rest.empty())
        {
            char temp[2] = ".";
            *temp = chatWindow->getPartyPrefix();
            chatWindow->chatLog(_("The current party prefix is ") + std::string(temp));
        }
        else if (rest.length() != 1)
        {
            chatWindow->chatLog(_("Party prefix must be one character long."));
        }
        else
        {
            if (rest == "/")
            {
                chatWindow->chatLog(_("Cannot use a '/' as the prefix."));
            }
            else
            {
                chatWindow->setPartyPrefix(rest.at(0));
                chatWindow->chatLog(_("Changing prefix to ") + rest);
            }
        }
    }
    else
        playerParty->respond(command, rest);
#endif
}

void CommandHandler::handleMe(const std::string &args)
{
    std::string action = strprintf("*%s*", args.c_str());
    chatWindow->chatSend(action);
    //std::stringstream actionStr;
    //actionStr << "*" << args << "*";
    //chatWindow->chatSend(actionStr.str());
}

void CommandHandler::handleRecord(const std::string &args)
{
    chatWindow->setRecordingFile(args);
}

void CommandHandler::handleToggle(const std::string &args)
{
    if (args.empty())
    {
        chatWindow->chatLog(chatWindow->getReturnTogglesChat() ?
                _("Return toggles chat.") : _("Message closes chat."));
        return;
    }

    std::string opt = args.substr(0, 1);

    if (opt == "1" ||
        opt == "y" || opt == "Y" ||
        opt == "t" || opt == "T")
    {
        chatWindow->chatLog(_("Return now toggles chat."));
        chatWindow->setReturnTogglesChat(true);
        return;
    }
    else if (opt == "0" ||
             opt == "n" || opt == "N" ||
             opt == "f" || opt == "F")
    {
        chatWindow->chatLog(_("Message now closes chat."));
        chatWindow->setReturnTogglesChat(false);
        return;
    }
    else
        chatWindow->chatLog(_("Options to /toggle are \"yes\", \"no\", \"true\", "
                    "\"false\", \"1\", \"0\"."));
}

void CommandHandler::handlePresent(const std::string &args)
{
    chatWindow->doPresent();
}