channel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00029 #include "channel.h"
00030
00036 Channel::Channel(string name)
00037 {
00038 this->topic = "";
00039 this->name = name;
00040 this->users.clear();
00041 for(unsigned int i=0;i<4;i++) {
00042 this->lastPart[i] = "";
00043 }
00044 time(&this->lastWhoUpdate);
00045 }
00046
00051 Channel::~Channel()
00052 {
00053 this->truncateUsersList();
00054 }
00055
00060 string Channel::getName()
00061 {
00062 return this->name;
00063 }
00064
00074 bool Channel::addUser(string nick,string host,string ident,string status)
00075 {
00076 string * buddy = new string[4];
00077 buddy[0]=nick;
00078 buddy[1]=host;
00079 buddy[2]=ident;
00080 buddy[3]=status;
00081 if ( this->getHostByNick(nick) == "" ) {
00082 this->users.push_back(buddy);
00083 return true;
00084 }
00085 else {
00086 return false;
00087 }
00088 }
00089
00096 vector<string*>::iterator Channel::getIterator(string comparator,unsigned int index)
00097 {
00098 if ( index > 3 )
00099 return this->users.end()+1;
00100 vector<string*>::iterator it = this->users.begin();
00101 while (it != this->users.end()) {
00102 if ( (*it)[index] == comparator ) {
00103 return it;
00104 }
00105 else
00106 it ++;
00107 }
00108 return this->users.end()+1;
00109 }
00110
00118 bool Channel::delUserByNick(string nick)
00119 {
00120 vector<string*>::iterator it = this->getIterator(nick,0) ;
00121 if(it != (this->users.end()+1 )) {
00122 for(unsigned int i = 0 ; i < 4 ; i ++ ) {
00123 this->lastPart[i] = (*it)[i];
00124 }
00125 delete [] (*it) ;
00126 this->users.erase(it);
00127 return true;
00128 }
00129 else
00130 return false;
00131 }
00132
00140 bool Channel::delUserByHost(string host)
00141 {
00142 vector<string*>::iterator it = this->getIterator(host,1) ;
00143 if(it != (this->users.end()+1 )) {
00144 for(unsigned int i = 0 ; i < 4 ; i ++ ) {
00145 this->lastPart[i] = (*it)[i];
00146 }
00147 delete [] (*it) ;
00148 this->users.erase(it);
00149 return true;
00150 }
00151 else
00152 return false;
00153 }
00154
00160 string Channel::getNickByHost(string host)
00161 {
00162 vector<string*>::iterator it = this->getIterator(host,1) ;
00163 if(it != (this->users.end()+1 ))
00164 return (*it)[0] ;
00165 else
00166 return "";
00167 }
00168
00178 string* Channel::getInfosByNick(string nick)
00179 {
00180 vector<string*>::iterator it = this->getIterator(nick,0) ;
00181 if(it != (this->users.end()+1 )) {
00182 return (*it);
00183 }
00184 else {
00185 return NULL;
00186 }
00187 }
00188
00194 string Channel::getStatusByNick(string nick)
00195 {
00196 vector<string*>::iterator it = this->getIterator(nick,0) ;
00197 if(it != (this->users.end()+1 )) {
00198 return (*it)[3] ;
00199 }
00200 else
00201 return "";
00202 }
00203
00209 string Channel::getStatusByHost(string host)
00210 {
00211 vector<string*>::iterator it = this->getIterator(host,1) ;
00212 if(it != (this->users.end()+1 )) {
00213 return (*it)[3] ;
00214 }
00215 else
00216 return "";
00217 }
00218
00224 string Channel::getHostByNick(string nick)
00225 {
00226 vector<string*>::iterator it = this->getIterator(nick,0) ;
00227 if(it != (this->users.end()+1 )) {
00228 return (*it)[1] ;
00229 }
00230 else
00231 return "";
00232 }
00233
00239 string Channel::getIdentByNick(string nick)
00240 {
00241 vector<string*>::iterator it = this->getIterator(nick,0) ;
00242 if(it != (this->users.end()+1 )) {
00243 return (*it)[2] ;
00244 }
00245 else
00246 return "";
00247 }
00248
00254 string Channel::getIdentByHost(string host)
00255 {
00256 vector<string*>::iterator it = this->getIterator(host,1) ;
00257 if(it != (this->users.end()+1 )) {
00258 return (*it)[2] ;
00259 }
00260 else
00261 return "";
00262 }
00263
00271 bool Channel::setNickByNick(string old,string newnick)
00272 {
00273 vector<string*>::iterator it = this->getIterator(old,0) ;
00274 if(it != (this->users.end()+1 )) {
00275 (*it)[0] = newnick;
00276 return true ;
00277 }
00278 else
00279 return false;
00280 }
00281
00289 bool Channel::setNickByHost(string host,string newnick)
00290 {
00291 vector<string*>::iterator it = this->getIterator(host,1) ;
00292 if(it != (this->users.end()+1 )) {
00293 (*it)[0] = newnick;
00294 return true ;
00295 }
00296 else
00297 return false;
00298 }
00299
00308 bool Channel::updateStatusByNick(string nick,char sign,char mode)
00309 {
00310 string::size_type pos ;
00311 vector<string*>::iterator it = this->getIterator(nick,0) ;
00312 if(it != (this->users.end()+1 ))
00313 {
00314 if (sign == '-' )
00315 {
00316 pos = ((*it)[3]).find(mode) ;
00317 if (pos!=string::npos)
00318 {
00319 ((*it)[3]).erase(pos,1);
00320 }
00321 }
00322 else if (sign == '+')
00323 {
00324 pos = ((*it)[3]).find(mode) ;
00325 if (pos==string::npos)
00326 {
00327 ((*it)[3])+=mode;
00328 }
00329 }
00330 else {
00331 return false;
00332 }
00333 return true ;
00334 }
00335 else
00336 {
00337 return false;
00338 }
00339 }
00340
00347 bool Channel::checkNickAccess(string nick,char access)
00348 {
00349 string::size_type pos ;
00350 vector<string*>::iterator it = this->getIterator(nick,0) ;
00351 if(it != (this->users.end()+1 ))
00352 {
00353 pos = ((*it)[3]).find(access) ;
00354 if(pos == string::npos )
00355 {
00356 return false;
00357 }
00358 else
00359 {
00360 return true;
00361 }
00362 }
00363 else {
00364 return false;
00365 }
00366 }
00367
00371 void Channel::truncateUsersList()
00372 {
00373 vector<string*>::iterator it = this->users.begin();
00374 while (it != this->users.end()) {
00375 delete [] (*it) ;
00376 it++;
00377 }
00378 this->users.clear();
00379 }
00380
00390 vector<string*> Channel::getUsers()
00391 {
00392 return this->users;
00393 }
00394
00399 time_t Channel::getLastWhoUpdate()
00400 {
00401 return this->lastWhoUpdate;
00402 }
00403
00407 void Channel::notifyWho()
00408 {
00409 time(&this->lastWhoUpdate);
00410 }
00411
00416 string Channel::getTopic()
00417 {
00418 return this->topic;
00419 }
00420
00425 void Channel::setTopic(string topic)
00426 {
00427 this->topic = topic;
00428 }
00429
00434 string* Channel::getLastPartInfos()
00435 {
00436 return this->lastPart ;
00437 }
00438
00444 bool Channel::isOnChannel(string nick) {
00445 if ( this->getInfosByNick(nick) == NULL )
00446 return false;
00447 else
00448 return true;
00449 }