quotes.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 "quotes.h"
00030 
00034 Quotes::Quotes(BotKernel*b)
00035 {
00036         this->author = "eponyme";
00037         this->description = "Quote storage and access";
00038         this->version = VERSION;
00039         this->name = "quotes";
00040         this->bindFunction("quote",IN_COMMAND_HANDLER,"quote",0,10);
00041         this->bindFunction("addquote",IN_COMMAND_HANDLER,"addQuote",0,10);
00042         this->bindFunction("delquote",IN_COMMAND_HANDLER,"delQuote",0,10);
00043         this->bindFunction("searchquote",IN_COMMAND_HANDLER,"searchQuote",0,10);
00044         this->bindFunction("quoteinfos",IN_COMMAND_HANDLER,"quoteInfos",0,10);
00045         this->bindFunction("lastquote",IN_COMMAND_HANDLER,"lastQuote",0,10);
00046    this->addRequirement("admin");
00047 
00048    this->doc = new TiXmlDocument(b->getDatasDir()+"quotes.xml");
00049    if ( this->doc->LoadFile() ) {
00050       this->root = this->doc->FirstChild();
00051    }
00052    else {
00053       TiXmlElement born("trustyrc_quotes");
00054       this->doc->InsertEndChild(born);
00055       this->root = this->doc->FirstChild();
00056       this->doc->SaveFile();
00057    }
00058    this->nbQuotes = this->getNbChilds(this->root);
00059 }
00060 
00066 unsigned int Quotes::getNbChilds(TiXmlNode*node)
00067 {
00068    TiXmlNode * child = node->FirstChild();
00069    if ( child != NULL) {
00070       int i = 0 ;
00071       while (child != 0)
00072       {
00073          child = child->NextSibling();
00074          i += 1 ; 
00075       }
00076       return i ;
00077    }
00078    else {
00079       return 0;
00080    }
00081 }
00082 
00088 void Quotes::addQuote(string host,string quote)
00089 {
00090    time_t now;
00091    time(&now);
00092         char timeFormat[17];
00093         strftime(timeFormat,18,"%y-%m-%d %X",localtime(&now));
00094    TiXmlElement item( "quote" );
00095    item.SetAttribute( "from", host );
00096    item.SetAttribute( "date", timeFormat );
00097    TiXmlText text(quote);
00098    item.InsertEndChild(text);
00099    this->root->InsertEndChild(item);
00100    this->doc->SaveFile();
00101    this->nbQuotes ++ ;
00102 }
00103 
00109 string Quotes::getQuote(unsigned int index)
00110 {
00111    TiXmlHandle docHandle (this->doc);
00112    TiXmlElement * elem = docHandle.FirstChild().Child(index-1).ToElement() ;
00113    if ( this->nbQuotes > 0 ) {
00114       if(elem && (index>=1)) {
00115          return("["+Tools::intToStr(index)+"/"+Tools::intToStr(this->nbQuotes)+"] => "+elem->GetText());
00116       }
00117       else {
00118          return "* Inexistent quote. There are "+Tools::intToStr(this->nbQuotes)+" quotes (from 1 to "+Tools::intToStr(this->nbQuotes)+") *";
00119       }
00120    }
00121    else {
00122       return "* No recorded quotes *";
00123    }
00124 }
00125 
00130 string Quotes::getRandomQuote() 
00131 {
00132    if ( this->nbQuotes > 0 ) {
00133       return(this->getQuote(Tools::random(1,this->nbQuotes)));
00134    }
00135    else {
00136       return "* No recorded quotes *";
00137    }
00138 }
00139 
00145 vector<string> Quotes::searchQuote(string pattern)
00146 {
00147    vector<string> answer;
00148    TiXmlHandle docHandle (this->doc);
00149         string quotes_nums = "";
00150         string buffer = "";
00151    unsigned int i = 1;
00152    if (pattern.length() >= 3) {
00153            for(TiXmlElement*child = docHandle.FirstChild().FirstChild().ToElement(); child!=0; child=child->NextSiblingElement()) {
00154          buffer = child->GetText();
00155          if ( Tools::to_lower(buffer).find(Tools::to_lower(pattern),0) != string::npos ) {
00156             quotes_nums += Tools::intToStr(i)+" ";
00157             answer.push_back("["+Tools::intToStr(i)+"/"+Tools::intToStr(this->nbQuotes)+"] => " + buffer) ;
00158          }
00159          i++;
00160            }
00161       if (answer.size() > 0 ) {
00162          buffer = answer[Tools::random(0,answer.size()-1)];
00163          answer.clear();
00164          answer.push_back(buffer);
00165          answer.push_back("* other matches => " + quotes_nums+ " *");
00166       }
00167       else {
00168          answer.push_back("* No match found *");
00169       }
00170    }
00171    else {
00172       answer.push_back("* pattern must contain at least of 3 chars *");      
00173    }
00174    return answer;
00175 }
00176 
00182 bool Quotes::delQuote(unsigned int index)
00183 {
00184    TiXmlHandle docHandle (this->doc);
00185    TiXmlElement*elem = docHandle.FirstChild().Child(index-1).ToElement() ;
00186    if (elem) {
00187       this->root->RemoveChild(elem);
00188       this->nbQuotes --;
00189       this->doc->SaveFile();
00190       return true;
00191    }
00192    else {
00193       return false;
00194    }
00195 }
00196 
00201 string Quotes::getLastQuote()
00202 {
00203    return this->getQuote(this->nbQuotes);
00204 }
00205 
00211 string Quotes::quoteInfos(unsigned int index)
00212 {
00213    TiXmlHandle docHandle (this->doc);
00214    TiXmlElement*elem = docHandle.FirstChild().Child(index-1).ToElement() ;
00215    if (elem) {
00216       return ("date:"+(string)elem->Attribute("date")+" from:"+(string)elem->Attribute("from"));
00217    }
00218    else {
00219       return "* Inexistent quote *";
00220    }
00221 }
00222 
00223 extern "C"
00224 {
00225         Plugin *contruct_quotes(BotKernel*b)
00226         {
00227                 return new Quotes(b);
00228         }
00229         void destroy_quotes(Plugin*p)
00230         {
00231                 delete p;
00232         }
00233         bool quote (Message*m,Plugin*p,BotKernel*b) 
00234    {
00235       Quotes*q = (Quotes*)p;
00236       if (m->isPublic() ) {
00237          if (m->nbParts() == 4 ) {
00238             b->send(IRCProtocol::sendMsg(m->getSource(),q->getRandomQuote()));
00239          }
00240          else {
00241             b->send(IRCProtocol::sendMsg(m->getSource(),q->getQuote(Tools::strToInt(m->getPart(4)))));
00242          }
00243       }
00244       return true;
00245    }
00246         bool addQuote (Message*m,Plugin*p,BotKernel*b) 
00247    {
00248       Quotes*q = (Quotes*)p;
00249       if (m->isPublic() && (m->nbParts()>4) ) {
00250          q->addQuote(m->getSender(),Tools::vectorToString(m->getSplit()," ",4));
00251          b->send(IRCProtocol::sendNotice(m->getNickSender(),"* quote added *"));
00252       }
00253       return true;
00254    }
00255         bool delQuote (Message*m,Plugin*p,BotKernel*b) 
00256    {
00257       pPlugin * ppAdm = b->getPlugin("admin");
00258       Admin*adm = NULL;
00259       Quotes*q = (Quotes*)p;
00260       if ((ppAdm!=NULL) && m->isPublic() && (m->nbParts()==5) ) {
00261          adm = (Admin*)ppAdm->object ;
00262          if (adm->isSuperAdmin(m->getSender()) ) {
00263             if (q->delQuote(Tools::strToInt(m->getPart(4)))) {
00264                b->send(IRCProtocol::sendNotice(m->getNickSender(),"* Quote deleted *"));
00265             }
00266             else {
00267                b->send(IRCProtocol::sendNotice(m->getNickSender(),"* Error *"));
00268             }
00269          }
00270       }
00271       return true;
00272    }
00273         bool searchQuote (Message*m,Plugin*p,BotKernel*b) 
00274    {
00275       Quotes*q = (Quotes*)p;
00276       if ( m->isPublic() && (m->nbParts() > 4)) {
00277          b->send(IRCProtocol::sendMsg(m->getSource(),q->searchQuote(Tools::vectorToString(m->getSplit()," ",4))));
00278       }
00279       return true;
00280    }
00281         bool quoteInfos (Message*m,Plugin*p,BotKernel*b) 
00282    {
00283       pPlugin * ppAdm = b->getPlugin("admin");
00284       Admin*adm = NULL;
00285       Quotes*q = (Quotes*)p;
00286       if ((ppAdm!=NULL) && m->isPublic() && (m->nbParts()==5) ) {
00287          adm = (Admin*)ppAdm->object ;
00288          if (adm->isSuperAdmin(m->getSender()) ) {
00289             b->send(IRCProtocol::sendNotice(m->getNickSender(),q->quoteInfos(Tools::strToInt(m->getPart(4)))));
00290          }
00291       }
00292       return true;
00293    }
00294         bool lastQuote (Message*m,Plugin*p,BotKernel*b) 
00295    {
00296       Quotes*q = (Quotes*)p;
00297       if (m->isPublic() ) {
00298          b->send(IRCProtocol::sendMsg(m->getSource(),q->getLastQuote()));
00299       }
00300       return true;
00301    }
00302 }

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