ignore.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 "ignore.h"
00030 
00034 Ignore::Ignore(BotKernel*b)
00035 {
00036         this->author = "eponyme";
00037         this->description = "Ignore manager";
00038         this->version = VERSION;
00039         this->name = "ignore";
00040         this->bindFunction("addignore",IN_COMMAND_HANDLER,"addIgnore",0,10);
00041         this->bindFunction("delignore",IN_COMMAND_HANDLER,"delIgnore",0,10);
00042         this->bindFunction("ignorelist",IN_COMMAND_HANDLER,"ignoreList",0,10);
00043         this->bindFunction("isignored",IN_COMMAND_HANDLER,"isIgnored",0,10);
00044         this->bindFunction("35",IN_LOOP,"purifyList",0,30);
00045         this->bindFunction("",IN_BEFORE_TREATMENT,"testIgnoredUser",0,10);
00046    this->addRequirement("admin");
00047 
00048    this->doc = new TiXmlDocument(b->getDatasDir()+"ignore.xml");
00049    if ( this->doc->LoadFile() )
00050    {
00051       this->root = this->doc->FirstChild();
00052    }
00053    else
00054    {
00055       this->initFile();
00056    }
00057 }
00058 
00062 void Ignore::initFile()
00063 {
00064         TiXmlElement born("trustyrc_ignore_list");
00065    this->doc->InsertEndChild(born);
00066    this->root = this->doc->FirstChild();
00067    this->doc->SaveFile();
00068 }
00069 
00076 void Ignore::addIgnore(string mask,string by,unsigned int duration)
00077 {
00078    time_t now;
00079         time(&now);
00080         char date[17];
00081    TiXmlElement item("ignore");
00082    item.SetAttribute("mask",Tools::to_lower(mask));
00083    item.SetAttribute("timestamp",now);
00084         strftime(date,18,"%y-%m-%d %X",localtime(&now));
00085         item.SetAttribute("date",date);
00086    item.SetAttribute("duration",duration);
00087    item.SetAttribute("by",by);
00088    this->root->InsertEndChild(item);
00089    this->doc->SaveFile();
00090 }
00091 
00096 bool Ignore::delIgnore(unsigned int index)
00097 {
00098    bool result;
00099    TiXmlHandle docHandle (this->doc);
00100    TiXmlNode * parent ;
00101    TiXmlElement * elem = docHandle.FirstChild("trustyrc_ignore_list").Child(index).Element() ;
00102    if (elem)
00103    {
00104       parent = elem->Parent();
00105       result = parent->RemoveChild(elem);
00106       this->doc->SaveFile();
00107       return result;
00108    }
00109    return false;
00110 }
00111 
00117 bool Ignore::isIgnored(string host)
00118 {
00119         TiXmlElement * elem = this->root->FirstChildElement();
00120         if ( elem != NULL) {
00121         while (elem != 0) {
00122                         if (Tools::ircMaskMatch(Tools::to_lower(host),Tools::to_lower(elem->Attribute("mask")))) {
00123                                 return true;
00124                         }
00125                 elem = elem->NextSiblingElement();
00126         }
00127                 return false;
00128         }
00129    return false;
00130 }
00131 
00136 vector<string> Ignore::getIgnoreList()
00137 {
00138         char date[17];
00139    time_t futur;
00140    string endIgnore = "";
00141         vector<string> list ;
00142    TiXmlElement * elem = this->root->FirstChildElement();
00143    unsigned int i = 0 ;
00144    while ( elem != NULL )
00145    {
00146       futur = Tools::strToInt(elem->Attribute("timestamp"))+Tools::strToInt(elem->Attribute("duration")) ;
00147       if ( futur > Tools::strToInt(elem->Attribute("timestamp")) ) {
00148          strftime(date,18,"%y-%m-%d %X",localtime(&futur));
00149          endIgnore = " to " +string(date) ;
00150       }
00151       else {
00152          endIgnore = " (permanent)" ;
00153       }
00154                 list.push_back("#"+Tools::intToStr(i)+" "+(string)elem->Attribute("mask")+" on "+(string)elem->Attribute("date")+endIgnore+" "+(string)elem->Attribute("by"));
00155                 elem = elem->NextSiblingElement();
00156       i++;
00157    }  
00158         return list;
00159 }
00160 
00164 void Ignore::purifyList()
00165 {
00166    time_t now;
00167    time(&now);
00168         TiXmlElement * elem;
00169    elem = this->root->FirstChildElement();
00170    while ( elem != NULL )
00171    {
00172       if (((string)elem->Attribute("duration")!="0") && ( (Tools::strToInt(elem->Attribute("timestamp"))+Tools::strToInt(elem->Attribute("duration"))) <= now ))
00173       {
00174          this->root->RemoveChild(elem);
00175       }
00176       elem = elem->NextSiblingElement();
00177    }   
00178    this->doc->SaveFile();
00179 }
00180 
00181 extern "C"
00182 {
00183         Plugin *contruct_ignore(BotKernel*b)
00184         {
00185                 return new Ignore(b);
00186         }
00187         void destroy_ignore(Plugin*p)
00188         {
00189                 delete p;
00190         }
00191    bool isIgnored (Message*m,Plugin*p,BotKernel*b)
00192    {
00193       bool exec = false;
00194       string buffer;
00195       pPlugin * pp = b->getPlugin("admin");
00196       Ignore* im = (Ignore*)p;
00197       Admin * adm ;
00198       if ( pp != NULL )
00199       {
00200          adm = (Admin*) pp->object;
00201       }
00202       else 
00203       {
00204          adm = NULL;  
00205       }    
00206       if(m->isPrivate())
00207       {
00208          if ( m->getSplit().size() == 5 )
00209          {
00210             if ( adm == NULL )
00211             {
00212                exec = true;
00213             }
00214             else
00215             {
00216                if (adm->isSuperAdmin( m->getSender()))
00217                { 
00218                   exec = true;
00219                }
00220                else
00221                {
00222                   exec = false;
00223                }
00224             }
00225             if ( exec )
00226             {
00227                if(im->isIgnored(m->getPart(4)))
00228                {
00229                   b->send(IRCProtocol::sendNotice(m->getNickSender(),"YES"));
00230                }
00231                else
00232                {
00233                   b->send(IRCProtocol::sendNotice(m->getNickSender(),"NO"));
00234                }
00235             }
00236          }
00237       }
00238                 return true;
00239    }
00240    bool addIgnore (Message*m,Plugin*p,BotKernel*b)
00241    {
00242       string time;
00243       bool exec = false;
00244       string buffer;
00245       pPlugin * pp = b->getPlugin("admin");
00246       Ignore* im = (Ignore*)p;
00247       Admin * adm ;
00248       if ( pp != NULL )
00249       {
00250          adm = (Admin*) pp->object;
00251       }
00252       else 
00253       {
00254          adm = NULL;  
00255       }    
00256       if(m->isPrivate())
00257       {
00258          if ( m->getSplit().size() == 6)
00259          {
00260             if ( adm == NULL )
00261             {
00262                exec = true;
00263             }
00264             else
00265             {
00266                if (adm->isSuperAdmin( m->getSender()))
00267                { 
00268                   exec = true;
00269                }
00270                else
00271                {
00272                   exec = false;
00273                }
00274             }
00275             if ( exec )
00276             {
00277                               if ( m->getPart(5).length()>8 )
00278                {
00279                                       time = m->getPart(5).substr(0,8);
00280                }
00281                               else
00282                {
00283                   time = m->getPart(5);
00284                                       im->addIgnore(m->getPart(4),m->getSender(),Tools::strtimeToSeconds(time));
00285                                       b->send(IRCProtocol::sendNotice(m->getNickSender(),m->getPart(4)+" ignored"));
00286                   b->getSysLog()->log(m->getPart(4) + " ignored by "+m->getSender(),INFO);
00287                               }
00288             }
00289          }
00290       }
00291                 return true;
00292    }
00293    bool delIgnore (Message*m,Plugin*p,BotKernel*b)
00294    {
00295       bool exec = false;
00296       string buffer;
00297       pPlugin * pp = b->getPlugin("admin");
00298       Ignore* im = (Ignore*)p;
00299       Admin * adm ;
00300       if ( pp != NULL )
00301       {
00302          adm = (Admin*) pp->object;
00303       }
00304       else 
00305       {
00306          adm = NULL;  
00307       }    
00308       if(m->isPrivate())
00309       {
00310          if ( m->getSplit().size() == 5)
00311          {
00312             if ( adm == NULL )
00313             {
00314                exec = true;
00315             }
00316             else
00317             {
00318                if (adm->isSuperAdmin( m->getSender()))
00319                { 
00320                   exec = true;
00321                }
00322                else
00323                {
00324                   exec = false;
00325                }
00326             }
00327             if ( exec )
00328             {
00329                                    if ( im->delIgnore(Tools::strToInt(m->getPart(4))) )
00330                {
00331                                       b->send(IRCProtocol::sendNotice(m->getNickSender(),"#"+m->getPart(4)+" unignored"));
00332                   b->getSysLog()->log("#"+m->getPart(4) + " unignored by "+m->getSender(),INFO);
00333                }
00334             }
00335          }
00336       }
00337                 return true;
00338    }
00339    bool ignoreList (Message*m,Plugin*p,BotKernel*b)
00340    {
00341       bool exec = false;
00342       string buffer;
00343       pPlugin * pp = b->getPlugin("admin");
00344       Ignore* im = (Ignore*)p;
00345       Admin * adm ;
00346       if ( pp != NULL )
00347       {
00348          adm = (Admin*) pp->object;
00349       }
00350       else 
00351       {
00352          adm = NULL;  
00353       }    
00354       if(m->isPrivate())
00355       {
00356          if ( adm == NULL )
00357          {
00358             exec = true;
00359          }
00360          else
00361          {
00362             if (adm->isSuperAdmin( m->getSender()))
00363             { 
00364                exec = true;
00365             }
00366             else
00367             {
00368                exec = false;
00369             }
00370          }
00371          if ( exec )
00372          {
00373                            b->send(IRCProtocol::sendNotices(m->getNickSender(),im->getIgnoreList()));
00374          }
00375       }
00376                 return true;
00377    }
00378    bool purifyList (Message*m,Plugin*p,BotKernel*b)
00379    {
00380       Ignore* im = (Ignore*)p;
00381       im->purifyList();
00382                 return true;
00383    }
00384         bool testIgnoredUser (Message*m,Plugin*p,BotKernel*b)
00385         {
00386       Ignore* im = (Ignore*)p;
00387            if ((m->getPart(1) == "PRIVMSG")&&(im->isIgnored(m->getSender()) ) ) {
00388          return false;
00389                 }
00390       return true;
00391         }
00392 }

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