Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
86 lines (75 sloc) 2.48 KB
#include "connection.h"
//#include "user.cpp"
Channel::Channel(Server* thisServer) {
parentServer = thisServer;
topic = "";
namesSync = false;
}
void Channel::parseNames(std::vector<std::string> names) {
if (namesSync) {
users.clear();
namesSync = false;
}
std::tr1::unordered_map<char, char> prefixes = parentServer->getPrefixes();
std::vector<char> modes;
for (unsigned int i = 0; i < names.size(); i++) {
for (std::tr1::unordered_map<char, char>::iterator prefixIter = prefixes.begin(); prefixIter != prefixes.end(); prefixIter++) {
if (prefixIter->second == names[i][0]) {
modes.push_back(prefixIter->first);
names[i] = names[i].substr(1);
}
}
users.insert(std::pair<std::string, User*> (names[i], new User (this)));
for (unsigned int j = 0; j < modes.size(); j++) {
for (std::tr1::unordered_map<std::string, User*>::iterator userIter = users.begin(); userIter != users.end(); userIter++) {
if (names[i] == userIter->first)
userIter->second->status(true, modes[j]);
}
}
modes.clear();
}
}
void Channel::numeric366() {
namesSync = true;
}
void Channel::setTopic(std::string newTopic) {
topic = newTopic;
}
void Channel::setMode(bool add, char mode, std::string param) {
std::tr1::unordered_map<char, char> prefixes = parentServer->getPrefixes();
for (std::tr1::unordered_map<char, char>::iterator it = prefixes.begin(); it != prefixes.end(); it++) {
if (it->first == mode) {
bool exists = false;
for (std::tr1::unordered_map<std::string, User*>::iterator iter = users.begin(); iter != users.end(); iter++) {
if (iter->first == param) {
iter->second->status(add, mode);
exists = true;
}
}
if (!exists)
parentServer->resyncChannels();
}
}
}
void Channel::joinChannel(std::string nick) {
users.insert(std::pair<std::string, User*> (nick, new User (this)));
}
void Channel::leaveChannel(std::string nick) {
users.erase(nick);
}
std::list<std::string> Channel::getUsers() {
std::list<std::string> channelUsers;
for (std::tr1::unordered_map<std::string, User*>::iterator userIter = users.begin(); userIter != users.end(); ++userIter)
channelUsers.insert(channelUsers.end(), userIter->first);
return channelUsers;
}
char Channel::getStatus(std::string user) {
for (std::tr1::unordered_map<std::string, User*>::iterator userIter = users.begin(); userIter != users.end(); ++userIter) {
if (userIter->first == user)
return userIter->second->getStatus();
}
return '0';
}
std::string Channel::getTopic() {
return topic;
}