summaryrefslogtreecommitdiff
path: root/src/gui/chat.cpp
blob: c15f7968c928a88b77399ef3e7f52f91343a53d7 (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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
 *  The Mana World
 *  Copyright 2004 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
 *
 *  $Id$
 */

#include <algorithm>
#include <sstream>

#include <guichan/focushandler.hpp>

#include "chat.h"

#include "browserbox.h"
#include "chatinput.h"
#include "scrollarea.h"
#include "sdlinput.h"
#include "windowcontainer.h"

#include "widgets/tab.h"
#include "widgets/tabbedarea.h"

#include "../channelmanager.h"
#include "../channel.h"
#include "../configuration.h"
#include "../game.h"
#include "../localplayer.h"

#include "../net/chatserver/chatserver.h"
#include "../net/gameserver/player.h"

#include "../utils/dtor.h"
#include "../utils/trim.h"

ChatWindow::ChatWindow():
    Window("Chat"),
    mTmpVisible(false)
{
    setResizable(true);
    setDefaultSize(0, (windowContainer->getHeight() - 105), 400, 100);

    mChatInput = new ChatInput();
    mChatInput->setActionEventId("chatinput");
    mChatInput->addActionListener(this);

    BrowserBox *textOutput = new BrowserBox(BrowserBox::AUTO_WRAP);
    textOutput->setOpaque(false);
    textOutput->disableLinksAndUserColors();
    textOutput->setMaxRow((int) config.getValue("ChatLogLength", 0));

    ScrollArea *scrollArea = new ScrollArea(textOutput);
    scrollArea->setPosition(
            scrollArea->getFrameSize(), scrollArea->getFrameSize());
    scrollArea->setScrollPolicy(
            gcn::ScrollArea::SHOW_NEVER, gcn::ScrollArea::SHOW_ALWAYS);
    scrollArea->setOpaque(false);

    mChatTabs = new TabbedArea();
    mChatTabs->addTab("General", scrollArea);
    mChatTabs->setPosition(mChatTabs->getFrameSize(),
                           mChatTabs->getFrameSize());

    mChannels.insert(
            std::make_pair("General", ChatArea(textOutput, scrollArea)));

    add(mChatTabs);
    add(mChatInput);

    // Add key listener to chat input to be able to respond to up/down
    mChatInput->addKeyListener(this);
    mCurHist = mHistory.end();

    loadWindowState("Chat");
}

ChatWindow::~ChatWindow()
{
    delete mChatInput;
    delete mChatTabs;
}

void ChatWindow::widgetResized(const gcn::Event &event)
{
    Window::widgetResized(event);

    const gcn::Rectangle area = getChildrenArea();

    mChatInput->setPosition(mChatInput->getFrameSize(),
                            area.height - mChatInput->getHeight() -
                                mChatInput->getFrameSize());
    mChatInput->setWidth(area.width - 2 * mChatInput->getFrameSize());

    mChatTabs->setWidth(area.width - 2 * mChatTabs->getFrameSize());
    mChatTabs->setHeight(area.height - 2 * mChatTabs->getFrameSize());

    const std::string &channelName = mChatTabs->getSelectedTab()->getCaption();
    ChannelMap::const_iterator chan = mChannels.find(channelName);
    if (chan != mChannels.end()) {
        ScrollArea *scroll = chan->second.scroll;
        scroll->setWidth(area.width - 2 * scroll->getFrameSize());
        scroll->setHeight(area.height - 2 * scroll->getFrameSize() -
                mChatInput->getHeight() - 5);
        scroll->logic();
    }
}

void
ChatWindow::chatLog(std::string line, int own, const std::string &channelName)
{
    ChannelMap::const_iterator chan = mChannels.find(channelName);
    if (chan == mChannels.end())
        return;

    BrowserBox * const output = chan->second.browser;
    ScrollArea * const scroll = chan->second.scroll;

    // Trim whitespace
    trim(line);

    CHATLOG tmp;
    tmp.own  = own;
    tmp.nick = "";
    tmp.text = line;

    std::string::size_type pos = line.find(" : ");
    if (pos != std::string::npos) {
        tmp.nick = line.substr(0, pos);
        tmp.text = line.substr(pos + 3);
    }

    std::string lineColor = "##0"; // Equiv. to BrowserBox::BLACK
    switch (own) {
        case BY_GM:
            tmp.nick += "Global announcement: ";
            lineColor = "##1"; // Equiv. to BrowserBox::RED
            break;
        case BY_PLAYER:
            tmp.nick += ": ";
            lineColor = "##3"; // Equiv. to BrowserBox::BLUE
            break;
        case BY_OTHER:
            tmp.nick += ": ";
            lineColor = "##0"; // Equiv. to BrowserBox::BLACK
            break;
        case BY_SERVER:
            tmp.nick = "Server: ";
            tmp.text = line;
            lineColor = "##7"; // Equiv. to BrowserBox::PINK
            break;
        case BY_LOGGER:
            tmp.nick = "";
            tmp.text = line;
            lineColor = "##8"; // Equiv. to BrowserBox::GREY
            break;
    }

    // Get the current system time
    time_t t;
    time(&t);

    // Format the time string properly
    std::stringstream timeStr;
    timeStr << "["
            << ((((t / 60) / 60) % 24 < 10) ? "0" : "")
            << (int)(((t / 60) / 60) % 24)
            << ":"
            << (((t / 60) % 60 < 10) ? "0" : "")
            << (int)((t / 60) % 60)
            << "] ";

    line = lineColor + timeStr.str() + tmp.nick + tmp.text;

    // We look if the Vertical Scroll Bar is set at the max before
    // adding a row, otherwise the max will always be a row higher
    // at comparison.
    if (scroll->getVerticalScrollAmount() == scroll->getVerticalMaxScroll())
    {
        output->addRow(line);
        scroll->setVerticalScrollAmount(scroll->getVerticalMaxScroll());
    }
    else
    {
        output->addRow(line);
    }
}

#if 0
void
ChatWindow::chatLog(CHATSKILL act)
{
    chatLog(const_msg(act), BY_SERVER);
}
#endif

void
ChatWindow::action(const gcn::ActionEvent &event)
{
    if (event.getId() == "chatinput")
    {
        std::string message = mChatInput->getText();

        if (!message.empty()) {
            // If message different from previous, put it in the history
            if (mHistory.empty() || message != mHistory.back()) {
                mHistory.push_back(message);
            }

            // Reset history iterator
            mCurHist = mHistory.end();

            // Send the message to the server
            gcn::Tab *tab = mChatTabs->getSelectedTab();
            chatSend(player_node->getName(), message, tab->getCaption());

            // Clear the text from the chat input
            mChatInput->setText("");
        }

        // Remove focus and hide input
        mFocusHandler->focusNone();

        // If the chatWindow is shown up because you want to send a message
        // It should hide now
        if (mTmpVisible) {
            setVisible(false);
        }
    }
}

void
ChatWindow::requestChatFocus()
{
    // Make sure chatWindow is visible
    if (!isVisible())
    {
        setVisible(true);

        /*
         * This is used to hide chatWindow after sending the message. There is
         * a trick here, because setVisible will set mTmpVisible to false, you
         * have to put this sentence *after* setVisible, not before it
         */
        mTmpVisible = true;
    }

    // Give focus to the chat input
    mChatInput->setVisible(true);
    mChatInput->requestFocus();
}

bool
ChatWindow::isFocused()
{
    return mChatInput->isFocused();
}

void ChatWindow::chatSend(std::string const &nick, std::string const &msg,
                          std::string const &channelName)
{
    /* Some messages are managed client side, while others
     * require server handling by proper packet. Probably
     * those if elses should be replaced by protocol calls */

    if (msg.empty()) return;

    // Prepare ordinary message
    if (msg[0] != '/') {
        gcn::Tab *tab = mChatTabs->getSelectedTab();
        if (tab->getCaption() == "General")
        {
            Net::GameServer::Player::say(msg);
        }
        else
        {
            Channel *channel = channelManager->findByName(channelName);
            if (channel)
            {
                int channelId = channel->getId();
                Net::ChatServer::chat(channelId, msg);
            }
        }
        return;
    }

    std::string::size_type pos = msg.find(' ', 1);
    std::string command(msg, 1, pos == std::string::npos ? pos : pos - 1);
    std::string arg(msg, pos == std::string::npos ? msg.size() : pos + 1);

    if (command == "announce")
    {
        Net::ChatServer::announce(arg);
    }
    else if (command == "help")
    {
        chatLog("-- Help --", BY_SERVER, channelName);
        chatLog("/help > Display this help.", BY_SERVER, channelName);
        chatLog("/announce > Global announcement (GM only)", BY_SERVER, channelName);
        chatLog("/where > Display map name", BY_SERVER, channelName);
        chatLog("/who > Display number of online users", BY_SERVER, channelName);
        chatLog("/msg > Send a private message to a user", BY_SERVER, channelName);
        chatLog("/list > Display all public channels", BY_SERVER, channelName);
        chatLog("/register > Register a new channel", BY_SERVER, channelName);
        chatLog("/join > Join an already registered channel", BY_SERVER, channelName);
        chatLog("/quit > Leave a channel", BY_SERVER, channelName);
        chatLog("/admin > Send a command to the server (GM only)", BY_SERVER, channelName);
        chatLog("/clear > Clears this window", BY_SERVER);
    }
    else if (command == "where")
    {
        chatLog(map_path, BY_SERVER);
    }
    else if (command == "who")
    {
        // XXX Convert for new server
        /*
        MessageOut outMsg(0x00c1);
        */
    }
    else if (command == "msg")
    {
        std::string::size_type pos = arg.find(' ', 1);
        std::string recipient(arg, 0, pos-1);
        std::string text(arg, pos+1);
        chatLog("* " + text, BY_SERVER);
        Net::ChatServer::privMsg(recipient, text);
    }
    else if (command == "register")
    {
        // TODO: Parse the announcement and password
        chatLog("Requesting to register channel " + arg, BY_SERVER);
        Net::ChatServer::registerChannel(arg, "", "");
    }
    else if (command == "join")
    {
        //TODO: have passwords too
        chatLog("Requesting to join channel " + arg, BY_SERVER);
        enterChannel(arg, "None");
    }
    else if (command == "list")
    {
        Net::ChatServer::getChannelList();
    }
    else if (command == "quit")
    {
        if (Channel *channel = channelManager->findByName(channelName))
        {
            Net::ChatServer::quitChannel(channel->getId());
        }
        else
        {
            chatLog("Unable to quit this channel", BY_SERVER);
        }
    }
    else if (command == "admin")
    {
        Net::GameServer::Player::say("/" + arg);
    }
    else if (command == "clear")
    {
        ChannelMap::const_iterator chan = mChannels.find(channelName);
        if (chan != mChannels.end())
            chan->second.browser->clearRows();
    }
    else
    {
        chatLog("Unknown command", BY_SERVER, channelName);
    }
}

#if 0
std::string
ChatWindow::const_msg(CHATSKILL act)
{
    std::string msg;
    if (act.success == SKILL_FAILED && act.skill == SKILL_BASIC) {
        switch (act.bskill) {
            case BSKILL_TRADE :
                msg = "Trade failed!";
                break;
            case BSKILL_EMOTE :
                msg = "Emote failed!";
                break;
            case BSKILL_SIT :
                msg = "Sit failed!";
                break;
            case BSKILL_CREATECHAT :
                msg = "Chat creating failed!";
                break;
            case BSKILL_JOINPARTY :
                msg = "Could not join party!";
                break;
            case BSKILL_SHOUT :
                msg = "Cannot shout!";
                break;
        }

        switch (act.reason) {
            case RFAIL_SKILLDEP :
                msg += " You have not yet reached a high enough lvl!";
                break;
            case RFAIL_INSUFHP :
                msg += " Insufficient HP!";
                break;
            case RFAIL_INSUFSP :
                msg += " Insufficient SP!";
                break;
            case RFAIL_NOMEMO :
                msg += " You have no memos!";
                break;
            case RFAIL_SKILLDELAY :
                msg += " You cannot do that right now!";
                break;
            case RFAIL_ZENY :
                msg += " Seems you need more Zeny... ;-)";
                break;
            case RFAIL_WEAPON :
                msg += " You cannot use this skill with that kind of weapon!";
                break;
            case RFAIL_REDGEM :
                msg += " You need another red gem!";
                break;
            case RFAIL_BLUEGEM :
                msg += " You need another blue gem!";
                break;
            case RFAIL_OVERWEIGHT :
                msg += " You're carrying to much to do this!";
                break;
            default :
                msg += " Huh? What's that?";
                break;
        }
    } else {
        switch(act.skill) {
            case SKILL_WARP :
                msg = "Warp failed...";
                break;
            case SKILL_STEAL :
                msg = "Could not steal anything...";
                break;
            case SKILL_ENVENOM :
                msg = "Poison had no effect...";
                break;
        }
    }

    return msg;
}
#endif

void
ChatWindow::addChannel(short channelId, const std::string &channelName)
{
    Channel *channel = new Channel(channelId);
    channel->setName(channelName);
    channelManager->addChannel(channel);
}

void
ChatWindow::removeChannel(short channelId)
{
    removeChannel(channelManager->findById(channelId));
}

void
ChatWindow::removeChannel(const std::string &channelName)
{
    removeChannel(channelManager->findByName(channelName));
}

void
ChatWindow::removeChannel(Channel *channel)
{
    if (channel)
    {
        Tab *tab = mChatTabs->getTab(channel->getName());
        if (!tab)
            return;
        mChatTabs->removeTab(tab);
        mChannels.erase(channel->getName());
        channelManager->removeChannel(channel);

        logic();
    }
}

void
ChatWindow::createNewChannelTab(const std::string &channelName)
{
    // Create new channel
    BrowserBox *textOutput = new BrowserBox(BrowserBox::AUTO_WRAP);
    textOutput->setOpaque(false);
    textOutput->disableLinksAndUserColors();
    textOutput->setMaxRow((int) config.getValue("ChatLogLength", 0));
    ScrollArea *scrollArea = new ScrollArea(textOutput);
    scrollArea->setPosition(scrollArea->getFrameSize(), scrollArea->getFrameSize());
    scrollArea->setScrollPolicy(gcn::ScrollArea::SHOW_NEVER, gcn::ScrollArea::SHOW_ALWAYS);
    scrollArea->setOpaque(false);
    scrollArea->setWidth(getChildrenArea().width - 2 * scrollArea->getFrameSize());
    scrollArea->setHeight(getChildrenArea().height - 2 * scrollArea->getFrameSize() -
                mChatInput->getHeight() - 5);
    scrollArea->logic();
    textOutput->setWidth(scrollArea->getChildrenArea().width);
    textOutput->setHeight(scrollArea->getChildrenArea().height);

    // Add channel to the tabbed area
    mChatTabs->addTab(channelName, scrollArea);
    mChannels.insert(
            std::make_pair(channelName, ChatArea(textOutput, scrollArea)));

    // Ask for channel users
    Net::ChatServer::getUserList(channelName);

    // Update UI
    logic();
}

void
ChatWindow::enterChannel(const std::string &channel, const std::string &password)
{
    Net::ChatServer::enterChannel(channel, password);
}

void
ChatWindow::sendToChannel(short channelId, const std::string &user, const std::string &msg)
{
    Channel *channel = channelManager->findById(channelId);
    if (channel)
    {
        std::string channelName = channel->getName();
        chatLog(user + ": " + msg, user == player_node->getName() ? BY_PLAYER : BY_OTHER, channelName);
    }
}

void
ChatWindow::keyPressed(gcn::KeyEvent &event)
{
    if (event.getKey().getValue() == Key::DOWN &&
            mCurHist != mHistory.end())
    {
        // Move forward through the history
        HistoryIterator prevHist = mCurHist++;
        if (mCurHist != mHistory.end()) {
            mChatInput->setText(*mCurHist);
            mChatInput->setCaretPosition(mChatInput->getText().length());
        }
        else {
            mCurHist = prevHist;
        }
    }
    else if (event.getKey().getValue() == Key::UP &&
            mCurHist != mHistory.begin() && mHistory.size() > 0)
    {
        // Move backward through the history
        mCurHist--;
        mChatInput->setText(*mCurHist);
        mChatInput->setCaretPosition(mChatInput->getText().length());
    }
}

void
ChatWindow::setInputText(std::string input_str)
{
     mChatInput->setText(input_str + " ");
     requestChatFocus();
}

void
ChatWindow::setVisible(bool isVisible)
{
     Window::setVisible(isVisible);

     /*
      * For whatever reason, if setVisible is called, the mTmpVisible effect
      * should be disabled.
      */
     mTmpVisible = false;
}

bool
ChatWindow::tabExists(const std::string &tabName)
{
    Tab *tab = mChatTabs->getTab(tabName);
    if (tab)
        return true;
    return false;
}