usersinfos.cpp

Go to the documentation of this file.
00001 /*
00002 #########################################################################
00003 #
00004 #  This file is part of trustyRC.
00005 #
00006 #  trustyRC, fully modular IRC robot 
00007 #  Copyright (C) 2006-2008 Nicoleau Fabien 
00008 #
00009 #  trustyRC is free software: you can redistribute it and/or modify
00010 #  it under the terms of the GNU General Public License as published by
00011 #  the Free Software Foundation, either version 3 of the License, or
00012 #  (at your option) any later version.
00013 #
00014 #  trustyRC is distributed in the hope that it will be useful,
00015 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 #  GNU General Public License for more details.
00018 #
00019 #  You should have received a copy of the GNU General Public License
00020 #  along with trustyRC.  If not, see <http://www.gnu.org/licenses/>.
00021 #
00022 #########################################################################
00023 */
00024 
00029 #include "usersinfos.h"
00030 
00034 UsersInfos::UsersInfos(BotKernel*b)
00035 {
00036         this->author = "Nicoleau Fabien";
00037         this->description = "Follow users modes";
00038         this->version = VERSION;
00039         this->name = "usersinfos";
00040         this->bindFunction("JOIN",IN_TYPE_HANDLER,"onJoin",0,10);
00041         this->bindFunction("PART",IN_TYPE_HANDLER,"onPart",0,10);
00042         this->bindFunction("QUIT",IN_TYPE_HANDLER,"onQuit",0,10);
00043         this->bindFunction("KICK",IN_TYPE_HANDLER,"onKick",0,10);
00044         this->bindFunction("MODE",IN_TYPE_HANDLER,"mode",0,10);
00045         this->bindFunction("NICK",IN_TYPE_HANDLER,"nick",0,10);
00046         this->bindFunction("352",IN_TYPE_HANDLER,"event352",0,10);
00047         this->bindFunction("005",IN_TYPE_HANDLER,"event005",0,10);
00048         this->bindFunction("240",IN_LOOP,"reloadUsers",0,10);
00049         //this->bindFunction("test",IN_COMMAND_HANDLER,"test",0,10);
00050    this->lastQuitChannels.clear();
00051 }
00052 
00056 UsersInfos::~UsersInfos()
00057 {
00058         for (map<string,Channel*>::const_iterator iter = this->users.begin();iter!=this->users.end();++iter)
00059         { 
00060                 delete ((Channel*)iter->second);
00061         }
00062 }
00063 
00073 void UsersInfos::addPrefixe(char mode,char prefixe)
00074 {
00075         string translation = "";
00076         translation += mode;
00077         translation += prefixe;
00078         this->prefixes.push_back(translation);
00079 }
00080 
00090 char UsersInfos::getPrefixe(char mode)
00091 {
00092         for(unsigned int i = 0 ; i < this->prefixes.size();i++)
00093         {
00094                 if (this->prefixes[i][0]==mode)
00095                 {
00096                         return this->prefixes[i][1];
00097                         break;
00098                 }
00099         }
00100         return '\0';
00101 }
00102 
00107 string UsersInfos::getPrefixes() {
00108    string prefixes = "";
00109         for(unsigned int i = 0 ; i < this->prefixes.size();i++)
00110         {
00111       prefixes += this->prefixes[i] ;
00112         }
00113    return prefixes;
00114 }
00115 
00123 bool UsersInfos::hasMode(string channel,string nick,char mode)
00124 {
00125    string status = "";
00126    map<string,Channel*>::iterator fter = this->users.find(channel);
00127    if ( fter == this->users.end() ) {
00128       return false;
00129    }
00130    else {
00131       status = ((Channel*)(fter->second))->getStatusByNick(nick);
00132       if ( status.find(this->getPrefixe(mode)) != string::npos )
00133          return true;
00134       else
00135          return false;
00136    }
00137 }
00138 
00143 vector<string>* UsersInfos::getLastQuitChannels()
00144 {
00145    return &this->lastQuitChannels;
00146 }
00147 
00152 map<string,Channel*>* UsersInfos::getUsers()
00153 {
00154         return &this->users;
00155 }
00156 
00157 extern "C"
00158 {
00159         Plugin *contruct_usersinfos(BotKernel*b)
00160         {
00161                 return new UsersInfos(b);
00162         }
00163         void destroy_usersinfos(Plugin*p)
00164         {
00165                 delete p;
00166         }
00167         bool reloadUsers(Message*m,Plugin*p,BotKernel*b)
00168         {
00169                 time_t minus;
00170                 time(&minus);
00171                 map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00172                 if (users->size()>0)
00173                 {
00174                         map<string,Channel*>::const_iterator toUpdate = users->begin();
00175                         map<string,Channel*>::const_iterator iter = users->begin();
00176                         for (map<string,Channel*>::const_iterator iter = users->begin();iter!=users->end();++iter)
00177                         { 
00178                                 if (((Channel*)iter->second)->getLastWhoUpdate()<minus)
00179                                 {
00180                                         minus = ((Channel*)iter->second)->getLastWhoUpdate() ;
00181                                         toUpdate = iter;
00182                                 }
00183                         }
00184                         ((Channel*)toUpdate->second)->truncateUsersList();
00185                         b->send(IRCProtocol::who(((Channel*)toUpdate->second)->getName(),""));
00186                         ((Channel*)toUpdate->second)->notifyWho();
00187                 }
00188                 return true;
00189         }
00190 
00191         bool onJoin(Message*m,Plugin*p,BotKernel*b)
00192         {
00193       string channel="";
00194                 map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00195                 map<string,Channel*>::iterator fter = users->find(m->getSource());
00196                 if(m->getNickSender()==b->getNick())
00197                 {
00198                         if ( fter != users->end() ) {
00199                                 delete ((Channel*)fter->second);
00200                                 users->erase(fter);
00201                         }
00202          channel = m->getSource() ;
00203          if (channel[0] == ':') { // Unreal IRCD
00204             channel=channel.erase(0,1);
00205          }
00206                         (*users)[channel] = new Channel(channel);
00207                         b->send(IRCProtocol::who(channel,""));
00208                 }
00209                 else
00210                 {
00211                         if ( fter == users->end() ) {
00212                                 (*users)[m->getSource()] = new Channel(m->getSource());
00213                                 b->send(IRCProtocol::who(m->getSource(),""));
00214                         }
00215                         else
00216                         {
00217                                 ((Channel*)fter->second)->addUser(m->getNickSender(),m->getHostSender(),m->getIdentSender(),"");
00218                         }
00219                 }
00220                 return true;
00221         }
00222 
00223         bool onPart(Message*m,Plugin*p,BotKernel*b)
00224         {
00225                 map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00226                 map<string,Channel*>::iterator fter = users->find(m->getSource());
00227                 if ( fter != users->end() ) {
00228                         if(m->getNickSender()!=b->getNick())
00229                         {
00230                                 ((Channel*)fter->second)->delUserByNick(m->getNickSender());
00231                         }
00232                         else
00233                         {
00234                                 delete ((Channel*)fter->second);
00235                                 users->erase(fter);
00236                         }
00237                 }
00238                 return true;
00239         }
00240 
00241         bool onQuit(Message*m,Plugin*p,BotKernel*b)
00242         {
00243       UsersInfos* ui = (UsersInfos*)p;
00244       ui->getLastQuitChannels()->clear();
00245                 map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00246                 for (map<string,Channel*>::const_iterator iter = users->begin();iter!=users->end();++iter)
00247                 { 
00248                         if (((Channel*)iter->second)->delUserByNick(m->getNickSender())) {
00249             ui->getLastQuitChannels()->push_back(((Channel*)iter->second)->getName());
00250          }
00251                 }
00252                 return true;
00253         }
00254 
00255         bool onKick(Message*m,Plugin*p,BotKernel*b)
00256         {
00257                 map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00258                 map<string,Channel*>::iterator fter = users->find(m->getSource());
00259                 if ( fter != users->end() ) {
00260                         if(m->getPart(3)!=b->getNick())
00261                         {
00262                                 ((Channel*)fter->second)->delUserByNick(m->getPart(3));
00263                         }
00264                         else
00265                         {
00266                                 delete ((Channel*)fter->second);
00267                                 users->erase(fter);
00268                         }
00269                 }
00270                 return true;
00271         }
00272 
00273         bool mode(Message*m,Plugin*p,BotKernel*b)
00274         {       
00275                 UsersInfos*obj = (UsersInfos*)p;
00276                 if ( m->getSplit().size() > 4 )
00277                 {
00278                         map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00279                         map<string,Channel*>::iterator fter = users->find(m->getSource());
00280                         if ( fter != users->end() ) {
00281                                 string modes = m->getPart(3) ;
00282                                 char sign = '\0';
00283                                 unsigned int nicksIndex = 4 ;
00284                                 for (unsigned int i = 0 ; i < modes.length() ; i ++ )
00285                                 {
00286                                         if ((modes[i]=='+')||(modes[i]=='-'))
00287                                         {
00288                                                 sign = modes[i];
00289                                         }
00290                                         else
00291                                         {
00292                                                 ((Channel*)fter->second)->updateStatusByNick(m->getPart(nicksIndex),sign,obj->getPrefixe(modes[i]));
00293                                                 nicksIndex++;
00294                                         }
00295                                 }
00296                         }
00297                 }
00298       return true;
00299         }
00300 
00301         bool nick(Message*m,Plugin*p,BotKernel*b)
00302         {
00303                 map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00304                 for (map<string,Channel*>::const_iterator iter = users->begin();iter!=users->end();++iter)
00305                 { 
00306                         ((Channel*)iter->second)->setNickByNick(m->getNickSender(),m->getSource().substr(1)); // source is new nick
00307                 }
00308                 return true;
00309         }
00310 
00311         bool event352(Message*m,Plugin*p,BotKernel*b)
00312         {
00313            map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00314                 map<string,Channel*>::iterator fter = users->find(m->getPart(3));
00315                 if ( fter != users->end() ) {
00316                         ((Channel*)fter->second)->addUser(m->getPart(7),m->getPart(5),m->getPart(4),m->getPart(8));
00317                 }
00318                 return true;
00319         }
00320 
00321         bool event005(Message*m,Plugin*p,BotKernel*b)
00322         {
00323                 int index1 = 0;
00324                 int index2 = 0;
00325                 UsersInfos*obj = (UsersInfos*)p;
00326                 string founded;
00327                 vector<string> parts = m->getSplit();
00328                 for (unsigned int i = 0; i < parts.size() ; i ++ )
00329                 {
00330                         if ( parts[i].find("PREFIX=") != string::npos)
00331                         {
00332                                 founded = parts[i].substr(8) ;
00333                                 index2 = founded.find(")")+1;
00334                                 while (founded[index1]!= ')')
00335                                 {
00336                                         obj->addPrefixe(founded[index1],founded[index2]);
00337                                         index1++;
00338                                         index2++;
00339                                 }
00340                         }
00341                 }
00342                 return true;
00343         }
00344   
00345    /*bool test (Message*m,Plugin*p,BotKernel*b) 
00346    {
00347            map<string,Channel*>* users = ((UsersInfos*)p)->getUsers();
00348                 map<string,Channel*>::iterator fter = users->find(m->getSource());
00349                 vector<string*> chanUsers = ((Channel*)fter->second)->getUsers();
00350       for (unsigned int i= 0;i < chanUsers.size() ; i ++ ) {
00351          cout << "nick:" << chanUsers[i][0] << " host:" << chanUsers[i][1] << " ident:" << chanUsers[i][2] << " status:" << chanUsers[i][3] << endl;
00352       }
00353       return true;
00354    }*/
00355 }

Generated on Sun Aug 16 15:28:29 2009 for trustyRC by  doxygen 1.5.8