summaryrefslogtreecommitdiff
path: root/src/chathandler.cpp
blob: e2f17c8930782a5d8a8b23b236e884dc2e0cdf4f (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
/*
 *  The Mana World Server
 *  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 "chathandler.h"
#include "state.h"
#include "being.h"
#include "defines.h"
#include "utils/logger.h"
#include "utils/slangsfilter.h"

ChatHandler::ChatHandler()
{
    // TODO: Implement loading public chat channels from db.
    // That require adding a table for that.
}

void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
    // If not logged in...
    if (computer.getAccount().get() == NULL)
    {
        LOG_INFO("Not logged in, can't chat...", 2)
        MessageOut result;
        result.writeShort(SMSG_CHAT);
        result.writeByte(CHAT_NOLOGIN);
        computer.send(result.getPacket());
        return;
    }
    else
    {
        // If no character selected yet...
        if (computer.getCharacter().get() == NULL)
        {
            MessageOut result;
            result.writeShort(SMSG_CHAT);
            result.writeByte(CHAT_NO_CHARACTER_SELECTED);
            computer.send(result.getPacket());
            LOG_INFO("No character selected. Can't chat...", 2)
            return; // character not selected
        }
    }

    switch (message.getId())
    {
        case CMSG_SAY:
            {
                // chat to people around area
                std::string text = message.readString();
                // If it's slang clean,
                if (slangsFilter->filterContent(text))
                {
                    short channel = message.readShort();
                    LOG_INFO("Say: (Channel " << channel << "): " << text, 2)
                    if ( channel == 0 ) // Let's say that is the default channel for now.
                    {
                        if ( text.substr(0, 1) == "@" || text.substr(0, 1) == "#" || text.substr(0, 1) == "/" )
                        {
                            // The message is a command. Deal with it.
                            handleCommand(computer, text);
                        }
                        else
                        {
                            // The default channel (0) is when the character speaks
                            // to the characters around him in the map.
                            // We, then, look for every characters around him and
                            // send the message to them.
                            // By 'around', let's say 10 tiles square wide for now.
                            sayAround(computer, text);
                        }
                    }
                    else
                    {
                        // We send the message to the players registered in the channel.
                        sayInChannel(computer, channel, text);
                    }
                }
                else
                {
                    warnPlayerAboutBadWords(computer);
                }
            }
            break;

        case CMSG_ANNOUNCE:
            {
                std::string text = message.readString();
                // If it's slang's free.
                if (slangsFilter->filterContent(text))
                {
                    // We send the message to every players in the default channel
                    // as it is an annouce.
                    announce(computer, text);
                }
                else
                {
                    warnPlayerAboutBadWords(computer);
                }
            }
            break;

        case CMSG_PRIVMSG:
            {
                std::string user = message.readString();
                std::string text = message.readString();
                if (slangsFilter->filterContent(text))
                {
                    // We seek the player to whom the message is told
                    // and send it to her/him.
                    sayToPlayer(computer, user, text);
                }
                else
                {
                    warnPlayerAboutBadWords(computer);
                }
            } break;

        default:
            LOG_INFO("Chat: Invalid message type", 2)
            break;
    }
}

void ChatHandler::handleCommand(NetComputer &computer, std::string command)
{
    LOG_INFO("Chat: Recieved unhandled command: " << command, 2)
    MessageOut result;
    result.writeShort(SMSG_CHAT);
    result.writeByte(CHATCMD_UNHANDLED_COMMAND);
    computer.send(result.getPacket());
}

void ChatHandler::warnPlayerAboutBadWords(NetComputer &computer)
{
    // We could later count if the player is really often unpolite.
    MessageOut result;
    result.writeShort(SMSG_CHAT);
    result.writeByte(CHAT_USING_BAD_WORDS); // The Channel
    computer.send(result.getPacket());

    LOG_INFO(computer.getCharacter()->getName() << " says bad words.", 2)
}

void ChatHandler::announce(NetComputer &computer, std::string text)
{
    MessageOut result;
    if ( computer.getAccount()->getLevel() == (AccountLevels)AL_ADMIN ||
    computer.getAccount()->getLevel() == (AccountLevels)AL_GM )
    {
        LOG_INFO("ANNOUNCE: " << text, 0)
        // Send it to every beings.
        result.writeShort(SMSG_ANNOUNCEMENT);
        result.writeString(text);
        connectionHandler->sendToEveryone(result);
    }
    else
    {
        result.writeShort(SMSG_CHAT);
        result.writeByte(CHATCMD_UNSUFFICIENT_RIGHTS);
        computer.send(result.getPacket());
        LOG_INFO(computer.getCharacter()->getName() <<
            " couldn't make an announcement due to insufficient rights.", 2)
    }
}

void ChatHandler::sayAround(NetComputer &computer, std::string text)
{
    MessageOut result;
    LOG_INFO( computer.getCharacter()->getName() << " says: " << text, 2)
    // Send it to every beings around
    result.writeShort(SMSG_CHAT);
    result.writeShort(0); // The default channel
    std::string say = computer.getCharacter()->getName();
    say += ": ";
    say += text;
    result.writeString(say);
    connectionHandler->sendAround(computer.getCharacter(), result);
}

void ChatHandler::sayToPlayer(NetComputer &computer, std::string playerName, std::string text)
{
    MessageOut result;
    LOG_INFO( computer.getCharacter()->getName() << " says to " << playerName
            << ": " << text, 2)
    // Send it to the being if the being exists
    result.writeShort(SMSG_PRIVMSG);
    std::string say = computer.getCharacter()->getName();
    say += ": ";
    say += text;
    result.writeString(say);
    connectionHandler->sendTo(playerName, result);
}

void ChatHandler::sayInChannel(NetComputer &computer, short channel, std::string text)
{
    MessageOut result;
    LOG_INFO( computer.getCharacter()->getName() << " says in channel " << channel
            << ": " << text, 2)
    // TODO: Send it to every beings in channel
    result.writeShort(SMSG_CHAT);
    result.writeShort(channel);
    std::string say = computer.getCharacter()->getName();
    say += ": ";
    say += text;
    result.writeString(say);

}