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
32 #include "ServerConnection.H"
33 #include "ShitEntry.H"
35 #include "StringTokenizer.H"
43 Channel::Channel(ServerConnection *c,
44 String name, String wanted = "")
45 : channelName(name.toLower()), channelTopic(""),
46 lockedTopic(false), channelMode(0), channelLimit(0),
47 channelKey(""), keepModes(DEFAULT_KEEPMODES), wantedModes(wanted), count(0),
48 countOp(0), joined(false), doMode(true), gotWho(false), cnx(c)
51 channelMemory.clear();
52 channelBanlist.clear();
54 if (c->bot->wantedChannels[channelName])
56 if (c->bot->wantedChannels[channelName]->keep != "")
57 keepModes = c->bot->wantedChannels[channelName]->keep;
59 if (c->bot->wantedChannels[channelName]->mode != "")
60 wantedModes = c->bot->wantedChannels[channelName]->mode;
67 std::map<String, User *, std::less<String> >::iterator it;
69 while (channelMemory.size() != 0) {
70 it = channelMemory.begin();
72 if (u && u->userListItem && u->userListItem->identified > 0)
73 u->userListItem->identified--;
74 channelMemory.erase(it);
79 std::vector<BanEntry *>::iterator it2;
81 while (channelBanlist.size() != 0) {
82 it2 = channelBanlist.begin();
84 channelBanlist.erase(it2);
90 Channel::addNick(String n, String uh, int mode, UserList *ul,
93 if (User * u = getUser(n.toLower())) {
95 if (u->mode & User::OP_MODE)
97 channelMemory.erase(n.toLower());
104 u = new User(n, mode);
106 u = new User(n, uh, channelName, mode, ul);
108 channelMemory[n.toLower()] = u;
110 if (u->userListItem) {
111 if (u->userListItem->identified)
112 u->userListItem->identified++;
114 if (u->userListItem->passwd == "")
115 u->userListItem->identified = 1;
120 if (u->mode & User::OP_MODE)
125 Channel::delNick(String n)
128 User *u = getUser(n);
133 if (channelMemory[n]!=0) {
134 channelMemory.erase(n);
138 if (u->mode & User::OP_MODE)
140 if (u->userListItem && u->userListItem->identified > 0)
141 u->userListItem->identified--;
146 Channel::changeNick(String on, String nn)
149 User *u = getUser(on);
150 channelMemory.erase(on);
151 channelMemory[nn.toLower()] = u;
155 Channel::hasNick(String n)
157 return getUser(n.toLower()) != 0;
161 Channel::getUser(String n)
167 if ((u = channelMemory[n]) == 0) {
168 channelMemory.erase(n);
176 Channel::addBan(String mask, std::time_t expiration)
178 for (std::vector<BanEntry *>::iterator it = channelBanlist.begin();
179 it != channelBanlist.end(); ++it)
180 if ((*it)->getMask() == mask) {
181 if (expiration > (*it)->getExpirationDate())
182 (*it)->setExpirationDate(expiration);
185 channelBanlist.push_back(new BanEntry(mask, expiration));
189 Channel::delBan(String mask)
191 for (std::vector<BanEntry *>::iterator it = channelBanlist.begin();
192 it != channelBanlist.end(); ++it)
193 if (mask == (*it)->getMask()) {
195 channelBanlist.erase(it);
202 Channel::resynchModes()
204 cnx->queue->sendChannelMode(String("MODE ") + channelName +
205 " +" + cnx->bot->wantedChannels[channelName]->mode);
209 Channel::parseMode(Person *from, String mode)
212 StringTokenizer st(mode);
213 String m = st.next_token(), n;
216 u = getUser(from->getNick());
217 // u == 0 if it's a server mode
218 bool doNotObey = !u || (u->getProt() <= User::NO_PROT);
222 for (int i = 0; i < m.length(); i++)
228 case 'p': sign =='+' ? channelMode |= PRIVATE :
229 channelMode &= ~PRIVATE;
230 if (keepModes.find('p') != -1 && doNotObey) {
231 if (sign == '-' && wantedModes.find('p') != -1)
232 cnx->queue->sendChannelMode(channelName, "+p", "");
234 cnx->queue->sendChannelMode(channelName, "-p", "");
237 case 's': sign =='+' ? channelMode |= SECRET :
238 channelMode &= ~SECRET;
239 if (keepModes.find('s') != -1 && doNotObey) {
240 if (sign == '-' && wantedModes.find('s') != -1)
241 cnx->queue->sendChannelMode(channelName, "+s", "");
243 cnx->queue->sendChannelMode(channelName, "-s", "");
246 case 'i': sign =='+' ? channelMode |= INVITE_ONLY :
247 channelMode &= ~INVITE_ONLY;
248 if (keepModes.find('i') != -1 && doNotObey) {
249 if (sign == '-' && wantedModes.find('i') != -1)
250 cnx->queue->sendChannelMode(channelName, "+i", "");
252 cnx->queue->sendChannelMode(channelName, "-i", "");
255 case 't': sign =='+' ? channelMode |= TOPIC_RESTRICTED :
256 channelMode &= ~TOPIC_RESTRICTED;
257 if (keepModes.find('t') != -1 && doNotObey) {
258 if (sign == '-' && wantedModes.find('t') != -1)
259 cnx->queue->sendChannelMode(channelName, "+t", "");
261 cnx->queue->sendChannelMode(channelName, "-t", "");
264 case 'n': sign =='+' ? channelMode |= EXTMSG_RESTRICTED :
265 channelMode &= ~EXTMSG_RESTRICTED;
266 if (keepModes.find('n') != -1 && doNotObey) {
267 if (sign == '-' && wantedModes.find('n') != -1)
268 cnx->queue->sendChannelMode(channelName, "+n", "");
270 cnx->queue->sendChannelMode(channelName, "-n", "");
273 case 'm': sign =='+' ? channelMode |= MODERATED :
274 channelMode &= ~MODERATED;
275 if (keepModes.find('m') != -1 && doNotObey) {
276 if (sign == '-' && wantedModes.find('m') != -1)
277 cnx->queue->sendChannelMode(channelName, "+m", "");
279 cnx->queue->sendChannelMode(channelName, "-m", "");
283 if (keepModes.find('l') != -1 && doNotObey) {
284 if (sign == '-' && wantedModes.find('l') != -1)
285 cnx->queue->sendChannelMode(channelName, "+l",
286 String((long)channelLimit));
288 cnx->queue->sendChannelMode(channelName, "-l", "");
290 channelLimit = (sign == '+' ? std::atoi(st.next_token().c_str()) :
294 channelKey = st.next_token();
295 if (keepModes.find('k') != -1 && doNotObey) {
296 if (sign == '-' && wantedModes.find('k') != -1)
297 cnx->queue->sendChannelMode(channelName, "+k", channelKey);
299 cnx->queue->sendChannelMode(channelName, "-k", channelKey);
301 cnx->bot->wantedChannels[channelName]->key = (sign == '+' ? channelKey :
308 sign == '+' ? countOp++ : countOp--;
311 sign == '+' ? (u->mode |= User::OP_MODE) :
312 (u->mode &= ~User::OP_MODE);
313 if (sign == '-' && u->getProt() >= User::NO_DEOP) {
314 String fromNick = from->getNick();
315 User * v = getUser(fromNick);
318 if (v->getProt() < User::NO_DEOP)
319 cnx->queue->sendChannelMode(channelName, "-o", fromNick);
320 cnx->queue->sendChannelMode(channelName, "+o", n);
323 if (doNotObey && !from &&
324 u->getProt() < User::NO_DEOP && !u->getAop())
325 cnx->queue->sendChannelMode(channelName, "-o", n);
327 cnx->bot->shitList->getShit(n+"!"+cnx->bot->getUserhost(channelName, n),
329 if (se && se->isStillValid() &&
330 se->getShitLevel() >= ShitEntry::SHIT_NOOP)
331 cnx->queue->sendChannelMode(channelName, "-o", n);
332 if (n.toLower() == cnx->bot->nickName.toLower() && doMode) {
339 u = getUser(st.next_token());
340 u->mode = (sign == '+' ? u->mode |= User::VOICE_MODE :
341 u->mode &= ~User::VOICE_MODE);
344 String m = st.next_token();
345 sign == '+' ? addBan(m) : delBan(m);
348 cnx->bot->shitList->getShit(m, channelName);
349 if (se && se->isStillValid() &&
350 se->getShitLevel() >= ShitEntry::SHIT_NODEBAN)
351 cnx->queue->sendChannelMode(channelName, "+b", m);