1 // Channel.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005,2008 Clinton Ebadi
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 #include "StringTokenizer.H"
34 Channel::Channel(ServerConnection *c,
35 String name, String wanted = "")
36 : channelName(name.toLower()), channelTopic(""),
37 lockedTopic(false), channelMode(0), channelLimit(0),
38 channelKey(""), keepModes(DEFAULT_KEEPMODES), wantedModes(wanted), count(0),
39 countOp(0), joined(false), doMode(true), gotWho(false), cnx(c)
42 channelMemory.clear();
43 channelBanlist.clear();
45 if (c->bot->wantedChannels[channelName])
47 if (c->bot->wantedChannels[channelName]->keep != "")
48 keepModes = c->bot->wantedChannels[channelName]->keep;
50 if (c->bot->wantedChannels[channelName]->mode != "")
51 wantedModes = c->bot->wantedChannels[channelName]->mode;
58 std::map<String, User *, std::less<String> >::iterator it;
60 while (channelMemory.size() != 0) {
61 it = channelMemory.begin();
63 if (u && u->userListItem && u->userListItem->identified > 0)
64 u->userListItem->identified--;
65 channelMemory.erase(it);
70 std::vector<BanEntry *>::iterator it2;
72 while (channelBanlist.size() != 0) {
73 it2 = channelBanlist.begin();
75 channelBanlist.erase(it2);
81 Channel::addNick(String n, String uh, int mode, UserList *ul,
84 if (User * u = getUser(n.toLower())) {
86 if (u->mode & User::OP_MODE)
88 channelMemory.erase(n.toLower());
95 u = new User(n, mode);
97 u = new User(n, uh, channelName, mode, ul);
99 channelMemory[n.toLower()] = u;
101 if (u->userListItem) {
102 if (u->userListItem->identified)
103 u->userListItem->identified++;
105 if (u->userListItem->passwd == "")
106 u->userListItem->identified = 1;
111 if (u->mode & User::OP_MODE)
116 Channel::delNick(String n)
119 User *u = getUser(n);
124 if (channelMemory[n]!=0) {
125 channelMemory.erase(n);
129 if (u->mode & User::OP_MODE)
131 if (u->userListItem && u->userListItem->identified > 0)
132 u->userListItem->identified--;
137 Channel::changeNick(String on, String nn)
140 User *u = getUser(on);
141 channelMemory.erase(on);
142 channelMemory[nn.toLower()] = u;
146 Channel::hasNick(String n)
148 return getUser(n.toLower()) != 0;
152 Channel::getUser(String n)
158 if ((u = channelMemory[n]) == 0) {
159 channelMemory.erase(n);
167 Channel::addBan(String mask, std::time_t expiration)
169 for (std::vector<BanEntry *>::iterator it = channelBanlist.begin();
170 it != channelBanlist.end(); ++it)
171 if ((*it)->getMask() == mask) {
172 if (expiration > (*it)->getExpirationDate())
173 (*it)->setExpirationDate(expiration);
176 channelBanlist.push_back(new BanEntry(mask, expiration));
180 Channel::delBan(String mask)
182 for (std::vector<BanEntry *>::iterator it = channelBanlist.begin();
183 it != channelBanlist.end(); ++it)
184 if (mask == (*it)->getMask()) {
186 channelBanlist.erase(it);
193 Channel::resynchModes()
195 cnx->queue->sendChannelMode(String("MODE ") + channelName +
196 " +" + cnx->bot->wantedChannels[channelName]->mode);
200 Channel::parseMode(Person *from, String mode)
203 StringTokenizer st(mode);
204 String m = st.next_token(), n;
207 u = getUser(from->getNick());
208 // u == 0 if it's a server mode
209 bool doNotObey = !u || (u->getProt() <= User::NO_PROT);
213 for (int i = 0; i < m.length(); i++)
219 case 'p': sign =='+' ? channelMode |= PRIVATE :
220 channelMode &= ~PRIVATE;
221 if (keepModes.find('p') != -1 && doNotObey) {
222 if (sign == '-' && wantedModes.find('p') != -1)
223 cnx->queue->sendChannelMode(channelName, "+p", "");
225 cnx->queue->sendChannelMode(channelName, "-p", "");
228 case 's': sign =='+' ? channelMode |= SECRET :
229 channelMode &= ~SECRET;
230 if (keepModes.find('s') != -1 && doNotObey) {
231 if (sign == '-' && wantedModes.find('s') != -1)
232 cnx->queue->sendChannelMode(channelName, "+s", "");
234 cnx->queue->sendChannelMode(channelName, "-s", "");
237 case 'i': sign =='+' ? channelMode |= INVITE_ONLY :
238 channelMode &= ~INVITE_ONLY;
239 if (keepModes.find('i') != -1 && doNotObey) {
240 if (sign == '-' && wantedModes.find('i') != -1)
241 cnx->queue->sendChannelMode(channelName, "+i", "");
243 cnx->queue->sendChannelMode(channelName, "-i", "");
246 case 't': sign =='+' ? channelMode |= TOPIC_RESTRICTED :
247 channelMode &= ~TOPIC_RESTRICTED;
248 if (keepModes.find('t') != -1 && doNotObey) {
249 if (sign == '-' && wantedModes.find('t') != -1)
250 cnx->queue->sendChannelMode(channelName, "+t", "");
252 cnx->queue->sendChannelMode(channelName, "-t", "");
255 case 'n': sign =='+' ? channelMode |= EXTMSG_RESTRICTED :
256 channelMode &= ~EXTMSG_RESTRICTED;
257 if (keepModes.find('n') != -1 && doNotObey) {
258 if (sign == '-' && wantedModes.find('n') != -1)
259 cnx->queue->sendChannelMode(channelName, "+n", "");
261 cnx->queue->sendChannelMode(channelName, "-n", "");
264 case 'm': sign =='+' ? channelMode |= MODERATED :
265 channelMode &= ~MODERATED;
266 if (keepModes.find('m') != -1 && doNotObey) {
267 if (sign == '-' && wantedModes.find('m') != -1)
268 cnx->queue->sendChannelMode(channelName, "+m", "");
270 cnx->queue->sendChannelMode(channelName, "-m", "");
274 if (keepModes.find('l') != -1 && doNotObey) {
275 if (sign == '-' && wantedModes.find('l') != -1)
276 cnx->queue->sendChannelMode(channelName, "+l",
277 String((long)channelLimit));
279 cnx->queue->sendChannelMode(channelName, "-l", "");
281 channelLimit = (sign == '+' ? std::atoi(st.next_token().c_str()) :
285 channelKey = st.next_token();
286 if (keepModes.find('k') != -1 && doNotObey) {
287 if (sign == '-' && wantedModes.find('k') != -1)
288 cnx->queue->sendChannelMode(channelName, "+k", channelKey);
290 cnx->queue->sendChannelMode(channelName, "-k", channelKey);
292 cnx->bot->wantedChannels[channelName]->key = (sign == '+' ? channelKey :
299 sign == '+' ? countOp++ : countOp--;
302 sign == '+' ? (u->mode |= User::OP_MODE) :
303 (u->mode &= ~User::OP_MODE);
304 if (sign == '-' && u->getProt() >= User::NO_DEOP) {
305 String fromNick = from->getNick();
306 User * v = getUser(fromNick);
309 if (v->getProt() < User::NO_DEOP)
310 cnx->queue->sendChannelMode(channelName, "-o", fromNick);
311 cnx->queue->sendChannelMode(channelName, "+o", n);
314 if (doNotObey && !from &&
315 u->getProt() < User::NO_DEOP && !u->getAop())
316 cnx->queue->sendChannelMode(channelName, "-o", n);
318 cnx->bot->shitList->getShit(n+"!"+cnx->bot->getUserhost(channelName, n),
320 if (se && se->isStillValid() &&
321 se->getShitLevel() >= ShitEntry::SHIT_NOOP)
322 cnx->queue->sendChannelMode(channelName, "-o", n);
323 if (n.toLower() == cnx->bot->nickName.toLower() && doMode) {
330 u = getUser(st.next_token());
331 u->mode = (sign == '+' ? u->mode |= User::VOICE_MODE :
332 u->mode &= ~User::VOICE_MODE);
335 String m = st.next_token();
336 sign == '+' ? addBan(m) : delBan(m);
339 cnx->bot->shitList->getShit(m, channelName);
340 if (se && se->isStillValid() &&
341 se->getShitLevel() >= ShitEntry::SHIT_NODEBAN)
342 cnx->queue->sendChannelMode(channelName, "+b", m);