advertising.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 "advertising.h"
00030 
00031 extern "C" bool displayAdvertise(Message*,Plugin*,BotKernel*);
00032 
00036 Advertising::Advertising(BotKernel*b)
00037 {
00038         this->author = "eponyme";
00039         this->description = "Display ads";
00040         this->version = VERSION;
00041         this->name = "advertising";
00042    this->bindFunction("addad",IN_COMMAND_HANDLER,"addad",0,10);
00043    this->bindFunction("delad",IN_COMMAND_HANDLER,"delad",0,10);
00044    this->bindFunction("adinfos",IN_COMMAND_HANDLER,"adinfos",0,10);
00045    this->bindFunction("listads",IN_COMMAND_HANDLER,"listads",0,10);
00046         this->bindFunction("60",IN_LOOP,"cleanList",0,10); 
00047    this->addRequirement("admin");
00048 
00049    this->doc = new TiXmlDocument(b->getDatasDir()+"advertising.xml");
00050    if ( this->doc->LoadFile() )
00051    {
00052       this->root = this->doc->FirstChild();
00053    }
00054    else
00055    {
00056       this->initFile();
00057    }
00058    TiXmlHandle docHandle (this->doc);
00059    TiXmlElement * elem = docHandle.FirstChild("trustyrc_advertising").Element();
00060    for (elem=elem->FirstChildElement() ; elem!=0;elem=elem->NextSiblingElement() ) {
00061       this->launchAdvertise(b,elem->ValueStr().substr(2),Tools::strToInt(elem->Attribute("frequency")));
00062    }
00063 }
00064 
00068 void Advertising::initFile()
00069 {
00070         TiXmlElement born("trustyrc_advertising");
00071    this->doc->InsertEndChild(born);
00072    this->root = this->doc->FirstChild();
00073    this->doc->SaveFile();
00074 }
00075 
00082 void Advertising::launchAdvertise(BotKernel*b,string id, unsigned int freq)
00083 {
00084    Message m(id);
00085    b->addCountDown(this,displayAdvertise,&m,freq,10);
00086 }
00087 
00097 time_t Advertising::addAdvertise(string channel,unsigned int frequency,unsigned int until,string by,string text)
00098 {
00099    time_t now,back;
00100    time(&now);
00101    back = now;
00102    if (!this->adExists(Tools::intToStr(now)) )
00103    {
00104            char date[17];
00105       TiXmlElement item("ad"+Tools::intToStr(now));
00106       item.SetAttribute("channel",channel);
00107       item.SetAttribute("frequency",frequency);
00108       item.SetAttribute("until",until);
00109            strftime(date,18,"%y-%m-%d %X",localtime(&now));
00110            item.SetAttribute("date",date);
00111       item.SetAttribute("by",by);
00112       TiXmlText textItem(text);
00113       item.InsertEndChild(textItem);
00114       this->root->InsertEndChild(item);
00115       this->doc->SaveFile();
00116       return back;
00117    }
00118    else {
00119       return 0;
00120    }
00121 }
00122 
00128 bool Advertising::delAdvertise(string id)
00129 {
00130    bool result;
00131    TiXmlNode * parent ;
00132    TiXmlHandle docHandle (this->doc);
00133    TiXmlElement * elem = docHandle.FirstChild().FirstChild("ad"+id).Element() ;
00134    if (elem) {
00135       parent = elem->Parent();
00136       result = parent->RemoveChild(elem);
00137       this->doc->SaveFile();
00138       return result;
00139    }
00140    return false;
00141 }
00142 
00148 bool Advertising::adExists(string id)
00149 {
00150    TiXmlHandle docHandle (this->doc);
00151    TiXmlElement * elem = docHandle.FirstChild().FirstChild("ad"+id).Element() ;
00152    if (elem) {
00153       return true;
00154    }
00155    return false;
00156 }
00157 
00169 vector<string> Advertising::getAdvertiseInfos(string id)
00170 {
00171    vector<string> infos;
00172    infos.clear();
00173    TiXmlHandle docHandle (this->doc);
00174    TiXmlElement * elem = docHandle.FirstChild().FirstChild("ad"+id).Element() ;
00175    if (elem) {
00176       infos.push_back(elem->Attribute("channel"));
00177       infos.push_back(elem->Attribute("frequency"));
00178       infos.push_back(elem->Attribute("until"));
00179       infos.push_back(elem->Attribute("date"));
00180       infos.push_back(elem->Attribute("by"));
00181       infos.push_back(elem->GetText());
00182    }
00183    return infos;
00184 }
00185 
00190 vector<string> Advertising::getAdvertisesList() 
00191 {
00192    char date[17];
00193    time_t futur;
00194    vector<string> list;
00195    TiXmlHandle docHandle (this->doc);
00196    TiXmlElement * elem = docHandle.FirstChild("trustyrc_advertising").Element();
00197    for (elem=elem->FirstChildElement() ; elem!=0;elem=elem->NextSiblingElement() ) {
00198       futur = Tools::strToInt(elem->ValueStr().substr(2))+Tools::strToInt(elem->Attribute("until")) ;
00199       strftime(date,18,"%y-%m-%d %X",localtime(&futur));
00200       list.push_back(elem->ValueStr().substr(2)+" : "+elem->GetText()+ " ("+(string)date+")");
00201    }
00202    return list;
00203 }
00204 
00208 void Advertising::deleteOutdatedAds() 
00209 {
00210    time_t now;
00211    time(&now);
00212    TiXmlNode * parent ;
00213    TiXmlHandle docHandle (this->doc);
00214    TiXmlElement * elem = docHandle.FirstChild("trustyrc_advertising").Element();
00215    for (elem=elem->FirstChildElement() ; elem!=0;elem=elem->NextSiblingElement() ) {
00216       if ( (Tools::strToInt(elem->ValueStr().substr(2)) + Tools::strToInt(elem->Attribute("until"))) <= now ) {
00217          parent = elem->Parent();
00218          parent->RemoveChild(elem);
00219       }   
00220    }
00221    this->doc->SaveFile();
00222 }
00223 
00224 extern "C"
00225 {
00226         Plugin *contruct_advertising(BotKernel*b)
00227         {
00228                 return new Advertising(b);
00229         }
00230         void destroy_advertising(Plugin*p)
00231         {
00232                 delete p;
00233         }
00234    bool displayAdvertise(Message*m,Plugin*p,BotKernel*b)
00235    {
00236       Advertising*pAd = (Advertising*) p;
00237       vector<string> infos = pAd->getAdvertiseInfos(m->getMessage());
00238       if ( infos.size() > 0 ) {
00239          b->send(IRCProtocol::sendMsg(infos[0],infos[5]));
00240          return false;
00241       }
00242       return true;
00243    }
00244         bool addad (Message*m,Plugin*p,BotKernel*b)
00245         {  
00246       unsigned int timestamp;
00247       Message msg;
00248       pPlugin * ppAdm = b->getPlugin("admin");
00249       Admin*adm = NULL;
00250       Advertising*pAd = (Advertising*) p;
00251       if ((ppAdm!=NULL) && m->isPrivate() && (m->nbParts()>=8) ) {
00252          adm = (Admin*)ppAdm->object ;
00253          if (adm->isSuperAdmin(m->getSender()) ) {
00254             timestamp = pAd->addAdvertise(m->getPart(4),Tools::strtimeToSeconds(m->getPart(5)),Tools::strtimeToSeconds(m->getPart(6)),m->getSender(),Tools::vectorToString(m->getSplit()," ",7)) ;
00255             if (timestamp> 0) { 
00256                msg.setMessage(Tools::intToStr(timestamp)); 
00257                if(b->addCountDown(p,displayAdvertise,&msg,Tools::strtimeToSeconds(m->getPart(5)),10)==NULL) {
00258                   b->send(IRCProtocol::sendNotice(m->getNickSender(),"ERROR : the ad is registred but couldn't be launched")) ;
00259                }
00260                else {
00261                   b->send(IRCProtocol::sendNotice(m->getNickSender(),"Advertise added and launched"));
00262                }
00263             }
00264             else {
00265                b->send(IRCProtocol::sendNotice(m->getNickSender(),"Error, try again in a few seconds"));
00266             }
00267          }
00268       }
00269                 return true;
00270         }
00271         bool delad (Message*m,Plugin*p,BotKernel*b)
00272         {
00273       pPlugin * ppAdm = b->getPlugin("admin");
00274       Admin*adm = NULL;
00275       Advertising*pAd = (Advertising*) p;
00276       if ((ppAdm!=NULL) && m->isPrivate() && (m->nbParts()==5) ) {
00277          adm = (Admin*)ppAdm->object ;
00278          if (adm->isSuperAdmin(m->getSender()) ) {
00279             if ( pAd->delAdvertise(m->getPart(4)) ) {
00280                b->send(IRCProtocol::sendNotice(m->getNickSender(),"Advertise deleted"));
00281             }
00282             else {
00283                b->send(IRCProtocol::sendNotice(m->getNickSender(),"ERROR : no advertise deleted"));
00284             }
00285          }
00286       }
00287                 return true;
00288         }
00289         bool adinfos (Message*m,Plugin*p,BotKernel*b)
00290         {
00291            char date[17];
00292       time_t futur;
00293       vector<string> infos;
00294       pPlugin * ppAdm = b->getPlugin("admin");
00295       Admin*adm = NULL;
00296       Advertising*pAd = (Advertising*) p;
00297       if ((ppAdm!=NULL) && m->isPrivate() && (m->nbParts()==5) ) {
00298          adm = (Admin*)ppAdm->object ;
00299          if (adm->isSuperAdmin(m->getSender()) ) {
00300             infos = pAd->getAdvertiseInfos(m->getPart(4));
00301             if ( infos.size() > 0 ) {
00302                futur = Tools::strToInt(m->getPart(4))+Tools::strToInt(infos[2]) ;
00303                strftime(date,18,"%y-%m-%d %X",localtime(&futur));
00304                b->send(IRCProtocol::sendNotice(m->getNickSender(),"channel: "+infos[0]+" frequency : "+infos[1]+" from "+infos[4]+" on "+infos[3]+" to "+(string) date));
00305                b->send(IRCProtocol::sendNotice(m->getNickSender(),"text : "+infos[5]));
00306             }
00307             else {
00308                b->send(IRCProtocol::sendNotice(m->getNickSender(),"ERROR : advertise not found"));
00309             }
00310          }
00311       }
00312                 return true;
00313         }
00314         bool listads (Message*m,Plugin*p,BotKernel*b)
00315         {
00316       pPlugin * ppAdm = b->getPlugin("admin");
00317       Admin*adm = NULL;
00318       Advertising*pAd = (Advertising*) p;
00319       if ((ppAdm!=NULL) && m->isPrivate() ) {
00320          adm = (Admin*)ppAdm->object ;
00321          if (adm->isSuperAdmin(m->getSender()) ) {
00322             b->send(IRCProtocol::sendNotices(m->getNickSender(),pAd->getAdvertisesList()));
00323          }
00324       }
00325                 return true;
00326         }
00327    bool cleanList(Message*m,Plugin*p,BotKernel*b)
00328    {
00329       Advertising*pAd = (Advertising*) p;
00330       pAd->deleteOutdatedAds();
00331       return true;
00332    }
00333 }

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