summaryrefslogtreecommitdiffstats
path: root/src/chat-server/chathandler.h
blob: 06d725132d3cff010a2728fc0b348e1427322088 (plain)
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
/*
 *  The Mana Server
 *  Copyright (C) 2004-2010  The Mana World Development Team
 *
 *  This file is part of The Mana Server.
 *
 *  The Mana Server 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 Server 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 Server.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef CHATHANDLER_H
#define CHATHANDLER_H

#include <deque>
#include <map>
#include <string>

#include "chat-server/guild.h"

#include "net/connectionhandler.h"

#include "utils/tokencollector.h"

class ChatChannel;
class ChatClient;

/**
 * Manages chat related things like private messaging, chat channel handling
 * as well as guild chat. The only form of chat not handled by this server is
 * local chat, which is handled by the game server.
 *
 * @todo <b>b_lindeijer:</b> Extend this class with handling of team chat once
 *       teams are implemented.
 */
class ChatHandler : public ConnectionHandler
{
    private:
        /**
         * Data needed for initializing a ChatClient.
         */
        struct Pending
        {
            std::string character;
            unsigned char level;
        };

        struct PartyInvite
        {
            PartyInvite(std::string inviterName, std::string inviteeName)
                : mInviter(inviterName)
                , mInvitee(inviteeName)
            {
                const int validTimeframe = 60;
                mExpireTime = time(NULL) + validTimeframe;
            }

            std::string mInviter;
            std::string mInvitee;
            time_t mExpireTime;
        };

        std::map<std::string, ChatClient*> mPlayerMap;
        std::deque<PartyInvite> mInvitations;
        std::map<std::string, int> mNumInvites;

    public:
        ChatHandler();

        /**
         * Start the handler.
         */
        bool startListen(enet_uint16 port, const std::string &host);

        /**
         * Tell a list of users about an event in a chatchannel.
         *
         * @param channel the channel to send the message in, must not be NULL
         * @param info information pertaining to the event
         */
        void warnUsersAboutPlayerEventInChat(ChatChannel *channel,
                                             const std::string &info,
                                             char eventId);

        /**
         * Called by TokenCollector when a client wrongly connected.
         */
        void deletePendingClient(ChatClient *);

        /**
         * Called by TokenCollector when a client failed to connect.
         */
        void deletePendingConnect(Pending *);

        /**
         * Called by TokenCollector when a client succesfully connected.
         */
        void tokenMatched(ChatClient *, Pending *);

        /**
         * Send information about a change in the guild list to guild members.
         */
        void sendGuildListUpdate(Guild *guild,
                                 const std::string &characterName,
                                 char eventId);

        void handlePartyInvite(MessageIn &msg);

        /**
         * Sends an announce to all connected clients.
         */
        void handleAnnounce(const std::string &message, int senderId,
                            const std::string &senderName);

        /**
         * Returns ChatClient from the Player Map
         * @param The name of the character
         * @return The Chat Client
         */
        ChatClient *getClient(const std::string &name) const;

    protected:
        /**
         * Process chat related messages.
         */
        void processMessage(NetComputer *computer, MessageIn &message);

        /**
         * Returns a ChatClient instance.
         */
        NetComputer *computerConnected(ENetPeer *);

        /**
         * Cleans up after the disconnected client.
         */
        void computerDisconnected(NetComputer *);

        /**
         * Send messages for each guild the character belongs to.
         */
        void sendGuildRejoin(ChatClient &computer);

        /**
         * Send chat and guild info to chat client, so that they can join the
         * correct channels.
         */
        void sendGuildEnterChannel(const MessageOut &msg,
                                   const std::string &name);

        void sendGuildInvite(const std::string &invitedName,
                             const std::string &inviterName,
                             const std::string &guildName);

    private:
        // TODO: Unused
        void handleCommand(ChatClient &client, const std::string &command);

        void handleChatMessage(ChatClient &client, MessageIn &msg);
        void handlePrivMsgMessage(ChatClient &client, MessageIn &msg);
        void handleWhoMessage(ChatClient &client);

        void handleEnterChannelMessage(ChatClient &client, MessageIn &msg);
        void handleModeChangeMessage(ChatClient &client, MessageIn &msg);
        void handleKickUserMessage(ChatClient &client, MessageIn &msg);
        void handleQuitChannelMessage(ChatClient &client, MessageIn &msg);

        void handleListChannelsMessage(ChatClient &client, MessageIn &msg);
        void handleListChannelUsersMessage(ChatClient &client, MessageIn &msg);

        void handleTopicChange(ChatClient &client, MessageIn &msg);

        void handleDisconnectMessage(ChatClient &client, MessageIn &msg);

        void handleGuildCreate(ChatClient &client, MessageIn &msg);
        void handleGuildInvite(ChatClient &client, MessageIn &msg);
        void handleGuildAcceptInvite(ChatClient &client, MessageIn &msg);
        void handleGuildGetMembers(ChatClient &client, MessageIn &msg);
        void handleGuildMemberLevelChange(ChatClient &client, MessageIn &msg);
        void removeCharacterFormGuild(ChatClient &client, Guild *guild);
        void handleGuildKickMember(ChatClient &client, MessageIn &msg);
        void handleGuildQuit(ChatClient &client, MessageIn &msg);

        void handlePartyInviteAnswer(ChatClient &client, MessageIn &msg);
        void handlePartyQuit(ChatClient &client);
        void removeExpiredPartyInvites();
        void removeUserFromParty(ChatClient &client);

        /**
         * Tell all the party members a member has left
         */
        void informPartyMemberQuit(ChatClient &client);

        /**
         * Tell the player to be more polite.
         */
        void warnPlayerAboutBadWords(ChatClient &computer);

        /**
         * Say something private to a player.
         */
        void sayToPlayer(ChatClient &computer, const std::string &playerName,
                         const std::string &text);

        /**
         * Finds out the name of a character by its id. Either searches it
         * in the list of online characters or otherwise gets it from the db.
         */
        unsigned int getIdOfChar(const std::string &name);

        /**
         * Sends a message to every client in a registered channel.
         *
         * @param channel the channel to send the message in, must not be NULL
         * @param msg     the message to be sent
         */
        void sendInChannel(ChatChannel *channel, MessageOut &msg);

        /**
         * Retrieves the guild channel or creates one automatically
         * Automatically makes client join it
         * @param The name of the guild (and therefore the channel)
         * @param The client to join the channel
         * @return Returns the channel joined
         */
        ChatChannel *joinGuildChannel(const std::string &name,
                                      ChatClient &client);

        /**
         * Set the topic of a guild channel
         */
        void guildChannelTopicChange(ChatChannel *channel, int playerId,
                                     const std::string &topic);

        /**
         * Container for pending clients and pending connections.
         */
        TokenCollector<ChatHandler, ChatClient *, Pending *> mTokenCollector;
        friend void registerChatClient(const std::string &, const std::string &, int);
};

/**
 * Register future client attempt. Temporary until physical server split.
 */
void registerChatClient(const std::string &, const std::string &, int);

extern ChatHandler *chatHandler;

#endif