-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.cpp
More file actions
53 lines (39 loc) · 1.33 KB
/
Channel.cpp
File metadata and controls
53 lines (39 loc) · 1.33 KB
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
#include "Channel.hpp"
Channel::Channel(const string& name): _name(name) {}
Channel::~Channel() { }
void Channel::addUser(int clientFd, User *user) {
if (_userList.empty()) _operList.insert(clientFd);
_userList.insert(pair<int, User *>(clientFd, user));
}
int Channel::deleteUser(int clientFd) {
map<int, User *>::iterator it;
string clientName;
it = _userList.find(clientFd);
if (it == _userList.end()) return _userList.size();
clientName = it->second->getNickname();
_userList.erase(clientFd);
_operList.erase(clientFd);
if (_userList.empty()) return 0;
broadcast(clientName.append(" leave channel."));
if (_operList.empty()) {
pair<int, User *> nextOper;
nextOper = *_userList.begin();
_operList.insert(nextOper.first);
broadcast(nextOper.second->getNickname().append(" is new channel operator."));
}
return _userList.size();
}
bool Channel::isUserOper(int clientFd) {
set<int>::iterator it;
if (_operList.find(clientFd) != _operList.end())
return true;
else
return false;
}
void Channel::broadcast(const string& msg, int ignoreFd) {
map<int, User *>::iterator it;
for(it = _userList.begin(); it != _userList.end(); ++it) {
if (it->first == ignoreFd) continue;
it->second->addToReplyBuffer(msg);
}
}