summaryrefslogtreecommitdiffstats
path: root/src/chat-server/chatchannelmanager.cpp
blob: a94ede247ea968d7d9f1aab8cab765a7286cb157 (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
/*
 *  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/>.
 */

#include <list>

#include "chat-server/chatchannelmanager.h"

#include "account-server/storage.h"
#include "chat-server/chatclient.h"
#include "chat-server/chathandler.h"
#include "chat-server/guildmanager.h"
#include "common/configuration.h"
#include "common/manaserv_protocol.h"
#include "utils/stringfilter.h"

using namespace ManaServ;

ChatChannelManager::ChatChannelManager() : mNextChannelId(1)
{
}


ChatChannelManager::~ChatChannelManager()
{
}

int ChatChannelManager::createNewChannel(const std::string &channelName,
                                         const std::string &channelAnnouncement,
                                         const std::string &channelPassword,
                                         bool joinable)
{
    int channelId = nextUsable();

    // Register channel
    mChatChannels.insert(std::make_pair(channelId,
                                        ChatChannel(channelId,
                                                    channelName,
                                                    channelAnnouncement,
                                                    channelPassword,
                                                    joinable)));
    return channelId;
}

bool ChatChannelManager::tryNewPublicChannel(const std::string &name)
{
    if (!stringFilter->filterContent(name))
    {
        return false;
    }

    // Checking strings for length and double quotes
    unsigned maxNameLength = Configuration::getValue("chat_maxChannelNameLength", 15);
    if (name.empty() ||
        name.length() > maxNameLength ||
        stringFilter->findDoubleQuotes(name))
    {
        return false;
    }
    else if (guildManager->doesExist(name) ||
             channelExists(name))
    {
        // Channel already exists
        return false;
    }
    else
    {
        // We attempt to create a new channel
        short id = createNewChannel(name, std::string(), std::string(), true);
        return id != 0;
    }
}

bool ChatChannelManager::removeChannel(int channelId)
{
    ChatChannels::iterator i = mChatChannels.find(channelId);
    if (i == mChatChannels.end())
        return false;
    i->second.removeAllUsers();
    mChatChannels.erase(i);
    mChannelsNoLongerUsed.push_back(channelId);
    return true;
}

std::list<const ChatChannel*> ChatChannelManager::getPublicChannels() const
{
    std::list<const ChatChannel*> channels;

    for (ChatChannels::const_iterator i = mChatChannels.begin(),
            i_end = mChatChannels.end();
         i != i_end; ++i)
    {
        if (i->second.canJoin())
        {
            channels.push_back(&i->second);
        }
    }

    return channels;
}

int ChatChannelManager::getChannelId(const std::string &channelName) const
{
    for (ChatChannels::const_iterator i = mChatChannels.begin(),
            i_end = mChatChannels.end();
         i != i_end; ++i)
    {
        if (i->second.getName() == channelName)
            return i->first;
    }
    return 0;
}

ChatChannel *ChatChannelManager::getChannel(int channelId)
{
    ChatChannels::iterator i = mChatChannels.find(channelId);
    if (i != mChatChannels.end())
        return &i->second;
    return 0;
}

ChatChannel *ChatChannelManager::getChannel(const std::string &name)
{
    for (ChatChannels::iterator i = mChatChannels.begin();
         i != mChatChannels.end(); ++i)
    {
        if (i->second.getName() == name)
            return &(i->second);
    }

    return 0;
}

void ChatChannelManager::setChannelTopic(int channelId, const std::string &topic)
{
    ChatChannels::iterator i = mChatChannels.find(channelId);
    if (i == mChatChannels.end())
        return;

    i->second.setAnnouncement(topic);
    chatHandler->warnUsersAboutPlayerEventInChat(&(i->second),
                                                 topic,
                                                 CHAT_EVENT_TOPIC_CHANGE);
}

void ChatChannelManager::removeUserFromAllChannels(ChatClient *user)
{
    // Local copy as they will be destroyed under our feet.
    std::vector<ChatChannel *> channels = user->channels;
    // Reverse iterator to reduce load on vector operations.
    for (std::vector<ChatChannel *>::const_reverse_iterator
         i = channels.rbegin(), i_end = channels.rend(); i != i_end; ++i)
    {
        chatHandler->warnUsersAboutPlayerEventInChat((*i),
                                                     user->characterName,
                                                     CHAT_EVENT_LEAVING_PLAYER);
        (*i)->removeUser(user);
    }
}

bool ChatChannelManager::channelExists(int channelId) const
{
    return mChatChannels.find(channelId) != mChatChannels.end();
}

bool ChatChannelManager::channelExists(const std::string &channelName) const
{
    for (ChatChannels::const_iterator i = mChatChannels.begin();
         i != mChatChannels.end(); ++i)
    {
        if (i->second.getName() == channelName)
            return true;
    }
    return false;
}

int ChatChannelManager::nextUsable()
{
    int channelId = 0;

    if (mChannelsNoLongerUsed.size() > 0)
    {
        channelId = mChannelsNoLongerUsed[0];
        mChannelsNoLongerUsed.pop_front();
    }
    else
    {
        channelId = mNextChannelId;
        ++mNextChannelId;
    }

    return channelId;
}