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
27 #include "StringTokenizer.H"
32 Channel::Channel(ServerConnection *c,
33 String name, String wanted = "")
34 : channelName(name.toLower()), channelTopic(""),
35 lockedTopic(false), channelMode(0), channelLimit(0),
36 channelKey(""), keepModes(DEFAULT_KEEPMODES), wantedModes(wanted), count(0),
37 countOp(0), joined(false), doMode(true), gotWho(false), cnx(c)
40 channelMemory.clear();
41 channelBanlist.clear();
43 if (c->bot->wantedChannels[channelName])
45 if (c->bot->wantedChannels[channelName]->keep != "")
46 keepModes = c->bot->wantedChannels[channelName]->keep;
48 if (c->bot->wantedChannels[channelName]->mode != "")
49 wantedModes = c->bot->wantedChannels[channelName]->mode;
56 std::map<String, User *, std::less<String> >::iterator it;
58 while (channelMemory.size() != 0) {
59 it = channelMemory.begin();
61 if (u && u->userListItem && u->userListItem->identified > 0)
62 u->userListItem->identified--;
63 channelMemory.erase(it);
68 std::vector<BanEntry *>::iterator it2;
70 while (channelBanlist.size() != 0) {
71 it2 = channelBanlist.begin();
73 channelBanlist.erase(it2);
79 Channel::addNick(String n, String uh, int mode, UserList *ul,
82 if (User * u = getUser(n.toLower())) {
84 if (u->mode & User::OP_MODE)
86 channelMemory.erase(n.toLower());
93 u = new User(n, mode);
95 u = new User(n, uh, channelName, mode, ul);
97 channelMemory[n.toLower()] = u;
99 if (u->userListItem) {
100 if (u->userListItem->identified)
101 u->userListItem->identified++;
103 if (u->userListItem->passwd == "")
104 u->userListItem->identified = 1;
109 if (u->mode & User::OP_MODE)
114 Channel::delNick(String n)
117 User *u = getUser(n);
122 if (channelMemory[n]!=0) {
123 channelMemory.erase(n);
127 if (u->mode & User::OP_MODE)
129 if (u->userListItem && u->userListItem->identified > 0)
130 u->userListItem->identified--;
135 Channel::changeNick(String on, String nn)
138 User *u = getUser(on);
139 channelMemory.erase(on);
140 channelMemory[nn.toLower()] = u;
144 Channel::hasNick(String n)
146 return getUser(n.toLower()) != 0;
150 Channel::getUser(String n)
156 if ((u = channelMemory[n]) == 0) {
157 channelMemory.erase(n);
165 Channel::addBan(String mask, std::time_t expiration)
167 for (std::vector<BanEntry *>::iterator it = channelBanlist.begin();
168 it != channelBanlist.end(); ++it)
169 if ((*it)->getMask() == mask) {
170 if (expiration > (*it)->getExpirationDate())
171 (*it)->setExpirationDate(expiration);
174 channelBanlist.push_back(new BanEntry(mask, expiration));
178 Channel::delBan(String mask)
180 for (std::vector<BanEntry *>::iterator it = channelBanlist.begin();
181 it != channelBanlist.end(); ++it)
182 if (mask == (*it)->getMask()) {
184 channelBanlist.erase(it);
191 Channel::resynchModes()
193 cnx->queue->sendChannelMode(String("MODE ") + channelName +
194 " +" + cnx->bot->wantedChannels[channelName]->mode);
198 Channel::parseMode(Person *from, String mode)
201 StringTokenizer st(mode);
202 String m = st.next_token(), n;
205 u = getUser(from->getNick());
206 // u == 0 if it's a server mode
207 bool doNotObey = !u || (u->getProt() <= User::NO_PROT);
211 for (int i = 0; i < m.length(); i++)
217 case 'p': sign =='+' ? channelMode |= PRIVATE :
218 channelMode &= ~PRIVATE;
219 if (keepModes.find('p') != -1 && doNotObey) {
220 if (sign == '-' && wantedModes.find('p') != -1)
221 cnx->queue->sendChannelMode(channelName, "+p", "");
223 cnx->queue->sendChannelMode(channelName, "-p", "");
226 case 's': sign =='+' ? channelMode |= SECRET :
227 channelMode &= ~SECRET;
228 if (keepModes.find('s') != -1 && doNotObey) {
229 if (sign == '-' && wantedModes.find('s') != -1)
230 cnx->queue->sendChannelMode(channelName, "+s", "");
232 cnx->queue->sendChannelMode(channelName, "-s", "");
235 case 'i': sign =='+' ? channelMode |= INVITE_ONLY :
236 channelMode &= ~INVITE_ONLY;
237 if (keepModes.find('i') != -1 && doNotObey) {
238 if (sign == '-' && wantedModes.find('i') != -1)
239 cnx->queue->sendChannelMode(channelName, "+i", "");
241 cnx->queue->sendChannelMode(channelName, "-i", "");
244 case 't': sign =='+' ? channelMode |= TOPIC_RESTRICTED :
245 channelMode &= ~TOPIC_RESTRICTED;
246 if (keepModes.find('t') != -1 && doNotObey) {
247 if (sign == '-' && wantedModes.find('t') != -1)
248 cnx->queue->sendChannelMode(channelName, "+t", "");
250 cnx->queue->sendChannelMode(channelName, "-t", "");
253 case 'n': sign =='+' ? channelMode |= EXTMSG_RESTRICTED :
254 channelMode &= ~EXTMSG_RESTRICTED;
255 if (keepModes.find('n') != -1 && doNotObey) {
256 if (sign == '-' && wantedModes.find('n') != -1)
257 cnx->queue->sendChannelMode(channelName, "+n", "");
259 cnx->queue->sendChannelMode(channelName, "-n", "");
262 case 'm': sign =='+' ? channelMode |= MODERATED :
263 channelMode &= ~MODERATED;
264 if (keepModes.find('m') != -1 && doNotObey) {
265 if (sign == '-' && wantedModes.find('m') != -1)
266 cnx->queue->sendChannelMode(channelName, "+m", "");
268 cnx->queue->sendChannelMode(channelName, "-m", "");
272 if (keepModes.find('l') != -1 && doNotObey) {
273 if (sign == '-' && wantedModes.find('l') != -1)
274 cnx->queue->sendChannelMode(channelName, "+l",
275 String((long)channelLimit));
277 cnx->queue->sendChannelMode(channelName, "-l", "");
279 channelLimit = (sign == '+' ? std::atoi(st.next_token().c_str()) :
283 channelKey = st.next_token();
284 if (keepModes.find('k') != -1 && doNotObey) {
285 if (sign == '-' && wantedModes.find('k') != -1)
286 cnx->queue->sendChannelMode(channelName, "+k", channelKey);
288 cnx->queue->sendChannelMode(channelName, "-k", channelKey);
290 cnx->bot->wantedChannels[channelName]->key = (sign == '+' ? channelKey :
297 sign == '+' ? countOp++ : countOp--;
300 sign == '+' ? (u->mode |= User::OP_MODE) :
301 (u->mode &= ~User::OP_MODE);
302 if (sign == '-' && u->getProt() >= User::NO_DEOP) {
303 String fromNick = from->getNick();
304 User * v = getUser(fromNick);
307 if (v->getProt() < User::NO_DEOP)
308 cnx->queue->sendChannelMode(channelName, "-o", fromNick);
309 cnx->queue->sendChannelMode(channelName, "+o", n);
312 if (doNotObey && !from &&
313 u->getProt() < User::NO_DEOP && !u->getAop())
314 cnx->queue->sendChannelMode(channelName, "-o", n);
316 cnx->bot->shitList->getShit(n+"!"+cnx->bot->getUserhost(channelName, n),
318 if (se && se->isStillValid() &&
319 se->getShitLevel() >= ShitEntry::SHIT_NOOP)
320 cnx->queue->sendChannelMode(channelName, "-o", n);
321 if (n.toLower() == cnx->bot->nickName.toLower() && doMode) {
328 u = getUser(st.next_token());
329 u->mode = (sign == '+' ? u->mode |= User::VOICE_MODE :
330 u->mode &= ~User::VOICE_MODE);
333 String m = st.next_token();
334 sign == '+' ? addBan(m) : delBan(m);
337 cnx->bot->shitList->getShit(m, channelName);
338 if (se && se->isStillValid() &&
339 se->getShitLevel() >= ShitEntry::SHIT_NODEBAN)
340 cnx->queue->sendChannelMode(channelName, "+b", m);