1 // Channel.C -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005 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 02110-1301, USA.
26 #include "StringTokenizer.H"
31 Channel::Channel(ServerConnection *c,
32 String name, String wanted = "")
33 : channelName(name.toLower()), channelTopic(""),
34 lockedTopic(false), channelMode(0), channelLimit(0),
35 channelKey(""), keepModes(DEFAULT_KEEPMODES), wantedModes(wanted), count(0),
36 countOp(0), joined(false), doMode(true), gotWho(false), cnx(c)
39 channelMemory.clear();
40 channelBanlist.clear();
42 if (c->bot->wantedChannels[channelName])
44 if (c->bot->wantedChannels[channelName]->keep != "")
45 keepModes = c->bot->wantedChannels[channelName]->keep;
47 if (c->bot->wantedChannels[channelName]->mode != "")
48 wantedModes = c->bot->wantedChannels[channelName]->mode;
55 std::map<String, User *, std::less<String> >::iterator it;
57 while (channelMemory.size() != 0) {
58 it = channelMemory.begin();
60 if (u && u->userListItem && u->userListItem->identified > 0)
61 u->userListItem->identified--;
62 channelMemory.erase(it);
67 std::vector<BanEntry *>::iterator it2;
69 while (channelBanlist.size() != 0) {
70 it2 = channelBanlist.begin();
72 channelBanlist.erase(it2);
78 Channel::addNick(String n, String uh, int mode, UserList *ul,
81 if (User * u = getUser(n.toLower())) {
83 if (u->mode & User::OP_MODE)
85 channelMemory.erase(n.toLower());
92 u = new User(n, mode);
94 u = new User(n, uh, channelName, mode, ul);
96 channelMemory[n.toLower()] = u;
98 if (u->userListItem) {
99 if (u->userListItem->identified)
100 u->userListItem->identified++;
102 if (u->userListItem->passwd == "")
103 u->userListItem->identified = 1;
108 if (u->mode & User::OP_MODE)
113 Channel::delNick(String n)
116 User *u = getUser(n);
121 if (channelMemory[n]!=0) {
122 channelMemory.erase(n);
126 if (u->mode & User::OP_MODE)
128 if (u->userListItem && u->userListItem->identified > 0)
129 u->userListItem->identified--;
134 Channel::changeNick(String on, String nn)
137 User *u = getUser(on);
138 channelMemory.erase(on);
139 channelMemory[nn.toLower()] = u;
143 Channel::hasNick(String n)
145 return getUser(n.toLower()) != 0;
149 Channel::getUser(String n)
155 if ((u = channelMemory[n]) == 0) {
156 channelMemory.erase(n);
164 Channel::addBan(String mask, std::time_t expiration)
166 for (std::vector<BanEntry *>::iterator it = channelBanlist.begin();
167 it != channelBanlist.end(); ++it)
168 if ((*it)->getMask() == mask) {
169 if (expiration > (*it)->getExpirationDate())
170 (*it)->setExpirationDate(expiration);
173 channelBanlist.push_back(new BanEntry(mask, expiration));
177 Channel::delBan(String mask)
179 for (std::vector<BanEntry *>::iterator it = channelBanlist.begin();
180 it != channelBanlist.end(); ++it)
181 if (mask == (*it)->getMask()) {
183 channelBanlist.erase(it);
190 Channel::resynchModes()
192 cnx->queue->sendChannelMode(String("MODE ") + channelName +
193 " +" + cnx->bot->wantedChannels[channelName]->mode);
197 Channel::parseMode(Person *from, String mode)
200 StringTokenizer st(mode);
201 String m = st.next_token(), n;
204 u = getUser(from->getNick());
205 // u == 0 if it's a server mode
206 bool doNotObey = !u || (u->getProt() <= User::NO_PROT);
210 for (int i = 0; i < m.length(); i++)
216 case 'p': sign =='+' ? channelMode |= PRIVATE :
217 channelMode &= ~PRIVATE;
218 if (keepModes.find('p') != -1 && doNotObey) {
219 if (sign == '-' && wantedModes.find('p') != -1)
220 cnx->queue->sendChannelMode(channelName, "+p", "");
222 cnx->queue->sendChannelMode(channelName, "-p", "");
225 case 's': sign =='+' ? channelMode |= SECRET :
226 channelMode &= ~SECRET;
227 if (keepModes.find('s') != -1 && doNotObey) {
228 if (sign == '-' && wantedModes.find('s') != -1)
229 cnx->queue->sendChannelMode(channelName, "+s", "");
231 cnx->queue->sendChannelMode(channelName, "-s", "");
234 case 'i': sign =='+' ? channelMode |= INVITE_ONLY :
235 channelMode &= ~INVITE_ONLY;
236 if (keepModes.find('i') != -1 && doNotObey) {
237 if (sign == '-' && wantedModes.find('i') != -1)
238 cnx->queue->sendChannelMode(channelName, "+i", "");
240 cnx->queue->sendChannelMode(channelName, "-i", "");
243 case 't': sign =='+' ? channelMode |= TOPIC_RESTRICTED :
244 channelMode &= ~TOPIC_RESTRICTED;
245 if (keepModes.find('t') != -1 && doNotObey) {
246 if (sign == '-' && wantedModes.find('t') != -1)
247 cnx->queue->sendChannelMode(channelName, "+t", "");
249 cnx->queue->sendChannelMode(channelName, "-t", "");
252 case 'n': sign =='+' ? channelMode |= EXTMSG_RESTRICTED :
253 channelMode &= ~EXTMSG_RESTRICTED;
254 if (keepModes.find('n') != -1 && doNotObey) {
255 if (sign == '-' && wantedModes.find('n') != -1)
256 cnx->queue->sendChannelMode(channelName, "+n", "");
258 cnx->queue->sendChannelMode(channelName, "-n", "");
261 case 'm': sign =='+' ? channelMode |= MODERATED :
262 channelMode &= ~MODERATED;
263 if (keepModes.find('m') != -1 && doNotObey) {
264 if (sign == '-' && wantedModes.find('m') != -1)
265 cnx->queue->sendChannelMode(channelName, "+m", "");
267 cnx->queue->sendChannelMode(channelName, "-m", "");
271 if (keepModes.find('l') != -1 && doNotObey) {
272 if (sign == '-' && wantedModes.find('l') != -1)
273 cnx->queue->sendChannelMode(channelName, "+l",
274 String((long)channelLimit));
276 cnx->queue->sendChannelMode(channelName, "-l", "");
278 channelLimit = (sign == '+' ? std::atoi(st.next_token().c_str()) :
282 channelKey = st.next_token();
283 if (keepModes.find('k') != -1 && doNotObey) {
284 if (sign == '-' && wantedModes.find('k') != -1)
285 cnx->queue->sendChannelMode(channelName, "+k", channelKey);
287 cnx->queue->sendChannelMode(channelName, "-k", channelKey);
289 cnx->bot->wantedChannels[channelName]->key = (sign == '+' ? channelKey :
296 sign == '+' ? countOp++ : countOp--;
299 sign == '+' ? (u->mode |= User::OP_MODE) :
300 (u->mode &= ~User::OP_MODE);
301 if (sign == '-' && u->getProt() >= User::NO_DEOP) {
302 String fromNick = from->getNick();
303 User * v = getUser(fromNick);
306 if (v->getProt() < User::NO_DEOP)
307 cnx->queue->sendChannelMode(channelName, "-o", fromNick);
308 cnx->queue->sendChannelMode(channelName, "+o", n);
311 if (doNotObey && !from &&
312 u->getProt() < User::NO_DEOP && !u->getAop())
313 cnx->queue->sendChannelMode(channelName, "-o", n);
315 cnx->bot->shitList->getShit(n+"!"+cnx->bot->getUserhost(channelName, n),
317 if (se && se->isStillValid() &&
318 se->getShitLevel() >= ShitEntry::SHIT_NOOP)
319 cnx->queue->sendChannelMode(channelName, "-o", n);
320 if (n.toLower() == cnx->bot->nickName.toLower() && doMode) {
327 u = getUser(st.next_token());
328 u->mode = (sign == '+' ? u->mode |= User::VOICE_MODE :
329 u->mode &= ~User::VOICE_MODE);
332 String m = st.next_token();
333 sign == '+' ? addBan(m) : delBan(m);
336 cnx->bot->shitList->getShit(m, channelName);
337 if (se && se->isStillValid() &&
338 se->getShitLevel() >= ShitEntry::SHIT_NODEBAN)
339 cnx->queue->sendChannelMode(channelName, "+b", m);