summaryrefslogtreecommitdiff
path: root/src/chathandler.cpp
blob: 082a1b6a85383f60bfef513fb95b060f2100cc67 (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
/*
 *  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/stringfilter.h"
#include "chatchannelmanager.h"

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 (stringFilter->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 AROUND_AREA_IN_TILES tiles square wide.
                            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 (stringFilter->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 (stringFilter->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;

        // Channels handling
        // =================
        case CMSG_REGISTER_CHANNEL:
            {
                MessageOut result;
                result.writeShort(SMSG_REGISTER_CHANNEL_RESPONSE);
                // 0 public, 1 private
                char channelType = message.readByte();
                if (!channelType)
                {
                    if (computer.getAccount()->getLevel() != (AccountLevels)AL_ADMIN &&
                        computer.getAccount()->getLevel() != (AccountLevels)AL_GM)
                    {
                        result.writeByte(CHATCNL_CREATE_UNSUFFICIENT_RIGHTS);
                        computer.send(result.getPacket());
                        return;
                    }
                }
                std::string channelName = message.readString();
                std::string channelAnnouncement = message.readString();
                std::string channelPassword = message.readString();
                // Checking datas
                // Seeking double-quotes in strings
                if (channelName == "" || channelName.length() > MAX_CHANNEL_NAME ||
                stringFilter->findDoubleQuotes(channelName))
                {
                        result.writeByte(CHATCNL_CREATE_INVALID_NAME);
                        computer.send(result.getPacket());
                        return;
                }
                if (channelAnnouncement.length() > MAX_CHANNEL_ANNOUNCEMENT ||
                    stringFilter->findDoubleQuotes(channelAnnouncement))
                {
                        result.writeByte(CHATCNL_CREATE_INVALID_ANNOUNCEMENT);
                        computer.send(result.getPacket());
                        return;
                }
                if (channelPassword.length() > MAX_CHANNEL_PASSWORD ||
                stringFilter->findDoubleQuotes(channelPassword))
                {
                        result.writeByte(CHATCNL_CREATE_INVALID_PASSWORD);
                        computer.send(result.getPacket());
                        return;
                }

                // If it's slang's free.
                if (stringFilter->filterContent(channelName) &&
                stringFilter->filterContent(channelAnnouncement))
                {
                    // We attempt to create a new channel
                    short channelId;
                    if (channelType)
                        channelId = chatChannelManager->registerPrivateChannel(channelName,
                                                                            channelAnnouncement,
                                                                            channelPassword);
                    else
                        channelId = chatChannelManager->registerPublicChannel(channelName,
                                                                            channelAnnouncement,
                                                                            channelPassword);
                    if (channelId != 0)
                    {
                        // We add the player as admin of this channel as he created it.
                        // The user registering a private channel is the only one to be able
                        // to update the password and the announcement in it and also to remove it.
                        chatChannelManager->addUserInChannel(computer.getCharacter(), channelId);

                        result.writeByte(CHATCNL_CREATE_OK);
                        result.writeShort(channelId);
                        computer.send(result.getPacket());
                        return;
                    }
                    else
                    {
                        result.writeByte(CHATCNL_CREATE_UNKNOWN);
                        computer.send(result.getPacket());
                        return;
                    }
                }
                else
                {
                    warnPlayerAboutBadWords(computer);
                }
            }
            break;

        case CMSG_UNREGISTER_CHANNEL:
            {
                MessageOut result;
                result.writeShort(SMSG_UNREGISTER_CHANNEL_RESPONSE);

                short channelId = message.readShort();
                if (channelId != 0)
                {
                    // Public channels
                    if (channelId < (signed)MAX_PUBLIC_CHANNELS_RANGE)
                    {
                        if (computer.getAccount()->getLevel() != (AccountLevels)AL_ADMIN &&
                        computer.getAccount()->getLevel() != (AccountLevels)AL_GM)
                        {
                            result.writeByte(CHATCNL_DEL_UNSUFFICIENT_RIGHTS);
                        }
                        else
                        { // if the channel actually exist
                            if (chatChannelManager->isChannelRegistered(channelId))
                            {
                                // Make every user quit the channel
                                connectionHandler->makeUsersLeaveChannel(channelId);
                                if (chatChannelManager->removeChannel(channelId))
                                    result.writeByte(CHATCNL_DEL_OK);
                                else
                                    result.writeByte(CHATCNL_DEL_UNKNOWN);
                            }
                            else
                            { // Couldn't remove channel because it doesn't exist
                                result.writeByte(CHATCNL_DEL_INVALID_ID);
                            }
                        }
                    }
                    else if (channelId < (signed)MAX_PRIVATE_CHANNELS_RANGE)
                    { // Private channels
                        if (chatChannelManager->isChannelRegistered(channelId))
                        {
                            // We first see if the user is the admin (first user) of the channel
                            std::vector<tmwserv::BeingPtr> userList =
                                chatChannelManager->getUserListInChannel(channelId);
                            std::vector<tmwserv::BeingPtr>::const_iterator i = userList.begin();
                            // if it's actually the private channel's admin
                            if ( (*i).get() == computer.getCharacter().get() )
                            {
                                // Make every user quit the channel
                                connectionHandler->makeUsersLeaveChannel(channelId);
                                if (chatChannelManager->removeChannel(channelId))
                                    result.writeByte(CHATCNL_DEL_OK);
                                else
                                    result.writeByte(CHATCNL_DEL_UNKNOWN);
                            }
                            else
                            {
                                result.writeByte(CHATCNL_DEL_UNSUFFICIENT_RIGHTS);
                            }
                        }
                        else
                        {
                            result.writeByte(CHATCNL_DEL_INVALID_ID);
                        }
                    }
                    else
                    { // Id too high or too low
                        result.writeByte(CHATCNL_DEL_INVALID_ID);
                    }
                }
                else
                {
                    result.writeByte(CHATCNL_DEL_INVALID_ID);
                }
                computer.send(result.getPacket());
            } break;



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

void ChatHandler::handleCommand(NetComputer &computer, const std::string& command)
{
    LOG_INFO("Chat: Received 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, const 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, const 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, const std::string& playerName, const 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, const std::string& text)
{
    MessageOut result;
    LOG_INFO( computer.getCharacter()->getName() << " says in channel " << channel
            << ": " << text, 2)
    // 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);
    connectionHandler->sendInChannel(channel, result);

}